Skip to content

Commit 402d062

Browse files
shivamhwpclaude
andauthored
fix(composer): persist model picks before the next message (#31739)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent cd87cce commit 402d062

19 files changed

Lines changed: 382 additions & 40 deletions

‎apps/mobile/src/App.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
import { RootStack, rootPathnameFromState, workspaceLocationFromState } from "./Stack";
5050
import { appAtomRegistry } from "./state/atom-registry";
5151
import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
52-
import { environmentThreadShells } from "./state/threads";
52+
import { environmentThreadShells, threadEnvironment } from "./state/threads";
5353
import { environmentShell } from "./state/shell";
5454
import { OverlayPortalHost } from "./components/ui/OverlayPortal";
5555
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
@@ -187,6 +187,7 @@ const appServiceDisposers = [
187187
threadShellAtom: environmentThreadShells.threadShellAtom,
188188
threadRefsAtom: environmentThreadShells.threadRefsAtom,
189189
shellStateAtom: environmentShell.stateAtom,
190+
pendingThreadIdsAtom: threadEnvironment.pendingThreadIdsAtom,
190191
}),
191192
installAppearanceRuntime(),
192193
installSubscriptionUsageRefresh(),

‎apps/mobile/src/features/threads/ThreadSettingsSheet.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import {
8888
} from "../../state/use-composer-drafts";
8989
import { useQuietThreadStructure } from "../../state/use-thread-detail";
9090
import { activeComposerDraftKey, useQueuedRunEdit } from "../../state/queued-run-edit";
91-
import { updateThreadDraftModelSelection } from "../../state/use-thread-composer-state";
91+
import { selectThreadModel } from "../../state/thread-model-selection";
9292
import { useThreadSelection } from "../../state/use-thread-selection";
9393
import { threadAllowsProviderSwitch } from "../../state/thread-provider-switching";
9494
import { environmentServerConfigsAtom, serverEnvironment } from "../../state/server";
@@ -1338,7 +1338,7 @@ export function ExistingThreadSettingsRouteScreen({
13381338
option.selection.model === modelSelection.model,
13391339
) ?? null;
13401340
const updateModelSelection = (selection: ModelSelection) =>
1341-
updateThreadDraftModelSelection(threadKey, selection);
1341+
selectThreadModel(route.params, selection);
13421342

13431343
return (
13441344
<ThreadSettingsPickerScreen
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { runAtomCommand, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2+
import {
3+
AuthOrchestrationOperateScope,
4+
type EnvironmentId,
5+
type ModelSelection,
6+
type ThreadId,
7+
} from "@t3tools/contracts";
8+
9+
import { scopedThreadKey } from "../lib/scopedEntities";
10+
import * as Dialog from "../components/ui/Dialog";
11+
import { appAtomRegistry } from "./atom-registry";
12+
import { readEnvironmentScope } from "./session";
13+
import { environmentThreadShells, threadEnvironment } from "./threads";
14+
import {
15+
releaseComposerDraftModelSelection,
16+
updateComposerDraftSettings,
17+
} from "./use-composer-drafts";
18+
19+
/** Sets an existing thread's pick on the thread, so any next turn runs on it. */
20+
export function selectThreadModel(
21+
ref: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId },
22+
value: ModelSelection,
23+
): void {
24+
const threadKey = scopedThreadKey(ref.environmentId, ref.threadId);
25+
// Its own object, so an older reply for an equal pick cannot release this one.
26+
const modelSelection = { ...value };
27+
updateComposerDraftSettings(threadKey, { modelSelection });
28+
const thread = appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
29+
// Another instance replaces the live session, which may hold an open question; that pick
30+
// still waits for a sent message.
31+
if (
32+
thread?.modelSelection.instanceId !== modelSelection.instanceId ||
33+
!readEnvironmentScope(ref.environmentId, AuthOrchestrationOperateScope)
34+
) {
35+
return;
36+
}
37+
void runAtomCommand(
38+
appAtomRegistry,
39+
threadEnvironment.setModelSelection,
40+
{ environmentId: ref.environmentId, input: { threadId: ref.threadId, modelSelection } },
41+
{ label: "change model", reportFailure: true, reportDefect: true },
42+
).then((result) => {
43+
// Released on the reply, so a later change from another client shows.
44+
if (result._tag === "Success") {
45+
releaseComposerDraftModelSelection(threadKey, modelSelection);
46+
} else {
47+
const error = squashAtomCommandFailure(result);
48+
Dialog.alert(
49+
"Could not apply model selection",
50+
`This model pick was not saved. ${error instanceof Error ? error.message : "Try again."}`,
51+
);
52+
}
53+
});
54+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ function modelReconciliationHarness(
310310
],
311311
]),
312312
});
313+
const noPendingThreadIdsAtom = Atom.make((): ReadonlySet<ThreadId> => new Set());
313314
const shells = createEnvironmentThreadShellAtoms({
314315
catalogValueAtom,
315316
snapshotAtom: () => snapshotAtom,
@@ -323,6 +324,7 @@ function modelReconciliationHarness(
323324
threadShellAtom: shells.threadShellAtom,
324325
threadRefsAtom: shells.threadRefsAtom,
325326
shellStateAtom: () => shellResultAtom,
327+
pendingThreadIdsAtom: () => noPendingThreadIdsAtom,
326328
}),
327329
);
328330
flush();

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type ProviderOptionSelection,
2020
type RuntimeMode,
2121
type ScopedThreadRef,
22+
type ThreadId,
2223
} from "@t3tools/contracts";
2324
import * as Schema from "effect/Schema";
2425
import { useEffect, useState } from "react";
@@ -1541,6 +1542,20 @@ export function updateComposerDraftSettings(
15411542
});
15421543
}
15431544

