@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
95 lines • 4.79 kB
JavaScript
/**
* Job workers of a tool child (senpi#3566, plan todo 4). Heavy jobs and offloaded extension bodies run in workers so
* the protocol loop stays free; `startGatedWorker` starts one with the barrier clock and routes its launch messages to
* the gate, and `installWorkerBarrier` (first thing in the worker) gates `child_process`, the redirect extension imports
* resolve to, and Bun's launch globals for every launch that worker makes.
*/
import realChildProcess from "node:child_process";
import { syncBuiltinESMExports } from "node:module";
import { extname } from "node:path";
import { fileURLToPath } from "node:url";
import realWorkerThreads, { parentPort, Worker } from "node:worker_threads";
import { installGatedBun } from "./gated-bun.js";
import { createGatedChildProcess } from "./gated-child-process.js";
import { CHILD_PROCESS_LAUNCH_FUNCTIONS, installChildProcessModule, RefusedWorker } from "./gated-modules.js";
import { toolChildReentry } from "./launch-env-names.js";
import { readTestControls } from "./test-controls.js";
import { WorkerLaunchClient } from "./worker-launch.js";
function isLaunchMessage(value) {
return (typeof value === "object" &&
value !== null &&
"kind" in value &&
(value.kind === "launch" || value.kind === "invoking" || value.kind === "started" || value.kind === "done"));
}
export function startGatedWorker(gate, file, jobId, options = {}) {
const workerData = {
...options.workerData,
barrierClock: gate.clock.buffer,
jobId,
reentry: toolChildReentry(),
};
const worker = new Worker(file, { ...options, workerData });
worker.on("message", (message) => {
if (isLaunchMessage(message))
gate.handleWorker(message);
});
return worker;
}
export function installWorkerBarrier(data, env = process.env) {
const port = parentPort;
if (port === null)
throw new Error("installWorkerBarrier runs only inside a tool-child worker");
const client = new WorkerLaunchClient({
clockBuffer: data.barrierClock,
post: (message) => port.postMessage(message),
jobId: () => data.jobId,
skipAckWait: readTestControls(env).enabled("SKIP_ACK_WAIT"),
});
// The Bun gate goes first: the gated child_process makes its real calls inside its bypass (one gate per launch).
const bun = Reflect.get(globalThis, "Bun");
const bypass = isBunLaunchGlobals(bun) ? installGatedBun(bun, client) : (fn) => fn();
const gated = createGatedChildProcess(bypassingBunGate(realChildProcess, bypass), client, process.platform, data.reentry);
installChildProcessModule(gated);
for (const name of CHILD_PROCESS_LAUNCH_FUNCTIONS) {
Object.defineProperty(realChildProcess, name, {
value: Reflect.get(gated, name),
configurable: true,
writable: true,
});
}
// Node re-binds named ESM imports of the builtin to the patched functions only after this call (Bun never does;
// there, extension code reaches the gated module through the importer's redirect).
// A job worker never starts threads of its own: their launches would not pass this barrier.
Object.defineProperty(realWorkerThreads, "Worker", { value: RefusedWorker, configurable: true, writable: true });
if (Reflect.get(globalThis, "Worker") !== undefined)
Object.defineProperty(globalThis, "Worker", { value: RefusedWorker, configurable: true, writable: true });
syncBuiltinESMExports();
return client;
}
/** A copy of `node:child_process` whose launch functions run the original inside the Bun gate's bypass. */
function bypassingBunGate(module, bypass) {
const copy = { ...module };
for (const name of CHILD_PROCESS_LAUNCH_FUNCTIONS) {
const original = Reflect.get(module, name);
if (typeof original !== "function")
continue;
Reflect.set(copy, name, (...args) => bypass(() => Reflect.apply(original, module, args)));
}
return copy;
}
function isBunLaunchGlobals(value) {
return (typeof value === "object" &&
value !== null &&
typeof Reflect.get(value, "spawn") === "function" &&
typeof Reflect.get(value, "spawnSync") === "function" &&
typeof Reflect.get(value, "$") === "function");
}
/**
* A worker entry beside the calling module, named by its source path: the built package ships `.js`, so the extension
* follows the caller's own file. Keep the `"./x.ts"` literal - the single-writer audit follows it into the worker.
*/
export function siblingWorkerUrl(sourceSpecifier, callerUrl) {
const extension = extname(fileURLToPath(callerUrl));
return new URL(sourceSpecifier.replace(/\.ts$/, extension), callerUrl);
}
//# sourceMappingURL=gated-worker.js.map