@iso4/sandbox 0.6.0 → 0.6.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
@@ -354,15 +354,20 @@ type RunResult
354
354
  // durationMs — wall-clock time of the run; cpuTimeMs — active V8 execution
355
355
  // time (bridge waits excluded). Both measured in the runtime, µs resolution.
356
356
 
357
- interface BridgeCallEntry { // recorded in the Rust runtime; one per attempt, in order
357
+ type BridgeCallEntry = { // recorded in the Rust runtime; one per attempt, in order
358
358
  name: string // 'fetch', 'myTool', or '<specifier>.<path>' for host-module imports
359
359
  startMs: number // offset from run start (same clock as durationMs)
360
360
  durationMs: number // round-trip the sandbox waited (handler + IPC)
361
361
  argBytes: number // serialized call payload size
362
- responseBytes: number // serialized response value size (0 on handler error)
363
- ok: boolean
364
- blocked: boolean // blocked by a limit runtime-side; never reached the host
365
- }
362
+ responseBytes: number // serialized response value size (0 unless ok)
363
+ } & (
364
+ | { ok: true }
365
+ | { ok: false, reason: 'blocked' | 'error' | 'unanswered' | 'dropped' }
366
+ )
367
+ // blocked: refused runtime-side (limit, function argument, ...), never reached the host
368
+ // error: the handler threw / rejected, or the host could not decode or encode a value
369
+ // unanswered: no answer when the run ended (handler still ran)
370
+ // dropped: the run was already aborted, the handler was never called
366
371
 
