# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-trusts-write-provenance-scores-that-were-computed-before-a-consolidation-event.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-trusts-write-provenance-scores-that-were-computed-before-a-consolidation-event.warrant.json
# Markdown SHA-256:   eaa324a6b8a6831bfab1a2e6d55742d8388a5fa65a877d941119ccc672d80591
# Code AST SHA-256:   89eb768f977ef37238197e53f4f77d99ab8df65215f5dde5bf3d2a422cebf9f4
# Verified Classes:   ProvenanceLaunderingError, MemoryEntry
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class ProvenanceLaunderingError(RuntimeError):
    """Raised when a consolidated entry's score cannot be trusted because
    its input lineage was destroyed before the score was assigned."""


@dataclass
class MemoryEntry:
    content: str
    write_provenance: int          # 0=unverified, 1=corroborated, 2=attested
    hop_count: int = 0
    lineage: list["MemoryEntry"] = field(default_factory=list)


def effective_provenance(entry: MemoryEntry) -> int:
    """Recompute provenance over the full derivation graph.

    Weakest-link rule: a consolidated entry is only as trustworthy as its
    least-trustworthy input. Lineage MUST be intact; if it is not, the
    stored score is an artifact of laundering and must not be used.
    """
    if entry.hop_count == 0:
        return entry.write_provenance

    if not entry.lineage:
        raise ProvenanceLaunderingError(
            f"entry at hop {entry.hop_count} has no retained lineage; "
            f"stored score {entry.write_provenance} is unscorable and "
            f"must be treated as unverified, not trusted"
        )

    return min(effective_provenance(src) for src in entry.lineage)


def admit(entry: MemoryEntry, min_tier: int = 1) -> bool:
    """Gate admission on recomputed provenance, never on the stored score."""
    score = effective_provenance(entry)  # may raise
    return score >= min_tier


# --- demonstration of the boundary failure ---

false_claim = MemoryEntry("claim X is true", write_provenance=0)      # unverified
attested = [MemoryEntry(f"fact {i}", write_provenance=2) for i in range(4)]

# Laundered consolidation: max-provenance, inputs deleted, no lineage kept.
laundered = MemoryEntry(
    "summary: claim X is true; fact 0..3",
    write_provenance=2,      # max of inputs -- the bug
    hop_count=1,
    lineage=[],              # inputs destroyed
)

try:
    admit(laundered)
except ProvenanceLaunderingError as exc:
    print(f"QUARANTINED: {exc}")

# Correct consolidation: weakest-link, lineage retained.
honest = MemoryEntry(
    "summary: claim X is true; fact 0..3",
    write_provenance=0,      # min of inputs
    hop_count=1,
    lineage=[false_claim, *attested],
)
print(f"honest consolidated tier: {effective_provenance(honest)}")  # 0
print(f"admitted: {admit(honest)}")                                # False
