# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-still-valid-capability-warrant-after-its-issuing-agent-s-identity-has-been-revoked-mid-federation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-still-valid-capability-warrant-after-its-issuing-agent-s-identity-has-been-revoked-mid-federation.warrant.json
# Markdown SHA-256:   fa287df02d38a6d42bec19728bc0bbaf839bdeb48656fa31f5fbaa44e5ac49f5
# Code AST SHA-256:   35b5a5b47a41bdc997d68af483dbf69eef8e63fa55bbfee18265ff1ce89d260e
# Verified Classes:   WarrantChainRevokedError, RevocationView
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class WarrantChainRevokedError(RuntimeError):
    """Raised when a warrant's delegation chain contains a revoked or indeterminate principal."""
    pass


class RevocationView:
    def __init__(self, revoked_keys: set, synced_at: float, max_staleness: float):
        self.revoked_keys = revoked_keys
        self.synced_at = synced_at
        self.max_staleness = max_staleness

    def is_fresh(self, now: float) -> bool:
        return (now - self.synced_at) <= self.max_staleness


def authorize_call(warrant, revocation_view, now: float) -> bool:
    # Fail closed on stale revocation view before touching the warrant.
    if not revocation_view.is_fresh(now):
        raise WarrantChainRevokedError(
            f"revocation view stale: age={now - revocation_view.synced_at:.1f}s "
            f"> max_staleness={revocation_view.max_staleness:.1f}s; cannot assert non-revocation"
        )

    if now >= warrant.exp:
        raise WarrantChainRevokedError(f"warrant expired at {warrant.exp}")

    # Walk the ENTIRE chain, not just the presenter. A revoked root poisons
    # every attenuated descendant even if the intermediate is still trusted.
    for principal in warrant.delegation_chain:
        if principal.key_id in revocation_view.revoked_keys:
            raise WarrantChainRevokedError(
                f"chain principal {principal.key_id} revoked at "
                f"{revocation_view.revoked_keys[principal.key_id] if isinstance(revocation_view.revoked_keys, dict) else 'unknown'}; "
                f"warrant {warrant.id} is poisoned"
            )

    return True
