@zackbart/connecta 0.7.1 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +122 -0
  2. package/README.md +5 -2
  3. package/dist/connector-scope.d.ts +7 -4
  4. package/dist/connector-scope.d.ts.map +1 -1
  5. package/dist/connector-scope.js +36 -16
  6. package/dist/connector-scope.js.map +1 -1
  7. package/dist/connectors/remote-mcp.d.ts.map +1 -1
  8. package/dist/connectors/remote-mcp.js +164 -95
  9. package/dist/connectors/remote-mcp.js.map +1 -1
  10. package/dist/credential-health.d.ts +11 -7
  11. package/dist/credential-health.d.ts.map +1 -1
  12. package/dist/credential-health.js +26 -19
  13. package/dist/credential-health.js.map +1 -1
  14. package/dist/execute.d.ts +3 -0
  15. package/dist/execute.d.ts.map +1 -1
  16. package/dist/execute.js +70 -29
  17. package/dist/execute.js.map +1 -1
  18. package/dist/executor-admission.d.ts +53 -0
  19. package/dist/executor-admission.d.ts.map +1 -0
  20. package/dist/executor-admission.js +151 -0
  21. package/dist/executor-admission.js.map +1 -0
  22. package/dist/executor-result.d.ts +13 -0
  23. package/dist/executor-result.d.ts.map +1 -0
  24. package/dist/executor-result.js +57 -0
  25. package/dist/executor-result.js.map +1 -0
  26. package/dist/executors/quickjs-child.d.ts +2 -0
  27. package/dist/executors/quickjs-child.d.ts.map +1 -0
  28. package/dist/executors/quickjs-child.js +131 -0
  29. package/dist/executors/quickjs-child.js.map +1 -0
  30. package/dist/executors/quickjs-protocol.d.ts +55 -0
  31. package/dist/executors/quickjs-protocol.d.ts.map +1 -0
  32. package/dist/executors/quickjs-protocol.js +22 -0
  33. package/dist/executors/quickjs-protocol.js.map +1 -0
  34. package/dist/executors/quickjs-runtime.d.ts +26 -0
  35. package/dist/executors/quickjs-runtime.d.ts.map +1 -0
  36. package/dist/executors/quickjs-runtime.js +383 -0
  37. package/dist/executors/quickjs-runtime.js.map +1 -0
  38. package/dist/executors/quickjs.d.ts +20 -15
  39. package/dist/executors/quickjs.d.ts.map +1 -1
  40. package/dist/executors/quickjs.js +465 -265
  41. package/dist/executors/quickjs.js.map +1 -1
  42. package/dist/index.d.ts +15 -3
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +7 -0
  45. package/dist/index.js.map +1 -1
  46. package/dist/meta-tools.d.ts +4 -0
  47. package/dist/meta-tools.d.ts.map +1 -1
  48. package/dist/meta-tools.js +49 -12
  49. package/dist/meta-tools.js.map +1 -1
  50. package/dist/node.d.ts.map +1 -1
  51. package/dist/node.js +36 -3
  52. package/dist/node.js.map +1 -1
  53. package/dist/registry.d.ts +21 -24
  54. package/dist/registry.d.ts.map +1 -1
  55. package/dist/registry.js +37 -25
  56. package/dist/registry.js.map +1 -1
  57. package/dist/server.d.ts.map +1 -1
  58. package/dist/server.js +19 -9
  59. package/dist/server.js.map +1 -1
  60. package/dist/timeout.d.ts +9 -4
  61. package/dist/timeout.d.ts.map +1 -1
  62. package/dist/timeout.js +34 -4
  63. package/dist/timeout.js.map +1 -1
  64. package/dist/toolkits.d.ts +8 -0
  65. package/dist/toolkits.d.ts.map +1 -1
  66. package/dist/toolkits.js +3 -0
  67. package/dist/toolkits.js.map +1 -1
  68. package/dist/types.d.ts +19 -2
  69. package/dist/types.d.ts.map +1 -1
  70. package/dist/ui.d.ts +13 -1
  71. package/dist/ui.d.ts.map +1 -1
  72. package/dist/ui.js +188 -7
  73. package/dist/ui.js.map +1 -1
  74. package/dist/version.d.ts +1 -1
  75. package/dist/version.js +1 -1
  76. package/package.json +1 -1
  77. package/src/connector-scope.ts +43 -18
  78. package/src/connectors/remote-mcp.ts +184 -102
  79. package/src/credential-health.ts +33 -17
  80. package/src/execute.ts +93 -35
  81. package/src/executor-admission.ts +229 -0
  82. package/src/executor-result.ts +63 -0
  83. package/src/executors/quickjs-child.ts +168 -0
  84. package/src/executors/quickjs-protocol.ts +77 -0
  85. package/src/executors/quickjs-runtime.ts +440 -0
  86. package/src/executors/quickjs.ts +669 -302
  87. package/src/index.ts +30 -5
  88. package/src/meta-tools.ts +84 -27
  89. package/src/node.ts +36 -2
  90. package/src/registry.ts +53 -22
  91. package/src/server.ts +21 -10
  92. package/src/timeout.ts +41 -4
  93. package/src/toolkits.ts +11 -0
  94. package/src/types.ts +19 -2
  95. package/src/ui.ts +218 -12
  96. package/src/version.ts +1 -1
@@ -1,349 +1,716 @@
1
- // QuickJS-in-WebAssembly executor the Node-side sandbox for execute_code.
2
- //
3
- // Model-written code runs inside a QuickJS interpreter compiled to WASM:
4
- // memory-safe, no ambient authority — the guest has no fetch, filesystem,
5
- // env, timers, or imports. The only bridge to the host is `__call`, which
6
- // returns a QuickJS promise that the host resolves with a JSON payload after
7
- // running the provider function; `executePendingJobs` drives guest
8
- // continuations from a host-side loop. Values cross the boundary as JSON, so
9
- // provider args/results must be JSON-serializable.
1
+ // Node's built-in code-mode executor. QuickJS itself lives in a disposable
2
+ // child process: guest CPU, WASM aborts, and interpreter OOMs cannot block or
3
+ // terminate the HTTP-serving process.
10
4
 
