Skip to content

Commit 57c6a76

Browse files
committed
fix(claude): follow the session model after a refusal fallback
- Report Claude's session-scoped model_refusal_fallback as a new app_thread.model_selection.updated provider event - Move the live query's selection to the fallback model, so picking the original model again calls setModel - Write the thread's model selection under its lock, sharing the subagent model sync, and leave threads moved to another provider alone
1 parent eb1510d commit 57c6a76

6 files changed

Lines changed: 204 additions & 26 deletions

File tree

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,6 +3323,83 @@ describe("ClaudeAdapterV2 background wake turns", () => {
33233323
),
33243324
);
33253325

3326+
it.effect("moves the thread to the model a session refusal fallback switched to", () =>
3327+
Effect.gen(function* () {
3328+
const harness = yield* makeWakeHarness;
3329+
const now = yield* DateTime.now;
3330+
const original = {
3331+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
3332+
model: "claude-opus-5-5",
3333+
} satisfies ModelSelection;
3334+
const fallback = { ...original, model: "claude-opus-4-8" };
3335+
const turn = (ordinal: number, modelSelection: ModelSelection) =>
3336+
makeClaudeTestTurnInput({
3337+
threadId: harness.threadId,
3338+
providerThread: harness.providerThread,
3339+
now,
3340+
attemptId: RunAttemptId.make(`attempt-refusal-fallback:${ordinal}`),
3341+
text: `Request ${ordinal}`,
3342+
attachments: [],
3343+
providerTurnOrdinal: ordinal,
3344+
modelSelection,
3345+
});
3346+
const settle = Effect.gen(function* () {
3347+
yield* harness.offerAndWait(turnOneResult);
3348+
yield* Queue.take(harness.terminalReceipts);
3349+
});
3350+
const refusalFallback = (scope: "session" | "local", uuid: string) =>
3351+
claudeSdkFrame({
3352+
type: "system",
3353+
subtype: "model_refusal_fallback",
3354+
trigger: "refusal",
3355+
direction: "retry",
3356+
scope,
3357+
original_model: original.model,
3358+
fallback_model: fallback.model,
3359+
request_id: null,
3360+
content: "Switched to Opus 4.8",
3361+
uuid,
3362+
session_id: WAKE_NATIVE_SESSION,
3363+
});
3364+
3365+
yield* harness.runtime.startTurn(turn(1, original));
3366+
yield* Queue.offer(
3367+
harness.sdkMessages,
3368+
refusalFallback("local", "00000000-0000-4000-8000-000000000401"),
3369+
);
3370+
yield* Queue.offer(
3371+
harness.sdkMessages,
3372+
refusalFallback("session", "00000000-0000-4000-8000-000000000402"),
3373+
);
3374+
yield* settle;
3375+
assert.deepEqual(
3376+
harness.events.flatMap((event) =>
3377+
event.type === "app_thread.model_selection.updated" ? [event] : [],
3378+
),
3379+
[
3380+
{
3381+
type: "app_thread.model_selection.updated",
3382+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
3383+
threadId: harness.threadId,
3384+
modelSelection: fallback,
3385+
},
3386+
],
3387+
);
3388+
3389+
yield* harness.runtime.startTurn(turn(2, fallback));
3390+
yield* settle;
3391+
assert.deepEqual(harness.liveSelectionChanges, []);
3392+
yield* harness.runtime.startTurn(turn(3, original));
3393+
yield* settle;
3394+
assert.deepEqual(harness.liveSelectionChanges[0], { model: original.model });
3395+
}).pipe(
3396+
Effect.scoped,
3397+
Effect.provide(
3398+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3399+
),
3400+
),
3401+
);
3402+
33263403
it.effect.each(["completed", "interrupted"] as const)(
33273404
"projects Claude thinking blocks when %s",
33283405
(status) =>

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7887,6 +7887,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
78877887
) {
78887888
const now = yield* DateTime.now;
78897889
const nativeItemId = message.uuid;
7890+
// "local" covers a subagent or side question; absent (older CLIs) means "session".
7891+
if (message.subtype === "model_refusal_fallback" && message.scope !== "local") {
7892+
const modelSelection = {
7893+
...context.input.modelSelection,
7894+
model: resolveClaudeModelSlug(subagentModelCatalog, message.fallback_model),
7895+
};
7896+
const live = yield* Ref.get(queryContext);
7897+
if (live !== null) live.selection = compileClaudeModelSelection(modelSelection);
7898+
yield* emitProviderEvent({
7899+
type: "app_thread.model_selection.updated",
7900+
driver: CLAUDE_PROVIDER,
7901+
threadId: context.input.threadId,
7902+
modelSelection,
7903+
});
7904+
}
78907905
context.assistant.noticeTexts.add(message.content);
78917906
yield* emitProviderEvent({
78927907
type: "turn_item.updated",

‎apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,4 +2116,50 @@ layer("ProviderEventIngestorV2", (it) => {
21162116
);
21172117
}),
21182118
);
2119+
2120+
it.effect("moves a thread to the model its provider switched the session to", () =>
2121+
Effect.gen(function* () {
2122+
const now = yield* DateTime.now;
2123+
const eventSink = yield* EventSink.EventSinkV2;
2124+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2125+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
2126+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2127+
const rootEvent = yield* threadCreatedEvent(now);
2128+
const threadId = rootEvent.threadId;
2129+
if (threadId === undefined) throw new Error("Expected a thread fixture event");
2130+
yield* eventSink.write({ events: [rootEvent] });
2131+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2132+
providerInstanceId: modelSelection.instanceId,
2133+
threadId,
2134+
});
2135+
const reportModel = (reported: ModelSelection) =>
2136+
ingestor.ingestNormalized({
2137+
providerSessionId,
2138+
providerInstanceId: modelSelection.instanceId,
2139+
threadId,
2140+
event: {
2141+
type: "app_thread.model_selection.updated",
2142+
driver: CODEX_DRIVER,
2143+
threadId,
2144+
modelSelection: reported,
2145+
},
2146+
});
2147+
const switched = { ...modelSelection, model: "gpt-fallback" };
2148+
2149+
const first = yield* reportModel(switched);
2150+
const repeated = yield* reportModel(switched);
2151+
const otherInstance = yield* reportModel({
2152+
...switched,
2153+
instanceId: ProviderInstanceId.make("codex-work"),
2154+
});
2155+
2156+
assert.deepEqual(
2157+
first.map((stored) => stored.event.type),
2158+
["thread.model-selection-updated"],
2159+
);
2160+
assert.lengthOf(repeated, 0);
2161+
assert.lengthOf(otherInstance, 0);
2162+
assert.deepEqual((yield* projectionStore.getThread(threadId)).modelSelection, switched);
2163+
}),
2164+
);
21192165
});

