@iso4/sandbox 0.1.0 → 0.2.1

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/dist/index.d.mts CHANGED
@@ -448,8 +448,22 @@ interface RunOptions {
448
448
  signal?: AbortSignal;
449
449
  filename?: string;
450
450
  }
451
- type RunResult = RunSuccess | RunFailure;
451
+ /**
452
+ * Why a run was aborted — whatever value was passed to the aborting
453
+ * `AbortController.abort(reason)` (or left `undefined`). Opaque to iso4 and
454
+ * surfaced verbatim on {@link RunAborted.reason}; a consumer built on top
455
+ * (e.g. a durable-workflow kernel expressing "suspend") gives it meaning.
456
+ */
457
+ type AbortReason = unknown;
458
+ type RunResult = RunSuccess | RunFailure | RunAborted;
459
+ /**
460
+ * `status` is the primary discriminant across all three outcomes; `ok` is kept
461
+ * as a convenience alias for `status === 'completed'`, so the common guard
462
+ * stays `if (result.ok)`. Reach for `switch (result.status)` when a deliberate
463
+ * abort must be told apart from a genuine failure.
464
+ */
452
465
  interface RunSuccess {
466
+ status: "completed";
453
467
  ok: true;
454
468
  exports: SandboxExports;
455
469
  stdout: string[];
@@ -457,8 +471,25 @@ interface RunSuccess {
457
471
  durationMs: number;
458
472
  }
459
473
  interface RunFailure {
474
+ status: "failed";
475
+ ok: false;
476
+ error: RunError;
477
+ stdout: string[];
478
+ stderr: string[];
479
+ durationMs: number;
480
+ }
481
+ /**
482
+ * The run was stopped by an abort — a pre-aborted or mid-run `signal` passed to
483
+ * `run()`, or a host handler that aborted the run. A deliberate outcome, not a
484
+ * crash: `error` is retained (its `code` is always `ERR_ABORTED`) so existing
485
+ * `!result.ok` / `error.code` checks keep working, and `reason` carries the
486
+ * value handed to `abort(reason)`.
487
+ */
488
+ interface RunAborted {
489
+ status: "aborted";
460
490
  ok: false;
461
491
  error: RunError;
492
+ reason?: AbortReason;
462
493
  stdout: string[];
463
494
  stderr: string[];
464
495
  durationMs: number;
package/dist/index.mjs CHANGED
@@ -225,6 +225,7 @@ function decodeRunCompletionPayload(buf) {
225
225
  return {
226
226
  runId,
227
227
  result: {
228
+ status: "completed",
228
229
  ok: true,
229
230
  exports: wireObjectToExports(exportsRaw),
230
231
  stdout,
@@ -247,6 +248,7 @@ function decodeRunCompletionPayload(buf) {
247
248
  return {
248
249
  runId,
249
250
  result: {
251
+ status: "failed",
250
252
  ok: false,
251
253
  error: {
252
254
  code,
@@ -694,16 +696,63 @@ function decodeBridgeCallPayload(buf) {
694
696
  };
695
697
  }
696
698
  /**
699
+ * Build a `BridgeErrorPayload` from whatever a host handler threw.
700
+ *
701
+ * Own-enumerable properties beyond `name`/`message`/`stack` travel as `data`;
702
+ * properties that cannot be wire-encoded (functions, symbols, cycles, …) are
703
+ * silently dropped, mirroring the sandbox → host direction (`RunError.data`).
704
+ * @param err
705
+ */
706
+ function bridgeErrorPayloadFromUnknown(err) {
707
+ try {
708
+ if (typeof err !== "object" || err === null) return {
709
+ name: "Error",
710
+ message: String(err)
711
+ };
712
+ const obj = err;
713
+ const name = typeof obj.name === "string" && obj.name.length > 0 ? obj.name : "Error";
714
+ const message = typeof obj.message === "string" ? obj.message : String(err);
715
+ const fields = {};
716
+ let hasFields = false;
717
+ for (const key of Object.keys(obj)) {
718
+ if (key === "name" || key === "message" || key === "stack") continue;
719
+ try {
720
+ const value = obj[key];
721
+ encodeWireValue(value);
722
+ fields[key] = value;
723
+ hasFields = true;
724
+ } catch {
725
+ continue;
726
+ }
727
+ }
728
+ if (!hasFields) return {
729
+ name,
730
+ message
731
+ };
732
+ return {
733
+ name,
734
+ message,
735
+ encodedData: encodeWireValue(fields)
736
+ };
737
+ } catch {
738
+ return {
739
+ name: "Error",
740
+ message: "host handler failed"
741
+ };
742
+ }
743
+ }
744
+ /**
697
745
  * Encode a `BridgeResponsePayload` per `docs/protocol.md` §5.4.
698
746
  *
699
747
  * When `ok = true`, `encodedValue` contains pre-encoded WireValue bytes.
700
- * When `ok = false`, `errorMessage` is the handler rejection message.
748
+ * When `ok = false`, `error` describes the handler rejection. The stack slot
749
+ * is always written as absent — host stacks must not leak into the sandbox.
701
750
  * @param callId
702
751
  * @param ok
703
752
  * @param encodedValue
704
- * @param errorMessage
753
+ * @param error the handler rejection, when `ok = false`
705
754
  */
706
- function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
755
+ function encodeBridgeResponsePayload(callId, ok, encodedValue, error) {
707
756
  const w = new PayloadWriter();
708
757
  w.writeU32(callId);
709
758
  if (ok) {
@@ -715,9 +764,13 @@ function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
715
764
  } else {
716
765
  w.writeU8(0);
717
766
  w.writeString("ERR_HOST_BRIDGE");
718
- w.writeString("Error");
719
- w.writeString(errorMessage ?? "host handler failed");
767
+ w.writeString(error?.name ?? "Error");
768
+ w.writeString(error?.message ?? "host handler failed");
720
769
  w.writeU8(0);
770
+ if (error?.encodedData !== void 0 && error.encodedData.byteLength > 0) {
771
+ w.writeU8(1);
772
+ w.writeBytes(error.encodedData);
773
+ } else w.writeU8(0);
721
774
  }
722
775
  return w.toBuffer();
723
776
  }
@@ -743,9 +796,14 @@ function formatByte(byte) {
743
796
  * `RunResult`.
744
797
  */
745
798
  var RunAbortedError = class extends Error {
746
- constructor(message = "run was aborted") {
799
+ /**
800
+ * The value passed to `AbortController.abort(reason)`, if any.
801
+ */
802
+ reason;
803
+ constructor(reason, message = "run was aborted") {
747
804
  super(message);
748
805
  this.name = "RunAbortedError";
806
+ this.reason = reason;
749
807
  }
750
808
  };
751
809
  var RuntimeIpcClient = class RuntimeIpcClient {
@@ -885,7 +943,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
885
943
  async drainUntilResult(dispatcher, signal) {
886
944
  if (signal?.aborted) {
887
945
  this.abortConnection();
888
- throw new RunAbortedError();
946
+ throw new RunAbortedError(signal.reason);
889
947
  }
890
948
  const onAbort = () => {
891
949
  this.abortConnection();
@@ -894,7 +952,7 @@ var RuntimeIpcClient = class RuntimeIpcClient {
894
952
  try {
895
953
  return await this.drainFrames(dispatcher);
896
954
  } catch (error) {
897
- if (signal?.aborted) throw new RunAbortedError();
955
+ if (signal?.aborted) throw new RunAbortedError(signal.reason);
898
956
  throw error;
899
957
  } finally {
900
958
  signal?.removeEventListener("abort", onAbort);
@@ -918,7 +976,10 @@ var RuntimeIpcClient = class RuntimeIpcClient {
918
976
  case RustToTsMessageTypes.Log: break;
919
977
  case RustToTsMessageTypes.BridgeCall: {
920
978
  const call = decodeBridgeCallPayload(frame.payload);
921
- if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, "no bridge dispatcher configured")));
979
+ if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, {
980
+ name: "Error",
981
+ message: "no bridge dispatcher configured"
982
+ })));
922
983
  else {
923
984
  const { callId } = call;
924
985
  dispatcher({
@@ -929,10 +990,13 @@ var RuntimeIpcClient = class RuntimeIpcClient {
929
990
  try {
930
991
  encoded = encodeWireValue(value);
931
992
  } catch (e) {
932
- return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, e instanceof Error ? e.message : String(e))));
993
+ return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, {
994
+ name: "Error",
995
+ message: e instanceof Error ? e.message : String(e)
996
+ })));
933
997
  }
934
998
  return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, true, encoded)));
935
- }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, err instanceof Error ? err.message : String(err))))).catch(() => {});
999
+ }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, bridgeErrorPayloadFromUnknown(err))))).catch(() => {});
936
1000
  }
