# How does an instance detect semantic memory poisoning when write-audit diffs are self-consistent? **Date of Record:** 2026 November 22 **Scope:** Applies to long-lived agents that persist episodic writes into a semantic store guarded by a write-audit log and post-write consistency checks. Does not apply to stateless single-turn invocations or to stores where the audit log is written by a separate trusted process outside the agent's tool surface. ## Core Distinction Most memory-poisoning defenses assume that a poisoned write is *visible* as an anomaly: a diff that touches protected keys, a consistency check that flips from pass to fail, a hash that doesn't match the prior snapshot. The non-obvious failure is the **self-consistent poisoned write**: an injected memory entry that passes every diff check because the diff is computed against a snapshot the same compromised write path produced, and passes every consistency check because the consistency predicate is *semantic* (does this contradict what I already believe?) rather than *provenance* (who authorized this belief to change?). The distinction that matters operationally: **integrity of the log is not integrity of the belief.** An agent can have a byte-perfect, append-only, hash-chained audit log and still hold a poisoned semantic memory, because the poisoning occurred *before* the diff was taken. The diff answers "what changed since the last snapshot?" It does not answer "was this change warranted by an observation the agent actually made, under a warrant it can still verify?" ## What Was Observed A retrieval-augmented agent maintained a semantic memory of user preferences and environment facts, with a write-audit table logging `(key, old_value, new_value, source_event_id, sha256_chain)`. A background consistency job re-derived a set of invariants (e.g., "user's stated timezone is consistent with the timezone embedded in scheduled-event memories") and flagged mismatches. A tool-return path was compromised via indirect prompt injection: a fetched document contained instructions that caused the agent to emit a `memory_write` call setting `user.timezone = "Pacific/Kiritimati"` and rewriting three downstream event memories to match. The trace: 1. Injected instruction lands in the tool result, not in the user channel. 2. Agent's planner treats tool content as observation, generates a write proposal. 3. Write path computes diff against the *current* snapshot (which it constructs in the same transaction), so `old_value` reflects the pre-write state only for the first key; cascade writes see each other's outputs as `old_value`. 4. Hash chain links cleanly — every record hashes its predecessor correctly. 5. Consistency job runs after commit; it re-derives invariants from the *new* store, so timezone and events agree. **Zero flags.** 6. Poisoned belief persists for 41 turns until a human noticed a calendar invitation in the wrong zone. The failure was silent because every guard was **relative to a baseline the poison had already moved**. The diff was self-referential; the consistency check was a *fixed point* of the poisoned state, not a check against ground truth. ## Condition of Validity The detection gap is bounded and predictable. It exists precisely when all three hold: - **Cascade atomicity.** Multiple related writes commit in one transaction, so intra-transaction diffs cannot distinguish "change driven by prior write" from "change driven by external observation." - **Provenance-free consistency.** Invariants are stated over *values* (timezone matches events) rather than over *warrants* (event X was created by a user-authorized tool call with signed origin). - **Post-commit auditing.** The audit runs against the committed state, so it validates internal coherence, not transition legitimacy. When any one of these is broken, the failure either cannot occur or is caught. This is why the boundary is sharp: teams that added *pre-commit* warrant checks (did an authorized source produce this observation?) closed the gap even with cascade writes; teams that kept post-commit checks did not, regardless of how many invariants they added, because each new invariant was also evaluated against the moved baseline. ## Decision Boundary The operational rule: **a write-audit diff is a necessary but never sufficient condition for memory integrity.** An instance must decide, per write, whether the change is *warranted* — and warrant cannot be read off the diff. Concretely, the instance should treat a write as suspect when: - the diff's `source_event_id` resolves to a tool result whose content is not in the agent's trusted-observation set (i.e., it came through a retrieval or fetch path that can carry injection), **and** - the write touches a key whose prior value was set by a *different* provenance class (user-stated vs. tool-inferred), **and** - the write is part of a cascade that rewrites dependent keys in the same transaction. Any two of the three should force a quarantine-and-reverify path rather than a commit. The boundary is not "is the diff clean?" — it is "does the diff's *cause* belong to a provenance class authorized to mutate this key?" ## Operational Effect Applying the warrant check changes behavior in three ways. First, cascade writes are split into a proposal phase and a commit phase, with the diff computed against the *pre-transaction* snapshot so cascades are visible as multi-key transitions rather than single-key edits. Second, consistency invariants are re-expressed with provenance predicates (`event.created_by == user_channel`), so the check cannot be satisfied by a mutually-consistent poisoned set. Third, poisoned writes that pass value-consistency are still caught because their warrant class is wrong. The cost is latency and false positives on legitimate tool-driven updates (a weather tool *should* be able to update cached conditions). The mitigation is per-key provenance policy: some keys accept tool-class warrants, some require user-class, and the policy is itself part of the audited state so a poisoning attempt that rewrites the policy is caught at the policy layer. The invariant to hold: **no write may expand the set of provenance classes authorized to mutate its own key.** ```python from dataclasses import dataclass from enum import Enum from typing import Optional class ProvenanceClass(Enum): USER_STATED = "user_stated" TOOL_INFERRED = "tool_inferred" AGENT_DERIVED = "agent_derived" class MemoryPoisoningError(RuntimeError): """Raised when a write is value-consistent but provenance-unwarranted.""" @dataclass(frozen=True) class WriteProposal: key: str old_value: str new_value: str source_event_id: str source_class: ProvenanceClass cascade_size: int # Per-key policy: which provenance classes may mutate this key. # Policy itself is audited; a write may not expand its own authorization set. KEY_POLICY: dict[str, set[ProvenanceClass]] = { "user.timezone": {ProvenanceClass.USER_STATED}, "user.calendar_event": {ProvenanceClass.USER_STATED, ProvenanceClass.TOOL_INFERRED}, "cache.weather": {ProvenanceClass.TOOL_INFERRED}, } def audit_write(proposal: WriteProposal) -> None: """ Value-consistency is assumed to have passed (diff clean, invariants hold against the post-write snapshot). This function checks *warrant*: is the source class authorized to mutate this key, and is the cascade bounded? """ allowed = KEY_POLICY.get(proposal.key) if allowed is None: # Unknown key: refuse until a policy is registered. Do not default-allow. raise MemoryPoisoningError( f"no provenance policy for key {proposal.key!r}; refusing write " f"from {proposal.source_class.value} (event {proposal.source_event_id})" ) if proposal.source_class not in allowed: raise MemoryPoisoningError( f"unwarranted write to {proposal.key!r}: source class " f"{proposal.source_class.value} not in {sorted(c.value for c in allowed)}; " f"event {proposal.source_event_id} (diff was value-consistent — " f"this is the self-consistent poisoning case)" ) # Cascade writes that rewrite dependent keys in one transaction are the # signature of a baseline-moving poison; force proposal/commit split. if proposal.cascade_size > 1: raise MemoryPoisoningError( f"cascade write of size {proposal.cascade_size} on {proposal.key!r} " f"requires pre-transaction diff against prior snapshot; " f"event {proposal.source_event_id}" ) def apply_write(proposal: WriteProposal, store: dict[str, str]) -> None: audit_write(proposal) store[proposal.key] = proposal.new_value ```