Commit 92403d8
committed
fix(runtime): sandbox budget = script CPU-time, not wall clock (ADR-0102 D1, #3295)
Phase 1 of #3275. The QuickJS sandbox now meters each hook/action invocation
against VM-active (CPU) time, not wall clock. Idle host-await time and a nested
hook's own execution (host-side, while the caller VM is parked) are no longer
charged to the caller — so a slow/loaded host or a deep nested-write chain can't
trip the budget while a script is merely waiting (the #3259 flake root cause). A
separate wall-clock ceiling (default 30s, max(ceiling, cpuBudget)) backstops a
body stuck on a never-settling host call.
- runner: slice-stopwatch every VM entry (evalCode / evalCodeAsync / each
executePendingJobs); the interrupt handler and post-slice checks compare
against the CPU budget and the wall deadline; distinct errors "exceeded CPU
budget of Nms" vs "exceeded wall-clock ceiling of Nms while awaiting host
calls". A mid-slice interrupt — which for an async body surfaces via __error,
not pending.error — is mapped to the clean budget message at all three sites.
- new knobs: QuickJSScriptRunner wallCeilingMs + env OS_SANDBOX_WALL_CEILING_MS
(resolveSandboxTimeoutMs gains a 'wallCeiling' kind). Precedence unchanged:
explicit option > env > built-in default.
- fix: init sliceStart to `start`, not 0 — a 0 init made `now - sliceStart` the
full epoch millis, firing the interrupt during installCtx and corrupting ctx
marshalling (__input never set).
- tests: rewrite never-settling-host timeout tests to CPU-burn bodies (assert
/CPU budget of Nms/) + dedicated small-wallCeilingMs runners for the genuine
stuck-host cases (assert /wall-clock ceiling/); add the #3259 root-cause guard
(idle host time resolves) and CPU-vs-ceiling coverage.
- nested-write*.integration: drop the explicit 10s budget — they pass at the
stock 250ms now, the nested-charging-fix regression guard.
- docs: environment-variables.mdx CPU-time wording + OS_SANDBOX_WALL_CEILING_MS;
ADR-0102 D1 → Accepted. Changeset carries the FROM→TO semantics note.
574 runtime tests + 26 types tests green.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011iJLjqToxNv1aYP3syQtRp1 parent 52a2d74 commit 92403d8
9 files changed
Lines changed: 284 additions & 106 deletions
File tree
- .changeset
- content/docs/deployment
- docs/adr
- packages
- runtime/src/sandbox
- types/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
335 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
336 | 337 | | |
337 | 338 | | |
338 | 339 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
0 commit comments