Skip to content

Commit 3cd4ace

Browse files
fix(provider-cursor): use the selected context window for handoff budgets
The Cursor runtime did not implement getModelContextWindow, so provider handoff budgets assumed a 128k-token window even when the selection asked Cursor for a 1M context. Report the token count named by the selected context parameter ("272k", "1m"). Selections without one keep the unknown-window fallback. Co-authored-by: Cursor <[email protected]>
1 parent 101f8b2 commit 3cd4ace

3 files changed

Lines changed: 90 additions & 1 deletion

File tree

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import * as Schema from "effect/Schema";
2323
import * as Stream from "effect/Stream";
2424

2525
import { layerTestProviderHost } from "@t3tools/provider-testing/host";
26+
import {
27+
DEFAULT_HANDOFF_TOKEN_CAP,
28+
handoffBudget,
29+
} from "@t3tools/provider-core/server/handoffBudget";
2630
import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession";
2731
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
2832
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
@@ -771,6 +775,78 @@ describe("CursorAdapterV2", () => {
771775
);
772776
});
773777

778+
it.effect("reports the selected context window so long threads keep a handoff allowance", () =>
779+
Effect.gen(function* () {
780+
const fileSystem = yield* FileSystem.FileSystem;
781+
const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-window-" });
782+
const instanceId = ProviderInstanceId.make("cursor");
783+
const threadId = ThreadId.make("cursor-window-thread");
784+
const selection = (contextWindow?: string) => ({
785+
instanceId,
786+
model: "claude-opus-5-5",
787+
...(contextWindow === undefined
788+
? {}
789+
: { options: [{ id: "contextWindow", value: contextWindow }] }),
790+
});
791+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
792+
runtimeMode: "full-access",
793+
interactionMode: "default",
794+
cwd: workspace,
795+
});
796+
const adapter = yield* makeCursorAdapterV2({
797+
instanceId,
798+
settings: yield* decodeCursorSettings({}),
799+
environment: { HOME: workspace },
800+
}).pipe(
801+
Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
802+
assertComplete: Effect.void,
803+
open: () =>
804+
Effect.succeed({
805+
agentId: "native-cursor-window",
806+
listMessages: Effect.succeed([]),
807+
close: Effect.void,
808+
send: () => Effect.die("Cursor turns are not started in this test."),
809+
}),
810+
}),
811+
Effect.provide(layerTestProviderHost({ cwd: workspace })),
812+
);
813+
const runtime = yield* adapter.openSession({
814+
threadId,
815+
providerSessionId: ProviderSessionId.make("cursor-window-session"),
816+
modelSelection: selection("1m"),
817+
runtimePolicy,
818+
});
819+
const providerThread = yield* runtime.ensureThread({
820+
threadId,
821+
modelSelection: selection("1m"),
822+
runtimePolicy,
823+
});
824+
825+
assert.equal(runtime.getModelContextWindow?.(selection("1m")), 1_000_000);
826+
assert.equal(runtime.getModelContextWindow?.(selection("272k")), 272_000);
827+
assert.equal(runtime.getModelContextWindow?.(selection("300K")), 300_000);
828+
assert.isUndefined(runtime.getModelContextWindow?.(selection()));
829+
assert.isUndefined(runtime.getModelContextWindow?.(selection("max")));
830+
831+
// Cursor reports no context usage, so the budget falls back to a byte
832+
// estimate of the native transcript, here a thread with long shell output.
833+
const budget = (modelContextWindow: number | undefined) =>
834+
handoffBudget({
835+
tokenCap: DEFAULT_HANDOFF_TOKEN_CAP,
836+
userText: "Continue.",
837+
attachments: [],
838+
providerThread,
839+
nativeContextEstimate: 224_379,
840+
modelContextWindow,
841+
});
842+
assert.equal(budget(undefined), 0);
843+
assert.equal(
844+
budget(runtime.getModelContextWindow?.(selection("1m"))),
845+
DEFAULT_HANDOFF_TOKEN_CAP,
846+
);
847+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(NodeServices.layer, IdAllocator.layer))),
848+
);
849+
774850
it("maps runtime modes to the SDK sandbox and auto-review controls", () => {
775851
const base = {
776852
interactionMode: "default" as const,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
4646
import { mcpToolPresentation } from "@t3tools/provider-core/server/mcpToolPresentation";
4747
import * as McpProviderSession from "@t3tools/provider-core/server/mcpSession";
4848
import { CursorTransportFailure } from "./transportFailure.ts";
49-
import { cursorSdkModelSelection } from "./sdkModel.ts";
49+
import { cursorContextWindowTokens, cursorSdkModelSelection } from "./sdkModel.ts";
5050
import {
5151
discoverCursorSkills,
5252
hasCursorSkillMention,
@@ -2369,6 +2369,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
23692369
driver: CursorAgentSdk.CURSOR_PROVIDER,
23702370
providerSessionId: input.providerSessionId,
23712371
providerSession: session,
2372+
getModelContextWindow: cursorContextWindowTokens,
23722373
events: Stream.fromEffectRepeat(Queue.take(events)),
23732374
ensureThread: Effect.fn("CursorAdapterV2.ensureThread")(
23742375
function* (threadInput: ProviderAdapter.ProviderAdapterV2EnsureThreadInput) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ModelSelection as CursorSdkModelSelection, ModelParameterValue } from "@cursor/sdk";
22
import type { ModelSelection } from "@t3tools/contracts";
3+
import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
34

45
const CURSOR_SDK_PARAMETER_TO_PROVIDER_OPTION: Readonly<Record<string, string>> = {
56
context: "contextWindow",
@@ -35,6 +36,17 @@ export function cursorSdkParameterPriority(parameterId: string): number {
3536
}
3637
}
3738

39+
/**
40+
* 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.
42+
*/
43+
export function cursorContextWindowTokens(modelSelection: ModelSelection): number | undefined {
44+
const value = getModelSelectionStringOptionValue(modelSelection, "contextWindow");
45+
const match = /^(\d+)([km])$/i.exec(value?.trim() ?? "");
46+
if (match === null) return undefined;
47+
return Number(match[1]) * (match[2]?.toLowerCase() === "m" ? 1_000_000 : 1_000);
48+
}
49+
3850
export function cursorSdkModelSelection(modelSelection: ModelSelection): CursorSdkModelSelection {
3951
return {
4052
id: modelSelection.model === "auto" ? "default" : modelSelection.model,

0 commit comments

Comments
 (0)