367
372
  interface RunError {
368
373
  code: RunErrorCode
package/dist/index.d.mts CHANGED
@@ -1155,7 +1155,35 @@ interface CallSuccess {
1155
1155
  * an entry (~100 bytes); consumers that only care about a subset filter the
1156
1156
  * list themselves.
1157
1157
  */
1158
- interface BridgeCallEntry {
1158
+ type BridgeCallEntry = BridgeCallEntryBase & ({
1159
+ ok: true;
1160
+ } | {
1161
+ ok: false;
1162
+ reason: BridgeCallFailureReason;
1163
+ });
1164
+ /**
1165
+ * Why a bridge call did not succeed ({@link BridgeCallEntry} with
1166
+ * `ok: false`).
1167
+ *
1168
+ * - `blocked`: refused inside the runtime (`maxBridgeCalls`, payload over
1169
+ * `maxBridgeCallBytes`, function argument, invalid import handle, failed
1170
+ * write). Never reached the host; the sandbox promise rejected.
1171
+ * - `error`: the host answered with an error — the handler threw or
1172
+ * rejected, the host could not decode the arguments, or the return value
1173
+ * could not be serialized. The sandbox promise rejected.
1174
+ * - `unanswered`: sent, but no answer by the time these records were taken —
1175
+ * abort, a limit, an uncaught throw, or the sandbox never awaited it. The
1176
+ * handler ran (or is still running); its answer is discarded. On a run
1177
+ * with pending `waitUntil` work the Result's records are a snapshot, so a
1178
+ * call still in flight shows here even if it settles during the epilogue.
1179
+ * - `dropped`: the run's signal was already aborted when the call reached
1180
+ * the host, so the handler was never invoked.
1181
+ */
1182
+ type BridgeCallFailureReason = "blocked" | "error" | "unanswered" | "dropped";
1183
+ /**
1184
+ * Fields shared by every {@link BridgeCallEntry}.
1185
+ */
1186
+ interface BridgeCallEntryBase {
1159
1187
  /**
1160
1188
  * The name sandbox code called. Plain globals appear as-is (`fetch`),
1161
1189
  * shimmed globals under their public name (not the private stub), and
@@ -1170,8 +1198,8 @@ interface BridgeCallEntry {
1170
1198
  startMs: number;
1171
1199
  /**
1172
1200
  * Round-trip time the sandbox waited for the response (host handler + IPC),
1173
- * in milliseconds. For calls still unanswered when the run ended
1174
- * (timeout/abort): the time until the run ended. `0` for blocked attempts.
1201
+ * in milliseconds. For `unanswered` and `dropped` calls: the time until
1202
+ * the run ended. `0` for blocked attempts.
1175
1203
  */
1176
1204
  durationMs: number;
1177
1205
  /**
@@ -1185,18 +1213,6 @@ interface BridgeCallEntry {
1185
1213
  * handler failed or the call never settled.
1186
1214
  */
1187
1215
  responseBytes: number;
1188
- /**
1189
- * `true` when the host handler resolved and its response reached the
1190
- * sandbox. `false` for handler errors, blocked attempts, and calls still
1191
- * in flight when the run ended.
1192
- */
1193
- ok: boolean;
1194
- /**
1195
- * `true` when the attempt was blocked inside the runtime (`maxBridgeCalls`
1196
- * exceeded, payload over `maxBridgeCallBytes`, function argument) and never
1197
- * reached the host.
1198
- */
1199
- blocked: boolean;
1200
1216
  }
1201
1217
  /**
1202
1218
  * Outcome of a run's `waitUntil` grace phase, delivered through
@@ -1318,8 +1334,8 @@ interface RunSuccess {
1318
1334
  queueWaitMs?: number;
1319
1335
  /**
1320
1336
  * One entry per bridge call the sandbox attempted, in attempt order —
1321
- * including attempts blocked by limits ({@link BridgeCallEntry.blocked}).
1322
- * Recorded by the Rust runtime.
1337
+ * including attempts that never reached the host (see
1338
+ * {@link BridgeCallFailureReason}). Recorded by the Rust runtime.
1323
1339
  */
1324
1340
  bridgeCalls: BridgeCallEntry[];
1325
1341
  /**
@@ -1445,4 +1461,4 @@ type RunErrorCode = "ERR_USER_CODE" | "ERR_MEMORY_LIMIT" | "ERR_CPU_TIMEOUT" | "
1445
1461
  //#region src/index.d.ts
1446
1462
  declare function createSandbox(options?: SandboxOptions): Promise<Sandbox>;
1447
1463
  //#endregion
1448
- export { type BridgeCallEntry, type BridgeGlobal, type BridgeWithShim, type CallResult, type CallSuccess, type CallTarget, type CreateSandbox, type DataGlobal, type GlobalOptions, type HostExportData, type HostExportFunction, type HostGlobalValue, type HostGlobals, type HostModuleObject, type HostModuleValue, type ImportValue, type Imports, type MemoryLimit, type OneOffResourceLimits, type PrecompileOptions, type Prefix, type PrefixCallOptions, type PrefixRunOptions, type ReadExportsOptions, type ReadExportsResult, type RebindGlobals, type RebindHostModule, type RebindImports, type RebindValue, type ResetCause, type ResourceLimits, type RunCallOptions, type RunError, type RunErrorCode, type RunFailure, type RunOptions, type RunResult, type RunSuccess, type Sandbox, type SandboxExports, type SandboxOptions, type SandboxStats, type StringGlobal, type WaitUntilResult, createSandbox };
1464
+ export { type BridgeCallEntry, type BridgeCallEntryBase, type BridgeCallFailureReason, type BridgeGlobal, type BridgeWithShim, type CallResult, type CallSuccess, type CallTarget, type CreateSandbox, type DataGlobal, type GlobalOptions, type HostExportData, type HostExportFunction, type HostGlobalValue, type HostGlobals, type HostModuleObject, type HostModuleValue, type ImportValue, type Imports, type MemoryLimit, type OneOffResourceLimits, type PrecompileOptions, type Prefix, type PrefixCallOptions, type PrefixRunOptions, type ReadExportsOptions, type ReadExportsResult, type RebindGlobals, type RebindHostModule, type RebindImports, type RebindValue, type ResetCause, type ResourceLimits, type RunCallOptions, type RunError, type RunErrorCode, type RunFailure, type RunOptions, type RunResult, type RunSuccess, type Sandbox, type SandboxExports, type SandboxOptions, type SandboxStats, type StringGlobal, type WaitUntilResult, createSandbox };
package/dist/index.mjs CHANGED
@@ -1330,8 +1330,10 @@ function encodeBridgeResponsePayload(runId, callId, ok, encodedValue, error) {
1330
1330
  /**
1331
1331
  * Decode a `RunComplete` frame payload.
1332
1332
  * @param buf the frame payload
1333
+ * @param droppedCallIds bridge calls the client skipped because the run was
1334
+ * already aborted; their records are reported as `dropped`
1333
1335
  */
1334
- function decodeRunCompletePayload(buf) {
1336
+ function decodeRunCompletePayload(buf, droppedCallIds) {
1335
1337
  const reader = new PayloadReader(buf);
1336
1338
  const runId = reader.readU32();
1337
1339
  const statusByte = reader.readU8();
@@ -1346,7 +1348,7 @@ function decodeRunCompletePayload(buf) {
1346
1348
  const cpuTimeMs = reader.readF64();
1347
1349
  const stdout = reader.readStringList();
1348
1350
  const stderr = reader.readStringList();
1349
- const bridgeCalls = readBridgeCallRecords(reader);
1351
+ const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
1350
1352
  const error = reader.readU8() === 1 ? {
1351
1353
  name: reader.readString(),
1352
1354
  message: reader.readString()
@@ -1544,7 +1546,7 @@ function peekRunCompletionSlotAllowance(buf) {
1544
1546
  if (buf.byteLength < 8) return 0;
1545
1547
  return (buf[4] << 24 | buf[5] << 16 | buf[6] << 8 | buf[7]) >>> 0;
1546
1548
  }
1547
- function decodeRunCompletionPayload(buf, resultKind) {
1549
+ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1548
1550
  const reader = new PayloadReader(buf);
1549
1551
  const runId = reader.readU32();
1550
1552
  const slotAllowance = reader.readU32();
@@ -1557,7 +1559,7 @@ function decodeRunCompletionPayload(buf, resultKind) {
1557
1559
  const durationMs = reader.readF64();
1558
1560
  const wallTimeMs = reader.readF64();
1559
1561
  const cpuTimeMs = reader.readF64();
1560
- const bridgeCalls = readBridgeCallRecords(reader);
1562
+ const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
1561
1563
  const heapUsedBytes = reader.readOptionalU64();
1562
1564
  const backgroundFlags = reader.readU8();
1563
1565
  reader.readU8();
@@ -1611,7 +1613,7 @@ function decodeRunCompletionPayload(buf, resultKind) {
1611
1613
  const durationMs = reader.readF64();
1612
1614
  const wallTimeMs = reader.readF64();
1613
1615
  const cpuTimeMs = reader.readF64();
1614
- const bridgeCalls = readBridgeCallRecords(reader);
1616
+ const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
1615
1617
  const heapUsedBytes = reader.readOptionalU64();
1616
1618
  reader.assertDone();
1617
1619
  return {
@@ -1668,8 +1670,9 @@ function readResetInfo(reader) {
1668
1670
  * already resolved — the runtime owns the import handle table and the shim
1669
1671
  * naming convention, so no client-side mapping remains.
1670
1672
  * @param reader
1673
+ * @param droppedCallIds
1671
1674
  */
1672
- function readBridgeCallRecords(reader) {
1675
+ function readBridgeCallRecords(reader, droppedCallIds) {
1673
1676
  const count = reader.readU32();
1674
1677
  const entries = [];
1675
1678
  for (let i = 0; i < count; i++) {
@@ -1678,16 +1681,38 @@ function readBridgeCallRecords(reader) {
1678
1681
  const durationMs = reader.readF64();
1679
1682
  const argBytes = reader.readU32();
1680
1683
  const responseBytes = reader.readU32();
1681
- const ok = reader.readBool();
1682
- const blocked = reader.readBool();
1683
- entries.push({
1684
+ const callId = reader.readU32();
1685
+ const outcome = reader.readU8();
1686
+ const base = {
1684
1687
  name,
1685
1688
  startMs,
1686
1689
  durationMs,
1687
1690
  argBytes,
1688
- responseBytes,
1689
- ok,
1690
- blocked
1691
+ responseBytes
1692
+ };
1693
+ let reason;
1694
+ switch (outcome) {
1695
+ case 0:
1696
+ entries.push({
1697
+ ...base,
1698
+ ok: true
1699
+ });
1700
+ continue;
1701
+ case 1:
1702
+ reason = "blocked";
1703
+ break;
1704
+ case 2:
1705
+ reason = "error";
1706
+ break;
1707
+ case 3:
1708
+ reason = droppedCallIds?.has(callId) ? "dropped" : "unanswered";
1709
+ break;
1710
+ default: throw new PayloadDecodeError(`unknown bridge call outcome byte: ${outcome}`);
1711
+ }
1712
+ entries.push({
1713
+ ...base,
1714
+ ok: false,
1715
+ reason
1691
1716
  });
1692
1717
  }
1693
1718
  return entries;
@@ -2656,7 +2681,8 @@ var RuntimeIpcClient = class RuntimeIpcClient {
2656
2681
  entry.resolve({
2657
2682
  result: payload,
2658
2683
  graceWork: false,
2659
- outStreams: entry.outStreams
2684
+ outStreams: entry.outStreams,
2685
+ ...entry.droppedCallIds && { droppedCallIds: entry.droppedCallIds }
2660
2686
  });
2661
2687
  return;
2662
2688
  }
@@ -2688,7 +2714,8 @@ var RuntimeIpcClient = class RuntimeIpcClient {
2688
2714
  result: payload,
2689
2715
  epilogue,
2690
2716
  graceWork: (flags & 1) !== 0,
2691
- outStreams: entry.outStreams
2717
+ outStreams: entry.outStreams,
2718
+ ...entry.droppedCallIds && { droppedCallIds: entry.droppedCallIds }
2692
2719
  });
2693
2720
  }
2694
2721
  /**
@@ -2698,7 +2725,8 @@ var RuntimeIpcClient = class RuntimeIpcClient {
2698
2725
  * @param payload
2699
2726
  */
2700
2727
  routeRunComplete(payload) {
2701
- const report = decodeRunCompletePayload(payload);
2728
+ const runId = peekRunCompletionRunId(payload);
2729
+ const report = decodeRunCompletePayload(payload, runId === void 0 ? void 0 : this.runs.get(runId)?.droppedCallIds);
2702
2730
  const entry = this.runs.get(report.runId);
2703
2731
  if (entry === void 0 || entry.epilogue === void 0) throw new ProtocolDesyncError(`RunComplete frame carries runId ${report.runId} but no run with that id is in its waitUntil grace phase on this connection`);
2704
2732
  this.runs.delete(report.runId);
@@ -2805,6 +2833,11 @@ var RuntimeIpcClient = class RuntimeIpcClient {
2805
2833
  */
2806
2834
  async dispatchBridgeCallFrame(payload, entry, runId) {
2807
2835
  const { dispatcher, streams } = entry;
2836
+ if (entry.signal?.aborted || entry.hardAbortSignal?.aborted) {
2837
+ const callId = peekBridgeCallId(payload);
2838
+ if (callId !== void 0) (entry.droppedCallIds ??= /* @__PURE__ */ new Set()).add(callId);
2839
+ return;
2840
+ }
2808
2841
  let call;
2809
2842
  try {
2810
2843
  call = decodeBridgeCallPayload(payload);
@@ -3850,7 +3883,7 @@ var SandboxImpl = class {
3850
3883
  call,
3851
3884
  streams
3852
3885
  });
3853
- const decoded = withBodyStreamBinder((id) => raw.outStreams.attach(id), () => call === void 0 ? decodeRunCompletionPayload(raw.result).result : decodeRunCompletionPayload(raw.result, "call").result);
3886
+ const decoded = withBodyStreamBinder((id) => raw.outStreams.attach(id), () => call === void 0 ? decodeRunCompletionPayload(raw.result, void 0, raw.droppedCallIds).result : decodeRunCompletionPayload(raw.result, "call", raw.droppedCallIds).result);
3854
3887
  if (raw.epilogue !== void 0 && raw.graceWork && decoded.ok) decoded.waitUntil = raw.epilogue.then(waitUntilResultFrom);
3855
3888
  if (decoded.status === "failed" && decoded.error.code === "ERR_ABORTED") {
3856
3889
  if (options.signal?.aborted) return abortedResult(options.signal.reason, decoded);
@@ -4122,7 +4155,7 @@ var PrefixImpl = class {
4122
4155
  call: payload.call,
4123
4156
  streams
4124
4157
  });
4125
- const decoded = withBodyStreamBinder((id) => raw.outStreams.attach(id), () => payload.call === void 0 ? decodeRunCompletionPayload(raw.result).result : decodeRunCompletionPayload(raw.result, "call").result);
4158
+ const decoded = withBodyStreamBinder((id) => raw.outStreams.attach(id), () => payload.call === void 0 ? decodeRunCompletionPayload(raw.result, void 0, raw.droppedCallIds).result : decodeRunCompletionPayload(raw.result, "call", raw.droppedCallIds).result);
4126
4159
  if (raw.epilogue !== void 0 && raw.graceWork && decoded.ok) decoded.waitUntil = raw.epilogue.then(waitUntilResultFrom);
4127
4160
  if (decoded.status === "failed" && decoded.error.code === "ERR_ABORTED") {
4128
4161
  if (options.signal?.aborted) return abortedResult(options.signal.reason, decoded);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.6.0",
3
+ "version": "0.6.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": [
@@ -49,10 +49,10 @@
49
49
  "vitest": "^4.1.5"
50
50
  },
51
51
  "optionalDependencies": {
52
- "@iso4/v8-darwin-arm64": "^0.6.0",
53
- "@iso4/v8-darwin-x64": "^0.6.0",
54
- "@iso4/v8-linux-arm64-gnu": "^0.6.0",
55
- "@iso4/v8-linux-x64-gnu": "^0.6.0"
52
+ "@iso4/v8-darwin-arm64": "^0.6.2",
53
+ "@iso4/v8-darwin-x64": "^0.6.2",
54
+ "@iso4/v8-linux-arm64-gnu": "^0.6.2",
55
+ "@iso4/v8-linux-x64-gnu": "^0.6.2"
56
56
  },
57
57
  "engines": {
58
58
  "node": ">=24.0.0"