Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(adapters): end a client-cancelled stream without a truncation verdict
Jack Rudenkoeaff40c21f8b
diff --git a/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.tsindex 47f08502..8e536b32 100644--- a/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts@@ -135,6 +135,8 @@ export function createAnthropicPassthroughStream(const encoder = new TextEncoder();const decoder = new TextDecoder();let isClosed = false;+ // Set by `cancel()` only: the client went away (see `endCancelled`).+ let cancelled = false;let lastActivity = Date.now();let pingInterval: ReturnType<typeof setInterval> | null = null;@@ -288,6 +290,24 @@ export function createAnthropicPassthroughStream(}};+ /**+ * The end of a stream the CLIENT cancelled: the upstream reader was cancelled+ * under the loop, so its read ended (or threw) without upstream ending the+ * turn. No turn-end hook, no stream-error log; usage only when upstream sent+ * some (message_start carries input_tokens, so a started turn usually has it).+ */+ const endCancelled = (): void => {+ const sawUsage = inputTokens > 0 || outputTokens > 0;+ log(+ `[AnthropicSSE] Client cancelled the stream for ${opts.modelName}; usage ${sawUsage ? "reported" : "not received, not recorded"}`+ );+ if (sawUsage) opts.onTokenUpdate?.(inputTokens, outputTokens);+ if (pingInterval) {+ clearInterval(pingInterval);+ pingInterval = null;+ }+ };+const upstream: UpstreamReaderSlot = {};return c.body(new ReadableStream(@@ -632,6 +652,11 @@ export function createAnthropicPassthroughStream(}}+ if (cancelled) {+ endCancelled();+ return;+ }+log(`[AnthropicSSE] Stream complete for ${opts.modelName}: ${totalLines} lines, ${textChunks} text chunks, ${toolUseBlocks} tool_use blocks, stop_reason=${stopReason}${filterThinking ? `, filtered ${thinkingBlocksSuppressed} thinking blocks` : ""}`);@@ -651,6 +676,10 @@ export function createAnthropicPassthroughStream(controller.close();}} catch (e) {+ if (cancelled) {+ endCancelled();+ return;+ }log(`[AnthropicSSE] Stream error: ${e}`);// The upstream socket dropped mid-answer (Z.AI, MiniMax and Kimi all do// this under load). Closing the controller here without a terminal event@@ -677,6 +706,7 @@ export function createAnthropicPassthroughStream(// Downstream went away: cancelEndsUpstreamWork cancels the upstream reader// after this and resolves only once the read loop above has settled.cancel() {+ cancelled = true;isClosed = true;if (pingInterval) {clearInterval(pingInterval);diff --git a/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts b/packages/cli/src/handlers/shared/stream-parsers/devin-connect.tsindex 3081fb07..034a258f 100644--- a/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts@@ -152,6 +152,8 @@ export function createDevinConnectStream(): Response {const encoder = new TextEncoder();let isClosed = false;+ // Set by `cancel()` only: the client went away (see finalize).+ let cancelled = false;let pingInterval: ReturnType<typeof setInterval> | null = null;const upstream: UpstreamReaderSlot = {};@@ -264,6 +266,22 @@ export function createDevinConnectStream(if (finalized) return;finalized = true;+ // The client cancelled: the read ended because claudish cancelled the+ // upstream reader, not because Devin sent an end-of-stream frame. No+ // turn-end hook, no stream-error log, and no zero-token update; usage+ // only when Devin sent it.+ if (cancelled) {+ log(+ `[DevinConnect] Client cancelled the stream for ${opts.modelName} (loop ended: ${reason}); usage ${sawUsage ? "reported" : "not received, not recorded"}`+ );+ if (sawUsage) opts.onTokenUpdate?.(inputTokens, outputTokens);+ if (pingInterval) {+ clearInterval(pingInterval);+ pingInterval = null;+ }+ return;+ }+closeCurrentTool();closeThinking();closeText();@@ -530,6 +548,7 @@ export function createDevinConnectStream(// Downstream went away: cancelEndsUpstreamWork cancels the upstream reader// after this and resolves only once the read loop above has settled.cancel() {+ cancelled = true;isClosed = true;if (pingInterval) {clearInterval(pingInterval);diff --git a/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.tsindex 8521063d..768dfbaa 100644--- a/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts@@ -58,6 +58,8 @@ export function createGeminiSseStream(const encoder = new TextEncoder();const decoder = new TextDecoder();let isClosed = false;+ // Set by `cancel()` only: the client went away (see finalize).+ let cancelled = false;let pingInterval: ReturnType<typeof setInterval> | null = null;const upstream: UpstreamReaderSlot = {};@@ -131,6 +133,23 @@ export function createGeminiSseStream(}finalized = true;+ // The client cancelled: the upstream reader was cancelled under the loop,+ // so its read ended without the provider ending the turn. No turn-end+ // hook and no zero-token update; usage only when the provider sent it.+ if (cancelled) {+ try {+ log(+ `[GeminiSSE] Client cancelled the stream for ${opts.modelName} (loop ended: ${reason}); usage ${usage ? "reported" : "not received, not recorded"}`+ );+ if (usage && opts.onTokenUpdate) {+ opts.onTokenUpdate(usage.promptTokenCount || 0, usage.candidatesTokenCount || 0);+ }+ } finally {+ teardown();+ }+ return;+ }+try {if (thinkingStarted) {send("content_block_stop", { type: "content_block_stop", index: thinkingIdx });@@ -394,6 +413,7 @@ export function createGeminiSseStream(// Downstream went away: cancelEndsUpstreamWork cancels the upstream reader// after this and resolves only once the read loop above has settled.cancel() {+ cancelled = true;isClosed = true;if (pingInterval) {clearInterval(pingInterval);diff --git a/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts b/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.tsindex 8753ae9f..c3071b73 100644--- a/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts@@ -32,6 +32,8 @@ export function createOllamaJsonlStream(const encoder = new TextEncoder();const decoder = new TextDecoder();let isClosed = false;+ // Set by `cancel()` only: the client went away (see finalize).+ let cancelled = false;let pingInterval: ReturnType<typeof setInterval> | null = null;const upstream: UpstreamReaderSlot = {};@@ -100,6 +102,21 @@ export function createOllamaJsonlStream(}finalized = true;+ // The client cancelled: the read ended because claudish cancelled the+ // upstream reader, not because Ollama finished. Usage arrives only on the+ // `done` chunk, which finalizes before any cancel can, so nothing is+ // recorded here: no zero-token update over the real context figure.+ if (cancelled) {+ try {+ log(+ `[OllamaJSONL] Client cancelled the stream for ${opts.modelName} (loop ended: ${reason})`+ );+ } finally {+ teardown();+ }+ return;+ }+try {if (textStarted) {send("content_block_stop", { type: "content_block_stop", index: 0 });@@ -188,13 +205,14 @@ export function createOllamaJsonlStream(// Stream ended without done=truefinalize("done");} catch (error) {- log(`[OllamaJSONL] Stream error: ${error}`);+ if (!cancelled) log(`[OllamaJSONL] Stream error: ${error}`);finalize("error", String(error));}},// Downstream went away: cancelEndsUpstreamWork cancels the upstream reader// after this and resolves only once the read loop above has settled.cancel() {+ cancelled = true;isClosed = true;if (pingInterval) {clearInterval(pingInterval);diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.tsindex 1320533b..4a33a703 100644--- a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts@@ -302,6 +302,9 @@ export function createStreamingResponseHandler(};log(`[Streaming] ===== HANDLER STARTED for ${target} =====`);let isClosed = false;+ // Set by `cancel()` only: the client went away. `isClosed` alone cannot say+ // that, because teardown sets it on every normal end too.+ let cancelled = false;let ping: NodeJS.Timeout | null = null;const encoder = new TextEncoder();const decoder = new TextDecoder();@@ -440,6 +443,24 @@ export function createStreamingResponseHandler(}};+ /** Report the usage the provider sent. Only called when `state.usage` is set. */+ const reportProviderUsage = () => {+ if (!onTokenUpdate || !state.usage) return;+ log(+ `[Streaming] Final usage: prompt=${state.usage.prompt_tokens || 0}, completion=${state.usage.completion_tokens || 0}`+ );+ // FIRST ARGUMENT STAYS `prompt_tokens`: the tracker's input number+ // is the context-occupancy figure the status line renders and the+ // billing baseline the delta strategy compares against. The split+ // goes in the third argument, where only the cost arithmetic sees+ // it.+ const costSplit = splitPromptTokens(state.usage);+ onTokenUpdate(state.usage.prompt_tokens || 0, state.usage.completion_tokens || 0, {+ cacheReadTokens: costSplit.cacheReadTokens,+ cacheCreationTokens: costSplit.cacheCreationTokens,+ });+ };+const finalize = async (reason: string, err?: string) => {// A second call still has to tear down: the first may have thrown// before reaching its own `finally`.@@ -449,6 +470,23 @@ export function createStreamingResponseHandler(}state.finalized = true;+ // The client cancelled: the upstream reader was cancelled under the+ // loop, so its read resolved `done` (or threw) without the provider+ // having ended anything. Not a truncation and not a failure, and the+ // user abandoned the turn, so no ending classification, no estimate,+ // no turn-end hook. Usage is recorded only when the provider sent it.+ if (cancelled) {+ try {+ log(+ `[Streaming] Client cancelled the stream for ${target} (loop ended: ${reason}); usage ${state.usage ? "reported" : "not received, not recorded"}`+ );+ reportProviderUsage();+ } finally {+ teardown();+ }+ return;+ }+try {// Release anything the think-tag splitter is still holding — at most// seven characters of a tag that never completed. Runs FIRST, before@@ -804,23 +842,7 @@ export function createStreamingResponseHandler(// Update token counts - use actual usage if available, otherwise estimateif (onTokenUpdate) {if (state.usage) {- log(- `[Streaming] Final usage: prompt=${state.usage.prompt_tokens || 0}, completion=${state.usage.completion_tokens || 0}`- );- // FIRST ARGUMENT STAYS `prompt_tokens`: the tracker's input number- // is the context-occupancy figure the status line renders and the- // billing baseline the delta strategy compares against. The split- // goes in the third argument, where only the cost arithmetic sees- // it.- const costSplit = splitPromptTokens(state.usage);- onTokenUpdate(- state.usage.prompt_tokens || 0,- state.usage.completion_tokens || 0,- {- cacheReadTokens: costSplit.cacheReadTokens,- cacheCreationTokens: costSplit.cacheCreationTokens,- }- );+ reportProviderUsage();} else {// Estimate tokens for local models that don't return usage data// Rough estimate: ~4 characters per token@@ -1239,6 +1261,7 @@ export function createStreamingResponseHandler(// Downstream went away: cancelEndsUpstreamWork cancels the upstream reader// after this and resolves only once the read loop above has settled.cancel() {+ cancelled = true;isClosed = true;if (ping) clearInterval(ping);},