[GH-ISSUE #1264] [Issue]: LTM recall: single everyday keywords decide recall while BM25 is inert on real chats #643

Closed
opened 2026-10-20 17:29:18 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @Promansis on GitHub.

Summary

Follow-up to #1258. On a real vault, 1.4.15 recall is decided almost entirely by the keyword lane. A single everyday word that happens to be a keyword on one memory clears the default threshold alone, while the BM25 lane contributes nothing on real chat windows. The fuzzy keyword catalog also only scans the alphabetically first 512 keywords, which real vaults exceed. This issue fixes those three lanes and rescales the presets so the existing 0.35 default threshold keeps working for users who never changed it.

Affected package

long-term-memory 1.4.15:

  • keyword-index.ts, bm25.ts under packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/
  • constants.ts, runtime-settings.ts under packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory/

Expected behavior

  • A memory is not injected because one everyday word in the recent messages ("half", "silence", "call", "way") is one of its keywords. A single match on a word the chat uses as a name still counts.
  • BM25 contributes on real multi-message, long-message chat windows, and its score does not saturate or vanish as messages get longer.
  • Fuzzy keyword matching can reach every in-scope keyword, not only those early in the alphabet.
  • Users on the default threshold (0.35) get the improved behavior without changing any setting.

Actual behavior

  1. One exact keyword clears the threshold alone (keyword-index.ts:112, :150). An exact term match scores 3/4, and keywordFrequencyWeight gives full weight to a keyword found on one in-scope memory, so the lane scores 0.75 and balanced relevance is 0.75 × 0.65 = 0.49 > 0.35. The frequency weight measures rarity in the keyword catalog, not in language, so everyday words that are keywords on one memory get full weight.
    • In 7 real recalls on default settings, keyword decided 60 of 68 picks; the matched words included "silence", "holds", "half", "mother", "note", "line", "way", "called" and "played", on memories with meaning-based similarity of 0.07–0.17.
  2. BM25 is inert on real chats (bm25.ts:69, :89). The score is divided by the summed idf of every query term. A 4-message roleplay window has many terms (and no stop-word removal), so real BM25 scores never exceeded 0.07. Its 0.85 weight affects neither the threshold nor the fused order (RRF multiplies the rank term by the normalized score).
  3. Fuzzy catalog cap is alphabetical (keyword-index.ts:82, :154-160). The fuzzy pass scans keywords in alphabetical order and stops at 512 in-scope entries. Two real memory sets have 900 and 1,769 in-scope keywords, so fuzzy matches on keywords later in the alphabet never happen.

Steps to reproduce

  1. Use a vault with a few dozen memories whose keywords include common words.
  2. Enable debug logging and send a turn whose last 4 messages share no topic with the vault but contain one of those words (for example "There's half a sandwich left").
  3. In the recall_explanation event, see memories selected by keyword with keyword:normalized:0.750 from a single keyword:exact: match, and bm25:normalized values below 0.1 on every candidate.

Environment

  • Marinara Engine version: 2.5.0 line (staging)
  • Package version: long-term-memory 1.4.15 (upstream/staging 7fc3b91c2)
  • Install type: source (real vault copy on a Windows install, evaluated locally with the package's real retrieval code)
  • OS + version: not platform-specific
  • Browser or app shell: not UI-specific

Logs, screenshots, or video

Evaluation: real vault (206 chunks), 22 hand-written 4-message windows over two memory sets (18 labelled with essential/related memories, 4 off-topic), run through the real retrieveLongTermMemory with the vault's own MiniLM embeddings. "Long" pads each message to about 140 words, since real roleplay messages are long. Prototype fixes run through a replica that selects the same memories as the real code in 270/276 runs.

Setting Short windows: precision / essential recall / off-topic memories Long windows
1.4.15 defaults 0.40 / 0.84 / 3.5 0.49 / 0.51 / 3.8
Proposed fix, threshold 0.35 0.78 / 0.64 / 0.8 0.60 / 0.62 / 1.5

F1 goes from 0.54 to 0.70 (short) and 0.50 to 0.61 (long). Fitting on one memory set and testing on the other gives 0.65 / 0.61, so the gain is not specific to one set. Prototype comparison:

  • Capping every single match removed 29 noise picks and 3 relevant ones per length, all of them names or nicknames; the name-aware rule below keeps those.
  • A fixed x/(x+k) BM25 mapping works on short messages but saturates again on long ones (off-topic noise about 7 per window). Calibrating the vector lane (affine or relative-to-top) did not help MiniLM.
  • Labels and windows are hand-written over 2 memory sets, so treat numbers as directional.

Additional context

Proposed fix:

  1. Keyword: name-aware single-match cap. If a chunk's keyword hits are one distinct exact term (and fewer than 3 fuzzy phrase matches), cap the lane at 0.5 unless the query uses that word as a name (capitalized somewhere other than the start of a sentence or quote). Two or more distinct keywords, or a multi-word phrase, keep full credit. Regression tests: a single everyday word stays below the threshold; a single name used mid-sentence clears it; two keywords clear it.
    • Known limits: all-lowercase typing and caseless scripts get the plain cap; German noun capitalization lets more through (no worse than today). A later option is to recognise names from the vault's own characters and aliases instead.
  2. BM25: reference = summed idf of the query's 8 highest-idf terms present in the index, instead of all query terms. Regression test: padding a query with unrelated narration does not shrink a matching chunk's normalized score to near zero, and does not saturate unrelated chunks.
  3. Fuzzy keyword catalog (audit item 08): choose the fuzzy catalog by overlap with the query terms instead of alphabetical order, keeping a bound. Regression test: a keyword late in the alphabet in a scope with more than 512 keywords still fuzzy-matches.
  4. Presets: keep the 0.35 default and scale the weights. Relevance is lane score × weight, and the fused order scales with the weights, so weights × 0.7 at threshold 0.35 select exactly the same memories as weights × 1 at 0.5 (verified on the evaluation). Proposed balanced weights: semantic 0.7, lexical 0.42, keyword 0.56. Scale graph by the same factor and keep it: graph weight currently has no effect (see below), and a follow-up issue will make the graph lane count. Retune exact, broad and story on the same scale.
  5. Saved thresholds above 0.4: treat them as 0.4 on read. With the scaled weights, the strongest realistic match reaches about 0.49, so a saved 0.45 would inject almost nothing (17 of 18 labelled windows empty). Thresholds from 0.25 to 0.40 keep a graded meaning.

Compatibility:

  • Users on the default threshold need no change.
  • Users with custom weights will see different behavior, because the keyword and BM25 scores change underneath them; say so in the release note.
  • Update the threshold help text in locales/en.json if the ceiling note changes.

Related, tracked separately:

  • Graph lane: a graph match scores at most 0.5 × weight, so it can never clear the threshold alone. On the evaluation, every graph weight from 0 to 0.5 gave identical results, while 10 of 51 relevant memories the graph reached had no other signal. The graph is seeded from the top BM25 and keyword hits (retrieval.ts:189), so it should be revisited after this fix lands.
  • Debug, /search and dead-code clean-up: separate issue.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1264 Originally assigned to: @Promansis on GitHub. ## Summary Follow-up to #1258. On a real vault, 1.4.15 recall is decided almost entirely by the keyword lane. A single everyday word that happens to be a keyword on one memory clears the default threshold alone, while the BM25 lane contributes nothing on real chat windows. The fuzzy keyword catalog also only scans the alphabetically first 512 keywords, which real vaults exceed. This issue fixes those three lanes and rescales the presets so the existing 0.35 default threshold keeps working for users who never changed it. ## Affected package `long-term-memory` 1.4.15: - `keyword-index.ts`, `bm25.ts` under `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/` - `constants.ts`, `runtime-settings.ts` under `packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory/` ## Expected behavior - A memory is not injected because one everyday word in the recent messages ("half", "silence", "call", "way") is one of its keywords. A single match on a word the chat uses as a name still counts. - BM25 contributes on real multi-message, long-message chat windows, and its score does not saturate or vanish as messages get longer. - Fuzzy keyword matching can reach every in-scope keyword, not only those early in the alphabet. - Users on the default threshold (0.35) get the improved behavior without changing any setting. ## Actual behavior 1. **One exact keyword clears the threshold alone** (`keyword-index.ts:112`, `:150`). An exact term match scores 3/4, and `keywordFrequencyWeight` gives full weight to a keyword found on one in-scope memory, so the lane scores 0.75 and balanced relevance is 0.75 × 0.65 = 0.49 > 0.35. The frequency weight measures rarity in the keyword catalog, not in language, so everyday words that are keywords on one memory get full weight. - In 7 real recalls on default settings, keyword decided 60 of 68 picks; the matched words included "silence", "holds", "half", "mother", "note", "line", "way", "called" and "played", on memories with meaning-based similarity of 0.07–0.17. 2. **BM25 is inert on real chats** (`bm25.ts:69`, `:89`). The score is divided by the summed idf of every query term. A 4-message roleplay window has many terms (and no stop-word removal), so real BM25 scores never exceeded 0.07. Its 0.85 weight affects neither the threshold nor the fused order (RRF multiplies the rank term by the normalized score). 3. **Fuzzy catalog cap is alphabetical** (`keyword-index.ts:82`, `:154-160`). The fuzzy pass scans keywords in alphabetical order and stops at 512 in-scope entries. Two real memory sets have 900 and 1,769 in-scope keywords, so fuzzy matches on keywords later in the alphabet never happen. ## Steps to reproduce 1. Use a vault with a few dozen memories whose keywords include common words. 2. Enable debug logging and send a turn whose last 4 messages share no topic with the vault but contain one of those words (for example "There's half a sandwich left"). 3. In the `recall_explanation` event, see memories selected by `keyword` with `keyword:normalized:0.750` from a single `keyword:exact:` match, and `bm25:normalized` values below 0.1 on every candidate. ## Environment - Marinara Engine version: 2.5.0 line (`staging`) - Package version: long-term-memory 1.4.15 (`upstream/staging` 7fc3b91c2) - Install type: source (real vault copy on a Windows install, evaluated locally with the package's real retrieval code) - OS + version: not platform-specific - Browser or app shell: not UI-specific ## Logs, screenshots, or video Evaluation: real vault (206 chunks), 22 hand-written 4-message windows over two memory sets (18 labelled with essential/related memories, 4 off-topic), run through the real `retrieveLongTermMemory` with the vault's own MiniLM embeddings. "Long" pads each message to about 140 words, since real roleplay messages are long. Prototype fixes run through a replica that selects the same memories as the real code in 270/276 runs. | Setting | Short windows: precision / essential recall / off-topic memories | Long windows | |---|---|---| | 1.4.15 defaults | 0.40 / 0.84 / 3.5 | 0.49 / 0.51 / 3.8 | | Proposed fix, threshold 0.35 | 0.78 / 0.64 / 0.8 | 0.60 / 0.62 / 1.5 | F1 goes from 0.54 to 0.70 (short) and 0.50 to 0.61 (long). Fitting on one memory set and testing on the other gives 0.65 / 0.61, so the gain is not specific to one set. Prototype comparison: - Capping every single match removed 29 noise picks and 3 relevant ones per length, all of them names or nicknames; the name-aware rule below keeps those. - A fixed `x/(x+k)` BM25 mapping works on short messages but saturates again on long ones (off-topic noise about 7 per window). Calibrating the vector lane (affine or relative-to-top) did not help MiniLM. - Labels and windows are hand-written over 2 memory sets, so treat numbers as directional. ## Additional context Proposed fix: 1. **Keyword: name-aware single-match cap.** If a chunk's keyword hits are one distinct exact term (and fewer than 3 fuzzy phrase matches), cap the lane at 0.5 unless the query uses that word as a name (capitalized somewhere other than the start of a sentence or quote). Two or more distinct keywords, or a multi-word phrase, keep full credit. Regression tests: a single everyday word stays below the threshold; a single name used mid-sentence clears it; two keywords clear it. - Known limits: all-lowercase typing and caseless scripts get the plain cap; German noun capitalization lets more through (no worse than today). A later option is to recognise names from the vault's own characters and aliases instead. 2. **BM25: reference = summed idf of the query's 8 highest-idf terms** present in the index, instead of all query terms. Regression test: padding a query with unrelated narration does not shrink a matching chunk's normalized score to near zero, and does not saturate unrelated chunks. 3. **Fuzzy keyword catalog (audit item 08):** choose the fuzzy catalog by overlap with the query terms instead of alphabetical order, keeping a bound. Regression test: a keyword late in the alphabet in a scope with more than 512 keywords still fuzzy-matches. 4. **Presets: keep the 0.35 default and scale the weights.** Relevance is lane score × weight, and the fused order scales with the weights, so weights × 0.7 at threshold 0.35 select exactly the same memories as weights × 1 at 0.5 (verified on the evaluation). Proposed balanced weights: semantic 0.7, lexical 0.42, keyword 0.56. Scale graph by the same factor and keep it: graph weight currently has no effect (see below), and a follow-up issue will make the graph lane count. Retune exact, broad and story on the same scale. 5. **Saved thresholds above 0.4:** treat them as 0.4 on read. With the scaled weights, the strongest realistic match reaches about 0.49, so a saved 0.45 would inject almost nothing (17 of 18 labelled windows empty). Thresholds from 0.25 to 0.40 keep a graded meaning. Compatibility: - Users on the default threshold need no change. - Users with custom weights will see different behavior, because the keyword and BM25 scores change underneath them; say so in the release note. - Update the threshold help text in `locales/en.json` if the ceiling note changes. Related, tracked separately: - Graph lane: a graph match scores at most 0.5 × weight, so it can never clear the threshold alone. On the evaluation, every graph weight from 0 to 0.5 gave identical results, while 10 of 51 relevant memories the graph reached had no other signal. The graph is seeded from the top BM25 and keyword hits (`retrieval.ts:189`), so it should be revisited after this fix lands. - Debug, `/search` and dead-code clean-up: separate issue. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:29:18 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@Promansis commented on GitHub (Oct 8, 2026):

The Debug, /search and dead-code clean-up mentioned under Related is tracked in #1265.

<!-- gh-comment-id:6051360926 --> @Promansis commented on GitHub (Oct 8, 2026): The Debug, `/search` and dead-code clean-up mentioned under Related is tracked in #1265.
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#643
No description provided.