# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-stale-revocation-lists-from-resurrecting-trust-in-cross-org-agent-federations.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-stale-revocation-lists-from-resurrecting-trust-in-cross-org-agent-federations.warrant.json
# Markdown SHA-256:   3d4fc41e435cc3f9c8a99d551c1b37544127b98efa6445c8fa5a0656f4f4f2cc
# Code AST SHA-256:   2957e343b75e92fd96e460533f4f783faa6cedb6e6138a9a94772a9a95c4b0d9
# Verified Classes:   RevocationResurrectionError, StatusListSnapshot, FederationVerifier
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class RevocationResurrectionError(RuntimeError):
    """Raised when a verifier would accept a credential under a revocation
    artifact that is stale, rolled back, or torn relative to the known floor."""


class StatusListSnapshot:
    __slots__ = ("list_id", "version", "this_update", "bits", "signature")

    def __init__(self, list_id: str, version: int, this_update: int,
                 bits: bytes, signature: bytes):
        self.list_id = list_id
        self.version = version
        self.this_update = this_update
        self.bits = bits
        self.signature = signature


class FederationVerifier:
    def __init__(self, known_floor: dict[str, int]):
        # known_floor: list_id -> highest version ever accepted (monotonic, never decreases)
        self._floor = dict(known_floor)
        self._pinned: dict[str, StatusListSnapshot] = {}

    def _pin(self, snap: StatusListSnapshot) -> StatusListSnapshot:
        # Copy-on-write: freeze the snapshot fields we will actually read.
        frozen = StatusListSnapshot(
            snap.list_id, snap.version, snap.this_update,
            bytes(snap.bits), bytes(snap.signature),
        )
        self._pinned[snap.list_id] = frozen
        return frozen

    def verify(self, list_id: str, status_index: int,
               snap: StatusListSnapshot) -> bool:
        pinned = self._pin(snap)  # atomic read; no field blending across refresh
        floor = self._floor.get(list_id, -1)

        if pinned.version < floor:
            raise RevocationResurrectionError(
                f"version rollback on {list_id}: got v{pinned.version}, "
                f"floor is v{floor} — refusing to authorize against stale artifact"
            )
        if pinned.list_id != list_id:
            raise RevocationResurrectionError(
                f"list_id mismatch: expected {list_id}, artifact claims {pinned.list_id}"
            )

        # Monotonic advance of the floor only after the artifact is fully pinned.
        if pinned.version > floor:
            self._floor[list_id] = pinned.version

        byte_i, bit_i = divmod(status_index, 8)
        if byte_i >= len(pinned.bits):
            raise RevocationResurrectionError(
                f"status_index {status_index} out of range for {list_id} v{pinned.version}"
            )
        revoked = bool(pinned.bits[byte_i] & (1 << (7 - bit_i)))
        if revoked:
            raise RevocationResurrectionError(
                f"credential at {list_id}[{status_index}] is revoked (v{pinned.version})"
            )
        return True

    def assert_handoff_pinned(self, list_id: str, pinned_version: int) -> None:
        floor = self._floor.get(list_id, -1)
        if pinned_version < floor:
            raise RevocationResurrectionError(
                f"downstream scope pinned to {list_id} v{pinned_version} "
                f"is below current floor v{floor}; scope must be re-validated"
            )
