# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-an-agent-identity-revocation-has-not-actually-propagated-sub-token-lifetime-revocation-windows-and-the-quarantine-ack.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-an-agent-identity-revocation-has-not-actually-propagated-sub-token-lifetime-revocation-windows-and-the-quarantine-ack.warrant.json
# Markdown SHA-256:   6f3428828791c4e10fd2387ac2969d7f5688575619153355ffdff59f7c722473
# Code AST SHA-256:   67283c47c00c683b752a105a1ef894c0291ea3261a2b9d1b561153fbcb22d266
# Verified Classes:   RevocationNotPropagated, Credential, Verifier
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class RevocationNotPropagated(RuntimeError):
    """Raised when an identity is marked revoked but a verifier can still accept its live credential."""
    pass


@dataclass
class Credential:
    identity: str
    kid: str
    exp: float  # absolute expiry


@dataclass
class Verifier:
    name: str
    trusted_kids: dict          # kid -> grace_until (key-overlap window end)
    revocation_set_refreshed_at: float
    staleness_bound: float
    revoked_ack: set = field(default_factory=set)  # identities this verifier has ack'd as revoked

    def accepts(self, cred: Credential, now: float, revoked: set) -> bool:
        if now >= cred.exp:
            return False
        grace_until = self.trusted_kids.get(cred.kid)
        if grace_until is None or now >= grace_until:
            return False
        # The tell: signature/key valid, but revocation state ignored or stale.
        if cred.identity in revoked and self.revocation_set_refreshed_at < now - self.staleness_bound:
            return True  # stale revocation set -> still accepts
        return cred.identity not in revoked


def assert_contained(identity, cred, verifiers, revoked, now, propagation_sla):
    """Containment invariant: no verifier may accept the live credential, and all must ack."""
    for v in verifiers:
        if v.accepts(cred, now, revoked):
            raise RevocationNotPropagated(
                f"{identity} still acceptable at verifier={v.name}: "
                f"kid={cred.kid} grace_until={v.trusted_kids.get(cred.kid):.1f} "
                f"now={now:.1f} exp={cred.exp:.1f} "
                f"revset_age={now - v.revocation_set_refreshed_at:.1f}s"
            )
        if identity not in v.revoked_ack:
            raise RevocationNotPropagated(
                f"{identity} not acknowledged by verifier={v.name} within SLA={propagation_sla}s"
            )
    # Grace window must not outlive the credential under any trusted key.
    for v in verifiers:
        grace_until = v.trusted_kids.get(cred.kid, 0.0)
        if grace_until > cred.exp:
            raise RevocationNotPropagated(
                f"key-overlap window at {v.name} ({grace_until:.1f}) exceeds credential exp "
                f"({cred.exp:.1f}); grace window, not token, is the real deadline"
            )
    return True
