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

CursorSdk.ts

91 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 { loadCursorSdk, loadCursorSqlite } from "./sdk.ts";
7​
8// The SDK leaves a shell-error promise unhandled, which would exit the server.
9const CURSOR_SHELL_SPAWN_MARKERS = [
10 "dump_zsh_state",
11 "dump_bash_state",
12 "__CURSOR_SANDBOX_ENV_RESTORE",
13] as const;
14​
15function isCursorShellSpawnFailure(reason: unknown): boolean {
16 if (typeof reason !== "object" || reason === null) {
17 return false;
18 }
19 const syscall = Reflect.get(reason, "syscall");
20 const code = Reflect.get(reason, "code");
21 const spawnargs = Reflect.get(reason, "spawnargs");
22 if (typeof syscall !== "string" || !syscall.startsWith("spawn") || typeof code !== "string") {
23 return false;
24 }
25 if (!Array.isArray(spawnargs)) {
26 return false;
27 }
28 return spawnargs.some(
29 (arg) =>
30 typeof arg === "string" && CURSOR_SHELL_SPAWN_MARKERS.some((marker) => arg.includes(marker)),
31 );
33​
34/**
35 * The SDK writes final frames from promises nothing awaits. A closed stream rejects them
36 * with WriteIterableClosedError; an ended request body rejects them with the Aborted
37 * ConnectError its connect-node pump wraps around ERR_STREAM_WRITE_AFTER_END. The classes
38 * are minified, so only `name` identifies them.
39 */
40function isCursorClosedStreamWrite(reason: unknown): boolean {
41 if (!(reason instanceof Error)) {
42 return false;
43 }
44 if (reason.name === "WriteIterableClosedError") {
45 return true;
46 }
47 return (
48 reason.name === "ConnectError" &&
49 reason.cause instanceof Error &&
50 Reflect.get(reason.cause, "code") === "ERR_STREAM_WRITE_AFTER_END"
51 );
53​
54function onUnhandledRejection(reason: unknown): void {
55 if (isCursorShellSpawnFailure(reason)) {
56 console.error("Cursor shell spawn failed. The server will keep running.", reason);
57 return;
58 }
59 if (isCursorClosedStreamWrite(reason)) {
60 console.error("Cursor SDK wrote to a closed stream. The server will keep running.");
61 return;
62 }
63 if (process.listenerCount("unhandledRejection") > 1) {
64 return;
65 }
66 console.error(reason);
67 process.exit(1);
69​
70/** Keeps the shell spawn guard armed while in-process agents run. */
71export class CursorSdk extends Context.Service<
72 CursorSdk,
73 {
74 readonly load: typeof loadCursorSdk;
75 readonly loadSqlite: typeof loadCursorSqlite;
76 }
77>()("@t3tools/provider-cursor/server/CursorSdk") {}
78​
79/** Provide once per process: a second guard would defer to the first and swallow real rejections. */
80export const layer = Layer.effect(
81 CursorSdk,
82 Effect.acquireRelease(
83 Effect.sync(() => {
84 process.on("unhandledRejection", onUnhandledRejection);
85 }),
86 () =>
87 Effect.sync(() => {
88 process.off("unhandledRejection", onUnhandledRejection);
89 }),
90 ).pipe(Effect.as(CursorSdk.of({ load: loadCursorSdk, loadSqlite: loadCursorSqlite }))),
91);