Skip to content

Commit 2158a34

Browse files
vitalyiegorovclaude
andcommitted
fix(server,mobile): a model pick applies to the message it was sent with
Unpicked client follow-ups queue on the newest queued pick, skipping monitor updates; goal commands and server-created messages keep the thread's model. Mobile clears a delivered pick from the draft once drafts have loaded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 93f6519 commit 2158a34

6 files changed

Lines changed: 239 additions & 1 deletion

File tree

‎apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ vi.mock("./use-composer-drafts", async () => {
152152
}),
153153
replaceComposerDraftAttachments: vi.fn(),
154154
undoComposerDraftMerge: vi.fn(),
155+
releaseDeliveredComposerModelPick: vi.fn(async () => {}),
155156
updateComposerDraftSettings: vi.fn(),
156157
waitForComposerDraftsLoaded: vi.fn(),
157158
};

‎apps/mobile/src/state/threadOutboxDelivery.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
undoComposerDraftMerge,
8383
updateComposerDraftSettings,
8484
waitForComposerDraftsLoaded,
85+
releaseDeliveredComposerModelPick,
8586
} from "./use-composer-drafts";
8687
import {
8788
dispatchingQueuedMessageIdAtom,
@@ -913,6 +914,12 @@ export function createThreadOutboxDelivery(): {
913914
return restoreQueuedMessage(persistedMessage, failure.message);
914915
}
915916
acknowledgedExistingThreadMessageIds.add(persistedMessage.messageId);
917+
if (queuedMessage.modelSelection) {
918+
await releaseDeliveredComposerModelPick(
919+
scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId),
920+
queuedMessage.modelSelection,
921+
);
922+
}
916923
const delivered =
917924
(await completeQueuedMessageDelivery(
918925
persistedMessage,

‎apps/mobile/src/state/use-composer-drafts.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ import {
181181
archiveCloudComposerDrafts,
182182
clearComposerDraftContent,
183183
clearComposerDraftContentState,
184+
releaseDeliveredComposerModelPick,
184185
subscribeComposerDraftModelReconciliation,
185186
clearComposerDraftsEnvironment,
186187
ComposerDraftPersistenceError,
@@ -548,6 +549,23 @@ describe("mobile composer drafts", () => {
548549
expect(getComposerDraftSnapshot(key)).toEqual({ ...before, modelSelection: undefined });
549550
});
550551

552+
it("releases a pick once a message carrying it is delivered", async () => {
553+
const key = "environment-1:thread-1";
554+
const pick = { instanceId: ProviderInstanceId.make("claudeAgent"), model: "claude-opus-5-5" };
555+
setComposerDraftText(key, "next follow-up");
556+
updateComposerDraftSettings(key, { modelSelection: pick, interactionMode: "plan" });
557+
const before = getComposerDraftSnapshot(key);
558+
559+
await releaseDeliveredComposerModelPick(key, { ...pick, model: "claude-sonnet-5-5" });
560+
expect(getComposerDraftSnapshot(key)).toEqual(before);
561+
562+
await releaseDeliveredComposerModelPick(key, {
563+
...pick,
564+
options: [{ id: "fastMode", value: false }],
565+
});
566+
expect(getComposerDraftSnapshot(key)).toEqual({ ...before, modelSelection: undefined });
567+
});
568+
551569
it("retains an unavailable provider pick through differing durable model updates", () => {
552570
const environmentId = EnvironmentId.make("environment-1");
553571
const threadId = ThreadId.make("thread-1");

‎apps/mobile/src/state/use-composer-drafts.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,21 @@ export function updateComposerDraftSettings(
14901490
});
14911491
}
14921492

1493+
export async function releaseDeliveredComposerModelPick(
1494+
draftKey: string,
1495+
delivered: ModelSelection,
1496+
): Promise<void> {
1497+
await waitForComposerDraftsLoaded();
1498+
updateComposerDrafts((current) => {
1499+
const draft = current[draftKey];
1500+
if (!draft?.modelSelection || !draftModelPickMatchesThread(draft.modelSelection, delivered)) {
1501+
return current;
1502+
}
1503+
const { modelSelection: _modelSelection, ...rest } = draft;
1504+
return withComposerDraft(current, draftKey, rest);
1505+
});
1506+
}
1507+
14931508
export function subscribeComposerDraftModelReconciliation(input: {
14941509
readonly registry: AtomRegistry.AtomRegistry;
14951510
readonly threadRefsAtom: Atom.Atom<ReadonlyArray<ScopedThreadRef>>;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5175,7 +5175,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
51755175
]);
51765176
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
51775177
}
5178-
const modelSelection = command.modelSelection ?? projection.thread.modelSelection;
5178+
let modelSelection = command.modelSelection ?? projection.thread.modelSelection;
51795179
let dispatchMode = resolveMessageDispatchIntent(
51805180
projection,
51815181
command.dispatchMode,
@@ -5461,6 +5461,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54615461
command.createdBy === "user" &&
54625462
(command.creationSource === "web" || command.creationSource === "mobile") &&
54635463
command.scheduledTaskId === undefined;
5464+
// A queued pick reaches the thread only when its run starts; unpicked messages behind it follow it.
5465+
// Monitor updates follow whatever runs before them; goal commands stay on the provider validated above.
5466+
const newestQueuedPick =
5467+
shouldQueue &&
5468+
sentByUserFromClient &&
5469+
command.modelSelection === undefined &&
5470+
parsedGoalCommand === null
5471+
? queuedRunsInDeliveryOrder(projection).findLast(
5472+
(run) =>
5473+
!followsCurrentConversation(
5474+
projection.messages.find((message) => message.id === run.userMessageId),
5475+
),
5476+
)
5477+
: undefined;
5478+
if (newestQueuedPick) modelSelection = newestQueuedPick.modelSelection;
54645479
const heldRunsToRelease =
54655480
sentByUserFromClient && startsOrQueuesRun && !worktreeSetupUnresolved
54665481
? projection.runs.filter((run) => run.status === "queued" && run.queueHeld === true)

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,188 @@ describe("orchestration v2 provider switching", () => {
18841884
),
18851885
);
18861886

1887+
it.live("queues an unpicked follow-up on the newest queued pick", () =>
1888+
Effect.scoped(
1889+
Effect.gen(function* () {
1890+
const cwd = yield* checkpointWorkspace("queued-unpicked-follow-up");
1891+
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1892+
const started = yield* Deferred.make<void>();
1893+
const threadId = ThreadId.make("thread:queued-unpicked-follow-up");
1894+
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1895+
makeTestAdapter({
1896+
instanceId: CODEX_MODEL_SELECTION.instanceId,
1897+
driver: CODEX_DRIVER,
1898+
capabilities: CodexProviderCapabilitiesV2,
1899+
modelSelection: CODEX_MODEL_SELECTION,
1900+
responseByRunOrdinal: {},
1901+
capturedTurns,
1902+
holdFirstTurn: started,
1903+
}),
1904+
makeTestAdapter({
1905+
instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1906+
driver: CLAUDE_DRIVER,
1907+
capabilities: ClaudeProviderCapabilitiesV2,
1908+
modelSelection: CLAUDE_MODEL_SELECTION,
1909+
responseByRunOrdinal: {},
1910+
capturedTurns,
1911+
}),
1912+
]);
1913+
yield* Effect.gen(function* () {
1914+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1915+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1916+
yield* orchestrator.dispatch({
1917+
type: "thread.create",
1918+
createdBy: "user",
1919+
creationSource: "web",
1920+
commandId: CommandId.make("command:queued-unpicked-follow-up:create"),
1921+
threadId,
1922+
projectId: ProjectId.make("project:queued-unpicked-follow-up"),
1923+
title: "Queued unpicked follow-up",
1924+
modelSelection: CODEX_MODEL_SELECTION,
1925+
runtimeMode: "full-access",
1926+
interactionMode: "default",
1927+
branch: null,
1928+
worktreePath: cwd,
1929+
});
1930+
yield* orchestrator.dispatch({
1931+
type: "message.dispatch",
1932+
createdBy: "user",
1933+
creationSource: "web",
1934+
commandId: CommandId.make("command:queued-unpicked-follow-up:first"),
1935+
threadId,
1936+
messageId: MessageId.make("message:queued-unpicked-follow-up:first"),
1937+
text: "Current Codex turn",
1938+
attachments: [],
1939+
modelSelection: CODEX_MODEL_SELECTION,
1940+
dispatchMode: { type: "start_immediately" },
1941+
});
1942+
yield* Deferred.await(started);
1943+
yield* worker.drain();
1944+
yield* orchestrator.dispatch({
1945+
type: "message.dispatch",
1946+
createdBy: "user",
1947+
creationSource: "mobile",
1948+
commandId: CommandId.make("command:queued-unpicked-follow-up:pick"),
1949+
threadId,
1950+
messageId: MessageId.make("message:queued-unpicked-follow-up:pick"),
1951+
text: "Picked Claude",
1952+
attachments: [],
1953+
modelSelection: CLAUDE_MODEL_SELECTION,
1954+
dispatchMode: { type: "queue_after_active" },
1955+
});
1956+
yield* orchestrator.dispatch({
1957+
type: "message.dispatch",
1958+
createdBy: "user",
1959+
creationSource: "mobile",
1960+
commandId: CommandId.make("command:queued-unpicked-follow-up:follow-up"),
1961+
threadId,
1962+
messageId: MessageId.make("message:queued-unpicked-follow-up:follow-up"),
1963+
text: "Follow-up without a pick",
1964+
attachments: [],
1965+
dispatchMode: { type: "queue_after_active" },
1966+
});
1967+
const projection = yield* orchestrator.getThreadProjection(threadId);
1968+
assert.deepEqual(
1969+
projection.runs.map((run) => [run.status, run.modelSelection.instanceId]),
1970+
[
1971+
["running", CODEX_MODEL_SELECTION.instanceId],
1972+
["queued", CLAUDE_MODEL_SELECTION.instanceId],
1973+
["queued", CLAUDE_MODEL_SELECTION.instanceId],
1974+
],
1975+
);
1976+
yield* orchestrator.dispatch({
1977+
type: "message.dispatch",
1978+
createdBy: "agent",
1979+
creationSource: "server",
1980+
commandId: CommandId.make("command:queued-unpicked-follow-up:monitor"),
1981+
threadId,
1982+
messageId: MessageId.make("message:queued-unpicked-follow-up:monitor"),
1983+
text: "Monitor update",
1984+
attachments: [],
1985+
dispatchMode: { type: "queue_after_active" },
1986+
notification: { source: { kind: "monitor" }, outcome: "updated", summary: "Update" },
1987+
});
1988+
yield* orchestrator.dispatch({
1989+
type: "message.dispatch",
1990+
createdBy: "user",
1991+
creationSource: "mobile",
1992+
commandId: CommandId.make("command:queued-unpicked-follow-up:after-monitor"),
1993+
threadId,
1994+
messageId: MessageId.make("message:queued-unpicked-follow-up:after-monitor"),
1995+
text: "Follow-up after a monitor update",
1996+
attachments: [],
1997+
dispatchMode: { type: "queue_after_active" },
1998+
});
1999+
const afterMonitor = yield* orchestrator.getThreadProjection(threadId);
2000+
assert.equal(
2001+
afterMonitor.runs.find(
2002+
(run) => run.userMessageId === "message:queued-unpicked-follow-up:after-monitor",
2003+
)?.modelSelection.instanceId,
2004+
CLAUDE_MODEL_SELECTION.instanceId,
2005+
);
2006+
yield* orchestrator.dispatch({
2007+
type: "message.dispatch",
2008+
createdBy: "user",
2009+
creationSource: "server",
2010+
commandId: CommandId.make("command:queued-unpicked-follow-up:server"),
2011+
threadId,
2012+
messageId: MessageId.make("message:queued-unpicked-follow-up:server"),
2013+
text: "Server-created answer",
2014+
attachments: [],
2015+
dispatchMode: { type: "queue_after_active" },
2016+
});
2017+
const withServerMessage = yield* orchestrator.getThreadProjection(threadId);
2018+
assert.equal(
2019+
withServerMessage.runs.find(
2020+
(run) => run.userMessageId === "message:queued-unpicked-follow-up:server",
2021+
)?.modelSelection.instanceId,
2022+
CODEX_MODEL_SELECTION.instanceId,
2023+
);
2024+
yield* orchestrator.dispatch({
2025+
type: "thread.stop",
2026+
commandId: CommandId.make("command:queued-unpicked-follow-up:stop"),
2027+
threadId,
2028+
});
2029+
yield* worker.drain();
2030+
yield* orchestrator.dispatch({
2031+
type: "message.dispatch",
2032+
createdBy: "user",
2033+
creationSource: "mobile",
2034+
commandId: CommandId.make("command:queued-unpicked-follow-up:continue"),
2035+
threadId,
2036+
messageId: MessageId.make("message:queued-unpicked-follow-up:continue"),
2037+
text: "Continue without a pick",
2038+
attachments: [],
2039+
dispatchMode: { type: "start_immediately" },
2040+
});
2041+
const continued = yield* orchestrator.getThreadProjection(threadId);
2042+
const continuation = continued.runs.find(
2043+
(run) => run.userMessageId === "message:queued-unpicked-follow-up:continue",
2044+
);
2045+
assert.equal(continuation?.modelSelection.instanceId, CODEX_MODEL_SELECTION.instanceId);
2046+
}).pipe(
2047+
Effect.provide(
2048+
ProviderReplayHarness.layerWithRegistry(
2049+
{
2050+
name: "queued-unpicked-follow-up",
2051+
runtimePolicyOverride: {
2052+
cwd,
2053+
approvalPolicy: "never",
2054+
sandboxPolicy: {
2055+
type: "readOnly",
2056+
access: { type: "fullAccess" },
2057+
networkAccess: false,
2058+
},
2059+
},
2060+
},
2061+
layerRegistry,
2062+
),
2063+
),
2064+
);
2065+
}),
2066+
),
2067+
);
2068+
18872069
it.live("hands completed Grok steering context to earlier queued Codex and later Claude", () =>
18882070
Effect.scoped(
18892071
Effect.gen(function* () {

0 commit comments

Comments
 (0)