[PR #7416] [MERGED] fix(summary): make Write honor the Range summary scope #7723

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

📋 Pull Request Information

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

Base: staging ← Head: fix/summary-write-range


📝 Commits (4)

  • 7dcbc90 fix(summary): make Write honor the Range summary scope
  • 5c1d21f fix(summary): let a refused Write range be changed and say why
  • d1baad8 fix(summary): say why a Write range is refused, and keep one entry per save
  • 7cdb301 fix(summary): refuse a Write range without positive whole message numbers

📊 Changes

8 files changed (+582 additions, -125 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/components/chat/ChatSummaryPanel.tsx (+43 -9)
📝 packages/client/src/hooks/use-chats.ts (+3 -3)
📝 packages/client/src/lib/chat-summary-batch.ts (+29 -0)
📝 packages/client/src/localization/locales/en.json (+1 -0)
📝 packages/server/src/routes/chats.routes.ts (+164 -113)
📝 packages/server/src/routes/generate/generate-route-utils.ts (+31 -0)
➕ scripts/regressions/summary-write-range.regression.ts (+310 -0)

📄 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 #7410

Why this change

  • In Chat Summary, a summary written with Write while Summary Scope was Range ignored the selected messages. It was saved as a "last messages" entry with no messageIds, no rangeStartIndex/rangeEndIndex and no hiddenMessageIds. So it showed no range, and with Hide summarised messages on, it never hid what it summarized. Generate with the same range worked, so the two buttons gave different results for the same scope.

What changed

  • Client: in Range scope, a new Write draft covers the first range row (sourceMode: "range", rangeStartIndex, rangeEndIndex). The draft row shows that range while you type.
    • Until it is saved, the draft follows Summary Scope. If the server refuses its range, you can change the range (or switch to Last) and click Save again, and your text stays. The draft's coverage comes from resolveChatSummaryWriteCoverage (lib/chat-summary-batch.ts), and the panel uses it both to show the draft and to save it.
    • An invalid range (missing, not a number, outside the history, too large) shows the existing range message, both on Write and on Save, as it disables Generate.
    • If the server refuses the range, the toast says why in the user's language. The server returns a stable code (hidden, invalid, outside, tooLarge), and the panel maps it to a localized message. invalid, outside and tooLarge reuse the existing range messages. hidden uses one new key, ui.chat.summarypopover.batchRangeAllHidden ("Every message in this range is already hidden."). Any other error still shows "Could not save summary entry."
  • Client: saving a new range entry refreshes the message list, because the server may have hidden messages (before this, only delete did that).
  • Server: I moved the Generate route's range handling into shared code instead of copying it:
    • selectSummaryRangeMessages (generate-route-utils) turns a range into its non-hidden messages and one-based indices. It returns the errors Generate already used (not found, outside history, more than 500, nothing visible), each with a stable code.
    • appendSummaryEntryHidingMessages (in chatsRoutes) is the queued hide-then-append step with rollback on failure, moved from Generate. It takes the route's request, so the queue is still keyed by req.params.id. Inside the queue it checks again whether the entry id already exists, so two saves of the same draft that both pass the route's first check keep one entry.
    • POST /generate-summary now calls both, and its behavior and response are unchanged.
    • PATCH /summary-entries replace calls both for a new entry with sourceMode: "range". A new range entry whose indices are not positive whole numbers is refused with the code invalid, instead of being saved as a range that covers nothing. The server sets messageIds, messageCount and the range itself, and ignores any hiddenMessageIds the client sends. Edits to an existing entry still go through the plain replace and keep the coverage it already has.
  • Regression: scripts/regressions/summary-write-range.regression.ts covers:
    • Generate 1–3 and Write 1–3 on matching chats: both save the same coverage (source, range, count, messages, hidden). This fails on origin/staging, where messageCount, messageIds and hiddenMessageIds were all missing.
    • The protected tail stays visible for Write 4–6, and editing an entry keeps its coverage.
    • Write refuses the ranges Generate refuses, with the same error, the matching code, and nothing saved.
    • Write refuses a new range entry with a zero, fractional or missing index (code invalid) and saves nothing.
    • A refused draft saved again with the range picked next (5–6) covers 5–6, and one saved after switching to Last has no coverage.
    • Two saves of the same new draft held behind the chat's queue keep one entry. Without the queued check, they saved two.
    • Source checks that the panel shows and saves the draft with the current scope and maps the hidden code to its localized message.
  • CHANGELOG entry under [Unreleased].

Behavior choices for review:

  • With several range rows, Write covers the first row, the one the panel lists first.
  • An open Write draft follows the range fields until it is saved. If you change the first range while typing, the draft row shows the new range, and that is what gets saved.
  • In Last scope, Write still saves an entry with no coverage, as before. The issue only covers Range, so this PR leaves Last alone.
  • Generate still shows the server's English reason for a refused range. This PR only changes the Write save toast.

Review follow-up

  • Adversarial review (fixed): a Write draft the server refused could not be saved, and the toast didn't say why. With Hide summarised messages on, Generate 1–3 hides 1–3. Write 1–3 then opened a draft fixed to 1–3, and every Save got the server's 400 and the generic toast, so the text had to be copied out. Fix: the draft follows Summary Scope until it is saved, Save rechecks the range, and the refusal toast says why. The regression covers saving the refused draft again with another range and with Last.
  • Full regression suite (fixed): issue-sweep-5391-5394 failed on this branch's first commit. It pins Generate's summary queue as withChatMetadataPatchQueue(req.params.id, …) followed by metadataQueueHeld: true, and the shared helper had keyed the queue by a chatId parameter. I left the test as it is and changed the helper to take the route's request, so the queue is keyed exactly as before.

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

Commands run: pnpm check (passes; the one lint warning is already on staging, in HomeProfessorMariChat.tsx), node ./scripts/run-regressions.mjs --filter summary-write-range (fails on staging code and on each fix reverted on its own, passes with this branch), node ./scripts/run-regressions.mjs --filter issue-sweep-5391-5394 (passes), node ./scripts/run-regressions.mjs (580/581; the one failure, termux-sharp, is ERR_PNPM_NO_OFFLINE_TARBALL for @img/sharp-wasm32 in this offline sandbox, and this PR changes no dependencies), pnpm localization:check (passes), coderabbit review --agent --committed --base-commit <merge-base>.

Manual verification notes

  • Manually verify that in a Roleplay chat with several messages, choosing Summary Scope › Range with messages 1–3, clicking Write, typing text and saving gives an entry labelled with messages 1–3.
  • Manually verify that with Hide summarised messages on, those messages show as hidden in the transcript right after saving, without a reload, and the protected tail stays visible.
  • Manually verify that deleting that entry unhides the messages again.
  • Manually verify that with an empty or out-of-range Range field, Write shows the range warning in a toast and opens no entry.
  • Manually verify that after Generate for 1–3 with Hide summarised messages on, Write for 1–3 and Save shows "Every message in this range is already hidden." and keeps the draft open with your text.
  • Manually verify that changing the first range to messages that are still visible (or switching to Last) updates the open draft's label, and Save then works without retyping.
  • Manually verify that editing a saved range entry's text keeps its range label and hidden messages.
  • Manually verify that in Last scope, Write and Generate work as before, and that Range Generate (one and several ranges) still works.

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)

No files under docs/ changed, so no [docs-i18n] follow-up is needed.

Local CodeRabbit

I ran coderabbit review --agent --committed --base-commit <merge-base> three times, after each round of fixes. It reported 6 findings in all: 4 fixed, 2 rejected.

Fixed:

  • Two saves of the same new range entry could append it twice (run 1). The route checked whether the entry was new outside the chat's queue. I reproduced it in the regression by holding the queue: two saves passed the check and two entries were saved. The helper now checks again inside the queue and returns the chat unchanged if the entry is already there.
  • The server's English refusal text reached users without localization (run 1). The server now returns a stable code, and the panel shows a localized message (see What changed).
  • Range entries with non-positive or non-integer indices skipped validation (run 2, reported twice). Index 0 was refused as "not found", and a fractional or missing index fell through to the plain replace and was saved as a range that covers nothing. A new range entry now needs positive whole numbers, or it is refused with the code invalid. The only caller is the panel, which always sends whole numbers for a range draft and sends no sourceMode when editing, so no existing caller breaks.

Rejected:

  • Transfer hide ownership for overlapping ranges (run 1, minor). If summary A hid messages before summary B covers them, B's hiddenMessageIds leaves them out, so deleting A unhides them while B is still on. This PR doesn't introduce that: appendSummaryEntryHidingMessages is staging's Generate block moved as is (hiddenMessageIds = what bulkSetHiddenFromAI newly flipped), and getChatSummaryMessageIdsToUnhideAfterDelete already behaves that way for two overlapping Generate summaries on staging. Write now behaves like Generate, which is what #7410 asks for. Changing who owns a hidden message changes delete and unhide for every summary, so it belongs in its own issue.
  • The range is resolved against messages read outside the queue (run 3, trivial). Generate has the same gap, and there it lasts the whole LLM call. Moving the read inside the queue wouldn't close it either: message create and delete don't take the chat metadata queue (removeMessages uses per-message interruption queues), so a deletion can still land between selection and save. The range is also whatever the user picked from the panel, which can be out of date in the same way.

GitHub CodeRabbit had hit its review limit when I checked the PR, so its review is still pending.


🔄 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/7416 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/11/2026 **Status:** ✅ Merged **Merged:** 10/11/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/summary-write-range` --- ### 📝 Commits (4) - [`7dcbc90`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7dcbc906d67e2dadbbd98231cc9b1ab549b594aa) fix(summary): make Write honor the Range summary scope - [`5c1d21f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5c1d21fe17571c7429849d9c500d65e587e31970) fix(summary): let a refused Write range be changed and say why - [`d1baad8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d1baad8a00ae4a3d3dbd85bf17d427fa3045d8f8) fix(summary): say why a Write range is refused, and keep one entry per save - [`7cdb301`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7cdb3019c01e37548bf89a5d33482a2896c7a9cb) fix(summary): refuse a Write range without positive whole message numbers ### 📊 Changes **8 files changed** (+582 additions, -125 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/components/chat/ChatSummaryPanel.tsx` (+43 -9) 📝 `packages/client/src/hooks/use-chats.ts` (+3 -3) 📝 `packages/client/src/lib/chat-summary-batch.ts` (+29 -0) 📝 `packages/client/src/localization/locales/en.json` (+1 -0) 📝 `packages/server/src/routes/chats.routes.ts` (+164 -113) 📝 `packages/server/src/routes/generate/generate-route-utils.ts` (+31 -0) ➕ `scripts/regressions/summary-write-range.regression.ts` (+310 -0) </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`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7410 ## Why this change - In **Chat Summary**, a summary written with **Write** while **Summary Scope** was **Range** ignored the selected messages. It was saved as a "last messages" entry with no `messageIds`, no `rangeStartIndex`/`rangeEndIndex` and no `hiddenMessageIds`. So it showed no range, and with **Hide summarised messages** on, it never hid what it summarized. **Generate** with the same range worked, so the two buttons gave different results for the same scope. ## What changed - **Client:** in Range scope, a new Write draft covers the first range row (`sourceMode: "range"`, `rangeStartIndex`, `rangeEndIndex`). The draft row shows that range while you type. - Until it is saved, the draft follows **Summary Scope**. If the server refuses its range, you can change the range (or switch to **Last**) and click **Save** again, and your text stays. The draft's coverage comes from `resolveChatSummaryWriteCoverage` (`lib/chat-summary-batch.ts`), and the panel uses it both to show the draft and to save it. - An invalid range (missing, not a number, outside the history, too large) shows the existing range message, both on **Write** and on **Save**, as it disables **Generate**. - If the server refuses the range, the toast says why in the user's language. The server returns a stable `code` (`hidden`, `invalid`, `outside`, `tooLarge`), and the panel maps it to a localized message. `invalid`, `outside` and `tooLarge` reuse the existing range messages. `hidden` uses one new key, `ui.chat.summarypopover.batchRangeAllHidden` ("Every message in this range is already hidden."). Any other error still shows "Could not save summary entry." - **Client:** saving a new range entry refreshes the message list, because the server may have hidden messages (before this, only delete did that). - **Server:** I moved the Generate route's range handling into shared code instead of copying it: - `selectSummaryRangeMessages` (generate-route-utils) turns a range into its non-hidden messages and one-based indices. It returns the errors Generate already used (not found, outside history, more than 500, nothing visible), each with a stable `code`. - `appendSummaryEntryHidingMessages` (in `chatsRoutes`) is the queued hide-then-append step with rollback on failure, moved from Generate. It takes the route's request, so the queue is still keyed by `req.params.id`. Inside the queue it checks again whether the entry id already exists, so two saves of the same draft that both pass the route's first check keep one entry. - `POST /generate-summary` now calls both, and its behavior and response are unchanged. - `PATCH /summary-entries` `replace` calls both for a **new** entry with `sourceMode: "range"`. A new range entry whose indices are not positive whole numbers is refused with the code `invalid`, instead of being saved as a range that covers nothing. The server sets `messageIds`, `messageCount` and the range itself, and ignores any `hiddenMessageIds` the client sends. Edits to an existing entry still go through the plain replace and keep the coverage it already has. - **Regression:** `scripts/regressions/summary-write-range.regression.ts` covers: - Generate 1–3 and Write 1–3 on matching chats: both save the same coverage (source, range, count, messages, hidden). This fails on `origin/staging`, where `messageCount`, `messageIds` and `hiddenMessageIds` were all missing. - The protected tail stays visible for Write 4–6, and editing an entry keeps its coverage. - Write refuses the ranges Generate refuses, with the same error, the matching `code`, and nothing saved. - Write refuses a new range entry with a zero, fractional or missing index (code `invalid`) and saves nothing. - A refused draft saved again with the range picked next (5–6) covers 5–6, and one saved after switching to **Last** has no coverage. - Two saves of the same new draft held behind the chat's queue keep one entry. Without the queued check, they saved two. - Source checks that the panel shows and saves the draft with the current scope and maps the `hidden` code to its localized message. - CHANGELOG entry under `[Unreleased]`. Behavior choices for review: - With several range rows, Write covers the **first** row, the one the panel lists first. - An open Write draft follows the range fields until it is saved. If you change the first range while typing, the draft row shows the new range, and that is what gets saved. - In **Last** scope, Write still saves an entry with no coverage, as before. The issue only covers Range, so this PR leaves Last alone. - **Generate** still shows the server's English reason for a refused range. This PR only changes the **Write** save toast. ## Review follow-up - **Adversarial review (fixed):** a Write draft the server refused could not be saved, and the toast didn't say why. With **Hide summarised messages** on, Generate 1–3 hides 1–3. Write 1–3 then opened a draft fixed to 1–3, and every **Save** got the server's 400 and the generic toast, so the text had to be copied out. Fix: the draft follows **Summary Scope** until it is saved, **Save** rechecks the range, and the refusal toast says why. The regression covers saving the refused draft again with another range and with **Last**. - **Full regression suite (fixed):** `issue-sweep-5391-5394` failed on this branch's first commit. It pins Generate's summary queue as `withChatMetadataPatchQueue(req.params.id, …)` followed by `metadataQueueHeld: true`, and the shared helper had keyed the queue by a `chatId` parameter. I left the test as it is and changed the helper to take the route's request, so the queue is keyed exactly as before. ## 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` Commands run: `pnpm check` (passes; the one lint warning is already on staging, in `HomeProfessorMariChat.tsx`), `node ./scripts/run-regressions.mjs --filter summary-write-range` (fails on staging code and on each fix reverted on its own, passes with this branch), `node ./scripts/run-regressions.mjs --filter issue-sweep-5391-5394` (passes), `node ./scripts/run-regressions.mjs` (580/581; the one failure, `termux-sharp`, is `ERR_PNPM_NO_OFFLINE_TARBALL` for `@img/sharp-wasm32` in this offline sandbox, and this PR changes no dependencies), `pnpm localization:check` (passes), `coderabbit review --agent --committed --base-commit <merge-base>`. ### Manual verification notes - Manually verify that in a Roleplay chat with several messages, choosing **Summary Scope** › **Range** with messages 1–3, clicking **Write**, typing text and saving gives an entry labelled with messages 1–3. - Manually verify that with **Hide summarised messages** on, those messages show as hidden in the transcript right after saving, without a reload, and the protected tail stays visible. - Manually verify that deleting that entry unhides the messages again. - Manually verify that with an empty or out-of-range Range field, **Write** shows the range warning in a toast and opens no entry. - Manually verify that after **Generate** for 1–3 with **Hide summarised messages** on, **Write** for 1–3 and **Save** shows "Every message in this range is already hidden." and keeps the draft open with your text. - Manually verify that changing the first range to messages that are still visible (or switching to **Last**) updates the open draft's label, and **Save** then works without retyping. - Manually verify that editing a saved range entry's text keeps its range label and hidden messages. - Manually verify that in **Last** scope, Write and Generate work as before, and that Range **Generate** (one and several ranges) still works. ## 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) No files under `docs/` changed, so no `[docs-i18n]` follow-up is needed. ## Local CodeRabbit I ran `coderabbit review --agent --committed --base-commit <merge-base>` three times, after each round of fixes. It reported 6 findings in all: 4 fixed, 2 rejected. Fixed: - **Two saves of the same new range entry could append it twice** (run 1). The route checked whether the entry was new outside the chat's queue. I reproduced it in the regression by holding the queue: two saves passed the check and two entries were saved. The helper now checks again inside the queue and returns the chat unchanged if the entry is already there. - **The server's English refusal text reached users without localization** (run 1). The server now returns a stable `code`, and the panel shows a localized message (see What changed). - **Range entries with non-positive or non-integer indices skipped validation** (run 2, reported twice). Index `0` was refused as "not found", and a fractional or missing index fell through to the plain replace and was saved as a range that covers nothing. A new range entry now needs positive whole numbers, or it is refused with the code `invalid`. The only caller is the panel, which always sends whole numbers for a range draft and sends no `sourceMode` when editing, so no existing caller breaks. Rejected: - **Transfer hide ownership for overlapping ranges** (run 1, minor). If summary A hid messages before summary B covers them, B's `hiddenMessageIds` leaves them out, so deleting A unhides them while B is still on. This PR doesn't introduce that: `appendSummaryEntryHidingMessages` is staging's Generate block moved as is (`hiddenMessageIds` = what `bulkSetHiddenFromAI` newly flipped), and `getChatSummaryMessageIdsToUnhideAfterDelete` already behaves that way for two overlapping Generate summaries on staging. Write now behaves like Generate, which is what #7410 asks for. Changing who owns a hidden message changes delete and unhide for every summary, so it belongs in its own issue. - **The range is resolved against messages read outside the queue** (run 3, trivial). Generate has the same gap, and there it lasts the whole LLM call. Moving the read inside the queue wouldn't close it either: message create and delete don't take the chat metadata queue (`removeMessages` uses per-message interruption queues), so a deletion can still land between selection and save. The range is also whatever the user picked from the panel, which can be out of date in the same way. GitHub CodeRabbit had hit its review limit when I checked the PR, so its review is still pending. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:06:52 +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#7723
No description provided.