Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CursorSdk.ts

90 lines2.9 KB

1// @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.
2import * as Context from "effect/Context";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import { loadCursorSdk, loadCursorSqlite } from "./sdk.ts";
7import type { Agent, createAgentPlatform } from "@cursor/sdk";
8import type { SqliteLocalAgentStore } from "@cursor/sdk/sqlite";
9​
10/**
11 * The SDK listens for that error, then rejects an internal promise nothing awaits.
12 * Node exits on that unhandled rejection and takes the server with it.
13 */
14const CURSOR_SHELL_SPAWN_MARKERS = [
15 "dump_zsh_state",
16 "dump_bash_state",
17 "__CURSOR_SANDBOX_ENV_RESTORE",
18] as const;
19​
20export function isCursorShellSpawnFailure(reason: unknown): boolean {
21 if (typeof reason !== "object" || reason === null) {
22 return false;
23 }
24 const syscall = Reflect.get(reason, "syscall");
25 const code = Reflect.get(reason, "code");
26 const spawnargs = Reflect.get(reason, "spawnargs");
27 if (typeof syscall !== "string" || !syscall.startsWith("spawn") || typeof code !== "string") {
28 return false;
29 }
30 if (!Array.isArray(spawnargs)) {
31 return false;
32 }
33 return spawnargs.some(
34 (arg) =>
35 typeof arg === "string" && CURSOR_SHELL_SPAWN_MARKERS.some((marker) => arg.includes(marker)),
36 );
38​
39function onUnhandledRejection(reason: unknown): void {
40 if (isCursorShellSpawnFailure(reason)) {
41 console.error("Cursor shell spawn failed. The server will keep running.", reason);
42 return;
43 }
44 if (process.listenerCount("unhandledRejection") > 1) {
45 return;
46 }
47 console.error(reason);
48 process.exit(1);
50​
51/**
52 * The SDK entry points that run agents in this process. Reaching them through
53 * this service keeps the shell spawn guard armed while they are in use.
54 */
55export class CursorSdk extends Context.Service<
56 CursorSdk,
57 {
58 readonly Agent: typeof Agent;
59 readonly createAgentPlatform: typeof createAgentPlatform;
60 readonly SqliteLocalAgentStore: typeof SqliteLocalAgentStore;
61 }
62>()("@t3tools/provider-cursor/server/CursorSdk") {}
63​
64/** Provide once per process: a second guard would defer to the first and swallow real rejections. */
65export const layer = Layer.effect(
66 CursorSdk,
67 Effect.acquireRelease(
68 Effect.sync(() => {
69 process.on("unhandledRejection", onUnhandledRejection);
70 }),
71 () =>
72 Effect.sync(() => {
73 process.off("unhandledRejection", onUnhandledRejection);
74 }),
75 ).pipe(
76 Effect.as(
77 CursorSdk.of({
78 get Agent() {
79 return loadCursorSdk().Agent;
80 },
81 get createAgentPlatform() {
82 return loadCursorSdk().createAgentPlatform;
83 },
84 get SqliteLocalAgentStore() {
85 return loadCursorSqlite().SqliteLocalAgentStore;
86 },
87 }),
88 ),
89 ),
90);