[GH-ISSUE #1258] [Issue]: LTM recall scoring: finish per-lane calibration and retune the recall presets #641

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

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

Originally assigned to: @Promansis on GitHub.

Summary

Follow-up to #1250. The 1.4.9 fix removed the unbounded BM25 boost and gave the keyword lane a fixed ceiling, but the lanes are still not on a comparable, absolute scale. BM25 still normalizes every hit to about 0.95. The keyword lane still saturates because it sums matches. As a result, the score threshold still behaves as a cliff, and the recall presets (style weights, maxChunks, threshold) cannot be tuned meaningfully. This issue finishes the calibration and then retunes the presets on top of it.

Affected package

long-term-memory 1.4.11:

  • ranking.ts, retrieval.ts, keyword-index.ts and graph.ts under packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/
  • constants.ts, schema.ts and runtime-settings.ts under packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory/

Expected behavior

  • Each lane's normalized score is on an absolute 0–1 scale that separates relevant hits from non-relevant ones. Raising the threshold therefore removes progressively weaker matches in every style instead of dropping from many memories to almost none.
  • A chunk that only shares common auto-generated keywords with the query (for example two character names) does not reach full keyword relevance.
  • The default settings inject a bounded, mostly relevant set of memories. The style presets are tuned against the calibrated scale.
  • Per-mode default recall styles either apply or do not exist.

Actual behavior

  1. BM25 saturates (ranking.ts:43, rawScore / (rawScore + 1)). The raw BM25 score is an unbounded idf sum, so almost every hit normalizes near 1.
    • In four real recall_explanation events (78 eligible chunks, raw scores 16.5–54.6), every BM25 hit normalized to 0.943–0.982.
    • Under balanced (lexical weight 0.3), every BM25 hit therefore gets relevance of about 0.29.
  2. Keyword relevance saturates (keyword-index.ts:83, retrieval.ts:178). The add helper sums every exact (3–4) and fuzzy (0.75–2.25) match on a chunk, and retrieval clamps the sum at 4.
    • Two exact matches on generic auto-extracted keywords (character names, words like "keeps" or "crates") reach 1.0.
    • On a labelled 40-chunk fixture, median keyword relevance is 1.000 for relevant hits and 0.982 for non-relevant hits (AUC 0.71).
  3. Graph ceiling (graph.ts:92, 1 / (distance + 1)). The graph lane never exceeds 0.5 × weight, and this ceiling is not documented.
  4. Threshold cliff. On the fixture, with 1.4.11 scoring and maxChunks 20:
    • balanced injects 16.8 memories per query at threshold 0.25 and 2.8 at 0.30.
    • story injects none at 0.40.
    • exact ignores any threshold up to 0.5.
  5. Presets are untuned for the current scoring. The defaults are longTermMemoryMaxChunks 20 and longTermMemoryScoreThreshold 0 (schema.ts:156-157), with the style weights at constants.ts:284-313.
    • At these defaults the fixture injects 20 chunks per query at precision 0.17, and the two off-topic queries inject 39 irrelevant chunks.
    • The four styles score nDCG@10 0.770–0.819.
  6. Unreachable per-mode defaults (constants.ts:275-279, runtime-settings.ts:100-104). DEFAULT_LTM_RECALL_STYLE_BY_MODE (roleplay → story, game → exact) is used only when no global settings exist. The resolved global settings always carry a style, so the map never applies.

Steps to reproduce

  1. Enable debug logging and run recall with balanced on a chat whose recall window spans several messages.
  2. In the recall_explanation event, read the normalized BM25 score of each selected candidate (about 0.94–0.98) and its relevance (about 0.29).
  3. Set the score threshold to 0.25, then to 0.30, and compare the number of injected memories.
  4. Query with a message that mentions two main character names but nothing else relevant, and observe keyword relevance of 1.0 on unrelated notes that list those names as keywords.

Environment

  • Marinara Engine version: 2.5.0 line (staging)
  • Package version: long-term-memory 1.4.11
  • Install type: source (code audit, tsx probes against src/, and a labelled fixture that runs the real storage, index and retrieval code with all-MiniLM-L6-v2 embeddings)
  • OS + version: not platform-specific
  • Browser or app shell: not UI-specific

Logs, screenshots, or video

Fixture results with 1.4.11 scoring (12 labelled queries and 2 off-topic queries; the replica matched real retrieveLongTermMemory selections in 112/112 runs):

Setting F1 Precision Recall Off-topic noise
balanced, maxChunks 20, t=0 0.29 0.17 1.00 39
balanced, maxChunks 12, t=0 0.38 0.24 0.92 23
balanced, maxChunks 8, t=0 0.45 0.32 0.82 15
BM25 x/(x+10), weights .7/.7/.3/.3, t=0.40 0.58 0.61 0.61 1
Best grid weights without BM25 recalibration (.75/.3/.3/.3, t=0.35) 0.43 0.37 0.60 8
Best grid weights with BM25 x/(x+10) (.75/.75/.75/.3, t=0.40) 0.63 0.61 0.74 1

Real raw BM25 scores are 2–3× larger than the fixture's, so the x/(x+10) constant is a fixture result, not a recommendation.

Additional context

Proposed fix, in order:

  1. BM25. Map scores to an absolute scale that does not saturate with query length or corpus size. Options: normalize by the query's maximum attainable BM25 score, or use x/(x+k) with k calibrated on a real vault export. Add a test that a long multi-message query does not normalize every hit above 0.9.
  2. Keyword. Stop summing to saturation, for example by taking the best match per chunk or adding diminishing returns. Down-weight keywords that appear on a large share of notes. Add a test that two generic shared keywords stay well below 1.0, while an exact phrase match still scores high.
  3. Graph. Rescale the graph lane or document its ceiling, together with each style's maximum relevance per lane.
  4. Presets.
    • Lower the default longTermMemoryMaxChunks to 10–12.
    • Retune the style weights and set a default threshold (expected around 0.35–0.40) on the calibrated scale.
    • Validate against a real vault export, not only the fixture.
  5. Per-mode defaults. Either delete DEFAULT_LTM_RECALL_STYLE_BY_MODE and the unused custom preset weights, or make the per-mode style apply until the user picks a style. Deleting is simpler; on the fixture, story now scores lowest.

Compatibility note: a threshold that a user saved under the old scale (for example 0.25) will mean something different after calibration. The release note should say so, and the settings help text (locales/en.json) should describe the new scale.

Out of scope, to be tracked separately:

  • The query is embedded with the document input type (needs an Engine embedding-adapter change).
  • Activity view relevance display vs fused order.
  • /search preview defaults.
  • Dead cooldown plumbing.
  • Serializer estimate of section labels and titles.
  • Alphabetical 512-keyword fuzzy cap.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1258 Originally assigned to: @Promansis on GitHub. ## Summary Follow-up to #1250. The 1.4.9 fix removed the unbounded BM25 boost and gave the keyword lane a fixed ceiling, but the lanes are still not on a comparable, absolute scale. BM25 still normalizes every hit to about 0.95. The keyword lane still saturates because it sums matches. As a result, the score threshold still behaves as a cliff, and the recall presets (style weights, `maxChunks`, threshold) cannot be tuned meaningfully. This issue finishes the calibration and then retunes the presets on top of it. ## Affected package `long-term-memory` 1.4.11: - `ranking.ts`, `retrieval.ts`, `keyword-index.ts` and `graph.ts` under `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/` - `constants.ts`, `schema.ts` and `runtime-settings.ts` under `packages/long-term-memory/src/engine/packages/shared/src/features/agents/long-term-memory/` ## Expected behavior - Each lane's normalized score is on an absolute 0–1 scale that separates relevant hits from non-relevant ones. Raising the threshold therefore removes progressively weaker matches in every style instead of dropping from many memories to almost none. - A chunk that only shares common auto-generated keywords with the query (for example two character names) does not reach full keyword relevance. - The default settings inject a bounded, mostly relevant set of memories. The style presets are tuned against the calibrated scale. - Per-mode default recall styles either apply or do not exist. ## Actual behavior 1. **BM25 saturates** (`ranking.ts:43`, `rawScore / (rawScore + 1)`). The raw BM25 score is an unbounded idf sum, so almost every hit normalizes near 1. - In four real `recall_explanation` events (78 eligible chunks, raw scores 16.5–54.6), every BM25 hit normalized to 0.943–0.982. - Under `balanced` (lexical weight 0.3), every BM25 hit therefore gets relevance of about 0.29. 2. **Keyword relevance saturates** (`keyword-index.ts:83`, `retrieval.ts:178`). The `add` helper sums every exact (3–4) and fuzzy (0.75–2.25) match on a chunk, and retrieval clamps the sum at 4. - Two exact matches on generic auto-extracted keywords (character names, words like "keeps" or "crates") reach 1.0. - On a labelled 40-chunk fixture, median keyword relevance is 1.000 for relevant hits and 0.982 for non-relevant hits (AUC 0.71). 3. **Graph ceiling** (`graph.ts:92`, `1 / (distance + 1)`). The graph lane never exceeds 0.5 × weight, and this ceiling is not documented. 4. **Threshold cliff.** On the fixture, with 1.4.11 scoring and `maxChunks` 20: - `balanced` injects 16.8 memories per query at threshold 0.25 and 2.8 at 0.30. - `story` injects none at 0.40. - `exact` ignores any threshold up to 0.5. 5. **Presets are untuned for the current scoring.** The defaults are `longTermMemoryMaxChunks` 20 and `longTermMemoryScoreThreshold` 0 (`schema.ts:156-157`), with the style weights at `constants.ts:284-313`. - At these defaults the fixture injects 20 chunks per query at precision 0.17, and the two off-topic queries inject 39 irrelevant chunks. - The four styles score nDCG@10 0.770–0.819. 6. **Unreachable per-mode defaults** (`constants.ts:275-279`, `runtime-settings.ts:100-104`). `DEFAULT_LTM_RECALL_STYLE_BY_MODE` (roleplay → story, game → exact) is used only when no global settings exist. The resolved global settings always carry a style, so the map never applies. ## Steps to reproduce 1. Enable debug logging and run recall with `balanced` on a chat whose recall window spans several messages. 2. In the `recall_explanation` event, read the normalized BM25 score of each selected candidate (about 0.94–0.98) and its relevance (about 0.29). 3. Set the score threshold to 0.25, then to 0.30, and compare the number of injected memories. 4. Query with a message that mentions two main character names but nothing else relevant, and observe keyword relevance of 1.0 on unrelated notes that list those names as keywords. ## Environment - Marinara Engine version: 2.5.0 line (`staging`) - Package version: long-term-memory 1.4.11 - Install type: source (code audit, tsx probes against `src/`, and a labelled fixture that runs the real storage, index and retrieval code with all-MiniLM-L6-v2 embeddings) - OS + version: not platform-specific - Browser or app shell: not UI-specific ## Logs, screenshots, or video Fixture results with 1.4.11 scoring (12 labelled queries and 2 off-topic queries; the replica matched real `retrieveLongTermMemory` selections in 112/112 runs): | Setting | F1 | Precision | Recall | Off-topic noise | |---|---|---|---|---| | `balanced`, maxChunks 20, t=0 | 0.29 | 0.17 | 1.00 | 39 | | `balanced`, maxChunks 12, t=0 | 0.38 | 0.24 | 0.92 | 23 | | `balanced`, maxChunks 8, t=0 | 0.45 | 0.32 | 0.82 | 15 | | BM25 `x/(x+10)`, weights .7/.7/.3/.3, t=0.40 | 0.58 | 0.61 | 0.61 | 1 | | Best grid weights without BM25 recalibration (.75/.3/.3/.3, t=0.35) | 0.43 | 0.37 | 0.60 | 8 | | Best grid weights with BM25 `x/(x+10)` (.75/.75/.75/.3, t=0.40) | 0.63 | 0.61 | 0.74 | 1 | Real raw BM25 scores are 2–3× larger than the fixture's, so the `x/(x+10)` constant is a fixture result, not a recommendation. ## Additional context Proposed fix, in order: 1. **BM25.** Map scores to an absolute scale that does not saturate with query length or corpus size. Options: normalize by the query's maximum attainable BM25 score, or use `x/(x+k)` with `k` calibrated on a real vault export. Add a test that a long multi-message query does not normalize every hit above 0.9. 2. **Keyword.** Stop summing to saturation, for example by taking the best match per chunk or adding diminishing returns. Down-weight keywords that appear on a large share of notes. Add a test that two generic shared keywords stay well below 1.0, while an exact phrase match still scores high. 3. **Graph.** Rescale the graph lane or document its ceiling, together with each style's maximum relevance per lane. 4. **Presets.** - Lower the default `longTermMemoryMaxChunks` to 10–12. - Retune the style weights and set a default threshold (expected around 0.35–0.40) on the calibrated scale. - Validate against a real vault export, not only the fixture. 5. **Per-mode defaults.** Either delete `DEFAULT_LTM_RECALL_STYLE_BY_MODE` and the unused `custom` preset weights, or make the per-mode style apply until the user picks a style. Deleting is simpler; on the fixture, `story` now scores lowest. Compatibility note: a threshold that a user saved under the old scale (for example 0.25) will mean something different after calibration. The release note should say so, and the settings help text (`locales/en.json`) should describe the new scale. Out of scope, to be tracked separately: - The query is embedded with the document input type (needs an Engine embedding-adapter change). - Activity view relevance display vs fused order. - `/search` preview defaults. - Dead cooldown plumbing. - Serializer estimate of section labels and titles. - Alphabetical 512-keyword fuzzy cap. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:29:16 +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#641
No description provided.