# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-agent-svid-re-issue-when-the-federation-trust-bundle-is-stale-but-the-delegated-token-chain-is-still.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-an-agent-svid-re-issue-when-the-federation-trust-bundle-is-stale-but-the-delegated-token-chain-is-still.warrant.json
# Markdown SHA-256:   dbee12e630d425135876d590c325626f6d11681639d8355ec55d808c149812c0
# Code AST SHA-256:   f163a7c9fe8a4952644358b5e576fd6e095a69d400051254b072ee73b672718b
# Verified Classes:   StaleFederationBundleError, TrustBundle, AgentSVID
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Set


class StaleFederationBundleError(RuntimeError):
    """Raised when re-issue would mint an SVID orphaned from the current trust bundle."""
    pass


@dataclass(frozen=True)
class TrustBundle:
    version: int
    refresh_time: float
    trust_anchors: Set[str]


@dataclass(frozen=True)
class AgentSVID:
    not_before: float
    trust_anchor: str
    delegated_token_chain_valid: bool


def must_refuse_reissue(
    current_svid: AgentSVID,
    local_bundle: TrustBundle,
    fetched_bundle: TrustBundle,
    proposed_not_before: float,
) -> None:
    # Condition 1: delegated chain still verifies.
    if not current_svid.delegated_token_chain_valid:
        return  # ordinary auth failure path, not this boundary

    # Condition 2: fetched bundle is genuinely newer than local cache.
    if fetched_bundle.refresh_time <= local_bundle.refresh_time:
        return  # nothing to act on; local cache is current

    # Condition 3: the anchor required by attestation was rotated OUT (not augmented).
    anchor_rotated_out = current_svid.trust_anchor not in fetched_bundle.trust_anchors

    # Condition 4: the new SVID would postdate the bundle refresh.
    would_postdate = proposed_not_before > fetched_bundle.refresh_time

    if anchor_rotated_out and would_postdate:
        raise StaleFederationBundleError(
            f"Refusing re-issue: attesting anchor {current_svid.trust_anchor!r} "
            f"absent from bundle v{fetched_bundle.version} "
            f"(refresh_time={fetched_bundle.refresh_time}); "
            f"proposed NotBefore={proposed_not_before} would orphan the SVID. "
            f"Retry after bundle propagation."
        )
