[GH-ISSUE #1433] [Issue]: Long-Term Memory: a new branch doesn't recall memories from the history it shares with the original chat #729

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

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

Originally assigned to: @Promansis on GitHub.

Summary

Engine describes a branch as "a copy of a chat that shares history up to a chosen point" (docs/chats/branches.md). When it creates a branch, Engine copies everything tied to the copied messages. That includes every rolling summary whose messages all come before the branch point. Summaries that cross the branch point are left behind.

Long-Term Memory doesn't follow this. A memory is saved to the single chat record (branch) it came from, and recall only matches that record. A new branch therefore recalls none of the memories made from its shared history, even though Engine copied the summaries those memories came from.

This issue makes a branch also recall memories that were built entirely from summaries it shares with the original. It changes recall only. Stored availability, saving and editing stay as they are, so memories that users have already assigned to a branch keep working without any change.

It should land before #1420. #1420 removes the per-branch tab from the place pickers, and the compatibility rules below apply to it too.

Affected package

long-term-memory 1.4.43 (upstream/staging dce3590bb).

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

  • Saving: S/chat-scope.ts:68 resolveChatLtmWriteScope saves to chatOnlyLtmScope(chat.id), which is one branch.
  • Recall scope: S/generation-injection.ts:134 builds the recall scope from the current chat (resolveChatLtmScope), which gives its own chat id and its conversation id.
  • Recall filter: S/retrieval.ts:103-121 filters chunks with matchesLtmScope. A memory saved to branch A (chatIds: [A], no group id) never matches branch B.
  • Where memories come from:
    • Chat-summary sources record { kind: "chat_summary", sourceId: <chat id>, entryId: <summary entry id> } (S/interop.ts:520-530).
    • Every source that creates or updates a memory adds an extracted_from link to it (S/draft-projector.ts:463).
  • Shared summary ids: S/interop.ts:332 already relies on branches sharing summary entry ids, to remove duplicates in the Sources preview.
  • Engine side (2039583ca, read only):
    • packages/server/src/routes/chats.routes.ts:4845 keeps only the summary entries whose messages were all copied.
    • :4970 copies those entries with the same id.

Expected behavior

In branch B, recall includes a memory saved to another branch of the same chat when every chat summary that memory came from is also one of B's summaries. This is the same test Engine uses for summaries.

  • Branch of a branch: works, because copied summaries keep their ids.
  • After the branch point: memories made from, or later updated by, a summary written after the branch point stay in their own branch.
  • No chat-summary source (lorebook or character imports, memories typed in): there is no branch point to check, so these stay in their own branch. Engine does the same with summaries that have no message range.
  • Different conversations: a memory never crosses into another conversation.

Actual behavior

Branch B recalls nothing that was saved to the chat it came from, unless that memory was set to the whole chat ("All branches").

Steps to reproduce

This comes from reading the code; it hasn't been run live.

  1. In a Roleplay chat with Long-Term Memory on, write until a rolling summary exists. Import that summary in Sources with the default destination (the current chat).
  2. Branch the chat from a message after the summary's range. Engine copies the summary into the branch.
  3. In the branch, send a message that should recall the memory. The memory isn't recalled. The same message in the original chat recalls it.

Minimal fix

  1. New helper. Add a pure helper next to the scope helpers in S/chat-scope.ts. It takes:

    • the current chat's summary entry ids, read with the same reader Sources uses (export summaries from S/interop.ts:194);
    • the ids of the other branches in the same conversation (same groupId);
    • the notes.

    It returns the ids of memories that meet all of these:

    • the memory is saved to another branch of this conversation;
    • it has at least one extracted_from link;
    • every extracted_from link points to a chat-summary source note from this conversation, and that note's entryId is one of the current chat's summary entry ids.

    A link to a missing source note fails the test.

  2. Retrieval. S/retrieval.ts gets an optional inheritedNoteIds?: ReadonlySet<string> input. The chunk filter becomes matchesLtmScope(...) || inheritedNoteIds?.has(chunk.noteId). The status, mode, resolved and exclusiveCharacterIds filters still apply as before.

  3. Recall entry. S/generation-injection.ts builds the set only when the chat has a groupId and at least one summary entry, then passes it to retrieveLongTermMemory.

    • Other branches come from getPackagePersistence().listChats(), filtered by groupId.
    • Notes come from storage.listNotes(), which uses the cached vault snapshot.
    • Unbranched chats do no extra work.

No Engine change is needed.

Compatibility: memories already assigned to branches

Many memories are saved to one branch (chatIds: [<branch chat id>]). That is the default for everything imported from a chat, and users have also assigned some by hand with the availability editor's Branch tab. None of them may break.

This issue

  • No stored-data change: notes, scopes, the recall index, backups and the schema stay the same, and there is no migration.
  • Saving and editing don't change:
    • These keep using the stored scope: resolveChatLtmWriteScope, Sources destinations, draft apply, extraction update targets, transfers and the Vault filter.
    • Only generation-injection passes inheritedNoteIds, and every other retrieveLongTermMemory caller is unchanged. Inherited memories are therefore read-only in the branch: extraction never picks them as update targets, so a branch can't change a memory that belongs to another branch.
  • Unchanged scope matching: matchesLtmScope and ltmScopesOverlap in the shared scope.ts stay as they are.
  • Recall only gains memories: a memory is never dropped from the branch it is assigned to.
  • Older setups: Engine versions that don't copy summaries into branches, and older branches without lineage, share no summary ids, so they recall exactly what they do today.

#1420 (removing the per-branch tab) must keep these working

  • Existing branch assignments:
    • The single and bulk availability editors still show an existing branch assignment as a removable chip that names the chat and the branch.
    • Saving any other change leaves that assignment as it is: it isn't widened to the whole chat and it isn't dropped.
  • "Current" in Sources still means this branch (chatOnlyLtmScope), so new imports keep today's default.
  • Whole-chat choices:
    • Choosing a whole chat sets the conversation and all its branches, { groupId, chatIds: group.chatIds }, which is what Sources' "All branches" does today.
    • The Vault filter for a whole chat includes memories assigned to any of its branches.
  • Deleted chats: an assignment to a branch whose chat no longer exists still shows, with the fallback label.

Regression proof

Add tests/long-term-memory-branch-recall.regression.ts and add it to the "Run Long-Term Memory regressions" list in .github/workflows/ltm-browser.yml.

Fixtures:

  • chat A (group G) with summary entries E1 and E2;
  • branch B (group G) with a copy of E1 and its own E3;
  • chat C in another conversation.
Memory Saved to Sources Recalled in
m1 A E1 A, B
m2 A E1, then updated by E2 A only
m3 A lorebook import A only
m4 B E3 B only
m5 whole chat (G) E1 A, B
m6 B, by hand from the Branch tab E3 B only
m7 C a summary with the same entry id as E1 C only

Also check:

  • note files and recall.json are byte-identical after recall;
  • an unbranched chat builds no inherited set.

Run the existing recall regressions as well: runtime, reconciliation-recall, index-keys, routes and routes-scope-identity.

Environment

  • Marinara Engine version: 2.5.0 line (staging 2039583ca)
  • Package version: long-term-memory 1.4.43 (upstream/staging dce3590bb)
  • Install type: source (code audit)
  • OS + version: not platform-specific
  • Browser or app shell: any
Originally created by @Promansis on GitHub (Oct 17, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1433 Originally assigned to: @Promansis on GitHub. ## Summary Engine describes a branch as "a copy of a chat that shares history up to a chosen point" (`docs/chats/branches.md`). When it creates a branch, Engine copies everything tied to the copied messages. That includes every rolling summary whose messages all come before the branch point. Summaries that cross the branch point are left behind. Long-Term Memory doesn't follow this. A memory is saved to the single chat record (branch) it came from, and recall only matches that record. A new branch therefore recalls none of the memories made from its shared history, even though Engine copied the summaries those memories came from. This issue makes a branch also recall memories that were built entirely from summaries it shares with the original. It changes recall only. Stored availability, saving and editing stay as they are, so memories that users have already assigned to a branch keep working without any change. It should land before #1420. #1420 removes the per-branch tab from the place pickers, and the compatibility rules below apply to it too. ## Affected package `long-term-memory` 1.4.43 (`upstream/staging` dce3590bb). `S` = `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory` - **Saving**: `S/chat-scope.ts:68` `resolveChatLtmWriteScope` saves to `chatOnlyLtmScope(chat.id)`, which is one branch. - **Recall scope**: `S/generation-injection.ts:134` builds the recall scope from the current chat (`resolveChatLtmScope`), which gives its own chat id and its conversation id. - **Recall filter**: `S/retrieval.ts:103-121` filters chunks with `matchesLtmScope`. A memory saved to branch A (`chatIds: [A]`, no group id) never matches branch B. - **Where memories come from**: - Chat-summary sources record `{ kind: "chat_summary", sourceId: <chat id>, entryId: <summary entry id> }` (`S/interop.ts:520-530`). - Every source that creates or updates a memory adds an `extracted_from` link to it (`S/draft-projector.ts:463`). - **Shared summary ids**: `S/interop.ts:332` already relies on branches sharing summary entry ids, to remove duplicates in the Sources preview. - **Engine side** (`2039583ca`, read only): - `packages/server/src/routes/chats.routes.ts:4845` keeps only the summary entries whose messages were all copied. - `:4970` copies those entries with the same `id`. ## Expected behavior In branch B, recall includes a memory saved to another branch of the same chat when every chat summary that memory came from is also one of B's summaries. This is the same test Engine uses for summaries. - **Branch of a branch**: works, because copied summaries keep their ids. - **After the branch point**: memories made from, or later updated by, a summary written after the branch point stay in their own branch. - **No chat-summary source** (lorebook or character imports, memories typed in): there is no branch point to check, so these stay in their own branch. Engine does the same with summaries that have no message range. - **Different conversations**: a memory never crosses into another conversation. ## Actual behavior Branch B recalls nothing that was saved to the chat it came from, unless that memory was set to the whole chat ("All branches"). ## Steps to reproduce This comes from reading the code; it hasn't been run live. 1. In a Roleplay chat with Long-Term Memory on, write until a rolling summary exists. Import that summary in Sources with the default destination (the current chat). 2. Branch the chat from a message after the summary's range. Engine copies the summary into the branch. 3. In the branch, send a message that should recall the memory. The memory isn't recalled. The same message in the original chat recalls it. ## Minimal fix 1. **New helper.** Add a pure helper next to the scope helpers in `S/chat-scope.ts`. It takes: - the current chat's summary entry ids, read with the same reader Sources uses (export `summaries` from `S/interop.ts:194`); - the ids of the other branches in the same conversation (same `groupId`); - the notes. It returns the ids of memories that meet all of these: - the memory is saved to another branch of this conversation; - it has at least one `extracted_from` link; - every `extracted_from` link points to a chat-summary source note from this conversation, and that note's `entryId` is one of the current chat's summary entry ids. A link to a missing source note fails the test. 2. **Retrieval.** `S/retrieval.ts` gets an optional `inheritedNoteIds?: ReadonlySet<string>` input. The chunk filter becomes `matchesLtmScope(...) || inheritedNoteIds?.has(chunk.noteId)`. The status, mode, resolved and `exclusiveCharacterIds` filters still apply as before. 3. **Recall entry.** `S/generation-injection.ts` builds the set only when the chat has a `groupId` and at least one summary entry, then passes it to `retrieveLongTermMemory`. - Other branches come from `getPackagePersistence().listChats()`, filtered by `groupId`. - Notes come from `storage.listNotes()`, which uses the cached vault snapshot. - Unbranched chats do no extra work. No Engine change is needed. ## Compatibility: memories already assigned to branches Many memories are saved to one branch (`chatIds: [<branch chat id>]`). That is the default for everything imported from a chat, and users have also assigned some by hand with the availability editor's Branch tab. None of them may break. **This issue** - **No stored-data change**: notes, scopes, the recall index, backups and the schema stay the same, and there is no migration. - **Saving and editing don't change**: - These keep using the stored scope: `resolveChatLtmWriteScope`, Sources destinations, draft apply, extraction update targets, transfers and the Vault filter. - Only `generation-injection` passes `inheritedNoteIds`, and every other `retrieveLongTermMemory` caller is unchanged. Inherited memories are therefore read-only in the branch: extraction never picks them as update targets, so a branch can't change a memory that belongs to another branch. - **Unchanged scope matching**: `matchesLtmScope` and `ltmScopesOverlap` in the shared `scope.ts` stay as they are. - **Recall only gains memories**: a memory is never dropped from the branch it is assigned to. - **Older setups**: Engine versions that don't copy summaries into branches, and older branches without lineage, share no summary ids, so they recall exactly what they do today. **#1420 (removing the per-branch tab) must keep these working** - **Existing branch assignments**: - The single and bulk availability editors still show an existing branch assignment as a removable chip that names the chat and the branch. - Saving any other change leaves that assignment as it is: it isn't widened to the whole chat and it isn't dropped. - **"Current" in Sources** still means this branch (`chatOnlyLtmScope`), so new imports keep today's default. - **Whole-chat choices**: - Choosing a whole chat sets the conversation and all its branches, `{ groupId, chatIds: group.chatIds }`, which is what Sources' "All branches" does today. - The Vault filter for a whole chat includes memories assigned to any of its branches. - **Deleted chats**: an assignment to a branch whose chat no longer exists still shows, with the fallback label. ## Regression proof Add `tests/long-term-memory-branch-recall.regression.ts` and add it to the "Run Long-Term Memory regressions" list in `.github/workflows/ltm-browser.yml`. Fixtures: - chat A (group G) with summary entries E1 and E2; - branch B (group G) with a copy of E1 and its own E3; - chat C in another conversation. | Memory | Saved to | Sources | Recalled in | |---|---|---|---| | m1 | A | E1 | A, B | | m2 | A | E1, then updated by E2 | A only | | m3 | A | lorebook import | A only | | m4 | B | E3 | B only | | m5 | whole chat (G) | E1 | A, B | | m6 | B, by hand from the Branch tab | E3 | B only | | m7 | C | a summary with the same entry id as E1 | C only | Also check: - note files and `recall.json` are byte-identical after recall; - an unbranched chat builds no inherited set. Run the existing recall regressions as well: `runtime`, `reconciliation-recall`, `index-keys`, `routes` and `routes-scope-identity`. ## Environment - Marinara Engine version: 2.5.0 line (`staging` 2039583ca) - Package version: long-term-memory 1.4.43 (`upstream/staging` dce3590bb) - Install type: source (code audit) - OS + version: not platform-specific - Browser or app shell: any
bsakaguchi 2026-10-20 17:30:08 +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#729
No description provided.