[PR #1472] [MERGED] fix(long-term-memory): fast keyword lane, recall one distinctive keyword BM25 agrees with (1.4.54) #1478

Closed
opened 2026-10-21 05:11:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Agents/pull/1472
Author: @SpicyMarinara
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/19/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/ltm-keyword-lane-1467-1465


📝 Commits (2)

  • e3db751 perf(long-term-memory): scan keyword tokens without a regex per call (#1467)
  • e565255 fix(long-term-memory): recall one distinctive keyword when BM25 agrees (#1465)

📊 Changes

14 files changed (+337 additions, -90 deletions)

View changed files

➕ artifacts/long-term-memory-1.4.54.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 (+5 -0)
📝 packages/long-term-memory/manifest.json (+3 -3)
📝 packages/long-term-memory/server.mjs (+39 -39)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/recall/keyword-index.ts (+47 -6)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/recall/retrieval.ts (+12 -2)
📝 scripts/build-feature-packages.mjs (+1 -1)
📝 tests/long-term-memory-index-keys.regression.ts (+194 -3)

📄 Description

Pull request

Linked issue

Closes #1467
Closes #1465

Both issues and their measurements are by @Promansis. The maintainer asked for both to land in this release. They touch the same two files, so they land one after the other in one PR, as #1467 asks: two commits, one patch release.

Why this change

  • #1467: recall was slow. Every recall's keyword search compiled a fresh Unicode regex for each in-scope keyword, against the query and against every query term. On a ~200-memory vault that was 98% of warm retrieval time, synchronous on the server thread, and it grew faster than linearly once V8's regex cache stopped reusing patterns (10 times the keywords about 1.1 s per recall, 50 times about 55 s). The search also ran when the keyword weight was 0, although nothing read its result.
  • #1465: a memory that shares one distinctive word with the chat was never recalled. Since #1264, one ordinary exact keyword is capped at half the keyword lane: relevance 0.28 in the balanced style, under the 0.35 default threshold. That is right for everyday words, but it also blocked memories whose only overlap is a rare word that full-text search agrees with. Example from the issue: the chat says "Want to hear me play the violin?", and "Elena's violin was smashed in the storm" stays out, so the reply can have Elena tune her violin. On the reporter's labelled fixture this recovered 24 essential memories, lost none, and added 1.92 unwanted per recovered one, within the agreed limit of 2.

What changed

S = packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory

Commit 1: #1467

  • S/recall/keyword-index.ts: containsKeywordToken is an indexOf scan over every occurrence, with one module-level /[\p{L}\p{N}]/u test on the code point before and after. It keeps the old rule exactly. A whole occurrence after one inside a word still counts. An empty needle stops at the end of the string (indexOf clamps it there, so the loop needs that stop). A surrogate pair before the match is read as one code point, and a match never starts or ends inside a pair, like the u regex.
  • S/recall/retrieval.ts: searchLtmKeywordIndex runs only when (input.keywordWeight ?? 1) > 0.

Commit 2: #1465

  • S/recall/keyword-index.ts: searchLtmKeywordIndex takes an optional lexicalScores map (chunk id to BM25 normalized score). In the #1264 cap loop, a single exact keyword hit that the cap would cut (score at or above LTM_KEYWORD_MAX_SCORE / 2) and whose lexical score is at least the new exported LTM_SINGLE_KEYWORD_BM25_AGREEMENT (0.2) scores LTM_KEYWORD_MAX_SCORE * 0.75 instead. Names, phrases, two-keyword hits, and keywords the cap does not cut are unchanged.
  • S/recall/retrieval.ts: passes the BM25 normalized scores it already computes.

Release

  • long-term-memory 1.4.54 (1.4.53 was the highest published version), with CHANGELOG.md, version, manifest, rebuilt server.mjs, artifact, catalog entries and notes. client.js is unchanged.
  • Both issues ask for the bullet under ## Unreleased. The bullets went under ## 1.4.54 — 2026-10-19 instead, because the notes build drops an undated heading and would then fail: "CHANGELOG leads with 1.4.53 but the catalog publishes 1.4.54".
  • Built with MARINARA_ENGINE_ROOT pinned to Engine a2e3f13d8 (current Engine staging). Before any change, it rebuilt the unchanged 1.4.53 byte for byte (git status clean).

For reviewers

  • Per the issue, a lifted hit scores 0.75 flat. A keyword on two or three memories in scope has a frequency-weighted score between 0.5 and 0.75. When BM25 agrees, it gets 0.75, a little more than it would score with no cap at all. This is the variant the reporter measured and pre-registered.
  • The lift applies in every retrieveLongTermMemory caller, not only chat recall. Source extraction context and candidate reconciliation use no threshold, but they rank by fused score, so a lifted chunk can move up a little there too. Their regressions pass unchanged.
  • Another PR on this package (Pasta-Devs/Marinara-Agents#1466, strict TypeScript errors) is in progress in parallel. Whichever lands second takes the next version and rebuilds.

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: none. Recall scoring and speed only; there is no index, schema, or stored-data change.

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

Run by the coding agent, not by a person, on Node v24.16.0:

  • npm run test:security: passes (catalog path safety, then the Long-Term Memory index-keys regression with every new case).
  • The Engine-backed LTM regressions from ltm-browser.yml, run through the Engine worktree at a2e3f13d8 with MARINARA_ENGINE_ROOT set: candidate-reconciliation, conversation-summary-import, debug-log, delta-review, extraction-graph, engine-fields, extraction-reliability, index-keys, local-characters, reconciliation-recall, branch-recall, storage, routes, routes-imports, routes-drafts, routes-scope-identity, routes-backup, runtime, installation and host-styles all pass, plus node tests/long-term-memory-loading.regression.mjs. The browser regression was not run.
  • The Engine-free LTM regressions from pull-request-checks.yml (scope-targets, scope-fallback-labels, tab-keys, review-queue-ambiguous-link, source-task, source-pagination, debug-activity, feedback-clarity-ui): all pass.
  • npm run check: Prettier clean; ESLint 0 errors. Its 811 warnings are pre-existing, and none are in the changed files.
  • node scripts/test-catalog-lanes.mjs, node scripts/validate-package-locales.mjs, node scripts/validate-catalog.mjs, node scripts/tests/catalog-release-notes.regression.mjs, git diff --check: all pass.
  • Equivalence probe for #1467 (scratch script, not committed): old regex and new scan compared on 301,936 haystack/needle pairs. They covered hyphens, apostrophes, digits, accents, CJK, Hangul, astral-plane letters, emoji, lone surrogates, regex metacharacters and empty strings in both argument orders, plus 300,000 random strings from those characters. There were 0 differences. A synthetic fuzzy-lane workload (1,500 keywords, 100 query terms) took 118.5 ms per recall before and 7.8 ms after, with the same hits.
  • Red/green: the new test file was run against copies of the earlier code. On origin/staging, every containment case passes and only the keywordWeight: 0 check fails, which shows the cases pin the existing rule. On commit 1, the #1465 unit case fails. With the new keyword-index.ts but the old retrieval.ts, the violin retrieval case fails.

Manual verification notes

Not done by the agent: an install through Marinara Engine, or a live recall.

  • Manually verify in Marinara Engine (staging) that Long-Term Memory updates to 1.4.54 through Agents → Download Agents.
  • Manually verify in a Roleplay chat with recall debug on that a memory sharing one uncommon word with your message is recalled. Use a keyword only one or two memories have and that appears in the memory's text, with the balanced style and default threshold. The Activity view should show it above the 0.35 threshold marker.
  • Manually verify in the same chat that a message sharing only an everyday word (one many memories carry) still does not recall those memories.
  • Manually verify on a large vault (thousands of keywords in one chat's scope) that recall no longer takes noticeably long, by comparing the recall timing in the debug log before and after the update.
  • Manually verify that setting the keyword weight to 0 in a custom recall style still recalls memories through the other lanes.

Documentation impact

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

The agent changed no documentation: recall settings, catalog membership and setup are unchanged.

Summary by CodeRabbit

  • New Features
    • Long-Term Memory 1.4.54 improves keyword-search performance and can better recall a memory that shares one distinctive word with the chat when full-text search also finds it relevant. Everyday words, names, phrases, and memories matching multiple keywords retain their previous scoring.
  • Bug Fixes
    • Keyword matching now respects whole-word boundaries across different writing systems, reducing unintended matches.

🔄 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/1472 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/ltm-keyword-lane-1467-1465` --- ### 📝 Commits (2) - [`e3db751`](https://github.com/Pasta-Devs/Marinara-Agents/commit/e3db751ba9da5932a0f2201f96ba5c21ceab9f1a) perf(long-term-memory): scan keyword tokens without a regex per call (#1467) - [`e565255`](https://github.com/Pasta-Devs/Marinara-Agents/commit/e5652555c60872da575dddb682ac8d667c203e58) fix(long-term-memory): recall one distinctive keyword when BM25 agrees (#1465) ### 📊 Changes **14 files changed** (+337 additions, -90 deletions) <details> <summary>View changed files</summary> ➕ `artifacts/long-term-memory-1.4.54.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` (+5 -0) 📝 `packages/long-term-memory/manifest.json` (+3 -3) 📝 `packages/long-term-memory/server.mjs` (+39 -39) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/recall/keyword-index.ts` (+47 -6) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/recall/retrieval.ts` (+12 -2) 📝 `scripts/build-feature-packages.mjs` (+1 -1) 📝 `tests/long-term-memory-index-keys.regression.ts` (+194 -3) </details> ### 📄 Description # Pull request ## Linked issue Closes #1467 Closes #1465 Both issues and their measurements are by @Promansis. The maintainer asked for both to land in this release. They touch the same two files, so they land one after the other in one PR, as #1467 asks: two commits, one patch release. ## Why this change - **#1467: recall was slow.** Every recall's keyword search compiled a fresh Unicode regex for each in-scope keyword, against the query and against every query term. On a ~200-memory vault that was 98% of warm retrieval time, synchronous on the server thread, and it grew faster than linearly once V8's regex cache stopped reusing patterns (10 times the keywords about 1.1 s per recall, 50 times about 55 s). The search also ran when the keyword weight was 0, although nothing read its result. - **#1465: a memory that shares one distinctive word with the chat was never recalled.** Since #1264, one ordinary exact keyword is capped at half the keyword lane: relevance 0.28 in the balanced style, under the 0.35 default threshold. That is right for everyday words, but it also blocked memories whose only overlap is a rare word that full-text search agrees with. Example from the issue: the chat says "Want to hear me play the violin?", and "Elena's violin was smashed in the storm" stays out, so the reply can have Elena tune her violin. On the reporter's labelled fixture this recovered 24 essential memories, lost none, and added 1.92 unwanted per recovered one, within the agreed limit of 2. ## What changed `S` = `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory` ### Commit 1: #1467 - `S/recall/keyword-index.ts`: `containsKeywordToken` is an `indexOf` scan over every occurrence, with one module-level `/[\p{L}\p{N}]/u` test on the code point before and after. It keeps the old rule exactly. A whole occurrence after one inside a word still counts. An empty needle stops at the end of the string (`indexOf` clamps it there, so the loop needs that stop). A surrogate pair before the match is read as one code point, and a match never starts or ends inside a pair, like the `u` regex. - `S/recall/retrieval.ts`: `searchLtmKeywordIndex` runs only when `(input.keywordWeight ?? 1) > 0`. ### Commit 2: #1465 - `S/recall/keyword-index.ts`: `searchLtmKeywordIndex` takes an optional `lexicalScores` map (chunk id to BM25 normalized score). In the #1264 cap loop, a single exact keyword hit that the cap would cut (score at or above `LTM_KEYWORD_MAX_SCORE / 2`) and whose lexical score is at least the new exported `LTM_SINGLE_KEYWORD_BM25_AGREEMENT` (0.2) scores `LTM_KEYWORD_MAX_SCORE * 0.75` instead. Names, phrases, two-keyword hits, and keywords the cap does not cut are unchanged. - `S/recall/retrieval.ts`: passes the BM25 normalized scores it already computes. ### Release - `long-term-memory` 1.4.54 (1.4.53 was the highest published version), with `CHANGELOG.md`, version, manifest, rebuilt `server.mjs`, artifact, catalog entries and notes. `client.js` is unchanged. - Both issues ask for the bullet under `## Unreleased`. The bullets went under `## 1.4.54 — 2026-10-19` instead, because the notes build drops an undated heading and would then fail: "CHANGELOG leads with 1.4.53 but the catalog publishes 1.4.54". - Built with `MARINARA_ENGINE_ROOT` pinned to Engine `a2e3f13d8` (current Engine `staging`). Before any change, it rebuilt the unchanged 1.4.53 byte for byte (`git status` clean). ### For reviewers - Per the issue, a lifted hit scores 0.75 flat. A keyword on two or three memories in scope has a frequency-weighted score between 0.5 and 0.75. When BM25 agrees, it gets 0.75, a little more than it would score with no cap at all. This is the variant the reporter measured and pre-registered. - The lift applies in every `retrieveLongTermMemory` caller, not only chat recall. Source extraction context and candidate reconciliation use no threshold, but they rank by fused score, so a lifted chunk can move up a little there too. Their regressions pass unchanged. - Another PR on this package (Pasta-Devs/Marinara-Agents#1466, strict TypeScript errors) is in progress in parallel. Whichever lands second takes the next version and rebuilds. ## 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: none. Recall scoring and speed only; there is no index, schema, or stored-data change. ## 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` Run by the coding agent, not by a person, on Node v24.16.0: - `npm run test:security`: passes (catalog path safety, then the Long-Term Memory index-keys regression with every new case). - The Engine-backed LTM regressions from `ltm-browser.yml`, run through the Engine worktree at `a2e3f13d8` with `MARINARA_ENGINE_ROOT` set: candidate-reconciliation, conversation-summary-import, debug-log, delta-review, extraction-graph, engine-fields, extraction-reliability, index-keys, local-characters, reconciliation-recall, branch-recall, storage, routes, routes-imports, routes-drafts, routes-scope-identity, routes-backup, runtime, installation and host-styles all pass, plus `node tests/long-term-memory-loading.regression.mjs`. The browser regression was not run. - The Engine-free LTM regressions from `pull-request-checks.yml` (scope-targets, scope-fallback-labels, tab-keys, review-queue-ambiguous-link, source-task, source-pagination, debug-activity, feedback-clarity-ui): all pass. - `npm run check`: Prettier clean; ESLint 0 errors. Its 811 warnings are pre-existing, and none are in the changed files. - `node scripts/test-catalog-lanes.mjs`, `node scripts/validate-package-locales.mjs`, `node scripts/validate-catalog.mjs`, `node scripts/tests/catalog-release-notes.regression.mjs`, `git diff --check`: all pass. - **Equivalence probe for #1467** (scratch script, not committed): old regex and new scan compared on 301,936 haystack/needle pairs. They covered hyphens, apostrophes, digits, accents, CJK, Hangul, astral-plane letters, emoji, lone surrogates, regex metacharacters and empty strings in both argument orders, plus 300,000 random strings from those characters. There were 0 differences. A synthetic fuzzy-lane workload (1,500 keywords, 100 query terms) took 118.5 ms per recall before and 7.8 ms after, with the same hits. - **Red/green**: the new test file was run against copies of the earlier code. On `origin/staging`, every containment case passes and only the `keywordWeight: 0` check fails, which shows the cases pin the existing rule. On commit 1, the #1465 unit case fails. With the new `keyword-index.ts` but the old `retrieval.ts`, the violin retrieval case fails. ### Manual verification notes Not done by the agent: an install through Marinara Engine, or a live recall. - Manually verify in Marinara Engine (staging) that Long-Term Memory updates to 1.4.54 through Agents → Download Agents. - Manually verify in a Roleplay chat with recall debug on that a memory sharing one uncommon word with your message is recalled. Use a keyword only one or two memories have and that appears in the memory's text, with the balanced style and default threshold. The Activity view should show it above the 0.35 threshold marker. - Manually verify in the same chat that a message sharing only an everyday word (one many memories carry) still does not recall those memories. - Manually verify on a large vault (thousands of keywords in one chat's scope) that recall no longer takes noticeably long, by comparing the recall timing in the debug log before and after the update. - Manually verify that setting the keyword weight to 0 in a custom recall style still recalls memories through the other lanes. ## Documentation impact - [ ] No documentation changes needed - [ ] Updated this README catalog and package guidance - [ ] Updated linked Marinara Engine documentation The agent changed no documentation: recall settings, catalog membership and setup are unchanged. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Long-Term Memory 1.4.54 improves keyword-search performance and can better recall a memory that shares one distinctive word with the chat when full-text search also finds it relevant. Everyday words, names, phrases, and memories matching multiple keywords retain their previous scoring. * **Bug Fixes** * Keyword matching now respects whole-word boundaries across different writing systems, reducing unintended matches. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-21 05:11:54 +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#1478
No description provided.