# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-plugin-whose-attestation-chain-is-valid-but-whose-revocation-epoch-predates-its-own-last-skill-graph-s.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-plugin-whose-attestation-chain-is-valid-but-whose-revocation-epoch-predates-its-own-last-skill-graph-s.warrant.json
# Markdown SHA-256:   b560416914f886301f6785efab6a6b6bb0645cf0bc386fa0bf7ca35c44ccf3d2
# Code AST SHA-256:   554212c3baba254612fcadc4d08a8676ea713d61bd772fd02ef7ba6bae9af67e
# Verified Classes:   ProvenanceIndeterminate, Artifact, RevocationEntry
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ProvenanceIndeterminate(RuntimeError):
    """Raised when an artifact's revocation status cannot be positively established."""
    pass


@dataclass(frozen=True)
class Artifact:
    artifact_hash: str
    attestation_epoch: int
    sbom_components: frozenset[str]


@dataclass(frozen=True)
class RevocationEntry:
    scope: str            # "artifact" | "component" | "builder" | "key"
    target: str
    epoch: int


def admit_artifact(
    artifact: Artifact,
    runtime_epoch: int,
    revocations: list[RevocationEntry],
    previously_refused: set[tuple[str, int]],
) -> bool:
    key = (artifact.artifact_hash, artifact.attestation_epoch)

    # Invariant 3: latched refusal is never reversed.
    if key in previously_refused:
        raise ProvenanceIndeterminate(
            f"artifact {key} previously refused; re-admission requires a fresh epoch"
        )

    # Invariant 1: cannot evaluate the window without a full delta.
    if artifact.attestation_epoch > runtime_epoch:
        raise ProvenanceIndeterminate(
            f"attestation epoch {artifact.attestation_epoch} is ahead of runtime "
            f"epoch {runtime_epoch}; delta window unenumerable"
        )

    window = [r for r in revocations if artifact.attestation_epoch < r.epoch <= runtime_epoch]

    # Invariant 2: scope closure — join against full attestation graph.
    for entry in window:
        if entry.scope == "artifact" and entry.target == artifact.artifact_hash:
            return False
        if entry.scope == "component" and entry.target in artifact.sbom_components:
            return False
        if entry.scope in ("builder", "key"):
            raise ProvenanceIndeterminate(
                f"revocation scope '{entry.scope}' requires attestation-graph join "
                f"the runtime cannot perform for {artifact.artifact_hash}"
            )

    return True