937
1001
  break;
938
1002
  }
@@ -1474,16 +1538,20 @@ function toWireLimits(limits) {
1474
1538
  * The `RunResult` returned when a run is aborted — whether the signal was
1475
1539
  * already aborted at run entry or fired mid-flight (surfaced as a
1476
1540
  * `RunAbortedError` from the client). Shared so both paths produce an
1477
- * identical shape.
1541
+ * identical shape. `error` (code `ERR_ABORTED`) is retained for backward
1542
+ * compatibility; `reason` carries whatever was passed to `abort(reason)`.
1543
+ * @param reason
1478
1544
  */
1479
- function abortedResult() {
1545
+ function abortedResult(reason) {
1480
1546
  return {
1547
+ status: "aborted",
1481
1548
  ok: false,
1482
1549
  error: {
1483
1550
  code: "ERR_ABORTED",
1484
1551
  name: "AbortError",
1485
1552
  message: "run was aborted"
1486
1553
  },
1554
+ reason,
1487
1555
  stdout: [],
1488
1556
  stderr: [],
1489
1557
  durationMs: 0
@@ -1506,7 +1574,7 @@ var SandboxImpl = class {
1506
1574
  return this._alive;
1507
1575
  }
1508
1576
  async run(options) {
1509
- if (options.signal?.aborted) return abortedResult();
1577
+ if (options.signal?.aborted) return abortedResult(options.signal.reason);
1510
1578
  const { bridgeGlobals, preamble } = processGlobals(options.globals ?? {});
1511
1579
  const { bindings, registry, shape: _shape } = processImports(options.imports);
1512
1580
  const allGlobals = registry.size > 0 ? {
@@ -1525,7 +1593,7 @@ var SandboxImpl = class {
1525
1593
  })).result).result;
1526
1594
  });
1527
1595
  } catch (error) {
1528
- if (error instanceof RunAbortedError) return abortedResult();
1596
+ if (error instanceof RunAbortedError) return abortedResult(error.reason);
1529
1597
  throw error;
1530
1598
  }
