Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off globalConsole:off -- Installed before the SDK loads, outside an Effect runtime. stderr must match Node's default unhandled-rejection print.
2import * as NodeModule from "node:module";
3
4/**
5 * The Cursor Agent SDK runs in this process and spawns a shell for tool
6 * calls. The command is Cursor's sandbox wrapper (`dump_zsh_state`,
7 * `dump_bash_state`, `__CURSOR_SANDBOX_ENV_RESTORE`). A bad working directory
8 * makes Node report `spawn /bin/zsh ENOENT`. The SDK listens for that error,
9 * then rejects an internal promise nothing awaits. Node exits on that
10 * unhandled rejection and takes the server with it.
11 *
12 * This listener ignores only that rejection. Every other unhandled rejection
13 * still prints and exits when this is the process's only listener, matching
14 * Node's default. `child_process.spawn` itself is left alone, so git,
15 * terminals, and other providers keep their own error handling.
16 */
17const CURSOR_SHELL_SPAWN_MARKERS = [
18 "dump_zsh_state",
19 "dump_bash_state",
20 "__CURSOR_SANDBOX_ENV_RESTORE",
21] as const;
22
23export function isCursorShellSpawnFailure(reason: unknown): boolean {
24 if (typeof reason !== "object" || reason === null) {
25 return false;
26 }
27 const syscall = Reflect.get(reason, "syscall");
28 const code = Reflect.get(reason, "code");
29 const spawnargs = Reflect.get(reason, "spawnargs");
30 if (typeof syscall !== "string" || !syscall.startsWith("spawn") || typeof code !== "string") {
31 return false;
32 }
33 if (!Array.isArray(spawnargs)) {
34 return false;
35 }
36 return spawnargs.some(
37 (arg) =>
38 typeof arg === "string" && CURSOR_SHELL_SPAWN_MARKERS.some((marker) => arg.includes(marker)),
39 );
40}
41
42let cursorShellSpawnGuardInstalled = false;
43
44function installCursorShellSpawnGuard(): void {
45 if (cursorShellSpawnGuardInstalled) {
46 return;
47 }
48 cursorShellSpawnGuardInstalled = true;
49
50 process.on("unhandledRejection", (reason) => {
51 if (isCursorShellSpawnFailure(reason)) {
52 console.error("Cursor shell spawn failed. The server will keep running.", reason);
53 return;
54 }
55 // Another handler registered besides this one owns the decision.
56 if (process.listenerCount("unhandledRejection") > 1) {
57 return;
58 }
59 console.error(reason);
60 process.exit(1);
61 });
62}
63
64// Arm before the SDK loads. Its shell tools spawn from this process.
65installCursorShellSpawnGuard();
66
67// Cursor's Webpack chunks and local helpers must stay beside the SDK entry.
68// createRequire also loads that disk-backed package from a Node SEA executable.
69const requireCursorSdk = NodeModule.createRequire(import.meta.url);
70export const {
71 Agent,
72 AuthenticationError,
73 createAgentPlatform,
74 Cursor,
75 CursorSdkError,
76 InMemoryCredentialStore,
77} = requireCursorSdk("@cursor/sdk") as typeof import("@cursor/sdk");