@vorionsys/cognigate 1.0.0 → 1.0.2
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 +45 -0
- package/LICENSE +190 -0
- package/README.md +460 -270
- package/dist/chunk-OIPPFRDP.js +35 -0
- package/dist/index.cjs +310 -54
- package/dist/index.d.cts +84 -318
- package/dist/index.d.ts +272 -0
- package/dist/index.js +279 -47
- package/dist/proof-bridge-BBmb7kVP.d.cts +394 -0
- package/dist/proof-bridge-BBmb7kVP.d.ts +394 -0
- package/dist/proof-bridge-CkzbawwC.d.cts +394 -0
- package/dist/proof-bridge-CkzbawwC.d.ts +394 -0
- package/dist/proof-bridge.cjs +59 -0
- package/dist/proof-bridge.d.cts +3 -0
- package/dist/proof-bridge.d.ts +3 -0
- package/dist/proof-bridge.js +6 -0
- package/package.json +77 -54
- package/src/__tests__/client.test.ts +0 -162
- package/src/client.ts +0 -421
- package/src/index.ts +0 -76
- package/src/types.ts +0 -272
- package/src/webhooks.ts +0 -146
- package/tsconfig.json +0 -25
- package/vitest.config.ts +0 -14
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/proof-bridge.ts
|
|
2
|
+
function createProofBridge(config) {
|
|
3
|
+
const { proofPlane, webhookRouter } = config;
|
|
4
|
+
let connected = true;
|
|
5
|
+
const handler = async (event) => {
|
|
6
|
+
if (!connected) return;
|
|
7
|
+
const payload = event.payload;
|
|
8
|
+
const now = /* @__PURE__ */ new Date();
|
|
9
|
+
const decision = {
|
|
10
|
+
decisionId: payload.decisionId ?? event.id,
|
|
11
|
+
intentId: payload.intentId ?? "",
|
|
12
|
+
agentId: payload.agentId ?? event.entityId,
|
|
13
|
+
correlationId: payload.correlationId ?? event.id,
|
|
14
|
+
permitted: payload.permitted ?? payload.decision === "ALLOW",
|
|
15
|
+
trustBand: payload.trustBand ?? 4,
|
|
16
|
+
trustScore: payload.trustScore ?? 0,
|
|
17
|
+
reasoning: Array.isArray(payload.reasoning) ? payload.reasoning : typeof payload.reasoning === "string" ? [payload.reasoning] : [],
|
|
18
|
+
decidedAt: payload.decidedAt ? new Date(payload.decidedAt) : now,
|
|
19
|
+
expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : new Date(now.getTime() + 24 * 60 * 60 * 1e3),
|
|
20
|
+
latencyMs: payload.latencyMs ?? 0,
|
|
21
|
+
version: payload.version ?? 1
|
|
22
|
+
};
|
|
23
|
+
await proofPlane.logDecisionMade(decision, decision.correlationId);
|
|
24
|
+
};
|
|
25
|
+
webhookRouter.on("governance.decision", handler);
|
|
26
|
+
return {
|
|
27
|
+
disconnect: () => {
|
|
28
|
+
connected = false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
createProofBridge
|
|
35
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -25,10 +25,12 @@ __export(index_exports, {
|
|
|
25
25
|
CognigateError: () => CognigateError,
|
|
26
26
|
GovernanceResultSchema: () => GovernanceResultSchema,
|
|
27
27
|
ProofRecordSchema: () => ProofRecordSchema,
|
|
28
|
-
TIER_THRESHOLDS: () => TIER_THRESHOLDS,
|
|
28
|
+
TIER_THRESHOLDS: () => import_shared_constants.TIER_THRESHOLDS,
|
|
29
29
|
TrustStatusSchema: () => TrustStatusSchema,
|
|
30
|
-
TrustTier: () => TrustTier,
|
|
30
|
+
TrustTier: () => import_shared_constants.TrustTier,
|
|
31
31
|
WebhookRouter: () => WebhookRouter,
|
|
32
|
+
WorkerSandbox: () => WorkerSandbox,
|
|
33
|
+
createProofBridge: () => createProofBridge,
|
|
32
34
|
parseWebhookPayload: () => parseWebhookPayload,
|
|
33
35
|
verifyWebhookSignature: () => verifyWebhookSignature,
|
|
34
36
|
z: () => import_zod2.z
|
|
@@ -37,31 +39,11 @@ module.exports = __toCommonJS(index_exports);
|
|
|
37
39
|
|
|
38
40
|
// src/types.ts
|
|
39
41
|
var import_zod = require("zod");
|
|
40
|
-
var
|
|
41
|
-
TrustTier2[TrustTier2["T0_SANDBOX"] = 0] = "T0_SANDBOX";
|
|
42
|
-
TrustTier2[TrustTier2["T1_OBSERVED"] = 1] = "T1_OBSERVED";
|
|
43
|
-
TrustTier2[TrustTier2["T2_PROVISIONAL"] = 2] = "T2_PROVISIONAL";
|
|
44
|
-
TrustTier2[TrustTier2["T3_VERIFIED"] = 3] = "T3_VERIFIED";
|
|
45
|
-
TrustTier2[TrustTier2["T4_OPERATIONAL"] = 4] = "T4_OPERATIONAL";
|
|
46
|
-
TrustTier2[TrustTier2["T5_TRUSTED"] = 5] = "T5_TRUSTED";
|
|
47
|
-
TrustTier2[TrustTier2["T6_CERTIFIED"] = 6] = "T6_CERTIFIED";
|
|
48
|
-
TrustTier2[TrustTier2["T7_AUTONOMOUS"] = 7] = "T7_AUTONOMOUS";
|
|
49
|
-
return TrustTier2;
|
|
50
|
-
})(TrustTier || {});
|
|
51
|
-
var TIER_THRESHOLDS = {
|
|
52
|
-
[0 /* T0_SANDBOX */]: { min: 0, max: 199, name: "Sandbox" },
|
|
53
|
-
[1 /* T1_OBSERVED */]: { min: 200, max: 349, name: "Observed" },
|
|
54
|
-
[2 /* T2_PROVISIONAL */]: { min: 350, max: 499, name: "Provisional" },
|
|
55
|
-
[3 /* T3_VERIFIED */]: { min: 500, max: 649, name: "Verified" },
|
|
56
|
-
[4 /* T4_OPERATIONAL */]: { min: 650, max: 799, name: "Operational" },
|
|
57
|
-
[5 /* T5_TRUSTED */]: { min: 800, max: 875, name: "Trusted" },
|
|
58
|
-
[6 /* T6_CERTIFIED */]: { min: 876, max: 949, name: "Certified" },
|
|
59
|
-
[7 /* T7_AUTONOMOUS */]: { min: 950, max: 1e3, name: "Autonomous" }
|
|
60
|
-
};
|
|
42
|
+
var import_shared_constants = require("@vorionsys/shared-constants");
|
|
61
43
|
var TrustStatusSchema = import_zod.z.object({
|
|
62
44
|
entityId: import_zod.z.string(),
|
|
63
45
|
trustScore: import_zod.z.number().min(0).max(1e3),
|
|
64
|
-
trustTier: import_zod.z.nativeEnum(TrustTier),
|
|
46
|
+
trustTier: import_zod.z.nativeEnum(import_shared_constants.TrustTier),
|
|
65
47
|
tierName: import_zod.z.string(),
|
|
66
48
|
capabilities: import_zod.z.array(import_zod.z.string()),
|
|
67
49
|
factorScores: import_zod.z.record(import_zod.z.string(), import_zod.z.number()),
|
|
@@ -72,7 +54,7 @@ var TrustStatusSchema = import_zod.z.object({
|
|
|
72
54
|
var GovernanceResultSchema = import_zod.z.object({
|
|
73
55
|
decision: import_zod.z.enum(["ALLOW", "DENY", "ESCALATE", "DEGRADE"]),
|
|
74
56
|
trustScore: import_zod.z.number(),
|
|
75
|
-
trustTier: import_zod.z.nativeEnum(TrustTier),
|
|
57
|
+
trustTier: import_zod.z.nativeEnum(import_shared_constants.TrustTier),
|
|
76
58
|
grantedCapabilities: import_zod.z.array(import_zod.z.string()),
|
|
77
59
|
deniedCapabilities: import_zod.z.array(import_zod.z.string()),
|
|
78
60
|
reasoning: import_zod.z.string(),
|
|
@@ -100,7 +82,7 @@ var AgentSchema = import_zod.z.object({
|
|
|
100
82
|
description: import_zod.z.string(),
|
|
101
83
|
ownerId: import_zod.z.string(),
|
|
102
84
|
trustScore: import_zod.z.number(),
|
|
103
|
-
trustTier: import_zod.z.nativeEnum(TrustTier),
|
|
85
|
+
trustTier: import_zod.z.nativeEnum(import_shared_constants.TrustTier),
|
|
104
86
|
status: import_zod.z.enum(["ACTIVE", "PAUSED", "SUSPENDED", "TERMINATED"]),
|
|
105
87
|
capabilities: import_zod.z.array(import_zod.z.string()),
|
|
106
88
|
executions: import_zod.z.number(),
|
|
@@ -206,26 +188,26 @@ var Cognigate = class {
|
|
|
206
188
|
* Get tier from trust score
|
|
207
189
|
*/
|
|
208
190
|
static getTierFromScore(score) {
|
|
209
|
-
if (score >=
|
|
210
|
-
if (score >= 876) return
|
|
211
|
-
if (score >= 800) return
|
|
212
|
-
if (score >= 650) return
|
|
213
|
-
if (score >= 500) return
|
|
214
|
-
if (score >= 350) return
|
|
215
|
-
if (score >= 200) return
|
|
216
|
-
return
|
|
191
|
+
if (score >= 951) return import_shared_constants.TrustTier.T7_AUTONOMOUS;
|
|
192
|
+
if (score >= 876) return import_shared_constants.TrustTier.T6_CERTIFIED;
|
|
193
|
+
if (score >= 800) return import_shared_constants.TrustTier.T5_TRUSTED;
|
|
194
|
+
if (score >= 650) return import_shared_constants.TrustTier.T4_STANDARD;
|
|
195
|
+
if (score >= 500) return import_shared_constants.TrustTier.T3_MONITORED;
|
|
196
|
+
if (score >= 350) return import_shared_constants.TrustTier.T2_PROVISIONAL;
|
|
197
|
+
if (score >= 200) return import_shared_constants.TrustTier.T1_OBSERVED;
|
|
198
|
+
return import_shared_constants.TrustTier.T0_SANDBOX;
|
|
217
199
|
}
|
|
218
200
|
/**
|
|
219
201
|
* Get tier name
|
|
220
202
|
*/
|
|
221
203
|
static getTierName(tier) {
|
|
222
|
-
return TIER_THRESHOLDS[tier].name;
|
|
204
|
+
return import_shared_constants.TIER_THRESHOLDS[tier].name;
|
|
223
205
|
}
|
|
224
206
|
/**
|
|
225
207
|
* Get tier thresholds
|
|
226
208
|
*/
|
|
227
209
|
static getTierThresholds(tier) {
|
|
228
|
-
return TIER_THRESHOLDS[tier];
|
|
210
|
+
return import_shared_constants.TIER_THRESHOLDS[tier];
|
|
229
211
|
}
|
|
230
212
|
};
|
|
231
213
|
var AgentsClient = class {
|
|
@@ -418,21 +400,18 @@ async function verifyWebhookSignature(payload, signature, secret) {
|
|
|
418
400
|
const expectedSignature = bufferToHex(signatureBuffer);
|
|
419
401
|
return timingSafeEqual(signature, expectedSignature);
|
|
420
402
|
}
|
|
421
|
-
function parseWebhookPayload(body, signature, secret) {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
reject(new Error("Invalid webhook payload"));
|
|
434
|
-
}
|
|
435
|
-
});
|
|
403
|
+
async function parseWebhookPayload(body, signature, secret) {
|
|
404
|
+
const isValid = await verifyWebhookSignature(body, signature, secret);
|
|
405
|
+
if (!isValid) {
|
|
406
|
+
throw new Error("Invalid webhook signature");
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
const event = JSON.parse(body);
|
|
410
|
+
event.timestamp = new Date(event.timestamp);
|
|
411
|
+
return event;
|
|
412
|
+
} catch {
|
|
413
|
+
throw new Error("Invalid webhook payload");
|
|
414
|
+
}
|
|
436
415
|
}
|
|
437
416
|
var WebhookRouter = class {
|
|
438
417
|
handlers = /* @__PURE__ */ new Map();
|
|
@@ -469,15 +448,15 @@ var WebhookRouter = class {
|
|
|
469
448
|
* Create an Express/Connect compatible middleware
|
|
470
449
|
*/
|
|
471
450
|
middleware(secret) {
|
|
472
|
-
return async (req, res
|
|
451
|
+
return async (req, res) => {
|
|
473
452
|
try {
|
|
474
453
|
const signature = req.headers["x-cognigate-signature"];
|
|
475
454
|
const body = typeof req.body === "string" ? req.body : JSON.stringify(req.body);
|
|
476
455
|
const event = await parseWebhookPayload(body, signature, secret);
|
|
477
456
|
await this.handle(event);
|
|
478
457
|
res.status(200).json({ received: true });
|
|
479
|
-
} catch (
|
|
480
|
-
res.status(400).json({ error:
|
|
458
|
+
} catch (err) {
|
|
459
|
+
res.status(400).json({ error: err.message });
|
|
481
460
|
}
|
|
482
461
|
};
|
|
483
462
|
}
|
|
@@ -496,6 +475,281 @@ function timingSafeEqual(a, b) {
|
|
|
496
475
|
return result === 0;
|
|
497
476
|
}
|
|
498
477
|
|
|
478
|
+
// src/proof-bridge.ts
|
|
479
|
+
function createProofBridge(config) {
|
|
480
|
+
const { proofPlane, webhookRouter } = config;
|
|
481
|
+
let connected = true;
|
|
482
|
+
const handler = async (event) => {
|
|
483
|
+
if (!connected) return;
|
|
484
|
+
const payload = event.payload;
|
|
485
|
+
const now = /* @__PURE__ */ new Date();
|
|
486
|
+
const decision = {
|
|
487
|
+
decisionId: payload.decisionId ?? event.id,
|
|
488
|
+
intentId: payload.intentId ?? "",
|
|
489
|
+
agentId: payload.agentId ?? event.entityId,
|
|
490
|
+
correlationId: payload.correlationId ?? event.id,
|
|
491
|
+
permitted: payload.permitted ?? payload.decision === "ALLOW",
|
|
492
|
+
trustBand: payload.trustBand ?? 4,
|
|
493
|
+
trustScore: payload.trustScore ?? 0,
|
|
494
|
+
reasoning: Array.isArray(payload.reasoning) ? payload.reasoning : typeof payload.reasoning === "string" ? [payload.reasoning] : [],
|
|
495
|
+
decidedAt: payload.decidedAt ? new Date(payload.decidedAt) : now,
|
|
496
|
+
expiresAt: payload.expiresAt ? new Date(payload.expiresAt) : new Date(now.getTime() + 24 * 60 * 60 * 1e3),
|
|
497
|
+
latencyMs: payload.latencyMs ?? 0,
|
|
498
|
+
version: payload.version ?? 1
|
|
499
|
+
};
|
|
500
|
+
await proofPlane.logDecisionMade(decision, decision.correlationId);
|
|
501
|
+
};
|
|
502
|
+
webhookRouter.on("governance.decision", handler);
|
|
503
|
+
return {
|
|
504
|
+
disconnect: () => {
|
|
505
|
+
connected = false;
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// src/sandbox/worker-sandbox.ts
|
|
511
|
+
var import_node_worker_threads = require("worker_threads");
|
|
512
|
+
var DEFAULT_TIMEOUT2 = 3e4;
|
|
513
|
+
var DEFAULT_MEMORY_LIMIT_MB = 64;
|
|
514
|
+
var WORKER_SCRIPT = `
|
|
515
|
+
const { parentPort, workerData } = require('node:worker_threads');
|
|
516
|
+
const vm = require('node:vm');
|
|
517
|
+
|
|
518
|
+
const { code, context } = workerData;
|
|
519
|
+
const startTime = performance.now();
|
|
520
|
+
const memBefore = process.memoryUsage().heapUsed;
|
|
521
|
+
|
|
522
|
+
// Build restricted globals for the vm context
|
|
523
|
+
const safeGlobals = {
|
|
524
|
+
console: {
|
|
525
|
+
log: function() {},
|
|
526
|
+
warn: function() {},
|
|
527
|
+
error: function() {},
|
|
528
|
+
info: function() {},
|
|
529
|
+
},
|
|
530
|
+
JSON: JSON,
|
|
531
|
+
Math: Math,
|
|
532
|
+
Date: Date,
|
|
533
|
+
Array: Array,
|
|
534
|
+
Object: Object,
|
|
535
|
+
String: String,
|
|
536
|
+
Number: Number,
|
|
537
|
+
Boolean: Boolean,
|
|
538
|
+
Map: Map,
|
|
539
|
+
Set: Set,
|
|
540
|
+
WeakMap: WeakMap,
|
|
541
|
+
WeakSet: WeakSet,
|
|
542
|
+
Promise: Promise,
|
|
543
|
+
Symbol: Symbol,
|
|
544
|
+
RegExp: RegExp,
|
|
545
|
+
Error: Error,
|
|
546
|
+
TypeError: TypeError,
|
|
547
|
+
RangeError: RangeError,
|
|
548
|
+
SyntaxError: SyntaxError,
|
|
549
|
+
URIError: URIError,
|
|
550
|
+
parseInt: parseInt,
|
|
551
|
+
parseFloat: parseFloat,
|
|
552
|
+
isNaN: isNaN,
|
|
553
|
+
isFinite: isFinite,
|
|
554
|
+
encodeURIComponent: encodeURIComponent,
|
|
555
|
+
decodeURIComponent: decodeURIComponent,
|
|
556
|
+
encodeURI: encodeURI,
|
|
557
|
+
decodeURI: decodeURI,
|
|
558
|
+
undefined: undefined,
|
|
559
|
+
NaN: NaN,
|
|
560
|
+
Infinity: Infinity,
|
|
561
|
+
setTimeout: function(fn, ms) { return setTimeout(fn, Math.min(ms, 5000)); },
|
|
562
|
+
clearTimeout: clearTimeout,
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const vmContext = vm.createContext(safeGlobals);
|
|
566
|
+
|
|
567
|
+
// Wrap code in an async IIFE to support await and return
|
|
568
|
+
const wrappedCode = '(async () => { ' + code + ' })()';
|
|
569
|
+
|
|
570
|
+
async function run() {
|
|
571
|
+
try {
|
|
572
|
+
const script = new vm.Script(wrappedCode, {
|
|
573
|
+
filename: 'sandbox-' + context.agentId + '.js',
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
const result = await script.runInContext(vmContext, {
|
|
577
|
+
timeout: context.timeout,
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
const durationMs = performance.now() - startTime;
|
|
581
|
+
const memAfter = process.memoryUsage().heapUsed;
|
|
582
|
+
|
|
583
|
+
parentPort.postMessage({
|
|
584
|
+
type: 'result',
|
|
585
|
+
output: result,
|
|
586
|
+
durationMs: durationMs,
|
|
587
|
+
memoryUsedBytes: Math.max(0, memAfter - memBefore),
|
|
588
|
+
});
|
|
589
|
+
} catch (err) {
|
|
590
|
+
const durationMs = performance.now() - startTime;
|
|
591
|
+
const memAfter = process.memoryUsage().heapUsed;
|
|
592
|
+
|
|
593
|
+
parentPort.postMessage({
|
|
594
|
+
type: 'error',
|
|
595
|
+
error: err && err.message ? err.message : String(err),
|
|
596
|
+
durationMs: durationMs,
|
|
597
|
+
memoryUsedBytes: Math.max(0, memAfter - memBefore),
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
run();
|
|
603
|
+
`;
|
|
604
|
+
var WorkerSandbox = class {
|
|
605
|
+
worker = null;
|
|
606
|
+
isShutdown = false;
|
|
607
|
+
/**
|
|
608
|
+
* Execute code in an isolated worker thread.
|
|
609
|
+
*
|
|
610
|
+
* Spawns a new worker for each execution to ensure full isolation.
|
|
611
|
+
* The worker has memory limits enforced by V8 via `resourceLimits`,
|
|
612
|
+
* a vm-level timeout for synchronous code, and a main-thread timeout
|
|
613
|
+
* as a fallback for async code or hangs.
|
|
614
|
+
*
|
|
615
|
+
* @param code - JavaScript code string to execute inside the sandbox.
|
|
616
|
+
* The code is wrapped in an async IIFE, so `return` and `await` are valid.
|
|
617
|
+
* @param context - Execution context with identity and resource constraints
|
|
618
|
+
* @returns Promise resolving to a SandboxResult
|
|
619
|
+
*/
|
|
620
|
+
async execute(code, context) {
|
|
621
|
+
if (this.isShutdown) {
|
|
622
|
+
return {
|
|
623
|
+
success: false,
|
|
624
|
+
output: void 0,
|
|
625
|
+
error: "Sandbox has been shut down",
|
|
626
|
+
durationMs: 0,
|
|
627
|
+
memoryUsedBytes: 0
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
const timeout = context.timeout || DEFAULT_TIMEOUT2;
|
|
631
|
+
const memoryLimitMb = context.memoryLimitMb || DEFAULT_MEMORY_LIMIT_MB;
|
|
632
|
+
const startTime = performance.now();
|
|
633
|
+
return new Promise((resolve) => {
|
|
634
|
+
let settled = false;
|
|
635
|
+
let timeoutId = null;
|
|
636
|
+
const settle = (result) => {
|
|
637
|
+
if (settled) return;
|
|
638
|
+
settled = true;
|
|
639
|
+
if (timeoutId) {
|
|
640
|
+
clearTimeout(timeoutId);
|
|
641
|
+
timeoutId = null;
|
|
642
|
+
}
|
|
643
|
+
resolve(result);
|
|
644
|
+
};
|
|
645
|
+
try {
|
|
646
|
+
const worker = new import_node_worker_threads.Worker(WORKER_SCRIPT, {
|
|
647
|
+
eval: true,
|
|
648
|
+
workerData: {
|
|
649
|
+
code,
|
|
650
|
+
context: {
|
|
651
|
+
tenantId: context.tenantId,
|
|
652
|
+
agentId: context.agentId,
|
|
653
|
+
trustLevel: context.trustLevel ?? 0,
|
|
654
|
+
allowedModules: context.allowedModules ?? [],
|
|
655
|
+
timeout,
|
|
656
|
+
memoryLimitMb
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
resourceLimits: {
|
|
660
|
+
maxOldGenerationSizeMb: memoryLimitMb,
|
|
661
|
+
maxYoungGenerationSizeMb: Math.max(1, Math.floor(memoryLimitMb / 4)),
|
|
662
|
+
codeRangeSizeMb: Math.max(1, Math.floor(memoryLimitMb / 8))
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
this.worker = worker;
|
|
666
|
+
timeoutId = setTimeout(() => {
|
|
667
|
+
const durationMs = performance.now() - startTime;
|
|
668
|
+
worker.terminate().catch(() => {
|
|
669
|
+
});
|
|
670
|
+
settle({
|
|
671
|
+
success: false,
|
|
672
|
+
output: void 0,
|
|
673
|
+
error: `Execution timed out after ${timeout}ms`,
|
|
674
|
+
durationMs,
|
|
675
|
+
memoryUsedBytes: 0
|
|
676
|
+
});
|
|
677
|
+
}, timeout + 500);
|
|
678
|
+
worker.on("message", (message) => {
|
|
679
|
+
if (message.type === "result") {
|
|
680
|
+
settle({
|
|
681
|
+
success: true,
|
|
682
|
+
output: message.output,
|
|
683
|
+
durationMs: message.durationMs,
|
|
684
|
+
memoryUsedBytes: message.memoryUsedBytes
|
|
685
|
+
});
|
|
686
|
+
} else if (message.type === "error") {
|
|
687
|
+
settle({
|
|
688
|
+
success: false,
|
|
689
|
+
output: void 0,
|
|
690
|
+
error: message.error,
|
|
691
|
+
durationMs: message.durationMs,
|
|
692
|
+
memoryUsedBytes: message.memoryUsedBytes
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
worker.terminate().catch(() => {
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
worker.on("error", (error) => {
|
|
699
|
+
const durationMs = performance.now() - startTime;
|
|
700
|
+
settle({
|
|
701
|
+
success: false,
|
|
702
|
+
output: void 0,
|
|
703
|
+
error: error.message || "Worker error",
|
|
704
|
+
durationMs,
|
|
705
|
+
memoryUsedBytes: 0
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
worker.on("exit", (exitCode) => {
|
|
709
|
+
if (exitCode !== 0) {
|
|
710
|
+
const durationMs = performance.now() - startTime;
|
|
711
|
+
settle({
|
|
712
|
+
success: false,
|
|
713
|
+
output: void 0,
|
|
714
|
+
error: `Worker exited with code ${exitCode} (possible memory limit exceeded)`,
|
|
715
|
+
durationMs,
|
|
716
|
+
memoryUsedBytes: 0
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
this.worker = null;
|
|
720
|
+
});
|
|
721
|
+
} catch (err) {
|
|
722
|
+
const durationMs = performance.now() - startTime;
|
|
723
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
724
|
+
settle({
|
|
725
|
+
success: false,
|
|
726
|
+
output: void 0,
|
|
727
|
+
error: `Failed to spawn worker: ${errorMessage}`,
|
|
728
|
+
durationMs,
|
|
729
|
+
memoryUsedBytes: 0
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Gracefully shut down the sandbox.
|
|
736
|
+
* Terminates any running worker and prevents future executions.
|
|
737
|
+
*/
|
|
738
|
+
async shutdown() {
|
|
739
|
+
this.isShutdown = true;
|
|
740
|
+
if (this.worker) {
|
|
741
|
+
await this.worker.terminate();
|
|
742
|
+
this.worker = null;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Check whether the sandbox has been shut down.
|
|
747
|
+
*/
|
|
748
|
+
get terminated() {
|
|
749
|
+
return this.isShutdown;
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
|
|
499
753
|
// src/index.ts
|
|
500
754
|
var import_zod2 = require("zod");
|
|
501
755
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -509,6 +763,8 @@ var import_zod2 = require("zod");
|
|
|
509
763
|
TrustStatusSchema,
|
|
510
764
|
TrustTier,
|
|
511
765
|
WebhookRouter,
|
|
766
|
+
WorkerSandbox,
|
|
767
|
+
createProofBridge,
|
|
512
768
|
parseWebhookPayload,
|
|
513
769
|
verifyWebhookSignature,
|
|
514
770
|
z
|