1545+
/** Lets the thread's model show again once the server accepted this exact pick. */
1546+
export function releaseComposerDraftModelSelection(
1547+
draftKey: string,
1548+
modelSelection: ModelSelection,
1549+
): void {
1550+
updateComposerDrafts((current) => {
1551+
const existing = current[draftKey];
1552+
// A newer pick, even of the same model, is still waiting for its own reply.
1553+
if (existing?.modelSelection !== modelSelection) return current;
1554+
const { modelSelection: _modelSelection, ...draft } = existing;
1555+
return withComposerDraft(current, draftKey, draft);
1556+
});
1557+
}
1558+
15441559
export function setFreshSessionPending(draftKey: string, pending: boolean): void {
15451560
if ((getComposerDraftSnapshot(draftKey).freshContext === true) === pending) return;
15461561
updateComposerDraftSettings(draftKey, { freshContext: pending ? true : undefined });
@@ -1557,6 +1572,7 @@ export function subscribeComposerDraftModelReconciliation(input: {
15571572
readonly shellStateAtom: (
15581573
environmentId: EnvironmentId,
15591574
) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentShellState, unknown>>;
1575+
readonly pendingThreadIdsAtom: (environmentId: EnvironmentId) => Atom.Atom<ReadonlySet<ThreadId>>;
15601576
}): () => void {
15611577
const draftsAtom = Atom.make(appAtomRegistry.get(composerDraftsAtom));
15621578
const unsubscribeDrafts = appAtomRegistry.subscribe(composerDraftsAtom, (drafts) =>
@@ -1576,6 +1592,8 @@ export function subscribeComposerDraftModelReconciliation(input: {
15761592
const thread = get(input.threadShellAtom(ref));
15771593
if (!thread || !draftModelPickMatchesThread(draft.modelSelection, thread.modelSelection))
15781594
continue;
1595+
// The pick can match an old model while an earlier pick in flight still replaces it.
1596+
if (get(input.pendingThreadIdsAtom(ref.environmentId)).has(ref.threadId)) continue;
15791597
// Status and selection must come from the same emission; derived atoms can update apart.
15801598
const shell = AsyncResult.getOrElse(get(input.shellStateAtom(ref.environmentId)), () => null);
15811599
if (shell?.status !== "live") continue;

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import {
7979
scheduleUnusedComposerAttachmentCleanup,
8080
setComposerDraftText,
8181
setFreshSessionPending,
82-
updateComposerDraftSettings,
8382
useComposerDraft,
8483
useComposerDraftSettings,
8584
} from "./use-composer-drafts";
@@ -178,10 +177,6 @@ export function useThreadDraftForThread(input: {
178177
};
179178
}
180179

181-
export function updateThreadDraftModelSelection(threadKey: string, value: ModelSelection): void {
182-
updateComposerDraftSettings(threadKey, { modelSelection: value });
183-
}
184-
185180
export function useThreadComposerState(input: {
186181
/** Forks the thread into a side chat that gets `prompt`; resolves false when it did not start. */
187182
readonly startSideChat: (

‎apps/web/src/appInit.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { installChatRouteCommands } from "./shortcuts/chatRouteCommands";
5454
import { startWorkspaceFocus } from "./lib/workspaceFocus";
5555
import { installCommandPaletteCommands } from "./shortcuts/commandPaletteCommands";
5656
import { registerOpenFavoriteEditorCommand } from "./components/chat/OpenInPickerShortcut";
57-
import { environmentThreadShells } from "./state/threads";
57+
import { environmentThreadShells, threadEnvironment } from "./state/threads";
5858
import { resolveThreadRouteTarget } from "./threadRoutes";
5959

6060
/** Writes appearance settings onto the document, touching only the groups that changed. */
@@ -205,6 +205,7 @@ export function startAppServices(): AppRouter {
205205
threadShellAtom: environmentThreadShells.threadShellAtom,
206206
threadRefsAtom: environmentThreadShells.threadRefsAtom,
207207
shellStateAtom: environmentShell.stateAtom,
208+
pendingThreadIdsAtom: threadEnvironment.pendingThreadIdsAtom,
208209
});
209210
startMainAppLocationTracking(router);
210211
startWorkspaceFocus(router);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createMemoryHistory } from "@tanstack/react-router";
2+
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
3+
import type * as Context from "effect/Context";
4+
import { expect, it, vi } from "vite-plus/test";
5+
import { page } from "vite-plus/test/browser";
6+
7+
import { AppRoot } from "~/AppRoot";
8+
// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
9+
// takes 6-11s; importing it here keeps that out of the test's timeout.
10+
import "~/components/ChatView";
11+
import { __setClientSettingsForTests } from "~/hooks/useSettings";
12+
import { getRouter } from "~/router";
13+
import {
14+
ENVIRONMENT_ID,
15+
dispatchedCommands,
16+
installFakeEnvironment,
17+
threadWithHistory,
18+
type FakeDriver,
19+
} from "../../test/fakeEnvironment";
20+
import { render } from "../../test/render";
21+
22+
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
23+
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
24+
const [actual, { fakeDriver }, Layer] = await Promise.all([
25+
importOriginal<{ readonly ConnectionDriver: Context.Key<unknown, FakeDriver> }>(),
26+
import("../../test/fakeEnvironment"),
27+
import("effect/Layer"),
28+
]);
29+
return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
30+
});
31+
32+
vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
33+
...(await original()),
34+
trackRpcRequestSent: () => {},
35+
}));
36+
37+
// Its own file: the app's shell subscription outlives a test, and it must list this thread.
38+
it("a model picked for an existing thread reaches the server before any message is sent", async () => {
39+
const thread = threadWithHistory("thread-model-pick", 1);
40+
using _environment = installFakeEnvironment([thread], { acceptCommands: true });
41+
__setClientSettingsForTests({
42+
...DEFAULT_CLIENT_SETTINGS,
43+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
44+
});
45+
const router = getRouter(
46+
createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
47+
);
48+
await router.load();
49+
await using _app = await render(<AppRoot router={router} />, {
50+
// AppRoot brings its own router.
51+
router: false,
52+
});
53+
await expect.element(page.getByText("Answer 0 for thread-model-pick.")).toBeVisible();
54+
55+
await page.getByRole("button", { name: "GPT-5.4", exact: true }).click();
56+
await page.getByRole("option", { name: /GPT-5\.5/ }).click();
57+
await vi.waitFor(() =>
58+
expect(
59+
dispatchedCommands().filter((command) => command.type === "thread.model-selection.set"),
60+
).toEqual([
61+
expect.objectContaining({
62+
threadId: thread.id,
63+
modelSelection: expect.objectContaining({ instanceId: "codex", model: "gpt-5.5" }),
64+
}),
65+
]),
66+
);
67+
});

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ import { resolveComposerMenuActiveItemId } from "./composerMenuHighlight";
209209
import { buildPullRequestReferenceContext } from "../pullRequest/pullRequestDetail.logic";
210210
import { getComposerPromptInjectionState, getComposerReasoningStep } from "./composerProviderState";
211211
import { readPromptUltrathink } from "./TraitsPicker";
212+
import { useApplyThreadModelPick } from "./useApplyThreadModelPick";
212213
import {
213214
buildAttachmentVideoPreview,
214215
buildExpandedImagePreview,
@@ -598,6 +599,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
598599
const activePendingIsResponding = !holdingUserInput && incomingPendingIsResponding;
599600
const isLiteralPendingAnswer = activePendingProgress?.activeQuestion?.initialAnswer !== undefined;
600601
const isEditingQueuedMessage = editingQueuedAttachments !== null;
602+
const applyThreadModelPick = useApplyThreadModelPick();
601603
const composerDraft = useComposerThreadDraft(composerDraftTarget);
602604
const composerDraftTargetKeyRef = useRef("");
603605
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes the committed draft target key to a ref read by async handlers
@@ -2947,6 +2949,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
29472949
model: selectedModel,
29482950
persistSticky: true,
29492951
});
2952+
applyThreadModelPick(composerDraftTarget);
29502953
if (change.prompt !== promptRef.current) {
29512954
applyPromptReplacementStable(0, promptRef.current.length, change.prompt, {
29522955
expandedCursorAfterReplace: change.cursor,

‎apps/web/src/components/chat/ComposerControlsBar.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
import type { EffortDisplay } from "@t3tools/contracts/settings";
2222
import { cn } from "~/lib/utils";
2323
import { resolveComposerTraitsInput } from "./composerProviderState";
24+
import { useApplyThreadModelPick } from "./useApplyThreadModelPick";
25+
import { useStableCallback } from "../../hooks/useStableCallback";
2426
import {
2527
ComposerEffortColumn,
2628
ComposerPickerFooter,
@@ -142,10 +144,14 @@ export function ComposerControlsBar(props: {
142144
measuredHiddenBlockCount,
143145
});
144146
const controlSize = composerControlsCollapsed ? "xs" : "sm";
147+
const applyThreadModelPick = useApplyThreadModelPick();
148+
const applyRouteThreadModelPick = useStableCallback(() => applyThreadModelPick(routeThreadRef));
145149
const traitsInput = resolveComposerTraitsInput({
146150
provider: selectedProvider,
147151
instanceId: selectedInstanceId,
148-
...(routeKind === "server" ? { threadRef: routeThreadRef } : {}),
152+
...(routeKind === "server"
153+
? { threadRef: routeThreadRef, onDraftModelOptionsChange: applyRouteThreadModelPick }
154+
: {}),
149155
...(routeKind === "draft" && draftId ? { draftId } : {}),
150156
model: selectedModel,
151157
models: selectedProviderModels,

0 commit comments

Comments
 (0)