# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-delegated-capability-replay-forks-from-silently-diverging-audit-trails-across-federated-agent-trust-brokers.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-delegated-capability-replay-forks-from-silently-diverging-audit-trails-across-federated-agent-trust-brokers.warrant.json
# Markdown SHA-256:   13c29ee6fff936e0eb712a2096782d8d2d2e1fec29f12e2b86f905c8c63230b9
# Code AST SHA-256:   ff07faa81f779f914d3472594d1f5adea81e27efc01f8ec9886b36362d6ebf9b
# Verified Classes:   DelegationForkError, Warrant, BrokerState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class DelegationForkError(RuntimeError):
    """Raised when a delegated warrant cannot be resolved to a single
    effective scope agreed upon by broker epoch and verifier pin."""
    pass


@dataclass
class Warrant:
    subject: str
    scope: frozenset[str]
    parent_hash: str
    broker_epoch: int
    signature: str = "sig"


@dataclass
class BrokerState:
    # lineage: parent_hash -> latest epoch at which that lineage was attenuated
    attenuation_epoch: dict[str, int] = field(default_factory=dict)
    current_epoch: int = 0

    def attenuate(self, parent_hash: str, new_epoch: int) -> None:
        self.attenuation_epoch[parent_hash] = new_epoch
        self.current_epoch = max(self.current_epoch, new_epoch)


def resolve_effective_scope(
    warrant: Warrant,
    broker: BrokerState,
    verifier_pinned_epoch: Optional[int],
) -> frozenset[str]:
    """Return the scope both verifiers must agree on, or raise if unresolvable."""
    if verifier_pinned_epoch is None:
        # No pin recorded: cannot prove agreement across orgs.
        raise DelegationForkError(
            f"warrant for {warrant.subject} has no verifier epoch pin; "
            "audit trail cannot be reconciled across orgs"
        )

    latest = broker.attenuation_epoch.get(warrant.parent_hash, 0)

    if warrant.broker_epoch < latest and verifier_pinned_epoch < latest:
        # Stale on both sides: the warrant predates an attenuation neither
        # verifier has observed. Effective scope is indeterminate.
        raise DelegationForkError(
            f"warrant epoch {warrant.broker_epoch} precedes parent attenuation "
            f"epoch {latest} for parent {warrant.parent_hash}; "
            "scope is INDETERMINATE, not VALID"
        )

    if verifier_pinned_epoch != warrant.broker_epoch:
        # Pin disagrees with the warrant's mint epoch: replay fork.
        raise DelegationForkError(
            f"replay fork: verifier pinned epoch {verifier_pinned_epoch} != "
            f"warrant broker_epoch {warrant.broker_epoch}"
        )

    return warrant.scope


def authorize(warrant: Warrant, broker: BrokerState, pin: Optional[int]) -> frozenset[str]:
    return resolve_effective_scope(warrant, broker, pin)
