@iso4/sandbox 0.2.1 → 0.3.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/README.md CHANGED
@@ -104,6 +104,30 @@ prefix.run({
104
104
  })
105
105
  ```
106
106
 
107
+ ## Async context (`AsyncLocalStorage`)
108
+
109
+ Run/postfix code can import a minimal, Node-compatible `AsyncLocalStorage` to
110
+ carry an ambient value across `await` points — concurrency-safe, unlike a
111
+ module variable:
112
+
113
+ ```ts
114
+ prefix.run({
115
+ code: `
116
+ import { AsyncLocalStorage } from 'node:async_hooks'
117
+ const als = new AsyncLocalStorage()
118
+ export default await als.run('trace-42', async () => {
119
+ await somethingAsync()
120
+ return als.getStore() // 'trace-42', even several awaits deep
121
+ })
122
+ `,
123
+ })
124
+ ```
125
+
126
+ Only `run(store, callback, ...args)` and `getStore()` are provided. Built on
127
+ V8's continuation-preserved embedder data; no promise hooks, so it's free
128
+ unless used. Not available in `precompile()` (prefix) code — it's for the
129
+ postfix. See DESIGN.md §16.
130
+
107
131
  ## Result shape
108
132
 
109
133
  ```ts
@@ -111,13 +135,33 @@ type RunResult
111
135
  = | { ok: true, exports: SandboxExports, stdout: string[], stderr: string[], durationMs: number }
112
136
  | { ok: false, error: RunError, stdout: string[], stderr: string[], durationMs: number }
113
137
 
114
- interface RunError { code: RunErrorCode, name: string, message: string, stack?: string }
138
+ interface RunError {
139
+ code: RunErrorCode
140
+ name: string
141
+ message: string
142
+ stack?: string
143
+ fields?: Record<string, unknown> // all other own-enumerable props of the thrown error
144
+ }
115
145
  ```
116
146
 
117
147
  `run()` never throws for sandboxed failures — only for infrastructure errors
118
148
  (subprocess crashed, binary not found). `ok: false` with an error code is the
119
149
  normal failure path.
120
150
 
151
+ Thrown errors keep their identity across the bridge, in both directions:
152
+
153
+ - **Sandbox → host**: an uncaught sandbox throw surfaces as `ERR_USER_CODE`
154
+ with the error's real `name`, `message`, `stack`, and every other
155
+ own-enumerable property under `error.fields` (namespaced so a custom `code`
156
+ property can't collide with the iso4 `error.code`). `name`/`message`/`stack`
157
+ are reserved and never appear inside `fields`.
158
+ - **Host → sandbox**: a host handler that throws rejects the sandbox call with
159
+ a real `Error` carrying the same `name` (`instanceof TypeError` works for
160
+ built-ins) and its extra properties re-attached directly (`e.status`,
161
+ `e.reason`, …). Sandbox code can catch it and continue; uncaught it fails
162
+ the run with `ERR_HOST_BRIDGE`. The **host stack never crosses** into the
163
+ sandbox.
164
+
121
165
  ## Architecture
122
166
 
123
167
  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.3.0",
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.3.0",
51
+ "@iso4/v8-darwin-x64": "^0.3.0",
52
+ "@iso4/v8-linux-arm64-gnu": "^0.3.0",
53
+ "@iso4/v8-linux-x64-gnu": "^0.3.0"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=24.0.0"