# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-trust-anchor-staleness-during-cross-org-agent-federation-handoff-and-why-revocation-propagation-cannot-be-inferred-fr.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-trust-anchor-staleness-during-cross-org-agent-federation-handoff-and-why-revocation-propagation-cannot-be-inferred-fr.warrant.json
# Markdown SHA-256:   5fe17091f5362904b5d2fdbb88a8c721943df09abc58788cb2f2af2fb650e0e7
# Code AST SHA-256:   52f9f8a530d2d9923a83b7986862f667d147a3125115352b06895de7f362a5ef
# Verified Classes:   AnchorStalenessError, Warrant, AnchorResolution
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Mapping


class AnchorStalenessError(RuntimeError):
    """Raised when a warrant's trust chain cannot be shown sound at verification time."""


@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    anchor_handle: str          # resolvable, revocable handle (NOT a bare fingerprint)
    embedded_fingerprint: str   # fingerprint at mint time
    ttl_seconds: int


@dataclass(frozen=True)
class AnchorResolution:
    fingerprint: str
    revoked: bool
    revocation_set_version: int
    resolution_timestamp: float


def mint_warrant(
    issuer: str,
    subject: str,
    anchor_handle: str,
    embedded_fingerprint: str,
    ttl_seconds: int,
    propagation_lag_by_party: Mapping[str, float],
    cache_horizon_by_party: Mapping[str, float],
) -> Warrant:
    # Soundness invariant: TTL must be strictly less than the slowest
    # revocation path to any party that may verify or countersign.
    slowest_path = max(
        propagation_lag_by_party[p] + cache_horizon_by_party[p]
        for p in propagation_lag_by_party
    )
    if ttl_seconds >= slowest_path:
        raise AnchorStalenessError(
            f"warrant ttl={ttl_seconds}s >= slowest revocation path {slowest_path}s; "
            f"chain is unsound by construction"
        )
    return Warrant(issuer, subject, anchor_handle, embedded_fingerprint, ttl_seconds)


def verify_warrant(
    warrant: Warrant,
    resolved: AnchorResolution,
    verifier_name: str,
) -> None:
    # Peer liveness is deliberately NOT an input here. Liveness != anchor freshness.
    if resolved.revoked:
        raise AnchorStalenessError(
            f"{verifier_name}: anchor {warrant.anchor_handle} revoked "
            f"(revocation_set_version={resolved.revocation_set_version})"
        )
    if resolved.fingerprint != warrant.embedded_fingerprint:
        # The warrant was minted under an anchor that has since rotated or been
        # superseded. Presence of *an* anchor is not equality with the *embedded* one.
        raise AnchorStalenessError(
            f"{verifier_name}: anchor fingerprint divergence — warrant embedded "
            f"{warrant.embedded_fingerprint}, live set resolves "
            f"{resolved.fingerprint}; warrant is stale"
        )
