Skip to content

Commit 44ff99b

Browse files
committed
fix(claude): ask before retrying a flagged message on the fallback model
- Declare the refusal_fallback_prompt dialog so the CLI pauses instead of ending the turn when switchModelsOnFlag is off - Render it as a pending question offering a retry on the CLI's fallback model or stop - Share the dialog question helper with the resume_return prompt
1 parent d7a8d6f commit 44ff99b

2 files changed

Lines changed: 130 additions & 44 deletions

File tree

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,56 @@ describe("ClaudeAdapterV2 resume compaction", () => {
13521352
});
13531353
assert.deepEqual(yield* Fiber.join(dialog), { behavior: "completed", result: "compact" });
13541354

1355+
assert.deepEqual(openedOptions?.supportedDialogKinds, [
1356+
"resume_return",
1357+
"refusal_fallback_prompt",
1358+
]);
1359+
const answerRefusalDialog = (requestId: string, answer: string) =>
1360+
Effect.gen(function* () {
1361+
const refusalRequestEvent = yield* runtime.events.pipe(
1362+
Stream.filter(
1363+
(event) =>
1364+
event.type === "runtime_request.updated" &&
1365+
event.runtimeRequest.status === "pending",
1366+
),
1367+
Stream.runHead,
1368+
Effect.forkScoped,
1369+
);
1370+
const refusalDialog = yield* Effect.promise(() =>
1371+
callback!(
1372+
{
1373+
dialogKind: "refusal_fallback_prompt",
1374+
payload: {
1375+
originalModel: "claude-opus-5-5",
1376+
fallbackModel: "claude-opus-4-8",
1377+
apiRefusalCategory: "cyber",
1378+
},
1379+
},
1380+
{ signal: new AbortController().signal, requestId },
1381+
),
1382+
).pipe(Effect.forkScoped);
1383+
const refusalEvent = yield* Fiber.join(refusalRequestEvent);
1384+
assert.isTrue(Option.isSome(refusalEvent));
1385+
if (
1386+
Option.isNone(refusalEvent) ||
1387+
refusalEvent.value.type !== "runtime_request.updated"
1388+
) {
1389+
return yield* Effect.die("missing refusal question");
1390+
}
1391+
yield* runtime.respondToRuntimeRequest({
1392+
requestId: refusalEvent.value.runtimeRequest.id,
1393+
answers: { "Claude Opus 5.5's safeguards flagged this message.": answer },
1394+
});
1395+
return yield* Fiber.join(refusalDialog);
1396+
});
1397+
assert.deepEqual(
1398+
yield* answerRefusalDialog("dialog-refusal-1", "Retry with Claude Opus 4.8"),
1399+
{ behavior: "completed", result: "retry_fallback" },
1400+
);
1401+
assert.deepEqual(yield* answerRefusalDialog("dialog-refusal-2", "Stop"), {
1402+
behavior: "cancelled",
1403+
});
1404+
13551405
const cancelledController = new AbortController();
13561406
cancelledController.abort();
13571407
assert.deepEqual(

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

Lines changed: 80 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
type SDKRateLimitInfo,
4545
type SDKResultMessage,
4646
type SDKUserMessage,
47+
type UserDialogRequest,
4748
} from "@anthropic-ai/claude-agent-sdk";
4849
import type {
4950
AskUserQuestionInput,
@@ -56,7 +57,7 @@ import { parseCliArgs } from "@t3tools/shared/cliArgs";
5657
import { fromLenientJson } from "@t3tools/shared/schemaJson";
5758
import { AgentScope } from "@t3tools/shared/AgentScope";
5859
import * as HostProcess from "@t3tools/shared/HostProcess";
59-
import { applyClaudePromptEffortPrefix } from "@t3tools/shared/model";
60+
import { applyClaudePromptEffortPrefix, formatModelSlugName } from "@t3tools/shared/model";
6061
import {
6162
CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
6263
formatClaudeResumeCompactionQuestion,
@@ -9053,12 +9054,68 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90539054
const canUseTool: CanUseTool = (toolName, toolInput, callbackOptions) =>
90549055
runPromise(canUseToolEffect(toolName, toolInput, callbackOptions));
90559056

9057+
const askDialogQuestion = (
9058+
request: UserDialogRequest,
9059+
callbackOptions: { readonly signal: AbortSignal; readonly requestId: string },
9060+
prompt: {
9061+
readonly header: string;
9062+
readonly question: string;
9063+
readonly options: ReadonlyArray<{
9064+
readonly label: string;
9065+
readonly description: string;
9066+
}>;
9067+
},
9068+
) =>
9069+
canUseToolEffect(
9070+
"AskUserQuestion",
9071+
{ questions: [{ ...prompt, multiSelect: false }] },
9072+
{
9073+
signal: callbackOptions.signal,
9074+
requestId: callbackOptions.requestId,
9075+
toolUseID: request.toolUseID ?? callbackOptions.requestId,
9076+
},
9077+
).pipe(
9078+
Effect.map((result) => {
9079+
if (result.behavior !== "allow") return Option.none();
9080+
const answers =
9081+
result.updatedInput === undefined
9082+
? undefined
9083+
: Reflect.get(result.updatedInput, "answers");
9084+
return Option.some(
9085+
typeof answers === "object" && answers !== null
9086+
? Reflect.get(answers, prompt.question)
9087+
: undefined,
9088+
);
9089+
}),
9090+
);
9091+
90569092
const onUserDialog: NonNullable<ClaudeQueryOptions["onUserDialog"]> = (
90579093
request,
90589094
callbackOptions,
90599095
) =>
90609096
runPromise(
90619097
Effect.gen(function* () {
9098+
if (request.dialogKind === "refusal_fallback_prompt") {
9099+
const { originalModel, fallbackModel } = request.payload;
9100+
if (typeof originalModel !== "string" || typeof fallbackModel !== "string") {
9101+
return { behavior: "cancelled" as const };
9102+
}
9103+
const retryLabel = `Retry with ${formatModelSlugName(fallbackModel)}`;
9104+
const selection = yield* askDialogQuestion(request, callbackOptions, {
9105+
header: "Message flagged",
9106+
question: `${formatModelSlugName(originalModel)}'s safeguards flagged this message.`,
9107+
options: [
9108+
{
9109+
label: retryLabel,
9110+
description: "Send the same message again on the fallback model.",
9111+
},
9112+
{ label: "Stop", description: "End the turn without a response." },
9113+
],
9114+
});
9115+
return Option.isSome(selection) && selection.value === retryLabel
9116+
? { behavior: "completed" as const, result: "retry_fallback" as const }
9117+
: { behavior: "cancelled" as const };
9118+
}
90629119
if (request.dialogKind !== "resume_return") {
90639120
return { behavior: "cancelled" as const };
90649121
}
@@ -9087,52 +9144,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90879144
ageMinutes,
90889145
estimatedTokens,
90899146
});
9090-
const result = yield* canUseToolEffect(
9091-
"AskUserQuestion",
9092-
{
9093-
questions: [
9094-
{
9095-
header: "Resume session",
9096-
question,
9097-
options: [
9098-
{
9099-
label: "Compact and continue",
9100-
description: "Resume with a summary and use fewer tokens.",
9101-
},
9102-
{
9103-
label: "Keep full history",
9104-
description: "Resume without changing the conversation.",
9105-
},
9106-
{
9107-
label: CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
9108-
description: "Keep full history and skip future resume prompts.",
9109-
},
9110-
],
9111-
multiSelect: false,
9112-
},
9113-
],
9114-
},
9115-
{
9116-
signal: callbackOptions.signal,
9117-
requestId: callbackOptions.requestId,
9118-
toolUseID: request.toolUseID ?? callbackOptions.requestId,
9119-
},
9120-
);
9121-
if (result.behavior !== "allow") return { behavior: "cancelled" as const };
9122-
const answers =
9123-
result.updatedInput === undefined
9124-
? undefined
9125-
: Reflect.get(result.updatedInput, "answers");
9126-
const selection =
9127-
typeof answers === "object" && answers !== null
9128-
? Reflect.get(answers, question)
9129-
: undefined;
9147+
const selection = yield* askDialogQuestion(request, callbackOptions, {
9148+
header: "Resume session",
9149+
question,
9150+
options: [
9151+
{
9152+
label: "Compact and continue",
9153+
description: "Resume with a summary and use fewer tokens.",
9154+
},
9155+
{
9156+
label: "Keep full history",
9157+
description: "Resume without changing the conversation.",
9158+
},
9159+
{
9160+
label: CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
9161+
description: "Keep full history and skip future resume prompts.",
9162+
},
9163+
],
9164+
});
9165+
if (Option.isNone(selection)) return { behavior: "cancelled" as const };
91309166
return {
91319167
behavior: "completed" as const,
91329168
result:
9133-
selection === "Compact and continue"
9169+
selection.value === "Compact and continue"
91349170
? ("compact" as const)
9135-
: selection === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER
9171+
: selection.value === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER
91369172
? ("never" as const)
91379173
: ("continue" as const),
91389174
};
@@ -9315,7 +9351,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93159351
}),
93169352
canUseTool,
93179353
onUserDialog,
9318-
supportedDialogKinds: ["resume_return"],
9354+
supportedDialogKinds: ["resume_return", "refusal_fallback_prompt"],
93199355
...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }),
93209356
});
93219357
const querySession = yield* queryRunner

0 commit comments

Comments
 (0)