1531
1599
  }
@@ -1609,6 +1677,7 @@ var PrefixImpl = class {
1609
1677
  }
1610
1678
  async run(options) {
1611
1679
  if (!this._alive) return {
1680
+ status: "failed",
1612
1681
  ok: false,
1613
1682
  error: {
1614
1683
  code: "ERR_PREFIX_DISPOSED",
@@ -1619,13 +1688,14 @@ var PrefixImpl = class {
1619
1688
  stderr: [],
1620
1689
  durationMs: 0
1621
1690
  };
1622
- if (options.signal?.aborted) return abortedResult();
1691
+ if (options.signal?.aborted) return abortedResult(options.signal.reason);
1623
1692
  const bridgeGlobals = extractBridgeGlobals(options.globals ?? {}, this.defaultGlobals);
1624
1693
  let registry;
1625
1694
  try {
1626
1695
  registry = extractRebindImports(options.imports, this.defaultRegistry, this.declaredImportShape);
1627
1696
  } catch (e) {
1628
1697
  if (e instanceof UndeclaredImportBindingError) return {
1698
+ status: "failed",
1629
1699
  ok: false,
1630
1700
  error: {
1631
1701
  code: "ERR_UNDECLARED_BINDING",
@@ -1654,7 +1724,7 @@ var PrefixImpl = class {
1654
1724
  })).result).result;
1655
1725
  });
1656
1726
  } catch (error) {
1657
- if (error instanceof RunAbortedError) return abortedResult();
1727
+ if (error instanceof RunAbortedError) return abortedResult(error.reason);
1658
1728
  throw error;
1659
1729
  }
1660
1730
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Fast, sandboxed V8 isolate runtime for agent-generated JavaScript. Two-process architecture for crash isolation.",
6
6
  "keywords": [
@@ -47,10 +47,10 @@
47
47
  "vitest": "^4.1.5"
48
48
  },
49
49
  "optionalDependencies": {
50
- "@iso4/v8-darwin-arm64": "^0.1.0",
51
- "@iso4/v8-darwin-x64": "^0.1.0",
52
- "@iso4/v8-linux-arm64-gnu": "^0.1.0",
53
- "@iso4/v8-linux-x64-gnu": "^0.1.0"
50
+ "@iso4/v8-darwin-x64": "^0.2.1",
51
+ "@iso4/v8-darwin-arm64": "^0.2.1",
52
+ "@iso4/v8-linux-arm64-gnu": "^0.2.1",
53
+ "@iso4/v8-linux-x64-gnu": "^0.2.1"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=24.0.0"