[GH-ISSUE #1364] [Issue]: LTM recall: each turn re-scans the whole vault, and any edit re-embeds every memory #698

Closed
opened 2026-10-20 17:29:51 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Promansis on GitHub (Oct 13, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1364

Originally assigned to: @Promansis on GitHub.

Summary

Every recall re-reads, re-validates and re-hashes the whole vault, and any vault change re-embeds every memory. On 1.4.8 with 2,000 memories, recall took 1.7-1.9 s with an up-to-date index and 5.0 s after editing one tag, using an instant fake embedder. The code paths are unchanged at 1.4.30. #1328 deferred this work until it was measured at realistic vault sizes.

Affected package

long-term-memory 1.4.30 (upstream/staging a468f18e6), under packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/:

  • rebuild.ts: rebuildLongTermMemoryIndexes (line 119; embedding at 135) and loadOrRebuildLongTermMemoryIndexes (line 190; freshness check at 211-218)
  • source-processing.ts:386 and :567: rebuilds after extraction
  • index-state.ts:170 and mutation-transaction.ts:96, :111, :146: the index revision and dirty flag
  • Rebuild callers: routes.ts:476 (editor saves) and reconciliation.ts:932 (draft accept)

Expected behavior

  • When nothing in the vault has changed since the last recall, a turn does not re-read, re-validate or re-hash the whole vault.
  • An edit re-embeds only the memories whose embedded text changed.
  • Extraction that applies nothing to the vault does not rebuild the index.

Actual behavior

  1. Every recall scans the whole vault. Under the vault lock, recall reads and fully validates recall.json (vectors included), lists every memory, re-chunks them all and hashes the result to check freshness (rebuild.ts:204-218).
  2. Every rebuild re-embeds every chunk (rebuild.ts:135). No vectors are reused. A chunk's sourceHash also covers tags, scope and status, so it can't serve as an embedding cache key.
  3. Recall waits behind rebuilds. Rebuilds run under the vault lock on editor saves (routes.ts:476) and inside the draft and vault locks on draft accept (reconciliation.ts:932).
  4. Extraction rebuilds even when it changed nothing. It rebuilds whenever the draft has proposed mutations (source-processing.ts:386, :567), even when the commit applied none of them. Source memories are not indexed, so that rebuild changes nothing.
  5. The cheap signal is ignored. index-state.ts keeps a revision number and a dirty flag, which mutation-transaction.ts updates on every mutation, but recall never reads them.

Measured on 1.4.8 (real package modules, 2,000 synthetic memories, 384-dimension vectors, instant fake embedder):

Measurement Result
recall.json size 31.4 MB
Recall with an up-to-date index 1.7-1.9 s
Where that time goes re-chunk + hash ≈ 600 ms; vector validation ≈ 350 ms; JSON parse ≈ 180 ms
Recall right after one tag edit 5.0 s, re-embedding all 2,001 texts

A real embedder makes the rebuild case slower.

Steps to reproduce

  1. Create a vault with 2,000 memories (synthetic is fine) and let the index build.
  2. Time a recall. It takes about 1.7-1.9 s on the test machine.
  3. Edit one tag on one memory and recall again. Every chunk is re-embedded, taking about 5.0 s even with an instant fake embedder.

Environment

  • Marinara Engine version: 2.5.0 line (package range >=2.4.1 <4.0.0)
  • Package version: measured on long-term-memory 1.4.8; code paths re-checked on 1.4.30 (upstream/staging a468f18e6)
  • Install type: source (local measurement against the package's real modules)
  • OS + version: Linux
  • Browser or app shell: any

Logs, screenshots, or video

See the measurement table above. No private data is involved; the vault was synthetic.

Additional context

Minimal fix, in order:

  1. Measure on 1.4.30 at 200, 1,000 and 2,000 memories, with the fake embedder and with a real one. This is the measurement #1328 asked for, and it sets the targets.
  2. Reuse vectors. In rebuildLongTermMemoryIndexes, read the previous recall.json when it parses and has the same spaceId. Store a hash of each chunk's embedded text as a new optional textHash on each embedding entry, and embed only chunks whose text hash is new. In every other case (no old index, a malformed one, a different embedding space), embed everything, as now.
  3. Skip rebuilds after extraction that changed nothing. At source-processing.ts:386 and :567, rebuild only when the commit changed memories.
  4. Skip the per-turn scan when nothing changed. Keep the last parsed index in memory, keyed by vault root, file modification time and index-state revision. When the state is clean and the revision is unchanged, reuse it without re-reading memories. First confirm that every memory write goes through mutation-transaction.ts. Keep the full hash check on each process's first recall, so files edited outside the package are still caught after a restart.

Regressions: editing one tag re-embeds only that memory's chunks; changing the embedding space re-embeds everything; extraction that applies nothing does not rebuild; a mutation invalidates the cached index.

Notes:

  • textHash is a new field, and the index schema is strict. After a downgrade, an older package reads the index as malformed and rebuilds it once. The index is derived data, so nothing is lost.
  • Not covered here: an embedding failure during a needed rebuild still stops all recall, with no keyword fallback. That is a separate issue.
  • #1328 (1.4.29) already stopped quarantining a stale index (rebuild.ts:233-234).

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1364 Originally assigned to: @Promansis on GitHub. ## Summary Every recall re-reads, re-validates and re-hashes the whole vault, and any vault change re-embeds every memory. On 1.4.8 with 2,000 memories, recall took 1.7-1.9 s with an up-to-date index and 5.0 s after editing one tag, using an instant fake embedder. The code paths are unchanged at 1.4.30. #1328 deferred this work until it was measured at realistic vault sizes. ## Affected package `long-term-memory` 1.4.30 (`upstream/staging` a468f18e6), under `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/`: - `rebuild.ts`: `rebuildLongTermMemoryIndexes` (line 119; embedding at 135) and `loadOrRebuildLongTermMemoryIndexes` (line 190; freshness check at 211-218) - `source-processing.ts:386` and `:567`: rebuilds after extraction - `index-state.ts:170` and `mutation-transaction.ts:96`, `:111`, `:146`: the index revision and dirty flag - Rebuild callers: `routes.ts:476` (editor saves) and `reconciliation.ts:932` (draft accept) ## Expected behavior - When nothing in the vault has changed since the last recall, a turn does not re-read, re-validate or re-hash the whole vault. - An edit re-embeds only the memories whose embedded text changed. - Extraction that applies nothing to the vault does not rebuild the index. ## Actual behavior 1. **Every recall scans the whole vault.** Under the vault lock, recall reads and fully validates `recall.json` (vectors included), lists every memory, re-chunks them all and hashes the result to check freshness (`rebuild.ts:204-218`). 2. **Every rebuild re-embeds every chunk** (`rebuild.ts:135`). No vectors are reused. A chunk's `sourceHash` also covers tags, scope and status, so it can't serve as an embedding cache key. 3. **Recall waits behind rebuilds.** Rebuilds run under the vault lock on editor saves (`routes.ts:476`) and inside the draft and vault locks on draft accept (`reconciliation.ts:932`). 4. **Extraction rebuilds even when it changed nothing.** It rebuilds whenever the draft has proposed mutations (`source-processing.ts:386`, `:567`), even when the commit applied none of them. Source memories are not indexed, so that rebuild changes nothing. 5. **The cheap signal is ignored.** `index-state.ts` keeps a revision number and a dirty flag, which `mutation-transaction.ts` updates on every mutation, but recall never reads them. **Measured on 1.4.8** (real package modules, 2,000 synthetic memories, 384-dimension vectors, instant fake embedder): | Measurement | Result | |---|---| | `recall.json` size | 31.4 MB | | Recall with an up-to-date index | 1.7-1.9 s | | Where that time goes | re-chunk + hash ≈ 600 ms; vector validation ≈ 350 ms; JSON parse ≈ 180 ms | | Recall right after one tag edit | 5.0 s, re-embedding all 2,001 texts | A real embedder makes the rebuild case slower. ## Steps to reproduce 1. Create a vault with 2,000 memories (synthetic is fine) and let the index build. 2. Time a recall. It takes about 1.7-1.9 s on the test machine. 3. Edit one tag on one memory and recall again. Every chunk is re-embedded, taking about 5.0 s even with an instant fake embedder. ## Environment - Marinara Engine version: 2.5.0 line (package range `>=2.4.1 <4.0.0`) - Package version: measured on long-term-memory 1.4.8; code paths re-checked on 1.4.30 (`upstream/staging` a468f18e6) - Install type: source (local measurement against the package's real modules) - OS + version: Linux - Browser or app shell: any ## Logs, screenshots, or video See the measurement table above. No private data is involved; the vault was synthetic. ## Additional context **Minimal fix, in order:** 1. **Measure on 1.4.30** at 200, 1,000 and 2,000 memories, with the fake embedder and with a real one. This is the measurement #1328 asked for, and it sets the targets. 2. **Reuse vectors.** In `rebuildLongTermMemoryIndexes`, read the previous `recall.json` when it parses and has the same `spaceId`. Store a hash of each chunk's embedded text as a new optional `textHash` on each embedding entry, and embed only chunks whose text hash is new. In every other case (no old index, a malformed one, a different embedding space), embed everything, as now. 3. **Skip rebuilds after extraction that changed nothing.** At `source-processing.ts:386` and `:567`, rebuild only when the commit changed memories. 4. **Skip the per-turn scan when nothing changed.** Keep the last parsed index in memory, keyed by vault root, file modification time and index-state revision. When the state is clean and the revision is unchanged, reuse it without re-reading memories. First confirm that every memory write goes through `mutation-transaction.ts`. Keep the full hash check on each process's first recall, so files edited outside the package are still caught after a restart. **Regressions:** editing one tag re-embeds only that memory's chunks; changing the embedding space re-embeds everything; extraction that applies nothing does not rebuild; a mutation invalidates the cached index. **Notes:** - `textHash` is a new field, and the index schema is strict. After a downgrade, an older package reads the index as malformed and rebuilds it once. The index is derived data, so nothing is lost. - Not covered here: an embedding failure during a needed rebuild still stops all recall, with no keyword fallback. That is a separate issue. - #1328 (1.4.29) already stopped quarantining a stale index (`rebuild.ts:233-234`). ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:29:51 +07:00
  • closed this issue
  • added the
    bug
    label
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#698
No description provided.