Skip to content

Commit 61fb130

Browse files
only21milclaude
andcommitted
fix(web): re-picking a model whose save failed saves it again
Main now saves a composer pick on the thread right away. Re-picking the model the composer already shows changes nothing on this branch, so a pick whose save failed stayed unsaved until the next message. That re-pick now runs the save again and still keeps the shown options. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent db1b23f commit 61fb130

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

‎apps/web/src/components/chat/useComposerSelection.browser.test.tsx‎

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
23
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
34
import {
45
EnvironmentId,
@@ -9,13 +10,45 @@ import {
910
} from "@t3tools/contracts";
1011
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
1112
import { createModelSelection } from "@t3tools/shared/model";
13+
import * as Cause from "effect/Cause";
14+
import { AsyncResult, Atom } from "effect/reactivity";
1215
import { expect, it, vi } from "vite-plus/test";
1316
import { page } from "vite-plus/test/browser";
1417

18+
const mocks = vi.hoisted(() => ({
19+
setModelSelection: null as ((value: unknown) => Promise<AtomCommandResult<void, unknown>>) | null,
20+
}));
21+
22+
vi.mock(import("../../state/use-orchestration-command"), async (importOriginal) => ({
23+
...(await importOriginal()),
24+
useOrchestrationCommand: (() => (value: unknown) => mocks.setModelSelection?.(value)) as never,
25+
}));
26+
27+
vi.mock(import("../../state/threads"), async (original) => {
28+
const actual = await original();
29+
return {
30+
...actual,
31+
environmentThreadShells: {
32+
...actual.environmentThreadShells,
33+
threadShellAtom: ((ref: { threadId: string }) => shellAtom(ref.threadId)) as never,
34+
},
35+
};
36+
});
37+
1538
import { render } from "../../../test/render";
1639
import { deriveEffectiveComposerModelState, useComposerDraftStore } from "../../composerDraftStore";
40+
import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
1741
import { useComposerSelection } from "./useComposerSelection";
1842

43+
// Kept alive so a shell set before the hook mounts is still there when it saves a pick.
44+
const shellAtom = Atom.family((_threadId: string) => Atom.make<unknown>(null).pipe(Atom.keepAlive));
45+
46+
function setup() {
47+
const setModelSelection = vi.fn<(value: unknown) => Promise<AtomCommandResult<void, unknown>>>();
48+
mocks.setModelSelection = setModelSelection;
49+
return { setModelSelection };
50+
}
51+
1952
const environmentId = EnvironmentId.make("environment-local");
2053
const cursor = ProviderInstanceId.make("cursor");
2154
const stored = createModelSelection(cursor, "claude-opus-5-5", [{ id: "effort", value: "medium" }]);
@@ -44,6 +77,7 @@ function cursorThread(id: string) {
4477
function Picker(props: {
4578
thread: EnvironmentThreadShell;
4679
runtime: Parameters<typeof useComposerSelection>[0]["activeRuntime"];
80+
model?: string;
4781
}) {
4882
const { onProviderModelSelectStable } = useComposerSelection({
4983
environmentId,
@@ -65,16 +99,19 @@ function Picker(props: {
6599
scheduleComposerFocus: () => {},
66100
});
67101
return (
68-
<button type="button" onClick={() => onProviderModelSelectStable(cursor, stored.model)}>
69-
Pick the stored model
102+
<button
103+
type="button"
104+
onClick={() => onProviderModelSelectStable(cursor, props.model ?? stored.model)}
105+
>
106+
Pick the model
70107
</button>
71108
);
72109
}
73110

74111
it("keeps a Cursor thread's stored model and options when the user re-picks its unavailable row", async () => {
75112
const thread = cursorThread("thread-cursor");
76113
await using _screen = await render(<Picker thread={thread} runtime={null} />);
77-
await page.getByRole("button", { name: "Pick the stored model" }).click();
114+
await page.getByRole("button", { name: "Pick the model" }).click();
78115

79116
await vi.waitFor(() => {
80117
const draft = useComposerDraftStore
@@ -90,7 +127,7 @@ it("switches back to the thread's stored model while the session runs on another
90127
typeof useComposerSelection
91128
>[0]["activeRuntime"];
92129
await using _screen = await render(<Picker thread={thread} runtime={runtime} />);
93-
await page.getByRole("button", { name: "Pick the stored model" }).click();
130+
await page.getByRole("button", { name: "Pick the model" }).click();
94131

95132
await vi.waitFor(() => {
96133
const draft = useComposerDraftStore
@@ -110,3 +147,29 @@ it("switches back to the thread's stored model while the session runs on another
110147
).toEqual({ selectedModel: stored.model, modelOptions: { [cursor]: stored.options } });
111148
});
112149
});
150+
151+
it("saves a re-picked model again when its first save failed", async () => {
152+
const { setModelSelection } = setup();
153+
setModelSelection
154+
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("offline"))))
155+
.mockResolvedValue(AsyncResult.success(undefined));
156+
const thread = cursorThread("thread-cursor-retry");
157+
appAtomRegistry.set(shellAtom(thread.id), thread);
158+
await using _screen = await render(
159+
<AppAtomRegistryProvider>
160+
<Picker thread={thread} runtime={null} model="auto-smart" />
161+
</AppAtomRegistryProvider>,
162+
);
163+
const pick = page.getByRole("button", { name: "Pick the model" });
164+
await pick.click();
165+
await vi.waitFor(() => expect(setModelSelection).toHaveBeenCalledTimes(1));
166+
await pick.click();
167+
168+
const autoSave = expect.objectContaining({
169+
input: {
170+
threadId: thread.id,
171+
modelSelection: expect.objectContaining({ instanceId: cursor, model: "auto-smart" }),
172+
},
173+
});
174+
await vi.waitFor(() => expect(setModelSelection.mock.calls).toEqual([[autoSave], [autoSave]]));
175+
});

‎apps/web/src/components/chat/useComposerSelection.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ export function useComposerSelection(input: ComposerSelectionInput) {
174174
shownSelection.instanceId === instanceId &&
175175
shownSelection.model === resolvedModel)
176176
) {
177+
// Re-picking the shown model keeps its options and saves it again if that save failed.
178+
if (resolvedModel && !isLocalDraftThread) applyThreadModelPick(threadRef);
177179
if (options?.focusComposer !== false) scheduleComposerFocus();
178180
return;
179181
}

0 commit comments

Comments
 (0)