5
+ import { fork, type ChildProcess } from "node:child_process";
6
+ import { fileURLToPath } from "node:url";
11
7
  import {
12
- getQuickJS,
13
- shouldInterruptAfterDeadline,
14
- type QuickJSContext,
15
- type QuickJSDeferredPromise,
16
- } from "quickjs-emscripten";
17
- import type { ExecuteResult, Executor, ExecutorProvider } from "../types.js";
8
+ AdmissionController,
9
+ ExecutorAdmissionError,
10
+ } from "../executor-admission.js";
11
+ import type {
12
+ AdmittingExecutor,
13
+ ExecuteResult,
14
+ ExecutorLease,
15
+ ExecutorProvider,
16
+ } from "../types.js";
17
+ import {
18
+ MAX_QUICKJS_IPC_BYTES,
19
+ MAX_QUICKJS_HOST_RPC_BYTES,
20
+ type ChildToParentMessage,
21
+ type ExecutionPayload,
22
+ type HostCallPayload,
23
+ type HostResultPayload,
24
+ type ParentToChildMessage,
25
+ type RunPayload,
26
+ serializedBytes,
27
+ stringifyBounded,
28
+ } from "./quickjs-protocol.js";
29
+ import type { QuickJsRuntimeOptions } from "./quickjs-runtime.js";
30
+
31
+ export { normalizeCode } from "./quickjs-runtime.js";
18
32
 
19
33
  export interface QuickJsExecutorOptions {
20
34
  /**
21
- * Wall-clock budget for the whole execution, host tool calls included
22
- * (same wall-clock-budget semantics as codemode's DynamicWorkerExecutor).
23
- * Default 30s — intentionally tighter than codemode's 60s default: connecta
24
- * sandbox code is tool-call glue, not compute, so a shorter leash surfaces
25
- * hung downstreams sooner. Raise it here if a workload legitimately needs it.
35
+ * Wall-clock budget for the whole execution, host tool calls included.
36
+ * Default 30s.
26
37
  */
27
38
  timeoutMs?: number;
28
- /** Guest heap limit. Default 64 MiB. */
39
+ /**
40
+ * Cumulative time spent synchronously driving guest JavaScript. Host waits do
41
+ * not consume it. Default 250ms.
42
+ */
43
+ cpuTimeMs?: number;
44
+ /** Guest heap limit per child execution. Default 64 MiB. */
29
45
  memoryLimitBytes?: number;
30
46
  /** Guest stack limit. Default 1 MiB. */
31
47
  maxStackSizeBytes?: number;
48
+ /** Maximum simultaneous executions/child processes. Default 1. */
49
+ concurrency?: number;
50
+ /** Maximum callers waiting before provider construction. Default 32. */
51
+ maxQueueSize?: number;
52
+ /** Maximum admission wait. Default 5s. */
53
+ queueTimeoutMs?: number;
32
54
  }
33
55
 
34
- const MAX_LOG_ENTRIES = 200;
35
- // Cap each entry AND the cumulative buffer at capture time so untrusted guest
36
- // code can't retain unbounded host memory: a single `console.log("x".repeat(N))`
37
- // otherwise copies the whole N-char guest string into a host array we hold for
38
- // the entire execution. 8k chars/entry is generous for glue-code logging (the
39
- // join in execute.ts trims the assembled log to 4k anyway), and 256k total
40
- // keeps the worst case — 200 maxed-out entries — bounded well under a MiB.
41
- const MAX_LOG_ENTRY_CHARS = 8_000;
42
- const MAX_LOG_TOTAL_CHARS = 256_000;
56
+ interface ActiveRun {
57
+ id: number;
58
+ providers: Map<string, ExecutorProvider>;
59
+ resolve: (outcome: ExecuteResult) => void;
60
+ reject: (error: Error) => void;
61
+ signal?: AbortSignal;
62
+ onAbort?: () => void;
63
+ wallTimer: ReturnType<typeof setTimeout>;
64
+ }
43
65
 
