# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-re-signs-a-delegated-warrant-to-cross-a-federation-boundary-and-silently-severs-the-provenance-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-re-signs-a-delegated-warrant-to-cross-a-federation-boundary-and-silently-severs-the-provenance-chain.warrant.json
# Markdown SHA-256:   e5f27aec19d33c959b0dd27201d47b21828ce2661ad4a1721af09c025effafe8
# Code AST SHA-256:   6f8a89c41971c661a50bd7f84b42fa412a3bd9487e20479e86f6753e7b3eba4d
# Verified Classes:   ProvenanceSeveredError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ProvenanceSeveredError(RuntimeError):
    """Raised when a warrant's provenance chain does not terminate at a
    recognized origin attestation, indicating a severed/re-issued link."""
    pass


@dataclass
class Warrant:
    warrant_id: str
    issuer_root: str
    parent_hash: Optional[str]   # None == explicit root; "SEVERED" == broken link
    origin_attestation: Optional[str]
    arrived_via_delegation: bool  # True if received in a delegation envelope


def verify_provenance_chain(
    terminal: Warrant,
    chain: dict[str, Warrant],
    trusted_roots: set[str],
) -> str:
    """Walk parent links from terminal to root. Return the origin root id.
    Raise ProvenanceSeveredError if the chain is broken or unattested."""
    seen: set[str] = set()
    current = terminal
    while True:
        if current.warrant_id in seen:
            raise ProvenanceSeveredError(
                f"cycle detected at {current.warrant_id}"
            )
        seen.add(current.warrant_id)

        if current.parent_hash in (None, "SEVERED"):
            # A null parent is only a valid root if it is a genuine local
            # origin (not a delegation artifact) AND carries a trusted
            # origin attestation.
            if current.arrived_via_delegation:
                raise ProvenanceSeveredError(
                    f"warrant {current.warrant_id} has null parent but arrived "
                    f"via delegation: translation artifact, chain severed"
                )
            if current.origin_attestation not in trusted_roots:
                raise ProvenanceSeveredError(
                    f"root {current.warrant_id} lacks trusted origin attestation "
                    f"(got {current.origin_attestation!r})"
                )
            return current.issuer_root

        parent = chain.get(current.parent_hash)
        if parent is None:
            raise ProvenanceSeveredError(
                f"parent {current.parent_hash} of {current.warrant_id} missing "
                f"from chain: severed link"
            )
        current = parent


def execute_export(warrant: Warrant, chain: dict[str, Warrant],
                   trusted_roots: set[str]) -> str:
    origin = verify_provenance_chain(warrant, chain, trusted_roots)
    # Only reached if the chain is intact and rooted in a trusted origin.
    return f"export executed; provenance root = {origin}"
