[PR #8702] [MERGED] fix(generation): a failed routing agent prompt is sent to each connection once #8754

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

📋 Pull Request Information

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

Base: staging ← Head: fix/no-duplicate-provider-requests


📝 Commits (2)

  • b4a7c04 fix(generation): send a failed character-routing prompt to each connection once
  • 7c4fdfd fix(generation): keep the reply's fallback for character routing

📊 Changes

3 files changed (+285 additions, -22 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+15 -19)
📝 packages/server/src/services/generation/agent-resolution.ts (+14 -3)
➕ scripts/regressions/provider-requests-per-action.regression.ts (+256 -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 #8694

Why this change

The maintainer saw prompts and requests appear twice for one message on Google and Claude endpoints.

The main reply never sends twice. A fake Gemini and Anthropic server counted provider requests per action through the real /api/generate route and through the browser client:

  • Combinations: 116, covering send, regenerate, continue and impersonate; Roleplay and Conversation; single and group chats; streaming and reasoning on and off.
  • Result: exactly one request each.

The real duplicate was in the character-activity (routing) agent:

  • Its chat-connection provider was built with the main fallback and the rate-limit retry already wrapped around it. Agent resolution then wrapped it again with the agents fallback, another retry and admission.
  • When the routing prompt failed, the same body went to the fallback connection twice.
  • A 429 was retried inside another retry: 49 requests on the main connection and 56 on the fallback, instead of 7 each.

The debug console also logs every prompt twice per request: once as "Prompt sent to model" and once as the provider's "final request" body. That can look like a double send. #8691 makes those logs readable.

What changed

  • generate.routes.ts: the routing path passes the plain provider, as the reply's own agents already do, so agent resolution adds each fallback, retry and admission layer only once.

  • Routing keeps the reply's main fallback. resolveAgentPipelineAgents takes an optional chatConnectionMainFallback, so routing on the chat's connection still falls back to the reply's "If it fails" connection, as on staging. A review found that the first version of the fix dropped this, and that is fixed. The reply's own agents don't pass it, so they are unchanged.

  • New regression scripts/regressions/provider-requests-per-action.regression.ts. Against a fake server speaking Gemini generateContent/streamGenerateContent and Anthropic Messages (SSE and JSON), it checks:

    • exactly 1 request per send, regenerate, continue and impersonation, on both providers;
    • a refused routing prompt reaches each connection exactly once, never the same body twice;
    • a persistent 429 gives exactly 7 attempts per connection.

    It fails before the fix: { primary: 1, fallback: 2 }, and the 429 case gives { primary: 49, fallback: 56 }.

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, rebased on staging:

  • node ./scripts/pnpm-runner.mjs check exits 0.
  • provider-requests-per-action and agent-connection-parameters pass.
  • Also ruled out, with evidence:
    • a streaming-then-buffered fallback;
    • empty-reply, safety and thinking retries (none exist in either provider);
    • the transient retry (off by default);
    • token counting and Gemini context caches (never called);
    • per-generation model lookups;
    • account balance reads;
    • the prompt peek;
    • the client double-submitting (one POST per click; the server refuses a second with 409).
  • Claude (Subscription): the real Agent SDK pointed at the fake sends one /v1/messages per call. With an API key it also sends one HEAD /api/hello connectivity check, which carries no prompt.

For a human to verify:

  • With a custom pre-generation agent set to Character Activity on a Gemini or Claude chat connection, and an "If it fails" fallback set, make the routing fail. Manually verify the fallback provider's dashboard or logs show one fallback request, not two.
  • With Debug mode on, manually verify that one Send, Regenerate, /continue and /impersonate each produce one entry in the Google AI Studio or Anthropic Console request log.
  • In a chat with agents on the same connection, manually verify each agent group (pre-generation, parallel, batched post-processing) shows one request besides the reply. These are separate prompts, not duplicates.

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 changes: no user-facing doc describes agent fallback order or request counts.

UI evidence (if applicable)

No UI change.

🤖 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/8702 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/no-duplicate-provider-requests` --- ### 📝 Commits (2) - [`b4a7c04`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b4a7c04c5a542aba16dc579bebf8fcfff7411ab3) fix(generation): send a failed character-routing prompt to each connection once - [`7c4fdfd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7c4fdfd72e1ffdd5e0b4b0308c51b78c3ee030b9) fix(generation): keep the reply's fallback for character routing ### 📊 Changes **3 files changed** (+285 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+15 -19) 📝 `packages/server/src/services/generation/agent-resolution.ts` (+14 -3) ➕ `scripts/regressions/provider-requests-per-action.regression.ts` (+256 -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 #8694 ## Why this change The maintainer saw prompts and requests appear twice for one message on Google and Claude endpoints. The main reply never sends twice. A fake Gemini and Anthropic server counted provider requests per action through the real `/api/generate` route and through the browser client: - **Combinations:** 116, covering send, regenerate, continue and impersonate; Roleplay and Conversation; single and group chats; streaming and reasoning on and off. - **Result:** exactly one request each. The real duplicate was in the **character-activity (routing) agent**: - Its chat-connection provider was built with the main fallback and the rate-limit retry already wrapped around it. Agent resolution then wrapped it again with the agents fallback, another retry and admission. - When the routing prompt failed, the same body went to the fallback connection twice. - A 429 was retried inside another retry: 49 requests on the main connection and 56 on the fallback, instead of 7 each. The debug console also logs every prompt twice per request: once as "Prompt sent to model" and once as the provider's "final request" body. That can look like a double send. #8691 makes those logs readable. ## What changed - **`generate.routes.ts`:** the routing path passes the plain provider, as the reply's own agents already do, so agent resolution adds each fallback, retry and admission layer only once. - **Routing keeps the reply's main fallback.** `resolveAgentPipelineAgents` takes an optional `chatConnectionMainFallback`, so routing on the chat's connection still falls back to the reply's "If it fails" connection, as on staging. A review found that the first version of the fix dropped this, and that is fixed. The reply's own agents don't pass it, so they are unchanged. - **New regression `scripts/regressions/provider-requests-per-action.regression.ts`.** Against a fake server speaking Gemini `generateContent`/`streamGenerateContent` and Anthropic Messages (SSE and JSON), it checks: - exactly 1 request per send, regenerate, continue and impersonation, on both providers; - a refused routing prompt reaches each connection exactly once, never the same body twice; - a persistent 429 gives exactly 7 attempts per connection. It fails before the fix: `{ primary: 1, fallback: 2 }`, and the 429 case gives `{ primary: 49, fallback: 56 }`. ## 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, rebased on staging: - `node ./scripts/pnpm-runner.mjs check` exits 0. - `provider-requests-per-action` and `agent-connection-parameters` pass. - **Also ruled out, with evidence:** - a streaming-then-buffered fallback; - empty-reply, safety and thinking retries (none exist in either provider); - the transient retry (off by default); - token counting and Gemini context caches (never called); - per-generation model lookups; - account balance reads; - the prompt peek; - the client double-submitting (one POST per click; the server refuses a second with 409). - **Claude (Subscription):** the real Agent SDK pointed at the fake sends one `/v1/messages` per call. With an API key it also sends one `HEAD /api/hello` connectivity check, which carries no prompt. For a human to verify: - With a custom pre-generation agent set to Character Activity on a Gemini or Claude chat connection, and an "If it fails" fallback set, make the routing fail. Manually verify the fallback provider's dashboard or logs show one fallback request, not two. - With Debug mode on, manually verify that one Send, Regenerate, /continue and /impersonate each produce one entry in the Google AI Studio or Anthropic Console request log. - In a chat with agents on the same connection, manually verify each agent group (pre-generation, parallel, batched post-processing) shows one request besides the reply. These are separate prompts, not duplicates. ## 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 changes: no user-facing doc describes agent fallback order or request counts. ## UI evidence (if applicable) No UI change. 🤖 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:27 +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#8754
No description provided.