[PR #7671] [MERGED] refactor(generate): move the token output chain into its own module (#7573, slice 1) #7871

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

📋 Pull Request Information

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

Base: staging ← Head: chore/7573-generate-routes-split


📝 Commits (2)

  • ba7d59f refactor(generate): move the token output chain into GenerationStreamOutput
  • 635429f test(generate): pin the held stream's roll detection

📊 Changes

5 files changed (+243 additions, -105 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+28 -89)
➕ packages/server/src/routes/generate/generation-stream-output.ts (+103 -0)
➕ scripts/regressions/generation-stream-output.regression.ts (+94 -0)
📝 scripts/regressions/one-request-dice-rendering.regression.ts (+8 -8)
📝 scripts/regressions/roleplay-streaming.regression.ts (+10 -8)

📄 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

Part of #7573 (first slice; the issue stays open for the next slices).

Why this change

generate.routes.ts is about 15,600 lines and the generation handler is one closure. The stream output chain (the roleplay command, chance and spatial filters, the token chunking and the reasoning clock) was spread over about 60 lines in the handler, with the flush about 1,800 lines below it. #7570 showed what that costs: a rollback sent raw text to the client because the filter that hides commands was out of sight. This PR moves that chain into one named module with its own regression, so the next slices (the tool-call round loop, the post-generation step) can call it instead of closure variables.

No behaviour change.

What changed

  • New packages/server/src/routes/generate/generation-stream-output.ts: GenerationStreamOutput holds the three stream filters, the 6-character SSE token chunks with the periodic event-loop yield, the reasoning-duration clock and the end-of-reply flush. write() keeps the text-rewrite hold (no tokens, only the clock).
  • generate.routes.ts creates one streamOutput per character turn. writeContentChunked, the tool-round onToken callback and the narrator generator loop now call streamOutput.write(). The held onToken path still feeds the roleplay command filter, so a roll request is still detected.
  • New scripts/regressions/generation-stream-output.regression.ts pins the chunk size, the filter order in roleplay and game chats, the flush of chance-filter leftovers through the spatial filter, and the text-rewrite hold with the reasoning clock.
  • roleplay-streaming and one-request-dice-rendering source pins now point at the new call shapes.

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

Agent-run results (not a substitute for the boxes above):

  • pnpm check: passed.
  • Regressions passed: generation-stream-output, roleplay-streaming, one-request-dice-rendering (drives the real route end to end), roleplay-commands, provider-stream-completion, command-only-regenerate, generate-agent-abort, spatial-context.

Manual verification notes

  • Manually verify a roleplay chat streams a reply token by token, and a [illustrate: …] command does not show while it streams.
  • Manually verify a game chat with one-request dice: a [[roll: …]] span never shows while it streams.
  • Manually verify a chat with a text-rewrite agent (for example Prose Guardian): no tokens stream, the rewritten text arrives, and the reasoning duration shows when thinking is on.

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)

Summary by CodeRabbit

  • Improvements
    • Streamed replies now consistently process roleplay commands, game chance results, and spatial directions before displaying text.
    • Text rewrites remain hidden until ready, while streamed output continues to be delivered in small chunks.
    • Reasoning time is recorded when visible reply text begins, including when text rewrites delay its display.

🔄 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/7671 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `staging` ← **Head:** `chore/7573-generate-routes-split` --- ### 📝 Commits (2) - [`ba7d59f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ba7d59f5044b5bf645177dabf99ae73d2ec573ef) refactor(generate): move the token output chain into GenerationStreamOutput - [`635429f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/635429f994e6707df0f78dbf43de055a0d7e6fe1) test(generate): pin the held stream's roll detection ### 📊 Changes **5 files changed** (+243 additions, -105 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+28 -89) ➕ `packages/server/src/routes/generate/generation-stream-output.ts` (+103 -0) ➕ `scripts/regressions/generation-stream-output.regression.ts` (+94 -0) 📝 `scripts/regressions/one-request-dice-rendering.regression.ts` (+8 -8) 📝 `scripts/regressions/roleplay-streaming.regression.ts` (+10 -8) </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 Part of #7573 (first slice; the issue stays open for the next slices). ## Why this change `generate.routes.ts` is about 15,600 lines and the generation handler is one closure. The stream output chain (the roleplay command, chance and spatial filters, the token chunking and the reasoning clock) was spread over about 60 lines in the handler, with the flush about 1,800 lines below it. #7570 showed what that costs: a rollback sent raw text to the client because the filter that hides commands was out of sight. This PR moves that chain into one named module with its own regression, so the next slices (the tool-call round loop, the post-generation step) can call it instead of closure variables. No behaviour change. ## What changed - New `packages/server/src/routes/generate/generation-stream-output.ts`: `GenerationStreamOutput` holds the three stream filters, the 6-character SSE token chunks with the periodic event-loop yield, the reasoning-duration clock and the end-of-reply flush. `write()` keeps the text-rewrite hold (no tokens, only the clock). - `generate.routes.ts` creates one `streamOutput` per character turn. `writeContentChunked`, the tool-round `onToken` callback and the narrator generator loop now call `streamOutput.write()`. The held `onToken` path still feeds the roleplay command filter, so a roll request is still detected. - New `scripts/regressions/generation-stream-output.regression.ts` pins the chunk size, the filter order in roleplay and game chats, the flush of chance-filter leftovers through the spatial filter, and the text-rewrite hold with the reasoning clock. - `roleplay-streaming` and `one-request-dice-rendering` source pins now point at the new call shapes. ## 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` Agent-run results (not a substitute for the boxes above): - `pnpm check`: passed. - Regressions passed: `generation-stream-output`, `roleplay-streaming`, `one-request-dice-rendering` (drives the real route end to end), `roleplay-commands`, `provider-stream-completion`, `command-only-regenerate`, `generate-agent-abort`, `spatial-context`. ### Manual verification notes - Manually verify a roleplay chat streams a reply token by token, and a `[illustrate: …]` command does not show while it streams. - Manually verify a game chat with one-request dice: a `[[roll: …]]` span never shows while it streams. - Manually verify a chat with a text-rewrite agent (for example Prose Guardian): no tokens stream, the rewritten text arrives, and the reasoning duration shows when thinking is on. ## Docs and release impact - [x] 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) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Streamed replies now consistently process roleplay commands, game chance results, and spatial directions before displaying text. * Text rewrites remain hidden until ready, while streamed output continues to be delivered in small chunks. * Reasoning time is recorded when visible reply text begins, including when text rewrites delay its display. <!-- 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:21 +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#7871
No description provided.