Repository navigation
A failed expiry-sweep search mirror is recorded as a projection failure and replayed - #414
Merged
Merged
Conversation
…re and replayed The next pass never selects a key the sweep wrote (tombstoned, or its expiry cleared), so a failed mirror left a demoted memory hidden from search and an anonymized one's entity names in the vector tables. Each key's mirror is now recorded as a memory_expiry_mirror projection failure, which projection_repair replays; the demote copy is pinned at the memory row's version so a late replay leaves a later write's vectors alone. Fixes #399 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…te the residual Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…s cutoff is stated and tested Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…e entity_vectors cutoff plainly Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…malformed item Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A touch after the sweep copies the key's vectors at read + 1, below the sweep's version, so a late demote copy would clear the expiry it set. The replay reads the authority's head version and skips a demote whose head moved. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Gating the demote on an unchanged head version dropped it after a lock, unlock or verify, which move the head and leave the vectors' past expiry. The replay now reads the live head and copies its expiry and confidence at its version: a touch keeps its expiry, and every other later write still gets the demote. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…s the action, not unread overrides A late replayed retire copied each vector's text, so one landing after a redaction's purge put the purged text back. Retires now write bare tombstones, a replayed demote copies only rows of the head's text, and the sweep's mirror uses the sweep's client. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ize retires bare A bare tombstone at the sweep's version left memory.restore a vector-less row, since the restore takes its vector from the newest row of the key. A tombstone now keeps its vector as before; an anonymize, whose text must go, retires bare. The recorded failure names the action. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… read + 1 Pinned at the head's version, the copy could tie with the row the head's writer lands at that version and win with older tags or status. The replay now copies the head's expiry and confidence at read + 1, so a later row is newer. The recorded payload is checked item by item. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… own copy does Limiting the copy to rows of the head's content hash skipped vectors written before embedding rows carried a hash, and the failure then resolved with nothing copied. The tests define their fakes in place and cover a mirror failure that cannot be recorded. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…ich writes outrank a replay Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…eplay test covers a head relabelled high Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… still the anonymized shell While the mirror is owed, a status write such as a supersede can copy the key's original vector above the recorded version, where a retire pinned there does not reach it. The replay reads the head and, while it is the shell, pins the bare retire at its version + 1. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
… the version-rule docstrings name the sweep's pinned demote Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…'s replays Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
A late replay can meet a redaction issued at any time: a full copy read before the purge and inserted after it kept the redacted text. A restore after a replayed tombstone restores a stub the embedding worker fills from the memory's value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…the sweep alone Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
…the replay owns its sequential reads An entity heal can copy a supersede's copy of the old text to head + 1, where a head + 1 pin does not reach it. The pin is head + 2, above every vector-only copy. The demote overrides travel apart from the items, so the sweep and the record share one item type. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: zlareb1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #399
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
When the expiry sweep writes a memory's post-action but fails to update its search vectors, it records the miss as a projection failure, and the projection repair applies it later. Before this change, a demoted memory could stay hidden from search, and an anonymized one could keep its entity names in the vector tables.
Why
run_passwrites the version rows first, then mirrors the action ontomemory_embeddingsandentity_vectors. A failed mirror was logged and reported asmirror:infailures, and the comment said the next pass would redo it. The next pass never does:find_expired_candidatesselects only live rows with a pastexpires_at, and the version row the pass wrote is a tombstone or has no expiry. So the miss was never redone by the sweep:expires_at, so search kept hiding it;entity_vectors;orphan_vector_reconcile, which tombstones vectors whose memory row is tombstoned, reached them. Reads hid them through their expiry meanwhile. The demote and anonymize rows stay live, so that pass never selects them.Now a failed mirror is recorded in
loom.memory_projection_failuresas onememory_expiry_mirrorrow per writer (org, namespace, agent, user_id). The payload lists each key's identity key, the memory row's version and itson_expireaction.projection_repairandops.projection_failures.retryreplay the row as one batch. Its retires go toretire_vectors_for_keysin one bare batch, and a demote is replayed as a metadata copy of the head (below). The replay runs undersequential_reads(), asretire_old_vectorsdoes, so its head and vector reads see a row that landed on another replica.A replay can run long after the sweep, so the mirror has to be safe to apply late.
version_overkeeps the memory row's version above any vector-only copy of the head (read + 1), so on the first attempt the demote copy still reaches every vector-only copy of the head.memory.touchor amemory.verifycopies the key's vectors at read + 1, which lands below the sweep's version. A lock copies nothing, and anupdate_by_idwrites its row at its own version. So a replayed demote (replay_mirror) does not use the recorded version. It reads the key's live head from the authority (memory_rows_by_identity_keys, sequentially consistent) and copies that head's expiry and confidence as an ordinary metadata copy, at read + 1 over the key's live vectors, as the sweep's own copy reads them. A touch's expiry stays, a locked or verified key still gets the demote, and a row that a writer drawing its own version (memory.set,update_by_id) lands after the vector read is newer than the copy. A key with no live head, or ahighone, has nothing to copy. A tombstone runs as recorded, because the vectors below the version hold the text it took out. In the replay it is bare: a late replay can meet a redaction issued at any time, and a full copy that reads before the redaction's purge and inserts after it would keep the redacted text. Amemory.restoreafter a replayed tombstone then restores a vector-less row that the embedding worker re-embeds, at the cost of one embedder call. On the first attempt a tombstone keeps its vector, as onmain, so a restore brings it straight back.mirror_status_to_embeddingsis one such write. So while the head is still the anonymized shell, which keeps no vector, the replay acts on it. The shell is a head with the marker'scontent_sha256and the sweep'sexpired:anonymizeredaction reason, so amemory.setof the marker text does not count. The replay retires every live vector of the key through head + 1, pinned at the head's version + 2. A metadata copy of the supersede's copy, such as an entity heal, sits at head + 1, and a vector-only copy is never more than one above a head (reemit_version). A write drawn over the head (version_over, at least head + 2) can tie with the pin only when its clock trails the head's. In that case the later insert of the two wins. A head rewritten since (amemory.set) takes the recorded version, so its new vectors stay.retire_vectors_for_keys(..., bare=True)), in the sweep and in the replay. Itsmemory_embeddingstombstones then carry no text or vector, so one that lands after a redaction's purge leaves nothing to scrub. Itsentity_vectorstombstones are still full copies (tombstone_entity_vectors_for_keys), as onmain. "Anonymize, then redact the original version" is the expected sequence, and a late replay widens that window. A tombstone keeps its vector in the retire, as before, becausememory.restorerebuilds the restored row's vector from it. Under the Postgres authority the delivery already retires the old vectors bare (retire_old_vectors).Scope and risk
server/memory/app/expiry.py(the mirror is split into_mirror_items,apply_mirrorfor the sweep andreplay_mirrorfor the repair, each item carrying itson_expireaction, and a failure is recorded) andserver/memory/app/projection_failures.py(the new kind and its replay). Docstrings inserver/memory/store/embeddings.pyandserver/memory/store/rows.pyname the sweep's pinned demote, anddocs/architecture/memory-architecture.mdanddocs/architecture/failure-and-recovery.mddescribe the replay.except. Every fail-open projection has this window. Closing it would take an intent written before the insert, plus a head check, a grace period and a resolving mutation on every pass. That costs more than the residual, which is narrower than before because the record follows the failed mirror directly.memory-architecture.mdstates it.verifiedflag stays until the next write that copies the vector. The pin is kept because it protects a later write's own vectors, which sit at or above the version, from the demote's values.mirror_status_to_embeddings) draws its version before it reads. If it draws after the sweep's version and reads before the first-attempt demote copy lands, its copy, carrying the past expiry, sits above the pin and is kept. Search then hides the live, demoted memory until the next write that copies its vectors. The window is the few milliseconds between the demote's read and its insert.memory.restorecan rebuild the shell's vector from that tombstone, because restore takes the newest vector of the key whatever its text. Onmain, every anonymize leaves such a tombstone, even when its mirror succeeds. This PR's bare retire removes it on the first attempt and narrows it to the window while a mirror is owed. Closing it means restore must not revive a vector of a different content hash, a change tomemory.restorethat this PR leaves alone.memory.setorupdate_by_idthat lands both its row and its vector between those two reads is copied at read + 1 with the older head's expiry and confidence. Its TTL is then missing on the search side until the sweep reaches the row. This needs a write within milliseconds of a rare replay. Limiting the copy to the head's content hash would close it, but it would skip vectors written before embedding rows carried a hash, which is the case it would have to cover.mirror:, which is the behaviour before this change.mirror:<n> keyscount now leaves out demotedhighrows. Those never had a vector to mirror.user_id.Compatibility, rollout and rollback
memory.sweep_expiredreturns the same fields.memory_expiry_mirrorrow active asunsupported_projection, and N replays it once N-1 stops serving. This is rolling-upgrades item 17.memory_expiry_mirrorrows stay active, and the reverted build reports them asunsupported_projection.Tests
test_a_failed_mirror_is_recorded_per_writer_and_the_repair_applies_it(tests/unit/test_memory_expiry.py): it fails the retire for keys of two writers, checks one failure per writer with each key's item, then runsretry_failureson those rows (the replayed tombstone retires bare). It checks that each tombstone is retired at the memory row's version and that the demoted key gets a metadata copy of its head. It fails onmainand passes here.test_the_sweeps_mirror_leaves_a_later_writes_vectors_alone: it runs the realreemit_embedding_rows_for_keysandretire_vectors_for_keysover vector rows above and below the sweep's pin. It checks that only the older rows are copied or retired, at the pin, and that theentity_vectorsretire is cut off at the pin's second. It also checks that an anonymize's tombstone holds no excerpt or vector, that a tombstone keeps its vector for a restore, and that a demote copy keeps its text.test_a_replay_retires_bare_and_copies_the_head_unpinned: through the same store code, a replayed tombstone holds no text or vector, an anonymized shell's copy at head + 1 is retired at head + 2, and a replayed demote copies the head's expiry at read + 1.test_a_malformed_expiry_mirror_failure_is_abandoned: an item with a short key, a string key, a non-integer version or an unknown action is abandoned asmalformed_payloadand does not stay active.test_a_replay_mirrors_the_heads_as_they_are_now: after a touch and after a lock, the replayed demote copies the head's expiry and confidence with no pinned version. An anonymized shell whose head a supersede moved is retired through head + 1. A rewritten key, and one holding the marker text from amemory.set, are retired only below the recorded version. A deleted key and a key relabelledhighare skipped, and the anonymize runs as recorded.test_a_mirror_failure_that_cannot_be_recorded_still_reports: with both the mirror and the record failing, the pass returns its counts and themirror:failure.test_sweep_writes_one_batched_insert_and_mirrorsnow checks the demote copy's pinned version and that only the anonymize retires bare.LOOM_ENV_FILE= .venv/bin/python -m pytest -q:make format lint typecheckpass, and so dotests/unit/test_memory_expiry.py,tests/unit/test_projection_failures.py,tests/unit/test_memory_webhooks.py,tests/unit/test_doc_links.pyandtests/unit/test_public_copy.py(417 passed). A fulltests/unitrun (-n 6 -p randomly) on an earlier head of this branch gave 30,679 passed and 1 failed. The failure istest_deeply_nested_hidden_html_parses_in_bounded_time, a wall-clock bound that fails on this shared host and does not touch this change. The full suite runs again on the rebased head before the merge.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code. The diagnosis was checked against
find_expired_candidatesand the version pinning in_copy_row/retire_vectors_for_keys, and the new test was shown to fail onmain.I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed. (Format, lint and typecheck pass; the full unit suite runs on the rebased head before the merge; CI runs the integration suite.)main; no merged migration is edited. (No migration.)frontend/changed. (Unchanged.)docs/describe the system as it now is:docs/architecture/memory-architecture.md(expiry) anddocs/architecture/failure-and-recovery.md(the repair worker).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code