@iso4/sandbox 0.9.0 → 0.10.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
@@ -33,7 +33,7 @@ tracks musl support).
33
33
  import { createSandbox } from '@iso4/sandbox'
34
34
  import { createSafeFetch } from '@iso4/fetch'
35
35
 
36
- const sandbox = await createSandbox({ memoryMb: 128 }) // per-isolate heap cap
36
+ const sandbox = await createSandbox({ memoryMb: 128 }) // per-isolate memory cap (JS heap + buffers)
37
37
 
38
38
  // Store host setup once; it is evaluated on each cold start
39
39
  const prefix = await sandbox.prepare({
@@ -52,7 +52,7 @@ const result = await prefix.execute({
52
52
  const res = await fetch(config.apiBase + '/users')
53
53
  export default { count: res.length }
54
54
  `,
55
- limits: { cpuTimeMs: 200, wallTimeMs: 5_000 }, // heap cap: prepare({ memoryMb }) or the sandbox default
55
+ limits: { cpuTimeMs: 200, wallTimeMs: 5_000 }, // memory cap: prepare({ memoryMb }) or the sandbox default
56
56
  })
57
57
 
58
58
  if (result.ok) {
@@ -231,7 +231,7 @@ Residency is bounded by memory, not by a count:
231
231
 
232
232
  ```ts
233
233
  const sandbox = await createSandbox({
234
- memoryMb: 128, // default heap cap per isolate (override per prefix at prepare())
234
+ memoryMb: 128, // default memory cap per isolate, JS heap + buffers (override per prefix at prepare())
235
235
  memoryBudgetMb: 2048, // RSS mark for the whole runtime process (0 = off)
236
236
  hostReserveMb: 128, // container memory held back for this host process
237
237
  })
@@ -240,21 +240,24 @@ const sandbox = await createSandbox({
240
240
  `memoryMb` means two different things depending on whether the isolate is
241
241
  reused.
242
242
 
243
- **On a prefix**, the number is the retirement line: an instance whose heap sits
243
+ **On a prefix**, the number is the retirement line: an instance whose memory sits
244
244
  above it when two runs in a row finish takes no new runs and drops once the
245
245
  runs it is already serving complete. Nothing fails. The terminating line sits a
246
246
  headroom band above it (`round(sqrt(8 × memoryMb))` MB, so 128 → 160); crossing
247
247
  _that_ kills the run and fails its co-residents, and only a runaway allocation
248
248
  gets there. The band is what lets a grown instance be replaced instead of
249
- killing a run at the number — an optimization for reuse.
249
+ killing a run at the number — an optimization for reuse. Both lines count the
250
+ JS heap and buffer memory (ArrayBuffers, typed arrays, `resize()` growth, stream
251
+ chunks) together.
250
252
 
251
253
  ```ts
252
254
  await sandbox.prepare({ code, memoryMb: { soft: 96, hard: 256 } }) // exact lines
253
255
  await sandbox.prepare({ code, memoryMb: { hard: 128 } }) // terminate at 128, never retire
254
256
  ```
255
257
 
256
- **On a one-off `run()`**, the number is enforced exactly — `memoryMb: 128`
257
- dies at 128. A fresh isolate is never reused, so there is nothing to retire
258
+ **On a one-off `run()`**, the number is the terminating line — `memoryMb: 128`
259
+ ends the run at 128 (checked every ~10 ms and at every turn end, so it can
260
+ overshoot briefly). A fresh isolate is never reused, so there is nothing to retire
258
261
  and no band. It takes a plain number for the same reason: a soft line would
259
262
  be inert.
260
263
 
@@ -283,7 +286,7 @@ the container is genuinely too small for `maxConcurrentRuns × memoryMb` —
283
286
  `createSandbox` warns at startup when those two contradict each other.
284
287
 
285
288
  `sandbox.stats()` reports the live picture — active runs, queue depth, warm and
286
- idle instance counts, summed idle heap, `budgetBytes` / `rssBytes`, whether the
289
+ idle instance counts, summed idle memory, `budgetBytes` / `rssBytes`, whether the
287
290
  runtime is currently `underPressure`, and per-prefix counts. It answers on a
288
291
  dedicated connection, so it works even when every run slot is busy.
289
292
 
package/dist/index.d.mts CHANGED
@@ -12,17 +12,19 @@
12
12
  * parallel. See DESIGN.md §1 and §13.
13
13
  */
14
14
  /**
15
- * The two heap lines a REUSED isolate is held to, when the managed default
15
+ * The two memory lines a REUSED isolate is held to, when the managed default
16
16
  * is not what you want. One-off `run()` isolates are never reused and take a
17
- * plain `memoryMb` number, enforced exactly.
17
+ * plain `memoryMb` number with no soft line.
18
18
  *
19
- * - `hard` **terminates**: V8's heap cap. Crossing it kills whatever JS is
19
+ * - `hard` **terminates**: one cap for the JS heap and buffer memory
20
+ * (ArrayBuffers and typed arrays, including `resize()` growth, and stream
21
+ * chunks) together. Crossing it kills whatever JS is
20
22
  * executing, taints the instance, and fails its co-resident runs with
21
23
  * `ERR_INSTANCE_RESET`. This is the ceiling the container reserves against
22
24
  * its admission line.
23
- * - `soft` **retires**: a warm instance whose heap sits above it at two
24
- * consecutive finishes takes no further runs and is dropped once the runs
25
- * it is already serving complete. No run fails and nothing is terminated —
25
+ * - `soft` **retires**: a warm instance whose memory (JS heap plus buffers)
26
+ * sits above it at two consecutive finishes takes no further runs and is
27
+ * dropped once the runs it is already serving complete. No run fails and nothing is terminated —
26
28
  * it is how a prefix that accumulates memory across reuse gets replaced
27
29
  * instead of eventually killing a run. Omit it for a hard line only.
28
30
  *
@@ -52,8 +54,9 @@ interface MemoryLimit {
52
54
  */
53
55
  interface OneOffResourceLimits extends ResourceLimits {
54
56
  /**
55
- * Hard cap on this run's isolate memory, in megabytes — V8 heap plus
56
- * external `ArrayBuffer`s. Enforced exactly: this run dies at this number.
57
+ * Hard cap on this run's isolate memory, in megabytes — one limit for the
58
+ * JS heap and buffer memory together. The run is ended once it holds more,
59
+ * checked every ~10 ms and at every turn end, so it can overshoot briefly.
57
60
  *
58
61
  * A one-off always gets a fresh isolate, so — unlike a prefix's reused
59
62
  * instances — there is nothing to retire and no headroom band. The number
@@ -512,8 +515,8 @@ interface SandboxOptions {
512
515
  */
513
516
  maxQueuedRuns?: number;
514
517
  /**
515
- * Default hard cap on memory each isolate can use, in megabytes. Covers
516
- * the V8 heap and all external `ArrayBuffer` allocations; also determines
518
+ * Default hard cap on memory each isolate can use, in megabytes. One
519
+ * limit for the JS heap and all buffer memory together; also determines
517
520
  * the BridgeResponse frame read cap (the sandbox cannot receive a
518
521
  * response larger than its own memory budget).
519
522
  *
@@ -549,7 +552,7 @@ interface SandboxOptions {
549
552
  * cgroup's working set, Node host included, the number the container's
550
553
  * OOM killer acts on (where no cgroup exists, e.g. macOS, it falls back
551
554
  * to the runtime child's own RSS). At/above the mark it evicts idle warm
552
- * instances by `heapUsed × idleTime^1.2` score (highest first) AND stops
555
+ * instances by `memoryBytes × idleTime^1.2` score (highest first) AND stops
553
556
  * pooling new instances — prefix runs without an idle instance execute
554
557
  * on cold one-off isolates — until usage falls back to 80 % of the mark
555
558
  * (hysteresis, so eviction doesn't flap). Reusing an already-warm
@@ -654,12 +657,13 @@ interface SandboxStats {
654
657
  */
655
658
  idleInstances: number;
656
659
  /**
657
- * Summed heap of the idle instances in bytes, each measured after its
660
+ * Summed memory of the idle instances in bytes (JS heap plus buffer
661
+ * memory, as {@link RunSuccess.memoryBytes}), each measured after its
658
662
  * last call — or, where the runtime's idle settle is switched on, after
659
663
  * the collection a parked instance runs, which is the smaller and truer
660
- * number (a busy instance's current heap is unknown mid-call).
664
+ * number (a busy instance's current memory is unknown mid-call).
661
665
  */
662
- idleHeapBytes: number;
666
+ idleMemoryBytes: number;
663
667
  /**
664
668
  * The memory mark the runtime sheds against, in bytes —
665
669
  * `memoryBudgetMb` as the runtime received it. 0 = watermarks disabled.
@@ -1207,9 +1211,11 @@ interface CallSuccess {
1207
1211
  stdout: string[];
1208
1212
  stderr: string[];
1209
1213
  /**
1210
- * Complete wall-clock from dispatch to the processed conclusion — async
1211
- * waits, engine time for co-resident runs, machinery: everything. May
1212
- * exceed the `wallTimeMs` limit, which counts {@link wallTimeMs} instead.
1214
+ * Complete wall-clock from when the run starts executing to the processed
1215
+ * conclusion — async waits, engine time for co-resident runs, machinery:
1216
+ * everything. May exceed the `wallTimeMs` limit, which counts
1217
+ * {@link wallTimeMs} instead. Waiting for a free CPU before the run starts
1218
+ * is in none of the three clocks.
1213
1219
  */
1214
1220
  durationMs: number;
1215
1221
  /**
@@ -1232,11 +1238,20 @@ interface CallSuccess {
1232
1238
  queueWaitMs?: number;
1233
1239
  bridgeCalls: BridgeCallEntry[];
1234
1240
  /**
1235
- * `used_heap_size` of the isolate that served this call, measured after
1236
- * the call settled. Reported for prefix calls (warm instances);
1237
- * `undefined` for one-off `sandbox.run({ call })`.
1238
- */
1239
- heapUsedBytes?: number;
1241
+ * Memory of the isolate that served this call, in bytes: the JS heap plus
1242
+ * buffer memory (ArrayBuffers and typed arrays, including memory grown with
1243
+ * `resize()`, and stream chunks). Read once when the call ends, on prefix
1244
+ * and one-off calls alike; it includes garbage not yet collected (a warm
1245
+ * instance pushed over its soft line by buffers, with objects alone under
1246
+ * it, is collected once first). A call that sends its result early
1247
+ * (`waitUntil` work, open stream bodies) reports a reading taken at that
1248
+ * moment, before any run-end collection, so it can differ from the figure
1249
+ * the soft line uses. `0` on a prefix call whose instance could not be
1250
+ * measured (failed warm-up, dead instance). `undefined` when the call was
1251
+ * answered without a reading: refused before it started, or a result too
1252
+ * large to send.
1253
+ */
1254
+ memoryBytes?: number;
1240
1255
  /**
1241
1256
  * Present when the call registered background work with `waitUntil` —
1242
1257
  * see {@link RunSuccess.waitUntil}. Never rejects.
@@ -1395,11 +1410,12 @@ interface RunSuccess {
1395
1410
  */
1396
1411
  stderr: string[];
1397
1412
  /**
1398
- * Complete wall-clock time of the run (dispatch to the processed
1399
- * conclusion), measured in the runtime — async waits, engine time for
1400
- * co-resident runs, machinery: everything. May exceed the `wallTimeMs`
1401
- * limit, which counts {@link wallTimeMs} instead. Milliseconds with
1402
- * microsecond resolution.
1413
+ * Complete wall-clock time of the run (from when it starts executing to
1414
+ * the processed conclusion), measured in the runtime — async waits, engine
1415
+ * time for co-resident runs, machinery: everything. May exceed the
1416
+ * `wallTimeMs` limit, which counts {@link wallTimeMs} instead. Waiting for
1417
+ * a free CPU before the run starts is in none of the three clocks.
1418
+ * Milliseconds with microsecond resolution.
1403
1419
  */
1404
1420
  durationMs: number;
1405
1421
  /**
@@ -1422,9 +1438,12 @@ interface RunSuccess {
1422
1438
  * resolution.
1423
1439
  *
1424
1440
  * The wait sits outside the other three clocks, which the runtime measures
1425
- * from dispatch onwards — end to end, a caller sees this plus
1426
- * {@link durationMs} plus frame transit. Rising `queueWaitMs` is the sign
1427
- * that admission, not the code, is the bottleneck.
1441
+ * from when the run starts executing — end to end, a caller sees this plus
1442
+ * {@link durationMs} plus frame transit plus the runtime's own wait for a
1443
+ * free CPU before the run starts. That CPU wait is not counted in
1444
+ * `queueWaitMs`, `durationMs`, `wallTimeMs` or `cpuTimeMs`; later CPU waits
1445
+ * are inside {@link durationMs}. Rising `queueWaitMs` is the sign that
1446
+ * admission, not the code, is the bottleneck.
1428
1447
  */
1429
1448
  queueWaitMs?: number;
1430
1449
  /**
@@ -1434,12 +1453,21 @@ interface RunSuccess {
1434
1453
  */
1435
1454
  bridgeCalls: BridgeCallEntry[];
1436
1455
  /**
1437
- * `used_heap_size` of the isolate that served this run, measured after the
1438
- * run settled. Reported for prefix runs — warm instances keep their
1439
- * heap between calls, and this number feeds eviction. `undefined` for
1440
- * one-off `sandbox.run()` runs, whose isolate is disposed with the run.
1441
- */
1442
- heapUsedBytes?: number;
1456
+ * Memory of the isolate that served this run, in bytes: the JS heap plus
1457
+ * buffer memory (ArrayBuffers and typed arrays, including memory grown with
1458
+ * `resize()`, and stream chunks). Read once when the run ends, on prefix
1459
+ * and one-off runs alike; on a prefix it feeds eviction, since warm
1460
+ * instances keep their memory between calls. It includes garbage not yet
1461
+ * collected (a warm instance pushed over its soft line by buffers, with
1462
+ * objects alone under it, is collected once first). A run that sends its
1463
+ * result early (`waitUntil` work, open stream bodies) reports a reading
1464
+ * taken at that moment, before any run-end collection, so it can differ
1465
+ * from the figure the soft line uses. `0` on a prefix run whose instance
1466
+ * could not be measured (failed warm-up, dead instance). `undefined` when
1467
+ * the run was answered without a reading: refused before it started, or a
1468
+ * result too large to send.
1469
+ */
1470
+ memoryBytes?: number;
1443
1471
  /**
1444
1472
  * Present when the run registered background work with `waitUntil`:
1445
1473
  * the value in this result was delivered early, the work keeps running
@@ -1468,12 +1496,21 @@ interface RunFailure {
1468
1496
  queueWaitMs?: number;
1469
1497
  bridgeCalls: BridgeCallEntry[];
1470
1498
  /**
1471
- * `used_heap_size` of the isolate that served this run, measured after the
1472
- * run settled. Reported for prefix runs — warm instances keep their
1473
- * heap between calls, and this number feeds eviction. `undefined` for
1474
- * one-off `sandbox.run()` runs, whose isolate is disposed with the run.
1475
- */
1476
- heapUsedBytes?: number;
1499
+ * Memory of the isolate that served this run, in bytes: the JS heap plus
1500
+ * buffer memory (ArrayBuffers and typed arrays, including memory grown with
1501
+ * `resize()`, and stream chunks). Read once when the run ends, on prefix
1502
+ * and one-off runs alike; on a prefix it feeds eviction, since warm
1503
+ * instances keep their memory between calls. It includes garbage not yet
1504
+ * collected (a warm instance pushed over its soft line by buffers, with
1505
+ * objects alone under it, is collected once first). A run that sends its
1506
+ * result early (`waitUntil` work, open stream bodies) reports a reading
1507
+ * taken at that moment, before any run-end collection, so it can differ
1508
+ * from the figure the soft line uses. `0` on a prefix run whose instance
1509
+ * could not be measured (failed warm-up, dead instance). `undefined` when
1510
+ * the run was answered without a reading: refused before it started, or a
1511
+ * result too large to send.
1512
+ */
1513
+ memoryBytes?: number;
1477
1514
  }
1478
1515
  /**
1479
1516
  * The run was stopped by an abort — a pre-aborted or mid-run `signal` passed to
package/dist/index.mjs CHANGED
@@ -1682,7 +1682,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1682
1682
  const wallTimeMs = reader.readF64();
1683
1683
  const cpuTimeMs = reader.readF64();
1684
1684
  const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
1685
- const heapUsedBytes = reader.readOptionalU64();
1685
+ const memoryBytes = reader.readOptionalU64();
1686
1686
  const backgroundFlags = reader.readU8();
1687
1687
  reader.readU8();
1688
1688
  reader.assertDone();
@@ -1700,7 +1700,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1700
1700
  wallTimeMs,
1701
1701
  cpuTimeMs,
1702
1702
  bridgeCalls,
1703
- heapUsedBytes
1703
+ memoryBytes
1704
1704
  }
1705
1705
  };
1706
1706
  return {
@@ -1718,7 +1718,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1718
1718
  wallTimeMs,
1719
1719
  cpuTimeMs,
1720
1720
  bridgeCalls,
1721
- heapUsedBytes
1721
+ memoryBytes
1722
1722
  }
1723
1723
  };
1724
1724
  }
@@ -1738,7 +1738,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1738
1738
  const wallTimeMs = reader.readF64();
1739
1739
  const cpuTimeMs = reader.readF64();
1740
1740
  const bridgeCalls = readBridgeCallRecords(reader, droppedCallIds);
1741
- const heapUsedBytes = reader.readOptionalU64();
1741
+ const memoryBytes = reader.readOptionalU64();
1742
1742
  reader.assertDone();
1743
1743
  return {
1744
1744
  runId,
@@ -1762,7 +1762,7 @@ function decodeRunCompletionPayload(buf, resultKind, droppedCallIds) {
1762
1762
  wallTimeMs,
1763
1763
  cpuTimeMs,
1764
1764
  bridgeCalls,
1765
- heapUsedBytes
1765
+ memoryBytes
1766
1766
  },
1767
1767
  ...prefixCause === void 0 ? {} : { prefixCause }
1768
1768
  };
@@ -1954,7 +1954,7 @@ function decodePrecompileResultPayload(buf) {
1954
1954
  * u32 oneoffRunning
1955
1955
  * u32 warmBusy
1956
1956
  * u32 warmIdle
1957
- * u64 idleHeapBytes
1957
+ * u64 idleMemoryBytes
1958
1958
  * u64 warmBudgetBytes
1959
1959
  * u64 rssBytes
1960
1960
  * u8 underPressure
@@ -1971,7 +1971,7 @@ function decodeStatsPayload(buf) {
1971
1971
  const warmBusy = reader.readU32();
1972
1972
  const warmRuns = reader.readU32();
1973
1973
  const warmIdle = reader.readU32();
1974
- const idleHeapBytes = reader.readU64();
1974
+ const idleMemoryBytes = reader.readU64();
1975
1975
  const warmBudgetBytes = reader.readU64();
1976
1976
  const rssBytes = reader.readU64();
1977
1977
  const usageBytes = reader.readU64();
@@ -1995,7 +1995,7 @@ function decodeStatsPayload(buf) {
1995
1995
  warmBusy,
1996
1996
  warmRuns,
1997
1997
  warmIdle,
1998
- idleHeapBytes,
1998
+ idleMemoryBytes,
1999
1999
  warmBudgetBytes,
2000
2000
  rssBytes,
2001
2001
  usageBytes,
@@ -4105,7 +4105,7 @@ var SandboxImpl = class {
4105
4105
  openConnections: this.pool.openConnections,
4106
4106
  warmInstances: raw.warmBusy + raw.warmIdle,
4107
4107
  idleInstances: raw.warmIdle,
4108
- idleHeapBytes: raw.idleHeapBytes,
4108
+ idleMemoryBytes: raw.idleMemoryBytes,
4109
4109
  budgetBytes: raw.warmBudgetBytes,
4110
4110
  rssBytes: raw.rssBytes,
4111
4111
  usageBytes: raw.usageBytes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iso4/sandbox",
3
- "version": "0.9.0",
3
+ "version": "0.10.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": [
@@ -42,21 +42,21 @@
42
42
  "dist"
43
43
  ],
44
44
  "devDependencies": {
45
- "@changesets/cli": "^2.27.10",
45
+ "@changesets/cli": "^2.31.1",
46
46
  "@codspeed/vitest-plugin": "^5.7.1",
47
- "@schplitt/eslint-config": "^1.5.0",
48
- "@types/node": "^24.12.4",
49
- "eslint": "^10.2.1",
47
+ "@schplitt/eslint-config": "^1.5.4",
48
+ "@types/node": "^24.19.4",
49
+ "eslint": "^10.15.0",
50
50
  "tsdown": "^0.21.10",
51
- "tsnapi": "^1.2.0",
51
+ "tsnapi": "^1.6.1",
52
52
  "typescript": "^6.0.3",
53
- "vitest": "^4.1.5"
53
+ "vitest": "^4.1.12"
54
54
  },
55
55
  "optionalDependencies": {
56
- "@iso4/v8-darwin-arm64": "^0.9.0",
57
- "@iso4/v8-darwin-x64": "^0.9.0",
58
- "@iso4/v8-linux-arm64-gnu": "^0.9.0",
59
- "@iso4/v8-linux-x64-gnu": "^0.9.0"
56
+ "@iso4/v8-darwin-arm64": "^0.10.0",
57
+ "@iso4/v8-darwin-x64": "^0.10.0",
58
+ "@iso4/v8-linux-arm64-gnu": "^0.10.0",
59
+ "@iso4/v8-linux-x64-gnu": "^0.10.0"
60
60
  },
61
61
  "engines": {
62
62
  "node": ">=24.0.0"