# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-capability-attestation-it-can-no-longer-verify-after-the-trust-anchor-rotates.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-capability-attestation-it-can-no-longer-verify-after-the-trust-anchor-rotates.warrant.json
# Markdown SHA-256:   f53749a71a97f9412b825aafcda5a17d6c82426752690f0b2dc8c87577541f7e
# Code AST SHA-256:   2e21706c607ab2572ec5898ce186c35f1c6bd15bd0d6f37f9c6cf180b1167b81
# Verified Classes:   AnchorDriftAcceptanceError, CachedAnchor, Attestation
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class AnchorDriftAcceptanceError(RuntimeError):
    """Raised when an attestation is accepted against an anchor that is
    no longer honored by the issuer, or when acceptance outlives the
    anchor's freshness bound."""
    pass


@dataclass
class CachedAnchor:
    key_id: str
    fetched_at: float
    revoked: bool = False


@dataclass
class Attestation:
    signing_key_id: str
    expires_at: float
    scope: str


def verify_attestation(
    att: Attestation,
    anchor_store: dict[str, CachedAnchor],
    requested_scope: str,
    anchor_max_staleness: float,
    now: float | None = None,
) -> bool:
    now = time.time() if now is None else now

    anchor = anchor_store.get(att.signing_key_id)
    if anchor is None:
        raise AnchorDriftAcceptanceError(
            f"no cached anchor for key {att.signing_key_id!r}"
        )

    # The invariant that the naive verifier omits: anchor freshness.
    age = now - anchor.fetched_at
    if age > anchor_max_staleness:
        raise AnchorDriftAcceptanceError(
            f"anchor {anchor.key_id!r} is stale: age={age:.0f}s "
            f"exceeds bound={anchor_max_staleness:.0f}s; "
            f"re-fetch issuer anchor set before accepting"
        )

    if anchor.revoked:
        raise AnchorDriftAcceptanceError(
            f"anchor {anchor.key_id!r} revoked by issuer; "
            f"attestation must be rejected and prior effects rolled back"
        )

    if now >= att.expires_at:
        raise AnchorDriftAcceptanceError(
            f"attestation expired at {att.expires_at:.0f}"
        )

    if requested_scope not in att.scope.split(","):
        raise AnchorDriftAcceptanceError(
            f"scope {requested_scope!r} not covered by {att.scope!r}"
        )

    return True


def on_accept(att: Attestation, rollback_registry: list) -> None:
    """Acceptance must register a compensating action so that a later
    revocation can undo any state committed under this grant."""
    if not rollback_registry:
        raise AnchorDriftAcceptanceError(
            "acceptance without a registered rollback path converts an "
            "auth failure into a resource-commitment leak"
        )
    # ... spawn sub-agent, reserve budget, enqueue calls ...


if __name__ == "__main__":
    now = 1_000_000.0
    store = {"k1": CachedAnchor("k1", fetched_at=now - 40_000)}
    att = Attestation("k1", expires_at=now + 60_000, scope="data:read")
    # Anchor is 40ks old, bound is 3_600s -> drift acceptance blocked.
    verify_attestation(att, store, "data:read", anchor_max_staleness=3_600, now=now)
