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

CursorSdk.ts

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