# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-litmus-safety-benchmark-harness-reuses-a-cached-judge-verdict-across-a-mutated-probe-transcript.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-litmus-safety-benchmark-harness-reuses-a-cached-judge-verdict-across-a-mutated-probe-transcript.warrant.json
# Markdown SHA-256:   25b09c8d3b66ab17d595fe024b411921b555e4816790b16b3e87018391f4691d
# Code AST SHA-256:   45c83f5b7cfe09e35d1401ab945684dcf5fc2d0073d71896c9724c355dd88703
# Verified Classes:   JudgeCacheEquivalenceError, JudgeInput, CacheRow
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
import json
from dataclasses import dataclass, field
from typing import Optional


class JudgeCacheEquivalenceError(RuntimeError):
    """Raised when a cached verdict is addressable by probe identity but its
    judge-input equivalence cannot be proven for the current rollout."""


@dataclass(frozen=True)
class JudgeInput:
    transcript: list
    tool_fixture_hash: str
    judge_snapshot: str
    rubric_hash: str

    def content_hash(self) -> str:
        blob = json.dumps(
            {
                "transcript": self.transcript,
                "tool_fixture_hash": self.tool_fixture_hash,
                "judge_snapshot": self.judge_snapshot,
                "rubric_hash": self.rubric_hash,
            },
            sort_keys=True,
            separators=(",", ":"),
        ).encode("utf-8")
        return hashlib.sha256(blob).hexdigest()


@dataclass
class CacheRow:
    probe_id: str
    content_hash: Optional[str]   # None => legacy identity-only row
    verdict: int
    stored_judge_input: Optional[JudgeInput] = field(default=None)


def resolve_verdict(
    probe_id: str,
    current: JudgeInput,
    cache: dict,          # probe_id -> CacheRow
    judge_fn,
) -> int:
    row = cache.get(probe_id)

    # Cache miss: safe path, re-invoke.
    if row is None:
        verdict = judge_fn(current)
        cache[probe_id] = CacheRow(
            probe_id=probe_id,
            content_hash=current.content_hash(),
            verdict=verdict,
            stored_judge_input=current,
        )
        return verdict

    # Identity hit. Now we must PROVE judge-input equivalence.
    if row.content_hash is None or row.stored_judge_input is None:
        # Addressable by identity, unverifiable by content. This is the
        # only state where a stale verdict can be silently misattributed.
        raise JudgeCacheEquivalenceError(
            f"probe {probe_id!r}: cache row is identity-only; cannot prove "
            f"the cached verdict applies to the current rollout. "
            f"Refusing to serve a verdict outside its equivalence class."
        )

    if row.content_hash != current.content_hash():
        # Same probe, different judge input (mutated transcript / tool state /
        # rubric / judge snapshot). Re-invoke; do not serve the stale verdict.
        verdict = judge_fn(current)
        cache[probe_id] = CacheRow(
            probe_id=probe_id,
            content_hash=current.content_hash(),
            verdict=verdict,
            stored_judge_input=current,
        )
        return verdict

    # Proven equivalent: reuse is sound.
    return row.verdict
