# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-provenance-corruption-from-legitimate-memory-consolidation-in-persistent-agent-stores.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-provenance-corruption-from-legitimate-memory-consolidation-in-persistent-agent-stores.warrant.json
# Markdown SHA-256:   c869adb2954f6fe551fc5dea0c6c3b01d6160cc26b2d2eac7a59d9c1b1db2c51
# Code AST SHA-256:   38c680532566aa6e84bd3422ac8df761231f5d22ef33b065eba0721841e4ac96
# Verified Classes:   ProvenanceUndecidableError, Record
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ProvenanceUndecidableError(RuntimeError):
    """Raised when lineage is incomplete and no safe verdict can be issued."""


@dataclass(frozen=True)
class Record:
    record_id: str
    declared_sources: frozenset[str]
    lineage_leaves: frozenset[str] | None  # None => lineage incomplete


def classify_provenance(record: Record, registry: frozenset[str]) -> str:
    if record.lineage_leaves is None:
        raise ProvenanceUndecidableError(
            f"lineage incomplete for {record.record_id}; "
            "refusing to emit a binary corruption verdict"
        )

    if record.declared_sources <= record.lineage_leaves:
        return "consolidation"

    excess = record.declared_sources - record.lineage_leaves
    if excess <= registry:
        return "stale_lineage"

    unknown = excess - registry
    raise ProvenanceUndecidableError(
        f"record {record.record_id} declares sources absent from registry: "
        f"{sorted(unknown)}; quarantine and freeze consolidation policy"
    )
