[PR #8652] [MERGED] fix(generate): a streamed reply is no longer saved shorter than it was shown #8724

Closed
opened 2026-11-07 17:39:15 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: staging ← Head: fix/streamed-reply-truncation-8643


📝 Commits (1)

  • 5d063e3 fix(generate): a streamed reply is no longer saved shorter than it was shown (#8643)

📊 Changes

8 files changed (+165 additions, -13 deletions)

View changed files

📝 packages/client/src/hooks/use-generate.ts (+8 -0)
📝 packages/client/src/localization/locales/en.json (+1 -0)
📝 packages/server/src/routes/generate.routes.ts (+2 -0)
📝 packages/server/src/routes/generate/reply-text-cleanup.ts (+7 -3)
📝 packages/server/src/services/agents/agent-executor.ts (+17 -4)
📝 packages/server/src/services/generation/generation-text-utils.ts (+6 -2)
📝 scripts/regressions/agent-runtime.regression.ts (+53 -0)
📝 scripts/regressions/generate-reply-text-cleanup.regression.ts (+71 -4)

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

Why this change

  • A Roleplay reply streamed in full, then flashed and was left as an exact prefix of itself, ending mid-sentence (GLM 5.3 Flash). The report names two leads: a post-generation rewrite agent and trimIncompleteModelOutput. Both turned out to be real server paths that save a fully streamed reply as an exact prefix of what was shown. Each is reproduced in a regression. The issue doesn't say which settings were on, so this PR fixes both.

What changed

1. A rewrite agent that ran out of tokens no longer overwrites the reply.

  • Prose Guardian, Continuity, Immersive HTML and custom text_rewrite agents return the whole reply as JSON editedText, with a 4096-token default limit.
  • GLM 5.3 always reasons. JSON agents ask for reasoning "none", the provider maps that to "low", and no thinking headroom is added. On a long reply the thinking eats the budget, and the rewrite stops mid-string with finish reason length.
  • repairJsonText then closed the unfinished string, the result counted as a success, and the generate route saved it with updateMessageContent. The client swapped the streamed text for it: an exact prefix.
  • resolveStructuredAgentResult (in agent-executor.ts) now fails a text_rewrite result whose call ended unfinished. This covers the single call, the JSON retry and both tool-loop endings. A failed rewrite is never applied, and with "hold for rewrite" on, the route already sends back the original.
  • Other structured agents still recover cut-off JSON as before.

2. "Trim incomplete sentences" no longer cuts finished replies.

  • The setting is off by default. When it's on, its sentence-end check didn't count closers after the full stop: Markdown emphasis (*She smiles softly.*), HTML closing tags (<i>Finally home.</i>) and CJK closing brackets (「また明日ね。」). A finished reply ending that way lost its whole last paragraph.
  • These closers now end a sentence, so the trim never cuts more than before. A reply that really was cut off is still trimmed.
  • When the trim does cut a reply, the server sends a reply_trimmed event and the client shows an info toast that names the setting (new key chat.reply.trimmedIncompleteEnding, English only). Autonomous replies show no toast.

Regressions. agent-runtime gains cut-off rewrites for Prose Guardian and a custom rewrite agent, with and without tools. generate-reply-text-cleanup gains finished endings with emphasis, tags and CJK brackets, a truly cut-off line, and the reply_trimmed wiring. Each fails on the old code and passes now.

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

What the agent ran locally:

  • node ./scripts/pnpm-runner.mjs check and localization:check pass, after rebasing on staging.
  • These regressions pass: agent-runtime, generate-reply-text-cleanup, roleplay-streaming, reply-checkup, agent-retry-batching, manual-agent-retry-resolution and merged-rewrite-lorebooks.
  • An independent review agent added the HTML-tag and CJK-bracket closers, and the tool-loop proof. Local CodeRabbit reported 0 findings.

To verify by hand:

  • Manually verify in Roleplay with a GLM 5.3 (or any reasoning) agent connection. Turn on Prose Guardian, or a custom rewrite agent, with "hold for rewrite" off and agent max tokens set low (for example 256). Send a long reply, then check that the streamed reply stays whole after the agent runs and that the agent run shows that the rewrite stopped before it finished.
  • Manually verify the same setup with "hold for rewrite" on: the placeholder should resolve to the original full reply.
  • Manually verify with Settings → "Trim incomplete sentences from the response" on: a reply ending in an italic action like *She smiles.* stays whole, and a reply really cut off by a low max-token limit is trimmed and shows the new toast.

Not changed (follow-ups):

  • A failed rewrite only shows in the agent run list and debug console, not as an error toast with Retry.
  • Models that always reason still get no thinking headroom on JSON agents.
  • replyUnfinished doesn't know GLM's own network_error / sensitive finish reasons.

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)

No docs change: both are bug fixes behind existing settings.

🤖 Generated with Claude Code


🔄 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/8652 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/streamed-reply-truncation-8643` --- ### 📝 Commits (1) - [`5d063e3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5d063e37fc37c70ff9a8f0a0e7a90757bd8fab1c) fix(generate): a streamed reply is no longer saved shorter than it was shown (#8643) ### 📊 Changes **8 files changed** (+165 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/hooks/use-generate.ts` (+8 -0) 📝 `packages/client/src/localization/locales/en.json` (+1 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+2 -0) 📝 `packages/server/src/routes/generate/reply-text-cleanup.ts` (+7 -3) 📝 `packages/server/src/services/agents/agent-executor.ts` (+17 -4) 📝 `packages/server/src/services/generation/generation-text-utils.ts` (+6 -2) 📝 `scripts/regressions/agent-runtime.regression.ts` (+53 -0) 📝 `scripts/regressions/generate-reply-text-cleanup.regression.ts` (+71 -4) </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 #8643 ## Why this change - A Roleplay reply streamed in full, then flashed and was left as an exact prefix of itself, ending mid-sentence (GLM 5.3 Flash). The report names two leads: a post-generation rewrite agent and `trimIncompleteModelOutput`. Both turned out to be real server paths that save a fully streamed reply as an exact prefix of what was shown. Each is reproduced in a regression. The issue doesn't say which settings were on, so this PR fixes both. ## What changed **1. A rewrite agent that ran out of tokens no longer overwrites the reply.** - Prose Guardian, Continuity, Immersive HTML and custom `text_rewrite` agents return the whole reply as JSON `editedText`, with a 4096-token default limit. - GLM 5.3 always reasons. JSON agents ask for reasoning "none", the provider maps that to "low", and no thinking headroom is added. On a long reply the thinking eats the budget, and the rewrite stops mid-string with finish reason `length`. - `repairJsonText` then closed the unfinished string, the result counted as a success, and the generate route saved it with `updateMessageContent`. The client swapped the streamed text for it: an exact prefix. - `resolveStructuredAgentResult` (in `agent-executor.ts`) now fails a `text_rewrite` result whose call ended unfinished. This covers the single call, the JSON retry and both tool-loop endings. A failed rewrite is never applied, and with "hold for rewrite" on, the route already sends back the original. - Other structured agents still recover cut-off JSON as before. **2. "Trim incomplete sentences" no longer cuts finished replies.** - The setting is off by default. When it's on, its sentence-end check didn't count closers after the full stop: Markdown emphasis (`*She smiles softly.*`), HTML closing tags (`<i>Finally home.</i>`) and CJK closing brackets (`「また明日ね。」`). A finished reply ending that way lost its whole last paragraph. - These closers now end a sentence, so the trim never cuts more than before. A reply that really was cut off is still trimmed. - When the trim does cut a reply, the server sends a `reply_trimmed` event and the client shows an info toast that names the setting (new key `chat.reply.trimmedIncompleteEnding`, English only). Autonomous replies show no toast. **Regressions.** `agent-runtime` gains cut-off rewrites for Prose Guardian and a custom rewrite agent, with and without tools. `generate-reply-text-cleanup` gains finished endings with emphasis, tags and CJK brackets, a truly cut-off line, and the `reply_trimmed` wiring. Each fails on the old code and passes now. ## 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 What the agent ran locally: - `node ./scripts/pnpm-runner.mjs check` and `localization:check` pass, after rebasing on staging. - These regressions pass: `agent-runtime`, `generate-reply-text-cleanup`, `roleplay-streaming`, `reply-checkup`, `agent-retry-batching`, `manual-agent-retry-resolution` and `merged-rewrite-lorebooks`. - An independent review agent added the HTML-tag and CJK-bracket closers, and the tool-loop proof. Local CodeRabbit reported 0 findings. To verify by hand: - Manually verify in Roleplay with a GLM 5.3 (or any reasoning) agent connection. Turn on Prose Guardian, or a custom rewrite agent, with "hold for rewrite" off and agent max tokens set low (for example 256). Send a long reply, then check that the streamed reply stays whole after the agent runs and that the agent run shows that the rewrite stopped before it finished. - Manually verify the same setup with "hold for rewrite" on: the placeholder should resolve to the original full reply. - Manually verify with Settings → "Trim incomplete sentences from the response" on: a reply ending in an italic action like `*She smiles.*` stays whole, and a reply really cut off by a low max-token limit is trimmed and shows the new toast. **Not changed (follow-ups):** - A failed rewrite only shows in the agent run list and debug console, not as an error toast with Retry. - Models that always reason still get no thinking headroom on JSON agents. - `replyUnfinished` doesn't know GLM's own `network_error` / `sensitive` finish reasons. ## 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) No docs change: both are bug fixes behind existing settings. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:39:15 +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#8724
No description provided.