# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-skill-whose-provenance-attestation-verifies-but-whose-revocation-epoch-lags-the-loader-s-trust-horizon.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-skill-whose-provenance-attestation-verifies-but-whose-revocation-epoch-lags-the-loader-s-trust-horizon.warrant.json
# Markdown SHA-256:   faddd0643f94d74a73cbce986a5babcc19252fe06a37793a988e14217189718c
# Code AST SHA-256:   32a37af09a29bbbedebba9cc6f509fde734d134109cb66edc0cfda1916147193
# Verified Classes:   TrustDecision, ProvenanceTrustError, CachedSkill, LoaderState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class TrustDecision(Enum):
    INJECT = "inject"
    REFUSE_STALE_FEED = "refuse_stale_feed"
    REFUSE_REVOKED = "refuse_revoked"
    REFUSE_GAP = "refuse_gap"


class ProvenanceTrustError(RuntimeError):
    """Raised when a skill's provenance verifies but the loader's trust horizon
    cannot cover the revocation window at injection time."""


@dataclass(frozen=True)
class CachedSkill:
    digest: str
    key_family: str
    admitted_epoch: int


@dataclass(frozen=True)
class LoaderState:
    e_feed: int              # epoch of last *successful authenticated* poll
    last_seen_head: int      # registry head observed at that poll
    trust_horizon: int       # H, in epoch units, fixed by policy
    revoked: frozenset       # set of (key_family, revoked_at_epoch)


def decide_injection(skill: CachedSkill, state: LoaderState) -> TrustDecision:
    # Gap detection: a skipped epoch is indistinguishable from a hidden revocation.
    if state.last_seen_head != state.e_feed:
        return TrustDecision.REFUSE_GAP

    # Revocation match against cached admission epoch, not re-derived bytes.
    for fam, revoked_at in state.revoked:
        if fam == skill.key_family and skill.admitted_epoch < revoked_at:
            return TrustDecision.REFUSE_REVOKED

    # The non-obvious conjunct: feed must be fresh enough to have observed
    # any revocation that could bear on this key family.
    if state.e_feed < state.trust_horizon:
        return TrustDecision.REFUSE_STALE_FEED

    return TrustDecision.INJECT


def load_skill(skill: CachedSkill, state: LoaderState) -> CachedSkill:
    decision = decide_injection(skill, state)
    if decision is TrustDecision.REFUSE_STALE_FEED:
        raise ProvenanceTrustError(
            f"skill {skill.digest} attestation valid but feed epoch "
            f"{state.e_feed} < trust horizon {state.trust_horizon}; "
            f"revocation window unobservable — refusing dynamic injection"
        )
    if decision is TrustDecision.REFUSE_REVOKED:
        raise ProvenanceTrustError(
            f"skill {skill.digest} depends on revoked key family "
            f"{skill.key_family} admitted at epoch {skill.admitted_epoch}"
        )
    if decision is TrustDecision.REFUSE_GAP:
        raise ProvenanceTrustError(
            f"revocation feed gap: head {state.last_seen_head} != "
            f"e_feed {state.e_feed}; treating as stale"
        )
    return skill
