@iso4/sandbox 0.2.1 → 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,
@@ -698,9 +699,10 @@ function decodeBridgeCallPayload(buf) {
698
699
  /**
699
700
  * Build a `BridgeErrorPayload` from whatever a host handler threw.
700
701
  *
701
- * Own-enumerable properties beyond `name`/`message`/`stack` travel as `data`;
702
+ * Own-enumerable properties beyond `name`/`message`/`stack` travel as
703
+ * `fields` and reappear as direct own properties on the sandbox-side Error;
702
704
  * properties that cannot be wire-encoded (functions, symbols, cycles, …) are
703
- * silently dropped, mirroring the sandbox → host direction (`RunError.data`).
705
+ * silently dropped, mirroring the sandbox → host direction (`RunError.fields`).
704
706
  * @param err
705
707
  */
706
708
  function bridgeErrorPayloadFromUnknown(err) {
@@ -732,7 +734,7 @@ function bridgeErrorPayloadFromUnknown(err) {
732
734
  return {
733
735
  name,
734
736
  message,
735
- encodedData: encodeWireValue(fields)
737
+ encodedFields: encodeWireValue(fields)
736
738
  };
737
739
  } catch {
738
740
  return {
@@ -767,9 +769,9 @@ function encodeBridgeResponsePayload(callId, ok, encodedValue, error) {
767
769
  w.writeString(error?.name ?? "Error");
768
770
  w.writeString(error?.message ?? "host handler failed");
769
771
  w.writeU8(0);
770
- if (error?.encodedData !== void 0 && error.encodedData.byteLength > 0) {
772
+ if (error?.encodedFields !== void 0 && error.encodedFields.byteLength > 0) {
771
773
  w.writeU8(1);
772
- w.writeBytes(error.encodedData);
774
+ w.writeBytes(error.encodedFields);
773
775
  } else w.writeU8(0);
774
776
  }
775
777
  return w.toBuffer();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.2.1",
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-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"
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"