[PR #7701] [MERGED] feat(memory): Decision recall shortlist by meaning, time sized to the backend #7888

Closed
opened 2026-10-24 04:08:30 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7701
Author: @SpicyMarinara
Created: 10/15/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/memory-recall-shortlist


📝 Commits (8)

  • 76a9cfc feat(memory): Decision recall shortlists by meaning, with time sized to its model
  • a81b5cb docs(memory): Decision recall shortlist, time limits and continuations
  • fedc429 Merge branch 'staging' into feat/memory-recall-shortlist
  • c47698a fix(memory): Decision recall keeps word order until vectors cover the archive
  • 2f9ba69 feat(memory): recall reads every user message since the responder spoke
  • 5205d0b fix(memory): a user message with no text is no recall query
  • bb2713e fix(memory): continuations and rejected long turns keep span recall
  • 80eb565 fix(memory): retry rejected embeddings after a cooldown; image-only last message keeps the word cues

📊 Changes

15 files changed (+1451 additions, -124 deletions)

View changed files

📝 docs/agents/memory.md (+6 -4)
📝 packages/client/src/components/chat/AdvancedMemoryInspector.tsx (+1 -0)
📝 packages/client/src/localization/locales/en.json (+1 -0)
📝 packages/server/src/routes/generate.routes.ts (+1 -0)
📝 packages/server/src/services/advanced-memory-decisions.ts (+139 -30)
📝 packages/server/src/services/advanced-memory-ranking.ts (+86 -2)
📝 packages/server/src/services/advanced-memory.ts (+301 -75)
📝 packages/server/src/services/decision/decision-default.ts (+21 -0)
📝 packages/server/src/services/generation/advanced-memory-context.ts (+7 -1)
📝 scripts/regressions/advanced-memory-context.regression.ts (+16 -2)
📝 scripts/regressions/advanced-memory-decisions.regression.ts (+25 -8)
📝 scripts/regressions/advanced-memory-post-generation.regression.ts (+8 -0)
➕ scripts/regressions/advanced-memory-recall-fusion.regression.ts (+833 -0)
📝 scripts/regressions/advanced-memory-recall-shortlist.regression.ts (+2 -1)
📝 scripts/regressions/advanced-memory-recollections.regression.ts (+4 -1)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7699

Why this change

With the Decision model on, Advanced Memory recall sends only a 24-scene shortlist to the model. Decision mode had embeddings off, so that shortlist came from word matching alone. In long chats the right scene often never reached the model:

  • questions about a character who appears in many scenes;
  • paraphrases;
  • "what happened after X".

The fixed 10 s per pass was also too short for slower Decision backends, so recall fell back. Mari agreed to the plan on 2026-10-15. She asked that waits not get too long and that nothing lag the frontend.

What changed

  • The shortlist goes by meaning too.
    • Decision mode embeds summaries and excerpts again. This reverses the old "Decision mode needs no embeddings" rule, at Mari's request.
    • The shortlist fuses the word-match ranking and the meaning ranking.
    • The meaning ranking compares each user message since the replying character last spoke, up to six, each embedded alone; a scene scores its best match. In a group chat this covers what you said to another character before this one's turn (Mari's request). A first reply uses the latest user message. With the old 4-message window, the local embedder cut off the newest message, and every scene looked similar even to an unrelated question.
    • Recall with the Decision model off keeps its scoring rules. It reads the same messages.
  • Cue words and the Decision model read the same span. Word matching scores each of those user messages on its own. Word overlap and the Decision model's conversation cover at least the last 4 messages, and back to the span's start within 6000 characters.
  • Existing chats fill in missing vectors gradually, in the background after each reply: a small bounded batch, never before the reply. No manual Reindex is needed.
  • Time sized to the Decision backend. Each pass's limit scales with how many items it judges and that backend's speed, with a floor and a cap. When the work can't fit, the pass judges fewer items, best first, instead of timing out.
  • Small fixes:
    • Each scene the Decision model judges carries one short line with its date and participants. It is reader-safe.
    • The scene right after a strong match joins the shortlist, for "after X" questions. It goes through the same audience check.
    • A failed batch keeps the answers from batches that succeeded (new receipt reason).
    • A candidate too big for one request is trimmed instead of failing the pass.
    • Chinese and Japanese text is split into character pairs for word matching.
    • The docs now say continuations rerun recall, which is what the code does.
  • Preparation revision bumped, so a swipe never reuses a snapshot chosen the old way.

