Skip to content

Commit 1224a26

Browse files
t3dotggclaude
andcommitted
fix(server): failed Claude text generation shows the CLI's own error
When the Claude CLI exits non-zero, use the error result it printed as JSON on stdout, and fall back to stderr only when stdout has none. Stderr can hold only a notice from a wrapper around the binary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f99b202 commit 1224a26

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,43 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
484484
),
485485
);
486486

487+
const apiError = "API Error: 400 Model blocked by guardrail: 4 endpoints excluded";
488+
const wrapperNotice = "Using the OpenRouter credential from the global credential file.";
489+
it.effect.each([
490+
[
491+
"the stdout error over a stderr notice",
492+
JSON.stringify({ is_error: true, api_error_status: 400, result: apiError }),
493+
apiError,
494+
],
495+
[
496+
"the verbose stdout error over a stderr notice",
497+
JSON.stringify([
498+
{ type: "system", subtype: "init" },
499+
{ type: "result", is_error: true, result: apiError },
500+
{ type: "result", is_error: true, result: " " },
501+
]),
502+
apiError,
503+
],
504+
["stderr when stdout has no error", "not json", wrapperNotice],
505+
] as const)("reports %s when the Claude CLI fails", ([, output, expected]) =>
506+
withFakeClaudeEnv({ output, stderr: wrapperNotice, exitCode: 1 }, (textGeneration) =>
507+
Effect.gen(function* () {
508+
const error = yield* Effect.flip(
509+
textGeneration.generateThreadTitle({
510+
cwd: process.cwd(),
511+
message: "Name this thread",
512+
modelSelection: {
513+
instanceId: ProviderInstanceId.make("claudeAgent"),
514+
model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
515+
},
516+
}),
517+
);
518+
519+
expect(error.detail).toBe(`Claude CLI command failed: ${expected}`);
520+
}),
521+
),
522+
);
523+
487524
it.effect("falls back when Claude thread title normalization becomes whitespace-only", () =>
488525
withFakeClaudeEnv(
489526
{

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ const decodeClaudeOutput = Schema.decodeEffect(
7575
Schema.fromJsonString(Schema.Union([ClaudeOutputEnvelope, Schema.Array(ClaudeOutputMessage)])),
7676
);
7777

78+
const ClaudeErrorResult = Schema.Struct({ is_error: Schema.Literal(true), result: Schema.String });
79+
const isClaudeErrorResult = Schema.is(ClaudeErrorResult);
80+
const decodeJsonOption = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
81+
82+
/**
83+
* The error a failed `claude -p --output-format json` run reports on stdout. Stderr may only
84+
* hold notices from a wrapper around the binary, so the caller prefers this when present.
85+
*/
86+
function claudeErrorResult(stdout: string): string | undefined {
87+
const output = Option.getOrUndefined(decodeJsonOption(stdout));
88+
const messages: ReadonlyArray<unknown> = Array.isArray(output) ? output : [output];
89+
return messages
90+
.filter(isClaudeErrorResult)
91+
.map((message) => message.result.trim())
92+
.findLast((result) => result.length > 0);
93+
}
94+
7895
export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(function* (
7996
claudeSettings: ClaudeSettings,
8097
environment?: NodeJS.ProcessEnv,
@@ -258,7 +275,9 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
258275
if (exitCode !== 0) {
259276
const stderrDetail = stderr.trim();
260277
const stdoutDetail = stdout.trim();
261-
const detail = stderrDetail.length > 0 ? stderrDetail : stdoutDetail;
278+
const detail =
279+
claudeErrorResult(stdoutDetail) ??
280+
(stderrDetail.length > 0 ? stderrDetail : stdoutDetail);
262281
return yield* new TextGenerationError({
263282
operation,
264283
detail:

0 commit comments

Comments
 (0)