Skip to content

Commit a98bc69

Browse files
fix(web): Cursor threads keep a stored model the catalog doesn't list (pingdotgg#24872)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 6641231 commit a98bc69

7 files changed

Lines changed: 239 additions & 7 deletions

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ function shouldIncludeModelPickerOption(input: {
113113
if (isProviderInstancePickerReady(input.entry)) return true;
114114
return (
115115
input.entry.enabled &&
116-
(input.entry.driverKind === "opencode" || input.entry.driverKind === "antigravity") &&
116+
(input.entry.driverKind === "opencode" ||
117+
input.entry.driverKind === "antigravity" ||
118+
input.entry.driverKind === "cursor") &&
117119
input.entry.instanceId === input.activeInstanceId &&
118120
input.option.slug === input.activeModel &&
119121
input.option.isUnavailable === true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function getSelectedTraits(
122122
) {
123123
const caps = getProviderModelCapabilities(models, model, provider, includePlanAgent);
124124
const modelIsUnavailable =
125-
provider === "opencode" &&
125+
(provider === "opencode" || provider === "cursor") &&
126126
!models.some((candidate) => candidate.slug === normalizeModelSlug(model, provider));
127127
const descriptors = modelIsUnavailable
128128
? buildUnavailableModelOptionDescriptors(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export function getComposerProviderState(input: ComposerProviderStateInput): Com
8686
promptInjectionState = "none",
8787
includePlanAgent,
8888
} = input;
89-
if (provider === "opencode") {
89+
if (provider === "opencode" || provider === "cursor") {
9090
const normalizedModel = normalizeModelSlug(model, provider);
9191
const modelIsInCatalog = models.some((candidate) => candidate.slug === normalizedModel);
9292
if (!modelIsInCatalog) {
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
3+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4+
import {
5+
EnvironmentId,
6+
ProviderDriverKind,
7+
ProviderInstanceId,
8+
ThreadId,
9+
type ServerProvider,
10+
} from "@t3tools/contracts";
11+
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
12+
import { createModelSelection } from "@t3tools/shared/model";
13+
import * as Cause from "effect/Cause";
14+
import { AsyncResult, Atom } from "effect/reactivity";
15+
import { expect, it, vi } from "vite-plus/test";
16+
import { page } from "vite-plus/test/browser";
17+
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+
38+
import { render } from "../../../test/render";
39+
import { deriveEffectiveComposerModelState, useComposerDraftStore } from "../../composerDraftStore";
40+
import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry";
41+
import { useComposerSelection } from "./useComposerSelection";
42+
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+
52+
const environmentId = EnvironmentId.make("environment-local");
53+
const cursor = ProviderInstanceId.make("cursor");
54+
const stored = createModelSelection(cursor, "claude-opus-5-5", [{ id: "effort", value: "medium" }]);
55+
56+
const providers: ReadonlyArray<ServerProvider> = [
57+
{
58+
instanceId: cursor,
59+
driver: ProviderDriverKind.make("cursor"),
60+
enabled: true,
61+
installed: true,
62+
version: null,
63+
status: "ready",
64+
auth: { status: "authenticated" },
65+
checkedAt: "2026-01-01T00:00:00.000Z",
66+
models: [{ slug: "auto-smart", name: "Auto", isCustom: false, capabilities: {} }],
67+
slashCommands: [],
68+
skills: [],
69+
},
70+
];
71+
72+
// The hook reads only the thread's identity and stored selection.
73+
function cursorThread(id: string) {
74+
return { environmentId, id: ThreadId.make(id), modelSelection: stored } as EnvironmentThreadShell;
75+
}
76+
77+
function Picker(props: {
78+
thread: EnvironmentThreadShell;
79+
runtime: Parameters<typeof useComposerSelection>[0]["activeRuntime"];
80+
model?: string;
81+
}) {
82+
const { onProviderModelSelectStable } = useComposerSelection({
83+
environmentId,
84+
composerDraftTarget: scopeThreadRef(environmentId, props.thread.id),
85+
draftThread: null,
86+
activeThread: props.thread,
87+
activeRuntime: props.runtime,
88+
activeProjectSettings: { settings: { newWorktreesStartFromOrigin: false } },
89+
settings: DEFAULT_UNIFIED_SETTINGS,
90+
providerStatuses: providers,
91+
lockedProvider: null,
92+
supportsProviderSwitchingViaHandoff: true,
93+
multipleModelSelections: null,
94+
runtimeMode: "full-access",
95+
isLocalDraftThread: false,
96+
canOverrideServerThreadEnvMode: false,
97+
setPendingServerThreadEnvModeEntry: () => {},
98+
setPendingServerThreadStartFromOriginByThreadId: () => {},
99+
scheduleComposerFocus: () => {},
100+
});
101+
return (
102+
<button
103+
type="button"
104+
onClick={() => onProviderModelSelectStable(cursor, props.model ?? stored.model)}
105+
>
106+
Pick the model
107+
</button>
108+
);
109+
}
110+
111+
it("keeps a Cursor thread's stored model and options when the user re-picks its unavailable row", async () => {
112+
setup();
113+
const thread = cursorThread("thread-cursor");
114+
await using _screen = await render(<Picker thread={thread} runtime={null} />);
115+
await page.getByRole("button", { name: "Pick the model" }).click();
116+
117+
await vi.waitFor(() => {
118+
const draft = useComposerDraftStore
119+
.getState()
120+
.getComposerDraft(scopeThreadRef(environmentId, thread.id));
121+
expect(draft?.modelSelectionByProvider[cursor] ?? thread.modelSelection).toEqual(stored);
122+
});
123+
});
124+
125+
it("switches back to the thread's stored model while the session runs on another instance", async () => {
126+
setup();
127+
const thread = cursorThread("thread-cursor-handoff");
128+
const runtime = { providerInstanceId: ProviderInstanceId.make("codex") } as Parameters<
129+
typeof useComposerSelection
130+
>[0]["activeRuntime"];
131+
await using _screen = await render(<Picker thread={thread} runtime={runtime} />);
132+
await page.getByRole("button", { name: "Pick the model" }).click();
133+
134+
await vi.waitFor(() => {
135+
const draft = useComposerDraftStore
136+
.getState()
137+
.getComposerDraft(scopeThreadRef(environmentId, thread.id));
138+
expect(draft?.activeProvider).toBe(cursor);
139+
expect(
140+
deriveEffectiveComposerModelState({
141+
draft,
142+
providers,
143+
selectedProvider: ProviderDriverKind.make("cursor"),
144+
selectedInstanceId: cursor,
145+
threadModelSelection: thread.modelSelection,
146+
projectModelSelection: null,
147+
settings: DEFAULT_UNIFIED_SETTINGS,
148+
}),
149+
).toEqual({ selectedModel: stored.model, modelOptions: { [cursor]: stored.options } });
150+
});
151+
});
152+
153+
it("saves a re-picked model again when its first save failed", async () => {
154+
const { setModelSelection } = setup();
155+
setModelSelection
156+
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("offline"))))
157+
.mockResolvedValue(AsyncResult.success(undefined));
158+
const thread = cursorThread("thread-cursor-retry");
159+
appAtomRegistry.set(shellAtom(thread.id), thread);
160+
await using _screen = await render(
161+
<AppAtomRegistryProvider>
162+
<Picker thread={thread} runtime={null} model="auto-smart" />
163+
</AppAtomRegistryProvider>,
164+
);
165+
const pick = page.getByRole("button", { name: "Pick the model" });
166+
await pick.click();
167+
await vi.waitFor(() => expect(setModelSelection).toHaveBeenCalledTimes(1));
168+
await pick.click();
169+
170+
const autoSave = expect.objectContaining({
171+
input: {
172+
threadId: thread.id,
173+
modelSelection: expect.objectContaining({ instanceId: cursor, model: "auto-smart" }),
174+
},
175+
});
176+
await vi.waitFor(() => expect(setModelSelection.mock.calls).toEqual([[autoSave], [autoSave]]));
177+
});

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,30 @@ export function useComposerSelection(input: ComposerSelectionInput) {
152152
return;
153153
}
154154
}
155+
// The picker lists an unavailable model only as the active selection, so re-picking keeps it.
155156
const resolvedModel = resolveAppModelSelectionForInstance(
156157
instanceId,
157158
settings,
158159
providerStatuses,
159160
model,
161+
{ preserveUnavailableSelection: true },
160162
);
161-
if (!resolvedModel) {
163+
const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id);
164+
const draft = useComposerDraftStore.getState().getComposerDraft(threadRef);
165+
const shownInstanceId =
166+
draft?.activeProvider ??
167+
activeRuntime?.providerInstanceId ??
168+
activeThread.modelSelection.instanceId;
169+
const shownSelection =
170+
draft?.modelSelectionByProvider[shownInstanceId] ?? activeThread.modelSelection;
171+
if (
172+
!resolvedModel ||
173+
(instanceId === shownInstanceId &&
174+
shownSelection.instanceId === instanceId &&
175+
shownSelection.model === resolvedModel)
176+
) {
177+
// Re-picking the shown model keeps its options and saves it again if that save failed.
178+
if (resolvedModel && !isLocalDraftThread) applyThreadModelPick(threadRef);
162179
if (options?.focusComposer !== false) scheduleComposerFocus();
163180
return;
164181
}
@@ -184,7 +201,6 @@ export function useComposerSelection(input: ComposerSelectionInput) {
184201
if (options?.focusComposer !== false) scheduleComposerFocus();
185202
return;
186203
}
187-
const threadRef = scopeThreadRef(activeThread.environmentId, activeThread.id);
188204
setComposerDraftModelSelection(threadRef, nextModelSelection, {
189205
explicit: true,
190206
replaceOptions: true,

‎apps/web/src/modelSelection.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ describe("instance-scoped model selection", () => {
404404
availableModel: "gemini-3.1-pro",
405405
missingModel: "gemini-3.1-pro-high",
406406
},
407+
{
408+
driverName: "cursor",
409+
availableModel: "auto-smart",
410+
missingModel: "claude-opus-5-5",
411+
},
407412
])("$driverName catalog gaps", ({ driverName, availableModel, missingModel }) => {
408413
it("preserves a selected model when a catalog refresh no longer contains it", () => {
409414
const providers = [
@@ -510,6 +515,34 @@ describe("instance-scoped model selection", () => {
510515
expect(dispatch.modelOptionsForDispatch).toBeUndefined();
511516
});
512517

518+
it("sends a Cursor thread's stored model and options when the catalog lacks them", () => {
519+
const instanceId = ProviderInstanceId.make("cursor");
520+
const driver = ProviderDriverKind.make("cursor");
521+
const stored = createModelSelection(instanceId, "claude-opus-5-5", [
522+
{ id: "effort", value: "medium" },
523+
]);
524+
const providers = [provider({ provider: driver, instanceId, models: ["auto-smart"] })];
525+
const state = deriveEffectiveComposerModelState({
526+
draft: null,
527+
providers,
528+
selectedProvider: driver,
529+
selectedInstanceId: instanceId,
530+
threadModelSelection: stored,
531+
projectModelSelection: null,
532+
settings: settingsWithProviderInstances(),
533+
});
534+
const dispatch = getComposerProviderState({
535+
provider: driver,
536+
model: state.selectedModel,
537+
models: providers[0]!.models,
538+
modelOptions: state.modelOptions?.[instanceId],
539+
});
540+
541+
expect(
542+
createModelSelection(instanceId, state.selectedModel, dispatch.modelOptionsForDispatch),
543+
).toEqual(stored);
544+
});
545+
513546
it("preserves an explicit draft OpenCode selection while the catalog is empty", () => {
514547
const instanceId = ProviderInstanceId.make("opencode_work");
515548
const driver = ProviderDriverKind.make("opencode");

‎apps/web/src/modelSelection.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ function appendUnavailableDynamicModelSelection(
8080
selectedModel: string | null | undefined,
8181
hiddenModels: ReadonlyArray<string>,
8282
): AppModelOption[] {
83-
if (provider !== "opencode" && provider !== "antigravity") return options;
83+
if (provider !== "opencode" && provider !== "antigravity" && provider !== "cursor") {
84+
return options;
85+
}
8486
const slug = normalizeCustomModelSlug(selectedModel);
8587
if (!slug) return options;
8688
if (provider === "antigravity" && slug === ANTIGRAVITY_DEFAULT_MODEL) return options;
@@ -282,7 +284,9 @@ export function resolveAppModelSelectionForInstance(
282284
}
283285
if (
284286
resolutionOptions?.preserveUnavailableSelection &&
285-
(entry.driverKind === "opencode" || entry.driverKind === "antigravity")
287+
(entry.driverKind === "opencode" ||
288+
entry.driverKind === "antigravity" ||
289+
entry.driverKind === "cursor")
286290
) {
287291
const unavailableSelection = normalizeCustomModelSlug(selectedModel);
288292
const hiddenModels = readInstanceModelPreferences(settings, entry.instanceId).hiddenModels;

0 commit comments

Comments
 (0)