Skip to content

Commit 39ab6a2

Browse files
fix(cursor): honor HTTP/1.1 transport for proxy networks (#25338)
Co-authored-by: Julius Marminge <[email protected]>
1 parent f255158 commit 39ab6a2

5 files changed

Lines changed: 175 additions & 11 deletions

File tree

‎apps/server/scripts/record-cursor-agent-sdk-replay-fixture.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,6 @@ const recordCursorReplayCommand = Command.make(
219219
);
220220

221221
Command.run(recordCursorReplayCommand, { version: "0.0.0" }).pipe(
222-
Effect.provide(Layer.mergeAll(CursorSdk.layer, NodeServices.layer)),
222+
Effect.provide(CursorSdk.layer.pipe(Layer.provideMerge(NodeServices.layer))),
223223
NodeRuntime.runMain,
224224
);

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
12
import { assert, describe, it } from "@effect/vitest";
3+
import type { CursorConfigureOptions } from "@cursor/sdk";
4+
import * as HostProcess from "@t3tools/shared/HostProcess";
5+
import * as Context from "effect/Context";
26
import * as Effect from "effect/Effect";
7+
import * as FileSystem from "effect/FileSystem";
38
import * as Layer from "effect/Layer";
9+
import * as Logger from "effect/Logger";
10+
import * as Path from "effect/Path";
411
import { vi } from "vite-plus/test";
512

613
import * as CursorSdk from "./CursorSdk.ts";
714

15+
const sdkDefaults = vi.hoisted(() => ({ useHttp1ForAgent: undefined as boolean | undefined }));
16+
17+
// Holds the SDK's process-wide default the way it does: only keys present change, null clears.
18+
vi.mock("./sdk.ts", () => ({
19+
loadCursorSdk: () => ({
20+
Cursor: {
21+
configure: ({ local }: CursorConfigureOptions) => {
22+
if (local !== undefined && "useHttp1ForAgent" in local) {
23+
sdkDefaults.useHttp1ForAgent = local.useHttp1ForAgent ?? undefined;
24+
}
25+
},
26+
},
27+
}),
28+
loadCursorSqlite: () => ({}),
29+
}));
30+
31+
const withHost = (environment: NodeJS.ProcessEnv, home: string) =>
32+
Effect.provide(
33+
Layer.mergeAll(
34+
NodeServices.layer,
35+
Layer.succeed(HostProcess.Environment, environment),
36+
Layer.succeed(HostProcess.HomeDirectory, home),
37+
),
38+
);
39+
840
// Shaped like the SDK's minified class: the constructor is not named after the error.
941
// @effect-diagnostics-next-line extendsNativeError:off - mimics the Cursor SDK's own native Error class.
1042
class o extends Error {
@@ -27,7 +59,7 @@ const rejectThroughGuard = (reason: unknown) =>
2759
for (const listener of others) process.on("unhandledRejection", listener);
2860
}
2961
return exit.mock.calls;
30-
}).pipe(Effect.scoped);
62+
}).pipe(Effect.scoped, withHost({}, "/nonexistent-home"));
3163

