import { Worker } from "node:worker_threads"; import { existsSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import type { CapabilityCallInfo, PluginCapabilityGrant, PluginInvocationResult, PluginManifest } from "@toolctrl/plugin-runtime"; import type { SandboxWorkerResponse } from "./plugin-sandbox-worker.js"; export type PluginSandboxHostLogEntry = SandboxWorkerResponse["hostLogs"][number]; export type PluginSandboxIsolatedResult = { result: PluginInvocationResult; capabilityCalls: CapabilityCallInfo[]; droppedCapabilityCallCount: number; hostLogs: PluginSandboxHostLogEntry[]; }; type InvokePluginSandboxInWorkerArgs = { wasmBuffer: Uint8Array; manifest: PluginManifest; capabilityGrants: ReadonlyArray; hookName: string; payload: unknown; memoryLimitMb: number; fuelLimit: number; maxCapabilityCallEntries?: number; wallClockMs: number; workerUrl?: URL; }; const DEFAULT_TIMEOUT_MESSAGE = "plugin sandbox wall-clock timeout"; type SandboxWorkerLocation = { url: URL; options?: ConstructorParameters[1]; }; type SandboxWorkerLocationDeps = { compiledCandidates?: readonly string[]; sourcePath?: string; exists?: (path: string) => boolean; isProduction?: boolean; buildSource?: (sourcePath: string) => Promise; }; function getCurrentDir(): string { return dirname(fileURLToPath(import.meta.url)); } function getCompiledSandboxWorkerCandidates(): string[] { const currentDir = getCurrentDir(); const candidates = [ join(currentDir, "plugin-sandbox-worker.js"), join(currentDir, "jobs", "plugin-sandbox-worker.js"), ]; if (currentDir.endsWith(`${join("src", "jobs")}`)) { candidates.push(join(dirname(dirname(currentDir)), "dist", "jobs", "plugin-sandbox-worker.js")); } return candidates; } function getSourceSandboxWorkerPath(): string { return join(getCurrentDir(), "plugin-sandbox-worker.ts"); } async function buildSourceSandboxWorkerDataUrl(sourcePath: string): Promise { const { build } = await import("esbuild"); const result = await build({ entryPoints: [sourcePath], bundle: true, platform: "node", format: "esm", target: "node20", write: false, sourcemap: "inline", logLevel: "silent", }); const code = result.outputFiles[0]?.text; if (!code) { throw new Error("plugin sandbox worker bundle emitted no output"); } return new URL(`data:text/javascript;base64,${Buffer.from(code).toString("base64")}`); } async function resolveSandboxWorkerLocation(workerUrl?: URL, deps: SandboxWorkerLocationDeps = {}): Promise { if (workerUrl) { return { url: workerUrl }; } const exists = deps.exists ?? existsSync; for (const compiled of deps.compiledCandidates ?? getCompiledSandboxWorkerCandidates()) { if (exists(compiled)) { return { url: pathToFileURL(compiled) }; } } if (deps.isProduction ?? process.env.NODE_ENV === "production") { throw new Error("Compiled plugin sandbox worker is required in production."); } const buildSource = deps.buildSource ?? buildSourceSandboxWorkerDataUrl; return { url: await buildSource(deps.sourcePath ?? getSourceSandboxWorkerPath()) }; } export async function resolveSandboxWorkerLocationForTest(deps: SandboxWorkerLocationDeps): Promise { return resolveSandboxWorkerLocation(undefined, deps); } function timeoutResult(message = DEFAULT_TIMEOUT_MESSAGE): PluginSandboxIsolatedResult { return { result: { ok: false, reason: "timeout", fuelUsed: 0, message }, capabilityCalls: [], droppedCapabilityCallCount: 0, hostLogs: [], }; } export async function invokePluginSandboxInWorker(args: InvokePluginSandboxInWorkerArgs): Promise { const wallClockMs = Math.max(1, Math.floor(args.wallClockMs)); const workerLocation = await resolveSandboxWorkerLocation(args.workerUrl); const worker = new Worker(workerLocation.url, workerLocation.options); return await new Promise((resolve) => { let settled = false; const settle = (result: PluginSandboxIsolatedResult) => { if (settled) return; settled = true; clearTimeout(timeout); worker.removeAllListeners(); void worker.terminate().catch(() => undefined).finally(() => resolve(result)); }; const timeout = setTimeout(() => { settle(timeoutResult()); }, wallClockMs); worker.once("message", (response: SandboxWorkerResponse) => { settle({ ...response, droppedCapabilityCallCount: response.droppedCapabilityCallCount ?? 0, }); }); worker.once("error", (error) => { const message = error instanceof Error ? error.message : String(error); settle({ result: { ok: false, reason: "trap", fuelUsed: 0, message }, capabilityCalls: [], droppedCapabilityCallCount: 0, hostLogs: [], }); }); worker.once("exit", (code) => { if (code !== 0) { settle({ result: { ok: false, reason: "trap", fuelUsed: 0, message: `plugin sandbox worker exited with code ${code}` }, capabilityCalls: [], droppedCapabilityCallCount: 0, hostLogs: [], }); } }); worker.postMessage({ wasmBuffer: args.wasmBuffer, manifest: args.manifest, capabilityGrants: args.capabilityGrants, hookName: args.hookName, payload: args.payload, memoryLimitMb: args.memoryLimitMb, fuelLimit: args.fuelLimit, maxCapabilityCallEntries: args.maxCapabilityCallEntries, }); }); }