[PR #1519] [MERGED] perf(long-term-memory): list the vault once per source batch (1.4.61) #1526

Closed
opened 2026-10-22 04:03:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Agents/pull/1519
Author: @Promansis
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @Promansis

Base: staging ← Head: fix(long-term-memory)/reconciliation-large-vaults


📝 Commits (3)

  • 10a45f2 perf(long-term-memory): list the vault once per source batch (#1504)
  • 8ba56ac Merge upstream/staging into fix(long-term-memory)/reconciliation-large-vaults; release long-term-memory 1.4.60
  • 722e7f5 Merge upstream/staging into fix(long-term-memory)/reconciliation-large-vaults; release long-term-memory 1.4.61

📊 Changes

18 files changed (+628 additions, -120 deletions)

View changed files

➕ artifacts/long-term-memory-1.4.61.zip (+0 -0)
📝 catalog/catalog.json (+6 -6)
📝 catalog/notes.json (+6 -6)
📝 catalog/v2/catalog.json (+6 -6)
📝 catalog/v2/notes.json (+6 -6)
📝 catalog/v3/catalog.json (+6 -6)
📝 catalog/v3/notes.json (+6 -6)
📝 packages/long-term-memory/CHANGELOG.md (+4 -0)
📝 packages/long-term-memory/manifest.json (+3 -3)
📝 packages/long-term-memory/server.mjs (+41 -41)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/extraction/candidate-reconciliation.ts (+55 -30)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/extraction/source-processing.ts (+69 -5)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/persistence/mutation-transaction.ts (+6 -1)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/persistence/vault-snapshot.ts (+21 -2)
📝 scripts/build-feature-packages.mjs (+1 -1)
📝 tests/long-term-memory-candidate-reconciliation.regression.ts (+180 -0)
📝 tests/long-term-memory-extraction-reliability.regression.ts (+158 -0)
📝 tests/long-term-memory-storage.regression.ts (+54 -1)

📄 Description

Pull request

Linked issue

Closes #1504

Why this change

  • A batch of imported or re-extracted sources holds the vault lock while it commits each source, and recall for a chat reply waits for that lock. Each commit re-read and parsed every note in the vault, because the previous source's fingerprint write dropped the parsed snapshot, and reconciliation then re-filtered and re-tokenized every note for every candidate group. On a synthetic 8,000-note vault a 10-source batch held the lock for about 24 s; with this change, 1.3 s. Matching results are unchanged (details and the measurement table in the issue).

What changed

  • long-term-memory: the batch lists the committed vault once and re-reads only the notes that later vault writes report (extraction/source-processing.ts). A write that names no files, or a failed re-read, lists the vault again.
  • The vault reports every write: commitLtmMutation passes the root-relative paths it writes to invalidateLtmVaultSnapshot, which tells watchers registered with the new watchLtmVaultWrites (persistence/mutation-transaction.ts, persistence/vault-snapshot.ts).
  • Commit-time reconciliation filters the complete candidate set once per call and computes each note's title key and section tokens once per call (extraction/candidate-reconciliation.ts). Same rules, 0.85 threshold and candidate order.
  • Regressions: a candidateNotes rule table and a per-call read bound (candidate-reconciliation); the batch listing count, notes created and deleted by another writer mid-batch, and relists after writes that name no files (extraction-reliability); the write reports and watcher stop (storage).
  • Release: long-term-memory 1.4.61, with the CHANGELOG entry, version, manifest, rebuilt server.mjs, artifact and catalog entries, on top of staging 653a20b11. Open #1516 also releases 1.4.61; whichever merges second is re-released on top of the other.

Package and security impact

  • Affected package IDs: long-term-memory
  • Engine compatibility impact: none
  • New or changed permissions/entrypoints: none
  • Restart, storage, update, or uninstall impact: no stored format changes. The vault write path changes only in when the in-memory snapshot is dropped (after the transaction's paths are validated) and in reporting those paths to in-process watchers; see the validation notes below.

Validation

  • node scripts/validate-catalog.mjs passes locally
  • git diff --check passes locally
  • Rebuilt every affected manifest, payload, artifact, and catalog entry
  • Installed or updated the affected package through Marinara Engine
  • Checked supported modes, restart behavior, and uninstall cleanup
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • New regressions fail on the base and pass with the change: reconciliation reads each candidate's scope 12 times for 3 groups × 4 notes (now 4); a 3-source batch lists the vault 2 more times after its first draft (now 0, and 1 after a write that names no files, also when it lands during a re-read); watchLtmVaultWrites did not exist.
  • Each branch of the batch re-read was broken in turn (added note, deleted note, the check after the re-read's await, the stop() guard), and each break fails its case.
  • The #1087 batch cases (sibling reuse, concurrent create, terminal overlay, lock scope) pass unchanged; the terminal-overlay case is the freshness guard for a write the batch did not make.
  • Security-sensitive validation (vault write path): no new write or path; the reported paths are the ones create() already validated; they only select which notes to re-read, through getNotesByIds (note-id schema, safeJoin); writers that bypass commitLtmMutation report no paths, which forces a full listing; legacy source quarantine runs only right after such a report and moves only source notes, which are never candidates; backup restore holds the vault lock, so it cannot run inside a batch; rollback is unchanged.
Full validation notes (vault write path)

What can be written. Nothing new. No new write, file, folder or path built from input. commitLtmMutation writes the same files in the same order; the only change there is that the snapshot invalidation runs after create() instead of before it, and carries prepared.files[].path. Those paths already passed assertInsideDirectory and ltmSafeRelativePathSchema inside create(). create() is synchronous, so no read can run between it and the invalidation. If create() throws, nothing is written and nothing is reported; before, the snapshot was dropped anyway, which was harmless.

What the paths are used for. Only to choose which notes to re-read. batchCommittedNotes keeps paths under vault/, takes the file name without .json as a note id, and re-reads through storage.getNotesByIds, which parses every id with ltmNoteIdSchema and reads only vault/<folder>/<id>.json through safeJoin. A name that is not a valid note id throws there, and the batch falls back to a full listNotes().

Freshness. The batch list stays equal to a fresh listNotes() (same notes, same id order) as long as every vault write during the batch calls invalidateLtmVaultSnapshot, which the parsed snapshot already relies on today:

  • All note writes in LongTermMemoryStorage (create, project, update, bulk, redirect, rename, section rename, delete) go through commitLtmMutation, as do scope links and rejected suggestions. They report their paths.
  • Writers that bypass it call invalidateLtmVaultSnapshot without paths, which forces a full listing: identity repair, note-fork repair, maintenance quarantine, journal recovery and storage.cleanup(). A no-paths write that lands while an item re-reads changed notes is caught by the check after the await.
  • Two writers change vault files without invalidating at the time of the write. Both are safe for other reasons:
    • quarantineLegacyCapturedTurnSources (maintenance/legacy-source-quarantine.ts:15) moves captured-turn source files out of vault/sources/. It runs only inside initializeLtmStore for a root that is not yet initialized: at first startup, or after cleanup(), which has already reported undefined. Either way the batch's next list() is a full listNotes(), and that call's own initializeLtmStore runs the quarantine before it reads. Source notes are also never reconciliation candidates.
    • Backup restore (persistence/backup-restore.ts:149-151) swaps the whole root after its cleanup() has reported. It holds the vault lock for the whole restore, so it cannot run inside a batch. A batch waiting for the lock ignores writes until its first listing, which reads the restored root.
  • Writers in other async contexts wait for the batch's vault lock (the #1087 lock-scope case pins this).
  • Over-reporting is safe (an extra re-read).

Rollback. Unchanged. A failed apply restores the before state as today. Watchers heard the paths before the write, so the next item re-reads those notes and sees the restored content. A committed mutation whose publish step fails is already on disk and was reported.

Watcher lifetime. Registered before the batch waits for the lock; writes before the first listing are ignored. Stopped in .finally on the lock call, so a failed or aborted batch does not leak it. The per-root set is removed when empty, and only while it is still the root's registered set. Watchers run synchronously inside the invalidation; the batch's callback only adds to a Set.

Documentation impact

  • No documentation changes needed
  • Updated this README catalog and package guidance
  • Updated linked Marinara Engine documentation

UI evidence (if applicable)

  • None (server only).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Agents/pull/1519 **Author:** [@Promansis](https://github.com/Promansis) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@Promansis](https://github.com/Promansis) **Base:** `staging` ← **Head:** `fix(long-term-memory)/reconciliation-large-vaults` --- ### 📝 Commits (3) - [`10a45f2`](https://github.com/Pasta-Devs/Marinara-Agents/commit/10a45f2e78b0172e3333fb9d73dec2d09e1577ee) perf(long-term-memory): list the vault once per source batch (#1504) - [`8ba56ac`](https://github.com/Pasta-Devs/Marinara-Agents/commit/8ba56ac26382a37f6a195974511401da75a561c4) Merge upstream/staging into fix(long-term-memory)/reconciliation-large-vaults; release long-term-memory 1.4.60 - [`722e7f5`](https://github.com/Pasta-Devs/Marinara-Agents/commit/722e7f5854a9534f82df6b47d9a83596b664f68d) Merge upstream/staging into fix(long-term-memory)/reconciliation-large-vaults; release long-term-memory 1.4.61 ### 📊 Changes **18 files changed** (+628 additions, -120 deletions) <details> <summary>View changed files</summary> ➕ `artifacts/long-term-memory-1.4.61.zip` (+0 -0) 📝 `catalog/catalog.json` (+6 -6) 📝 `catalog/notes.json` (+6 -6) 📝 `catalog/v2/catalog.json` (+6 -6) 📝 `catalog/v2/notes.json` (+6 -6) 📝 `catalog/v3/catalog.json` (+6 -6) 📝 `catalog/v3/notes.json` (+6 -6) 📝 `packages/long-term-memory/CHANGELOG.md` (+4 -0) 📝 `packages/long-term-memory/manifest.json` (+3 -3) 📝 `packages/long-term-memory/server.mjs` (+41 -41) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/extraction/candidate-reconciliation.ts` (+55 -30) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/extraction/source-processing.ts` (+69 -5) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/persistence/mutation-transaction.ts` (+6 -1) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/persistence/vault-snapshot.ts` (+21 -2) 📝 `scripts/build-feature-packages.mjs` (+1 -1) 📝 `tests/long-term-memory-candidate-reconciliation.regression.ts` (+180 -0) 📝 `tests/long-term-memory-extraction-reliability.regression.ts` (+158 -0) 📝 `tests/long-term-memory-storage.regression.ts` (+54 -1) </details> ### 📄 Description # Pull request ## Linked issue Closes #1504 ## Why this change - A batch of imported or re-extracted sources holds the vault lock while it commits each source, and recall for a chat reply waits for that lock. Each commit re-read and parsed every note in the vault, because the previous source's fingerprint write dropped the parsed snapshot, and reconciliation then re-filtered and re-tokenized every note for every candidate group. On a synthetic 8,000-note vault a 10-source batch held the lock for about 24 s; with this change, 1.3 s. Matching results are unchanged (details and the measurement table in the issue). ## What changed - `long-term-memory`: the batch lists the committed vault once and re-reads only the notes that later vault writes report (`extraction/source-processing.ts`). A write that names no files, or a failed re-read, lists the vault again. - The vault reports every write: `commitLtmMutation` passes the root-relative paths it writes to `invalidateLtmVaultSnapshot`, which tells watchers registered with the new `watchLtmVaultWrites` (`persistence/mutation-transaction.ts`, `persistence/vault-snapshot.ts`). - Commit-time reconciliation filters the complete candidate set once per call and computes each note's title key and section tokens once per call (`extraction/candidate-reconciliation.ts`). Same rules, 0.85 threshold and candidate order. - Regressions: a `candidateNotes` rule table and a per-call read bound (candidate-reconciliation); the batch listing count, notes created and deleted by another writer mid-batch, and relists after writes that name no files (extraction-reliability); the write reports and watcher stop (storage). - **Release:** `long-term-memory` 1.4.61, with the CHANGELOG entry, version, manifest, rebuilt `server.mjs`, artifact and catalog entries, on top of staging 653a20b11. Open #1516 also releases 1.4.61; whichever merges second is re-released on top of the other. ## Package and security impact - Affected package IDs: `long-term-memory` - Engine compatibility impact: none - New or changed permissions/entrypoints: none - Restart, storage, update, or uninstall impact: no stored format changes. The vault write path changes only in when the in-memory snapshot is dropped (after the transaction's paths are validated) and in reporting those paths to in-process watchers; see the validation notes below. ## Validation - [ ] `node scripts/validate-catalog.mjs` passes locally - [ ] `git diff --check` passes locally - [ ] Rebuilt every affected manifest, payload, artifact, and catalog entry - [ ] Installed or updated the affected package through Marinara Engine - [ ] Checked supported modes, restart behavior, and uninstall cleanup - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - New regressions fail on the base and pass with the change: reconciliation reads each candidate's scope 12 times for 3 groups × 4 notes (now 4); a 3-source batch lists the vault 2 more times after its first draft (now 0, and 1 after a write that names no files, also when it lands during a re-read); `watchLtmVaultWrites` did not exist. - Each branch of the batch re-read was broken in turn (added note, deleted note, the check after the re-read's `await`, the `stop()` guard), and each break fails its case. - The #1087 batch cases (sibling reuse, concurrent create, terminal overlay, lock scope) pass unchanged; the terminal-overlay case is the freshness guard for a write the batch did not make. - Security-sensitive validation (vault write path): no new write or path; the reported paths are the ones `create()` already validated; they only select which notes to re-read, through `getNotesByIds` (note-id schema, `safeJoin`); writers that bypass `commitLtmMutation` report no paths, which forces a full listing; legacy source quarantine runs only right after such a report and moves only source notes, which are never candidates; backup restore holds the vault lock, so it cannot run inside a batch; rollback is unchanged. <details><summary>Full validation notes (vault write path)</summary> **What can be written.** Nothing new. No new write, file, folder or path built from input. `commitLtmMutation` writes the same files in the same order; the only change there is that the snapshot invalidation runs after `create()` instead of before it, and carries `prepared.files[].path`. Those paths already passed `assertInsideDirectory` and `ltmSafeRelativePathSchema` inside `create()`. `create()` is synchronous, so no read can run between it and the invalidation. If `create()` throws, nothing is written and nothing is reported; before, the snapshot was dropped anyway, which was harmless. **What the paths are used for.** Only to choose which notes to re-read. `batchCommittedNotes` keeps paths under `vault/`, takes the file name without `.json` as a note id, and re-reads through `storage.getNotesByIds`, which parses every id with `ltmNoteIdSchema` and reads only `vault/<folder>/<id>.json` through `safeJoin`. A name that is not a valid note id throws there, and the batch falls back to a full `listNotes()`. **Freshness.** The batch list stays equal to a fresh `listNotes()` (same notes, same id order) as long as every vault write during the batch calls `invalidateLtmVaultSnapshot`, which the parsed snapshot already relies on today: - All note writes in `LongTermMemoryStorage` (create, project, update, bulk, redirect, rename, section rename, delete) go through `commitLtmMutation`, as do scope links and rejected suggestions. They report their paths. - Writers that bypass it call `invalidateLtmVaultSnapshot` without paths, which forces a full listing: identity repair, note-fork repair, maintenance quarantine, journal recovery and `storage.cleanup()`. A no-paths write that lands while an item re-reads changed notes is caught by the check after the `await`. - Two writers change vault files without invalidating at the time of the write. Both are safe for other reasons: - `quarantineLegacyCapturedTurnSources` (`maintenance/legacy-source-quarantine.ts:15`) moves captured-turn source files out of `vault/sources/`. It runs only inside `initializeLtmStore` for a root that is not yet initialized: at first startup, or after `cleanup()`, which has already reported `undefined`. Either way the batch's next `list()` is a full `listNotes()`, and that call's own `initializeLtmStore` runs the quarantine before it reads. Source notes are also never reconciliation candidates. - Backup restore (`persistence/backup-restore.ts:149-151`) swaps the whole root after its `cleanup()` has reported. It holds the vault lock for the whole restore, so it cannot run inside a batch. A batch waiting for the lock ignores writes until its first listing, which reads the restored root. - Writers in other async contexts wait for the batch's vault lock (the #1087 lock-scope case pins this). - Over-reporting is safe (an extra re-read). **Rollback.** Unchanged. A failed `apply` restores the `before` state as today. Watchers heard the paths before the write, so the next item re-reads those notes and sees the restored content. A committed mutation whose publish step fails is already on disk and was reported. **Watcher lifetime.** Registered before the batch waits for the lock; writes before the first listing are ignored. Stopped in `.finally` on the lock call, so a failed or aborted batch does not leak it. The per-root set is removed when empty, and only while it is still the root's registered set. Watchers run synchronously inside the invalidation; the batch's callback only adds to a `Set`. </details> ## Documentation impact - [ ] No documentation changes needed - [ ] Updated this README catalog and package guidance - [ ] Updated linked Marinara Engine documentation ## UI evidence (if applicable) - None (server only). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:03:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Agents#1526
No description provided.