[GH-ISSUE #2766] [Issue]: Provider response & body-merge defects — Anthropic multi-block truncation, OpenAI Responses penalty 400, deepMergeRequestBody prototype pollution, empty-stream #1404

Closed
opened 2026-10-21 05:33:17 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2766

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Four serialization/response-handling defects: two cause hard failures or truncated output, one is a prototype-pollution security hole reachable via the agent customParameters salvage path, one silently returns blank output.

Sub-bug 1: Anthropic chat() drops all but the first text block (new-bug)

(a) The non-streaming branch yields json.content.find(c => c.type === 'text')?.text ?? '' — only the FIRST text block. Claude can return multiple text blocks (interleaved with thinking), so later text is silently discarded -> truncated responses. The parallel chatComplete correctly joins all text blocks, so the two paths disagree.
(b) Run an Anthropic generation with streaming OFF and interleaved-thinking that produces text/thinking/text segments (also the connection test-message path).
(c) anthropic.provider.ts:502-521 (line 512 the buggy .find); contrast chatComplete join at 342-345; reachable via generate.routes.ts:6292 (provider.chat with stream: input.streaming). Kind: new-bug.

Sub-bug 2: OpenAI Responses API sends penalties -> 400 (new-bug)

(a) buildResponsesBody emits frequency_penalty/presence_penalty (gated only by shouldSendPenaltyParams which only excludes xAI). The Responses API has no such params (VERIFIED: openai-node ResponseCreateParams contains zero penalty fields), so a non-zero preset penalty on a Responses-routed model (codex-*, *-codex, gpt-5.4 with effort none/unset) yields HTTP 400 'Unknown parameter' with NO salvage/retry — generation aborts.
(b) Use a codex / gpt-5.4 model with a preset that sets a non-zero frequency or presence penalty; generate.
(c) openai.provider.ts:1564-1576 (VERIFIED — penalty branch at 1572-1575 inside the !isNoTemperatureModel gate), 543-545 (shouldSendPenaltyParams), 553-562 (isNoTemperatureModel), 679-694 (routing); contrast valid Chat-Completions siblings 893-896/1138-1139. Kind: new-bug.

Sub-bug 3: Prototype pollution via deepMergeRequestBody + agent salvage path (security, new-bug)

(a) deepMergeRequestBody has no __proto__/constructor guard: a JSON-parsed customParameters with an own-enumerable __proto__ key recurses into target['__proto__'] === Object.prototype and mutates it in place. The happy path is protected (Zod z.record strips __proto__; mergeCustomParameters's spread drops it), but the AGENT path bypasses both: resolveConnectionCustomParameters -> parseStoredGenerationParameters SALVAGE branch copies customParameters RAW (VERIFIED at generate-route-utils.ts:965-966) when any sibling field fails full-schema validation, and that raw object flows verbatim into applyCustomParameters -> deepMergeRequestBody.
(b) Import/craft a connection whose defaultParameters.customParameters has __proto__:{...} plus one out-of-range sampler (to force the salvage branch); back an executed agent with it; run the agent.
(c) base-provider.ts:598-601 (applyCustomParameters), 713-723 (VERIFIED no guard); generate-route-utils.ts:965-967 (raw copy); agent-resolution.ts:124-126/192; agent-executor.ts:217-221/392. Kind: new-bug (security). Local single-user app, requires crafted import -> medium-high, not remote-critical.

Sub-bug 4: Anthropic streaming returns silently empty on blocked/empty completion (new-bug)

(a) The streaming loop yields text only on content_block_delta; if a stream finishes with no text delta (refusal/content-filter stop_reason with no text block) it returns usage-or-nothing with NO error. Gemini and the claude-subscription provider both throw a 'no content' terminal guard, so this is an error-surfacing asymmetry. Distinct from #2659 (which only covers explicit mid-stream type:error events).
(c) anthropic.provider.ts:523-612 (no terminal no-content guard at 606-612); contrast google.provider.ts:786-792 and claude-subscription.provider.ts:631-645. Kind: new-bug. Low frequency -> low/medium.

Suggested Fix

  • Anthropic chat(): join all text blocks (mirror chatComplete:342-345); optionally concat thinking blocks.
  • Responses: remove the penalty branch from buildResponsesBody (penalties are Chat-Completions only); optionally add a 400 'Unknown parameter' salvage-and-retry.
  • deepMergeRequestBody (and mergeCustomParameters): if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;; also validate the salvage-branch customParameters through Zod instead of copying raw.
  • Anthropic streaming: after the loop, if no text was emitted and no abort, throw a descriptive error including stop_reason (mirror Gemini/claude-subscription).

Template

  • Anthropic chat() returns full multi-block text
  • Responses path no longer 400s on penalties (verify codex/gpt-5.4 with non-zero penalty)
  • deepMergeRequestBody rejects proto/constructor/prototype; salvage path Zod-validated
  • Anthropic empty/blocked stream surfaces an error
  • pnpm check passes

Affected files

  • packages/server/src/services/llm/providers/anthropic.provider.ts
  • packages/server/src/services/llm/providers/openai.provider.ts
  • packages/server/src/services/llm/base-provider.ts
  • packages/server/src/routes/generate/generate-route-utils.ts
  • packages/server/src/services/generation/agent-resolution.ts

Audit provenance

Found by a multi-agent re-review of the connection/preset subsystem (editors UI + send-pipeline) against the merged staging tree (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified). The prior #2656-#2668 fixes were re-verified as HELD on their primary paths. Severity: HIGH.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2766 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Four serialization/response-handling defects: two cause hard failures or truncated output, one is a prototype-pollution security hole reachable via the agent customParameters salvage path, one silently returns blank output. ## Sub-bug 1: Anthropic chat() drops all but the first text block (new-bug) **(a)** The non-streaming branch yields `json.content.find(c => c.type === 'text')?.text ?? ''` — only the FIRST text block. Claude can return multiple text blocks (interleaved with thinking), so later text is silently discarded -> truncated responses. The parallel `chatComplete` correctly joins all text blocks, so the two paths disagree. **(b)** Run an Anthropic generation with streaming OFF and interleaved-thinking that produces text/thinking/text segments (also the connection test-message path). **(c)** anthropic.provider.ts:502-521 (line 512 the buggy `.find`); contrast chatComplete join at 342-345; reachable via generate.routes.ts:6292 (`provider.chat` with `stream: input.streaming`). Kind: new-bug. ## Sub-bug 2: OpenAI Responses API sends penalties -> 400 (new-bug) **(a)** `buildResponsesBody` emits `frequency_penalty`/`presence_penalty` (gated only by `shouldSendPenaltyParams` which only excludes xAI). The Responses API has no such params (VERIFIED: openai-node `ResponseCreateParams` contains zero penalty fields), so a non-zero preset penalty on a Responses-routed model (codex-*, *-codex, gpt-5.4 with effort none/unset) yields HTTP 400 'Unknown parameter' with NO salvage/retry — generation aborts. **(b)** Use a codex / gpt-5.4 model with a preset that sets a non-zero frequency or presence penalty; generate. **(c)** openai.provider.ts:1564-1576 (VERIFIED — penalty branch at 1572-1575 inside the `!isNoTemperatureModel` gate), 543-545 (shouldSendPenaltyParams), 553-562 (isNoTemperatureModel), 679-694 (routing); contrast valid Chat-Completions siblings 893-896/1138-1139. Kind: new-bug. ## Sub-bug 3: Prototype pollution via deepMergeRequestBody + agent salvage path (security, new-bug) **(a)** `deepMergeRequestBody` has no `__proto__`/`constructor` guard: a JSON-parsed `customParameters` with an own-enumerable `__proto__` key recurses into `target['__proto__'] === Object.prototype` and mutates it in place. The happy path is protected (Zod `z.record` strips `__proto__`; `mergeCustomParameters`'s spread drops it), but the AGENT path bypasses both: `resolveConnectionCustomParameters` -> `parseStoredGenerationParameters` SALVAGE branch copies `customParameters` RAW (VERIFIED at generate-route-utils.ts:965-966) when any sibling field fails full-schema validation, and that raw object flows verbatim into `applyCustomParameters` -> `deepMergeRequestBody`. **(b)** Import/craft a connection whose `defaultParameters.customParameters` has `__proto__:{...}` plus one out-of-range sampler (to force the salvage branch); back an executed agent with it; run the agent. **(c)** base-provider.ts:598-601 (applyCustomParameters), 713-723 (VERIFIED no guard); generate-route-utils.ts:965-967 (raw copy); agent-resolution.ts:124-126/192; agent-executor.ts:217-221/392. Kind: new-bug (security). Local single-user app, requires crafted import -> medium-high, not remote-critical. ## Sub-bug 4: Anthropic streaming returns silently empty on blocked/empty completion (new-bug) **(a)** The streaming loop yields text only on `content_block_delta`; if a stream finishes with no text delta (refusal/content-filter stop_reason with no text block) it returns usage-or-nothing with NO error. Gemini and the claude-subscription provider both throw a 'no content' terminal guard, so this is an error-surfacing asymmetry. Distinct from #2659 (which only covers explicit mid-stream `type:error` events). **(c)** anthropic.provider.ts:523-612 (no terminal no-content guard at 606-612); contrast google.provider.ts:786-792 and claude-subscription.provider.ts:631-645. Kind: new-bug. Low frequency -> low/medium. ## Suggested Fix - Anthropic chat(): join all text blocks (mirror chatComplete:342-345); optionally concat thinking blocks. - Responses: remove the penalty branch from `buildResponsesBody` (penalties are Chat-Completions only); optionally add a 400 'Unknown parameter' salvage-and-retry. - deepMergeRequestBody (and mergeCustomParameters): `if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;`; also validate the salvage-branch customParameters through Zod instead of copying raw. - Anthropic streaming: after the loop, if no text was emitted and no abort, throw a descriptive error including stop_reason (mirror Gemini/claude-subscription). ## Template - [ ] Anthropic chat() returns full multi-block text - [ ] Responses path no longer 400s on penalties (verify codex/gpt-5.4 with non-zero penalty) - [ ] deepMergeRequestBody rejects __proto__/constructor/prototype; salvage path Zod-validated - [ ] Anthropic empty/blocked stream surfaces an error - [ ] `pnpm check` passes --- ### Affected files - `packages/server/src/services/llm/providers/anthropic.provider.ts` - `packages/server/src/services/llm/providers/openai.provider.ts` - `packages/server/src/services/llm/base-provider.ts` - `packages/server/src/routes/generate/generate-route-utils.ts` - `packages/server/src/services/generation/agent-resolution.ts` ### Audit provenance Found by a multi-agent re-review of the connection/preset subsystem (editors UI + send-pipeline) against the merged `staging` tree (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified). The prior #2656-#2668 fixes were re-verified as HELD on their primary paths. Severity: **HIGH**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Fixed in PR #2747.\n\nWhat changed:\n- Anthropic non-stream chat now joins every returned text block instead of taking only the first one, while still forwarding thinking blocks.\n- OpenAI Responses body construction no longer sends frequency_penalty/presence_penalty, avoiding Responses API unknown-parameter failures.\n- customParameters merge paths now skip proto, constructor, and prototype keys, including the stored-parameter salvage path and final request-body deep merge.\n- Anthropic streaming now throws a descriptive error when the stream completes without any text and was not aborted, instead of silently returning blank output.\n\nDeferred pnpm/check validation until the end of the issue sweep per maintainer request.

<!-- gh-comment-id:4763384549 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed in PR #2747.\n\nWhat changed:\n- Anthropic non-stream chat now joins every returned text block instead of taking only the first one, while still forwarding thinking blocks.\n- OpenAI Responses body construction no longer sends frequency_penalty/presence_penalty, avoiding Responses API unknown-parameter failures.\n- customParameters merge paths now skip __proto__, constructor, and prototype keys, including the stored-parameter salvage path and final request-body deep merge.\n- Anthropic streaming now throws a descriptive error when the stream completes without any text and was not aborted, instead of silently returning blank output.\n\nDeferred pnpm/check validation until the end of the issue sweep per maintainer request.
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#1404
No description provided.