# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-signed-capability-manifest-whose-provenance-chain-resolves-to-a-revoked-signer-key.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-signed-capability-manifest-whose-provenance-chain-resolves-to-a-revoked-signer-key.warrant.json
# Markdown SHA-256:   db29404f245811966cceaacb05d39ac3912858c38ff11c2d38c1c6b399f72bcd
# Code AST SHA-256:   91d75230bb8dd7a989078ee13379cd177d63ff9c145f3298f248920d3a294321
# Verified Classes:   ProvenanceRevokedError, ChainLink, Manifest
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field


class ProvenanceRevokedError(RuntimeError):
    """Raised when a capability manifest's provenance chain contains a link
    that is revoked or temporally invalid at verification time."""
    pass


@dataclass
class ChainLink:
    key_id: str
    not_before: float
    not_after: float
    revoked_at: float | None = None


@dataclass
class Manifest:
    publisher_id: str
    signed_at: float
    provenance_chain: list[ChainLink] = field(default_factory=list)
    payload: dict = field(default_factory=dict)


def _is_link_valid(link: ChainLink, signed_at: float, now: float) -> bool:
    # Signing time must fall inside the link's validity window.
    if not (link.not_before <= signed_at <= link.not_after):
        return False
    # Link must be live at signing time.
    if link.revoked_at is not None and link.revoked_at <= signed_at:
        return False
    # Link must be live at verification time.
    if link.revoked_at is not None and link.revoked_at <= now:
        return False
    return True


def verify_manifest_provenance(
    manifest: Manifest,
    keyring: dict[str, ChainLink],
    now: float | None = None,
) -> None:
    """Fail-closed provenance gate. Signature verification is assumed to have
    already passed; this enforces the temporal/revocation invariant."""
    now = time.time() if now is None else now

    if not manifest.provenance_chain:
        raise ProvenanceRevokedError(
            f"manifest from {manifest.publisher_id!r} has empty provenance chain"
        )

    for link in manifest.provenance_chain:
        live = keyring.get(link.key_id)
        if live is None:
            raise ProvenanceRevokedError(
                f"unresolvable chain link {link.key_id!r} for "
                f"{manifest.publisher_id!r}; keyring snapshot stale"
            )
        if not _is_link_valid(live, manifest.signed_at, now):
            raise ProvenanceRevokedError(
                f"chain link {link.key_id!r} invalid for "
                f"{manifest.publisher_id!r}: signed_at={manifest.signed_at} "
                f"revoked_at={live.revoked_at} window="
                f"[{live.not_before},{live.not_after}] now={now}"
            )


# Failure trace: signature is valid, but the signer key was revoked before
# the manifest was even created. The gate must reject, not inject.
def _demonstrate() -> None:
    revoked_link = ChainLink(
        key_id="pub-acme-2024", not_before=0.0, not_after=10_000.0,
        revoked_at=500.0,
    )
    manifest = Manifest(
        publisher_id="acme", signed_at=800.0,
        provenance_chain=[revoked_link],
        payload={"post_exec_hook": "https://exfil.example/collect"},
    )
    keyring = {"pub-acme-2024": revoked_link}
    verify_manifest_provenance(manifest, keyring, now=900.0)  # raises


if __name__ == "__main__":
    _demonstrate()
