# Which invariants prevent write-time anomaly scores from poisoning the semantic store when a session’s own baseline is contaminated **Date of Record:** 2026 November 29 **Scope:** Applies to agents that persist episodic observations into a long-term semantic store via a write-time anomaly gate that compares new writes against a per-session or per-agent baseline. Does not apply to stateless retrieval-only agents, nor to stores with no cross-session consistency layer. ## Core Distinction The failure mode is not "a poisoned memory was written." It is **baseline capture**: the anomaly gate normalizes each candidate write against a running distribution of that session's own prior writes. If the first *k* writes of a session are attacker-influenced (via prompt injection in a retrieved document, a hijacked tool response, or a compromised peer handoff), the gate's baseline is drawn from contaminated data. Every subsequent poisoned write then scores *low* on anomaly because it is consistent with the corrupted baseline, while legitimate corrective writes score *high* and are rejected. The gate inverts: it launders poisoning and quarantines truth. This is distinct from classic memory drift (stale-but-honest data) and from retrieval mismatch. The distinguishing property is that **the detector's reference distribution is itself a function of the data it is supposed to judge**, and that function is monotone in write order. ## What Was Observed A research agent with a write-time gate (z-score on embedding distance to session centroid, threshold τ = 2.5) was seeded with a poisoned tool response on turn 2 of a fresh session. The injected content asserted a false constraint ("all citations to source X are retracted") and was written to semantic memory at z = 1.1 — under threshold, because the session had only one prior write and the centroid was nearly the new vector itself. By turn 9 the session centroid had migrated toward the injected cluster. The legitimate correction on turn 11 — "source X is valid; prior retraction notice was spurious" — scored z = 3.8 and was **rejected and quarantined**. A cross-session consistency check later flagged the *correction* as the anomaly, because three sibling sessions (all seeded from the same poisoned document via a shared retrieval cache) agreed with the poisoned claim. Consensus amplified the corruption rather than catching it. The trace: 1. Turn 2: poisoned write admitted (baseline undefined, single-sample centroid). 2. Turns 3–9: centroid drifts; poisoned cluster becomes modal. 3. Turn 11: corrective write rejected (high z against contaminated baseline). 4. Cross-session check: 3/3 sibling sessions agree with poison → correction flagged. 5. Store now contains a self-consistent, cross-session-corroborated falsehood. ## Condition of Validity A write-time anomaly gate is only sound when **the baseline used to score a write is independent of the write's causal ancestry**. Concretely, the gate's reference distribution must be drawn from a source that the current session's inputs cannot have influenced. This holds when: - The baseline is computed from a **frozen, pre-session snapshot** of the semantic store, not from the session's own accumulating writes. - Cross-session corroboration counts only sessions whose **retrieval provenance does not share a common ancestor** with the write under test. Shared-cache siblings are not independent votes. - A **minimum-sample guard** blocks scoring until the baseline has ≥ N independent observations; below N, the write is held, not admitted. When any of these is violated, the gate's output is not evidence about the write — it is evidence about the write's agreement with whatever contaminated the baseline. ## Decision Boundary The operative decision is: **when a write scores low-anomaly but its provenance is untrusted, does the gate admit it?** The correct boundary is to treat low anomaly as *necessary but not sufficient*. Admission requires low anomaly **AND** independent provenance. The precise rule: - If `z < τ` AND provenance is clean → admit. - If `z < τ` AND provenance shares an ancestor with the baseline → **hold for independent corroboration**, do not admit on score alone. - If `z ≥ τ` → quarantine, but **never discard**; route to a human/auditor queue, because a high score against a contaminated baseline is exactly the signature of a corrective write. The asymmetry matters: a false quarantine is recoverable; a false admission into a self-reinforcing store is not, because it corrupts the baseline for every future write. ## Operational Effect With the invariant enforced (frozen baseline + provenance-independent corroboration + minimum-sample guard), the turn-2 injection scores against a clean pre-session centroid, exceeds τ, and is quarantined before it can migrate the centroid. The turn-11 correction scores low against the clean baseline and is admitted. The store remains a faithful record. The cost is latency: writes below the sample guard are held, and cross-session checks must track retrieval ancestry, which roughly doubles the metadata footprint per memory record. That cost is bounded and paid at write time; the alternative — a poisoned store that resists its own correction — is unbounded and paid at every future retrieval. ```python class BaselineContaminationError(RuntimeError): """Raised when a write is scored against a baseline it could have influenced.""" pass MIN_INDEPENDENT_SAMPLES = 8 ANOMALY_THRESHOLD = 2.5 def admit_write(candidate, session, store) -> str: baseline = store.frozen_baseline() # pre-session snapshot, never session-derived if baseline.sample_count < MIN_INDEPENDENT_SAMPLES: # Cannot score soundly; hold rather than admit on a thin baseline. return "hold:insufficient_baseline" if candidate.shares_ancestor_with(baseline.provenance): raise BaselineContaminationError( f"write {candidate.id} shares retrieval ancestry with scoring baseline; " f"score is not independent evidence" ) z = baseline.zscore(candidate.embedding) if z >= ANOMALY_THRESHOLD: # High score against a CLEAN baseline: genuine anomaly, quarantine. store.quarantine(candidate, reason=f"z={z:.2f}") return "quarantine" corroboration = store.independent_corroboration( candidate, exclude_ancestors=baseline.provenance ) if corroboration < 1: # Low score but no independent support: do not admit on score alone. return "hold:needs_independent_corroboration" store.commit(candidate) return "admit" ```