mirror of
https://github.com/Pasta-Devs/Marinara-Agents.git
synced 2026-12-03 18:55:07 +07:00
[GH-ISSUE #1504] [Issue]: Long-Term Memory: a multi-source extraction re-reads the whole vault for every source while recall waits on the lock #1495
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#1495
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 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1504
Originally assigned to: @Promansis on GitHub.
Summary
When several sources are extracted in one batch, each source's commit lists and parses every note in the vault again and matches each candidate group against all of them. This happens while the batch holds the vault lock, and recall for a chat reply needs that same lock (
S/recall/retrieval.ts:84).On a synthetic vault of 8,000 notes, a 10-source batch holds the lock for about 24 s today, or 2,400 ms per source. Most of that time is spent re-reading the vault. A source's own fingerprint write discards the parsed vault snapshot, so the next source parses every file again.
The fix lists the vault once per batch and re-reads only the notes that later writes touch. It also filters and tokenizes the candidate set once per reconciliation instead of once per group and unit. With the fix the same batch holds the lock for 1.3 s (18.5× less), and every outcome stays identical.
Affected package
Re-checked 2026-10-21 at
upstream/stagingedb9c4928(long-term-memory 1.4.57, built on the Engine's host, capability API 1.341): the fix applies unchanged there; line numbers are at the commit named below.long-term-memory1.4.54 (upstream/stagingb1873710f). PR #1473 (headc4b1c0192) shifts lines insource-processing.tsand changes one line incandidate-reconciliation.tswithout moving any line. Where the two differ, references readstaging / #1473. #1473 merged on 2026-10-19 (staging8b72729f1, LTM 1.4.55), so the second number is the current line.S=packages/long-term-memory/src/engine/packages/server/src/services/long-term-memoryExpected behavior
listNotes()for every source, including writes that land between two commits.Actual behavior
processLongTermMemorySourceBatchholds one vault lock across the ordered commit loop (S/extraction/source-processing.ts:528 / :531). Each item's commit callsrecompilePreparedWithFreshCandidates, which runsstorage.listNotes()(:289 / :290).S/persistence/mutation-transaction.ts:91), and each item writes its source fingerprint (source-processing.ts:356-363 / :357-364). From the second item on,listNotes()therefore reads and parses every vault file, then clones and sorts them all. The audit's probe D did not include this cost.reconcileEvidenceUnitCandidates(S/extraction/candidate-reconciliation.ts:70) does the following on the complete-set path (:123-129), once per candidate group:canUpdateLtmScopedTarget), mode and status;identityMatchesGroup,:273);contentMatchesGroup,:287).Measurement
The fixture is private: synthetic vaults built from the real vault's non-source notes, with about 35% of the words changed and 60% of the notes in the batch's scope. It also includes a copy of the real vault (197 notes). Only aggregate numbers are shared here.
Each batch has 10 sources of 20 units each and mixes identity, content, ambiguous and sibling matches with creates. A fake provider returns fixed units, and each source is committed one after another (concurrency 1). Each figure is the median of 3 runs on a RAM disk.
"Lock" is the time the batch holds the vault lock, per source. "Today" and "fixed" are the same bench source with timing hooks; only "fixed" has the change. Listing and reconcile are in milliseconds per source. Other jobs were using the host (load 6–15 on 12 cores), so absolute times are 25–45% higher than in a quieter run of an earlier version of the patch. The ratios held.
Steps to reproduce
Minimal fix
One PR, patch bump. The finished patch is private (source plus regressions, on
8b72729f1). The first reference patch (171 changed lines) is superseded.S/persistence/vault-snapshot.ts:43-46(same at #1473):invalidateLtmVaultSnapshot(root, paths?)also calls write watchers with the paths a mutation wrote. Callers that give no paths passundefined: repair, recovery andcleanup().watchLtmVaultWrites(root, onWrite)registers a watcher and returns its stop function.S/persistence/mutation-transaction.ts:90-92(same):commitLtmMutationinvalidates aftercreate()and passesprepared.files.map((file) => file.path).create()has already validated these as root-relative paths. It is synchronous, so no read can run between it and the invalidation.S/extraction/source-processing.ts:batchCommittedNotes(storage)helper abovecommitPreparedLongTermMemorySource(:306 / :307):getNotesByIds). It deletes notes that are gone and re-sorts by id when a note was added, matchinglistNotes()order.listNotes().recompilePreparedWithFreshCandidates(:277 / :278) takescommittedNotes?and lists through it at:289 / :290.commitPreparedLongTermMemorySourcetakescommittedNotes?in its options (:308 / :309) and passes it at:316 / :317.processLongTermMemorySourceBatchcreates the helper before the lock (:528 / :531) and passes it to each commit (:538-542 / :541-545). It stops the helper with.finally(committedNotes.stop)on the lock call (:567 / :571).processLongTermMemorySource(:380-385 / :381-386) is unchanged; it already lists once.S/extraction/candidate-reconciliation.ts(same lines at both)::102): whencandidateNotesis given, filter it once withisReconcilableNoteand bucket the result by type.isReconcilableNoteis today'sisCompatibleCandidateNotewithout the type check: source-like and scene notes, archived/resolved, scope, mode.:123-129): take the bucket for the type, then drop the derived id and ids the unit cannot remap to, as today.:168-171):Setand compare them with each note's normalized title, cached once per call.contentMatchescomputes each unit's normalized text and tokens once per group, and each note's section text and tokens at most once per call.identityMatchesGroup(:273) andcontentMatchesGroup(:287) go away.isCompatibleCandidateNote(:248) becomesnote.type === noteType && isReconcilableNote(...), so the bounded preparation path (:163) is unchanged.## Unreleased: one bullet, below.Why a write watcher, not "re-read what apply wrote": a first version re-read only the notes that the batch's own auto-apply wrote. It passed the probe below but failed
tests/long-term-memory-extraction-reliability.regression.ts:1046(#1087 terminal-overlay case). That test archives a target between two commits through a write the batch did not make. The list must stay equal to a fresh listing whoever writes, so it follows the vault's own write reports.Validation of this patch (on a patched copy of the bench tree, with a private harness, pass rule fixed before the first run):
listNotes()calls after the first finalize, 3-source batch, review / auto-applyTypeError)candidate-reconciliationandreconciliation-recall)browserandinstallationneed the rebuilt archive and were not runscripts/typecheck-packages.mjs long-term-memorywith an Engine) on #1473 + patchThe PL-1 run covers single-source commits, so it checks only the reconciliation half. The batch half is covered by the probe and the extraction-reliability suite.
Not included
source-extraction.ts,evidence-unit-extraction.ts,structured-summary-normalizer.tsandevidence-unit-validation.ts, which this fix does not touch. They are not hot either: at 8,000 notes, the second compile without reconciliation takes about 12 ms per source.CHANGELOG
Regression proof
Written first and proven on
8b72729f1:tests/long-term-memory-candidate-reconciliation.regression.ts: a table for thecandidateNotespath. No test calls it directly today. It passes on staging and after the fix. A second case, which fails on staging, bounds reads per call: 3 groups × 4 candidate notes read each note'sscope12 times today and 4 times with the fix. The table pins:subjectIdand bysubjectNames;tests/long-term-memory-extraction-reliability.regression.ts: after the #1087 terminal-overlay case, a three-source model batch in review mode countsLongTermMemoryStorage.prototype.listNotescalls after the firstLongTermMemoryDraftStore.prototype.createDraft: 2 today, 0 with the fix. The first batch's draft hook also creates a matching note under another id and deletes another, and the later items must reuse the new note and not target the deleted one. A second three-source batch drops the snapshot without paths after the first draft: 2 today, 1 with the fix (one full relist). A two-source batch drops it while the second item re-reads: 1 relist. The listings returned to the batch are deep-frozen. The #1087 cases (:568-1220), and the terminal-overlay case at:935-1060in particular, remain the freshness guards.tests/long-term-memory-storage.regression.ts: next to the snapshot count (:3210-3222), check thatwatchLtmVaultWritesreports the note's root-relative path for create, update and delete. It reportsundefinedforcleanup(), nothing after stop, and a repeated stop leaves a later watcher on the same root registered.tests/long-term-memory-reconciliation-recall.regression.ts. ThelistNotescount in 2 is the bound, and a 2,000-note fixture adds CI time without a new assertion.All three suites run in
ltm-browser.yml("Run Long-Term Memory regressions").Environment
staging2039583ca, local checkout used read-only for tests)upstream/stagingb1873710f)Logs, screenshots, or video
None. Timings come from a private harness on a 12-core host that other jobs were also using.
Additional context
Tracker item EX-I4 (extraction stages audit F3, high at scale). F10 was to be folded in only where this fix touches the same code, and it does not (see Not included). The audit's probe D measured reconcile alone (41 / 354 / 1,405 ms at 200 / 2,000 / 8,000 notes, with 75% compatible notes). The table above uses about 10% compatible notes and adds the per-source vault read.
Scope: the fix may change
persistence/vault-snapshot.tsandpersistence/mutation-transaction.tsso the vault reports every write it makes. The alternative was weakeningtests/long-term-memory-extraction-reliability.regression.ts:1046. Because this changes executable server code on the vault write path, the PR carries explicit validation notes for it.The patch rebased onto
upstream/staging8b72729f1(after #1473) passes typecheck with the strict gate,npm run check, the package rebuild and all 32 LTM suites there.Related: #1473, #1087.
Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked: