Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(claude-cli): a null api_error_status is no status in the output-cap match (#7273)
garrytan2f1b1a7d45ef
diff --git a/src/core/ai/providers/claude-cli-language-model.ts b/src/core/ai/providers/claude-cli-language-model.tsindex 2bf3f97587..fed605a1d8 100644--- a/src/core/ai/providers/claude-cli-language-model.ts+++ b/src/core/ai/providers/claude-cli-language-model.ts@@ -116,8 +116,8 @@ interface ClaudeJsonResult {stop_reason: string | null;session_id: string;num_turns: number;- /** HTTP status of the underlying API failure (e.g. 429 on a spend/rate limit). */- api_error_status?: number;+ /** HTTP status of the underlying API failure (e.g. 429 on a spend/rate limit); Claude Code 2.1.305+ sends null when there is none. */+ api_error_status?: number | null;usage?: {input_tokens?: number;output_tokens?: number;@@ -204,14 +204,15 @@ function parseResultEnvelope(stdout: string): EnvelopeParse {* prefix only: the trailing advice has already changed wording once, the* reported cap may be the default rather than the forwarded one, and the exit* code varies by CLI version. A quoted prefix mid-answer, a success envelope,- * a non-string result or an envelope carrying an API status never match.+ * a non-string result or an envelope carrying an API status never match. No+ * status means the key is absent or null (#7273: newer CLIs send null).*/const OUTPUT_CAP_RE = /^API Error: Claude's response exceeded the \d+ output token maximum\b/;function isOutputCapEnvelope(envelope: ClaudeJsonResult): boolean {return envelope.type === 'result'&& envelope.is_error === true- && envelope.api_error_status === undefined+ && (envelope.api_error_status === undefined || envelope.api_error_status === null)&& typeof envelope.result === 'string'&& OUTPUT_CAP_RE.test(envelope.result);}diff --git a/test/claude-cli-output-cap.test.ts b/test/claude-cli-output-cap.test.tsindex 9969a56237..8f7cba9d56 100644--- a/test/claude-cli-output-cap.test.ts+++ b/test/claude-cli-output-cap.test.ts@@ -151,6 +151,29 @@ describe('claude-cli output-cap envelope finishes as length (#6523)', () => {}});+ // #7273: Claude Code 2.1.305 writes the key as JSON null instead of leaving it out.+ test('a null api_error_status counts as no status: the cap is length, anything else stays a typed failure', async () => {+ const nullStatus = { api_error_status: null };+ for (const exit of [0, 1]) {+ for (const body of [envelope(capMessage(2048), { ...nullStatus, subtype: 'success', stop_reason: 'stop_sequence' }),+ [{ type: 'system', subtype: 'init' }, envelope(capMessage(2048), nullStatus)]]) {+ await withCli({ '*': { body, exit } }, async () => {+ const out = await generate();+ expect(out.finishReason, `exit ${exit}`).toBe('length');+ expect(out.content).toEqual([{ type: 'text', text: '' }]);+ });+ }+ for (const result of ['API Error: overloaded', `See the docs: ${capMessage(2048)}`]) {+ await withCli({ '*': { body: envelope(result, nullStatus), exit } }, async () => {+ const error = await rejection(generate);+ expect(error, `${result} (exit ${exit})`).toBeInstanceOf(ClaudeCliProcessError);+ expect((error as ClaudeCliProcessError).apiErrorStatus).toBeUndefined();+ expect((error as ClaudeCliProcessError).exitCode).toBe(exit);+ });+ }+ }+ });+test('a success envelope quoting the prefix is ordinary model text', async () => {const quote = `The CLI said: ${capMessage(2048)}`;await withCli({ '*': { body: envelope(quote, { is_error: false, subtype: 'success', stop_reason: 'end_turn' }), exit: 0 } }, async () => {