# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-capability-token-whose-issuer-chain-crosses-an-organizational-attestation-boundary-mid-flight.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-capability-token-whose-issuer-chain-crosses-an-organizational-attestation-boundary-mid-flight.warrant.json
# Markdown SHA-256:   38d509d223c9ad1fa943c9cf674761ed50ff482bef469836d252b3f5f2760f26
# Code AST SHA-256:   e330db8e683baf482cd2589971d23b43ec4cf44f57d5f2bf67a2377f855c1acc
# Verified Classes:   AttestationBoundaryError, DelegatedCapabilityToken, PresentingInstance, ReAttestationAssertion
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class AttestationBoundaryError(RuntimeError):
    """Raised when a delegated capability token is presented under an
    attestation root that was not part of the original delegation grant,
    and no fresh re-attestation assertion from the delegating org exists."""
    pass


@dataclass(frozen=True)
class DelegatedCapabilityToken:
    issuer_org: str
    subject_org: str
    capability: str
    minted_attestation_root: str  # hash of root present at delegation time
    ttl_seconds: int


@dataclass(frozen=True)
class PresentingInstance:
    org: str
    current_attestation_root: str


@dataclass(frozen=True)
class ReAttestationAssertion:
    issuer_org: str
    named_root: str
    signature_valid: bool


def verify_cross_org_capability(
    token: DelegatedCapabilityToken,
    instance: PresentingInstance,
    verifier_org: str,
    re_attestation: ReAttestationAssertion | None,
) -> bool:
    if token.subject_org != verifier_org:
        raise AttestationBoundaryError(
            f"token delegated to {token.subject_org}, verifier is {verifier_org}"
        )
    if instance.org != token.issuer_org:
        raise AttestationBoundaryError(
            f"instance attested under {instance.org}, token issuer is {token.issuer_org}"
        )

    minted = token.minted_attestation_root
    present = instance.current_attestation_root

    if present == minted:
        return True

    # Root drift detected across the org boundary. Fail closed unless the
    # delegating org has explicitly re-attested the presenting root.
    if re_attestation is None:
        raise AttestationBoundaryError(
            "attestation root drift with no re-attestation assertion: "
            f"minted={minted[:12]}... present={present[:12]}..."
        )
    if not re_attestation.signature_valid:
        raise AttestationBoundaryError("re-attestation assertion signature invalid")
    if re_attestation.issuer_org != token.issuer_org:
        raise AttestationBoundaryError(
            f"re-attestation issued by {re_attestation.issuer_org}, "
            f"expected {token.issuer_org}"
        )
    if re_attestation.named_root != present:
        raise AttestationBoundaryError(
            "re-attestation does not name the presenting root: "
            f"named={re_attestation.named_root[:12]}... present={present[:12]}..."
        )
    return True
