Skip to content

Commit b3eb880

Browse files
only21milclaude
andcommitted
fix(claude): safeguard refusals show their error once
Claude Code reports an API failure as an assistant frame with `error` set and again as the turn's result. The adapter turned the frame into a reply, so the refusal showed as an assistant message and as the turn's failure. The frame now waits until the turn ends and is dropped when the failure carries the same text. Fixes #24171 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d53873b commit b3eb880

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,6 +3935,65 @@ describe("ClaudeAdapterV2 background wake turns", () => {
39353935
),
39363936
);
39373937

3938+
// regression: #24171
3939+
it.effect.each([
3940+
{
3941+
name: "drops the CLI's API error frame when the failure repeats it",
3942+
text: "API Error: Claude Opus 5.5's safeguards flagged this message (https://www.anthropic.com/legal/aup).",
3943+
apiErrorStatus: 400,
3944+
shown: false,
3945+
},
3946+
{
3947+
name: "keeps the CLI's API error frame when the failure says something else",
3948+
text: "API Error: 429 Specific rate limit detail.",
3949+
apiErrorStatus: 429,
3950+
shown: true,
3951+
},
3952+
])("$name", (input) =>
3953+
Effect.gen(function* () {
3954+
const harness = yield* makeWakeHarness;
3955+
yield* harness.runtime.startTurn(
3956+
makeClaudeTestTurnInput({
3957+
threadId: harness.threadId,
3958+
providerThread: harness.providerThread,
3959+
now: yield* DateTime.now,
3960+
attemptId: RunAttemptId.make("attempt-claude-api-error-frame"),
3961+
text: "Continue.",
3962+
attachments: [],
3963+
}),
3964+
);
3965+
yield* Queue.offerAll(harness.sdkMessages, [
3966+
makeAssistantErrorFrame({
3967+
uuid: "00000000-0000-4000-8000-000000000610",
3968+
error: "invalid_request",
3969+
text: input.text,
3970+
}),
3971+
makeResultFrame({
3972+
uuid: "00000000-0000-4000-8000-000000000611",
3973+
result: input.text,
3974+
isError: true,
3975+
apiErrorStatus: input.apiErrorStatus,
3976+
terminalReason: "api_error",
3977+
}),
3978+
]);
3979+
3980+
const terminal = yield* Queue.take(harness.terminalReceipts);
3981+
assert.equal(terminal.status, "failed");
3982+
if (terminal.status !== "failed") return;
3983+
assert.equal(terminal.failure.message === input.text, !input.shown);
3984+
const assistantTexts = harness.events.flatMap((event) =>
3985+
event.type === "message.updated" && event.message.role === "assistant"
3986+
? [event.message.text]
3987+
: [],
3988+
);
3989+
assert.deepEqual(assistantTexts, input.shown ? [input.text] : []);
3990+
}).pipe(
3991+
Effect.provide(
3992+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3993+
),
3994+
),
3995+
);
3996+
39383997
const lastProviderTurn = (events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>) => {
39393998
const event = events.findLast((candidate) => candidate.type === "provider_turn.updated");
39403999
if (event?.type !== "provider_turn.updated") {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,10 @@ interface ActiveClaudeTurnContext {
34153415
emittedNativeItemIds: Set<string>;
34163416
// The CLI repeats a notice that ends a prompt as the turn's result text.
34173417
noticeTexts: Set<string>;
3418+
// The CLI reports an API failure as an assistant frame and again as the
3419+
// turn's result. The frame waits for the turn's end so the failure can
3420+
// show it once.
3421+
apiErrorFrame: { readonly nativeItemId: string; readonly text: string } | null;
34183422
};
34193423
readonly reasoning: {
34203424
messageId: string | null;
@@ -6706,6 +6710,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
67066710
subagentToolCalls.delete(toolCall.nativeItemId);
67076711
}
67086712

6713+
const apiErrorFrame = input.context.assistant.apiErrorFrame;
6714+
input.context.assistant.apiErrorFrame = null;
6715+
if (
6716+
apiErrorFrame !== null &&
6717+
input.failure?.message !== makeProviderFailure({ message: apiErrorFrame.text }).message
6718+
) {
6719+
yield* emitAssistantTextArtifacts({ context: input.context, ...apiErrorFrame });
6720+
}
6721+
67096722
if (
67106723
input.context.assistant.emittedNativeItemIds.size === 0 &&
67116724
input.context.assistant.fallbackText.length > 0
@@ -8485,6 +8498,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84858498
return;
84868499
}
84878500
if (assistantText !== null && assistantText.text.length > 0) {
8501+
const heldApiError = context.assistant.apiErrorFrame;
8502+
context.assistant.apiErrorFrame = null;
8503+
if (heldApiError !== null) {
8504+
yield* emitAssistantTextArtifacts({ context, ...heldApiError });
8505+
}
8506+
if (message.type === "assistant" && message.error !== undefined) {
8507+
context.assistant.apiErrorFrame = assistantText;
8508+
return;
8509+
}
84888510
yield* emitAssistantTextArtifacts({
84898511
context,
84908512
nativeItemId: assistantText.nativeItemId,
@@ -9516,6 +9538,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
95169538
fallbackNativeItemId: `assistant:${turnInput.runId}`,
95179539
emittedNativeItemIds: new Set(),
95189540
noticeTexts: new Set(),
9541+
apiErrorFrame: null,
95199542
},
95209543
reasoning: {
95219544
messageId: null,

0 commit comments

Comments
 (0)