Skip to content

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

fix(adapters): end a Responses turn with an SSE error when a hook throws

Jack Rudenko7e8e395192a8
diff --git a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
index a5e4d555..e3d3c107 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/responses/responses-turn.ts
@@ -290,13 +290,30 @@ class ResponsesTurn {
headersAt = next.headersAt;
}
} catch (error) {
- // Only a throwing callback can land here; the leg itself never throws.
- log(`[ResponsesSSE] Stream error: ${error}`);
+ this.failTurn(error);
} finally {
this.sink.close();
}
}
+ /**
+ * A callback threw (`primaryBody`, `onApiError`, a usage or middleware
+ * hook); the leg itself never throws. A message with no ending yet still
+ * gets one: closed without `message_stop` or `error`, the non-streamed path
+ * folded it into a silent 200 partial.
+ */
+ private failTurn(error: unknown): void {
+ log(`[ResponsesSSE] Stream error: ${error}`);
+ if (!this.sink.hasEnded && !this.sink.isCancelled) {
+ this.sink.endWithError(`claudish: the turn failed: ${error}`);
+ }
+ try {
+ this.reportUsage();
+ } catch (err) {
+ log(`[ResponsesSSE] reporting usage failed: ${err}`);
+ }
+ }
+
/** A leg that did not end on an exec call: finish, retry, or end the message otherwise. */
private async afterLeg(outcome: LegOutcome): Promise<NextLeg> {
const decision = this.retryDecision(outcome);