[PR #7463] [MERGED] fix(memory): keep merged scene constants compactable when a reader leaves or the text uses {{user}} #7745

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

📋 Pull Request Information

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

Base: staging ← Head: fix/advanced-memory-merged-constant-followups


📝 Commits (1)

  • 76fc280 fix(memory): keep merged scene constants compactable when a reader leaves or the text uses {{user}}

📊 Changes

4 files changed (+199 additions, -39 deletions)

View changed files

📝 docs/agents/memory.md (+2 -2)
📝 packages/server/src/services/advanced-memory.ts (+51 -34)
📝 packages/shared/src/utils/macro-engine.ts (+25 -0)
📝 scripts/regressions/advanced-memory-post-generation.regression.ts (+121 -3)

📄 Description

Linked issue

Follow-up to #7458 (#7454)

Why this change

#7458 gives a scene one Advanced Memory constant for every reader of its recap, instead of a copy per reader. A reviewer reproduced two cases where that merged constant then never gets shortened, so a character's share of Summary and recall budget stays over the limit after every reply:

  • A reader leaves the chat. Individual group chat with narrator Dottore plus Maukie and Pantalone. Message 2 is hidden from Pantalone and message 3 from Maukie. After Pantalone leaves, Maukie's share is about 560 tokens against a 140-token budget, but the Helper is called 0 times. To decide whether a summary can be combined, compaction scoped it to the readers still in the chat. That dropped Pantalone's part, so the whole summary was skipped. Before #7458, only Pantalone's own copy froze and Maukie's was still shortened.
  • The shared part uses {{user}}. The merged constant now has private parts, and the privacy check treated text containing any macro as unreadable for its purposes, so such a summary was skipped (0 Helper calls). {{user}} reads the same for every character, so it doesn't need that protection.

It also tidies the order in which a scene's new constants are saved. The constant that needs no Helper call was saved after any paid re-summary of a partly covered copy. If that call failed, the narrator's and the other readers' constant was held back until the next run.

What changed

  • packages/shared/src/utils/macro-engine.ts: new characterConditionNames(text) lists the names a summary's character-only conditions compare against, the same way scopeCharacterSummary reads them.
  • packages/server/src/services/advanced-memory.ts, compaction:
    • A summary with a part for a character who has left the chat stays combinable while someone besides the narrator still reads it. That part keeps its own condition through combining: the input to the Helper, the scoped result and the privacy check's sections all include the names the summary's own conditions mention. The existing check still rejects any result that adds, drops or widens a section, and still catches private text copied into a wider section, including the departed character's. A summary only the narrator still reads, or one whose departed part has no condition naming them (an {{else}}), stays as it is, as before.
    • The privacy check keys text by its readers when its only macro is {{user}}. Any other macro ({{char}}, character fields, variables) still blocks combining a summary with private parts, as before.
    • Updated the ponytail: comment to match.
  • packages/server/src/services/advanced-memory.ts, new constants: the scene's constant that needs no Helper call is saved first, before re-summarizing partly covered copies.
  • docs/agents/memory.md: "Everyone who read a scene's recap shares one summary of it" now applies only to group chats where characters reply one at a time. The compaction paragraph now covers a departed character's part and {{user}}.

Known older limitation, out of scope: in a merged group chat, a scene with a message hidden from one character gets no automatic constant at all, because archivedIds uses the all-characters view there. This predates #7458, so the docs no longer claim one shared summary for merged chats.

No CHANGELOG entry: both freezes and the save order came from #7458, which hasn't been released. Its [Unreleased] entry already describes the behavior this PR restores.

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

Notes, commands run by the agent:

  • pnpm check: exit 0.
  • node ./scripts/run-regressions.mjs --filter advanced-memory: 16/16 passed.
  • node ./scripts/run-regressions.mjs --filter merged-chat-summary: 1/1 passed.
  • node ./scripts/run-regressions.mjs --filter memory-recall: 5/5 passed.
  • node ./scripts/run-regressions.mjs --filter context-fit: 2/2 passed.
  • New regressions in scripts/regressions/advanced-memory-post-generation.regression.ts. Each one fails when its fix is reverted on its own:
    • Reader leaves: after Pantalone leaves, one combine call shortens Maukie's share. Pantalone's part reaches the Helper and is saved under its own condition. Maukie never reads it, and it's Pantalone's again when he comes back. Fails without the fix: "Maukie's share is shortened after Pantalone leaves".
    • {{user}} in the shared part: one combine call. Maukie and Pantalone each still read only their own private part. Fails without the fix: "a shared part with {{user}} is shortened too".
    • Save order: the existing partly covered scene test now expects Pantalone's whole constant to be saved before the failed paid call. The failed summary is still not added. Its premise changed, so the assertion "a failed new-range summary is not added to Chat Summaries" was adapted rather than removed. Fails without the fix (2 entries instead of 3).
  • All existing assertions are kept, including the privacy ones and "the departed character's summary is not sent" for a summary only the narrator still reads.
  • Local CodeRabbit (coderabbit review --agent --committed --base-commit $(git merge-base HEAD origin/staging)): 2 findings. The review base was the merge base with staging taken before #7458 merged, so it also covered #7458's changes. Neither was fixed here:
    • docs/agents/memory.md needs the docs-i18n mirror or a follow-up issue. Dismissed: the existing follow-up issue #7462 ([docs-i18n] docs/agents/memory.md) covers it, and this PR's edits are noted there.
    • The leak check in advanced-memory.ts compares 4-word runs. A private section shorter than four words has no runs, so the check wouldn't catch it being copied word for word into shared text. Dismissed as out of scope: the gap is in #7458's runs/leaks code, which this PR doesn't change. This PR does send a few more summaries through that check (ones with a departed character's part or with {{user}}). The suggested fix (runs of min(4, words)) would also reject answers for 1- and 2-word sections that share a common word, and that tradeoff is for the maintainer to decide in a separate follow-up.

