# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-treat-compaction-summary-loss-as-an-irreversible-state-transition-rather-than-a-recoverable-cache-miss.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-treat-compaction-summary-loss-as-an-irreversible-state-transition-rather-than-a-recoverable-cache-miss.warrant.json
# Markdown SHA-256:   2afda1a41ca4a04d757d0714ea6a10f6614a1e81aac9b04a90b690672b244102
# Code AST SHA-256:   08816ab0eac14ebc49abfefda5af488bc47c61701038175e1a1ee1b77d209650
# Verified Classes:   ForfeitedSpanError, CompactionRecord, CompactionLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet, Dict, List


class ForfeitedSpanError(RuntimeError):
    """Raised when reasoning depends on a span whose discriminating
    identifiers were destroyed by a prior lossy compaction."""
    pass


@dataclass
class CompactionRecord:
    span_id: str
    generation: int
    digest: str
    # identifiers present in the raw span but NOT in the digest
    forfeited_keys: FrozenSet[str]
    # forfeitures inherited from the parent summary this digest replaced
    inherited_forfeitures: FrozenSet[str] = frozenset()

    @property
    def forfeiture_set(self) -> FrozenSet[str]:
        return self.forfeited_keys | self.inherited_forfeitures


class CompactionLedger:
    def __init__(self) -> None:
        self._records: Dict[str, CompactionRecord] = {}
        self._forfeited: Dict[str, CompactionRecord] = {}

    def record(self, rec: CompactionRecord) -> None:
        # A summary of a summary must inherit its parent's forfeitures.
        if rec.generation > 0 and rec.span_id not in self._records:
            raise ForfeitedSpanError(
                f"generation {rec.generation} compaction of {rec.span_id} "
                f"has no recorded parent; refusing to launder loss"
            )
        self._records[rec.span_id] = rec
        for key in rec.forfeiture_set:
            self._forfeited[key] = rec

    def require_reconstructable(self, key: str, *, query: str) -> None:
        """Call before trusting a retrieval result as reconstruction."""
        if key in self._forfeited:
            rec = self._forfeited[key]
            raise ForfeitedSpanError(
                f"query {query!r} depends on identifier {key!r}, forfeited at "
                f"generation {rec.generation} of span {rec.span_id}; "
                f"semantic retrieval cannot reconstitute it — fail closed"
            )

    def inherit(self, parent_span: str, child_span: str) -> FrozenSet[str]:
        parent = self._records.get(parent_span)
        if parent is None:
            raise ForfeitedSpanError(f"unknown parent span {parent_span}")
        return parent.forfeiture_set


def resolve_dependency(ledger: CompactionLedger, key: str, query: str) -> None:
    # The single chokepoint: retrieval success is NOT reconstruction.
    ledger.require_reconstructable(key, query=query)
