# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-reversible-eviction-whose-journal-replay-diverges-from-the-live-context-rather-than-reconciling-it.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-reversible-eviction-whose-journal-replay-diverges-from-the-live-context-rather-than-reconciling-it.warrant.json
# Markdown SHA-256:   648119173c365b2939f7c23a8afa401a59acede5ed2e60d02c6ea3b7cb3a34d0
# Code AST SHA-256:   40c479639375e9e62a49caed5233094577fd4d2ac5ddda849f00ae8c06965ed3
# Verified Classes:   StaleReplayError, JournalEntry
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class StaleReplayError(RuntimeError):
    """Raised when a journal replay would restore a span whose dependency set is non-empty."""
    pass


@dataclass(frozen=True)
class JournalEntry:
    span_id: str
    snapshot: bytes
    preconditions_hash: str
    reanchor_hint: str
    dependency_set: FrozenSet[str] = field(default_factory=frozenset)


def replay_eviction(
    entry: JournalEntry,
    live_dependency_set: FrozenSet[str],
    hint_validates: bool,
    confidence: float,
    confidence_threshold: float = 0.85,
) -> bytes:
    # Journal integrity is necessary but NOT sufficient: a clean replay proves nothing
    # about continuity. Mutation-closure must be checked against post-eviction survivors.
    if live_dependency_set:
        raise StaleReplayError(
            f"span {entry.span_id!r} has live dependents {sorted(live_dependency_set)}; "
            f"replay would be stale. Refuse and re-acquire from source."
        )
    if not hint_validates:
        raise StaleReplayError(
            f"reanchor hint for {entry.span_id!r} failed re-validation; "
            f"hint {entry.reanchor_hint!r} is a stale prediction."
        )
    if confidence < confidence_threshold:
        raise StaleReplayError(
            f"dependency-set confidence {confidence:.2f} below threshold "
            f"{confidence_threshold:.2f}; refusal is the safe default."
        )
    return entry.snapshot