Manual verification needed

  • In a Roleplay individual group chat with Advanced Memory on, set a narrator plus two characters (A and B). Hide one message in a scene from B and another from A, then start a new scene so the first one is archived. Check that Chat Summaries gets one entry for that scene, with a {{#if char == "B"}} section.
  • Remove B from the chat and lower Summary and recall budget until A's share is over 70%. Send a reply. Check that Updating continuity runs and the new combined entry still has B's section under {{#if char == "B"}}. Check that A's prompt (Prompt Inspector) doesn't contain B's part.
  • Add B back. Check that B's prompt shows B's part and not A's.
  • Repeat with {{user}} in the scene summary's shared text. Check that the over-budget share is shortened, {{user}} still shows your persona name in each character's prompt, and neither character sees the other's private part.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) 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)

Docs: docs/agents/memory.md is updated (the shared-summary sentence and the compaction paragraph). The translation follow-up is noted on the existing #7462 ([docs-i18n] docs/agents/memory.md).


🔄 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/7463 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/12/2026 **Status:** ✅ Merged **Merged:** 10/12/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/advanced-memory-merged-constant-followups` --- ### 📝 Commits (1) - [`76fc280`](https://github.com/Pasta-Devs/Marinara-Engine/commit/76fc280f3fa1aab1e5f65054d87025aff57fe0e9) fix(memory): keep merged scene constants compactable when a reader leaves or the text uses {{user}} ### 📊 Changes **4 files changed** (+199 additions, -39 deletions) <details> <summary>View changed files</summary> 📝 `docs/agents/memory.md` (+2 -2) 📝 `packages/server/src/services/advanced-memory.ts` (+51 -34) 📝 `packages/shared/src/utils/macro-engine.ts` (+25 -0) 📝 `scripts/regressions/advanced-memory-post-generation.regression.ts` (+121 -3) </details> ### 📄 Description ## Linked issue Follow-up to #7458 (#7454) ## Why this change #7458 gives a scene one Advanced Memory constant for every reader of its recap, instead of a copy per reader. A reviewer reproduced two cases where that merged constant then never gets shortened, so a character's share of **Summary and recall budget** stays over the limit after every reply: - **A reader leaves the chat.** Individual group chat with narrator Dottore plus Maukie and Pantalone. Message 2 is hidden from Pantalone and message 3 from Maukie. After Pantalone leaves, Maukie's share is about 560 tokens against a 140-token budget, but the Helper is called 0 times. To decide whether a summary can be combined, compaction scoped it to the readers still in the chat. That dropped Pantalone's part, so the whole summary was skipped. Before #7458, only Pantalone's own copy froze and Maukie's was still shortened. - **The shared part uses `{{user}}`.** The merged constant now has private parts, and the privacy check treated text containing any macro as unreadable for its purposes, so such a summary was skipped (0 Helper calls). `{{user}}` reads the same for every character, so it doesn't need that protection. It also tidies the order in which a scene's new constants are saved. The constant that needs no Helper call was saved after any paid re-summary of a partly covered copy. If that call failed, the narrator's and the other readers' constant was held back until the next run. ## What changed - `packages/shared/src/utils/macro-engine.ts`: new `characterConditionNames(text)` lists the names a summary's character-only conditions compare against, the same way `scopeCharacterSummary` reads them. - `packages/server/src/services/advanced-memory.ts`, compaction: - A summary with a part for a character who has left the chat stays combinable while someone besides the narrator still reads it. That part keeps its own condition through combining: the input to the Helper, the scoped result and the privacy check's sections all include the names the summary's own conditions mention. The existing check still rejects any result that adds, drops or widens a section, and still catches private text copied into a wider section, including the departed character's. A summary only the narrator still reads, or one whose departed part has no condition naming them (an `{{else}}`), stays as it is, as before. - The privacy check keys text by its readers when its only macro is `{{user}}`. Any other macro (`{{char}}`, character fields, variables) still blocks combining a summary with private parts, as before. - Updated the `ponytail:` comment to match. - `packages/server/src/services/advanced-memory.ts`, new constants: the scene's constant that needs no Helper call is saved first, before re-summarizing partly covered copies. - `docs/agents/memory.md`: "Everyone who read a scene's recap shares one summary of it" now applies only to group chats where characters reply one at a time. The compaction paragraph now covers a departed character's part and `{{user}}`. **Known older limitation, out of scope:** in a merged group chat, a scene with a message hidden from one character gets no automatic constant at all, because `archivedIds` uses the all-characters view there. This predates #7458, so the docs no longer claim one shared summary for merged chats. No CHANGELOG entry: both freezes and the save order came from #7458, which hasn't been released. Its `[Unreleased]` entry already describes the behavior this PR restores. ## 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` Notes, commands run by the agent: - `pnpm check`: exit 0. - `node ./scripts/run-regressions.mjs --filter advanced-memory`: 16/16 passed. - `node ./scripts/run-regressions.mjs --filter merged-chat-summary`: 1/1 passed. - `node ./scripts/run-regressions.mjs --filter memory-recall`: 5/5 passed. - `node ./scripts/run-regressions.mjs --filter context-fit`: 2/2 passed. - New regressions in `scripts/regressions/advanced-memory-post-generation.regression.ts`. Each one fails when its fix is reverted on its own: - Reader leaves: after Pantalone leaves, one combine call shortens Maukie's share. Pantalone's part reaches the Helper and is saved under its own condition. Maukie never reads it, and it's Pantalone's again when he comes back. Fails without the fix: "Maukie's share is shortened after Pantalone leaves". - `{{user}}` in the shared part: one combine call. Maukie and Pantalone each still read only their own private part. Fails without the fix: "a shared part with {{user}} is shortened too". - Save order: the existing partly covered scene test now expects Pantalone's whole constant to be saved before the failed paid call. The failed summary is still not added. Its premise changed, so the assertion "a failed new-range summary is not added to Chat Summaries" was adapted rather than removed. Fails without the fix (2 entries instead of 3). - All existing assertions are kept, including the privacy ones and "the departed character's summary is not sent" for a summary only the narrator still reads. - Local CodeRabbit (`coderabbit review --agent --committed --base-commit $(git merge-base HEAD origin/staging)`): 2 findings. The review base was the merge base with `staging` taken before #7458 merged, so it also covered #7458's changes. Neither was fixed here: - `docs/agents/memory.md` needs the `docs-i18n` mirror or a follow-up issue. Dismissed: the existing follow-up issue #7462 ([docs-i18n] docs/agents/memory.md) covers it, and this PR's edits are noted there. - The leak check in `advanced-memory.ts` compares 4-word runs. A private section shorter than four words has no runs, so the check wouldn't catch it being copied word for word into shared text. Dismissed as out of scope: the gap is in #7458's `runs`/`leaks` code, which this PR doesn't change. This PR does send a few more summaries through that check (ones with a departed character's part or with `{{user}}`). The suggested fix (runs of `min(4, words)`) would also reject answers for 1- and 2-word sections that share a common word, and that tradeoff is for the maintainer to decide in a separate follow-up. ### Manual verification needed - [ ] In a Roleplay individual group chat with Advanced Memory on, set a narrator plus two characters (A and B). Hide one message in a scene from B and another from A, then start a new scene so the first one is archived. Check that **Chat Summaries** gets one entry for that scene, with a `{{#if char == "B"}}` section. - [ ] Remove B from the chat and lower **Summary and recall budget** until A's share is over 70%. Send a reply. Check that **Updating continuity** runs and the new combined entry still has B's section under `{{#if char == "B"}}`. Check that A's prompt (Prompt Inspector) doesn't contain B's part. - [ ] Add B back. Check that B's prompt shows B's part and not A's. - [ ] Repeat with `{{user}}` in the scene summary's shared text. Check that the over-budget share is shortened, `{{user}}` still shows your persona name in each character's prompt, and neither character sees the other's private part. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) 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) Docs: `docs/agents/memory.md` is updated (the shared-summary sentence and the compaction paragraph). The translation follow-up is noted on the existing #7462 ([docs-i18n] docs/agents/memory.md). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:07:04 +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#7745
No description provided.