mirror of
https://github.com/Pasta-Devs/Marinara-Agents.git
synced 2026-12-03 20:58:37 +07:00
[GH-ISSUE #1467] [Issue]: Long-Term Memory: the keyword lane rebuilds regexes on every recall and takes 98% of retrieval time #747
Labels
No labels
agent: chess
agent: conversation-calls
agent: eightball
agent: haptic
agent: hierarchical-maps
agent: hierarchical-maps
agent: illustrator
agent: poker
agent: rock-paper-scissors
agent: tic-tac-toe
agent: uno
artifacts
bug
bugfix
build
catalog
ci
client
dependencies
docs
documentation
enhancement
feature
fixed-in-staging
in-pr
normal-scope
packages
pull-request
qol
regression
roadmap
scope-risk
server
soup
upstream
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Agents#747
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Promansis on GitHub (Oct 19, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1467
Originally assigned to: @SpicyMarinara, @Promansis on GitHub.
Summary
Every recall's keyword search compiles fresh Unicode regexes for each in-scope keyword, against the query and against every query term. On a vault of about 200 memories this is 98% of warm retrieval time, synchronous on the server thread. It grows faster than linearly once V8's regex cache stops reusing compiled patterns: 10 times the keywords takes about 1.1 s per recall, and 50 times about 55 s. The search also runs when the keyword weight is 0.
Affected package
long-term-memory1.4.53 (upstream/staging2a3c37bb2).S=packages/long-term-memory/src/engine/packages/server/src/services/long-term-memoryExpected behavior
Actual behavior
containsKeywordToken(S/recall/keyword-index.ts:61-64) builds anew RegExpon every call.:183-201) calls it twice per in-scope keyword for the whole query and, when that misses, up to twice per query term. A long window has about 100 query terms, so one recall can build several hundred thousand patterns.S/recall/retrieval.ts:178runs the search before the weight check at:179.Measurement
A private fixture: 206 memories, 4,670 distinct keywords, 900–1,712 in scope per chat, 76 query windows run short and long. Query terms per window: median 31 short, 102 long. Warm index, embeddings memoized, 10 repetitions. Only aggregate numbers are shared here.
At 50 times the keywords, a 5-fold increase in data costs 49 times the time; linear would be 5. A microbenchmark shows why: re-running a pattern costs about 1–12 µs while fewer than about 5,000 distinct patterns are live, and about 415 µs (a full recompile) beyond 10,000.
Audit finding 08's correctness problem is already fixed: scope filters before the cap (1.4.10) and the cap ranks by overlap (1.4.16). Today 0 of 76 windows exceed the 512-entry fuzzy cap; at 10 times the keywords every sampled window would.
Steps to reproduce
searchLtmKeywordIndex, or compare a recall against the same recall with an empty keyword index. Recall time grows with the keyword count much faster than the rest of retrieval.Minimal fix
One PR, patch bump.
S/recall/keyword-index.ts:61-64: replace thenew RegExpincontainsKeywordTokenwith anindexOfscan over every occurrence, plus a boundary check on the code point before and after each one against a single module-level/[\p{L}\p{N}]/u. Keep today's rule exactly. A later occurrence still counts when an earlier one sits inside a word. An empty needle matches as the regex does, butindexOfclamps an empty needle's start to the string length, so the loop needs an explicit end condition or it never ends.S/recall/retrieval.ts:178: callsearchLtmKeywordIndexonly when(input.keywordWeight ?? 1) > 0. Only the keyword lane at:179uses its result.## Unreleased: one bullet.A probe of this fix on the same fixture used a warm index, embeddings off and 5 repetitions:
The probe did not re-measure the 10× and 50× growth with the fix. The remaining work is a plain string scan per keyword and query term, so there is no compile cache left to overflow.
Not included
Overlap: #1465 also changes
S/recall/keyword-index.tsandS/recall/retrieval.ts:178. Land the two one after the other, not in parallel.Regression proof
tests/long-term-memory-index-keys.regression.ts: pincontainsKeywordToken's whole-token rule throughsearchLtmKeywordIndex.kingmust not fuzzy-match insidelooking, a second occurrence after a non-boundary one must still match, and boundaries next to hyphens, apostrophes, digits, CJK and astral-plane letters must behave as they do today.keywordWeight: 0that shows the keyword lane is absent and the selection is unchanged.Environment
staging2039583ca)upstream/staging2a3c37bb2)Logs, screenshots, or video
None. Timings come from a private harness on a 12-core host.
Additional context
Tracker item RS-3 (recall scoring audit, finding 08). #1364 removed the per-turn vault scan; this per-recall keyword scan remains.
Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:
@SpicyMarinara commented on GitHub (Oct 19, 2026):
@Promansis the maintainer asked us to land this for the release, so it is in Pasta-Devs/Marinara-Agents#1472, together with Pasta-Devs/Marinara-Agents#1465 as you suggested (two commits, Long-Term Memory 1.4.54).
containsKeywordTokenis now theindexOfscan with one module-level/[\p{L}\p{N}]/uboundary check, and the keyword search is skipped at keyword weight 0. One addition to your fix: the scan also skips positions inside a surrogate pair, because the olduregex never matched there. Without that, an empty needle next to an astral-plane letter differed. An equivalence probe found 0 differences over about 300k pairs, including lone surrogates. Thank you for the measurements and the exact fix; they made this quick to land.@SpicyMarinara commented on GitHub (Oct 19, 2026):
Fixed on staging by #1472; update Long-Term Memory from the catalog to get it.