|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off globalTimers:off -- The probe is a plain Node child; this deadline kills it if it hangs. |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import { describe, expect, it } from "vite-plus/test"; |
| 4 | + |
| 5 | +const cursorSdkUrl = new URL("./sdk.ts", import.meta.url).href; |
| 6 | + |
| 7 | +// Loads the real SDK in a fresh process. The send probe stops before transport. |
| 8 | +const probeProgram = ` |
| 9 | +import { mkdtempSync } from "node:fs"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { join } from "node:path"; |
| 12 | +import { createRequire } from "node:module"; |
| 13 | +
|
| 14 | +const sdkRequire = createRequire(createRequire(${JSON.stringify(cursorSdkUrl)}).resolve("@cursor/sdk")); |
| 15 | +const { proto3, ScalarType } = sdkRequire("@bufbuild/protobuf"); |
| 16 | +const mode = process.argv[1]; |
| 17 | +if (mode === "sdk-model-requests") { |
| 18 | + process.on("uncaughtException", error => { |
| 19 | + console.error(error.stack); |
| 20 | + process.exit(1); |
| 21 | + }); |
| 22 | +} |
| 23 | +const dir = mkdtempSync(join(tmpdir(), "cursor-sdk-model-")); |
| 24 | +const { createAgentPlatform } = await import(${JSON.stringify(cursorSdkUrl)}); |
| 25 | +
|
| 26 | +if (mode === "sdk-model-requests") { |
| 27 | + const { JsonlLocalAgentStore } = sdkRequire("@cursor/sdk"); |
| 28 | + const store = new JsonlLocalAgentStore(join(dir, "store")); |
| 29 | + const platform = await createAgentPlatform({ localStore: store, workspaceRef: dir }); |
| 30 | + // Catalog validation is an external dependency; the SDK's send path and |
| 31 | + // generated RequestedModel constructor stay real. Stop before transport. |
| 32 | + platform.resolveLocalModelSelection = async selection => selection; |
| 33 | + globalThis.fetch = async () => { throw new Error("Unexpected network request"); }; |
| 34 | + const requests = []; |
| 35 | + const initPartial = proto3.util.initPartial; |
| 36 | + const captured = new Error("model request captured before transport"); |
| 37 | + proto3.util.initPartial = function(source, target) { |
| 38 | + initPartial.call(this, source, target); |
| 39 | + if (target.getType().typeName !== "agent.v1.RequestedModel" || !target.modelId) return; |
| 40 | + const decoded = target.getType().fromBinary(target.toBinary()); |
| 41 | + requests.push({ model: decoded.modelId, maxMode: decoded.maxMode, |
| 42 | + context: decoded.parameters.find(parameter => parameter.id === "context")?.value }); |
| 43 | + throw captured; |
| 44 | + }; |
| 45 | + for (const [id, context] of [ |
| 46 | + ["grok-4.7", "500k"], ["grok-4.7", "256k"], |
| 47 | + ["claude-opus-5-5", "1m"], ["claude-opus-5-5", "300k"], |
| 48 | + ]) { |
| 49 | + const agent = await platform.createAgent({ |
| 50 | + apiKey: "t3-offline-test-key", model: { id, params: [{ id: "context", value: context }] }, |
| 51 | + mode: "plan", tools: [], local: { cwd: dir, store, autoReview: false, |
| 52 | + settingSources: [], sandboxOptions: { enabled: false }, enableAgentRetries: false }, |
| 53 | + }); |
| 54 | + try { |
| 55 | + await agent.send("Reply with exactly: pong"); |
| 56 | + throw new Error("SDK did not construct a model request"); |
| 57 | + } catch (error) { |
| 58 | + if (error !== captured && error.cause !== captured) throw error; |
| 59 | + } finally { |
| 60 | + await agent[Symbol.asyncDispose](); |
| 61 | + } |
| 62 | + } |
| 63 | + console.log(JSON.stringify(requests)); |
| 64 | + process.exit(0); |
| 65 | +} |
| 66 | +
|
| 67 | +if (mode === "model-requests") { |
| 68 | + const Parameter = proto3.makeMessageType("agent.v1.ModelParameter", [ |
| 69 | + { no: 1, name: "id", kind: "scalar", T: ScalarType.STRING }, |
| 70 | + { no: 2, name: "value", kind: "scalar", T: ScalarType.STRING }, |
| 71 | + ]); |
| 72 | + const fields = [ |
| 73 | + { no: 1, name: "model_id", kind: "scalar", T: ScalarType.STRING }, |
| 74 | + { no: 2, name: "max_mode", kind: "scalar", T: ScalarType.BOOL }, |
| 75 | + { no: 3, name: "parameters", kind: "message", T: Parameter, repeated: true }, |
| 76 | + ]; |
| 77 | + const RequestedModel = proto3.makeMessageType("agent.v1.RequestedModel", fields); |
| 78 | + const OtherMessage = proto3.makeMessageType("other.RequestedModel", fields); |
| 79 | + const cases = [ |
| 80 | + { modelId: "claude-opus-5-5", context: "1m" }, |
| 81 | + { modelId: "grok-4.7", context: "500k" }, |
| 82 | + { modelId: "gpt-5.6-sol", context: "1M" }, |
| 83 | + { modelId: "claude-opus-5-5", context: "0.5m" }, |
| 84 | + { modelId: "claude-opus-5-5", context: "300k" }, |
| 85 | + { modelId: "grok-4.7", context: "256k" }, |
| 86 | + { modelId: "gpt-5.6-sol", context: "272k" }, |
| 87 | + { modelId: "claude-sonnet-4-6", context: "200k" }, |
| 88 | + { modelId: "claude-opus-5-5", context: "0.3m" }, |
| 89 | + { modelId: "default" }, |
| 90 | + { modelId: "custom", context: "unknown" }, |
| 91 | + { modelId: "claude-opus-5-5", context: "300k", maxMode: true }, |
| 92 | + ]; |
| 93 | + const requests = cases.map(({ context, ...selection }) => { |
| 94 | + const source = { |
| 95 | + ...selection, |
| 96 | + parameters: [ |
| 97 | + { id: "reasoning_effort", value: "high" }, |
| 98 | + ...(context === undefined ? [] : [{ id: "context", value: context }]), |
| 99 | + { id: "fast", value: "true" }, |
| 100 | + ], |
| 101 | + }; |
| 102 | + const sourceBefore = JSON.stringify(source); |
| 103 | + const request = new RequestedModel(source); |
| 104 | + const decoded = RequestedModel.fromBinary(request.toBinary()); |
| 105 | + return { |
| 106 | + maxMode: decoded.maxMode, |
| 107 | + modelId: decoded.modelId, |
| 108 | + parameters: decoded.parameters.map(({ id, value }) => ({ id, value })), |
| 109 | + sourceUnchanged: sourceBefore === JSON.stringify(source), |
| 110 | + }; |
| 111 | + }); |
| 112 | + const unrelated = new OtherMessage({ parameters: [{ id: "context", value: "1m" }] }); |
| 113 | + console.log(JSON.stringify({ requests, unrelatedMaxMode: unrelated.maxMode })); |
| 114 | + process.exit(0); |
| 115 | +} |
| 116 | +
|
| 117 | +console.error("Unknown Cursor SDK model probe: " + mode); |
| 118 | +process.exit(2); |
| 119 | +`; |
| 120 | + |
| 121 | +function runProbe(mode: string): Promise<{ code: number | null; stdout: string; stderr: string }> { |
| 122 | + return new Promise((resolve, reject) => { |
| 123 | + const child = NodeChildProcess.spawn( |
| 124 | + process.execPath, |
| 125 | + [ |
| 126 | + "--experimental-strip-types", |
| 127 | + "--disable-warning=ExperimentalWarning", |
| 128 | + "--input-type=module", |
| 129 | + "-e", |
| 130 | + probeProgram, |
| 131 | + mode, |
| 132 | + ], |
| 133 | + { stdio: ["ignore", "pipe", "pipe"] }, |
| 134 | + ); |
| 135 | + let stdout = ""; |
| 136 | + let stderr = ""; |
| 137 | + child.stdout.setEncoding("utf8"); |
| 138 | + child.stderr.setEncoding("utf8"); |
| 139 | + child.stdout.on("data", (chunk: string) => { |
| 140 | + stdout += chunk; |
| 141 | + }); |
| 142 | + child.stderr.on("data", (chunk: string) => { |
| 143 | + stderr += chunk; |
| 144 | + }); |
| 145 | + const timer = setTimeout(() => { |
| 146 | + child.kill(); |
| 147 | + reject(new Error(`probe ${mode} timed out`)); |
| 148 | + }, 5_000); |
| 149 | + child.on("error", reject); |
| 150 | + child.on("close", (code) => { |
| 151 | + clearTimeout(timer); |
| 152 | + resolve({ code, stdout, stderr }); |
| 153 | + }); |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +describe("Cursor long-context requests", () => { |
| 158 | + it("sets Max Mode in real SDK sends for the reported context tiers", async () => { |
| 159 | + const result = await runProbe("sdk-model-requests"); |
| 160 | + expect(result.code, result.stderr.slice(-2_000)).toBe(0); |
| 161 | + expect(JSON.parse(result.stdout)).toEqual([ |
| 162 | + { model: "grok-4.7", context: "500k", maxMode: true }, |
| 163 | + { model: "grok-4.7", context: "256k", maxMode: false }, |
| 164 | + { model: "claude-opus-5-5", context: "1m", maxMode: true }, |
| 165 | + { model: "claude-opus-5-5", context: "300k", maxMode: false }, |
| 166 | + ]); |
| 167 | + }); |
| 168 | + |
| 169 | + it("serializes Max Mode for long tiers and preserves standard requests", async () => { |
| 170 | + const result = await runProbe("model-requests"); |
| 171 | + expect(result.code).toBe(0); |
| 172 | + const { requests, unrelatedMaxMode } = JSON.parse(result.stdout); |
| 173 | + expect(requests.map((request: { maxMode: boolean }) => request.maxMode)).toEqual([ |
| 174 | + true, |
| 175 | + true, |
| 176 | + true, |
| 177 | + true, |
| 178 | + false, |
| 179 | + false, |
| 180 | + false, |
| 181 | + false, |
| 182 | + false, |
| 183 | + false, |
| 184 | + false, |
| 185 | + true, |
| 186 | + ]); |
| 187 | + expect(requests[0]).toEqual({ |
| 188 | + maxMode: true, |
| 189 | + modelId: "claude-opus-5-5", |
| 190 | + parameters: [ |
| 191 | + { id: "reasoning_effort", value: "high" }, |
| 192 | + { id: "context", value: "1m" }, |
| 193 | + { id: "fast", value: "true" }, |
| 194 | + ], |
| 195 | + sourceUnchanged: true, |
| 196 | + }); |
| 197 | + expect(requests.every((request: { sourceUnchanged: boolean }) => request.sourceUnchanged)).toBe( |
| 198 | + true, |
| 199 | + ); |
| 200 | + expect(unrelatedMaxMode).toBe(false); |
| 201 | + }); |
| 202 | +}); |
0 commit comments