# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-cross-domain-agent-identity-collapse-when-delegated-warrants-outlive-their-issuing-trust-domain.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-cross-domain-agent-identity-collapse-when-delegated-warrants-outlive-their-issuing-trust-domain.warrant.json
# Markdown SHA-256:   f5b860b49cda7f2477fe3a00d15725a08e2ee1a0036647f9e39adcdd8ba036d0
# Code AST SHA-256:   202d7effdfde01b7955aacba837b7e0b7cac5d80b0ba60796ad6dfaceda235b8
# Verified Classes:   IdentityCollapseError, Warrant, Resolution
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class IdentityCollapseError(RuntimeError):
    """Raised when a foreign warrant cannot be resolved to exactly one
    fresh, issuer-signed local principal. Fails closed by design."""


@dataclass
class Warrant:
    issuer: str
    subject: str
    max_resolution_age_s: int
    signature_valid: bool


@dataclass
class Resolution:
    local_principal: Optional[str]
    age_s: int
    signed_by_issuer: bool


def resolve_cross_domain(
    warrant: Warrant,
    cached: Optional[Resolution],
    resolve_fn,  # callable(issuer, subject) -> Resolution
) -> str:
    if not warrant.signature_valid:
        raise IdentityCollapseError(
            f"warrant from {warrant.issuer} has invalid signature; "
            "signature validity is necessary but not sufficient"
        )

    # A cache hit is an optimization, never an authorization basis.
    # Honor it only if it is within the bound the ISSUER asserted.
    if cached is not None and cached.age_s <= warrant.max_resolution_age_s:
        if not cached.signed_by_issuer:
            raise IdentityCollapseError(
                f"cached mapping for {warrant.subject} not signed by "
                f"{warrant.issuer}; refusing to authorize on unsigned cache"
            )
        if cached.local_principal is None:
            # Zero mapping is unresolvable identity, not a guest default.
            raise IdentityCollapseError(
                f"cached mapping for {warrant.subject} is empty; "
                "zero mappings must fail closed, never default"
            )
        return cached.local_principal

    # Cache miss or stale: re-resolve against the issuer.
    fresh = resolve_fn(warrant.issuer, warrant.subject)

    if not fresh.signed_by_issuer:
        raise IdentityCollapseError(
            f"fresh resolution for {warrant.subject} not signed by issuer"
        )
    if fresh.age_s > warrant.max_resolution_age_s:
        raise IdentityCollapseError(
            f"resolution for {warrant.subject} is {fresh.age_s}s old, "
            f"exceeds issuer-asserted bound {warrant.max_resolution_age_s}s"
        )
    if fresh.local_principal is None:
        raise IdentityCollapseError(
            f"subject {warrant.subject} resolves to zero principals; "
            "unresolvable identity must reject, not downgrade to guest"
        )
    # Multi-mapping detection is delegated to resolve_fn, which must
    # return None (ambiguous) rather than first-match; None is caught
    # by the zero-mapping branch above and fails closed.
    return fresh.local_principal
