Skip to content

fix(runtime): stop reporting a truncated provider stream as a finished turn - #2297

Merged
Astro-Han merged 4 commits into
mainfrom
fix/runtime-truncated-stream-end-turn
Aug 6, 2026
Merged

Astro-Han merged 4 commits into
mainfrom
fix/runtime-truncated-stream-end-turn

Conversation

@Astro-Han

@Astro-Han Astro-Han commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

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 that end_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: completed with stopReason: end_turn while 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 with terminalEvent: false and errorClass: TypeError (undici's terminated). The runtime's own trace holds zero error and zero provider_retry events. It was caught only because the provider proxy noticed the terminal SSE event never arrived.

Refs #2245

Root cause

ModelAdapter.mapFinishReason mapped every unrecognized finish reason to end_turn. A truncated stream is indistinguishable from a clean one at that boundary because the SDK synthesizes a finish frame for it, and asking result.finishReason returns the same other a healthy stream can return.

What other actually means

The first cut of this fix treated other itself as the signal. Review found that fails complete answers: other is not a finish reason, it is the SDK declining to name one, and every provider package buckets an unrecognized finish_reason into it too. llama.cpp's eos_token, DeepSeek's insufficient_system_resource, an OpenAI-compatible stop_sequence — all other, 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]:

stream finishReason rawFinishReason
connection died mid-answer other undefined
provider said eos_token other eos_token

So translateChunk now forwards the provider's own spelling when the SDK has none — and only when it has none, because runtime-runner and the backend compare against 'tool-calls', a name only the SDK uses. A genuinely new reason arrives at mapFinishReason as 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' in ai-sdk-backend comes out of the catch path with an error event and a failed trace behind it, and the session's lastError and the request ledger are fed by exactly those. The truncation branch had neither: it ended the turn failed while the telemetry still read success. It now emits both — the same blindness this PR exists to remove.

Verification

  • npm run -w @maka/runtime test — 3271 pass, 0 fail
  • npm run build, npm run lint, npm run format:check — clean
  • does not call a truncated provider stream a finished turn drives a stream that delivers text and then ends without a finish frame; asserts stopReason === 'error' (not merely "not end_turn" — max_tokens would 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 for drives { unified: 'other', raw: 'eos_token' } and asserts end_turn with no error event. This is the regression the first cut would have shipped.
  • Not run: desktop E2E — this changes no renderer or main-process path

…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
Astro-Han marked this pull request as ready for review August 6, 2026 06:04
@Astro-Han
Astro-Han merged commit 29ea4da into main Aug 6, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant