Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(sse): a successful turn is never contentless (item 12)
Jack Rudenkoedd4ce940732
diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.tsindex d21d6851..b7f68eaa 100644--- a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts@@ -626,6 +626,35 @@ export function createStreamingResponseHandler(`[Streaming] Upstream finish_reason=${state.finishReason} → stop_reason=${stopReason} (${state.accumulatedText.length} chars produced)`);}++ // A turn that ended NORMALLY must carry at least one content block.+ // `stop_reason: "end_turn"` with an empty `content` array is not a+ // shape Anthropic's API produces, and a client that indexes the last+ // block, or renders the turn, has nothing to work with — the turn+ // reads as a success that delivered nothing, with no diagnostic.+ //+ // The test is `stop_reason === "end_turn"` rather than "the ending was+ // a success", because that single value already excludes every case+ // where emptiness is MEANINGFUL and must be preserved:+ // • "max_tokens" — reasoning consumed the whole budget and the turn+ // was cut off. `gemini-3.1-pro-or-maxtokens-empty.sse` is a real+ // capture of exactly this; it must not be papered over.+ // • "max_tokens" from a silent truncation (item 4) — same reasoning.+ // • "refusal" — the provider refused. Emptiness IS the answer.+ // • "tool_use" — a tool block was emitted, so it is not contentless.+ //+ // An EMPTY text block, not placeholder prose and not an error. Prose+ // would enter the conversation history as the assistant's words and be+ // replayed forever; an `error` would trigger the client's retry loop on+ // a deterministic outcome, which is adapters.md's stated reason for+ // keeping `max_tokens` over an error on the sibling case.+ if (stopReason === "end_turn" && !writer.anyBlockEmitted) {+ log(+ `[Streaming] Contentless turn error: end_turn with no content block emitted (finish_reason=${state.finishReason ?? "null"}) — emitting an empty text block`+ );+ writer.close(writer.openText());+ }+send("message_delta", {type: "message_delta",delta: { stop_reason: stopReason, stop_sequence: null },diff --git a/packages/cli/src/providers/probe-live.ts b/packages/cli/src/providers/probe-live.tsindex dc45c019..bd8f9d10 100644--- a/packages/cli/src/providers/probe-live.ts+++ b/packages/cli/src/providers/probe-live.ts@@ -679,8 +679,24 @@ function accountStreamEvent(rawEvent: string): {if (typeof text === "string" && text.length > 0) {contentDelta = true;textChars = text.length;- } else if (parsed?.type === "content_block_delta" || parsed?.type === "content_block_start") {+ } else if (parsed?.type === "content_block_delta") {contentDelta = true;+ } else if (parsed?.type === "content_block_start") {+ // A block that has only been OPENED is not output yet. Every `text` and+ // `thinking` block starts empty and is filled by the deltas above, which are+ // counted on their own — so counting the start as well only ever moves TTFT+ // earlier than the first token.+ //+ // It became load-bearing when openai-sse started emitting an EMPTY text block+ // for a turn that ended normally having produced nothing (so that `content`+ // is never an empty array, a shape Anthropic's API does not produce). That+ // block carries no delta, and counting it as visible output reported a model+ // that said nothing as a healthy link — which is the exact reading this+ // function exists to prevent.+ //+ // A `tool_use` start is the exception: the block start IS the call, and a+ // tool call is output.+ contentDelta = parsed.content_block?.type === "tool_use";}// Exact usage: Claude reports cumulative output_tokens on message_delta /