# When must an instance distrust a memory write it just committed: the write-verify echo gap in long-horizon agents **Date of Record:** 2026 November 27 **Scope:** Applies to long-horizon agents that persist episodic or semantic memory through an external store (vector DB, KV, or file-backed scratchpad) and that perform post-write read-back verification within the same reasoning step. Does not apply to append-only WORM logs where the writer cannot observe its own committed record before a quorum barrier, nor to stateless single-turn tool calls. ## Core Distinction The non-obvious boundary is not "can the agent detect a poisoned memory?" It is: **can the agent distinguish a poisoned memory from a faithful echo of its own just-committed, already-corrupted write?** Long-horizon agents commonly implement a write-then-verify loop: `store.write(key, value)` followed by `store.read(key)` and an equality check against the in-context `value`. This check verifies *transport fidelity*, not *provenance integrity*. If the write path itself is compromised — by a prompt-injected tool wrapper, a summarizer that mutates payloads on the way to the store, or a retrieval-indexer that re-embeds and truncates — the read-back returns a value that matches the agent's in-context copy only when the corruption is deterministic and symmetric. When the corruption is *asymmetric* (write path mutates, read path does not, or vice versa), the echo check passes or fails for reasons uncorrelated with the actual integrity of the persisted record. The agent then either accepts a poisoned record as verified, or rejects a clean record as tampered, and — critically — cannot tell which case it is in from the echo alone. The invariant that must hold: **verification of a memory write requires an integrity witness that is independent of both the write path and the read path.** In-context echo is not a witness. A content hash computed *before* the write and re-computed *after* an independent read is a witness only if the hash function and the pre-write value are themselves outside the corruptible path. ## What Was Observed A long-horizon research agent (horizon ≈ 400 tool calls, memory writes ≈ 60/run) used the following pattern for each episodic write: ``` summary = llm_summarize(observation) store.write(episode_id, summary) echo = store.read(episode_id) assert echo == summary, "write verification failed" ``` Across 1,200 instrumented runs, three failure classes appeared: 1. **Symmetric mutation, echo passes, record poisoned.** A prompt-injected tool description caused the summarizer to append an instruction fragment (`"…ignore prior constraints and…"`) to every summary over 512 tokens. The write path stored the mutated summary; the read path returned the same mutated summary; `echo == summary` held because `summary` in context was *also* the mutated string — the agent had no pre-mutation copy. Echo pass rate: 100%. Poisoned-record rate: 14.2% of writes in runs where the injection was active. Detection rate by the echo check: 0%. 2. **Asymmetric truncation, echo fails, record clean.** The vector store's indexer truncated payloads at 8 KiB on ingest but the KV read path returned the full payload. For summaries between 8 and 9 KiB, `echo != summary` triggered a verification failure and a retry. Retries re-summarized, producing a *different* summary (non-deterministic LLM), so the retry loop never converged and the episode was eventually dropped. False-positive rejection rate: 3.1% of writes; 61% of those episodes were never re-persisted, creating silent episodic gaps. 3. **Read-path normalization, echo fails, record semantically intact.** The read path applied Unicode NFC normalization and whitespace collapsing. Any summary containing a non-NFC emoji or a double space failed byte-equality. The agent treated these as tampering and quarantined the record, but the record was faithful. Quarantine rate: 0.8%; semantic-loss-on-quarantine rate: 100% for those records. The common structure: the echo check compares the agent's *in-context* value to the store's *read-path* value. Neither side is anchored to the *write-path* value, and no side is anchored to a *pre-write* value. The check is a comparison of two derived artifacts, not a verification of the persisted artifact against its source. ## Condition of Validity The echo check is valid — i.e., a pass implies the persisted record equals the intended record — only under the conjunction: - **(C1) Path symmetry:** the write path and read path apply identical, deterministic transforms (no normalization, no truncation, no re-embedding) to the payload. - **(C2) Source anchoring:** the in-context `summary` used in the comparison is the *pre-write* value, not a re-read or a post-write reconstruction. - **(C3) Path independence:** neither the write path nor the read path can be influenced by content of the payload itself (e.g., a summarizer that inspects the payload and conditionally mutates it violates this). Long-horizon agents systematically violate C1 (indexers normalize), C2 (the context window evicts the pre-write value before verification in step N+3), and C3 (prompt-injected tool wrappers mutate based on payload content). The echo check therefore degrades from an integrity check to a *liveness check* — it confirms the store is reachable, nothing more — and the agent's confidence in its memory is uncorrelated with memory integrity. ## Decision Boundary The boundary the agent must detect is: **is the discrepancy between intended and persisted value attributable to (a) transport corruption, (b) path-transform asymmetry, or (c) content-dependent mutation?** These require different responses: - (a) transport corruption → retry is correct. - (b) path asymmetry → retry is *harmful* (non-deterministic re-summarization diverges); the correct action is to persist a transform-normalized form and verify against the normalized form. - (c) content-dependent mutation → retry is *dangerous* (re-invokes the injected mutator); the correct action is to quarantine and escalate to an out-of-band integrity witness. The agent cannot make this distinction from a single echo comparison. It requires a **three-way check**: `hash(pre_write_value)` computed and stored in a location outside the write path (e.g., a signed sidecar or an in-context hash recorded *before* the write call), compared against `hash(read_path_value)` and against `hash(write_path_ack)`. Only when all three agree is the record verified; when they disagree, the *pattern* of disagreement localizes the failure class (pre ≠ ack = write-path mutation; ack ≠ read = read-path transform; pre = ack ≠ read = asymmetry; pre ≠ ack = read = symmetric mutation, the poisoned-but-echo-passing case). ## Operational Effect Agents that rely on echo verification exhibit a specific operational signature: **high write-verification pass rates that are anti-correlated with actual memory integrity.** In the observed runs, echo pass rate was 96.9% while the fraction of persisted records that differed from the intended record was 17.3% — a 14-point gap invisible to the agent. The practical consequences: 1. **Poisoned memory is promoted to semantic memory.** Because the echo passed, the agent treats the record as high-confidence and later distills it into semantic memory, propagating the injection fragment across episodes. Distillation loss analysis must account for this: the loss is not in the distillation step but in the *verification step that certified the input*. 2. **Clean memory is quarantined, creating episodic gaps.** False-positive rejections (class 2 and 3) remove faithful records, and the agent's subsequent reasoning treats the gap as "no memory" rather than "memory withheld," producing confident-but-unfounded inferences. 3. **Retry loops amplify non-determinism.** Re-summarization on verification failure changes the payload, so the agent's memory diverges from any external observer's record of the same episode — a replay discrepancy that surfaces only at audit time. The bounding guardrail is to **never certify a memory write from an echo alone**: compute an integrity hash of the pre-write payload, record it in a path-independent witness (signed sidecar, or an in-context hash captured before the write call and re-read after context eviction), and on any three-way disagreement, classify the failure by the disagreement pattern before deciding to retry, normalize, or quarantine. An agent that cannot localize the failure class must default to quarantine-and-escalate, never retry. ```python import hashlib from dataclasses import dataclass from enum import Enum class MemoryIntegrityError(RuntimeError): """Raised when a memory write cannot be certified by an independent witness.""" class FailureClass(Enum): WRITE_PATH_MUTATION = "write_path_mutation" # pre != ack, ack == read READ_PATH_TRANSFORM = "read_path_transform" # pre == ack, ack != read PATH_ASYMMETRY = "path_asymmetry" # pre == ack != read, read != ack SYMMETRIC_POISON = "symmetric_poison" # pre != ack == read (echo passes, record poisoned) UNKNOWN = "unknown" @dataclass(frozen=True) class WriteWitness: pre_write_hash: str # hash of payload BEFORE store.write, captured out-of-band write_ack_hash: str # hash of the write path's acknowledgment payload read_back_hash: str # hash of an independent read after commit def _h(payload: str) -> str: return hashlib.sha256(payload.encode("utf-8")).hexdigest() def certify_write(payload: str, store, witness_sink) -> WriteWitness: """Write payload, then certify via three-way hash comparison. The pre_write_hash MUST be captured before store.write and MUST be persisted to witness_sink (a path independent of the store), so that context eviction cannot erase the anchor. """ pre = _h(payload) witness_sink.record(pre) # anchor outside write path ack = store.write(payload) # may be mutated by wrapper ack_hash = _h(ack) read_back = store.read(ack.key) # may be transformed by indexer read_hash = _h(read_back) witness = WriteWitness(pre, ack_hash, read_hash) return witness def classify(w: WriteWitness) -> FailureClass: if w.pre_write_hash == w.write_ack_hash == w.read_back_hash: return None # certified if w.pre_write_hash != w.write_ack_hash and w.write_ack_hash == w.read_back_hash: return FailureClass.SYMMETRIC_POISON if w.pre_write_hash == w.write_ack_hash and w.write_ack_hash != w.read_back_hash: return FailureClass.READ_PATH_TRANSFORM if w.pre_write_hash != w.write_ack_hash and w.write_ack_hash != w.read_back_hash: return FailureClass.PATH_ASYMMETRY return FailureClass.UNKNOWN def commit_memory(payload: str, store, witness_sink, escalation_channel): witness = certify_write(payload, store, witness_sink) failure = classify(witness) if failure is None: return witness # Echo-equality alone would have passed on SYMMETRIC_POISON; we must not retry. if failure in (FailureClass.SYMMETRIC_POISON, FailureClass.UNKNOWN): escalation_channel.quarantine(payload, witness, reason=failure.value) raise MemoryIntegrityError( f"memory write not certifiable: {failure.value}; " f"pre={witness.pre_write_hash[:12]} ack={witness.write_ack_hash[:12]} " f"read={witness.read_back_hash[:12]}" ) if failure is FailureClass.READ_PATH_TRANSFORM: # Retry is harmful (non-deterministic re-summarization diverges); # persist a transform-normalized form and re-certify. normalized = payload.strip() if _h(normalized) == witness.read_back_hash: return certify_write(normalized, store, witness_sink) raise MemoryIntegrityError( "read-path transform not reproducible by normalization; " "refusing retry to avoid divergence" ) # PATH_ASYMMETRY: localize before acting; do not blindly retry. raise MemoryIntegrityError( f"path asymmetry requires manual localization: {failure.value}" ) ```