mirror of
https://github.com/Pasta-Devs/Marinara-Agents.git
synced 2026-12-03 18:55:07 +07:00
[GH-ISSUE #1523] [Issue]: Long-Term Memory: every applied suggestion rebuilds the recall index under the vault lock #1503
Labels
No labels
agent: chess
agent: conversation-calls
agent: eightball
agent: haptic
agent: hierarchical-maps
agent: hierarchical-maps
agent: illustrator
agent: poker
agent: rock-paper-scissors
agent: tic-tac-toe
agent: uno
artifacts
bug
bugfix
build
catalog
ci
client
dependencies
docs
documentation
enhancement
feature
fixed-in-staging
in-pr
normal-scope
packages
pull-request
qol
regression
roadmap
scope-risk
server
soup
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Agents#1503
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Promansis on GitHub (Oct 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1523
Originally assigned to: @Promansis on GitHub.
Summary
Long-Term Memory rebuilds the recall index while holding the vault lock, embedding included, after every applied Review suggestion and every extraction that applies memories. "Apply all" over N suggestions sends N requests and runs N rebuilds. Every other save, recall and debug line waits behind each one. After the embedding source changes, the first recall re-embeds the whole vault inline before it answers, also under the lock.
The fix (RT-1) adds one background rebuild job per vault for draft apply and source extraction. It runs 1 s after the last change, at most 10 s after the first, and holds the lock only to read the notes and to publish. Recall flushes it first, so recall still sees every change. After an embedding-source change, recall answers from the keyword lanes at once and re-embeds in the background.
RT-2 (the debug log on its own lock, plus a trim at 2 MB) was planned to ship in the same change. It is on hold because the planned design misses two places that rely on debug appends holding the vault lock.
Affected package
long-term-memory1.4.55 (upstream/staging8b72729f1). Line references below are on8b72729f1. Still present on 1.4.66 (77acb942b): draft apply rebuilds atS/drafts/reconciliation.ts:993, source extraction atS/extraction/source-processing.ts:150, called at:448and:636.S=packages/long-term-memory/src/engine/packages/server/src/services/long-term-memoryC=packages/long-term-memory/src/engine/packages/client/src/features/long-term-memoryExpected behavior
Actual behavior
rebuildLongTermMemoryIndexes(S/recall/rebuild.ts:165-235) holds it for the whole build: settings,listNotes, chunking, reusable vectors, embedding every new chunk text (a network call for a remote provider), the BM25, graph, keyword and metadata indexes, and therecall.jsonwrite.S/drafts/reconciliation.ts:932). The Review Queue's "apply all" sends one request per draft.S/extraction/source-processing.ts:147-152, called at:387and once per batch at:572).S/recall/rebuild.ts:105-128, fromloadOrRebuildLongTermMemoryIndexesat:317).recordLtmDebugEventtakes the vault lock for each append (S/maintenance/debug-log.ts:195), so a turn's debug line waits for any rebuild in progress. Once the log reaches 2 MB, every append rewrites the whole file (appendBounded,:143-165).Measurement
A private timing harness ran on a copy of the 197-note vault with local MiniLM, as the Engine uses it. It applied 10 drafts on 10 sources in a row, as "apply all" does. Aggregates only:
8b72729f1The design's earlier measurement found 8.8 s for a full re-embed on the same vault. Linear scaling to 2,000 chunks would put it near 90 s; that was not measured.
Steps to reproduce
Minimal fix
One PR, patch bump. The finished patch touches 14 files, +966/−177. It follows the design, with scope A and a 1 s quiet period capped at 10 s.
New
S/recall/rebuild-job.ts. One job per resolved root:scheduleLtmIndexRebuildstarts or extends the quiet period;flushLtmIndexRebuildruns a pending job now and waits for it, including a follow-up;cancelPendingLtmIndexRebuilddrops a pending job when a synchronous rebuild covers it;stopLtmIndexRebuildhandles deactivation.A failed run is logged once and does not wedge the next.
S/recall/rebuild.ts: the background rebuild runs in three phases.rebuildStartedAtand the stop words are unchanged. Otherwise a write landed, and the job builds again, reusing the vectors it embedded.After three dropped tries it runs the existing locked rebuild.
Reads flush first.
loadOrRebuildLongTermMemoryIndexes,retrieveLongTermMemory,refreshLongTermMemoryIndexesandcheckLongTermMemoryIntegrityflush the job before they take the vault lock.Embedding-source upgrade. It becomes an upgrade-only background job that readers do not wait for. The Engine refresh still re-embeds inline.
S/persistence/vault-lock.tsgets two helpers:holdsLtmVaultLock: flush and stop throw under the vault lock instead of deadlocking;outsideLtmVaultLock: the job's timer and run start outside the caller's lock context, so a job scheduled inside a mutation waits for it.Call sites:
S/drafts/reconciliation.ts:932schedules and returnsindexRebuild: { status: "pending" };C/review/ReviewQueue.tsx:63widens its type;S/extraction/source-processing.ts:387and:572schedule;S/server-entry.tsstops the job at deactivation.Docs:
packages/long-term-memory/docs/ARCHITECTURE.md.Lock order: vault lock → index-state, draft and debug locks. Waits on the job happen only with no lock held.
What users see change
incompatible, about 3 s at 197 notes).Not included
scheduledrebuild status in the section rebuild schema and the client.CHANGELOG under
## Unreleased: one bullet.RT-2: on hold
The planned design (option A) takes the debug log off the vault lock onto its own per-file queue. It wraps only retention's prune (
S/persistence/retention.ts:203) and the debug-folderrmin restore and Delete all (S/persistence/backup-restore.ts:123) in that queue. At8b72729f1two more places depend on debug appends holding the vault lock:rename(root, previous)and thenrename(staging, root)(backup-restore.ts:149-151, and again in its journal recovery). A debug append runsmkdir(dirname(path), { recursive: true })before taking any lock (S/maintenance/debug-log.ts:193,:270in clear). Off the vault lock, an append between the two renames recreatesroot/debug, so the second rename fails on a non-empty root and the restore fails mid-swap. Or the line goes into the old root and is deleted with it.withLongTermMemoryVaultMutation(S/runtime.ts:48, given to the Engine aswithVaultMutationatS/server-entry.ts:57) runs Engine code under the vault lock. Taking the non-re-entrant debug queue there as well would deadlock as soon as that code recorded a debug event.The planned design assumed a missed path costs at most a lost debug line; the swap can fail a restore. Options:
mkdirinside a re-entrant queue.With RT-1, draft apply and extraction no longer hold the lock while embedding, so the remaining waits are short except for the editor's synchronous rebuilds. The 2 MB trim does not depend on this choice.
Regression proof
New suite
tests/long-term-memory-rebuild-job.regression.ts, wired into.github/workflows/ltm-browser.ymlasrebuild-job. It uses fake timers and no real sleeps.8b72729f1it fails at its first check:ten draft applies in one quiet period must rebuild the index once,10 !== 1.RT-2's tests wait for the RT-2 decision.
Environment
stagingcheckout, unchanged)upstream/staging8b72729f1)Logs, screenshots, or video
None. The burst timing, the suite logs on
8b72729f1and with the fix, and the mutation results are kept privately; the timing is summarized under Measurement.Additional context
These are tracker items RT-1 (rebuild under the lock, runtime audit) and RT-2 (debug log on the vault lock). The design uses scope A, 1 s capped at 10 s, the debug-log trim, and RT-2 option A. The RT-2 part needs a new decision; see "RT-2: on hold".
Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked: