@enclave-vm/core 0.0.1 → 2.8.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/esm/index.mjs +11172 -0
- package/esm/package.json +6 -0
- package/index.js +11268 -0
- package/package.json +11 -10
- package/src/adapters/vm-adapter.js +0 -1140
- package/src/adapters/vm-adapter.js.map +0 -1
- package/src/adapters/worker-pool/config.js +0 -129
- package/src/adapters/worker-pool/config.js.map +0 -1
- package/src/adapters/worker-pool/errors.js +0 -181
- package/src/adapters/worker-pool/errors.js.map +0 -1
- package/src/adapters/worker-pool/execution-queue.js +0 -179
- package/src/adapters/worker-pool/execution-queue.js.map +0 -1
- package/src/adapters/worker-pool/index.js +0 -73
- package/src/adapters/worker-pool/index.js.map +0 -1
- package/src/adapters/worker-pool/memory-monitor.js +0 -184
- package/src/adapters/worker-pool/memory-monitor.js.map +0 -1
- package/src/adapters/worker-pool/protocol.js +0 -143
- package/src/adapters/worker-pool/protocol.js.map +0 -1
- package/src/adapters/worker-pool/rate-limiter.js +0 -101
- package/src/adapters/worker-pool/rate-limiter.js.map +0 -1
- package/src/adapters/worker-pool/safe-deserialize.js +0 -141
- package/src/adapters/worker-pool/safe-deserialize.js.map +0 -1
- package/src/adapters/worker-pool/worker-pool-adapter.js +0 -506
- package/src/adapters/worker-pool/worker-pool-adapter.js.map +0 -1
- package/src/adapters/worker-pool/worker-script.js +0 -770
- package/src/adapters/worker-pool/worker-script.js.map +0 -1
- package/src/adapters/worker-pool/worker-slot.js +0 -301
- package/src/adapters/worker-pool/worker-slot.js.map +0 -1
- package/src/babel/babel-wrapper.js +0 -395
- package/src/babel/babel-wrapper.js.map +0 -1
- package/src/babel/index.js +0 -17
- package/src/babel/index.js.map +0 -1
- package/src/babel/multi-file-transform.js +0 -344
- package/src/babel/multi-file-transform.js.map +0 -1
- package/src/double-vm/double-vm-wrapper.js +0 -523
- package/src/double-vm/double-vm-wrapper.js.map +0 -1
- package/src/double-vm/index.js +0 -34
- package/src/double-vm/index.js.map +0 -1
- package/src/double-vm/parent-vm-bootstrap.js +0 -1695
- package/src/double-vm/parent-vm-bootstrap.js.map +0 -1
- package/src/double-vm/suspicious-patterns.js +0 -304
- package/src/double-vm/suspicious-patterns.js.map +0 -1
- package/src/double-vm/types.js +0 -11
- package/src/double-vm/types.js.map +0 -1
- package/src/double-vm/wrap-adapter.js +0 -59
- package/src/double-vm/wrap-adapter.js.map +0 -1
- package/src/enclave.js +0 -671
- package/src/enclave.js.map +0 -1
- package/src/globals-validator.js +0 -206
- package/src/globals-validator.js.map +0 -1
- package/src/index.js +0 -130
- package/src/index.js.map +0 -1
- package/src/memory-proxy.js +0 -310
- package/src/memory-proxy.js.map +0 -1
- package/src/memory-tracker.js +0 -230
- package/src/memory-tracker.js.map +0 -1
- package/src/safe-error.js +0 -76
- package/src/safe-error.js.map +0 -1
- package/src/safe-runtime.js +0 -656
- package/src/safe-runtime.js.map +0 -1
- package/src/scoring/cache.js +0 -158
- package/src/scoring/cache.js.map +0 -1
- package/src/scoring/feature-extractor.js +0 -366
- package/src/scoring/feature-extractor.js.map +0 -1
- package/src/scoring/index.js +0 -53
- package/src/scoring/index.js.map +0 -1
- package/src/scoring/scorer.interface.js +0 -49
- package/src/scoring/scorer.interface.js.map +0 -1
- package/src/scoring/scorers/disabled.scorer.js +0 -34
- package/src/scoring/scorers/disabled.scorer.js.map +0 -1
- package/src/scoring/scorers/external-api.scorer.js +0 -141
- package/src/scoring/scorers/external-api.scorer.js.map +0 -1
- package/src/scoring/scorers/index.js +0 -22
- package/src/scoring/scorers/index.js.map +0 -1
- package/src/scoring/scorers/local-llm.scorer.js +0 -474
- package/src/scoring/scorers/local-llm.scorer.js.map +0 -1
- package/src/scoring/scorers/progressive.scorer.js +0 -199
- package/src/scoring/scorers/progressive.scorer.js.map +0 -1
- package/src/scoring/scorers/rule-based.scorer.js +0 -316
- package/src/scoring/scorers/rule-based.scorer.js.map +0 -1
- package/src/scoring/scoring-gate.js +0 -246
- package/src/scoring/scoring-gate.js.map +0 -1
- package/src/scoring/types.js +0 -107
- package/src/scoring/types.js.map +0 -1
- package/src/secure-proxy.js +0 -609
- package/src/secure-proxy.js.map +0 -1
- package/src/session/channels/embedded-channel.js +0 -130
- package/src/session/channels/embedded-channel.js.map +0 -1
- package/src/session/channels/index.js +0 -14
- package/src/session/channels/index.js.map +0 -1
- package/src/session/index.js +0 -27
- package/src/session/index.js.map +0 -1
- package/src/session/session-emitter.js +0 -253
- package/src/session/session-emitter.js.map +0 -1
- package/src/session/session-state-machine.js +0 -182
- package/src/session/session-state-machine.js.map +0 -1
- package/src/session/session.js +0 -398
- package/src/session/session.js.map +0 -1
- package/src/session-types.js +0 -39
- package/src/session-types.js.map +0 -1
- package/src/sidecar/index.js +0 -31
- package/src/sidecar/index.js.map +0 -1
- package/src/sidecar/reference-config.js +0 -118
- package/src/sidecar/reference-config.js.map +0 -1
- package/src/sidecar/reference-resolver.js +0 -262
- package/src/sidecar/reference-resolver.js.map +0 -1
- package/src/sidecar/reference-sidecar.js +0 -226
- package/src/sidecar/reference-sidecar.js.map +0 -1
- package/src/tool-bridge.js +0 -306
- package/src/tool-bridge.js.map +0 -1
- package/src/types.js +0 -187
- package/src/types.js.map +0 -1
- package/src/value-sanitizer.js +0 -421
- package/src/value-sanitizer.js.map +0 -1
- /package/{src/adapters → adapters}/vm-adapter.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/config.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/errors.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/execution-queue.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/index.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/memory-monitor.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/protocol.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/rate-limiter.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/safe-deserialize.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/worker-pool-adapter.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/worker-script.d.ts +0 -0
- /package/{src/adapters → adapters}/worker-pool/worker-slot.d.ts +0 -0
- /package/{src/babel → babel}/babel-wrapper.d.ts +0 -0
- /package/{src/babel → babel}/index.d.ts +0 -0
- /package/{src/babel → babel}/multi-file-transform.d.ts +0 -0
- /package/{src/double-vm → double-vm}/double-vm-wrapper.d.ts +0 -0
- /package/{src/double-vm → double-vm}/index.d.ts +0 -0
- /package/{src/double-vm → double-vm}/parent-vm-bootstrap.d.ts +0 -0
- /package/{src/double-vm → double-vm}/suspicious-patterns.d.ts +0 -0
- /package/{src/double-vm → double-vm}/types.d.ts +0 -0
- /package/{src/double-vm → double-vm}/wrap-adapter.d.ts +0 -0
- /package/{src/enclave.d.ts → enclave.d.ts} +0 -0
- /package/{src/globals-validator.d.ts → globals-validator.d.ts} +0 -0
- /package/{src/index.d.ts → index.d.ts} +0 -0
- /package/{src/memory-proxy.d.ts → memory-proxy.d.ts} +0 -0
- /package/{src/memory-tracker.d.ts → memory-tracker.d.ts} +0 -0
- /package/{src/safe-error.d.ts → safe-error.d.ts} +0 -0
- /package/{src/safe-runtime.d.ts → safe-runtime.d.ts} +0 -0
- /package/{src/scoring → scoring}/cache.d.ts +0 -0
- /package/{src/scoring → scoring}/feature-extractor.d.ts +0 -0
- /package/{src/scoring → scoring}/index.d.ts +0 -0
- /package/{src/scoring → scoring}/scorer.interface.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/disabled.scorer.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/external-api.scorer.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/index.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/local-llm.scorer.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/progressive.scorer.d.ts +0 -0
- /package/{src/scoring → scoring}/scorers/rule-based.scorer.d.ts +0 -0
- /package/{src/scoring → scoring}/scoring-gate.d.ts +0 -0
- /package/{src/scoring → scoring}/types.d.ts +0 -0
- /package/{src/secure-proxy.d.ts → secure-proxy.d.ts} +0 -0
- /package/{src/session → session}/channels/embedded-channel.d.ts +0 -0
- /package/{src/session → session}/channels/index.d.ts +0 -0
- /package/{src/session → session}/index.d.ts +0 -0
- /package/{src/session → session}/session-emitter.d.ts +0 -0
- /package/{src/session → session}/session-state-machine.d.ts +0 -0
- /package/{src/session → session}/session.d.ts +0 -0
- /package/{src/session-types.d.ts → session-types.d.ts} +0 -0
- /package/{src/sidecar → sidecar}/index.d.ts +0 -0
- /package/{src/sidecar → sidecar}/reference-config.d.ts +0 -0
- /package/{src/sidecar → sidecar}/reference-resolver.d.ts +0 -0
- /package/{src/sidecar → sidecar}/reference-sidecar.d.ts +0 -0
- /package/{src/tool-bridge.d.ts → tool-bridge.d.ts} +0 -0
- /package/{src/types.d.ts → types.d.ts} +0 -0
- /package/{src/value-sanitizer.d.ts → value-sanitizer.d.ts} +0 -0
|
@@ -1,770 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Worker Script
|
|
4
|
-
*
|
|
5
|
-
* This script runs inside a worker thread and provides a dual-layer sandbox:
|
|
6
|
-
* 1. Worker thread isolation (OS-level)
|
|
7
|
-
* 2. VM context isolation (prototype isolation)
|
|
8
|
-
*
|
|
9
|
-
* Tool calls are proxied to the main thread via message passing.
|
|
10
|
-
*
|
|
11
|
-
* @packageDocumentation
|
|
12
|
-
*/
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
const tslib_1 = require("tslib");
|
|
15
|
-
const worker_threads_1 = require("worker_threads");
|
|
16
|
-
const vm_1 = tslib_1.__importDefault(require("vm"));
|
|
17
|
-
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
|
18
|
-
const safe_deserialize_1 = require("./safe-deserialize");
|
|
19
|
-
/**
|
|
20
|
-
* Patch code executed inside the sandbox realm to prevent leaking host stack traces via `error.stack`.
|
|
21
|
-
*/
|
|
22
|
-
const STACK_TRACE_HARDENING_CODE = `
|
|
23
|
-
(function() {
|
|
24
|
-
function __ag_redactStackString(stackStr) {
|
|
25
|
-
try {
|
|
26
|
-
if (typeof stackStr !== 'string' || !stackStr) return 'Error';
|
|
27
|
-
var lines = String(stackStr).split('\\n');
|
|
28
|
-
var header = lines[0] ? String(lines[0]) : 'Error';
|
|
29
|
-
var frameCount = (lines.length > 1) ? (lines.length - 1) : 0;
|
|
30
|
-
var max = frameCount;
|
|
31
|
-
if (max > 25) max = 25;
|
|
32
|
-
var out = [header];
|
|
33
|
-
for (var i = 0; i < max; i++) out.push(' at [REDACTED]');
|
|
34
|
-
if (frameCount > max) out.push(' at [REDACTED]');
|
|
35
|
-
return out.join('\\n');
|
|
36
|
-
} catch (e) {
|
|
37
|
-
return 'Error';
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function __ag_prepareStackTrace(err, stack) {
|
|
42
|
-
try {
|
|
43
|
-
var name = (err && err.name) ? String(err.name) : 'Error';
|
|
44
|
-
var message = (err && err.message) ? String(err.message) : '';
|
|
45
|
-
var header = message ? (name + ': ' + message) : name;
|
|
46
|
-
if (!stack || !stack.length) return header;
|
|
47
|
-
var lines = [header];
|
|
48
|
-
var max = stack.length;
|
|
49
|
-
if (max > 25) max = 25;
|
|
50
|
-
for (var i = 0; i < max; i++) {
|
|
51
|
-
lines.push(' at [REDACTED]');
|
|
52
|
-
}
|
|
53
|
-
if (stack.length > max) {
|
|
54
|
-
lines.push(' at [REDACTED]');
|
|
55
|
-
}
|
|
56
|
-
return lines.join('\\n');
|
|
57
|
-
} catch (e) {
|
|
58
|
-
return 'Error';
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function __ag_lockStackGetter(proto) {
|
|
63
|
-
if (!proto) return;
|
|
64
|
-
try {
|
|
65
|
-
var desc = Object.getOwnPropertyDescriptor(proto, 'stack');
|
|
66
|
-
if (!desc || typeof desc.get !== 'function') return;
|
|
67
|
-
var origGet = desc.get;
|
|
68
|
-
var origSet = desc.set;
|
|
69
|
-
Object.defineProperty(proto, 'stack', {
|
|
70
|
-
get: function() {
|
|
71
|
-
try {
|
|
72
|
-
return __ag_redactStackString(origGet.call(this));
|
|
73
|
-
} catch (e) {
|
|
74
|
-
return 'Error';
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
set: function(v) {
|
|
78
|
-
try {
|
|
79
|
-
if (typeof origSet === 'function') return origSet.call(this, v);
|
|
80
|
-
} catch (e) {}
|
|
81
|
-
},
|
|
82
|
-
configurable: false,
|
|
83
|
-
enumerable: false
|
|
84
|
-
});
|
|
85
|
-
} catch (e) {}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function __ag_lockPrepareStackTrace(ErrCtor) {
|
|
89
|
-
if (!ErrCtor) return;
|
|
90
|
-
try {
|
|
91
|
-
Object.defineProperty(ErrCtor, 'prepareStackTrace', {
|
|
92
|
-
value: __ag_prepareStackTrace,
|
|
93
|
-
writable: false,
|
|
94
|
-
configurable: false,
|
|
95
|
-
enumerable: false
|
|
96
|
-
});
|
|
97
|
-
} catch (e) {}
|
|
98
|
-
try {
|
|
99
|
-
Object.defineProperty(ErrCtor, 'stackTraceLimit', {
|
|
100
|
-
value: 25,
|
|
101
|
-
writable: false,
|
|
102
|
-
configurable: false,
|
|
103
|
-
enumerable: false
|
|
104
|
-
});
|
|
105
|
-
} catch (e) {}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
__ag_lockStackGetter(Error && Error.prototype);
|
|
109
|
-
__ag_lockPrepareStackTrace(Error);
|
|
110
|
-
__ag_lockPrepareStackTrace(EvalError);
|
|
111
|
-
__ag_lockPrepareStackTrace(RangeError);
|
|
112
|
-
__ag_lockPrepareStackTrace(ReferenceError);
|
|
113
|
-
__ag_lockPrepareStackTrace(SyntaxError);
|
|
114
|
-
__ag_lockPrepareStackTrace(TypeError);
|
|
115
|
-
__ag_lockPrepareStackTrace(URIError);
|
|
116
|
-
try {
|
|
117
|
-
if (typeof AggregateError !== 'undefined') __ag_lockPrepareStackTrace(AggregateError);
|
|
118
|
-
} catch (e) {}
|
|
119
|
-
})();
|
|
120
|
-
`.trim();
|
|
121
|
-
const STACK_TRACE_HARDENING_SCRIPT = new vm_1.default.Script(STACK_TRACE_HARDENING_CODE);
|
|
122
|
-
/**
|
|
123
|
-
* Detect and report code-generation attempts (Function/eval) even if user code catches the thrown error.
|
|
124
|
-
*
|
|
125
|
-
* Used to fail closed in STRICT/SECURE modes.
|
|
126
|
-
*/
|
|
127
|
-
const CODE_GENERATION_VIOLATION_DETECTOR_CODE = `
|
|
128
|
-
(function() {
|
|
129
|
-
var __ag_report = (typeof __ag_reportViolation__ === 'function') ? __ag_reportViolation__ : null;
|
|
130
|
-
function __ag_reportOnce(kind) {
|
|
131
|
-
try {
|
|
132
|
-
if (__ag_report) __ag_report(kind);
|
|
133
|
-
} catch (e) {}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Capture intrinsics so later global sanitization can't break the proxy handler.
|
|
137
|
-
var __ag_Reflect = (typeof Reflect !== 'undefined') ? Reflect : null;
|
|
138
|
-
var __ag_Proxy = (typeof Proxy !== 'undefined') ? Proxy : null;
|
|
139
|
-
var __ag_Object = (typeof Object !== 'undefined') ? Object : null;
|
|
140
|
-
|
|
141
|
-
function __ag_wrapCtor(Ctor, kind) {
|
|
142
|
-
if (!Ctor || !__ag_Proxy || !__ag_Reflect || !__ag_Object) return;
|
|
143
|
-
try {
|
|
144
|
-
var proxy = new __ag_Proxy(Ctor, {
|
|
145
|
-
apply: function(target, thisArg, args) {
|
|
146
|
-
__ag_reportOnce(kind);
|
|
147
|
-
return __ag_Reflect.apply(target, thisArg, args);
|
|
148
|
-
},
|
|
149
|
-
construct: function(target, args, newTarget) {
|
|
150
|
-
__ag_reportOnce(kind);
|
|
151
|
-
return __ag_Reflect.construct(target, args, newTarget);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
if (Ctor.prototype) {
|
|
157
|
-
__ag_Object.defineProperty(Ctor.prototype, 'constructor', {
|
|
158
|
-
value: proxy,
|
|
159
|
-
writable: false,
|
|
160
|
-
configurable: false,
|
|
161
|
-
enumerable: false
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
} catch (e) {}
|
|
165
|
-
} catch (e) {}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
try { __ag_wrapCtor(Function, 'CODE_GENERATION'); } catch (e) {}
|
|
169
|
-
try { __ag_wrapCtor((async function(){}).constructor, 'CODE_GENERATION'); } catch (e) {}
|
|
170
|
-
try { __ag_wrapCtor((function*(){}).constructor, 'CODE_GENERATION'); } catch (e) {}
|
|
171
|
-
try { __ag_wrapCtor((async function*(){}).constructor, 'CODE_GENERATION'); } catch (e) {}
|
|
172
|
-
})();
|
|
173
|
-
`.trim();
|
|
174
|
-
const CODE_GENERATION_VIOLATION_DETECTOR_SCRIPT = new vm_1.default.Script(CODE_GENERATION_VIOLATION_DETECTOR_CODE);
|
|
175
|
-
// ============================================================================
|
|
176
|
-
// SECURITY: Capture parentPort then remove dangerous globals
|
|
177
|
-
// ============================================================================
|
|
178
|
-
const port = worker_threads_1.parentPort;
|
|
179
|
-
if (!port) {
|
|
180
|
-
throw new Error('worker-script.ts must run inside a worker thread');
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Globals that are dangerous and must be removed before any user code runs
|
|
184
|
-
*/
|
|
185
|
-
const DANGEROUS_GLOBALS = [
|
|
186
|
-
'parentPort',
|
|
187
|
-
'workerData',
|
|
188
|
-
'threadId',
|
|
189
|
-
'isMainThread',
|
|
190
|
-
'MessagePort',
|
|
191
|
-
'MessageChannel',
|
|
192
|
-
'BroadcastChannel',
|
|
193
|
-
'Worker',
|
|
194
|
-
'SharedArrayBuffer',
|
|
195
|
-
'Atomics',
|
|
196
|
-
];
|
|
197
|
-
for (const name of DANGEROUS_GLOBALS) {
|
|
198
|
-
try {
|
|
199
|
-
Object.defineProperty(globalThis, name, {
|
|
200
|
-
value: undefined,
|
|
201
|
-
writable: false,
|
|
202
|
-
configurable: false,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
// Some may already be undefined or not configurable
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
let currentExecution = null;
|
|
210
|
-
const pendingToolCalls = new Map();
|
|
211
|
-
// ============================================================================
|
|
212
|
-
// Message Handling
|
|
213
|
-
// ============================================================================
|
|
214
|
-
port.on('message', async (raw) => {
|
|
215
|
-
try {
|
|
216
|
-
const msg = (0, safe_deserialize_1.safeDeserialize)(raw);
|
|
217
|
-
switch (msg.type) {
|
|
218
|
-
case 'execute':
|
|
219
|
-
await handleExecute(msg);
|
|
220
|
-
break;
|
|
221
|
-
case 'tool-response':
|
|
222
|
-
handleToolResponse(msg);
|
|
223
|
-
break;
|
|
224
|
-
case 'memory-report':
|
|
225
|
-
handleMemoryReport();
|
|
226
|
-
break;
|
|
227
|
-
case 'abort':
|
|
228
|
-
handleAbort(msg.requestId);
|
|
229
|
-
break;
|
|
230
|
-
case 'terminate':
|
|
231
|
-
handleTerminate(msg.graceful);
|
|
232
|
-
break;
|
|
233
|
-
default:
|
|
234
|
-
console.error('Unknown message type:', msg.type);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
catch (error) {
|
|
238
|
-
console.error('Worker message handler error:', error);
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
// ============================================================================
|
|
242
|
-
// Execute Handler
|
|
243
|
-
// ============================================================================
|
|
244
|
-
async function handleExecute(msg) {
|
|
245
|
-
const startTime = Date.now();
|
|
246
|
-
currentExecution = {
|
|
247
|
-
id: msg.requestId,
|
|
248
|
-
aborted: false,
|
|
249
|
-
config: msg.config,
|
|
250
|
-
stats: {
|
|
251
|
-
duration: 0,
|
|
252
|
-
toolCallCount: 0,
|
|
253
|
-
iterationCount: 0,
|
|
254
|
-
startTime,
|
|
255
|
-
endTime: 0,
|
|
256
|
-
},
|
|
257
|
-
};
|
|
258
|
-
try {
|
|
259
|
-
// Create VM context with safe runtime
|
|
260
|
-
// codeGeneration.strings=false disables new Function() and eval() from strings
|
|
261
|
-
// This prevents sandbox escape via constructor chain: [][c][c]('malicious code')
|
|
262
|
-
const sandbox = createSandbox(msg.requestId, msg.config);
|
|
263
|
-
const context = vm_1.default.createContext(sandbox, {
|
|
264
|
-
codeGeneration: { strings: false, wasm: false },
|
|
265
|
-
});
|
|
266
|
-
const isStrictOrSecure = msg.config.securityLevel === 'STRICT' || msg.config.securityLevel === 'SECURE';
|
|
267
|
-
const policyViolation = {};
|
|
268
|
-
// SECURITY HARDENING: prevent leaking host stack traces via error.stack inside the sandbox.
|
|
269
|
-
const shouldHardenStacks = msg.config.sanitizeStackTraces ?? true;
|
|
270
|
-
if (shouldHardenStacks) {
|
|
271
|
-
try {
|
|
272
|
-
STACK_TRACE_HARDENING_SCRIPT.runInContext(context);
|
|
273
|
-
}
|
|
274
|
-
catch {
|
|
275
|
-
// Ignore if the environment forbids patching (defense-in-depth only)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
// STRICT/SECURE: record code-generation attempts (Function/eval) even if caught.
|
|
279
|
-
if (isStrictOrSecure) {
|
|
280
|
-
try {
|
|
281
|
-
Object.defineProperty(context, '__ag_reportViolation__', {
|
|
282
|
-
value: (type) => {
|
|
283
|
-
if (!policyViolation.type)
|
|
284
|
-
policyViolation.type = String(type);
|
|
285
|
-
},
|
|
286
|
-
writable: false,
|
|
287
|
-
configurable: false,
|
|
288
|
-
enumerable: false,
|
|
289
|
-
});
|
|
290
|
-
CODE_GENERATION_VIOLATION_DETECTOR_SCRIPT.runInContext(context);
|
|
291
|
-
}
|
|
292
|
-
catch {
|
|
293
|
-
// Best-effort only; codeGeneration.strings=false still blocks execution.
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
// Wrap code in async IIFE to support top-level await
|
|
297
|
-
// Must call __ag_main() if defined, as the enclave transforms code to wrap in async function __ag_main()
|
|
298
|
-
const wrappedCode = `
|
|
299
|
-
(async () => {
|
|
300
|
-
${msg.code}
|
|
301
|
-
return typeof __ag_main === 'function' ? await __ag_main() : undefined;
|
|
302
|
-
})();
|
|
303
|
-
`;
|
|
304
|
-
// Compile and run with timeout
|
|
305
|
-
const script = new vm_1.default.Script(wrappedCode, {
|
|
306
|
-
filename: 'agentscript.js',
|
|
307
|
-
});
|
|
308
|
-
const result = await script.runInContext(context, {
|
|
309
|
-
timeout: msg.config.timeout,
|
|
310
|
-
breakOnSigint: true,
|
|
311
|
-
});
|
|
312
|
-
// Update stats
|
|
313
|
-
currentExecution.stats.endTime = Date.now();
|
|
314
|
-
currentExecution.stats.duration = currentExecution.stats.endTime - startTime;
|
|
315
|
-
// STRICT/SECURE: Fail closed on recorded policy violations even if user code caught them.
|
|
316
|
-
if (isStrictOrSecure && policyViolation.type) {
|
|
317
|
-
sendMessage({
|
|
318
|
-
type: 'result',
|
|
319
|
-
requestId: msg.requestId,
|
|
320
|
-
success: false,
|
|
321
|
-
error: {
|
|
322
|
-
name: 'SecurityViolationError',
|
|
323
|
-
message: 'Blocked operation: security policy violation',
|
|
324
|
-
code: 'SECURITY_VIOLATION',
|
|
325
|
-
},
|
|
326
|
-
stats: currentExecution.stats,
|
|
327
|
-
});
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
// Send success result
|
|
331
|
-
sendMessage({
|
|
332
|
-
type: 'result',
|
|
333
|
-
requestId: msg.requestId,
|
|
334
|
-
success: true,
|
|
335
|
-
value: (0, safe_deserialize_1.sanitizeObject)(result),
|
|
336
|
-
stats: currentExecution.stats,
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
catch (error) {
|
|
340
|
-
// Update stats
|
|
341
|
-
if (currentExecution) {
|
|
342
|
-
currentExecution.stats.endTime = Date.now();
|
|
343
|
-
currentExecution.stats.duration = currentExecution.stats.endTime - startTime;
|
|
344
|
-
}
|
|
345
|
-
// Send error result
|
|
346
|
-
sendMessage({
|
|
347
|
-
type: 'result',
|
|
348
|
-
requestId: msg.requestId,
|
|
349
|
-
success: false,
|
|
350
|
-
error: serializeError(error, msg.config.sanitizeStackTraces),
|
|
351
|
-
stats: currentExecution?.stats ?? {
|
|
352
|
-
duration: Date.now() - startTime,
|
|
353
|
-
toolCallCount: 0,
|
|
354
|
-
iterationCount: 0,
|
|
355
|
-
startTime,
|
|
356
|
-
endTime: Date.now(),
|
|
357
|
-
},
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
finally {
|
|
361
|
-
// Clear pending tool calls for this execution
|
|
362
|
-
// Use delimiter to avoid false positives (e.g., "req-1" matching "req-10-abc")
|
|
363
|
-
for (const [callId, pending] of pendingToolCalls) {
|
|
364
|
-
if (callId.startsWith(`${msg.requestId}-`)) {
|
|
365
|
-
pending.reject(new Error('Execution ended'));
|
|
366
|
-
pendingToolCalls.delete(callId);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
currentExecution = null;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
// ============================================================================
|
|
373
|
-
// Tool Response Handler
|
|
374
|
-
// ============================================================================
|
|
375
|
-
function handleToolResponse(msg) {
|
|
376
|
-
const pending = pendingToolCalls.get(msg.callId);
|
|
377
|
-
if (!pending) {
|
|
378
|
-
console.warn('Received response for unknown tool call:', msg.callId);
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
pendingToolCalls.delete(msg.callId);
|
|
382
|
-
if (msg.error) {
|
|
383
|
-
const error = new Error(msg.error.message);
|
|
384
|
-
error.name = msg.error.name;
|
|
385
|
-
pending.reject(error);
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
pending.resolve((0, safe_deserialize_1.sanitizeObject)(msg.result));
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
// ============================================================================
|
|
392
|
-
// Memory Report Handler
|
|
393
|
-
// ============================================================================
|
|
394
|
-
function handleMemoryReport() {
|
|
395
|
-
const usage = process.memoryUsage();
|
|
396
|
-
sendMessage({
|
|
397
|
-
type: 'memory-report-result',
|
|
398
|
-
usage: {
|
|
399
|
-
rss: usage.rss,
|
|
400
|
-
heapTotal: usage.heapTotal,
|
|
401
|
-
heapUsed: usage.heapUsed,
|
|
402
|
-
external: usage.external,
|
|
403
|
-
arrayBuffers: usage.arrayBuffers,
|
|
404
|
-
},
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
// ============================================================================
|
|
408
|
-
// Abort Handler
|
|
409
|
-
// ============================================================================
|
|
410
|
-
function handleAbort(requestId) {
|
|
411
|
-
if (currentExecution && currentExecution.id === requestId) {
|
|
412
|
-
currentExecution.aborted = true;
|
|
413
|
-
}
|
|
414
|
-
// Reject all pending tool calls for this request
|
|
415
|
-
// Use delimiter to avoid false positives (e.g., "req-1" matching "req-10-abc")
|
|
416
|
-
for (const [callId, pending] of pendingToolCalls) {
|
|
417
|
-
if (callId.startsWith(`${requestId}-`)) {
|
|
418
|
-
pending.reject(new Error('Execution aborted'));
|
|
419
|
-
pendingToolCalls.delete(callId);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
// ============================================================================
|
|
424
|
-
// Terminate Handler
|
|
425
|
-
// ============================================================================
|
|
426
|
-
function handleTerminate(graceful) {
|
|
427
|
-
if (graceful && currentExecution) {
|
|
428
|
-
// Mark as aborted and let it finish naturally
|
|
429
|
-
currentExecution.aborted = true;
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
// Force exit
|
|
433
|
-
process.exit(0);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
// ============================================================================
|
|
437
|
-
// Sandbox Creation - Security-Level-Aware
|
|
438
|
-
// ============================================================================
|
|
439
|
-
/**
|
|
440
|
-
* Maps global names to their actual values.
|
|
441
|
-
* This mapping is used by createSandbox to selectively expose globals
|
|
442
|
-
* based on the security level.
|
|
443
|
-
*/
|
|
444
|
-
function getGlobalValue(name, requestId, config) {
|
|
445
|
-
// Runtime-injected safe functions (created dynamically)
|
|
446
|
-
switch (name) {
|
|
447
|
-
case '__safe_callTool':
|
|
448
|
-
case 'callTool': // Both are mapped to the safe version
|
|
449
|
-
return createProxiedCallTool(requestId, config);
|
|
450
|
-
case '__safe_forOf':
|
|
451
|
-
return createSafeForOf();
|
|
452
|
-
case '__safe_for':
|
|
453
|
-
return createSafeFor();
|
|
454
|
-
case '__safe_while':
|
|
455
|
-
case '__safe_doWhile':
|
|
456
|
-
return createSafeWhile();
|
|
457
|
-
case '__maxIterations':
|
|
458
|
-
return config.maxIterations ?? 10000;
|
|
459
|
-
case 'console':
|
|
460
|
-
case '__safe_console':
|
|
461
|
-
return createSafeConsole(requestId, config);
|
|
462
|
-
// Core built-in objects (always safe)
|
|
463
|
-
case 'Math':
|
|
464
|
-
return Math;
|
|
465
|
-
case 'JSON':
|
|
466
|
-
return JSON;
|
|
467
|
-
case 'Object':
|
|
468
|
-
return Object;
|
|
469
|
-
case 'Array':
|
|
470
|
-
return Array;
|
|
471
|
-
case 'String':
|
|
472
|
-
return String;
|
|
473
|
-
case 'Number':
|
|
474
|
-
return Number;
|
|
475
|
-
case 'Date':
|
|
476
|
-
return Date;
|
|
477
|
-
// Safe standard globals
|
|
478
|
-
case 'undefined':
|
|
479
|
-
return undefined;
|
|
480
|
-
case 'NaN':
|
|
481
|
-
return NaN;
|
|
482
|
-
case 'Infinity':
|
|
483
|
-
return Infinity;
|
|
484
|
-
// Utility functions (STANDARD level and above)
|
|
485
|
-
case 'parseInt':
|
|
486
|
-
return parseInt;
|
|
487
|
-
case 'parseFloat':
|
|
488
|
-
return parseFloat;
|
|
489
|
-
case 'isNaN':
|
|
490
|
-
return isNaN;
|
|
491
|
-
case 'isFinite':
|
|
492
|
-
return isFinite;
|
|
493
|
-
case 'encodeURI':
|
|
494
|
-
return encodeURI;
|
|
495
|
-
case 'decodeURI':
|
|
496
|
-
return decodeURI;
|
|
497
|
-
case 'encodeURIComponent':
|
|
498
|
-
return encodeURIComponent;
|
|
499
|
-
case 'decodeURIComponent':
|
|
500
|
-
return decodeURIComponent;
|
|
501
|
-
default:
|
|
502
|
-
return undefined;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* Get allowed globals based on security level.
|
|
507
|
-
* This mirrors the AST guard's getAgentScriptGlobals for defense-in-depth.
|
|
508
|
-
*/
|
|
509
|
-
function getAllowedGlobalsForSecurityLevel(securityLevel) {
|
|
510
|
-
// STRICT globals - absolute minimum
|
|
511
|
-
const strictGlobals = [
|
|
512
|
-
'callTool',
|
|
513
|
-
'__safe_callTool',
|
|
514
|
-
'Math',
|
|
515
|
-
'JSON',
|
|
516
|
-
'Array',
|
|
517
|
-
'Object',
|
|
518
|
-
'String',
|
|
519
|
-
'Number',
|
|
520
|
-
'Date',
|
|
521
|
-
'undefined',
|
|
522
|
-
'NaN',
|
|
523
|
-
'Infinity',
|
|
524
|
-
'__safe_forOf',
|
|
525
|
-
'__safe_for',
|
|
526
|
-
'__safe_while',
|
|
527
|
-
'__safe_doWhile',
|
|
528
|
-
'__maxIterations',
|
|
529
|
-
];
|
|
530
|
-
// SECURE globals - adds safe utility functions (pure, no side effects)
|
|
531
|
-
const secureGlobals = [
|
|
532
|
-
...strictGlobals,
|
|
533
|
-
'parseInt',
|
|
534
|
-
'parseFloat',
|
|
535
|
-
'isNaN',
|
|
536
|
-
'isFinite',
|
|
537
|
-
'encodeURI',
|
|
538
|
-
'decodeURI',
|
|
539
|
-
'encodeURIComponent',
|
|
540
|
-
'decodeURIComponent',
|
|
541
|
-
];
|
|
542
|
-
// STANDARD globals - same as SECURE (room for future expansion)
|
|
543
|
-
const standardGlobals = [...secureGlobals];
|
|
544
|
-
// PERMISSIVE globals - adds console for debugging
|
|
545
|
-
const permissiveGlobals = [...standardGlobals, 'console', '__safe_console'];
|
|
546
|
-
switch (securityLevel) {
|
|
547
|
-
case 'PERMISSIVE':
|
|
548
|
-
return permissiveGlobals;
|
|
549
|
-
case 'STANDARD':
|
|
550
|
-
return standardGlobals;
|
|
551
|
-
case 'SECURE':
|
|
552
|
-
return secureGlobals;
|
|
553
|
-
case 'STRICT':
|
|
554
|
-
default:
|
|
555
|
-
return strictGlobals;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Create a security-level-aware sandbox for code execution.
|
|
560
|
-
*
|
|
561
|
-
* IMPORTANT: This function enforces the same allowed globals as the AST guard
|
|
562
|
-
* for defense-in-depth. If code bypasses AST validation, the sandbox still
|
|
563
|
-
* blocks access to dangerous globals.
|
|
564
|
-
*/
|
|
565
|
-
function createSandbox(requestId, config) {
|
|
566
|
-
const sandbox = Object.create(null);
|
|
567
|
-
// Get allowed globals based on security level
|
|
568
|
-
const allowedGlobals = getAllowedGlobalsForSecurityLevel(config.securityLevel);
|
|
569
|
-
// Only add globals that are in the allowed list
|
|
570
|
-
for (const name of allowedGlobals) {
|
|
571
|
-
const value = getGlobalValue(name, requestId, config);
|
|
572
|
-
if (value !== undefined || name === 'undefined') {
|
|
573
|
-
sandbox[name] = value;
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
// Add custom globals if provided (these are assumed to be validated by AST guard)
|
|
577
|
-
if (config.globals) {
|
|
578
|
-
for (const [key, value] of Object.entries(config.globals)) {
|
|
579
|
-
// Only allow serializable values (functions are not allowed)
|
|
580
|
-
if (typeof value !== 'function') {
|
|
581
|
-
sandbox[key] = (0, safe_deserialize_1.sanitizeObject)(value);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
return sandbox;
|
|
586
|
-
}
|
|
587
|
-
// ============================================================================
|
|
588
|
-
// Safe Runtime Functions
|
|
589
|
-
// ============================================================================
|
|
590
|
-
function createProxiedCallTool(requestId, config) {
|
|
591
|
-
return async function __safe_callTool(toolName, args) {
|
|
592
|
-
// Check if aborted
|
|
593
|
-
if (currentExecution?.aborted) {
|
|
594
|
-
throw new Error('Execution aborted');
|
|
595
|
-
}
|
|
596
|
-
// Increment tool call count
|
|
597
|
-
if (currentExecution) {
|
|
598
|
-
currentExecution.stats.toolCallCount++;
|
|
599
|
-
}
|
|
600
|
-
// Check tool call limit
|
|
601
|
-
if (currentExecution && currentExecution.stats.toolCallCount > config.maxToolCalls) {
|
|
602
|
-
throw new Error(`Maximum tool call limit exceeded (${config.maxToolCalls}). ` + `This limit prevents runaway script execution.`);
|
|
603
|
-
}
|
|
604
|
-
// Validate inputs
|
|
605
|
-
if (typeof toolName !== 'string' || !toolName) {
|
|
606
|
-
throw new TypeError('Tool name must be a non-empty string');
|
|
607
|
-
}
|
|
608
|
-
if (typeof args !== 'object' || args === null || Array.isArray(args)) {
|
|
609
|
-
throw new TypeError('Tool arguments must be an object');
|
|
610
|
-
}
|
|
611
|
-
// Generate unique call ID
|
|
612
|
-
const callId = `${requestId}-${Date.now()}-${crypto_1.default.randomUUID()}`;
|
|
613
|
-
return new Promise((resolve, reject) => {
|
|
614
|
-
pendingToolCalls.set(callId, { resolve, reject });
|
|
615
|
-
sendMessage({
|
|
616
|
-
type: 'tool-call',
|
|
617
|
-
requestId,
|
|
618
|
-
callId,
|
|
619
|
-
toolName,
|
|
620
|
-
args: (0, safe_deserialize_1.sanitizeObject)(args),
|
|
621
|
-
});
|
|
622
|
-
});
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
function createSafeForOf() {
|
|
626
|
-
return function* __safe_forOf(iterable) {
|
|
627
|
-
let iterations = 0;
|
|
628
|
-
for (const item of iterable) {
|
|
629
|
-
// Check if aborted
|
|
630
|
-
if (currentExecution?.aborted) {
|
|
631
|
-
throw new Error('Execution aborted');
|
|
632
|
-
}
|
|
633
|
-
// Increment iteration count
|
|
634
|
-
iterations++;
|
|
635
|
-
if (currentExecution) {
|
|
636
|
-
currentExecution.stats.iterationCount++;
|
|
637
|
-
}
|
|
638
|
-
// Check iteration limit
|
|
639
|
-
const maxIterations = currentExecution?.config.maxIterations ?? 10000;
|
|
640
|
-
if (iterations > maxIterations) {
|
|
641
|
-
throw new Error(`Maximum iteration limit exceeded (${maxIterations}). ` + `This limit prevents infinite loops.`);
|
|
642
|
-
}
|
|
643
|
-
yield item;
|
|
644
|
-
}
|
|
645
|
-
};
|
|
646
|
-
}
|
|
647
|
-
function createSafeFor() {
|
|
648
|
-
return function __safe_for(init, test, update, body) {
|
|
649
|
-
let iterations = 0;
|
|
650
|
-
init();
|
|
651
|
-
while (test()) {
|
|
652
|
-
// Check if aborted
|
|
653
|
-
if (currentExecution?.aborted) {
|
|
654
|
-
throw new Error('Execution aborted');
|
|
655
|
-
}
|
|
656
|
-
// Increment iteration count
|
|
657
|
-
iterations++;
|
|
658
|
-
if (currentExecution) {
|
|
659
|
-
currentExecution.stats.iterationCount++;
|
|
660
|
-
}
|
|
661
|
-
// Check iteration limit
|
|
662
|
-
const maxIterations = currentExecution?.config.maxIterations ?? 10000;
|
|
663
|
-
if (iterations > maxIterations) {
|
|
664
|
-
throw new Error(`Maximum iteration limit exceeded (${maxIterations}). ` + `This limit prevents infinite loops.`);
|
|
665
|
-
}
|
|
666
|
-
body();
|
|
667
|
-
update();
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
}
|
|
671
|
-
function createSafeWhile() {
|
|
672
|
-
return function __safe_while(test, body) {
|
|
673
|
-
let iterations = 0;
|
|
674
|
-
while (test()) {
|
|
675
|
-
// Check if aborted
|
|
676
|
-
if (currentExecution?.aborted) {
|
|
677
|
-
throw new Error('Execution aborted');
|
|
678
|
-
}
|
|
679
|
-
// Increment iteration count
|
|
680
|
-
iterations++;
|
|
681
|
-
if (currentExecution) {
|
|
682
|
-
currentExecution.stats.iterationCount++;
|
|
683
|
-
}
|
|
684
|
-
// Check iteration limit
|
|
685
|
-
const maxIterations = currentExecution?.config.maxIterations ?? 10000;
|
|
686
|
-
if (iterations > maxIterations) {
|
|
687
|
-
throw new Error(`Maximum iteration limit exceeded (${maxIterations}). ` + `This limit prevents infinite loops.`);
|
|
688
|
-
}
|
|
689
|
-
body();
|
|
690
|
-
}
|
|
691
|
-
};
|
|
692
|
-
}
|
|
693
|
-
function createSafeConsole(requestId, config) {
|
|
694
|
-
let totalBytes = 0;
|
|
695
|
-
let callCount = 0;
|
|
696
|
-
function safeLog(level, args) {
|
|
697
|
-
// Check call limit
|
|
698
|
-
callCount++;
|
|
699
|
-
if (callCount > config.maxConsoleCalls) {
|
|
700
|
-
throw new Error(`Console call limit exceeded (${config.maxConsoleCalls})`);
|
|
701
|
-
}
|
|
702
|
-
// Serialize and check size
|
|
703
|
-
const serialized = args.map((a) => {
|
|
704
|
-
try {
|
|
705
|
-
return JSON.stringify((0, safe_deserialize_1.sanitizeObject)(a));
|
|
706
|
-
}
|
|
707
|
-
catch {
|
|
708
|
-
return String(a);
|
|
709
|
-
}
|
|
710
|
-
});
|
|
711
|
-
const bytes = serialized.reduce((sum, s) => sum + (s?.length ?? 0), 0);
|
|
712
|
-
totalBytes += bytes;
|
|
713
|
-
if (totalBytes > config.maxConsoleOutputBytes) {
|
|
714
|
-
throw new Error(`Console output limit exceeded (${config.maxConsoleOutputBytes} bytes)`);
|
|
715
|
-
}
|
|
716
|
-
// Send to main thread
|
|
717
|
-
sendMessage({
|
|
718
|
-
type: 'console',
|
|
719
|
-
requestId,
|
|
720
|
-
level,
|
|
721
|
-
args: serialized,
|
|
722
|
-
});
|
|
723
|
-
}
|
|
724
|
-
return {
|
|
725
|
-
log: (...args) => safeLog('log', args),
|
|
726
|
-
warn: (...args) => safeLog('warn', args),
|
|
727
|
-
error: (...args) => safeLog('error', args),
|
|
728
|
-
info: (...args) => safeLog('info', args),
|
|
729
|
-
};
|
|
730
|
-
}
|
|
731
|
-
// ============================================================================
|
|
732
|
-
// Utility Functions
|
|
733
|
-
// ============================================================================
|
|
734
|
-
function sendMessage(msg) {
|
|
735
|
-
if (!port) {
|
|
736
|
-
throw new Error('Worker port not initialized');
|
|
737
|
-
}
|
|
738
|
-
port.postMessage((0, safe_deserialize_1.safeSerialize)(msg));
|
|
739
|
-
}
|
|
740
|
-
function serializeError(error, sanitizeStackTraces) {
|
|
741
|
-
// Handle thrown strings (from iteration limit checks in transformed loops)
|
|
742
|
-
if (typeof error === 'string') {
|
|
743
|
-
return { name: 'Error', message: error };
|
|
744
|
-
}
|
|
745
|
-
const err = error;
|
|
746
|
-
const serialized = {
|
|
747
|
-
name: err.name || 'Error',
|
|
748
|
-
message: err.message || 'Unknown error',
|
|
749
|
-
};
|
|
750
|
-
if (err.code) {
|
|
751
|
-
serialized.code = err.code;
|
|
752
|
-
}
|
|
753
|
-
if (err.stack && !sanitizeStackTraces) {
|
|
754
|
-
serialized.stack = err.stack;
|
|
755
|
-
}
|
|
756
|
-
else if (err.stack && sanitizeStackTraces) {
|
|
757
|
-
// Basic sanitization - remove file paths
|
|
758
|
-
serialized.stack = err.stack
|
|
759
|
-
.split('\n')
|
|
760
|
-
.slice(0, 5)
|
|
761
|
-
.map((line) => line.replace(/\(.*?:\d+:\d+\)/g, '(...)'))
|
|
762
|
-
.join('\n');
|
|
763
|
-
}
|
|
764
|
-
return serialized;
|
|
765
|
-
}
|
|
766
|
-
// ============================================================================
|
|
767
|
-
// Signal Ready
|
|
768
|
-
// ============================================================================
|
|
769
|
-
sendMessage({ type: 'ready' });
|
|
770
|
-
//# sourceMappingURL=worker-script.js.map
|