[GH-ISSUE #7573] Split packages/server/src/routes/generate.routes.ts into bounded modules #4208

Closed
opened 2026-10-21 06:12:26 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @Gunterlie on GitHub (Oct 13, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7573

Originally assigned to: @Gunterlie on GitHub.

Problem

packages/server/src/routes/generate.routes.ts has about 15,200 lines. The main generation handler is one closure that holds the streaming, the tool-call rounds, the roleplay command filter, the game filters, the usage counters and the save step. Parts of it are already in packages/server/src/routes/generate/, but the core handler is not.

The size causes real defects. Example from #7570: a rollback for textual tool calls sent the raw reply to the client. The roleplay command filter that hides commands is defined about 1,400 lines above that line, so the author and two reviews did not see the link. CodeRabbit found it.

Proposal

Split the file in small steps. Each step moves one bounded part and changes no behaviour.

Candidates, in order of value:

  • The tool-call round loop (the request, the abort handling, the textual tool-call rollback, the usage counters).
  • The stream output chain (sendTokenTextChunked, writeContentChunked, the roleplay, chance and spatial filters, content_replace).
  • The post-generation step (command parsing, persistence, agent runs).
  • The roleplay roll handling.

Constraints

  • docs/development/code-cleanup-audit.md says that a standalone "split everything" PR adds churn without reliable proof. Thus: one part for each PR, and each PR adds or names the regression that pins the moved behaviour.
  • No behaviour change in a split PR.
  • The shared closure state (fullResponse, the filters, the abort controllers) needs an explicit context object first. That is the first step.

Done when

  • The main handler reads as a sequence of named steps.
  • No part of the file is longer than a person can review in one pass.
Originally created by @Gunterlie on GitHub (Oct 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7573 Originally assigned to: @Gunterlie on GitHub. ## Problem `packages/server/src/routes/generate.routes.ts` has about 15,200 lines. The main generation handler is one closure that holds the streaming, the tool-call rounds, the roleplay command filter, the game filters, the usage counters and the save step. Parts of it are already in `packages/server/src/routes/generate/`, but the core handler is not. The size causes real defects. Example from #7570: a rollback for textual tool calls sent the raw reply to the client. The roleplay command filter that hides commands is defined about 1,400 lines above that line, so the author and two reviews did not see the link. CodeRabbit found it. ## Proposal Split the file in small steps. Each step moves one bounded part and changes no behaviour. Candidates, in order of value: - The tool-call round loop (the request, the abort handling, the textual tool-call rollback, the usage counters). - The stream output chain (`sendTokenTextChunked`, `writeContentChunked`, the roleplay, chance and spatial filters, `content_replace`). - The post-generation step (command parsing, persistence, agent runs). - The roleplay roll handling. ## Constraints - `docs/development/code-cleanup-audit.md` says that a standalone "split everything" PR adds churn without reliable proof. Thus: one part for each PR, and each PR adds or names the regression that pins the moved behaviour. - No behaviour change in a split PR. - The shared closure state (`fullResponse`, the filters, the abort controllers) needs an explicit context object first. That is the first step. ## Done when - The main handler reads as a sequence of named steps. - No part of the file is longer than a person can review in one pass.
Author
Owner

@Gunterlie commented on GitHub (Oct 15, 2026):

Eight slices are merged (#7671, #7672, #7673, #7674, #7676, #7677, #7678, #7679). The remaining parts are tracked in #7686.

<!-- gh-comment-id:6193719915 --> @Gunterlie commented on GitHub (Oct 15, 2026): Eight slices are merged (#7671, #7672, #7673, #7674, #7676, #7677, #7678, #7679). The remaining parts are tracked in #7686.
Author
Owner

@SpicyMarinara commented on GitHub (Oct 19, 2026):

Closing for now to clear the board for the next release. Eight slices landed (#7671 to #7679), and the remaining parts were tracked in #7686, which is also closed for now; reopen either one after the release.

<!-- gh-comment-id:6264054407 --> @SpicyMarinara commented on GitHub (Oct 19, 2026): Closing for now to clear the board for the next release. Eight slices landed (#7671 to #7679), and the remaining parts were tracked in #7686, which is also closed for now; reopen either one after the release.
Author
Owner

@Gunterlie commented on GitHub (Oct 30, 2026):

Closing for now to clear the board before the next stable release. This is not a rejection; I will reopen it after the release.

<!-- gh-comment-id:6521118472 --> @Gunterlie commented on GitHub (Oct 30, 2026): Closing for now to clear the board before the next stable release. This is not a rejection; I will reopen it after the release.
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#4208
No description provided.