Skip to content

Commit 575146a

Browse files
fix(cli): report exact Claude stream failures (#127169)
Preserve parser-owned bounded diagnostics in managed Claude live turns and share the canonical output-limit formatter. Co-authored-by: Marvinthebored <[email protected]>
1 parent 0ac2606 commit 575146a

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

‎src/agents/cli-output-stream.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ export function normalizeClaudeCliStreamJsonRecord(
140140
return normalized ? { line: JSON.stringify(parsed), omittedRawChars } : undefined;
141141
}
142142

143-
function streamJsonOutputLimitErrorText(kind: "raw" | "line" | "lines", limit: number): string {
143+
export function streamJsonOutputLimitErrorText(
144+
kind: "raw" | "line" | "lines",
145+
limit: number,
146+
): string {
144147
if (kind === "line") {
145148
return `CLI JSONL line exceeded ${limit} characters; refusing to parse output.`;
146149
}

‎src/agents/cli-runner/claude-live-turn.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,22 +847,25 @@ describe("Claude live turn output bounds and result projection", () => {
847847
});
848848

849849
await expect(startLiveTurn("run-live-oversized-line", false)).rejects.toThrow(
850-
"Claude CLI JSONL line exceeded output limit.",
850+
"CLI JSONL line exceeded 8388608 characters; refusing to parse output.",
851851
);
852852
});
853853

854854
it.each([
855855
{
856856
name: "a coalesced blank-frame flood",
857857
createChunk: () => "\n".repeat(20_001),
858+
expectedError: "CLI JSONL output exceeded 20000 lines; refusing to parse output.",
858859
},
859860
{
860861
name: "whitespace-only records exceeding the raw budget",
861862
createChunk: () => `${" ".repeat(4_300_000)}\n${" ".repeat(4_300_000)}\n`,
863+
expectedError: "CLI JSONL output exceeded 8388608 characters; refusing to parse output.",
862864
},
863865
{
864866
name: "valid JSON padded beyond the raw budget",
865867
createChunk: () => `${" ".repeat(4_300_000)}{}\n${" ".repeat(4_300_000)}{}\n`,
868+
expectedError: "CLI JSONL output exceeded 8388608 characters; refusing to parse output.",
866869
},
867870
{
868871
name: "internal formatting around compacted Claude media",
@@ -886,14 +889,32 @@ describe("Claude live turn output bounds and result projection", () => {
886889
}).replace('"message":', `"message":${" ".repeat(4_300_000)}`);
887890
return `${line}\n${line}\n`;
888891
},
892+
expectedError: "CLI JSONL output exceeded 8388608 characters; refusing to parse output.",
889893
},
890-
])("rejects $name from the managed Claude live session", async ({ createChunk }) => {
894+
])("reports the exact limit for $name", async ({ createChunk, expectedError }) => {
891895
const live: ReturnType<typeof mockClaudeLiveRun> = mockClaudeLiveRun(supervisorSpawnMock, {
892896
onWrite: () => live.spawnInput.onStdout?.(createChunk()),
893897
});
894898

895-
await expect(startLiveTurn("run-live-output-budget", false)).rejects.toThrow(
896-
"Claude CLI turn output exceeded limit.",
899+
await expect(startLiveTurn("run-live-output-budget", false)).rejects.toThrow(expectedError);
900+
});
901+
902+
it("reports backend JSONL parser failures without relabeling them as output limits", async () => {
903+
mockClaudeLiveRun(supervisorSpawnMock, {
904+
events: [{ type: "system", subtype: "init", session_id: "live-parser-error" }],
905+
});
906+
907+
await expectRejectsWithFields(
908+
startLiveTurn("run-live-parser-error", false, {
909+
parseJsonlEvent: () => {
910+
throw new Error("invalid custom event");
911+
},
912+
}),
913+
{
914+
name: "FailoverError",
915+
reason: "format",
916+
message: "CLI backend claude-cli JSONL parser failed: invalid custom event",
917+
},
897918
);
898919
});
899920

‎src/agents/cli-runner/claude-live-turn.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
createCliJsonlStreamingParser,
2727
frameBoundedCliJsonlChunk,
2828
normalizeClaudeCliStreamJsonRecord,
29+
streamJsonOutputLimitErrorText,
2930
} from "../cli-output-stream.js";
3031
import { parseCliOutput } from "../cli-output.js";
3132
import type { FailoverError } from "../failover-error.js";
@@ -360,10 +361,11 @@ function applyBackgroundTasksChanged(
360361

361362
function pushTurnLine(host: ClaudeLiveTurnHost, turn: ClaudeLiveTurn, line: string): boolean {
362363
turn.streamingParser.push(`${line}\n`);
363-
if (!turn.streamingParser.getErrorText()) {
364+
const errorText = turn.streamingParser.getErrorText();
365+
if (!errorText) {
364366
return true;
365367
}
366-
host.close("abort", createClaudeOutputLimitError(host, "Claude CLI turn output exceeded limit."));
368+
host.close("abort", createClaudeOutputLimitError(host, errorText));
367369
return false;
368370
}
369371

@@ -492,7 +494,10 @@ export function acceptClaudeStdout(host: ClaudeLiveTurnHost, chunk: string): voi
492494
) {
493495
host.close(
494496
"abort",
495-
createClaudeOutputLimitError(host, "Claude CLI JSONL line exceeded output limit."),
497+
createClaudeOutputLimitError(
498+
host,
499+
streamJsonOutputLimitErrorText("line", maxPendingLineChars),
500+
),
496501
);
497502
}
498503
} catch (error) {

0 commit comments

Comments
 (0)