@iso4/sandbox 0.2.0 → 0.2.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/README.md CHANGED
@@ -111,13 +111,33 @@ type RunResult
111
111
  = | { ok: true, exports: SandboxExports, stdout: string[], stderr: string[], durationMs: number }
112
112
  | { ok: false, error: RunError, stdout: string[], stderr: string[], durationMs: number }
113
113
 
114
- interface RunError { code: RunErrorCode, name: string, message: string, stack?: string }
114
+ interface RunError {
115
+ code: RunErrorCode
116
+ name: string
117
+ message: string
118
+ stack?: string
119
+ fields?: Record<string, unknown> // all other own-enumerable props of the thrown error
120
+ }
115
121
  ```
116
122
 
117
123
  `run()` never throws for sandboxed failures — only for infrastructure errors
118
124
  (subprocess crashed, binary not found). `ok: false` with an error code is the
119
125
  normal failure path.
120
126
 
127
+ Thrown errors keep their identity across the bridge, in both directions:
128
+
129
+ - **Sandbox → host**: an uncaught sandbox throw surfaces as `ERR_USER_CODE`
130
+ with the error's real `name`, `message`, `stack`, and every other
131
+ own-enumerable property under `error.fields` (namespaced so a custom `code`
132
+ property can't collide with the iso4 `error.code`). `name`/`message`/`stack`
133
+ are reserved and never appear inside `fields`.
134
+ - **Host → sandbox**: a host handler that throws rejects the sandbox call with
135
+ a real `Error` carrying the same `name` (`instanceof TypeError` works for
136
+ built-ins) and its extra properties re-attached directly (`e.status`,
137
+ `e.reason`, …). Sandbox code can catch it and continue; uncaught it fails
138
+ the run with `ERR_HOST_BRIDGE`. The **host stack never crosses** into the
139
+ sandbox.
140
+
121
141
  ## Architecture
122
142
 
123
143
  V8 runs in a separate Rust subprocess communicating over a Unix domain socket.
package/dist/index.d.mts CHANGED
@@ -509,11 +509,20 @@ interface RunError {
509
509
  code: RunErrorCode;
510
510
  name: string;
511
511
  message: string;
512
+ /**
513
+ * Stack trace of the thrown error. `name`/`message`/`stack` are reserved:
514
+ * they are always read from the error's dedicated properties and never
515
+ * appear inside `fields`, so a thrown object cannot spoof them there.
516
+ */
512
517
  stack?: string;
513
518
  /**
514
- * Own enumerable properties of the thrown error, excluding name/message/stack. Present only for ERR_USER_CODE when the thrown value is an object with serializable own properties.
519
+ * All other own enumerable properties of the thrown error (everything
520
+ * except name/message/stack), e.g. `status`, `reason`, or a `data` payload
521
+ * from a custom error class — nested here so nothing can collide with
522
+ * `code`. Present for ERR_USER_CODE and ERR_HOST_BRIDGE when the thrown
523
+ * value is an object with serializable own properties.
515
524
  */
516
- data?: unknown;
525
+ fields?: Record<string, unknown>;
517
526
  }
518
527
  type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "ERR_WALL_TIMEOUT" | "ERR_ABORTED" | "ERR_MODULE_NOT_FOUND" | "ERR_COMPILE" | "ERR_FUNCTION_ARGUMENT_NOT_SUPPORTED" | "ERR_EXPORT_NOT_SERIALIZABLE" | "ERR_EXPORT_TOO_LARGE" | "ERR_EXPORT_UNRESOLVED_PROMISE" | "ERR_HOST_BRIDGE" | "ERR_BRIDGE_PAYLOAD_TOO_LARGE" | "ERR_BRIDGE_CALL_LIMIT_EXCEEDED" | "ERR_UNDECLARED_BINDING" | "ERR_PREFIX_DISPOSED" | "ERR_INTERNAL";
519
528
  //#endregion
package/dist/index.mjs CHANGED
@@ -205,6 +205,7 @@ function decodeWireValueFromSlice(buf) {
205
205
  * String name
206
206
  * String message
207
207
  * Optional<String> stack
208
+ * Optional<WireValue> fields
208
209
  * List<String> stdout
209
210
  * List<String> stderr
210
211
  * f64 durationMs
@@ -240,7 +241,7 @@ function decodeRunCompletionPayload(buf) {
240
241
  const name = reader.readString();
241
242
  const message = reader.readString();
242
243
  const stack = reader.readU8() === 1 ? reader.readString() : void 0;
243
- const data = reader.readU8() === 1 ? decodeWireValueFromReader(reader) : void 0;
244
+ const fields = reader.readU8() === 1 ? decodeWireValueFromReader(reader) : void 0;
244
245
  const stdout = reader.readStringList();
245
246
  const stderr = reader.readStringList();
246
247
  const durationMs = reader.readF64();
@@ -255,7 +256,7 @@ function decodeRunCompletionPayload(buf) {
255
256
  name,
256
257
  message,
257
258
  stack,
258
- data
259
+ fields
259
260
  },
260
261
  stdout,
261
262
  stderr,
@@ -696,16 +697,64 @@ function decodeBridgeCallPayload(buf) {
696
697
  };
697
698
  }
698
699
  /**
700
+ * Build a `BridgeErrorPayload` from whatever a host handler threw.
701
+ *
702
+ * Own-enumerable properties beyond `name`/`message`/`stack` travel as
703
+ * `fields` and reappear as direct own properties on the sandbox-side Error;
704
+ * properties that cannot be wire-encoded (functions, symbols, cycles, …) are
705
+ * silently dropped, mirroring the sandbox → host direction (`RunError.fields`).
706
+ * @param err
707
+ */
708
+ function bridgeErrorPayloadFromUnknown(err) {
709
+ try {
710
+ if (typeof err !== "object" || err === null) return {
711
+ name: "Error",
712
+ message: String(err)
713
+ };
714
+ const obj = err;
715
+ const name = typeof obj.name === "string" && obj.name.length > 0 ? obj.name : "Error";
716
+ const message = typeof obj.message === "string" ? obj.message : String(err);
717
+ const fields = {};
718
+ let hasFields = false;
719
+ for (const key of Object.keys(obj)) {
720
+ if (key === "name" || key === "message" || key === "stack") continue;
721
+ try {
722
+ const value = obj[key];
723
+ encodeWireValue(value);
724
+ fields[key] = value;
725
+ hasFields = true;
726
+ } catch {
727
+ continue;
728
+ }
729
+ }
730
+ if (!hasFields) return {
731
+ name,
732
+ message
733
+ };
734
+ return {
735
+ name,
736
+ message,
737
+ encodedFields: encodeWireValue(fields)
738
+ };
739
+ } catch {
740
+ return {
741
+ name: "Error",
742
+ message: "host handler failed"
743
+ };
744
+ }
745
+ }
746
+ /**
699
747
  * Encode a `BridgeResponsePayload` per `docs/protocol.md` §5.4.
700
748
  *
701
749
  * When `ok = true`, `encodedValue` contains pre-encoded WireValue bytes.
702
- * When `ok = false`, `errorMessage` is the handler rejection message.
750
+ * When `ok = false`, `error` describes the handler rejection. The stack slot
751
+ * is always written as absent — host stacks must not leak into the sandbox.
703
752
  * @param callId
704
753
  * @param ok
705
754
  * @param encodedValue
706
- * @param errorMessage
755
+ * @param error the handler rejection, when `ok = false`
707
756
  */
708
- function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
757
+ function encodeBridgeResponsePayload(callId, ok, encodedValue, error) {
709
758
  const w = new PayloadWriter();
710
759
  w.writeU32(callId);
711
760
  if (ok) {
@@ -717,9 +766,13 @@ function encodeBridgeResponsePayload(callId, ok, encodedValue, errorMessage) {
717
766
  } else {
718
767
  w.writeU8(0);
719
768
  w.writeString("ERR_HOST_BRIDGE");
720
- w.writeString("Error");
721
- w.writeString(errorMessage ?? "host handler failed");
769
+ w.writeString(error?.name ?? "Error");
770
+ w.writeString(error?.message ?? "host handler failed");
722
771
  w.writeU8(0);
772
+ if (error?.encodedFields !== void 0 && error.encodedFields.byteLength > 0) {
773
+ w.writeU8(1);
774
+ w.writeBytes(error.encodedFields);
775
+ } else w.writeU8(0);
723
776
  }
724
777
  return w.toBuffer();
725
778
  }
@@ -745,7 +798,9 @@ function formatByte(byte) {
745
798
  * `RunResult`.
746
799
  */
747
800
  var RunAbortedError = class extends Error {
748
- /** The value passed to `AbortController.abort(reason)`, if any. */
801
+ /**
802
+ * The value passed to `AbortController.abort(reason)`, if any.
803
+ */
749
804
  reason;
750
805
  constructor(reason, message = "run was aborted") {
751
806
  super(message);
@@ -923,7 +978,10 @@ var RuntimeIpcClient = class RuntimeIpcClient {
923
978
  case RustToTsMessageTypes.Log: break;
924
979
  case RustToTsMessageTypes.BridgeCall: {
925
980
  const call = decodeBridgeCallPayload(frame.payload);
926
- if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, "no bridge dispatcher configured")));
981
+ if (dispatcher === void 0) await this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(call.callId, false, void 0, {
982
+ name: "Error",
983
+ message: "no bridge dispatcher configured"
984
+ })));
927
985
  else {
928
986
  const { callId } = call;
929
987
  dispatcher({
@@ -934,10 +992,13 @@ var RuntimeIpcClient = class RuntimeIpcClient {
934
992
  try {
935
993
  encoded = encodeWireValue(value);
936
994
  } catch (e) {
937
- return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, e instanceof Error ? e.message : String(e))));
995
+ return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, {
996
+ name: "Error",
997
+ message: e instanceof Error ? e.message : String(e)
998
+ })));
938
999
  }
939
1000
  return this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, true, encoded)));
940
- }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, err instanceof Error ? err.message : String(err))))).catch(() => {});
1001
+ }, (err) => this.write(encodeTsToRustFrame(TsToRustMessageTypes.BridgeResponse, encodeBridgeResponsePayload(callId, false, void 0, bridgeErrorPayloadFromUnknown(err))))).catch(() => {});
941
1002
  }
942
1003
  break;
943
1004
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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.2.0",
51
- "@iso4/v8-darwin-x64": "^0.2.0",
52
- "@iso4/v8-linux-arm64-gnu": "^0.2.0",
53
- "@iso4/v8-linux-x64-gnu": "^0.2.0"
50
+ "@iso4/v8-darwin-arm64": "^0.2.2",
51
+ "@iso4/v8-darwin-x64": "^0.2.2",
52
+ "@iso4/v8-linux-arm64-gnu": "^0.2.2",
53
+ "@iso4/v8-linux-x64-gnu": "^0.2.2"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=24.0.0"