Skip to content

Commit 56d893e

Browse files
MarcoDotIOclaude
andauthored
fix(server): failed Claude text generation shows Claude's error instead of raw JSON (pingdotgg#30331)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f63e23e commit 56d893e

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎apps/server/src/textGeneration/ClaudeTextGeneration.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,33 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
350350

351351
const apiError = "API Error: 400 Model blocked by guardrail: 4 endpoints excluded";
352352
const wrapperNotice = "Using the OpenRouter credential from the global credential file.";
353+
const structuredOutputError = "Failed to provide valid structured output after 5 attempts";
354+
const structuredOutputFailure = {
355+
type: "result",
356+
subtype: "error_max_structured_output_retries",
357+
is_error: true,
358+
errors: [structuredOutputError],
359+
};
353360
it.effect.each([
361+
[
362+
"the errors list of a result with no result text",
363+
JSON.stringify(structuredOutputFailure),
364+
structuredOutputError,
365+
],
366+
[
367+
"the errors list from verbose output",
368+
JSON.stringify([{ type: "system", subtype: "init" }, structuredOutputFailure]),
369+
structuredOutputError,
370+
],
371+
[
372+
"the errors list without CLI telemetry",
373+
JSON.stringify({
374+
...structuredOutputFailure,
375+
subtype: "error_during_execution",
376+
errors: ["[ede_diagnostic] result_type=user last_content_type=n/a", apiError],
377+
}),
378+
apiError,
379+
],
354380
[
355381
"the stdout error over a stderr notice",
356382
JSON.stringify({ is_error: true, api_error_status: 400, result: apiError }),
@@ -392,4 +418,25 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
392418
}),
393419
),
394420
);
421+
422+
it.effect("does not report the raw result JSON when a failed run writes nothing to stderr", () =>
423+
withFakeClaudeEnv(
424+
{ output: JSON.stringify(structuredOutputFailure), exitCode: 1 },
425+
(textGeneration) =>
426+
Effect.gen(function* () {
427+
const error = yield* Effect.flip(
428+
textGeneration.generateThreadTitle({
429+
cwd: process.cwd(),
430+
message: "Name this thread",
431+
modelSelection: {
432+
instanceId: ProviderInstanceId.make("claudeAgent"),
433+
model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
434+
},
435+
}),
436+
);
437+
438+
expect(error.detail).toBe(`Claude CLI command failed: ${structuredOutputError}`);
439+
}),
440+
),
441+
);
395442
});

‎apps/server/src/textGeneration/ClaudeTextGeneration.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ const decodeClaudeOutput = Schema.decodeEffect(
6666
Schema.fromJsonString(Schema.Union([ClaudeOutputEnvelope, Schema.Array(ClaudeOutputMessage)])),
6767
);
6868

69-
const ClaudeErrorResult = Schema.Struct({ is_error: Schema.Literal(true), result: Schema.String });
69+
// Only `success` results carry `result`; error subtypes such as
70+
// error_max_structured_output_retries report through `errors` instead.
71+
const ClaudeErrorResult = Schema.Struct({
72+
is_error: Schema.Literal(true),
73+
result: Schema.optionalKey(Schema.String),
74+
errors: Schema.optionalKey(Schema.Array(Schema.String)),
75+
});
7076
const isClaudeErrorResult = Schema.is(ClaudeErrorResult);
7177
const decodeJsonOption = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
7278

@@ -76,7 +82,14 @@ function claudeErrorResult(stdout: string): string | undefined {
7682
const messages: ReadonlyArray<unknown> = Array.isArray(output) ? output : [output];
7783
return messages
7884
.filter(isClaudeErrorResult)
79-
.map((message) => message.result.trim())
85+
.map((message) =>
86+
(
87+
message.result ??
88+
// error_during_execution mixes CLI telemetry into the list.
89+
message.errors?.filter((error) => !error.startsWith("[ede_diagnostic]")).join("\n") ??
90+
""
91+
).trim(),
92+
)
8093
.findLast((result) => result.length > 0);
8194
}
8295

0 commit comments

Comments
 (0)