‎apps/server/src/orchestration-v2/ProviderEventIngestor.ts‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -391,35 +391,24 @@ export const layer: Layer.Layer<
391391
);
392392

393393
/**
394-
* A native subagent's thread starts on the parent's model when the provider
395-
* names the real one later (a Claude agent file's model arrives with the
396-
* subagent's first reply). Thread commands rewrite the whole thread row
397-
* under the thread's lock, so this read and write take it too.
394+
* Thread commands rewrite the whole thread row under the thread's lock, so
395+
* this read and write take it too.
398396
*/
399-
const syncSubagentThreadModel = Effect.fn("ProviderEventIngestor.syncSubagentThreadModel")(
400-
function* (input: ProviderEventIngestInput, subagent: OrchestrationV2Subagent) {
401-
const { childThreadId, model } = subagent;
402-
if (
403-
subagent.origin !== "provider_native" ||
404-
childThreadId === null ||
405-
(model === null && subagent.modelSelection === undefined)
406-
) {
407-
return [];
408-
}
409-
const reportedSelection = ({ modelSelection }: { modelSelection: ModelSelection }) =>
410-
subagent.modelSelection ??
411-
(model === null || model === modelSelection.model
412-
? modelSelection
413-
: { instanceId: modelSelection.instanceId, model });
414-
const staleThread = projections.getThread(childThreadId).pipe(
397+
const syncThreadModelSelection = Effect.fn("ProviderEventIngestor.syncThreadModelSelection")(
398+
function* (
399+
input: ProviderEventIngestInput,
400+
threadId: ThreadId,
401+
reportedSelection: (thread: { modelSelection: ModelSelection }) => ModelSelection,
402+
) {
403+
const staleThread = projections.getThread(threadId).pipe(
415404
Effect.map((thread) =>
416405
modelSelectionsEqual(thread.modelSelection, reportedSelection(thread)) ? null : thread,
417406
),
418407
Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }),
419408
);
420409
if ((yield* staleThread) === null) return [];
421410
return yield* threadCommands.withLock(
422-
childThreadId,
411+
threadId,
423412
Effect.gen(function* () {
424413
const thread = yield* staleThread;
425414
if (thread === null) return [];
@@ -440,6 +429,33 @@ export const layer: Layer.Layer<
440429
},
441430
);
442431

432+
/**
433+
* A native subagent's thread starts on the parent's model when the provider
434+
* names the real one later (a Claude agent file's model arrives with the
435+
* subagent's first reply).
436+
*/
437+
const syncSubagentThreadModel = Effect.fn("ProviderEventIngestor.syncSubagentThreadModel")(
438+
function* (input: ProviderEventIngestInput, subagent: OrchestrationV2Subagent) {
439+
const { childThreadId, model } = subagent;
440+
if (
441+
subagent.origin !== "provider_native" ||
442+
childThreadId === null ||
443+
(model === null && subagent.modelSelection === undefined)
444+
) {
445+
return [];
446+
}
447+
return yield* syncThreadModelSelection(
448+
input,
449+
childThreadId,
450+
({ modelSelection }) =>
451+
subagent.modelSelection ??
452+
(model === null || model === modelSelection.model
453+
? modelSelection
454+
: { instanceId: modelSelection.instanceId, model }),
455+
);
456+
},
457+
);
458+
443459
/**
444460
* Retire the runs that fell off its active branch with the same run,
445461
* root-node, and checkpoint updates CheckpointRollbackService writes for a
@@ -555,6 +571,9 @@ export const layer: Layer.Layer<
555571
}),
556572
];
557573
}
574+
case "app_thread.model_selection.updated":
575+
// Written under the thread's lock after ingestion.
576+
return [];
558577
case "provider_session.updated":
559578
return [
560579
yield* makeDomainEvent(input, {
@@ -815,10 +834,22 @@ export const layer: Layer.Layer<
815834
),
816835
),
817836
),
818-
Effect.flatMap((storedEvents) =>
819-
storedEvents.length === 0 || input.event.type !== "subagent.updated"
837+
Effect.flatMap((storedEvents) => {
838+
const event = input.event;
839+
const sync =
840+
event.type === "app_thread.model_selection.updated"
841+
? // A thread the user has since moved to another provider keeps its choice.
842+
syncThreadModelSelection(input, event.threadId, ({ modelSelection }) =>
843+
modelSelection.instanceId === event.modelSelection.instanceId
844+
? event.modelSelection
845+
: modelSelection,
846+
)
847+
: storedEvents.length > 0 && event.type === "subagent.updated"
848+
? syncSubagentThreadModel(input, event.subagent)
849+
: null;
850+
return sync === null
820851
? Effect.succeed(storedEvents)
821-
: syncSubagentThreadModel(input, input.event.subagent).pipe(
852+
: sync.pipe(
822853
Effect.map((synced) => [...storedEvents, ...synced]),
823854
Effect.mapError(
824855
(cause) =>
@@ -828,8 +859,8 @@ export const layer: Layer.Layer<
828859
cause,
829860
}),
830861
),
831-
),
832-
),
862+
);
863+
}),
833864
Effect.tap((storedEvents) =>
834865
Effect.gen(function* () {
835866
if (storedEvents.length === 0 || input.event.type !== "provider_turn.updated")

‎apps/server/src/orchestration-v2/RunExecutionService.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ export function routeProviderEvent(
398398
},
399399
];
400400
}
401+
case "app_thread.model_selection.updated":
402+
return [ownsThread(event.threadId), state];
401403
case "provider_thread.updated": {
402404
const belongs =
403405
state.ownedProviderThreadIds.has(event.providerThread.id) ||

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ export const ProviderAdapterV2Event = Schema.Union([
8383
appThread: OrchestrationV2AppThread,
8484
createOnly: Schema.optional(Schema.Boolean),
8585
}),
86+
/** The provider moved the thread's session to another model on its own, such as Claude's refusal fallback. */
87+
Schema.Struct({
88+
type: Schema.Literal("app_thread.model_selection.updated"),
89+
driver: ProviderDriverKind,
90+
threadId: ThreadId,
91+
modelSelection: ModelSelection,
92+
}),
8693
Schema.Struct({
8794
type: Schema.Literal("provider_session.updated"),
8895
driver: ProviderDriverKind,

0 commit comments

Comments
 (0)