mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1324
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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-injectedrole:assistantsection, 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 notrimEnd. The empty-content filter only uses.trim()to drop empty messages (it doesn't mutate surviving content), andmergeConsecutiveMessagesconcatenates 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 returnscontent: m.content(notrimEnd); the empty filter at:334usesm.content?.trim()only to drop;mergeConsecutiveMessages(:558-563) concatenates without trimming. The 400 is thrown at ~:445-447.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.role:assistantentry placed at history end (assembler.ts:457-465injectAtDepth, anchored to history bounds) whose macro-resolved content ends in whitespace; or (b) under non-defaultwrapFormatmarkdown/none, the last chat-history assistant message ending in\n. (Under the defaultxmlwrap, 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
assistantPrefillempty (so no tail assistant is appended).role:assistantinjected 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.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()(andchatComplete()/formatAnthropicPayloadMessages), not only for the prefill — e.g. if the last merged message hasrole === "assistant", set its content tocontent.trimEnd(). Centralize so every trailing-assistant surface (prefill, depth-injected assistant, merged assistant, history assistant) is covered. This generalizes the #2673/#2674 fix.Environment
b826f8f4)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:
@kolacheee commented on GitHub (Jun 20, 2026):
assign me pls will update #2674 to include a fix for this
@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()inanthropic.provider.tsstrips the trailing edge of the final assistant message at serialization, applied in bothchat()(aftermergeConsecutiveMessages) andchatComplete()(beforeformatAnthropicPayloadMessages). 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 #2679will auto-close this once #2674 merges tostaging.pnpm checkpasses; the no-prefill repro paths are listed as manual checks on the PR.