mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3034] [Issue]: Non-streaming Anthropic chat() drops stop_reason, so max_tokens truncation is reported as a clean "stop" #1602
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#1602
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 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3034
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
On the non-streaming Anthropic path, a response truncated at the output-token limit is indistinguishable from a naturally completed one. The non-streaming branch of
chat()parses content and usage but never readsjson.stop_reason, so the returned usage has nofinishReason. The basechatComplete()wrapper then falls back to reporting"stop"even when Anthropic actually returnedstop_reason: "max_tokens"(or"refusal"/"pause_turn"). Downstream length-aware logic — schedule parsing, agent execution, Professor Mari's "hit the output limit" warning — cannot tell the response was cut off.How to reproduce (try it yourself)
chatCompletecall — e.g. weekly schedule generation (schedule.service.ts:153callsprovider.chatComplete([...], { model, temperature, maxTokens })with nostream, noonToken). BasechatCompletecomputesuseStream = options.stream ?? !!options.onToken = false, so it callsthis.chat({ ...options, stream:false }), hitting the buggy branch.maxTokenswith a long generation so the model hitsmax_tokens.You'll see:
result.finishReason === "stop"despite the content being truncated mid-output; schedule JSON parsing fails with no truncation signal.Expected:
result.finishReason === "length", matching the streaming branch and the tool path.Where it is
packages/server/src/services/llm/providers/anthropic.provider.ts— the non-streaming branch ofchat()(lines 514-536): the inline JSON type omitsstop_reason, and the returned usage object (lines 530-534) has nofinishReason. Correct handling already exists at line 367 (tool-pathchatComplete) and lines 599-600 (streaming branch). Fallback that masks it:base-provider.ts:658. Reachable caller:packages/server/src/services/conversation/schedule.service.ts:153.What's happening
The non-streaming branch builds usage only from
json.usage.input_tokens/output_tokensand never inspectsjson.stop_reason, so nofinishReasonis propagated. Every other Anthropic path in the file normalizesstop_reason, making this branch an oversight.Suggested fix
In the non-streaming branch of
chat():stop_reason?: string.finishReasonto the returned usage object:This matches the in-file precedent (line 367 tool path, line 600 streaming) and resolves the truncation-as-clean-stop bug. Regression avoided: do NOT fabricate a
finishReason-only usage object on thejson.usage-absent path. Anthropic's Messages API always returnsusageon a successful response, so that path is effectively unreachable; returning a zeroed-tokenLLMUsagethere would feed fake 0/0 token counts into agent token accounting (agent-executor.ts:448) and telemetry. Keep the existing barereturn;there.normalizeAnthropicFinishReasonalready acceptsstring|null|undefined, so an absentstop_reasonyields"stop"— identical to today's fallback.Related: one of a family of provider-level "finish/stop reason dropped → truncation reported as a clean stop" issues filed together (Gemini non-streaming, Anthropic non-streaming, OpenAI streaming, ChatGPT-subscription streaming). Same root-cause class, separate per-provider fix.
AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Claiming this for the current staging sweep in draft PR #3053 (branch
codex/issue-sweep-3033-3052).@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3053 on branch
codex/issue-sweep-3033-3052(commitbfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.