3264
describe("CursorSdk unhandled rejection guard", () => {
3365
it.effect("keeps the server running when the SDK writes to a closed stream", () =>
@@ -70,3 +102,76 @@ describe("CursorSdk unhandled rejection guard", () => {
70102
}),
71103
);
72104
});
105+
106+
const http1 = JSON.stringify({ network: { useHttp1ForAgent: true } });
107+
108+
describe("CursorSdk agent transport", () => {
109+
it.effect.each([
110+
{
111+
name: "when set in CURSOR_CONFIG_DIR over XDG_CONFIG_HOME",
112+
env: { CURSOR_CONFIG_DIR: "config", XDG_CONFIG_HOME: "xdg" },
113+
file: "config/cli-config.json",
114+
text: http1,
115+
expected: true,
116+
},
117+
{
118+
name: "when set in XDG_CONFIG_HOME",
119+
env: { XDG_CONFIG_HOME: "xdg" },
120+
file: "xdg/cursor/cli-config.json",
121+
text: http1,
122+
expected: true,
123+
},
124+
{
125+
name: "when set in the home directory",
126+
env: {},
127+
file: "home/.cursor/cli-config.json",
128+
text: http1,
129+
expected: true,
130+
},
131+
{
132+
name: "when it is off",
133+
env: {},
134+
file: "home/.cursor/cli-config.json",
135+
text: JSON.stringify({ network: { useHttp1ForAgent: false } }),
136+
expected: undefined,
137+
},
138+
{
139+
name: "when the file is damaged",
140+
env: {},
141+
file: "home/.cursor/cli-config.json",
142+
text: "{",
143+
expected: undefined,
144+
warns: true,
145+
},
146+
{ name: "when there is no file", env: {}, file: undefined, text: "", expected: undefined },
147+
])("follows the Cursor CLI's HTTP/1.1 setting $name", ({ env, file, text, expected, warns }) =>
148+
Effect.gen(function* () {
149+
const fileSystem = yield* FileSystem.FileSystem;
150+
const path = yield* Path.Path;
151+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cursor-cli-config-" });
152+
if (file !== undefined) {
153+
yield* fileSystem.makeDirectory(path.dirname(path.join(root, file)), { recursive: true });
154+
yield* fileSystem.writeFileString(path.join(root, file), text);
155+
}
156+
const environment = Object.fromEntries(
157+
Object.entries(env).map(([key, directory]) => [key, path.join(root, directory)]),
158+
);
159+
const warnings: Array<unknown> = [];
160+
const captureWarnings = Logger.make(({ logLevel, message }) => {
161+
if (logLevel === "Warn") warnings.push(message);
162+
});
163+
yield* Effect.gen(function* () {
164+
const sdk = Context.get(yield* Layer.build(CursorSdk.layer), CursorSdk.CursorSdk);
165+
assert.isUndefined(sdkDefaults.useHttp1ForAgent);
166+
sdk.load();
167+
assert.strictEqual(sdkDefaults.useHttp1ForAgent, expected);
168+
}).pipe(
169+
Effect.scoped,
170+
withHost(environment, path.join(root, "home")),
171+
Effect.provide(Logger.layer([captureWarnings], { mergeWithExisting: false })),
172+
);
173+
assert.isUndefined(sdkDefaults.useHttp1ForAgent);
174+
assert.lengthOf(warnings, warns ? 1 : 0);
175+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
176+
);
177+
});

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

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// @effect-diagnostics nodeBuiltinImport:off globalConsole:off -- The guard runs in Node's unhandled-rejection callback, outside an Effect runtime. stderr must match Node's default unhandled-rejection print.
2+
import * as HostProcess from "@t3tools/shared/HostProcess";
23
import * as Context from "effect/Context";
34
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
46
import * as Layer from "effect/Layer";
7+
import * as Path from "effect/Path";
8+
import * as Schema from "effect/Schema";
59

610
import { loadCursorSdk, loadCursorSqlite } from "./sdk.ts";
711

@@ -67,6 +71,39 @@ function onUnhandledRejection(reason: unknown): void {
6771
process.exit(1);
6872
}
6973

74+
const decodeCursorCliConfig = Schema.decodeEffect(
75+
Schema.fromJsonString(
76+
Schema.Struct({
77+
network: Schema.optionalKey(
78+
Schema.Struct({ useHttp1ForAgent: Schema.optionalKey(Schema.Boolean) }),
79+
),
80+
}),
81+
),
82+
);
83+
84+
// Proxies without HTTP/2 need this Cursor CLI setting, which the SDK never reads itself.
85+
const readCursorCliUseHttp1ForAgent = Effect.gen(function* () {
86+
const fileSystem = yield* FileSystem.FileSystem;
87+
const path = yield* Path.Path;
88+
const { CURSOR_CONFIG_DIR, XDG_CONFIG_HOME } = yield* HostProcess.Environment;
89+
const configDirectory = CURSOR_CONFIG_DIR?.trim()
90+
? CURSOR_CONFIG_DIR
91+
: XDG_CONFIG_HOME?.trim()
92+
? path.join(XDG_CONFIG_HOME, "cursor")
93+
: path.join(yield* HostProcess.HomeDirectory, ".cursor");
94+
const configPath = path.join(configDirectory, "cli-config.json");
95+
const config = yield* fileSystem.readFileString(configPath).pipe(
96+
Effect.flatMap(decodeCursorCliConfig),
97+
Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined),
98+
Effect.catch(() =>
99+
Effect.logWarning("Cursor CLI config could not be read; using the default agent transport", {
100+
path: configPath,
101+
}).pipe(Effect.as(undefined)),
102+
),
103+
);
104+
return config?.network?.useHttp1ForAgent === true;
105+
}).pipe(Effect.withSpan("CursorSdk.readCliUseHttp1ForAgent"));
106+
70107
/** Keeps the shell spawn guard armed while in-process agents run. */
71108
export class CursorSdk extends Context.Service<
72109
CursorSdk,
@@ -79,13 +116,34 @@ export class CursorSdk extends Context.Service<
79116
/** Provide once per process: a second guard would defer to the first and swallow real rejections. */
80117
export const layer = Layer.effect(
81118
CursorSdk,
82-
Effect.acquireRelease(
83-
Effect.sync(() => {
84-
process.on("unhandledRejection", onUnhandledRejection);
85-
}),
86-
() =>
119+
Effect.gen(function* () {
120+
yield* Effect.acquireRelease(
121+
Effect.sync(() => {
122+
process.on("unhandledRejection", onUnhandledRejection);
123+
}),
124+
() =>
125+
Effect.sync(() => {
126+
process.off("unhandledRejection", onUnhandledRejection);
127+
}),
128+
);
129+
// The SDK picks the agent transport per process, so no provider instance can own this.
130+
const useHttp1ForAgent = yield* readCursorCliUseHttp1ForAgent;
131+
let configured = false;
132+
yield* Effect.addFinalizer(() =>
87133
Effect.sync(() => {
88-
process.off("unhandledRejection", onUnhandledRejection);
134+
if (configured) loadCursorSdk().Cursor.configure({ local: { useHttp1ForAgent: null } });
89135
}),
90-
).pipe(Effect.as(CursorSdk.of({ load: loadCursorSdk, loadSqlite: loadCursorSqlite }))),
136+
);
137+
return CursorSdk.of({
138+
load: () => {
139+
const sdk = loadCursorSdk();
140+
if (useHttp1ForAgent && !configured) {
141+
sdk.Cursor.configure({ local: { useHttp1ForAgent: true } });
142+
configured = true;
143+
}
144+
return sdk;
145+
},
146+
loadSqlite: loadCursorSqlite,
147+
});
148+
}),
91149
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const layerTest = TestProviderHost.layer({ runBackgroundWork: false }).pipe(
4545
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
4646
Layer.provideMerge(IdAllocator.layer),
4747
Layer.provideMerge(McpProviderSessions.layer),
48-
Layer.provideMerge(CursorSdk.layer),
48+
Layer.provideMerge(CursorSdk.layer.pipe(Layer.provide(NodeServices.layer))),
4949
Layer.provideMerge(
5050
Layer.mock(CursorAgentSdk.CursorAgentSdkRunner)({
5151
open: () => Effect.die("Maintenance resolution must not open a Cursor session"),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, expect, it } from "@effect/vitest";
55
import * as Effect from "effect/Effect";
66
import * as FileSystem from "effect/FileSystem";
77
import * as Layer from "effect/Layer";
8+
import * as Path from "effect/Path";
89
import * as Schema from "effect/Schema";
910
import { createModelSelection } from "@t3tools/shared/model";
1011
import { beforeEach, vi } from "vite-plus/test";
@@ -40,7 +41,7 @@ const layerFs = FileSystem.layerNoop({
4041
exists: () => Effect.succeed(hasCustomPolicy),
4142
makeTempDirectoryScoped: () => Effect.succeed("/isolated-text-generation"),
4243
});
43-
const layerTest = Layer.merge(layerFs, CursorSdk.layer);
44+
const layerTest = CursorSdk.layer.pipe(Layer.provideMerge(Layer.merge(layerFs, Path.layer)));
4445

4546
const decodeCursorSettings = Schema.decodeSync(CursorSettings);
4647
const cursorSettings = decodeCursorSettings({ enabled: true });

0 commit comments

Comments
 (0)