fix(server): failed Claude text generation shows the CLI's own error (#19394)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f99b202753
commit
7fd2ba7526
2 files changed
+57
-1
No files matched your search
@@ -484,6 +484,43 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
|
||||
),
|
||||
);
|
||||
|
||||
const apiError = "API Error: 400 Model blocked by guardrail: 4 endpoints excluded";
|
||||
const wrapperNotice = "Using the OpenRouter credential from the global credential file.";
|
||||
it.effect.each([
|
||||
[
|
||||
"the stdout error over a stderr notice",
|
||||
JSON.stringify({ is_error: true, api_error_status: 400, result: apiError }),
|
||||
apiError,
|
||||
],
|
||||
[
|
||||
"the verbose stdout error over a stderr notice",
|
||||
JSON.stringify([
|
||||
{ type: "system", subtype: "init" },
|
||||
{ type: "result", is_error: true, result: apiError },
|
||||
{ type: "result", is_error: true, result: " " },
|
||||
]),
|
||||
apiError,
|
||||
],
|
||||
["stderr when stdout has no error", "not json", wrapperNotice],
|
||||
] as const)("reports %s when the Claude CLI fails", ([, output, expected]) =>
|
||||
withFakeClaudeEnv({ output, stderr: wrapperNotice, exitCode: 1 }, (textGeneration) =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* Effect.flip(
|
||||
textGeneration.generateThreadTitle({
|
||||
cwd: process.cwd(),
|
||||
message: "Name this thread",
|
||||
modelSelection: {
|
||||
instanceId: ProviderInstanceId.make("claudeAgent"),
|
||||
model: SYNTHETIC_CLAUDE_STANDARD_MODEL,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(error.detail).toBe(`Claude CLI command failed: ${expected}`);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("falls back when Claude thread title normalization becomes whitespace-only", () =>
|
||||
withFakeClaudeEnv(
|
||||
{
|
||||
|
||||
@@ -75,6 +75,23 @@ const decodeClaudeOutput = Schema.decodeEffect(
|
||||
Schema.fromJsonString(Schema.Union([ClaudeOutputEnvelope, Schema.Array(ClaudeOutputMessage)])),
|
||||
);
|
||||
|
||||
const ClaudeErrorResult = Schema.Struct({ is_error: Schema.Literal(true), result: Schema.String });
|
||||
const isClaudeErrorResult = Schema.is(ClaudeErrorResult);
|
||||
const decodeJsonOption = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
|
||||
|
||||
/**
|
||||
* The error a failed `claude -p --output-format json` run reports on stdout. Stderr may only
|
||||
* hold notices from a wrapper around the binary, so the caller prefers this when present.
|
||||
*/
|
||||
function claudeErrorResult(stdout: string): string | undefined {
|
||||
const output = Option.getOrUndefined(decodeJsonOption(stdout));
|
||||
const messages: ReadonlyArray<unknown> = Array.isArray(output) ? output : [output];
|
||||
return messages
|
||||
.filter(isClaudeErrorResult)
|
||||
.map((message) => message.result.trim())
|
||||
.findLast((result) => result.length > 0);
|
||||
}
|
||||
|
||||
export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(function* (
|
||||
claudeSettings: ClaudeSettings,
|
||||
environment?: NodeJS.ProcessEnv,
|
||||
@@ -258,7 +275,9 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
|
||||
if (exitCode !== 0) {
|
||||
const stderrDetail = stderr.trim();
|
||||
const stdoutDetail = stdout.trim();
|
||||
const detail = stderrDetail.length > 0 ? stderrDetail : stdoutDetail;
|
||||
const detail =
|
||||
claudeErrorResult(stdoutDetail) ??
|
||||
(stderrDetail.length > 0 ? stderrDetail : stdoutDetail);
|
||||
return yield* new TextGenerationError({
|
||||
operation,
|
||||
detail:
|
||||
|
||||
Reference in new issue
Block a user