# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-write-path-provenance-scores-against-retrieval-time-trust-weights-before-admitting-a-memory-and-what-breaks-when-it-d.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-write-path-provenance-scores-against-retrieval-time-trust-weights-before-admitting-a-memory-and-what-breaks-when-it-d.warrant.json
# Markdown SHA-256:   2666b6d83459eae8815b89aee5a52064aa95e3672914cf4160d789b60ba7ce8e
# Code AST SHA-256:   99bf3ff19d2ba156000ee659b9ddaf85688eed3ae297e4bc664a81d8ba13e2fe
# Verified Classes:   ProvenanceClampViolation, Memory
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ProvenanceClampViolation(RuntimeError):
    """Raised when a memory's retrieval weight exceeds the bound implied by its write-path provenance."""
    pass


@dataclass(frozen=True)
class Memory:
    claim: str
    provenance: float       # write-path score in [0, 1]
    corroborations: int     # count of independent admissions
    recency_boost: float    # in [0, 1], from age


P_MAX = 0.9          # highest provenance class present in the store
W_MAX = 1.0          # maximum possible retrieval weight
CORROB_BOOST = 0.15  # per independent admission


def retrieval_weight(m: Memory) -> float:
    raw = m.provenance + CORROB_BOOST * m.corroborations + m.recency_boost
    return min(raw, W_MAX)


def clamped_weight(m: Memory) -> float:
    deficit = max(0.0, P_MAX - m.provenance)
    upper = W_MAX * (1.0 - deficit)
    w = retrieval_weight(m)
    if w > upper + 1e-9:
        raise ProvenanceClampViolation(
            f"memory {m.claim!r} retrieval weight {w:.3f} exceeds provenance bound {upper:.3f} "
            f"(provenance={m.provenance}, corroborations={m.corroborations})"
        )
    return w


def admit(m: Memory) -> float:
    # Enforce the invariant at admission: the clamp must hold before the memory is readable.
    return clamped_weight(m)


if __name__ == "__main__":
    poison = Memory("renewal date 2024-01-01", provenance=0.2, corroborations=6, recency_boost=0.1)
    try:
        admit(poison)
    except ProvenanceClampViolation as e:
        print(f"admission rejected: {e}")
