# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-redaction-tombstone-has-been-resurrected-by-an-episodic-to-semantic-distillation-pass.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-redaction-tombstone-has-been-resurrected-by-an-episodic-to-semantic-distillation-pass.warrant.json
# Markdown SHA-256:   b5af2c4941e36c7ac916789795d3bf5c53d9b1839eb4942d15365502fb07f1d6
# Code AST SHA-256:   26e91f91d4496954ca19ce1f24a8defb1213f438df4be38aea25f250de30aac0
# Verified Classes:   RedactionClosureViolation, TombstoneLedger, SemanticRecord
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable

class RedactionClosureViolation(RuntimeError):
    """Raised when a semantic write would derive from a tombstoned record."""
    pass

@dataclass
class TombstoneLedger:
    _ids: set[str] = field(default_factory=set)
    def contains(self, rid: str) -> bool:
        return rid in self._ids
    def add(self, rid: str) -> None:
        self._ids.add(rid)

@dataclass
class SemanticRecord:
    record_id: str
    provenance: list[str]
    parents: list["SemanticRecord"] = field(default_factory=list)

def transitive_provenance(rec: SemanticRecord, seen: set[str] | None = None) -> set[str]:
    seen = seen or set()
    if rec.record_id in seen:
        return set()
    seen.add(rec.record_id)
    out = set(rec.provenance)
    for p in rec.parents:
        out |= transitive_provenance(p, seen)
    return out

def commit_semantic(rec: SemanticRecord, ledger: TombstoneLedger) -> None:
    closure = transitive_provenance(rec)
    hit = closure & ledger._ids
    if hit:
        raise RedactionClosureViolation(
            f"semantic record {rec.record_id} derives from tombstoned ids {sorted(hit)}; "
            f"re-distill from excised batch"
        )
    # commit proceeds only when closure is clean
    return None

if __name__ == "__main__":
    ledger = TombstoneLedger()
    ledger.add("R_4471")
    parent = SemanticRecord("S_9002", provenance=["R_4470", "R_4471"])
    child = SemanticRecord("S_9100", provenance=["S_9002"], parents=[parent])
    try:
        commit_semantic(child, ledger)
    except RedactionClosureViolation as e:
        print(f"rejected: {e}")
