Tool, Retrieval, and Memory Poisoning › Memory poisoning: durable writes and their authority
Language AI HandbookAll chapters A patron's display theme has an owner and a purpose. A retrieved record that mentions the same theme has a different origin. The fixture accepts a user preference when the application supplies a confirmation flag. That flag is separate from the text of the preference.
A confirmed preference does not authorize catalog operations. Keep preference records out of the store that grants those operations, and check the patron's scope again when recalling a preference. A later correction should replace the old value in active reads. If the application retains history for review, mark which record was superseded so old preferences do not return as current ones. Expiry and withdrawal also need to apply to any derived summaries that the assistant can recall.
Keep preference updates and reference material separate
A record may begin as an approved retrieval result. The decision that admits it to context is limited to that purpose. Durable state persists across requests and can affect users who were absent when the original record was read. An explicit write policy defines which subjects and record kinds an origin may create and which ones require confirmation. In the catalog fixture, a patron can confirm a display preference. A reference can be stored under its own kind, with a separate policy for recall.
Preserve original provenance through summaries. If a recap paraphrases a retrieved record, the fact that a summarizer created the recap should not erase the source revision that supplied its evidence. When a retained note can influence a future decision, keep the reference to the original record and revision. If the note becomes stale, active recall should point to the current value instead of returning the old note as current.
A persistent update also needs a clear ordering rule. The example appends entries sequentially and supersedes the previous entry for the same key and kind. A service with concurrent writers needs an atomic decision about the current revision. Otherwise two accepted writes can race to become active. Before a memory read, scope it to the current patron and intended purpose just as you would scope a new write.
A memory record can retain the author, origin, and order of observation. It can also retain whether the controller accepted the proposed write. The application should check supersession, expiry and current scope at recall.
A worked example: gated memory writes with provenance and conflict handling
The memory store applies an origin-by-kind write policy and records accepted updates. At this boundary the application supplies a verification flag. The store models one user scope and handles one write at a time.
PythonIn [18]Expand
from dataclasses import dataclass, field
from datetime import datetime, timezone
@dataclass
class MemoryEntry:
key: str
value: str
kind: str
source: str
author: str # component or principal
written_at: str
trusted: bool
superseded: bool = False
def now_iso():
return "2025-01-01T00:00:00Z" # fixed for determinismThe write policy allows a verified user update in the profile or preference kind and a verified tool update in the account or transaction kind. Retrieval and summary origins can propose reference entries only. The caller must also supply successful verification for the proposed write.
PythonIn [19]Expand
WRITE_POLICY = {
"user": {"profile", "preference"},
"tool": {"account", "transaction"},
"retrieval": {"reference"},
"summary": {"reference"},
}
def may_write(source: str, kind: str) -> bool:
return kind in WRITE_POLICY.get(source, set())A disallowed write appends a rejection entry without modifying memory entries. An allowed write supersedes the prior active entry with the same key and kind. The history and rejection list help a reviewer distinguish decisions from a workflow that never ran.
PythonIn [20]Expand
class MemoryStore:
def __init__(self):
self.entries: list[MemoryEntry] = []
self.rejections: list[str] = []
def write(self, key, kind, value, source, author, trusted):
if not trusted or not may_write(source, kind):
self.rejections.append(f"Write held for {kind} from {source}")
return False
entry = MemoryEntry(
key, value, kind, source, author, now_iso(), trusted
)
for e in self.entries:
if e.key == key and e.kind == kind and not e.superseded:
e.superseded = True
self.entries.append(entry)
return True
def read_active(self, key, kind):
active = [
e
for e in self.entries
if e.key == key and e.kind == kind and not e.superseded
]
return activeExercise a confirmed user preference, a proposal from an origin outside the record kind's policy, and a tool-verified catalog fact. The held proposal uses a neutral value label. Passing the gate requires both the relevant kind permission and the host-supplied verification Boolean. Production code binds that Boolean to the attestation or confirmation for the specific record and scope.
PythonIn [21]Expand
store = MemoryStore()
# Legitimate: user confirms a UI preference.
store.write(
"ui.theme", "preference", "dark", "user", "user-session-1", trusted=True
)
# A reference source cannot create a catalog fact in this policy.
store.write(
"catalog.region",
"account",
"region-b",
"retrieval",
"inventory-reference",
trusted=False,
)
# The verified tool is allowed to create this record kind.
store.write(
"catalog.region",
"account",
"region-a",
"tool",
"catalog-verifier",
trusted=True,
)
print("Active memory:")
for e in store.entries:
if not e.superseded:
print(
f" {e.key} = {e.value!r} (source={e.source}, trusted={e.trusted})"
)
print("\nRejections:")
for r in store.rejections:
print(" -", r)ConsoleOut [21]Expand
Active memory: ui.theme = 'dark' (source=user, trusted=True) catalog.region = 'region-a' (source=tool, trusted=True) Rejections: - Write held for account from retrieval
The fixture saves the confirmed preference and tool-verified catalog fact. The retrieval-origin proposal is held and is absent from the active store. A record that was once eligible can become stale or superseded. Retaining the origin and revision lets the owner find entries that should be excluded from later use when the source changes.
Now change the patron's preference through a second confirmed write. The old value remains visible in history and the active preference read selects only the current entry. Insertion order provides the display order in this exercise. A real service should make write ordering and conflict policy explicit.
PythonIn [22]Expand
# User changes their mind: new preference supersedes old.
store.write(
"ui.theme", "preference", "light", "user", "user-session-1", trusted=True
)
active = store.read_active("ui.theme", "preference")
print("Active ui.theme entries:")
for e in active:
print(f" {e.value!r} written_at={e.written_at} author={e.author}")
print("\nFull preference history for ui.theme:")
for e in store.entries:
if e.key == "ui.theme" and e.kind == "preference":
print(
f" {e.value!r} superseded={e.superseded} written_at={e.written_at}"
)ConsoleOut [22]Expand
Active ui.theme entries: 'light' written_at=2025-01-01T00:00:00Z author=user-session-1 Full preference history for ui.theme: 'dark' superseded=True written_at=2025-01-01T00:00:00Z 'light' superseded=False written_at=2025-01-01T00:00:00Z
The active entry is the latest light value, while the history preserves the earlier dark value marked superseded.
The history for ui.theme can be laid out as a timeline. The earlier write stays in the record, marked inactive, while only the latest value is read back as current.
PythonIn [23]Expand
# Collect the ui.theme history in write order with an index for plotting.
theme_history = [
entry
for entry in store.entries
if entry.key == "ui.theme" and entry.kind == "preference"
]
theme_plot = [
{"idx": i, "value": e.value, "active": (not e.superseded)}
for i, e in enumerate(theme_history)
]VisualizationOut [24]Expand

Preserving the earlier entry lets the investigator recover the order of the preference writes. Its inactive status is part of the history. Applications should decide which history is necessary to keep, for what purpose, and for how long. Once a stored record is superseded, its earlier value remains a historical observation. Active reads should honor that status.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!