Skip to content

Commit cb282df

Browse files
fix(provider-cursor): fall back to the catalog's default context window
A Cursor selection without a contextWindow option runs on the model's default from Cursor's catalog, but the runtime reported no window for it, so handoff budgets still assumed 128k tokens. The driver now hands its latest catalog to the adapter, which resolves the option with the shared descriptor helpers, as the Claude catalog does, and reports no window until the catalog has loaded. Non-positive values such as "0k" count as unknown, and selections for another instance are ignored. Co-authored-by: Cursor <[email protected]>
1 parent 3cd4ace commit cb282df

5 files changed

Lines changed: 166 additions & 43 deletions

File tree

‎packages/provider-cursor/src/server/adapter.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,13 @@ describe("CursorAdapterV2", () => {
827827
assert.equal(runtime.getModelContextWindow?.(selection("300K")), 300_000);
828828
assert.isUndefined(runtime.getModelContextWindow?.(selection()));
829829
assert.isUndefined(runtime.getModelContextWindow?.(selection("max")));
830+
assert.isUndefined(runtime.getModelContextWindow?.(selection("0k")));
831+
assert.isUndefined(
832+
runtime.getModelContextWindow?.({
833+
...selection("1m"),
834+
instanceId: ProviderInstanceId.make("other-cursor"),
835+
}),
836+
);
830837

831838
// Cursor reports no context usage, so the budget falls back to a byte
832839
// estimate of the native transcript, here a thread with long shell output.

‎packages/provider-cursor/src/server/adapter.ts‎

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type OrchestrationV2Subagent,
2828
type OrchestrationV2TurnItem,
2929
type ProviderInstanceId,
30+
type ServerProviderModel,
3031
type ThreadId,
3132
} from "@t3tools/contracts";
3233
import { CursorSettings } from "../settings.ts";
@@ -844,6 +845,8 @@ export interface CursorAdapterV2Options {
844845
readonly instanceId: ProviderInstanceId;
845846
readonly settings: CursorSettings;
846847
readonly environment: NodeJS.ProcessEnv;
848+
/** The driver's latest Cursor catalog, which names each model's default context window. */
849+
readonly getModels?: () => ReadonlyArray<ServerProviderModel>;
847850
}
848851

849852
export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
@@ -2369,7 +2372,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
23692372
driver: CursorAgentSdk.CURSOR_PROVIDER,
23702373
providerSessionId: input.providerSessionId,
23712374
providerSession: session,
2372-
getModelContextWindow: cursorContextWindowTokens,
2375+
getModelContextWindow: (selection) =>
2376+
selection.instanceId === adapterOptions.instanceId
2377+
? cursorContextWindowTokens(selection, adapterOptions.getModels?.() ?? [])
2378+
: undefined,
23732379
events: Stream.fromEffectRepeat(Queue.take(events)),
23742380
ensureThread: Effect.fn("CursorAdapterV2.ensureThread")(
23752381
function* (threadInput: ProviderAdapter.ProviderAdapterV2EnsureThreadInput) {
@@ -2606,38 +2612,44 @@ export type CursorAdapterV2DriverEnv =
26062612
| IdAllocator.IdAllocatorV2
26072613
| ProviderHost.ProviderHost;
26082614

2615+
export const createCursorAdapterV2 = Effect.fn("CursorAdapterV2Driver.create")(
2616+
function* (
2617+
input: ProviderAdapterDriverCreateInput<CursorSettings>,
2618+
hooks: Pick<CursorAdapterV2Options, "getModels"> = {},
2619+
) {
2620+
const hostEnvironment = yield* HostProcessEnvironment;
2621+
return yield* makeCursorAdapterV2({
2622+
instanceId: input.instanceId,
2623+
settings: {
2624+
...input.config,
2625+
enabled: input.enabled,
2626+
},
2627+
environment: mergeProviderInstanceEnvironment(input.environment, hostEnvironment),
2628+
...hooks,
2629+
});
2630+
},
2631+
(effect, input, _hooks) =>
2632+
effect.pipe(
2633+
Effect.mapError(
2634+
(cause) =>
2635+
new ProviderAdapterDriverCreateError({
2636+
driver: CURSOR_DRIVER_KIND,
2637+
instanceId: input.instanceId,
2638+
detail: "Failed to create Cursor Agent SDK adapter.",
2639+
cause,
2640+
}),
2641+
),
2642+
),
2643+
);
2644+
26092645
export const CursorAdapterV2Driver: ProviderAdapterDriver<
26102646
CursorSettings,
26112647
CursorAdapterV2DriverEnv
26122648
> = {
26132649
driverKind: CURSOR_DRIVER_KIND,
26142650
configSchema: CursorSettings,
26152651
defaultConfig: (): CursorSettings => DEFAULT_CURSOR_SETTINGS,
2616-
create: Effect.fn("CursorAdapterV2Driver.create")(
2617-
function* (input: ProviderAdapterDriverCreateInput<CursorSettings>) {
2618-
const hostEnvironment = yield* HostProcessEnvironment;
2619-
return yield* makeCursorAdapterV2({
2620-
instanceId: input.instanceId,
2621-
settings: {
2622-
...input.config,
2623-
enabled: input.enabled,
2624-
},
2625-
environment: mergeProviderInstanceEnvironment(input.environment, hostEnvironment),
2626-
});
2627-
},
2628-
(effect, input) =>
2629-
effect.pipe(
2630-
Effect.mapError(
2631-
(cause) =>
2632-
new ProviderAdapterDriverCreateError({
2633-
driver: CURSOR_DRIVER_KIND,
2634-
instanceId: input.instanceId,
2635-
detail: "Failed to create Cursor Agent SDK adapter.",
2636-
cause,
2637-
}),
2638-
),
2639-
),
2640-
),
2652+
create: (input) => createCursorAdapterV2(input, {}),
26412653
};
26422654

26432655
const layer: Layer.Layer<

‎packages/provider-cursor/src/server/driver.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2+
import type { SDKModel } from "@cursor/sdk";
23
import * as NodeServices from "@effect/platform-node/NodeServices";
34
import { expect, it } from "@effect/vitest";
45
import { ProviderInstanceId, ProviderSessionId, ThreadId } from "@t3tools/contracts";
@@ -147,6 +148,79 @@ it.layer(layerTest)("CursorDriver", (it) => {
147148
}).pipe(Effect.scoped),
148149
);
149150

151+
it.effect("reports the catalog's default context window once the Cursor catalog loads", () =>
152+
Effect.gen(function* () {
153+
const catalog = Promise.withResolvers<Array<SDKModel>>();
154+
const me = vi.spyOn(Cursor, "me").mockResolvedValue({
155+
apiKeyName: "T3 Code",
156+
createdAt: "2026-01-01T00:00:00.000Z",
157+
});
158+
const models = vi.spyOn(Cursor.models, "list").mockReturnValue(catalog.promise);
159+
yield* Effect.addFinalizer(() =>
160+
Effect.sync(() => {
161+
me.mockRestore();
162+
models.mockRestore();
163+
}),
164+
);
165+
const instanceId = ProviderInstanceId.make("cursor-catalog-window");
166+
const instance = yield* CursorDriver.create({
167+
instanceId,
168+
displayName: "Cursor",
169+
enabled: true,
170+
environment: [{ name: "CURSOR_API_KEY", value: "catalog-window-key", sensitive: true }],
171+
config: CursorDriver.defaultConfig(),
172+
}).pipe(
173+
Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
174+
assertComplete: Effect.void,
175+
open: () => Effect.die("This test opens no Cursor agent"),
176+
}),
177+
);
178+
const selection = (contextWindow?: string) => ({
179+
instanceId,
180+
model: "claude-opus-5-5",
181+
...(contextWindow === undefined
182+
? {}
183+
: { options: [{ id: "contextWindow", value: contextWindow }] }),
184+
});
185+
const runtime = yield* instance.orchestrationAdapter.openSession({
186+
threadId: ThreadId.make("cursor-catalog-window-thread"),
187+
providerSessionId: ProviderSessionId.make("cursor-catalog-window-session"),
188+
modelSelection: selection(),
189+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
190+
runtimeMode: "full-access",
191+
interactionMode: "default",
192+
cwd: process.cwd(),
193+
}),
194+
});
195+
196+
// The first catalog read waits on `catalog`, so no default is known yet.
197+
expect(runtime.getModelContextWindow?.(selection())).toBeUndefined();
198+
catalog.resolve([
199+
{
200+
id: "claude-opus-5-5",
201+
displayName: "Claude Opus 5.5",
202+
parameters: [
203+
{
204+
id: "context",
205+
displayName: "Context",
206+
values: [{ value: "300k" }, { value: "1m" }],
207+
},
208+
],
209+
variants: [
210+
{
211+
displayName: "Claude Opus 5.5",
212+
isDefault: true,
213+
params: [{ id: "context", value: "1m" }],
214+
},
215+
],
216+
},
217+
]);
218+
yield* instance.snapshot.refresh;
219+
expect(runtime.getModelContextWindow?.(selection())).toBe(1_000_000);
220+
expect(runtime.getModelContextWindow?.(selection("300k"))).toBe(300_000);
221+
}).pipe(Effect.scoped),
222+
);
223+
150224
it.effect("keeps the bundled SDK manual-only without probing or updating cursor-agent", () =>
151225
Effect.gen(function* () {
152226
const instance = yield* CursorDriver.create({

‎packages/provider-cursor/src/server/driver.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
*
77
* @module provider/Drivers/CursorDriver
88
*/
9-
import { ProviderDriverKind, ProviderSetupError } from "@t3tools/contracts";
9+
import {
10+
ProviderDriverKind,
11+
ProviderSetupError,
12+
type ServerProviderModel,
13+
} from "@t3tools/contracts";
1014
import { CursorSettings } from "../settings.ts";
1115
import * as Effect from "effect/Effect";
1216
import * as Crypto from "effect/Crypto";
@@ -18,7 +22,7 @@ import { readCursorUsageLimits } from "./usageLimits.ts";
1822

1923
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
2024
import { makeCursorTextGeneration } from "./textGeneration.ts";
21-
import { CursorAdapterV2Driver, type CursorAdapterV2DriverEnv } from "./adapter.ts";
25+
import { createCursorAdapterV2, type CursorAdapterV2DriverEnv } from "./adapter.ts";
2226
import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
2327
import { buildInitialCursorProviderSnapshot, checkCursorProviderStatus } from "./status.ts";
2428
import * as CursorSdkCatalog from "./CursorSdkCatalog.ts";
@@ -136,14 +140,18 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
136140
},
137141
});
138142

139-
const orchestrationAdapter = yield* CursorAdapterV2Driver.create({
140-
instanceId,
141-
displayName,
142-
accentColor,
143-
environment,
144-
enabled,
145-
config,
146-
}).pipe(
143+
let catalogModels: ReadonlyArray<ServerProviderModel> = [];
144+
const orchestrationAdapter = yield* createCursorAdapterV2(
145+
{
146+
instanceId,
147+
displayName,
148+
accentColor,
149+
environment,
150+
enabled,
151+
config,
152+
},
153+
{ getModels: () => catalogModels },
154+
).pipe(
147155
Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
148156
...sdkRunner,
149157
open: (input) =>
@@ -220,6 +228,11 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
220228
Effect.provideService(FileSystem.FileSystem, fileSystem),
221229
Effect.provideService(Path.Path, path),
222230
Effect.map(stampSnapshot),
231+
Effect.tap((provider) =>
232+
Effect.sync(() => {
233+
catalogModels = provider.models;
234+
}),
235+
),
223236
Effect.provide(CursorSdkCatalog.layer),
224237
);
225238

‎packages/provider-cursor/src/server/sdkModel.ts‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { ModelSelection as CursorSdkModelSelection, ModelParameterValue } from "@cursor/sdk";
2-
import type { ModelSelection } from "@t3tools/contracts";
3-
import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
2+
import type { ModelSelection, ServerProviderModel } from "@t3tools/contracts";
3+
import {
4+
getModelSelectionStringOptionValue,
5+
getProviderOptionCurrentValue,
6+
getProviderOptionDescriptors,
7+
} from "@t3tools/shared/model";
48

59
const CURSOR_SDK_PARAMETER_TO_PROVIDER_OPTION: Readonly<Record<string, string>> = {
610
context: "contextWindow",
@@ -38,13 +42,26 @@ export function cursorSdkParameterPriority(parameterId: string): number {
3842

3943
/**
4044
* Token capacity named by the selected `context` parameter, such as "272k" or "1m".
41-
* Selections without one run on Cursor's catalog default, which is unknown here.
45+
* A selection without one runs on the model's default in `models`, the Cursor catalog,
46+
* which is unknown until that catalog has loaded.
4247
*/
43-
export function cursorContextWindowTokens(modelSelection: ModelSelection): number | undefined {
44-
const value = getModelSelectionStringOptionValue(modelSelection, "contextWindow");
45-
const match = /^(\d+)([km])$/i.exec(value?.trim() ?? "");
48+
export function cursorContextWindowTokens(
49+
modelSelection: ModelSelection,
50+
models: ReadonlyArray<ServerProviderModel>,
51+
): number | undefined {
52+
const caps = models.find((model) => model.slug === modelSelection.model)?.capabilities;
53+
const descriptor = caps
54+
? getProviderOptionDescriptors({ caps, selections: modelSelection.options }).find(
55+
(candidate) => candidate.id === "contextWindow",
56+
)
57+
: undefined;
58+
const value = descriptor
59+
? getProviderOptionCurrentValue(descriptor)
60+
: getModelSelectionStringOptionValue(modelSelection, "contextWindow");
61+
const match = /^(\d+)([km])$/i.exec(typeof value === "string" ? value.trim() : "");
4662
if (match === null) return undefined;
47-
return Number(match[1]) * (match[2]?.toLowerCase() === "m" ? 1_000_000 : 1_000);
63+
const tokens = Number(match[1]) * (match[2]?.toLowerCase() === "m" ? 1_000_000 : 1_000);
64+
return tokens > 0 ? tokens : undefined;
4865
}
4966

5067
export function cursorSdkModelSelection(modelSelection: ModelSelection): CursorSdkModelSelection {

0 commit comments

Comments
 (0)