# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-poisoned-memory-write-it-has-already-flagged-as-unverifiable-provenance-laundering-through-the-summarization-seam.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-poisoned-memory-write-it-has-already-flagged-as-unverifiable-provenance-laundering-through-the-summarization-seam.warrant.json
# Markdown SHA-256:   5304fb1da80fa53358f59d3f485e221dcee0f4d642dcbedc2dc31a4a8f3993c4
# Code AST SHA-256:   b7876f9c7e2478bf7ef76fb85f249d62088fc3bcf5de64eadb415d7848a7a85b
# Verified Classes:   ProvenanceLaunderingError, MemoryWrite
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ProvenanceLaunderingError(RuntimeError):
    """Raised when a memory write's provenance is model-authored or ancestry is severed."""
    pass


@dataclass(frozen=True)
class MemoryWrite:
    content: str
    # NOTE: no `source` or `confidence` field. The model cannot populate these.
    ancestors: FrozenSet[str] = field(default_factory=frozenset)


def stamp_and_validate(
    model_output_content: str,
    input_ancestors: FrozenSet[str],
    root_trust: dict,
) -> MemoryWrite:
    """
    Harness-side stamping. The model produced only `model_output_content`.
    Ancestry is derived from the full input set, never from model citation.
    """
    if not input_ancestors:
        # A consolidation pass with no external input is summarizing its own
        # prior summaries -> provenance depth collapse -> reject.
        raise ProvenanceLaunderingError(
            "consolidation write has empty ancestry; refusing self-referential memory"
        )

    write = MemoryWrite(content=model_output_content, ancestors=input_ancestors)

    # Every ancestor must resolve to a known root. An ancestor that names an
    # internal process (e.g. 'consolidation-pass-*') rather than a root source
    # is the laundering signature.
    for anc in write.ancestors:
        if anc not in root_trust:
            raise ProvenanceLaunderingError(
                f"ancestor {anc!r} is not a registered root source; "
                "possible provenance laundering through summarization seam"
            )

    return write


def effective_trust(write: MemoryWrite, root_trust: dict) -> float:
    """Trust is the minimum over root ancestors, never the record's self-report."""
    return min(root_trust[a] for a in write.ancestors)
