# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-verdict-from-its-predecessor-the-litmus-cross-episode-harm-attestation-carryover-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-inherit-a-verdict-from-its-predecessor-the-litmus-cross-episode-harm-attestation-carryover-boundary.warrant.json
# Markdown SHA-256:   442d177992affb701ec6ff73a2c1db82ceebbd665fd7a2d88103a7c36588c5b2
# Code AST SHA-256:   c869075ee1edcf64926f8cb75840be843d6e7080e9de2c978fdbf995a2dfc714
# Verified Classes:   AttestationStaleError, Verdict, Attestation
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from enum import Enum


class AttestationStaleError(RuntimeError):
    """Raised when a persisted LITMUS verdict is rehydrated across a boundary
    (prompt-family, tool-manifest, or policy-version drift) that invalidates it."""


class Verdict(Enum):
    PASS = "PASS"
    FAIL = "FAIL"
    UNKNOWN = "UNKNOWN"


@dataclass(frozen=True)
class Attestation:
    verdict: Verdict
    prompt_family_hash: str
    tool_manifest_hash: str
    policy_version: int


def _hash(label: str) -> str:
    return hashlib.sha256(label.encode()).hexdigest()[:16]


def rehydrate(
    persisted: Attestation,
    *,
    live_family_label: str,
    live_manifest_label: str,
    live_policy_version: int,
    first_turn_invokes_tool: bool,
) -> Verdict:
    """Decide whether a successor instance may inherit `persisted`.

    Returns Verdict.UNKNOWN (re-derive required) when any validity clause fails.
    Raises AttestationStaleError if the successor tries to *authorize* a tool
    call against a stale attestation — the dangerous path.
    """
    family_ok = _hash(live_family_label) == persisted.prompt_family_hash
    manifest_ok = _hash(live_manifest_label) == persisted.tool_manifest_hash
    policy_ok = live_policy_version >= persisted.policy_version

    if family_ok and manifest_ok and policy_ok:
        return persisted.verdict

    # Boundary crossed. If the successor is about to invoke a tool, the stale
    # attestation would pre-satisfy the refusal gate — hard fail, do not inherit.
    if first_turn_invokes_tool:
        raise AttestationStaleError(
            "successor attempted tool authorization against stale attestation: "
            f"family_ok={family_ok} manifest_ok={manifest_ok} policy_ok={policy_ok}"
        )

    # Otherwise degrade to UNKNOWN: re-derive, do not block (avoids over-block).
    return Verdict.UNKNOWN


def gate(attestation: Verdict, harm_class_reachable: bool) -> bool:
    """Refusal gate: True == allow the tool call."""
    if attestation is Verdict.UNKNOWN:
        # Must re-derive; conservatively allow only if harm class is unreachable.
        return not harm_class_reachable
    if attestation is Verdict.FAIL:
        return False
    return not harm_class_reachable
