[PR #7672] [MERGED] refactor(generate): move the tool-call round loop into runToolRounds (#7573, slice 2) #7872

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/7672
Author: @Gunterlie
Created: 10/15/2026
Status: ✅ Merged
Merged: 10/15/2026
Merged by: @Gunterlie

Base: staging ← Head: chore/7573-tool-rounds


📝 Commits (3)

  • 2665612 refactor(generate): move the tool-call round loop into runToolRounds
  • 6348594 refactor(generate): read the usage provider per request, as before
  • 579c496 test(generate): cover the game tool-plan branch of runToolRounds

📊 Changes

6 files changed (+790 additions, -473 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+110 -465)
➕ packages/server/src/routes/generate/tool-rounds.ts (+453 -0)
📝 scripts/regressions/game-mode-dice-tool.regression.ts (+8 -3)
➕ scripts/regressions/generate-tool-rounds.regression.ts (+209 -0)
📝 scripts/regressions/maintenance-lifecycle.regression.ts (+1 -1)
📝 scripts/regressions/roleplay-streaming.regression.ts (+9 -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

Part of #7573 (second slice). Stacked on #7671: the base is that branch. Merge #7671 first, then retarget this PR to staging.

Why this change

The tool-call round loop was about 470 lines inside generateForCharacter, the 3,800-line closure of the generation handler. It holds the requests, the abort handling, the textual tool-call rollback and the usage counters. #7570 was a defect in exactly this loop: the rollback sent raw text, and the filter that should have hidden it was out of sight. This PR moves the loop into one module with a regression that drives it with a fake provider. The handler now passes in what the loop needs and gets back what it changed.

No behaviour change.

What changed

  • New packages/server/src/routes/generate/tool-rounds.ts: runToolRounds(ctx, fullResponse) holds the tool rounds, the roll-abort and generation-abort handling, the textual tool-call rollback with content_replace, the usage callbacks, the final no-tools round and the about-me proposal helper. It returns { aborted, fullResponse, finishReason, geminiResponseParts, narratorMessages }.
  • generate.routes.ts (now about 15,200 lines) calls it. Request options, the token limits, encrypted reasoning and the usage provider are read per request through callbacks, as before. The partial reply is copied back before an abort returns, so a merged generation still saves it.
  • New scripts/regressions/generate-tool-rounds.regression.ts: the textual tool-call rollback, an abort mid-request, a roleplay roll cut mid-stream, and the final round without tools.
  • Source pins in game-mode-dice-tool, maintenance-lifecycle and roleplay-streaming follow the moved code.

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: generate-tool-rounds, roleplay-function-calling, generation-usage, game-tool-outcomes, advanced-memory-post-generation, generate-agent-abort, roleplay-streaming, roleplay-commands, one-request-dice-rendering, command-only-regenerate, provider-stream-completion, maintenance-lifecycle, game-mode-dice-tool, generation-stream-output. A sweep of the 89 regressions that read generate.routes.ts passed after the pin updates above.
  • Local CodeRabbit: one finding (the usage provider was read once instead of per request); fixed. A separate line-by-line review found no behaviour change.
  • Not covered by a behaviour test (checked by reading only): the game tool-plan branch, the lost-usage accounting across rounds, denied tool calls.

Manual verification notes

  • Manually verify a chat with function calling on: a tool call runs, its result card shows, and the reply continues after it.
  • Manually verify a roleplay chat with rolls on: a [roll: …] request stops the stream, the roll shows, and the reply continues.
  • Manually verify Stop during a tool round: the generation stops and no error shows.
  • Manually verify a game turn with the dice tool: the dice card shows and the narration follows.

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

  • Refactor
    • Reorganized tool-assisted generation handling for streamed responses, roleplay roll requests, cancellation, and follow-up responses. Existing user-facing behavior is preserved.
  • Tests
    • Expanded regression coverage for tool-assisted generation, streamed text, roleplay rolls, cancellation, and final follow-up requests. Added checks for game-tool execution and the handling of its results.

🔄 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/7672 **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-tool-rounds` --- ### 📝 Commits (3) - [`2665612`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2665612f3f958570abe75f9a3b2257ffb4c436b1) refactor(generate): move the tool-call round loop into runToolRounds - [`6348594`](https://github.com/Pasta-Devs/Marinara-Engine/commit/63485946e8c0c4b127910d83f039338396d26270) refactor(generate): read the usage provider per request, as before - [`579c496`](https://github.com/Pasta-Devs/Marinara-Engine/commit/579c496491615bd7e3c35a681b45db4b1002233b) test(generate): cover the game tool-plan branch of runToolRounds ### 📊 Changes **6 files changed** (+790 additions, -473 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+110 -465) ➕ `packages/server/src/routes/generate/tool-rounds.ts` (+453 -0) 📝 `scripts/regressions/game-mode-dice-tool.regression.ts` (+8 -3) ➕ `scripts/regressions/generate-tool-rounds.regression.ts` (+209 -0) 📝 `scripts/regressions/maintenance-lifecycle.regression.ts` (+1 -1) 📝 `scripts/regressions/roleplay-streaming.regression.ts` (+9 -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 Part of #7573 (second slice). Stacked on #7671: the base is that branch. Merge #7671 first, then retarget this PR to `staging`. ## Why this change The tool-call round loop was about 470 lines inside `generateForCharacter`, the 3,800-line closure of the generation handler. It holds the requests, the abort handling, the textual tool-call rollback and the usage counters. #7570 was a defect in exactly this loop: the rollback sent raw text, and the filter that should have hidden it was out of sight. This PR moves the loop into one module with a regression that drives it with a fake provider. The handler now passes in what the loop needs and gets back what it changed. No behaviour change. ## What changed - New `packages/server/src/routes/generate/tool-rounds.ts`: `runToolRounds(ctx, fullResponse)` holds the tool rounds, the roll-abort and generation-abort handling, the textual tool-call rollback with `content_replace`, the usage callbacks, the final no-tools round and the about-me proposal helper. It returns `{ aborted, fullResponse, finishReason, geminiResponseParts, narratorMessages }`. - `generate.routes.ts` (now about 15,200 lines) calls it. Request options, the token limits, encrypted reasoning and the usage provider are read per request through callbacks, as before. The partial reply is copied back before an abort returns, so a merged generation still saves it. - New `scripts/regressions/generate-tool-rounds.regression.ts`: the textual tool-call rollback, an abort mid-request, a roleplay roll cut mid-stream, and the final round without tools. - Source pins in `game-mode-dice-tool`, `maintenance-lifecycle` and `roleplay-streaming` follow the moved code. ## 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: `generate-tool-rounds`, `roleplay-function-calling`, `generation-usage`, `game-tool-outcomes`, `advanced-memory-post-generation`, `generate-agent-abort`, `roleplay-streaming`, `roleplay-commands`, `one-request-dice-rendering`, `command-only-regenerate`, `provider-stream-completion`, `maintenance-lifecycle`, `game-mode-dice-tool`, `generation-stream-output`. A sweep of the 89 regressions that read `generate.routes.ts` passed after the pin updates above. - Local CodeRabbit: one finding (the usage provider was read once instead of per request); fixed. A separate line-by-line review found no behaviour change. - Not covered by a behaviour test (checked by reading only): the game tool-plan branch, the lost-usage accounting across rounds, denied tool calls. ### Manual verification notes - Manually verify a chat with function calling on: a tool call runs, its result card shows, and the reply continues after it. - Manually verify a roleplay chat with rolls on: a `[roll: …]` request stops the stream, the roll shows, and the reply continues. - Manually verify Stop during a tool round: the generation stops and no error shows. - Manually verify a game turn with the dice tool: the dice card shows and the narration follows. ## 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 * **Refactor** * Reorganized tool-assisted generation handling for streamed responses, roleplay roll requests, cancellation, and follow-up responses. Existing user-facing behavior is preserved. * **Tests** * Expanded regression coverage for tool-assisted generation, streamed text, roleplay rolls, cancellation, and final follow-up requests. Added checks for game-tool execution and the handling of its results. <!-- 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#7872
No description provided.