Repository navigation
fix(runtime): stop reporting a truncated provider stream as a finished turn - #2297
Merged
Merged
Conversation
…d turn An upstream that drops the connection mid-answer yields no error part and throws nothing; the SDK reports `other`/`unknown` and the adapter's tolerant default called that `end_turn`. The turn then completed with a half-finished answer and no error anywhere — a benchmark cell recorded `status: completed` while its agent was still mid-task, caught only because the proxy noticed the terminal SSE event never arrived. A genuinely new provider reason still falls through to the tolerant default; these two are not new reasons, they are the SDK declining to name one.
…name for Review of the first cut found it fails complete answers: `other` is not a finish reason, it is the SDK declining to name one, and an unrecognized `finish_reason` from an OpenAI-compatible provider lands in the same bucket as a connection that died mid-answer. Forward the provider's own spelling when the SDK has none, so only a genuinely unnamed stop is an error. A failed terminal now also emits an error event and a failed trace, like every other one. Ending the turn failed while `lastError` stays empty and the request ledger reads `success` is the same blindness this fixes.
A content filter is the provider stopping the stream on its own policy, not a stop nothing named — reporting both as "ended without finishing" describes one of them wrongly. The reason was also written back to the accumulator it had just been read from, which nothing reads again and which would have carried a settled fallback into a later stream that had no finish event of its own.
The step's usage record kept the SDK's unified name while the terminal read the merged one, so the record and the outcome named different reasons for exactly the stream this branch exists to tell apart. Also covers the content-filter terminal, whose error event this branch is what introduced, and renames the accumulator to what it holds — the reason the stream events carried, not the provider's raw one.
Astro-Han
marked this pull request as ready for review
August 6, 2026 06:04
This was referenced Aug 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
An upstream that drops the SSE connection mid-answer yields no error part and throws nothing. The SDK reports the stop as
other, and the adapter's tolerant default called thatend_turn— so the turn completed with a half-finished answer, no error event, and no retry. Nothing downstream could tell it apart from a model that had finished speaking.This surfaced in the #2245 harness comparison. One Terminal-Bench cell recorded
status: completedwithstopReason: end_turnwhile its agent was mid-task: its last words were "Let me look at the top region structure in detail", and 5.5 seconds later the runtime declared the turn done. The proxy telemetry for that cell shows 105 completed requests and a 106th withterminalEvent: falseanderrorClass: TypeError(undici'sterminated). The runtime's own trace holds zero error and zeroprovider_retryevents. It was caught only because the provider proxy noticed the terminal SSE event never arrived.Refs #2245
Root cause
ModelAdapter.mapFinishReasonmapped every unrecognized finish reason toend_turn. A truncated stream is indistinguishable from a clean one at that boundary because the SDK synthesizes afinishframe for it, and askingresult.finishReasonreturns the sameothera healthy stream can return.What
otheractually meansThe first cut of this fix treated
otheritself as the signal. Review found that fails complete answers:otheris not a finish reason, it is the SDK declining to name one, and every provider package buckets an unrecognizedfinish_reasoninto it too. llama.cpp'seos_token, DeepSeek'sinsufficient_system_resource, an OpenAI-compatiblestop_sequence— allother, all ordinary finished turns, all would have been reported as failed.The SDK does keep the distinction, in a field the adapter was discarding. Verified against the pinned
[email protected]:finishReasonrawFinishReasonotherundefinedeos_tokenothereos_tokenSo
translateChunknow forwards the provider's own spelling when the SDK has none — and only when it has none, becauseruntime-runnerand the backend compare against'tool-calls', a name only the SDK uses. A genuinely new reason arrives atmapFinishReasonas itself and takes the tolerant default, exactly as before. Only a stop that nobody named is an error.The backend reads the same stream-observed value it already prefers for telemetry, instead of the settled promise that carries only the unified enum, so a turn's outcome and its record can no longer disagree about why the stream ended.
The failure is no longer silent
Every other
stopReason: 'error'inai-sdk-backendcomes out of the catch path with an error event and a failed trace behind it, and the session'slastErrorand the request ledger are fed by exactly those. The truncation branch had neither: it ended the turn failed while the telemetry still readsuccess. It now emits both — the same blindness this PR exists to remove.Verification
npm run -w @maka/runtime test— 3271 pass, 0 failnpm run build,npm run lint,npm run format:check— cleandoes not call a truncated provider stream a finished turndrives a stream that delivers text and then ends without a finish frame; assertsstopReason === 'error'(not merely "notend_turn" —max_tokenswould also satisfy that and still record the turn completed) and that an error event accompanies it. Verified red before the fix.keeps a turn the provider named its own reason fordrives{ unified: 'other', raw: 'eos_token' }and assertsend_turnwith no error event. This is the regression the first cut would have shipped.