@jini-ai/sandbox 0.6.17 → 0.7.0
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.
- package/CHANGELOG.md +3 -1
- package/dist/cjs/node-worker.d.ts +21 -1
- package/dist/cjs/node-worker.js +54 -4
- package/dist/node-worker.d.ts +21 -1
- package/dist/node-worker.js +51 -4
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @jini-ai/sandbox
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
3
5
|
## 0.6.17
|
|
4
6
|
|
|
5
7
|
### Patch Changes
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
- Published sandbox helpers use required/optional argument objects and injected host policies; process migration documents are archived.
|
|
32
34
|
- Distribution includes runtime output, release documentation and required assets only. Process records and per-job neutrality checks are no longer part of the package surface.
|
|
33
35
|
|
|
34
|
-
##
|
|
36
|
+
## 0.6.19
|
|
35
37
|
|
|
36
38
|
- Fix E2B command adapter overload compatibility by declaring the implementation's port result union; runtime behavior and overload contracts are unchanged.
|
|
37
39
|
- **BREAKING:** Error constructors, E2B helpers, sandbox sessions/providers and worker event ports now take required and optional argument objects. E2B output callbacks receive `{ data }`; Node and SDK arguments are translated inside adapters. See `API.md` for migration.
|
|
@@ -40,6 +40,24 @@ export interface WorkerScheduler {
|
|
|
40
40
|
run: () => void;
|
|
41
41
|
}): () => void;
|
|
42
42
|
}
|
|
43
|
+
/** Shared admission policy. A lease is held until the worker exits or termination completes. */
|
|
44
|
+
export interface WorkerCapacity {
|
|
45
|
+
acquire(input: Record<string, never>, options?: Record<string, never>): () => void;
|
|
46
|
+
}
|
|
47
|
+
/** Capacity exhaustion is a controlled refusal, allowing hosts to render their fallback. */
|
|
48
|
+
export declare class WorkerCapacityError extends Error {
|
|
49
|
+
constructor();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Bound active disposable workers without queuing caller payloads or keeping idle workers alive.
|
|
53
|
+
* @param required Positive maximum number of active workers shared by callers.
|
|
54
|
+
* @returns Admission port with idempotent lease release; acquire throws WorkerCapacityError at the cap.
|
|
55
|
+
* @throws RangeError for an invalid maximum.
|
|
56
|
+
* @complexity O(1) time and state per acquisition/release; no waiting queue.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createWorkerCapacity({ maxConcurrent }: {
|
|
59
|
+
maxConcurrent: number;
|
|
60
|
+
}, _options?: {}): WorkerCapacity;
|
|
43
61
|
export interface WorkerSandboxRequired<TPayload> {
|
|
44
62
|
workerEntry: string | URL;
|
|
45
63
|
input: TPayload;
|
|
@@ -48,6 +66,8 @@ export interface WorkerSandboxRequired<TPayload> {
|
|
|
48
66
|
defaultResourceLimits: ResourceLimits;
|
|
49
67
|
workerFactory: WorkerFactory;
|
|
50
68
|
scheduler: WorkerScheduler;
|
|
69
|
+
/** Host-owned shared limit; omitted for hosts retaining their existing admission policy. */
|
|
70
|
+
capacity?: WorkerCapacity;
|
|
51
71
|
}
|
|
52
72
|
export interface WorkerSandboxOptions {
|
|
53
73
|
timeoutMs?: number;
|
|
@@ -75,7 +95,7 @@ export declare function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, ma
|
|
|
75
95
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
76
96
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
77
97
|
* @param options Per-invocation timeout and resource limits.
|
|
78
|
-
* @returns Decoded first reply; rejects on timeout, error, exit-without-reply or codec failure.
|
|
98
|
+
* @returns Decoded first reply; rejects on capacity exhaustion, timeout, error, exit-without-reply or codec failure.
|
|
79
99
|
* @throws RangeError (as a rejected promise) for an invalid timeout.
|
|
80
100
|
* @complexity O(1) harness work, excluding structured clone, worker execution and decoding.
|
|
81
101
|
*/
|
package/dist/cjs/node-worker.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerCapacityError = void 0;
|
|
4
|
+
exports.createWorkerCapacity = createWorkerCapacity;
|
|
3
5
|
exports.resolveDefaultTimeoutMs = resolveDefaultTimeoutMs;
|
|
4
6
|
exports.runInWorkerSandbox = runInWorkerSandbox;
|
|
5
7
|
exports.renderInWorkerSandbox = renderInWorkerSandbox;
|
|
@@ -13,6 +15,40 @@ exports.createNodeWorkerScheduler = createNodeWorkerScheduler;
|
|
|
13
15
|
*/
|
|
14
16
|
const node_worker_threads_1 = require("node:worker_threads");
|
|
15
17
|
const node_path_1 = require("node:path");
|
|
18
|
+
/** Capacity exhaustion is a controlled refusal, allowing hosts to render their fallback. */
|
|
19
|
+
class WorkerCapacityError extends Error {
|
|
20
|
+
constructor() {
|
|
21
|
+
super("Worker capacity is busy; retry shortly");
|
|
22
|
+
this.name = "WorkerCapacityError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.WorkerCapacityError = WorkerCapacityError;
|
|
26
|
+
/**
|
|
27
|
+
* Bound active disposable workers without queuing caller payloads or keeping idle workers alive.
|
|
28
|
+
* @param required Positive maximum number of active workers shared by callers.
|
|
29
|
+
* @returns Admission port with idempotent lease release; acquire throws WorkerCapacityError at the cap.
|
|
30
|
+
* @throws RangeError for an invalid maximum.
|
|
31
|
+
* @complexity O(1) time and state per acquisition/release; no waiting queue.
|
|
32
|
+
*/
|
|
33
|
+
function createWorkerCapacity({ maxConcurrent }, _options = {}) {
|
|
34
|
+
if (!Number.isSafeInteger(maxConcurrent) || maxConcurrent < 1)
|
|
35
|
+
throw new RangeError("maxConcurrent must be a positive safe integer");
|
|
36
|
+
let active = 0;
|
|
37
|
+
return {
|
|
38
|
+
acquire() {
|
|
39
|
+
if (active >= maxConcurrent)
|
|
40
|
+
throw new WorkerCapacityError();
|
|
41
|
+
active++;
|
|
42
|
+
let released = false;
|
|
43
|
+
return () => {
|
|
44
|
+
if (released)
|
|
45
|
+
return;
|
|
46
|
+
released = true;
|
|
47
|
+
active--;
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
16
52
|
/**
|
|
17
53
|
* Strictly parse a host-supplied timeout value, falling back on malformed or out-of-range values.
|
|
18
54
|
* @param required Raw configuration and explicit default/maximum budgets in milliseconds.
|
|
@@ -33,7 +69,7 @@ function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeoutMs }) {
|
|
|
33
69
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
34
70
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
35
71
|
* @param options Per-invocation timeout and resource limits.
|
|
36
|
-
* @returns Decoded first reply; rejects on timeout, error, exit-without-reply or codec failure.
|
|
72
|
+
* @returns Decoded first reply; rejects on capacity exhaustion, timeout, error, exit-without-reply or codec failure.
|
|
37
73
|
* @throws RangeError (as a rejected promise) for an invalid timeout.
|
|
38
74
|
* @complexity O(1) harness work, excluding structured clone, worker execution and decoding.
|
|
39
75
|
*/
|
|
@@ -43,13 +79,24 @@ function runInWorkerSandbox(required, options = {}) {
|
|
|
43
79
|
return Promise.reject(new RangeError("timeoutMs must be a positive integer within the Node timer range"));
|
|
44
80
|
}
|
|
45
81
|
return new Promise((resolve, reject) => {
|
|
46
|
-
const
|
|
82
|
+
const release = required.capacity?.acquire({});
|
|
83
|
+
let worker;
|
|
84
|
+
try {
|
|
85
|
+
worker = required.workerFactory.spawn({ workerEntry: required.workerEntry, payload: required.input, resourceLimits: options.resourceLimits ?? required.defaultResourceLimits });
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
release?.();
|
|
89
|
+
reject(error);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
47
92
|
let settled = false;
|
|
48
93
|
let cancelTimer;
|
|
49
94
|
function terminate() {
|
|
50
95
|
// Teardown must never create a second rejection after the result has already settled.
|
|
96
|
+
// A reply/timeout is not proof the native worker is gone. Retain its slot until teardown
|
|
97
|
+
// completes or an exit arrives; failed termination must not admit more live workers.
|
|
51
98
|
try {
|
|
52
|
-
void worker.terminate().
|
|
99
|
+
void worker.terminate().then(release, () => { });
|
|
53
100
|
}
|
|
54
101
|
catch { /* best effort */ }
|
|
55
102
|
}
|
|
@@ -69,7 +116,10 @@ function runInWorkerSandbox(required, options = {}) {
|
|
|
69
116
|
try {
|
|
70
117
|
worker.once({ event: "message", listener: ({ message }) => finish(() => resolve(required.decodeResult({ message }))) });
|
|
71
118
|
worker.once({ event: "error", listener: ({ error }) => finish(() => reject(error)) });
|
|
72
|
-
worker.once({ event: "exit", listener: ({ code }) =>
|
|
119
|
+
worker.once({ event: "exit", listener: ({ code }) => {
|
|
120
|
+
release?.();
|
|
121
|
+
finish(() => reject(new Error(`${required.errorLabel} worker exited with code ${code}`)));
|
|
122
|
+
} });
|
|
73
123
|
cancelTimer = required.scheduler.schedule({ delayMs: timeoutMs, run: () => finish(() => reject(new Error(`${required.errorLabel} exceeded ${timeoutMs}ms timeout`))) });
|
|
74
124
|
}
|
|
75
125
|
catch (error) {
|
package/dist/node-worker.d.ts
CHANGED
|
@@ -40,6 +40,24 @@ export interface WorkerScheduler {
|
|
|
40
40
|
run: () => void;
|
|
41
41
|
}): () => void;
|
|
42
42
|
}
|
|
43
|
+
/** Shared admission policy. A lease is held until the worker exits or termination completes. */
|
|
44
|
+
export interface WorkerCapacity {
|
|
45
|
+
acquire(input: Record<string, never>, options?: Record<string, never>): () => void;
|
|
46
|
+
}
|
|
47
|
+
/** Capacity exhaustion is a controlled refusal, allowing hosts to render their fallback. */
|
|
48
|
+
export declare class WorkerCapacityError extends Error {
|
|
49
|
+
constructor();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Bound active disposable workers without queuing caller payloads or keeping idle workers alive.
|
|
53
|
+
* @param required Positive maximum number of active workers shared by callers.
|
|
54
|
+
* @returns Admission port with idempotent lease release; acquire throws WorkerCapacityError at the cap.
|
|
55
|
+
* @throws RangeError for an invalid maximum.
|
|
56
|
+
* @complexity O(1) time and state per acquisition/release; no waiting queue.
|
|
57
|
+
*/
|
|
58
|
+
export declare function createWorkerCapacity({ maxConcurrent }: {
|
|
59
|
+
maxConcurrent: number;
|
|
60
|
+
}, _options?: {}): WorkerCapacity;
|
|
43
61
|
export interface WorkerSandboxRequired<TPayload> {
|
|
44
62
|
workerEntry: string | URL;
|
|
45
63
|
input: TPayload;
|
|
@@ -48,6 +66,8 @@ export interface WorkerSandboxRequired<TPayload> {
|
|
|
48
66
|
defaultResourceLimits: ResourceLimits;
|
|
49
67
|
workerFactory: WorkerFactory;
|
|
50
68
|
scheduler: WorkerScheduler;
|
|
69
|
+
/** Host-owned shared limit; omitted for hosts retaining their existing admission policy. */
|
|
70
|
+
capacity?: WorkerCapacity;
|
|
51
71
|
}
|
|
52
72
|
export interface WorkerSandboxOptions {
|
|
53
73
|
timeoutMs?: number;
|
|
@@ -75,7 +95,7 @@ export declare function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, ma
|
|
|
75
95
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
76
96
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
77
97
|
* @param options Per-invocation timeout and resource limits.
|
|
78
|
-
* @returns Decoded first reply; rejects on timeout, error, exit-without-reply or codec failure.
|
|
98
|
+
* @returns Decoded first reply; rejects on capacity exhaustion, timeout, error, exit-without-reply or codec failure.
|
|
79
99
|
* @throws RangeError (as a rejected promise) for an invalid timeout.
|
|
80
100
|
* @complexity O(1) harness work, excluding structured clone, worker execution and decoding.
|
|
81
101
|
*/
|
package/dist/node-worker.js
CHANGED
|
@@ -6,6 +6,39 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { Worker } from "node:worker_threads";
|
|
8
8
|
import { isAbsolute } from "node:path";
|
|
9
|
+
/** Capacity exhaustion is a controlled refusal, allowing hosts to render their fallback. */
|
|
10
|
+
export class WorkerCapacityError extends Error {
|
|
11
|
+
constructor() {
|
|
12
|
+
super("Worker capacity is busy; retry shortly");
|
|
13
|
+
this.name = "WorkerCapacityError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Bound active disposable workers without queuing caller payloads or keeping idle workers alive.
|
|
18
|
+
* @param required Positive maximum number of active workers shared by callers.
|
|
19
|
+
* @returns Admission port with idempotent lease release; acquire throws WorkerCapacityError at the cap.
|
|
20
|
+
* @throws RangeError for an invalid maximum.
|
|
21
|
+
* @complexity O(1) time and state per acquisition/release; no waiting queue.
|
|
22
|
+
*/
|
|
23
|
+
export function createWorkerCapacity({ maxConcurrent }, _options = {}) {
|
|
24
|
+
if (!Number.isSafeInteger(maxConcurrent) || maxConcurrent < 1)
|
|
25
|
+
throw new RangeError("maxConcurrent must be a positive safe integer");
|
|
26
|
+
let active = 0;
|
|
27
|
+
return {
|
|
28
|
+
acquire() {
|
|
29
|
+
if (active >= maxConcurrent)
|
|
30
|
+
throw new WorkerCapacityError();
|
|
31
|
+
active++;
|
|
32
|
+
let released = false;
|
|
33
|
+
return () => {
|
|
34
|
+
if (released)
|
|
35
|
+
return;
|
|
36
|
+
released = true;
|
|
37
|
+
active--;
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
9
42
|
/**
|
|
10
43
|
* Strictly parse a host-supplied timeout value, falling back on malformed or out-of-range values.
|
|
11
44
|
* @param required Raw configuration and explicit default/maximum budgets in milliseconds.
|
|
@@ -26,7 +59,7 @@ export function resolveDefaultTimeoutMs({ rawValue, defaultTimeoutMs, maxTimeout
|
|
|
26
59
|
* Run structured-cloneable payloads in a fresh bounded worker using a caller-owned result codec.
|
|
27
60
|
* @param required Entry, payload, budgets and worker/timer/decoder ports.
|
|
28
61
|
* @param options Per-invocation timeout and resource limits.
|
|
29
|
-
* @returns Decoded first reply; rejects on timeout, error, exit-without-reply or codec failure.
|
|
62
|
+
* @returns Decoded first reply; rejects on capacity exhaustion, timeout, error, exit-without-reply or codec failure.
|
|
30
63
|
* @throws RangeError (as a rejected promise) for an invalid timeout.
|
|
31
64
|
* @complexity O(1) harness work, excluding structured clone, worker execution and decoding.
|
|
32
65
|
*/
|
|
@@ -36,13 +69,24 @@ export function runInWorkerSandbox(required, options = {}) {
|
|
|
36
69
|
return Promise.reject(new RangeError("timeoutMs must be a positive integer within the Node timer range"));
|
|
37
70
|
}
|
|
38
71
|
return new Promise((resolve, reject) => {
|
|
39
|
-
const
|
|
72
|
+
const release = required.capacity?.acquire({});
|
|
73
|
+
let worker;
|
|
74
|
+
try {
|
|
75
|
+
worker = required.workerFactory.spawn({ workerEntry: required.workerEntry, payload: required.input, resourceLimits: options.resourceLimits ?? required.defaultResourceLimits });
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
release?.();
|
|
79
|
+
reject(error);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
40
82
|
let settled = false;
|
|
41
83
|
let cancelTimer;
|
|
42
84
|
function terminate() {
|
|
43
85
|
// Teardown must never create a second rejection after the result has already settled.
|
|
86
|
+
// A reply/timeout is not proof the native worker is gone. Retain its slot until teardown
|
|
87
|
+
// completes or an exit arrives; failed termination must not admit more live workers.
|
|
44
88
|
try {
|
|
45
|
-
void worker.terminate().
|
|
89
|
+
void worker.terminate().then(release, () => { });
|
|
46
90
|
}
|
|
47
91
|
catch { /* best effort */ }
|
|
48
92
|
}
|
|
@@ -62,7 +106,10 @@ export function runInWorkerSandbox(required, options = {}) {
|
|
|
62
106
|
try {
|
|
63
107
|
worker.once({ event: "message", listener: ({ message }) => finish(() => resolve(required.decodeResult({ message }))) });
|
|
64
108
|
worker.once({ event: "error", listener: ({ error }) => finish(() => reject(error)) });
|
|
65
|
-
worker.once({ event: "exit", listener: ({ code }) =>
|
|
109
|
+
worker.once({ event: "exit", listener: ({ code }) => {
|
|
110
|
+
release?.();
|
|
111
|
+
finish(() => reject(new Error(`${required.errorLabel} worker exited with code ${code}`)));
|
|
112
|
+
} });
|
|
66
113
|
cancelTimer = required.scheduler.schedule({ delayMs: timeoutMs, run: () => finish(() => reject(new Error(`${required.errorLabel} exceeded ${timeoutMs}ms timeout`))) });
|
|
67
114
|
}
|
|
68
115
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/sandbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Execution/sandbox sibling to @jini-ai/vibecoding's edit loop: boot an environment, mount files, install deps, run commands, preview, and tear down — behind one interface with a driver-neutral core. Import `./core` with no adapter installed; add `./e2b` for the E2B (Firecracker microVM) backend.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -80,7 +80,8 @@
|
|
|
80
80
|
"!dist/**/*.test.d.ts",
|
|
81
81
|
"!dist/**/*.test.js.map",
|
|
82
82
|
"!dist/**/*.test.d.ts.map",
|
|
83
|
-
"!dist/**/__tests__/**"
|
|
83
|
+
"!dist/**/__tests__/**",
|
|
84
|
+
"!dist/**/*.map"
|
|
84
85
|
],
|
|
85
86
|
"publishConfig": {
|
|
86
87
|
"access": "public",
|