Skip to content

Commit 020c2ee

Browse files
committed
fix(claude): an API error that differs from the final result stays visible
1 parent fb6f093 commit 020c2ee

3 files changed

Lines changed: 294 additions & 32 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 245 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts";
9696
import { ThreadToolkit } from "../../mcp/toolkits/thread/tools.ts";
9797
import { OrchestratorToolkit } from "../../mcp/toolkits/orchestrator/tools.ts";
9898
import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutable.ts";
99+
import { claudeSignedOutMessage } from "../../provider/Drivers/ClaudeHome.ts";
99100
import {
100101
BUNDLED_CLAUDE_MODEL_CATALOG,
101102
type ClaudeModelCatalog,
@@ -7649,6 +7650,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76497650
),
76507651
);
76517652

7653+
const longConfigDir = `/claude/${"config/".repeat(600)}`;
76527654
// regression: #24171
76537655
it.effect.each([
76547656
{
@@ -7663,6 +7665,175 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76637665
apiErrorStatus: 400,
76647666
shown: false,
76657667
},
7668+
{
7669+
name: "drops the CLI's API error frame when a long 403 failure repeats it",
7670+
text: `API Error: 403 ${"access detail ".repeat(40)}`.trim(),
7671+
apiErrorStatus: 403,
7672+
shown: false,
7673+
},
7674+
{
7675+
name: "keeps the CLI's API error frame when only the truncated failure matches it",
7676+
text: `API Error: ${"safeguard detail ".repeat(40)}first ending`,
7677+
result: `API Error: ${"safeguard detail ".repeat(40)}second ending`,
7678+
apiErrorStatus: 400,
7679+
shown: true,
7680+
},
7681+
{
7682+
name: "keeps a 500-character API error frame when the longer result goes on",
7683+
text: `API Error: ${"boundary detail ".repeat(31)}`.slice(0, 500),
7684+
result: `${`API Error: ${"boundary detail ".repeat(31)}`.slice(0, 500)} and more`,
7685+
apiErrorStatus: 400,
7686+
shown: true,
7687+
},
7688+
{
7689+
name: "drops the CLI's API error frame when the result adds a trailing newline",
7690+
text: "API Error: rejected",
7691+
result: "API Error: rejected\n",
7692+
apiErrorStatus: 400,
7693+
shown: false,
7694+
},
7695+
{
7696+
name: "drops the CLI's API error frame when the result has a control character",
7697+
text: "API Error: rejected detail",
7698+
result: "API Error: rejected\u0000detail",
7699+
apiErrorStatus: 400,
7700+
shown: false,
7701+
},
7702+
{
7703+
name: "drops the CLI's API error frame when a listed error has a control character",
7704+
text: "API Error: rejected detail",
7705+
errors: ["API Error: rejected\u0007detail"],
7706+
apiErrorStatus: 400,
7707+
shown: false,
7708+
},
7709+
{
7710+
name: "drops the CLI's API error frame when a full-length listed error has a control character",
7711+
text: `API Error: ${"x".repeat(4096 - "API Error: ".length)}`,
7712+
errors: [`API Error:\u0000${"x".repeat(4096 - "API Error: ".length)}`],
7713+
failure: `API Error: ${"x".repeat(4096 - "API Error: ".length)}`,
7714+
apiErrorStatus: 400,
7715+
shown: false,
7716+
},
7717+
{
7718+
name: "keeps the CLI's API error frame when it equals only a capped listed error",
7719+
text: `API Error: ${"x".repeat(4094 - "API Error: ".length)}…`,
7720+
failure: `API Error: ${"x".repeat(4094 - "API Error: ".length)}…`,
7721+
errors: [`API Error: ${"x".repeat(4094 - "API Error: ".length)}\u{1F600}tail`],
7722+
apiErrorStatus: 400,
7723+
shown: true,
7724+
},
7725+
{
7726+
name: "drops the CLI's API error frame when a listed error repeats it under a longer result",
7727+
text: `API Error: ${"x".repeat(489)}`,
7728+
result: `API Error: ${"x".repeat(489)} hidden tail`,
7729+
errors: [`API Error: ${"x".repeat(489)}`],
7730+
failure: `API Error: ${"x".repeat(489)}`,
7731+
apiErrorStatus: 400,
7732+
shown: false,
7733+
},
7734+
{
7735+
name: "keeps the CLI's API error frame when a listed error shows only its first 500 characters",
7736+
text: `API Error: ${"x".repeat(489)} hidden tail`,
7737+
errors: [`API Error: ${"x".repeat(489)}`],
7738+
failure: `API Error: ${"x".repeat(489)}`,
7739+
apiErrorStatus: 400,
7740+
shown: true,
7741+
},
7742+
{
7743+
name: "keeps the CLI's API error frame when the result has a different secret",
7744+
text: "API Error: Bearer frame-value rejected",
7745+
result: "API Error: Bearer result-value rejected",
7746+
apiErrorStatus: 400,
7747+
shown: true,
7748+
},
7749+
{
7750+
name: "drops the CLI's API error frame when its snapshot repeats and the failure repeats it",
7751+
text: "API Error: Claude Opus 5.5's safeguards flagged this message (https://www.anthropic.com/legal/aup).",
7752+
repeatedSnapshot: true,
7753+
apiErrorStatus: 400,
7754+
shown: false,
7755+
},
7756+
{
7757+
name: "drops the CLI's API error frame when an error result lists it",
7758+
text: "API Error: Request could not be completed.",
7759+
subtype: "error_during_execution",
7760+
errors: ["API Error: Request could not be completed."],
7761+
apiErrorStatus: 400,
7762+
shown: false,
7763+
},
7764+
{
7765+
name: "keeps the CLI's API error frame when the endings differ beyond the failure cap",
7766+
text: `API Error: ${"detail ".repeat(700)}first ending`,
7767+
result: `API Error: ${"detail ".repeat(700)}second ending`,
7768+
apiErrorStatus: 400,
7769+
shown: true,
7770+
},
7771+
{
7772+
name: "keeps the CLI's API error frame when a 403 result differs beyond the failure cap",
7773+
text: `API Error: 403 ${"access detail ".repeat(300)}first ending`,
7774+
result: `API Error: 403 ${"access detail ".repeat(300)}second ending`,
7775+
apiErrorStatus: 403,
7776+
shown: true,
7777+
},
7778+
{
7779+
name: "keeps the CLI's API error frame when an error result differs beyond the failure cap",
7780+
text: `API Error: ${"detail ".repeat(700)}first ending`,
7781+
subtype: "error_during_execution",
7782+
errors: [`API Error: ${"detail ".repeat(700)}second ending`],
7783+
apiErrorStatus: 400,
7784+
shown: true,
7785+
},
7786+
{
7787+
name: "keeps the CLI's API error frame when a listed error differs beyond the failure cap",
7788+
text: `API Error: ${"detail ".repeat(700)}first ending`,
7789+
errors: [`API Error: ${"detail ".repeat(700)}second ending`],
7790+
apiErrorStatus: 400,
7791+
shown: true,
7792+
},
7793+
{
7794+
name: "keeps the CLI's API error frame when joined diagnostics differ beyond the failure cap",
7795+
text: `[ede_diagnostic] ${"detail ".repeat(700)}first ending`,
7796+
subtype: "error_during_execution",
7797+
errors: [`[ede_diagnostic] ${"detail ".repeat(700)}second ending`],
7798+
apiErrorStatus: 400,
7799+
terminalReason: null,
7800+
shown: true,
7801+
},
7802+
{
7803+
name: "keeps the CLI's API error frame when a long login hint differs beyond the failure cap",
7804+
text: claudeSignedOutMessage({
7805+
configDir: `${longConfigDir}frame`,
7806+
cwd: NodePath.resolve("/workspace"),
7807+
}),
7808+
environment: { CLAUDE_CONFIG_DIR: `${longConfigDir}login` },
7809+
frameError: "authentication_failed" as const,
7810+
apiErrorStatus: 401,
7811+
shown: true,
7812+
},
7813+
{
7814+
name: "keeps the CLI's API error frame when a custom endpoint's version error is reworded",
7815+
text: "API Error: 400 Claude Code 2.1.280 does not support this model; version 2.1.290 or newer is required. Run 'claude update', or update the Claude desktop app, then try again.",
7816+
environment: {
7817+
CLAUDE_CONFIG_DIR: "/synthetic/no-claude-config",
7818+
ANTHROPIC_BASE_URL: "https://gateway.example.test/v1",
7819+
},
7820+
installed: "2.1.322",
7821+
apiErrorStatus: 400,
7822+
shown: true,
7823+
},
7824+
{
7825+
name: "keeps the CLI's API error frame when a compaction error differs beyond the failure cap",
7826+
text: `API Error: ${"detail ".repeat(700)}first ending`,
7827+
compactError: `API Error: ${"detail ".repeat(700)}second ending`,
7828+
shown: true,
7829+
},
7830+
{
7831+
name: "emits the held API error frame before later assistant text",
7832+
text: "API Error: 400 Request rejected.",
7833+
followingText: "The request was rejected.",
7834+
apiErrorStatus: 400,
7835+
shown: true,
7836+
},
76667837
{
76677838
name: "keeps the CLI's API error frame when the failure says something else",
76687839
text: "API Error: 429 Specific rate limit detail.",
@@ -7678,7 +7849,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76787849
},
76797850
])("$name", (input) =>
76807851
Effect.gen(function* () {
7681-
const harness = yield* makeWakeHarness;
7852+
const harness = yield* makeWakeHarnessWithOptions(
7853+
"environment" in input ? { environment: input.environment } : undefined,
7854+
);
76827855
yield* harness.runtime.startTurn(
76837856
makeClaudeTestTurnInput({
76847857
threadId: harness.threadId,
@@ -7689,25 +7862,74 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76897862
attachments: [],
76907863
}),
76917864
);
7865+
if ("installed" in input) {
7866+
yield* harness.offerAndWait(
7867+
claudeSdkFrame({
7868+
type: "system",
7869+
subtype: "init",
7870+
uuid: "00000000-0000-4000-8000-000000000615",
7871+
session_id: WAKE_NATIVE_SESSION,
7872+
claude_code_version: input.installed,
7873+
}),
7874+
);
7875+
}
7876+
const result = makeResultFrame({
7877+
uuid: "00000000-0000-4000-8000-000000000611",
7878+
result: "result" in input ? input.result : input.text,
7879+
...("subtype" in input ? { subtype: input.subtype } : {}),
7880+
...("errors" in input ? { errors: input.errors } : {}),
7881+
isError: true,
7882+
...("apiErrorStatus" in input ? { apiErrorStatus: input.apiErrorStatus } : {}),
7883+
terminalReason: "terminalReason" in input ? input.terminalReason : "api_error",
7884+
});
76927885
yield* Queue.offerAll(harness.sdkMessages, [
7693-
makeAssistantErrorFrame({
7694-
uuid: "00000000-0000-4000-8000-000000000610",
7695-
error: "invalid_request",
7696-
text: input.text,
7697-
}),
7698-
makeResultFrame({
7699-
uuid: "00000000-0000-4000-8000-000000000611",
7700-
result: input.text,
7701-
isError: true,
7702-
apiErrorStatus: input.apiErrorStatus,
7703-
terminalReason: "terminalReason" in input ? input.terminalReason : "api_error",
7704-
}),
7886+
...("compactError" in input
7887+
? [
7888+
claudeSdkFrame({
7889+
type: "rate_limit_event",
7890+
rate_limit_info: { status: "rejected", rateLimitType: "five_hour", resetsAt: 0 },
7891+
uuid: "00000000-0000-4000-8000-000000000613",
7892+
session_id: WAKE_NATIVE_SESSION,
7893+
}),
7894+
claudeSdkFrame({
7895+
type: "system",
7896+
subtype: "status",
7897+
status: null,
7898+
compact_result: "failed",
7899+
compact_error: input.compactError,
7900+
uuid: "00000000-0000-4000-8000-000000000614",
7901+
session_id: WAKE_NATIVE_SESSION,
7902+
}),
7903+
]
7904+
: []),
7905+
...Array.from({ length: "repeatedSnapshot" in input ? 2 : 1 }, () =>
7906+
makeAssistantErrorFrame({
7907+
uuid: "00000000-0000-4000-8000-000000000610",
7908+
error: "frameError" in input ? input.frameError : "invalid_request",
7909+
text: input.text,
7910+
}),
7911+
),
7912+
...("followingText" in input
7913+
? [
7914+
makeAssistantTextFrame({
7915+
uuid: "00000000-0000-4000-8000-000000000612",
7916+
text: input.followingText,
7917+
}),
7918+
]
7919+
: []),
7920+
"compactError" in input ? claudeSdkFrame({ ...result, local_command: "compact" }) : result,
77057921
]);
77067922

77077923
const terminal = yield* Queue.take(harness.terminalReceipts);
7708-
assert.equal(terminal.status, "terminalReason" in input ? "interrupted" : "failed");
7924+
assert.equal(
7925+
terminal.status,
7926+
"terminalReason" in input && input.terminalReason === "aborted_streaming"
7927+
? "interrupted"
7928+
: "failed",
7929+
);
77097930
if (terminal.status === "failed") {
7710-
assert.equal(terminal.failure.message === input.text.slice(0, 500).trim(), !input.shown);
7931+
if (!input.shown) assert.isTrue(input.text.startsWith(terminal.failure.message));
7932+
if ("failure" in input) assert.equal(terminal.failure.message, input.failure);
77117933
} else {
77127934
assert.equal(terminal.failure, null);
77137935
}
@@ -7716,7 +7938,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
77167938
? [event.message.text]
77177939
: [],
77187940
);
7719-
assert.deepEqual(assistantTexts, input.shown ? [input.text] : []);
7941+
assert.deepEqual(
7942+
assistantTexts,
7943+
!input.shown
7944+
? []
7945+
: "followingText" in input
7946+
? [input.text, input.followingText]
7947+
: [input.text],
7948+
);
77207949
}).pipe(
77217950
Effect.provide(
77227951
Layer.mergeAll(

0 commit comments

Comments
 (0)