Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(adapters): record a client-cancelled turn as cancelled in stats

Jack Rudenko06e8a9f8bb30
diff --git a/ai-docs/architecture/adapters.md b/ai-docs/architecture/adapters.md
index 7a867662..6c04447e 100644
--- a/ai-docs/architecture/adapters.md
+++ b/ai-docs/architecture/adapters.md
@@ -858,7 +858,7 @@ before its fetch (`NativeHandler.handle`), so an ordinary native turn's lease is
| Request | Installed by | Released when |
|---|---|---|
-| Every session-app route | `app.use("*", …)` (`proxy-server.ts:793`) sets `SIGNING_SECRETS_KEY` | the response body is fully read, cancelled or errors; at once for no body (`releaseWhenBodyEnds`, `handlers/shared/call-context.ts`). A cancel releases only after the parser has cancelled its upstream reader AND its read loop has settled (`cancelEndsUpstreamWork`, `stream-parsers/upstream-cancel.ts`): a flag-only parser cancel left the loop reading and logging frames after the release. A cancel owns the release once it has begun: cancelling resolves an outstanding read with `done` before the source's cancel settles, so `releaseWhenBodyEnds` sets `cancelling` before `reader.cancel()` and a pull that wakes during it never releases. A parser's `cancel()` sets its own `cancelled` flag, and its finalize then writes one neutral "Client cancelled the stream" line, with no ending classification, no turn-end hook and a token update only for usage the provider sent. On a throw that reaches the middleware (only when an inner `onError` itself threw: hono's compose answers a route's Error inside `next()`), the error's message and stack are redacted against this request's own set before the release |
+| Every session-app route | `app.use("*", …)` (`proxy-server.ts:793`) sets `SIGNING_SECRETS_KEY` | the response body is fully read, cancelled or errors; at once for no body (`releaseWhenBodyEnds`, `handlers/shared/call-context.ts`). A cancel releases only after the parser has cancelled its upstream reader AND its read loop has settled (`cancelEndsUpstreamWork`, `stream-parsers/upstream-cancel.ts`): a flag-only parser cancel left the loop reading and logging frames after the release. A cancel owns the release once it has begun: cancelling resolves an outstanding read with `done` before the source's cancel settles, so `releaseWhenBodyEnds` sets `cancelling` before `reader.cancel()` and a pull that wakes during it never releases. A parser's `cancel()` sets its own `cancelled` flag, and its finalize then writes one neutral "Client cancelled the stream" line, with no ending classification, no turn-end hook and a token update only for usage the provider sent. That token update still fires `ComposedHandler`'s stats callback, so the handler wraps its streamed body in `notifyOnDownstreamCancel` (`stream-parsers/upstream-cancel.ts`), which flags the cancel BEFORE forwarding it; the record of a cancelled turn carries `success: false`, `error_class: "cancelled"`, `error_code: "client_cancelled"` (the existing error pair, no new `StatsEvent` field), and a cancelled turn without provider usage records nothing. On a throw that reaches the middleware (only when an inner `onError` itself threw: hono's compose answers a route's Error inside `next()`), the error's message and stack are redacted against this request's own set before the release |
| An advisor panel call | the panel gateway (`handlers/advisor-panel-gateway.ts`) | when the call's work settles, after the body was read and redacted; a timed-out call's aborted work can still log |
| A Claude advisor call | `runClaudeAdvisorCall`, for its own headers | when it returns (the login value is also on the main request's lease) |
| A Context nothing installed (tests) | `signingSecretsOf` (`call-context.ts:57`) | never: retention costs redaction, never exposure |
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index c00127e7..6788325d 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -162,6 +162,7 @@ import {
resolveStallPolicy,
} from "./shared/stream-parsers/openai-responses-sse.js";
import { createStreamingResponseHandler } from "./shared/stream-parsers/openai-sse.js";
+import { notifyOnDownstreamCancel } from "./shared/stream-parsers/upstream-cancel.js";
import { TokenTracker, type UsageCacheDetail } from "./shared/token-tracker.js";
import {
type ConnectionErrorInfo,
@@ -2841,6 +2842,11 @@ export class ComposedHandler implements ModelHandler {
// parser reports them via onApiError; capture it here so the turn is recorded
// as a failure, not a success. Per-request local — safe under concurrency.
let streamApiError: { code: string; message: string } | null = null;
+ // Set when Claude Code cancels the response body. A parser's cancelled
+ // finalize still reports usage the provider had sent (what was billed is
+ // billed), which fires onStreamComplete; this keeps that record from
+ // reading as a completed turn. Per-request local, like streamApiError.
+ let clientCancelled = false;
// Legs 2..n of a Responses turn: a degenerate `parallel` wrapper is retried
// inside the same downstream message (responses-turn.ts). Built from THIS
@@ -2947,8 +2953,11 @@ export class ComposedHandler implements ModelHandler {
provider_name: this.provider.name,
stream_format: this.provider.streamFormat,
latency_ms: latencyMs,
- success: streamApiError === null,
+ success: streamApiError === null && !clientCancelled,
http_status: httpStatus,
+ // A turn the client abandoned is not a completed one; the existing
+ // error pair marks it, so no new StatsEvent field is needed.
+ ...(clientCancelled ? { error_class: "cancelled", error_code: "client_cancelled" } : {}),
input_tokens: this.tokenTracker.getInputTokens(),
output_tokens: this.tokenTracker.getOutputTokens(),
estimated_cost: this.tokenTracker.getTotalCost(),
@@ -2978,22 +2987,27 @@ export class ComposedHandler implements ModelHandler {
}
};
- const streamed = this.handleStream(
- c,
- response,
- adapter,
- claudeRequest,
- // Decided from the binding this request was finally signed with (a 401
- // re-sign replaced it above); every leg of a Responses turn signs on
- // that same arm, or the leg issuer refuses it.
- this.signedBilling(binding),
- toolNameMap,
- onStreamComplete,
- (code, message) => {
- streamApiError = { code, message };
- },
- behaviorSession,
- responsesTurn
+ const streamed = notifyOnDownstreamCancel(
+ this.handleStream(
+ c,
+ response,
+ adapter,
+ claudeRequest,
+ // Decided from the binding this request was finally signed with (a 401
+ // re-sign replaced it above); every leg of a Responses turn signs on
+ // that same arm, or the leg issuer refuses it.
+ this.signedBilling(binding),
+ toolNameMap,
+ onStreamComplete,
+ (code, message) => {
+ streamApiError = { code, message };
+ },
+ behaviorSession,
+ responsesTurn
+ ),
+ () => {
+ clientCancelled = true;
+ }
);
// A client that did not ask for a stream gets the single JSON message the
diff --git a/packages/cli/src/handlers/shared/stream-parsers/upstream-cancel.ts b/packages/cli/src/handlers/shared/stream-parsers/upstream-cancel.ts
index 33667322..4425803f 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/upstream-cancel.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/upstream-cancel.ts
@@ -72,3 +72,54 @@ export function cancelEndsUpstreamWork<T>(
},
};
}
+
+/**
+ * `response` with `onCancel` called when its body is cancelled downstream
+ * (Claude Code went away), BEFORE the cancel is forwarded to the source. A
+ * parser's cancelled finalize still reports the usage the provider sent, which
+ * fires the handler's stats callback; this flag is how that callback tells a
+ * client-cancelled turn from a completed one. Same status, headers and bytes,
+ * pulled one chunk at a time (`highWaterMark: 0`); a response with no body is
+ * returned as is.
+ */
+export function notifyOnDownstreamCancel(response: Response, onCancel: () => void): Response {
+ const source = response.body;
+ if (!source) return response;
+ const reader = source.getReader();
+ let cancelled = false;
+ const stream = new ReadableStream<Uint8Array>(
+ {
+ async pull(controller) {
+ let result: Awaited<ReturnType<typeof reader.read>>;
+ try {
+ result = await reader.read();
+ } catch (err) {
+ if (!cancelled) controller.error(err);
+ return;
+ }
+ // A read the cancel below woke up: this stream is already closed.
+ if (cancelled) return;
+ if (result.done) {
+ controller.close();
+ return;
+ }
+ controller.enqueue(result.value);
+ },
+ async cancel(reason) {
+ cancelled = true;
+ try {
+ onCancel();
+ } catch {
+ // The flag is diagnostics; the upstream cancel below is what matters.
+ }
+ await reader.cancel(reason);
+ },
+ },
+ { highWaterMark: 0 }
+ );
+ return new Response(stream, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+}