Files
Netcatty/electron/bridges/aiBridge/sdk/cursorWorkerHost.cjs
T
8017c144fe feat(cli): bind Skills+CLI chat session via host env (#3347)
* feat(workspace): search hosts by tag (#3001)

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat(cli): bind Skills+CLI chat session via host env

Hide --chat-session from agents so they cannot spoof another chat tenant.
The host injects NETCATTY_CLI_CHAT_SESSION_ID, and Copilot/CodeBuddy
approval gates reject leftover flags and env-prefix overrides.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): isolate Skills+CLI chat env across concurrent SDK turns

Cursor and OpenCode mutate process.env around spawn. Serialize those
writes so overlapping chats cannot inherit or restore the wrong
NETCATTY_CLI_CHAT_SESSION_ID.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Revert "feat(workspace): search hosts by tag (#3001)"

This reverts commit 4b651a3841.

* fix(ai): preserve chat isolation when cancelling Cursor startup

* fix(ai): isolate Cursor turns in cancellable workers

* fix(ai): contain native Cursor cleanup in a utility process

* fix(ai): report Cursor utility process fatal errors

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-12 12:16:57 +08:00

88 lines
3.1 KiB
JavaScript

"use strict";
const path = require("node:path");
function createCursorWorker(options) {
// Native SDK storage can abort its process during forced cleanup. Keep that
// failure outside Electron main as well as isolating the chat environment.
return require("electron").utilityProcess.fork(
path.join(__dirname, "cursorTurnWorker.cjs"), [],
{ ...options, serviceName: "Netcatty Cursor", stdio: "pipe" },
);
}
// Each turn gets its own environment and SDK instance. A stalled SDK network
// request cannot retain a main-process environment lock or block another chat.
function runCursorWorkerTurn({ emitter, signal, ...params }, createWorker = createCursorWorker) {
let sessionId = params.resumeSessionId || null;
if (signal?.aborted) return Promise.resolve({ sessionId });
return new Promise((resolve) => {
let worker;
let settled = false;
let spawned = false;
let stopTimer;
const finish = (result) => {
if (settled) return;
settled = true;
signal?.removeEventListener("abort", onAbort);
resolve(result);
};
const terminate = () => {
clearTimeout(stopTimer);
worker.kill();
};
const onAbort = () => {
if (spawned) worker.postMessage({ type: "abort" });
// Allow run.cancel()/agent.close() first, then contain a stuck startup.
stopTimer = setTimeout(terminate, 1000);
finish({ sessionId });
};
try {
worker = createWorker({
env: params.runtimeEnv || process.env,
});
} catch (error) {
emitter.emitError(error?.message || String(error));
finish({ sessionId });
return;
}
worker.stdout?.resume();
worker.stderr?.resume();
worker.once("spawn", () => {
spawned = true;
if (settled) terminate();
else worker.postMessage({ type: "start", params });
});
worker.on("message", (message) => {
if (message?.type === "event") {
if (settled) return;
if (message.method === "sessionId") sessionId = message.args[0];
const handler = emitter[message.method];
if (typeof handler === "function") handler(...message.args);
} else if (message?.type === "result" || message?.type === "error") {
if (!settled && message.type === "error") emitter.emitError(message.message);
finish(message.result || { sessionId });
terminate();
}
});
worker.once("error", (type, location) => {
// The third argument is a full Node diagnostic report and can contain
// credentials from the environment. Keep it out of persisted chat errors.
const message = type instanceof Error
? type.message
: [type, location].filter(Boolean).join(": ");
if (!settled) emitter.emitError(message || "Cursor worker failed.");
finish({ sessionId });
terminate();
});
worker.once("exit", (code) => {
clearTimeout(stopTimer);
if (!settled) emitter.emitError(`Cursor worker exited before completing (code ${code}).`);
finish({ sessionId });
});
signal?.addEventListener("abort", onAbort, { once: true });
if (signal?.aborted) onAbort();
});
}
module.exports = { runCursorWorkerTurn };