Measured offline with the real ranking code and the local MiniLM embedder loaded from disk. The corpus is synthetic and themed, with planted target events. No model calls and no user data. The table is the share of trials where the target reaches the Decision shortlist (24 trials per cell, 2 seeds):

Question type 60 scenes 200 scenes 800 scenes
"What happened after X" 29% → 100% 4% → 100% 0% → 100%
Character in 30% of scenes 84% → 96% 42% → 75% 25% → 62%
Paraphrase + shared place 100% → 100% 79% → 96% 54% → 84%
Names, details, details only in messages 100% → 100% 100% → 100% 100% → 100%
  • Decision model off: the top 3 is identical before and after in every row.
  • No added lag: median prepare() went from 321 to 294 ms at 200 scenes and from 4,862 to 4,659 ms at 800. Reading the records now uses a projected select: 156 → 37 ms at 800 scenes.

Group replies (follow-up 2f9ba69ab, Mari's request). Same harness and corpus. The responder spoke, then the user and another character talked: user, other, user, other, user. The new question types put the cue only in the first user message (the latest is small talk), or only in the other character's line. Share of 24 trials where the target reaches the shortlist, latest message only → shipped:

Question type Earlier user lines 60 scenes 200 scenes 800 scenes
Cue in an earlier user message short small talk 29% → 100% 12% → 58% 0% → 25%
Cue in an earlier user message long story turns 33% → 100% 8% → 67% 0% → 29%
Cue only in the other character's line short / long 29% / 42% → 38% / 42% 4% / 4% → 4% / 4% 8% / 4% → 8% / 4%
  • The six earlier question types stay within one trial of before in both settings, and so does the Decision-off top 3.
  • Embedding those messages as is, without the penalty below, dropped a detail question to 0–8% with long story turns.
  • Adding the other character's lines as queries lost existing rows, so they are not queries.
  • Six queries of 6000 characters take about 0.18 s on the local embedder, in one request.

Time limits:

  • Each pass gets 1.5 × its expected request time, at least 10 s and at most 25 s. Both passes together get at most 40 s.
  • Expected request time:
    • Managed Open-Jev: statements × its measured ms per statement. 2B gets 12.6 s for 24.
    • Chat-model connections: statements × 1,000 ms, or less if the connection's own limit is shorter.
    • System One: its per-statement limit per request, capped at 4 s.
  • When the work can't fit, the pass judges fewer, best first. For example, 9B judges 20 of 24, and chat-model connections at most 16.
  • During the wait, the reply bubble shows the existing "Thinking…" placeholder.

Design calls made while measuring:

  • Rank-fusion constant 3, not 60. With 60, mid-ranked scenes crowded out strong single matches.
  • Meaning queries are the user's messages since the responder spoke, each alone. Joined into one window, every scene crossed the 0.45 floor even for an unrelated question. A long story turn among them is close to every scene, so an earlier message first loses 1.5 times its mean similarity above the latest's. With 1 times, long turns still pushed paraphrases out (800 scenes: 83% → 62%).
  • "After X" neighbours replace the weakest shortlist places instead of extending the list, so the existing batch pins hold.
  • An oversized scene isn't trimmed. It falls back to ordinary recall on its own, as an existing pin requires, and the other scenes keep their answers.
  • The reindex pin ("Decision reindex needs no model calls") is reversed too. It belongs to the same decision.
  • The after-reply fill-in only covers valid records with no vector, by Reindex's own check. Vectors from another embedding model are left to Reindex, which keeps the old vector until the new one is written.

Known, not changed here:

  • At 800 scenes prepare() itself takes about 4.7 s, mostly recordValid rebuilding message maps. That predates this PR and needs its own issue.
  • A prompt-overflow retry reruns both passes, now up to 40 s each (overflow is rare).

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Manually verify a long Roleplay chat with Advanced Memory and the Decision model on. Ask about an earlier scene in your own words (no shared names) and check Decision diagnostics shows it judged.
  • Manually verify that an existing Decision-mode chat gets vectors over a few replies with no visible slowdown.
  • Manually verify the wait before a reply on a slow local Decision model (9B): longer than before, but bounded.
  • Manually verify that Individual group mode still recalls only scenes the responding character was in.
  • Manually verify in an Individual group chat: ask Pantalone about an earlier scene in your own words, add a line or two of small talk, then let Maukie reply. Decision diagnostics should show that scene judged.

Agent-run validation:

  • tsc for shared, server, client and root; eslint; prettier; check-locales; localization:check
  • 44 regressions: all advanced-memory*, memory-* and decision*, plus agent-activation*, spatial-context and open-issues
  • The new fusion, neighbour and partial-batch cases fail on staging code.
  • Playwright: 21/22. The one failure, the memory-wheel sprite check (.png vs .webp), fails on staging too.
  • Follow-up 2f9ba69ab and 5205d0b27: tsc for shared, server, client and root, server lint and prettier pass, and so do the 40 regression files matching advanced-memory, memory-, decision, agent-activation, spatial-context and open-issues. The new group-span, cap, upkeep and Reindex cases fail on c47698a71, and the image-only case fails on 2f9ba69ab. Local CodeRabbit on 2f9ba69ab: one finding, the docs-i18n follow-up, now #7708. Not rerun for the follow-up: Playwright and the localization checks (no client change).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

Translated docs follow-up: #7708 ([docs-i18n] docs/agents/memory.md).

UI evidence (if applicable)

No UI change apart from one receipt reason in the memory inspector.

Summary by CodeRabbit

  • Improved Memory Recall
    • Memory search combines word- and meaning-based matching, with better support for Chinese and Japanese text.
    • Recall can include nearby scenes and date, message-range, and speaker context. It considers separate recent messages and distinctive terms from messages since the replying character last spoke.
  • More Reliable Responses
    • Decision-based memory selection has bounded time limits. Ordinary recall can provide results for memories a selection could not evaluate; broader fallback applies when a pass has no answers or its connection is unavailable.
  • Memory Indexing
    • Memories are indexed during Decision-mode preparation, with older records indexed incrementally. Reindexing rebuilds memory vectors.

🔄 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-Engine/pull/7701 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/memory-recall-shortlist` --- ### 📝 Commits (8) - [`76a9cfc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/76a9cfc36298bf48322f0a5ce80fe4b4c287234e) feat(memory): Decision recall shortlists by meaning, with time sized to its model - [`a81b5cb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a81b5cb7c7e1ff63b258d451ab9d43ab7f99d153) docs(memory): Decision recall shortlist, time limits and continuations - [`fedc429`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fedc4290aa61acd517f11823375da9816aef6734) Merge branch 'staging' into feat/memory-recall-shortlist - [`c47698a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c47698a719e0e0108c73c40db6ee85f20a2e0684) fix(memory): Decision recall keeps word order until vectors cover the archive - [`2f9ba69`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2f9ba69ab000fe279360d5c95c7fa64485f3c86c) feat(memory): recall reads every user message since the responder spoke - [`5205d0b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5205d0b2752808bb51982f515bbe219b67d2a27b) fix(memory): a user message with no text is no recall query - [`bb2713e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/bb2713e7f4b96383f14982bf5106e4ec951369d4) fix(memory): continuations and rejected long turns keep span recall - [`80eb565`](https://github.com/Pasta-Devs/Marinara-Engine/commit/80eb5656fc737c0897b6b0633f4ccde43f7e9894) fix(memory): retry rejected embeddings after a cooldown; image-only last message keeps the word cues ### 📊 Changes **15 files changed** (+1451 additions, -124 deletions) <details> <summary>View changed files</summary> 📝 `docs/agents/memory.md` (+6 -4) 📝 `packages/client/src/components/chat/AdvancedMemoryInspector.tsx` (+1 -0) 📝 `packages/client/src/localization/locales/en.json` (+1 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+1 -0) 📝 `packages/server/src/services/advanced-memory-decisions.ts` (+139 -30) 📝 `packages/server/src/services/advanced-memory-ranking.ts` (+86 -2) 📝 `packages/server/src/services/advanced-memory.ts` (+301 -75) 📝 `packages/server/src/services/decision/decision-default.ts` (+21 -0) 📝 `packages/server/src/services/generation/advanced-memory-context.ts` (+7 -1) 📝 `scripts/regressions/advanced-memory-context.regression.ts` (+16 -2) 📝 `scripts/regressions/advanced-memory-decisions.regression.ts` (+25 -8) 📝 `scripts/regressions/advanced-memory-post-generation.regression.ts` (+8 -0) ➕ `scripts/regressions/advanced-memory-recall-fusion.regression.ts` (+833 -0) 📝 `scripts/regressions/advanced-memory-recall-shortlist.regression.ts` (+2 -1) 📝 `scripts/regressions/advanced-memory-recollections.regression.ts` (+4 -1) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #7699 ## Why this change With the Decision model on, Advanced Memory recall sends only a 24-scene shortlist to the model. Decision mode had embeddings off, so that shortlist came from word matching alone. In long chats the right scene often never reached the model: - questions about a character who appears in many scenes; - paraphrases; - "what happened after X". The fixed 10 s per pass was also too short for slower Decision backends, so recall fell back. Mari agreed to the plan on 2026-10-15. She asked that waits not get too long and that nothing lag the frontend. ## What changed - **The shortlist goes by meaning too.** - Decision mode embeds summaries and excerpts again. This reverses the old "Decision mode needs no embeddings" rule, at Mari's request. - The shortlist fuses the word-match ranking and the meaning ranking. - The meaning ranking compares **each user message since the replying character last spoke**, up to six, each embedded alone; a scene scores its best match. In a group chat this covers what you said to another character before this one's turn (Mari's request). A first reply uses the latest user message. With the old 4-message window, the local embedder cut off the newest message, and every scene looked similar even to an unrelated question. - Recall with the Decision model off keeps its scoring rules. It reads the same messages. - **Cue words and the Decision model read the same span.** Word matching scores each of those user messages on its own. Word overlap and the Decision model's conversation cover at least the last 4 messages, and back to the span's start within 6000 characters. - **Existing chats fill in missing vectors gradually**, in the background after each reply: a small bounded batch, never before the reply. No manual Reindex is needed. - **Time sized to the Decision backend.** Each pass's limit scales with how many items it judges and that backend's speed, with a floor and a cap. When the work can't fit, the pass judges fewer items, best first, instead of timing out. - **Small fixes:** - Each scene the Decision model judges carries one short line with its date and participants. It is reader-safe. - The scene right after a strong match joins the shortlist, for "after X" questions. It goes through the same audience check. - A failed batch keeps the answers from batches that succeeded (new receipt reason). - A candidate too big for one request is trimmed instead of failing the pass. - Chinese and Japanese text is split into character pairs for word matching. - The docs now say continuations rerun recall, which is what the code does. - **Preparation revision bumped**, so a swipe never reuses a snapshot chosen the old way. **Measured** offline with the real ranking code and the local MiniLM embedder loaded from disk. The corpus is synthetic and themed, with planted target events. No model calls and no user data. The table is the share of trials where the target reaches the Decision shortlist (24 trials per cell, 2 seeds): | Question type | 60 scenes | 200 scenes | 800 scenes | |---|---|---|---| | "What happened after X" | 29% → 100% | 4% → 100% | 0% → 100% | | Character in 30% of scenes | 84% → 96% | 42% → 75% | 25% → 62% | | Paraphrase + shared place | 100% → 100% | 79% → 96% | 54% → 84% | | Names, details, details only in messages | 100% → 100% | 100% → 100% | 100% → 100% | - **Decision model off:** the top 3 is identical before and after in every row. - **No added lag:** median `prepare()` went from 321 to 294 ms at 200 scenes and from 4,862 to 4,659 ms at 800. Reading the records now uses a projected select: 156 → 37 ms at 800 scenes. **Group replies (follow-up 2f9ba69ab, Mari's request).** Same harness and corpus. The responder spoke, then the user and another character talked: user, other, user, other, user. The new question types put the cue only in the first user message (the latest is small talk), or only in the other character's line. Share of 24 trials where the target reaches the shortlist, latest message only → shipped: | Question type | Earlier user lines | 60 scenes | 200 scenes | 800 scenes | |---|---|---|---|---| | Cue in an earlier user message | short small talk | 29% → 100% | 12% → 58% | 0% → 25% | | Cue in an earlier user message | long story turns | 33% → 100% | 8% → 67% | 0% → 29% | | Cue only in the other character's line | short / long | 29% / 42% → 38% / 42% | 4% / 4% → 4% / 4% | 8% / 4% → 8% / 4% | - The six earlier question types stay within one trial of before in both settings, and so does the Decision-off top 3. - Embedding those messages as is, without the penalty below, dropped a detail question to 0–8% with long story turns. - Adding the other character's lines as queries lost existing rows, so they are not queries. - Six queries of 6000 characters take about 0.18 s on the local embedder, in one request. **Time limits:** - Each pass gets 1.5 × its expected request time, at least 10 s and at most 25 s. Both passes together get at most 40 s. - Expected request time: - **Managed Open-Jev:** statements × its measured ms per statement. 2B gets 12.6 s for 24. - **Chat-model connections:** statements × 1,000 ms, or less if the connection's own limit is shorter. - **System One:** its per-statement limit per request, capped at 4 s. - When the work can't fit, the pass judges fewer, best first. For example, 9B judges 20 of 24, and chat-model connections at most 16. - During the wait, the reply bubble shows the existing "Thinking…" placeholder. **Design calls made while measuring:** - **Rank-fusion constant 3, not 60.** With 60, mid-ranked scenes crowded out strong single matches. - **Meaning queries are the user's messages since the responder spoke, each alone.** Joined into one window, every scene crossed the 0.45 floor even for an unrelated question. A long story turn among them is close to every scene, so an earlier message first loses 1.5 times its mean similarity above the latest's. With 1 times, long turns still pushed paraphrases out (800 scenes: 83% → 62%). - **"After X" neighbours replace the weakest shortlist places** instead of extending the list, so the existing batch pins hold. - **An oversized scene isn't trimmed.** It falls back to ordinary recall on its own, as an existing pin requires, and the other scenes keep their answers. - **The reindex pin ("Decision reindex needs no model calls") is reversed too.** It belongs to the same decision. - **The after-reply fill-in only covers valid records with no vector**, by Reindex's own check. Vectors from another embedding model are left to Reindex, which keeps the old vector until the new one is written. **Known, not changed here:** - At 800 scenes `prepare()` itself takes about 4.7 s, mostly `recordValid` rebuilding message maps. That predates this PR and needs its own issue. - A prompt-overflow retry reruns both passes, now up to 40 s each (overflow is rare). ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Manually verify a long Roleplay chat with Advanced Memory and the Decision model on. Ask about an earlier scene in your own words (no shared names) and check Decision diagnostics shows it judged. - Manually verify that an existing Decision-mode chat gets vectors over a few replies with no visible slowdown. - Manually verify the wait before a reply on a slow local Decision model (9B): longer than before, but bounded. - Manually verify that Individual group mode still recalls only scenes the responding character was in. - Manually verify in an Individual group chat: ask Pantalone about an earlier scene in your own words, add a line or two of small talk, then let Maukie reply. Decision diagnostics should show that scene judged. Agent-run validation: - tsc for shared, server, client and root; eslint; prettier; `check-locales`; `localization:check` - 44 regressions: all `advanced-memory*`, `memory-*` and `decision*`, plus `agent-activation*`, `spatial-context` and `open-issues` - The new fusion, neighbour and partial-batch cases fail on staging code. - Playwright: 21/22. The one failure, the memory-wheel sprite check (`.png` vs `.webp`), fails on staging too. - Follow-up 2f9ba69ab and 5205d0b27: tsc for shared, server, client and root, server lint and prettier pass, and so do the 40 regression files matching `advanced-memory`, `memory-`, `decision`, `agent-activation`, `spatial-context` and `open-issues`. The new group-span, cap, upkeep and Reindex cases fail on c47698a71, and the image-only case fails on 2f9ba69ab. Local CodeRabbit on 2f9ba69ab: one finding, the docs-i18n follow-up, now #7708. Not rerun for the follow-up: Playwright and the localization checks (no client change). ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) Translated docs follow-up: #7708 (`[docs-i18n] docs/agents/memory.md`). ## UI evidence (if applicable) No UI change apart from one receipt reason in the memory inspector. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improved Memory Recall** * Memory search combines word- and meaning-based matching, with better support for Chinese and Japanese text. * Recall can include nearby scenes and date, message-range, and speaker context. It considers separate recent messages and distinctive terms from messages since the replying character last spoke. * **More Reliable Responses** * Decision-based memory selection has bounded time limits. Ordinary recall can provide results for memories a selection could not evaluate; broader fallback applies when a pass has no answers or its connection is unavailable. * **Memory Indexing** * Memories are indexed during Decision-mode preparation, with older records indexed incrementally. Reindexing rebuilds memory vectors. <!-- 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-24 04:08:30 +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-Engine#7888
No description provided.