[GH-ISSUE #2679] [Issue]: Anthropic trailing-assistant whitespace 400 beyond the prefill path (no-prefill case not covered by #2674) #1324

Closed
opened 2026-10-21 05:31:33 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @kolacheee on GitHub.

Summary

The trailing-whitespace fix from #2673 / #2674 lives only in the prefill helper (appendGenerationTailMessages), so it covers only the case where a prefill is configured. When no prefill is set, the trailing assistant message (from a depth-injected role:assistant section, or — under markdown/none wrap — the last chat-history assistant message) can still end in whitespace and is sent to Anthropic untrimmed, producing the same HTTP 400 ("final assistant message must not end with trailing whitespace") that surfaces to users as a refusal/block.

Expected behavior

Any final assistant turn sent to Anthropic should have its trailing whitespace stripped at serialization — not just the prefill — so a trailing-whitespace assistant message never triggers a 400.

Actual behavior

AnthropicProvider.chat() formats each message and, for a plain message, returns { role: m.role, content: m.content } with no trimEnd. The empty-content filter only uses .trim() to drop empty messages (it doesn't mutate surviving content), and mergeConsecutiveMessages concatenates without re-trimming. The assembler's final filter likewise only checks emptiness. So a trailing assistant message ending in \n/spaces reaches the Messages API untrimmed → 400.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/services/llm/providers/anthropic.provider.ts:377 — plain-message branch returns content: m.content (no trimEnd); the empty filter at :334 uses m.content?.trim() only to drop; mergeConsecutiveMessages (:558-563) concatenates without trimming. The 400 is thrown at ~:445-447.
  • The #2673/#2674 fix is only in packages/server/src/routes/generate/generate-route-utils.ts:547 (the prefill push). With no prefill, that helper pushes nothing, so the trailing message is whatever the assembler emitted.
  • The trailing message can be: (a) a depth-injected role:assistant entry placed at history end (assembler.ts:457-465 injectAtDepth, anchored to history bounds) whose macro-resolved content ends in whitespace; or (b) under non-default wrapFormat markdown/none, the last chat-history assistant message ending in \n. (Under the default xml wrap, the last history turn is wrapped as <last_message>…</last_message> with no trailing whitespace, so (b) only fires for markdown/none.)

Steps to reproduce

  1. Preset with assistantPrefill empty (so no tail assistant is appended).
  2. Either (a) a depth-0 role:assistant injected section / lorebook entry whose content ends in a newline, or (b) wrapFormat = markdown/none and the most recent chat-history message is an assistant message ending in \n.
  3. Generate via the Anthropic streaming chat() path (not claude-subscription). The request's final assistant message ends in whitespace → Anthropic returns 400 → surfaces as a refusal/block. The same preset works in SillyTavern (which sanitizes the trailing turn).

Suggested fix

Trim the trailing whitespace of the final assistant message at the point of serialization in chat() (and chatComplete() / formatAnthropicPayloadMessages), not only for the prefill — e.g. if the last merged message has role === "assistant", set its content to content.trimEnd(). Centralize so every trailing-assistant surface (prefill, depth-injected assistant, merged assistant, history assistant) is covered. This generalizes the #2673/#2674 fix.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the prompt-assembly / prefill path; a generalization of the already-fixed prefill trailing-whitespace bug (#2673 / PR #2674). Narrower blast radius than the prefill case (requires a non-default wrap or a trailing-whitespace depth-0 assistant entry), but the mechanism and a realistic trigger are both present in the code.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2679 Originally assigned to: @kolacheee on GitHub. ## Summary The trailing-whitespace fix from #2673 / #2674 lives only in the prefill helper (`appendGenerationTailMessages`), so it covers only the case where a prefill is configured. When **no prefill** is set, the trailing assistant message (from a depth-injected `role:assistant` section, or — under markdown/none wrap — the last chat-history assistant message) can still end in whitespace and is sent to Anthropic **untrimmed**, producing the same HTTP 400 ("final assistant message must not end with trailing whitespace") that surfaces to users as a refusal/block. ## Expected behavior Any final assistant turn sent to Anthropic should have its trailing whitespace stripped at serialization — not just the prefill — so a trailing-whitespace assistant message never triggers a 400. ## Actual behavior `AnthropicProvider.chat()` formats each message and, for a plain message, returns `{ role: m.role, content: m.content }` with no `trimEnd`. The empty-content filter only uses `.trim()` to *drop* empty messages (it doesn't mutate surviving content), and `mergeConsecutiveMessages` concatenates without re-trimming. The assembler's final filter likewise only checks emptiness. So a trailing assistant message ending in `\n`/spaces reaches the Messages API untrimmed → 400. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/services/llm/providers/anthropic.provider.ts:377` — plain-message branch returns `content: m.content` (no `trimEnd`); the empty filter at ~`:334` uses `m.content?.trim()` only to drop; `mergeConsecutiveMessages` (~`:558-563`) concatenates without trimming. The 400 is thrown at ~`:445-447`. - The #2673/#2674 fix is only in `packages/server/src/routes/generate/generate-route-utils.ts:547` (the prefill push). With no prefill, that helper pushes nothing, so the trailing message is whatever the assembler emitted. - The trailing message can be: (a) a depth-injected `role:assistant` entry placed at history end (`assembler.ts:457-465` `injectAtDepth`, anchored to history bounds) whose macro-resolved content ends in whitespace; or (b) under non-default `wrapFormat` markdown/none, the last chat-history assistant message ending in `\n`. (Under the default `xml` wrap, the last history turn is wrapped as `<last_message>…</last_message>` with no trailing whitespace, so (b) only fires for markdown/none.) ## Steps to reproduce 1. Preset with `assistantPrefill` empty (so no tail assistant is appended). 2. Either (a) a depth-0 `role:assistant` injected section / lorebook entry whose content ends in a newline, or (b) `wrapFormat` = markdown/none and the most recent chat-history message is an assistant message ending in `\n`. 3. Generate via the Anthropic streaming `chat()` path (not claude-subscription). The request's final assistant message ends in whitespace → Anthropic returns 400 → surfaces as a refusal/block. The same preset works in SillyTavern (which sanitizes the trailing turn). ## Suggested fix Trim the trailing whitespace of the **final assistant message at the point of serialization** in `chat()` (and `chatComplete()` / `formatAnthropicPayloadMessages`), not only for the prefill — e.g. if the last merged message has `role === "assistant"`, set its content to `content.trimEnd()`. Centralize so every trailing-assistant surface (prefill, depth-injected assistant, merged assistant, history assistant) is covered. This generalizes the #2673/#2674 fix. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the prompt-assembly / prefill path; a generalization of the already-fixed prefill trailing-whitespace bug (#2673 / PR #2674). Narrower blast radius than the prefill case (requires a non-default wrap or a trailing-whitespace depth-0 assistant entry), but the mechanism and a realistic trigger are both present in the code. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:33 +07:00
Author
Owner

@kolacheee commented on GitHub (Jun 20, 2026):

assign me pls will update #2674 to include a fix for this

<!-- gh-comment-id:4760288924 --> @kolacheee commented on GitHub (Jun 20, 2026): assign me pls will update #2674 to include a fix for this
Author
Owner

@kolacheee commented on GitHub (Jun 20, 2026):

Fixed by extending #2674 (owner: @kolacheee). Rather than a separate PR, the no-prefill case is folded into the existing trailing-whitespace PR: a new trimTrailingAssistantWhitespace() in anthropic.provider.ts strips the trailing edge of the final assistant message at serialization, applied in both chat() (after mergeConsecutiveMessages) and chatComplete() (before formatAnthropicPayloadMessages). That centralizes the guard so every trailing-assistant surface — prefill, depth-injected, merged, history — is covered, generalizing the #2673/#2674 prefill fix as suggested.

The PR's Closes #2679 will auto-close this once #2674 merges to staging. pnpm check passes; the no-prefill repro paths are listed as manual checks on the PR.

<!-- gh-comment-id:4760321895 --> @kolacheee commented on GitHub (Jun 20, 2026): Fixed by extending #2674 (owner: @kolacheee). Rather than a separate PR, the no-prefill case is folded into the existing trailing-whitespace PR: a new `trimTrailingAssistantWhitespace()` in `anthropic.provider.ts` strips the trailing edge of the final assistant message at serialization, applied in both `chat()` (after `mergeConsecutiveMessages`) and `chatComplete()` (before `formatAnthropicPayloadMessages`). That centralizes the guard so every trailing-assistant surface — prefill, depth-injected, merged, history — is covered, generalizing the #2673/#2674 prefill fix as suggested. The PR's `Closes #2679` will auto-close this once #2674 merges to `staging`. `pnpm check` passes; the no-prefill repro paths are listed as manual checks on the PR.
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#1324
No description provided.