# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-trust-anchor-staleness-in-cross-org-agent-federation-the-policy-revocation-lag-that-outlives-the-capability-grant.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-trust-anchor-staleness-in-cross-org-agent-federation-the-policy-revocation-lag-that-outlives-the-capability-grant.warrant.json
# Markdown SHA-256:   a8c7eb585c1a90b69e98127e58c6751e011076219e8516cb8d4fedd899b9fd17
# Code AST SHA-256:   eb9318e5bc4845ed69135070573e517cd6b5f246c0728ecdfb5ff5e258a9d9d1
# Verified Classes:   StaleWarrantCurrencyError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class StaleWarrantCurrencyError(RuntimeError):
    """Raised when a cached foreign warrant signature-verifies but its
    policy_digest can no longer be resolved to the issuer's current policy
    state, meaning the grant may have been revoked at origin."""
    pass


def admit_cached_warrant(warrant, anchor, now, last_currency_check):
    """
    Invariant: a cached warrant is admissible only if its policy_digest
    re-resolves to live issuer policy state within min(ttl, revocation_sla).
    Signature validity alone is NOT sufficient for cross-org cached grants.
    """
    # Integrity check: signature chain to root anchor.
    if not anchor.verify_chain(warrant.signature_chain):
        raise StaleWarrantCurrencyError(
            f"warrant {warrant.warrant_id}: signature chain broken"
        )

    # Currency check: digest must be resolvable to current policy state.
    resolver = anchor.policy_resolver_for(warrant.policy_digest)
    if resolver is None:
        # Digest-without-resolver: the exact structural signal of the boundary.
        raise StaleWarrantCurrencyError(
            f"warrant {warrant.warrant_id}: policy_digest "
            f"{warrant.policy_digest[:12]} has no resolver on anchor "
            f"{anchor.anchor_id}; currency unverifiable"
        )

    interval = min(warrant.ttl, anchor.revocation_sla)
    if now - last_currency_check > interval:
        raise StaleWarrantCurrencyError(
            f"warrant {warrant.warrant_id}: currency heartbeat overdue by "
            f"{(now - last_currency_check) - interval}s; TTL alone is not a "
            f"revocation bound"
        )

    state = resolver.current_state(warrant.policy_digest)
    if state != "active":
        raise StaleWarrantCurrencyError(
            f"warrant {warrant.warrant_id}: policy {warrant.policy_ref} is "
            f"{state} at issuer; grant is revoked-but-cached"
        )

    return True
