# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-did-issuer-trust-chain-break-during-cross-org-a2a-handshake-before-the-receiving-agent-commits-to-a-delegated-warra.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-did-issuer-trust-chain-break-during-cross-org-a2a-handshake-before-the-receiving-agent-commits-to-a-delegated-warra.warrant.json
# Markdown SHA-256:   87d71452ff0e2f7d635bb9fd5d43191740d7deea298f40885b2b48f90b8385ae
# Code AST SHA-256:   d51aceb28378dbdc3395864e5819b7b5d830d90e5e8de3f6572c6f0a59a35fae
# Verified Classes:   TrustChainUnanchoredError, VerifiedChain, Receiver
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class TrustChainUnanchoredError(RuntimeError):
    """Raised when a VC chain verifies cryptographically but does not
    terminate in an anchor the receiver has independently vouched for."""


@dataclass
class VerifiedChain:
    signing_did: str
    terminal_anchor: Optional[str]          # last controller/alsoKnownAs in chain
    subject_did: str
    controller_edges_are_vc_backed: bool
    signatures_valid: bool


@dataclass
class Receiver:
    trust_anchors: set = field(default_factory=set)   # org DIDs beta vouched for


def mint_warrant(chain: VerifiedChain, receiver: Receiver) -> dict:
    # Signature validity is necessary, never sufficient.
    if not chain.signatures_valid:
        raise TrustChainUnanchoredError(
            f"signature verification failed for {chain.signing_did}"
        )

    # Invariant 1: chain must terminate in a receiver-anchored org DID.
    if chain.terminal_anchor is None or chain.terminal_anchor not in receiver.trust_anchors:
        raise TrustChainUnanchoredError(
            "terminal anchor "
            f"{chain.terminal_anchor!r} not in receiver trust set "
            f"{sorted(receiver.trust_anchors)!r}; refusing to mint warrant"
        )

    # Invariant 2: controller edges must be VC/registry-backed, not bare
    # self-published DID-document fields.
    if not chain.controller_edges_are_vc_backed:
        raise TrustChainUnanchoredError(
            f"controller edges for {chain.signing_did} are self-asserted, "
            "not VC-backed; provenance unproven"
        )

    # Invariant 3: subject must be org-bound; bare did:key cannot be revoked.
    if chain.subject_did.startswith("did:key:"):
        raise TrustChainUnanchoredError(
            f"subject {chain.subject_did} is a bare key DID with no org backlink; "
            "delegation would be unrevocable and unattributable"
        )

    # Pin the ANCHOR (not the signing DID) into the warrant for audit/revocation.
    return {
        "anchored_org": chain.terminal_anchor,
        "signing_did": chain.signing_did,
        "subject_did": chain.subject_did,
        "revocable_against": chain.terminal_anchor,
    }
