Skip to content

Commit d188170

Browse files
committed
fix(agents): unify cli stream output classification
Fixes #98896
1 parent 809bc61 commit d188170

6 files changed

Lines changed: 550 additions & 97 deletions

File tree

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

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
extractCliErrorMessage,
66
parseCliJson,
77
parseCliJsonl,
8+
parseCliOutput,
89
supportsCliJsonlToolEvents,
910
type CliToolResultDelta,
1011
type CliToolUseStartDelta,
@@ -34,6 +35,114 @@ describe("supportsCliJsonlToolEvents", () => {
3435
});
3536

3637
describe("parseCliJson", () => {
38+
it("classifies Claude is_error JSON results as provider errors", () => {
39+
const result = parseCliJson(
40+
JSON.stringify({
41+
type: "result",
42+
subtype: "success",
43+
is_error: true,
44+
result: 'API Error: 400 {"error":{"message":"Bad request"}}',
45+
}),
46+
{
47+
command: "claude",
48+
output: "json",
49+
sessionIdFields: ["session_id"],
50+
},
51+
"claude-cli",
52+
);
53+
54+
expect(result).toEqual({
55+
text: "",
56+
sessionId: undefined,
57+
usage: undefined,
58+
errorText: "Bad request",
59+
});
60+
});
61+
62+
it("classifies generic is_error JSON results as provider errors", () => {
63+
const result = parseCliJson(
64+
JSON.stringify({
65+
is_error: true,
66+
result: "429 rate limit exceeded",
67+
}),
68+
{
69+
command: "custom",
70+
output: "json",
71+
},
72+
"custom-cli",
73+
);
74+
75+
expect(result).toEqual({
76+
text: "",
77+
sessionId: undefined,
78+
usage: undefined,
79+
errorText: "429 rate limit exceeded",
80+
});
81+
});
82+
83+
it("keeps successful JSON result message payloads as assistant text", () => {
84+
const result = parseCliJson(
85+
JSON.stringify({
86+
type: "result",
87+
message: "done",
88+
}),
89+
{
90+
command: "custom",
91+
output: "json",
92+
},
93+
"custom-cli",
94+
);
95+
96+
expect(result).toEqual({
97+
text: "done",
98+
sessionId: undefined,
99+
usage: undefined,
100+
});
101+
});
102+
103+
it("does not classify null JSON result error fields as provider errors", () => {
104+
const result = parseCliJson(
105+
JSON.stringify({
106+
type: "result",
107+
error: null,
108+
message: "done",
109+
}),
110+
{
111+
command: "custom",
112+
output: "json",
113+
},
114+
"custom-cli",
115+
);
116+
117+
expect(result).toEqual({
118+
text: "done",
119+
sessionId: undefined,
120+
usage: undefined,
121+
});
122+
});
123+
124+
it("classifies JSON status error result payloads as provider errors", () => {
125+
const result = parseCliJson(
126+
JSON.stringify({
127+
type: "result",
128+
status: "error",
129+
result: "rate limit",
130+
}),
131+
{
132+
command: "custom",
133+
output: "json",
134+
},
135+
"custom-cli",
136+
);
137+
138+
expect(result).toEqual({
139+
text: "",
140+
sessionId: undefined,
141+
usage: undefined,
142+
errorText: "rate limit",
143+
});
144+
});
145+
37146
it("recovers mixed-output Claude session metadata from embedded JSON objects", () => {
38147
const result = parseCliJson(
39148
[
@@ -752,6 +861,103 @@ describe("parseCliJsonl", () => {
752861

753862
expect(result).toBe(message);
754863
});
864+
865+
it("classifies Claude is_error stream-json results as provider errors", () => {
866+
const { message, jsonl } = createClaudeApiErrorFixture();
867+
const result = parseCliJsonl(
868+
jsonl,
869+
{
870+
command: "claude",
871+
output: "jsonl",
872+
sessionIdFields: ["session_id"],
873+
},
874+
"claude-cli",
875+
);
876+
877+
expect(result).toEqual({
878+
text: "",
879+
sessionId: "session-api-error",
880+
usage: undefined,
881+
errorText: message,
882+
});
883+
});
884+
885+
it("uses Claude error subtypes when result text is absent", () => {
886+
const result = parseCliJsonl(
887+
JSON.stringify({
888+
type: "result",
889+
subtype: "error_max_turns",
890+
session_id: "session-max-turns",
891+
}),
892+
{
893+
command: "claude",
894+
output: "jsonl",
895+
sessionIdFields: ["session_id"],
896+
},
897+
"claude-cli",
898+
);
899+
900+
expect(result).toEqual({
901+
text: "",
902+
sessionId: "session-max-turns",
903+
usage: undefined,
904+
errorText: "Claude CLI result subtype error_max_turns.",
905+
});
906+
});
907+
});
908+
909+
describe("parseCliOutput", () => {
910+
it("uses streamed Claude assistant text when the result envelope is missing", () => {
911+
const raw = [
912+
JSON.stringify({ type: "init", session_id: "session-stream-missing-result" }),
913+
JSON.stringify({
914+
type: "stream_event",
915+
event: {
916+
type: "content_block_delta",
917+
delta: { type: "text_delta", text: "partial answer" },
918+
},
919+
}),
920+
].join("\n");
921+
922+
const result = parseCliOutput({
923+
raw,
924+
backend: {
925+
command: "claude",
926+
output: "jsonl",
927+
sessionIdFields: ["session_id"],
928+
},
929+
providerId: "claude-cli",
930+
outputMode: "jsonl",
931+
});
932+
933+
expect(result).toEqual({
934+
text: "partial answer",
935+
sessionId: "session-stream-missing-result",
936+
usage: undefined,
937+
});
938+
});
939+
940+
it("fails stream-json output without result or assistant text instead of returning raw JSONL", () => {
941+
const raw = JSON.stringify({ type: "init", session_id: "session-empty" });
942+
943+
const result = parseCliOutput({
944+
raw,
945+
backend: {
946+
command: "claude",
947+
output: "jsonl",
948+
sessionIdFields: ["session_id"],
949+
},
950+
providerId: "claude-cli",
951+
outputMode: "jsonl",
952+
});
953+
954+
expect(result).toEqual({
955+
text: "",
956+
sessionId: "session-empty",
957+
usage: undefined,
958+
errorText: "CLI stream-json output ended without a result event.",
959+
});
960+
});
755961
});
756962

757963
describe("createCliJsonlStreamingParser", () => {
@@ -787,6 +993,66 @@ describe("createCliJsonlStreamingParser", () => {
787993
]);
788994
});
789995

996+
it("uses streamed Claude assistant text when no result envelope arrives", () => {
997+
const parser = createCliJsonlStreamingParser({
998+
backend: {
999+
command: "local-cli",
1000+
output: "jsonl",
1001+
jsonlDialect: "claude-stream-json",
1002+
sessionIdFields: ["session_id"],
1003+
},
1004+
providerId: "local-cli",
1005+
onAssistantDelta: () => {},
1006+
});
1007+
1008+
parser.push(
1009+
[
1010+
JSON.stringify({ type: "init", session_id: "session-stream-no-result" }),
1011+
JSON.stringify({
1012+
type: "stream_event",
1013+
event: {
1014+
type: "content_block_delta",
1015+
delta: { type: "text_delta", text: "streamed answer" },
1016+
},
1017+
}),
1018+
].join("\n") + "\n",
1019+
);
1020+
parser.finish();
1021+
1022+
expect(parser.getOutput()).toEqual({
1023+
text: "streamed answer",
1024+
sessionId: "session-stream-no-result",
1025+
usage: undefined,
1026+
});
1027+
});
1028+
1029+
it("reports an output-limit error and ignores later chunks", () => {
1030+
const parser = createCliJsonlStreamingParser({
1031+
backend: {
1032+
command: "local-cli",
1033+
output: "jsonl",
1034+
jsonlDialect: "claude-stream-json",
1035+
reliability: { outputLimits: { maxTurnRawChars: 1024 } },
1036+
},
1037+
providerId: "local-cli",
1038+
onAssistantDelta: () => {},
1039+
});
1040+
1041+
parser.push("x".repeat(1025));
1042+
parser.push(`${JSON.stringify({ type: "result", result: "late" })}\n`);
1043+
parser.finish();
1044+
1045+
expect(parser.getErrorText()).toBe(
1046+
"CLI JSONL output exceeded 1024 characters; refusing to parse output.",
1047+
);
1048+
expect(parser.getOutput()).toEqual({
1049+
text: "",
1050+
sessionId: undefined,
1051+
usage: undefined,
1052+
errorText: "CLI JSONL output exceeded 1024 characters; refusing to parse output.",
1053+
});
1054+
});
1055+
7901056
it("streams Gemini message deltas and tool events", () => {
7911057
const deltas: Array<{ text: string; delta: string; sessionId?: string }> = [];
7921058
const starts: CliToolUseStartDelta[] = [];

0 commit comments

Comments
 (0)