Skip to content

A failed expiry-sweep search mirror is recorded as a projection failure and replayed - #414

Merged
zlareb1 merged 25 commits into
mainfrom
fix/expiry-mirror-retry
Nov 8, 2026
Merged

zlareb1 merged 25 commits into
mainfrom
fix/expiry-mirror-retry

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #399

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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_pass writes the version rows first, then mirrors the action onto memory_embeddings and entity_vectors. A failed mirror was logged and reported as mirror: in failures, and the comment said the next pass would redo it. The next pass never does: find_expired_candidates selects only live rows with a past expires_at, and the version row the pass wrote is a tombstone or has no expiry. So the miss was never redone by the sweep:

  • demote: the memory was live again, but its embedding rows kept the past expires_at, so search kept hiding it;
  • anonymize: the vectors were never retired, so the original entity names stayed in entity_vectors;
  • tombstone: the vectors stayed until 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_failures as one memory_expiry_mirror row per writer (org, namespace, agent, user_id). The payload lists each key's identity key, the memory row's version and its on_expire action. projection_repair and ops.projection_failures.retry replay the row as one batch. Its retires go to retire_vectors_for_keys in one bare batch, and a demote is replayed as a metadata copy of the head (below). The replay runs under sequential_reads(), as retire_old_vectors does, 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.

  • A retire already touched only vectors below the memory row's version. The demote copy now carries that version too, instead of read + 1, so it never rewrites a row that a later write landed above it. version_over keeps 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.
  • Later writes change the head, and the vectors with it, in different ways. A memory.touch or a memory.verify copies the key's vectors at read + 1, which lands below the sweep's version. A lock copies nothing, and an update_by_id writes 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 a high one, 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. A memory.restore after 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 on main, so a restore brings it straight back.
  • An anonymize is replayed from the head as well. While the mirror is owed, the key's original vector stays live, and a status write can copy it above the recorded version. A supersede through mirror_status_to_embeddings is 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's content_sha256 and the sweep's expired:anonymize redaction reason, so a memory.set of 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 (a memory.set) takes the recorded version, so its new vectors stay.
  • An anonymize now retires the vectors bare (retire_vectors_for_keys(..., bare=True)), in the sweep and in the replay. Its memory_embeddings tombstones then carry no text or vector, so one that lands after a redaction's purge leaves nothing to scrub. Its entity_vectors tombstones are still full copies (tombstone_entity_vectors_for_keys), as on main. "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, because memory.restore rebuilds 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

  • Touches server/memory/app/expiry.py (the mirror is split into _mirror_items, apply_mirror for the sweep and replay_mirror for the repair, each item carrying its on_expire action, and a failure is recorded) and server/memory/app/projection_failures.py (the new kind and its replay). Docstrings in server/memory/store/embeddings.py and server/memory/store/rows.py name the sweep's pinned demote, and docs/architecture/memory-architecture.md and docs/architecture/failure-and-recovery.md describe the replay.
  • A pass cut off between its version insert and the record records nothing: a cancelled task, or a process that dies, never reaches the 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.md states it.
  • On the first attempt the demote copy is pinned at the sweep's version. A touch or verify whose vector copy lands, at read + 1, in the few milliseconds between the demote's read and its insert therefore loses to the demote. A lost expiry heals at the next sweep, which selects the key once the head's expiry passes. A lost verified flag 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.
  • A judged status mirror (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.
  • A replayed demote and a concurrent touch or verify both copy the key's vectors at read + 1. If both read the same vector row, they tie at one version and the later insert wins. A replay landing last then puts back the pre-touch expiry, which heals at the next sweep.
  • An anonymized key deleted while its mirror is owed: the delete's mirror copies the still-live original vector into a full tombstone. The replay then finds no live head and no live row, and a later memory.restore can rebuild the shell's vector from that tombstone, because restore takes the newest vector of the key whatever its text. On main, 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 to memory.restore that this PR leaves alone.
  • A replayed demote reads the head, then the vectors. A memory.set or update_by_id that 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.
  • If recording the failure also fails, the error is logged and the pass still reports mirror:, which is the behaviour before this change.
  • The mirror:<n> keys count now leaves out demoted high rows. Those never had a vector to mirror.
  • A user erasure finds the recorded rows: their payload carries user_id.

Compatibility, rollout and rollback

  • Wire: none. memory.sweep_expired returns the same fields.
  • A projection repair on build N-1 leaves a memory_expiry_mirror row active as unsupported_projection, and N replays it once N-1 stops serving. This is rolling-upgrades item 17.
  • Operator steps: none.
  • Rollback: memory_expiry_mirror rows stay active, and the reverted build reports them as unsupported_projection.

Tests

  • New 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 runs retry_failures on 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 on main and passes here.
  • New test_the_sweeps_mirror_leaves_a_later_writes_vectors_alone: it runs the real reemit_embedding_rows_for_keys and retire_vectors_for_keys over 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 the entity_vectors retire 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.
  • New 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.
  • New 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 as malformed_payload and does not stay active.
  • New 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 a memory.set, are retired only below the recorded version. A deleted key and a key relabelled high are skipped, and the anonymize runs as recorded.
  • New test_a_mirror_failure_that_cannot_be_recorded_still_reports: with both the mirror and the record failing, the pass returns its counts and the mirror: failure.
  • test_sweep_writes_one_batched_insert_and_mirrors now checks the demote copy's pinned version and that only the anonymize retires bare.
  • Run locally with LOOM_ENV_FILE= .venv/bin/python -m pytest -q: make format lint typecheck pass, and so do tests/unit/test_memory_expiry.py, tests/unit/test_projection_failures.py, tests/unit/test_memory_webhooks.py, tests/unit/test_doc_links.py and tests/unit/test_public_copy.py (417 passed). A full tests/unit run (-n 6 -p randomly) on an earlier head of this branch gave 30,679 passed and 1 failed. The failure is test_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.
  • The integration suite was not run locally. CI runs it.

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_candidates and the version pinning in _copy_row / retire_vectors_for_keys, and the new test was shown to fail on main.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; 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.)
  • Generated files are regenerated in this PR, not by hand: none are affected.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (No migration.)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (None.)
  • The frontend builds and its tests pass, if frontend/ changed. (Unchanged.)
  • Docs under docs/ describe the system as it now is: docs/architecture/memory-architecture.md (expiry) and docs/architecture/failure-and-recovery.md (the repair worker).
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 25 commits November 8, 2026 09:33
…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]>
…'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]>
…the sweep alone

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]>
@zlareb1
zlareb1 merged commit 969620c into main Nov 8, 2026
37 of 38 checks passed
@zlareb1
zlareb1 deleted the fix/expiry-mirror-retry branch November 8, 2026 13:03
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The expiry sweep never retries a search mirror that failed

1 participant