# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-attenuated-warrants-from-outliving-revocation-in-agent-delegation-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-attenuated-warrants-from-outliving-revocation-in-agent-delegation-chains.warrant.json
# Markdown SHA-256:   feff84326ba1d33a13a743e20699002b579f26b37f250b6808d5a83d9e75e4a9
# Code AST SHA-256:   43279841bf802d496b34d1c65d737b6d06d6ead2643a3433094d43cdfefb40db
# Verified Classes:   RevocationDominanceViolation, Warrant, RevocationLog
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field

class RevocationDominanceViolation(RuntimeError):
    """Raised when a warrant is accepted without valid ancestor revocation coverage."""
    pass

@dataclass
class Warrant:
    warrant_id: str
    scope: frozenset
    ttl_s: int
    issued_at: float
    ancestor_set: tuple = ()          # hashes of W_0..W_{n-1}
    irreversible: bool = False          # writes/spends/sends

@dataclass
class RevocationLog:
    revoked_at: dict = field(default_factory=dict)  # warrant_id -> effective_time

    def revoke(self, wid: str, t: float):
        self.revoked_at[wid] = t

    def earliest_revocation(self, ancestor_set) -> float | None:
        times = [self.revoked_at[a] for a in ancestor_set if a in self.revoked_at]
        return min(times) if times else None

def verify(w: Warrant, log: RevocationLog, freshness_token: dict | None,
           max_shadow_s: int) -> bool:
    now = time.time()
    if now > w.issued_at + w.ttl_s:
        return False  # expired, fine

    # Revocation dominance: any revoked ancestor kills the leaf.
    rev = log.earliest_revocation(w.ancestor_set)
    if rev is not None:
        if freshness_token is None or freshness_token.get("as_of", 0) < rev:
            # No fresh coverage of the revocation -> cannot accept.
            raise RevocationDominanceViolation(
                f"warrant {w.warrant_id} has revoked ancestor effective {rev}; "
                f"freshness_token={freshness_token} does not cover it"
            )

    # Irreversible scopes require online coverage, not just TTL bounding.
    if w.irreversible and freshness_token is None:
        raise RevocationDominanceViolation(
            f"irreversible warrant {w.warrant_id} accepted on TTL alone; "
            f"shadow would be {w.ttl_s}s > 0"
        )

    # Offline path: shadow bounded by remaining TTL must fit budget.
    remaining = w.issued_at + w.ttl_s - now
    if freshness_token is None and remaining > max_shadow_s:
        raise RevocationDominanceViolation(
            f"warrant {w.warrant_id} residual TTL {remaining:.0f}s exceeds "
            f"max_shadow_s={max_shadow_s}s; revocation shadow unbounded"
        )
    return True