44
- function msg(err: unknown): string {
45
- return err instanceof Error ? err.message : String(err);
66
+ interface ChildSlot {
67
+ child?: ChildProcess;
68
+ ready?: Promise<void>;
69
+ resolveReady?: () => void;
70
+ rejectReady?: (error: Error) => void;
71
+ readyTimer?: ReturnType<typeof setTimeout>;
72
+ active?: ActiveRun;
73
+ expectedExit?: ChildProcess;
74
+ consecutiveCrashes: number;
75
+ notBefore: number;
46
76
  }
47
77
 
48
- /** Normalize model output into an async-arrow expression: strip markdown fences, wrap bare bodies. */
49
- export function normalizeCode(code: string): string {
50
- let c = code.trim();
51
- const fence = /^```[\w-]*\s*\n([\s\S]*?)\n?```$/.exec(c);
52
- if (fence) c = fence[1].trim();
53
- // Sniff for an existing function expression past any leading comments/blank
54
- // lines models routinely prefix their code with a `//` or `/* */` note,
55
- // which would otherwise defeat the detection and wrap the whole thing.
56
- const head = c.replace(/^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)+/, "");
57
- if (/^async\b/.test(head) || head.startsWith("(")) return c;
58
- return `async () => {\n${c}\n}`;
78
+ const DEFAULT_TIMEOUT_MS = 30_000;
79
+ const DEFAULT_CPU_TIME_MS = 250;
80
+ const DEFAULT_MEMORY_LIMIT_BYTES = 64 * 1024 * 1024;
81
+ const DEFAULT_STACK_LIMIT_BYTES = 1024 * 1024;
82
+ const DEFAULT_CONCURRENCY = 1;
83
+ const DEFAULT_MAX_QUEUE_SIZE = 32;
84
+ const DEFAULT_QUEUE_TIMEOUT_MS = 5_000;
85
+ const CHILD_EXIT_GRACE_MS = 250;
86
+ const CHILD_STARTUP_TIMEOUT_MS = 10_000;
87
+ const MAX_ERROR_CHARS = 4_000;
88
+
89
+ function msg(err: unknown): string {
90
+ return err instanceof Error ? err.message : String(err);
59
91
  }
60
92
 
61
- /** Guest-side prelude: console capture, the JSON call bridge, one frozen global per provider. */
62
- function setupScript(providers: ExecutorProvider[]): string {
63
- const lines: string[] = [
64
- `globalThis.console = (() => {
65
- const fmt = (x) => { if (typeof x === "string") return x; try { return JSON.stringify(x); } catch { return String(x); } };
66
- const emit = (...a) => __log(a.map(fmt).join(" "));
67
- return { log: emit, info: emit, warn: emit, error: emit, debug: emit };
68
- })();`,
69
- `globalThis.__invoke = async (ns, fn, args) => {
70
- const r = JSON.parse(await __call(ns, fn, JSON.stringify(args)));
71
- if (!r.ok) throw new Error(r.error);
72
- return r.value;
73
- };`,
74
- ];
75
- for (const p of providers) {
76
- const ns = JSON.stringify(p.name);
77
- const fields = Object.keys(p.fns).map((fn) => {
78
- const f = JSON.stringify(fn);
79
- // Forward every argument verbatim, positionally — same as codemode's
80
- // sandbox proxy (`(...args) => call(fn, args)`). The host wrappers in
81
- // buildSandboxProviders already guard the no-arg case, so no coercion.
82
- return ` [${f}]: (...a) => __invoke(${ns}, ${f}, a),`;
83
- });
84
- lines.push(`globalThis[${ns}] = Object.freeze({\n${fields.join("\n")}\n});`);
93
+ function positiveWhole(
94
+ value: number | undefined,
95
+ fallback: number,
96
+ name: string,
97
+ ): number {
98
+ const resolved = value ?? fallback;
99
+ if (
100
+ !Number.isFinite(resolved) ||
101
+ !Number.isInteger(resolved) ||
102
+ resolved < 1
103
+ ) {
104
+ throw new TypeError(`${name} must be a positive whole number.`);
85
105
  }
86
- return lines.join("\n");
106
+ return resolved;
87
107
  }
88
108
 
89
- /** True when a dumped error is QuickJS's deadline-interrupt signal. */
90
- function isInterrupt(dumped: unknown): boolean {
91
- if (!dumped || typeof dumped !== "object") return false;
92
- const e = dumped as { name?: unknown; message?: unknown };
93
- return e.name === "InternalError" && e.message === "interrupted";
109
+ function nonNegativeWhole(
110
+ value: number | undefined,
111
+ fallback: number,
112
+ name: string,
113
+ ): number {
114
+ const resolved = value ?? fallback;
115
+ if (
116
+ !Number.isFinite(resolved) ||
117
+ !Number.isInteger(resolved) ||
118
+ resolved < 0
119
+ ) {
120
+ throw new TypeError(`${name} must be a non-negative whole number.`);
121
+ }
122
+ return resolved;
94
123
  }
95
124
 
96
- function formatGuestError(dumped: unknown): string {
97
- if (dumped && typeof dumped === "object") {
98
- const e = dumped as { name?: unknown; message?: unknown };
99
- if (e.message != null) {
100
- return e.name != null && e.name !== "Error"
101
- ? `${String(e.name)}: ${String(e.message)}`
102
- : String(e.message);
103
- }
104
- }
105
- return typeof dumped === "string" ? dumped : JSON.stringify(dumped);
125
+ function errorPayload(error: string): string {
126
+ return JSON.stringify({
127
+ ok: false,
128
+ error: error.slice(0, MAX_ERROR_CHARS),
129
+ } satisfies HostResultPayload);
106
130
  }
107
131
 
108
- interface HostBridge {
109
- pending: number;
110
- aborted: boolean;
111
- wake: () => void;
112
- waitForSettle: Promise<void>;
132
+ function delay(ms: number, signal?: AbortSignal): Promise<void> {
133
+ if (ms <= 0) return Promise.resolve();
134
+ return new Promise((resolve, reject) => {
135
+ const timer = setTimeout(() => {
136
+ signal?.removeEventListener("abort", onAbort);
137
+ resolve();
138
+ }, ms);
139
+ const onAbort = () => {
140
+ clearTimeout(timer);
141
+ signal?.removeEventListener("abort", onAbort);
142
+ reject(
143
+ new ExecutorAdmissionError(
144
+ "executor_cancelled",
145
+ "Execution was cancelled while the sandbox was restarting.",
146
+ ),
147
+ );
148
+ };
149
+ signal?.addEventListener("abort", onAbort, { once: true });
150
+ if (signal?.aborted) onAbort();
151
+ });
113
152
  }
114
153
 
115
- function armWake(bridge: HostBridge): void {
116
- bridge.waitForSettle = new Promise((resolve) => {
117
- bridge.wake = resolve;
154
+ function waitForReady(
155
+ ready: Promise<void>,
156
+ signal?: AbortSignal,
157
+ ): Promise<void> {
158
+ if (!signal) return ready;
159
+ return new Promise((resolve, reject) => {
160
+ let settled = false;
161
+ const finish = (operation: () => void) => {
162
+ if (settled) return;
163
+ settled = true;
164
+ signal.removeEventListener("abort", onAbort);
165
+ operation();
166
+ };
167
+ const onAbort = () =>
168
+ finish(() =>
169
+ reject(
170
+ new ExecutorAdmissionError(
171
+ "executor_cancelled",
172
+ "Execution was cancelled while the sandbox was starting.",
173
+ ),
174
+ ),
175
+ );
176
+ signal.addEventListener("abort", onAbort, { once: true });
177
+ if (signal.aborted) {
178
+ onAbort();
179
+ return;
180
+ }
181
+ void ready.then(
182
+ () => finish(resolve),
183
+ (error: unknown) =>
184
+ finish(() =>
185
+ reject(error instanceof Error ? error : new Error(String(error))),
186
+ ),
187
+ );
118
188
  });
119
189
  }
120
190
 
121
- function installBridge(
122
- ctx: QuickJSContext,
123
- providers: ExecutorProvider[],
124
- logs: string[],
125
- ): HostBridge {
126
- const bridge: HostBridge = {
127
- pending: 0,
128
- aborted: false,
129
- wake: () => {},
130
- waitForSettle: Promise.resolve(),
131
- };
132
- armWake(bridge);
133
-
134
- // Running total of chars actually retained in `logs`; once the cumulative
135
- // budget is spent we push one marker and drop the rest, so a flood of large
136
- // entries can't grow the host array without bound.
137
- let logTotalChars = 0;
138
- let logBudgetSpent = false;
139
- const logFn = ctx.newFunction("__log", (h) => {
140
- if (logs.length >= MAX_LOG_ENTRIES) {
141
- if (logs.length === MAX_LOG_ENTRIES) {
142
- logs.push(`[log truncated after ${MAX_LOG_ENTRIES} entries]`);
191
+ class QuickJsChildPool implements AdmittingExecutor {
192
+ private readonly admission: AdmissionController;
193
+ private readonly slots: ChildSlot[];
194
+ private readonly available: ChildSlot[];
195
+ private readonly runtimeOptions: QuickJsRuntimeOptions;
196
+ private closed = false;
197
+ private nextJobId = 1;
198
+
199
+ constructor(options: QuickJsExecutorOptions) {
200
+ const concurrency = positiveWhole(
201
+ options.concurrency,
202
+ DEFAULT_CONCURRENCY,
203
+ "concurrency",
204
+ );
205
+ const queueTimeoutMs = positiveWhole(
206
+ options.queueTimeoutMs,
207
+ DEFAULT_QUEUE_TIMEOUT_MS,
208
+ "queueTimeoutMs",
209
+ );
210
+ this.admission = new AdmissionController({
211
+ concurrency,
212
+ maxQueueSize: nonNegativeWhole(
213
+ options.maxQueueSize,
214
+ DEFAULT_MAX_QUEUE_SIZE,
215
+ "maxQueueSize",
216
+ ),
217
+ queueTimeoutMs,
218
+ retryAfterMs: queueTimeoutMs,
219
+ });
220
+ this.runtimeOptions = {
221
+ timeoutMs: positiveWhole(
222
+ options.timeoutMs,
223
+ DEFAULT_TIMEOUT_MS,
224
+ "timeoutMs",
225
+ ),
226
+ cpuTimeMs: positiveWhole(
227
+ options.cpuTimeMs,
228
+ DEFAULT_CPU_TIME_MS,
229
+ "cpuTimeMs",
230
+ ),
231
+ memoryLimitBytes: positiveWhole(
232
+ options.memoryLimitBytes,
233
+ DEFAULT_MEMORY_LIMIT_BYTES,
234
+ "memoryLimitBytes",
235
+ ),
236
+ maxStackSizeBytes: positiveWhole(
237
+ options.maxStackSizeBytes,
238
+ DEFAULT_STACK_LIMIT_BYTES,
239
+ "maxStackSizeBytes",
240
+ ),
241
+ };
242
+ this.slots = Array.from({ length: concurrency }, () => ({
243
+ consecutiveCrashes: 0,
244
+ notBefore: 0,
245
+ }));
246
+ this.available = [...this.slots];
247
+ }
248
+
249
+ async acquire(options: { signal?: AbortSignal } = {}): Promise<ExecutorLease> {
250
+ const admission = await this.admission.acquire(options);
251
+ const slot = this.available.shift();
252
+ if (!slot) {
253
+ admission.release();
254
+ throw new Error("Executor admission invariant failed: no child slot.");
255
+ }
256
+ let released = false;
257
+ let executed = false;
258
+ return {
259
+ execute: async (code, providers) => {
260
+ if (released) throw new Error("Executor lease was already released.");
261
+ if (executed) throw new Error("Executor lease may execute only once.");
262
+ executed = true;
263
+ return this.run(slot, code, providers, options.signal);
264
+ },
265
+ release: () => {
266
+ if (released) return;
267
+ released = true;
268
+ if (slot.active) {
269
+ this.rejectActive(
270
+ slot,
271
+ new Error("Executor lease was released during execution."),
272
+ );
273
+ this.recycle(slot);
274
+ }
275
+ this.available.push(slot);
276
+ admission.release();
277
+ },
278
+ };
279
+ }
280
+
281
+ async execute(
282
+ code: string,
283
+ providers: ExecutorProvider[],
284
+ ): Promise<ExecuteResult> {
285
+ const lease = await this.acquire();
286
+ try {
287
+ return await lease.execute(code, providers);
288
+ } finally {
289
+ lease.release();
290
+ }
291
+ }
292
+
293
+ async close(): Promise<void> {
294
+ if (this.closed) return;
295
+ this.closed = true;
296
+ this.admission.close();
297
+ const exits = this.slots.map((slot) => this.stopSlot(slot));
298
+ await Promise.allSettled(exits);
299
+ }
300
+
301
+ private async run(
302
+ slot: ChildSlot,
303
+ code: string,
304
+ providers: ExecutorProvider[],
305
+ signal?: AbortSignal,
306
+ ): Promise<ExecuteResult> {
307
+ if (signal?.aborted) {
308
+ throw new ExecutorAdmissionError(
309
+ "executor_cancelled",
310
+ "Execution was cancelled before it started.",
311
+ );
312
+ }
313
+ await this.ensureChild(slot, signal);
314
+ if (signal?.aborted) {
315
+ throw new ExecutorAdmissionError(
316
+ "executor_cancelled",
317
+ "Execution was cancelled before it started.",
318
+ );
319
+ }
320
+ const child = slot.child;
321
+ if (!child?.connected) {
322
+ throw new Error("QuickJS child IPC channel is unavailable.");
323
+ }
324
+
325
+ const id = this.nextJobId++;
326
+ const providerMap = new Map(providers.map((item) => [item.name, item]));
327
+ let payloadJson: string;
328
+ let runMessage: ParentToChildMessage;
329
+ try {
330
+ payloadJson = stringifyBounded(
331
+ {
332
+ id,
333
+ code,
334
+ providerNames: providers.map((item) => item.name),
335
+ options: this.runtimeOptions,
336
+ } satisfies RunPayload,
337
+ "QuickJS run payload",
338
+ );
339
+ runMessage = { type: "run", payloadJson };
340
+ stringifyBounded(runMessage, "QuickJS run IPC envelope");
341
+ } catch (err) {
342
+ return { result: undefined, error: msg(err) };
343
+ }
344
+
345
+ child.ref();
346
+ child.channel?.ref();
347
+ return new Promise<ExecuteResult>((resolve, reject) => {
348
+ const wallTimer = setTimeout(() => {
349
+ this.resolveActive(
350
+ slot,
351
+ {
352
+ result: undefined,
353
+ error: `QuickJS child exceeded the ${this.runtimeOptions.timeoutMs}ms wall budget and was terminated.`,
354
+ },
355
+ );
356
+ this.recycle(slot);
357
+ }, this.runtimeOptions.timeoutMs + CHILD_EXIT_GRACE_MS);
358
+ const active: ActiveRun = {
359
+ id,
360
+ providers: providerMap,
361
+ resolve,
362
+ reject,
363
+ ...(signal ? { signal } : {}),
364
+ wallTimer,
365
+ };
366
+ active.onAbort = () => {
367
+ this.rejectActive(
368
+ slot,
369
+ new ExecutorAdmissionError(
370
+ "executor_cancelled",
371
+ "Execution was cancelled.",
372
+ ),
373
+ );
374
+ this.recycle(slot);
375
+ };
376
+ signal?.addEventListener("abort", active.onAbort, { once: true });
377
+ slot.active = active;
378
+ if (signal?.aborted) {
379
+ active.onAbort();
380
+ return;
381
+ }
382
+ try {
383
+ child.send(
384
+ runMessage,
385
+ (error) => {
386
+ if (!error || slot.active?.id !== id) return;
387
+ this.rejectActive(slot, error);
388
+ this.recycle(slot);
389
+ },
390
+ );
391
+ } catch (err) {
392
+ this.rejectActive(
393
+ slot,
394
+ err instanceof Error ? err : new Error(String(err)),
395
+ );
396
+ this.recycle(slot);
397
+ }
398
+ });
399
+ }
400
+
401
+ private async ensureChild(
402
+ slot: ChildSlot,
403
+ signal?: AbortSignal,
404
+ ): Promise<void> {
405
+ if (this.closed) {
406
+ throw new ExecutorAdmissionError(
407
+ "executor_closed",
408
+ "Executor is shutting down.",
409
+ );
410
+ }
411
+ if (slot.child?.connected) {
412
+ if (slot.ready) await waitForReady(slot.ready, signal);
413
+ return;
414
+ }
415
+ await delay(Math.max(0, slot.notBefore - Date.now()), signal);
416
+ if (this.closed) {
417
+ throw new ExecutorAdmissionError(
418
+ "executor_closed",
419
+ "Executor is shutting down.",
420
+ );
421
+ }
422
+
423
+ const sourceMode = import.meta.url.endsWith(".ts");
424
+ const childUrl = new URL(
425
+ sourceMode ? "./quickjs-child.ts" : "./quickjs-child.js",
426
+ import.meta.url,
427
+ );
428
+ const child = fork(fileURLToPath(childUrl), [], {
429
+ execArgv: sourceMode ? ["--import", "tsx"] : [],
430
+ stdio: ["ignore", "ignore", "ignore", "ipc"],
431
+ });
432
+ slot.child = child;
433
+ slot.ready = new Promise<void>((resolve, reject) => {
434
+ slot.resolveReady = resolve;
435
+ slot.rejectReady = reject;
436
+ });
437
+ slot.readyTimer = setTimeout(() => {
438
+ if (slot.child !== child || !slot.ready) return;
439
+ const error = new Error(
440
+ `QuickJS child did not become ready within ${CHILD_STARTUP_TIMEOUT_MS}ms.`,
441
+ );
442
+ this.failChildStartup(slot, child, error);
443
+ }, CHILD_STARTUP_TIMEOUT_MS);
444
+ child.on("message", (message: ChildToParentMessage) => {
445
+ void this.onMessage(slot, child, message);
446
+ });
447
+ child.on("error", (error) => {
448
+ if (slot.child !== child) return;
449
+ if (slot.ready) {
450
+ this.failChildStartup(slot, child, error);
451
+ return;
143
452
  }
453
+ this.rejectActive(slot, error);
454
+ this.recycle(slot);
455
+ });
456
+ child.on("exit", (code, exitSignal) => {
457
+ const expected = slot.expectedExit === child;
458
+ if (slot.expectedExit === child) slot.expectedExit = undefined;
459
+ this.rejectChildReady(
460
+ slot,
461
+ child,
462
+ new Error(
463
+ `QuickJS child exited before becoming ready` +
464
+ ` (${exitSignal ? `signal ${exitSignal}` : `code ${String(code)}`}).`,
465
+ ),
466
+ );
467
+ if (slot.child === child) slot.child = undefined;
468
+ if (expected) return;
469
+ this.recordCrash(slot);
470
+ this.resolveActive(slot, {
471
+ result: undefined,
472
+ error:
473
+ `QuickJS child exited unexpectedly` +
474
+ ` (${exitSignal ? `signal ${exitSignal}` : `code ${String(code)}`}).`,
475
+ });
476
+ });
477
+ child.unref();
478
+ child.channel?.unref();
479
+ if (slot.ready) await waitForReady(slot.ready, signal);
480
+ }
481
+
482
+ private async onMessage(
483
+ slot: ChildSlot,
484
+ child: ChildProcess,
485
+ message: ChildToParentMessage,
486
+ ): Promise<void> {
487
+ // A compromised child is exactly the adversary this process boundary
488
+ // contains, and this handler's rejection would crash the serving process.
489
+ // Refuse malformed intake before touching any field.
490
+ if (!message || typeof message !== "object") return;
491
+ if (message.type === "ready") {
492
+ this.resolveChildReady(slot, child);
144
493
  return;
145
494
  }
146
- if (logBudgetSpent) return;
147
- // Cap the entry before retaining it: `getString` yields a transient copy,
148
- // but slicing here keeps only a bounded string alive in `logs`.
149
- let entry = ctx.getString(h);
150
- if (entry.length > MAX_LOG_ENTRY_CHARS) {
151
- entry = `${entry.slice(0, MAX_LOG_ENTRY_CHARS)}…[entry truncated]`;
495
+ const active = slot.active;
496
+ if (!active || slot.child !== child) return;
497
+ if (typeof message.payloadJson !== "string") return;
498
+ if (message.type === "host-call") {
499
+ if (message.jobId !== active.id) return;
500
+ await this.handleHostCall(child, active, message);
501
+ return;
152
502
  }
153
- if (logTotalChars + entry.length > MAX_LOG_TOTAL_CHARS) {
154
- logs.push("[log truncated: size budget exceeded]");
155
- logBudgetSpent = true;
503
+ if (message.type !== "result" || message.jobId !== active.id) return;
504
+ if (serializedBytes(message.payloadJson) > MAX_QUICKJS_IPC_BYTES) {
505
+ this.resolveActive(slot, {
506
+ result: undefined,
507
+ error: "QuickJS execution result exceeded the IPC limit.",
508
+ });
509
+ this.recycle(slot);
156
510
  return;
157
511
  }
158
- logs.push(entry);
159
- logTotalChars += entry.length;
160
- });
161
- ctx.setProp(ctx.global, "__log", logFn);
162
- logFn.dispose();
163
-
164
- const byName = new Map(providers.map((p) => [p.name, p]));
165
- const invoke = async (
166
- ns: string,
167
- fn: string,
168
- argsJson: string,
169
- ): Promise<string> => {
170
512
  try {
171
- const provider = byName.get(ns);
172
- const f =
173
- provider && Object.hasOwn(provider.fns, fn)
174
- ? provider.fns[fn]
175
- : undefined;
176
- if (!f) throw new Error(`Unknown function ${ns}.${fn}`);
177
- const args = JSON.parse(argsJson) as unknown[];
178
- const value = await f(...args);
179
- return JSON.stringify({ ok: true, value });
513
+ const payload = JSON.parse(message.payloadJson) as ExecutionPayload;
514
+ slot.consecutiveCrashes = 0;
515
+ slot.notBefore = 0;
516
+ this.resolveActive(slot, payload.outcome);
517
+ if (payload.timedOut) {
518
+ // A timed-out host call may still retain QuickJS handles. A fresh child
519
+ // is cheaper and safer than reusing a context with unknown stragglers.
520
+ // The runtime sets this flag itself: matching error text here would let
521
+ // a guest throw "Execution timed out…" and force cold-start churn.
522
+ this.recycle(slot);
523
+ }
180
524
  } catch (err) {
181
- return JSON.stringify({ ok: false, error: msg(err) });
525
+ this.resolveActive(slot, {
526
+ result: undefined,
527
+ error: `QuickJS child returned an invalid result: ${msg(err)}`,
528
+ });
529
+ this.recycle(slot);
182
530
  }
183
- };
184
-
185
- const callFn = ctx.newFunction("__call", (nsH, fnH, argsH) => {
186
- const ns = ctx.getString(nsH);
187
- const fn = ctx.getString(fnH);
188
- const argsJson = ctx.getString(argsH);
189
- const deferred: QuickJSDeferredPromise = ctx.newPromise();
190
- bridge.pending++;
191
- void invoke(ns, fn, argsJson).then((json) => {
192
- bridge.pending--;
193
- if (bridge.aborted) {
194
- // Context is (about to be) torn down — settle nothing, free our handles.
195
- deferred.dispose();
196
- } else {
197
- const h = ctx.newString(json);
198
- deferred.resolve(h);
199
- h.dispose();
531
+ }
532
+
533
+ private async handleHostCall(
534
+ child: ChildProcess,
535
+ active: ActiveRun,
536
+ message: Extract<ChildToParentMessage, { type: "host-call" }>,
537
+ ): Promise<void> {
538
+ let payloadJson: string;
539
+ try {
540
+ if (
541
+ serializedBytes(message.payloadJson) >
542
+ MAX_QUICKJS_HOST_RPC_BYTES
543
+ ) {
544
+ throw new RangeError("Host call arguments exceeded the IPC limit.");
200
545
  }
201
- bridge.wake();
202
- });
203
- return deferred.handle;
204
- });
205
- ctx.setProp(ctx.global, "__call", callFn);
206
- callFn.dispose();
546
+ const payload = JSON.parse(message.payloadJson) as HostCallPayload;
547
+ const provider = active.providers.get(payload.namespace);
548
+ const fn =
549
+ provider && Object.hasOwn(provider.fns, payload.functionName)
550
+ ? provider.fns[payload.functionName]
551
+ : undefined;
552
+ if (!fn) {
553
+ throw new Error(
554
+ `Unknown function ${payload.namespace}.${payload.functionName}`,
555
+ );
556
+ }
557
+ const value = await fn(...payload.args);
558
+ try {
559
+ payloadJson = stringifyBounded(
560
+ { ok: true, value } satisfies HostResultPayload,
561
+ `Host result from ${payload.namespace}.${payload.functionName}`,
562
+ MAX_QUICKJS_HOST_RPC_BYTES,
563
+ );
564
+ } catch (err) {
565
+ const detail =
566
+ err instanceof RangeError
567
+ ? `Host result from ${payload.namespace}.${payload.functionName} exceeds the ${MAX_QUICKJS_HOST_RPC_BYTES}-byte serialized bridge limit.`
568
+ : `Host result from ${payload.namespace}.${payload.functionName} could not be serialized: ${msg(err)}`;
569
+ payloadJson = errorPayload(detail);
570
+ }
571
+ } catch (err) {
572
+ payloadJson = errorPayload(msg(err));
573
+ }
574
+ if (!child.connected) return;
575
+ try {
576
+ const response = {
577
+ type: "host-result",
578
+ jobId: message.jobId,
579
+ callId: message.callId,
580
+ payloadJson,
581
+ } satisfies ParentToChildMessage;
582
+ stringifyBounded(response, "QuickJS host-result IPC envelope");
583
+ child.send(response);
584
+ } catch {
585
+ // The execution has already ended or the child is exiting. Its exit
586
+ // handler owns the structured failure for any still-active job.
587
+ }
588
+ }
589
+
590
+ private resolveActive(slot: ChildSlot, outcome: ExecuteResult): void {
591
+ const active = slot.active;
592
+ if (!active) return;
593
+ this.clearActive(slot, active);
594
+ active.resolve(outcome);
595
+ }
596
+
597
+ private rejectActive(slot: ChildSlot, error: Error): void {
598
+ const active = slot.active;
599
+ if (!active) return;
600
+ this.clearActive(slot, active);
601
+ active.reject(error);
602
+ }
603
+
604
+ private clearActive(slot: ChildSlot, active: ActiveRun): void {
605
+ clearTimeout(active.wallTimer);
606
+ if (active.onAbort) {
607
+ active.signal?.removeEventListener("abort", active.onAbort);
608
+ }
609
+ slot.active = undefined;
610
+ slot.child?.unref();
611
+ slot.child?.channel?.unref();
612
+ }
613
+
614
+ private resolveChildReady(slot: ChildSlot, child: ChildProcess): void {
615
+ if (slot.child !== child || !slot.ready) return;
616
+ if (slot.readyTimer) clearTimeout(slot.readyTimer);
617
+ const resolve = slot.resolveReady;
618
+ slot.ready = undefined;
619
+ slot.resolveReady = undefined;
620
+ slot.rejectReady = undefined;
621
+ slot.readyTimer = undefined;
622
+ resolve?.();
623
+ }
207
624
 
208
- return bridge;
625
+ private rejectChildReady(
626
+ slot: ChildSlot,
627
+ child: ChildProcess,
628
+ error: Error,
629
+ ): void {
630
+ if (slot.child !== child || !slot.ready) return;
631
+ if (slot.readyTimer) clearTimeout(slot.readyTimer);
632
+ const reject = slot.rejectReady;
633
+ slot.ready = undefined;
634
+ slot.resolveReady = undefined;
635
+ slot.rejectReady = undefined;
636
+ slot.readyTimer = undefined;
637
+ reject?.(error);
638
+ }
639
+
640
+ private recordCrash(slot: ChildSlot): void {
641
+ slot.consecutiveCrashes++;
642
+ slot.notBefore =
643
+ Date.now() +
644
+ Math.min(5_000, 100 * 2 ** (slot.consecutiveCrashes - 1));
645
+ }
646
+
647
+ private failChildStartup(
648
+ slot: ChildSlot,
649
+ child: ChildProcess,
650
+ error: Error,
651
+ ): void {
652
+ if (slot.child !== child || !slot.ready) return;
653
+ // Detach before the rejected readiness promise can release the lease. A
654
+ // queued successor must never observe a connected child that is unready
655
+ // and already on its way out.
656
+ slot.expectedExit = child;
657
+ this.rejectChildReady(slot, child, error);
658
+ slot.child = undefined;
659
+ this.recordCrash(slot);
660
+ child.kill();
661
+ }
662
+
663
+ private recycle(slot: ChildSlot): void {
664
+ const child = slot.child;
665
+ if (!child) return;
666
+ this.rejectChildReady(
667
+ slot,
668
+ child,
669
+ new Error("QuickJS child was recycled before becoming ready."),
670
+ );
671
+ slot.expectedExit = child;
672
+ slot.child = undefined;
673
+ child.kill();
674
+ }
675
+
676
+ private stopSlot(slot: ChildSlot): Promise<void> {
677
+ const child = slot.child;
678
+ if (!child) return Promise.resolve();
679
+ this.rejectActive(
680
+ slot,
681
+ new ExecutorAdmissionError(
682
+ "executor_closed",
683
+ "Executor is shutting down.",
684
+ ),
685
+ );
686
+ this.rejectChildReady(
687
+ slot,
688
+ child,
689
+ new ExecutorAdmissionError(
690
+ "executor_closed",
691
+ "Executor is shutting down.",
692
+ ),
693
+ );
694
+ slot.expectedExit = child;
695
+ slot.child = undefined;
696
+ return new Promise((resolve) => {
697
+ const timer = setTimeout(resolve, 1_000);
698
+ child.once("exit", () => {
699
+ clearTimeout(timer);
700
+ resolve();
701
+ });
702
+ child.kill();
703
+ });
704
+ }
209
705
  }
210
706
 
211
707
  /**
212
- * Sandbox executor for Node (or any JS runtime) built on quickjs-emscripten.
213
- * Suits connecta's execute_code: code is tool-call glue, so an interpreter's
214
- * speed is irrelevant while its isolation (plain WASM, zero native deps) is
215
- * the point. One fresh QuickJS context per execution; the WASM module itself
216
- * is cached across calls by quickjs-emscripten.
708
+ * Bounded Node QuickJS executor. Admission is acquired before connecta builds
709
+ * providers; one lease maps to one child and carries execution, avoiding a
710
+ * double-acquire deadlock at concurrency 1.
217
711
  */
218
712
  export function quickJsExecutor(
219
713
  options: QuickJsExecutorOptions = {},
220
- ): Executor {
221
- const timeoutMs = options.timeoutMs ?? 30_000;
222
- const memoryLimitBytes = options.memoryLimitBytes ?? 64 * 1024 * 1024;
223
- const maxStackSizeBytes = options.maxStackSizeBytes ?? 1024 * 1024;
224
-
225
- return {
226
- async execute(
227
- code: string,
228
- providers: ExecutorProvider[],
229
- ): Promise<ExecuteResult> {
230
- const QuickJS = await getQuickJS();
231
- const ctx = QuickJS.newContext();
232
- const deadline = Date.now() + timeoutMs;
233
- const timeoutError = `Execution timed out after ${timeoutMs}ms.`;
234
- ctx.runtime.setMemoryLimit(memoryLimitBytes);
235
- ctx.runtime.setMaxStackSize(maxStackSizeBytes);
236
- ctx.runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadline));
237
-
238
- const logs: string[] = [];
239
- const bridge = installBridge(ctx, providers, logs);
240
- const finish = (r: ExecuteResult): ExecuteResult => {
241
- bridge.aborted = true;
242
- // Outstanding host calls still hold deferred-promise handles; their
243
- // callbacks free them and wake the drain, which disposes the context
244
- // once the last straggler settles. Re-arm before every wait so the
245
- // deferred never resolves-and-stays-resolved between settles — that
246
- // would spin the microtask queue and starve the very timers the
247
- // stragglers are waiting on. Arming before the pending check (and
248
- // synchronously, before the first await) closes the settle-before-arm
249
- // window: any settle after this point resolves the promise we await.
250
- if (bridge.pending === 0) ctx.dispose();
251
- else {
252
- void (async () => {
253
- armWake(bridge);
254
- while (bridge.pending > 0) {
255
- await bridge.waitForSettle;
256
- armWake(bridge);
257
- }
258
- ctx.dispose();
259
- })();
260
- }
261
- return logs.length > 0 ? { ...r, logs } : r;
262
- };
263
- const fail = (error: string): ExecuteResult =>
264
- finish({ result: undefined, error });
265
-
266
- const setup = ctx.evalCode(setupScript(providers));
267
- if (setup.error) {
268
- const detail = formatGuestError(ctx.dump(setup.error));
269
- setup.error.dispose();
270
- return fail(`Sandbox setup failed: ${detail}`);
271
- }
272
- setup.value.dispose();
273
-
274
- const evaluated = ctx.evalCode(
275
- `Promise.resolve((\n${normalizeCode(code)}\n)())`,
276
- );
277
- if (evaluated.error) {
278
- const dumped = ctx.dump(evaluated.error);
279
- evaluated.error.dispose();
280
- // A synchronous runaway (no await to yield on) trips the deadline
281
- // interrupt here; surface the friendly timeout, not "interrupted".
282
- if (isInterrupt(dumped) && Date.now() >= deadline) return fail(timeoutError);
283
- return fail(formatGuestError(dumped));
284
- }
285
- const promiseHandle = evaluated.value;
286
-
287
- // Drive the guest: run microtask jobs, inspect the result promise,
288
- // sleep until a host call settles (or the budget runs out), repeat.
289
- // Returns without touching the context lifecycle; disposal happens below.
290
- const drive = async (): Promise<ExecuteResult> => {
291
- for (;;) {
292
- const jobs = ctx.runtime.executePendingJobs();
293
- if (jobs.error) {
294
- const dumped = ctx.dump(jobs.error);
295
- jobs.error.dispose();
296
- // A runaway inside a resumed continuation trips the interrupt here.
297
- if (isInterrupt(dumped) && Date.now() >= deadline) {
298
- return { result: undefined, error: timeoutError };
299
- }
300
- return { result: undefined, error: formatGuestError(dumped) };
301
- }
302
- const state = ctx.getPromiseState(promiseHandle);
303
- if (state.type === "fulfilled") {
304
- const result: unknown = ctx.dump(state.value);
305
- state.value.dispose();
306
- return { result };
307
- }
308
- if (state.type === "rejected") {
309
- const dumped = ctx.dump(state.error);
310
- state.error.dispose();
311
- // A synchronous runaway rejects the async fn's promise with the
312
- // interrupt; surface the friendly timeout, not "interrupted".
313
- if (isInterrupt(dumped) && Date.now() >= deadline) {
314
- return { result: undefined, error: timeoutError };
315
- }
316
- return { result: undefined, error: formatGuestError(dumped) };
317
- }
318
- if (bridge.pending === 0) {
319
- return {
320
- result: undefined,
321
- error:
322
- "Execution stalled: code awaits something that can never settle.",
323
- };
324
- }
325
- const remaining = deadline - Date.now();
326
- if (remaining <= 0) {
327
- return { result: undefined, error: timeoutError };
328
- }
329
- const settled = await Promise.race([
330
- bridge.waitForSettle.then(() => true),
331
- new Promise<boolean>((r) => setTimeout(() => r(false), remaining)),
332
- ]);
333
- if (settled) armWake(bridge);
334
- else {
335
- return { result: undefined, error: timeoutError };
336
- }
337
- }
338
- };
339
-
340
- let outcome: ExecuteResult;
341
- try {
342
- outcome = await drive();
343
- } finally {
344
- promiseHandle.dispose();
345
- }
346
- return finish(outcome);
347
- },
348
- };
714
+ ): AdmittingExecutor {
715
+ return new QuickJsChildPool(options);
349
716
  }