Repository navigation
fix(emergent): bound what QuickJSExecutor copies out of the guest - #168
Conversation
Review of #160 found host copies of guest data with no bound, and three smaller defects in the executor. - A binding's argument is read with a bound: a string's length first, any other value as its JSON, made in the guest by a JSON.stringify taken before guest code runs. What one call hands the host counts against its memory budget, two bytes a character; past it the binding throws RangeError. - A thrown value is reported by its message, cut in the guest at 16,384 characters, and a result is read only as the wrapper's JSON text. - run() refuses a timeoutMs that is not a positive finite number, which the interrupt handler could never reach, and the wait between pumps is held under Node's longest timer. - dispose() drops the call's WebAssembly.Memory, so a host promise that settles late keeps none of it. - The prelude's fetch sends bytes, URLSearchParams (with the form content type) and any other body as fetch reads them, instead of dropping every body that is not a string. - GUEST_PRELUDE has its own TSDoc.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
This review includes 5 billable files and costs up to $1.25.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Or wait 6 minutes for your next included review. View limit detailsLimit details: You’ve used all 4 included reviews currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (5)
Comment |
Reviewer's GuideThe QuickJS executor now bounds and controls every guest-to-host copy using captured guest helpers, validates deadlines, releases guest memory on disposal, and restores fetch body conversions, with corresponding documentation and integration tests. Sequence diagram for bounded QuickJS guest-to-host data transfersequenceDiagram
participant Guest as QuickJS guest
participant Executor as QuickJSExecutor
participant Binding as Host binding
participant Host as Host service
Guest->>Binding: invoke host function
Binding->>Executor: take(argument)
alt string argument
Executor->>Executor: readString(length)
else other data argument
Executor->>Guest: captured JSON.stringify(value)
Guest-->>Executor: JSON text
Executor->>Executor: readString(json)
end
alt memory budget exceeded
Executor-->>Guest: RangeError
else within budget
Executor->>Host: call with bounded data
Host-->>Executor: result
Executor-->>Guest: resolved value
end
Sequence diagram for QuickJS result and thrown-value boundariessequenceDiagram
participant Guest as QuickJS guest
participant Executor as QuickJSExecutor
participant Host as Host runtime
Guest->>Host: execute forged code
alt code returns
Host->>Guest: wrapper result as JSON text
Guest-->>Executor: JSON string or undefined
Executor->>Executor: readResult()
alt result is not JSON text
Executor-->>Guest: execution error
else valid JSON text
Executor-->>Guest: parsed result
end
else code throws
Host->>Guest: thrown value
Guest->>Guest: captured message/String and cut at 16,384 characters
Guest-->>Executor: bounded error text
Executor-->>Guest: execution error
end
State diagram for QuickJS executor call validation and disposalstateDiagram-v2
[*] --> ValidateTimeout
ValidateTimeout --> Rejected: timeoutMs not positive finite
ValidateTimeout --> Running: timeoutMs valid
Running --> Completed: result or error read
Running --> Interrupted: deadline or abort signal
Running --> Disposed: dispose()
Completed --> Disposed
Interrupted --> Disposed
Disposed --> MemoryReleased: drop context, runtime, WebAssembly.Memory
Rejected --> [*]
MemoryReleased --> [*]
Flow diagram for QuickJS fetch body conversionflowchart LR
Input[Guest fetch body] --> Kind{Body type}
Kind -->|string| Text[Send as text]
Kind -->|ArrayBuffer or view| Bytes[Convert to bytes]
Kind -->|URLSearchParams| Form[Send form text]
Kind -->|other non-null value| Stringify[String body value]
Form --> Header[Add form content type if absent]
Text --> Broker[Host fetch broker]
Bytes --> Broker
Header --> Broker
Stringify --> Broker
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoBound QuickJS guest-to-host copies and correct fetch body handling
AI Description
Diagram
High-Level Assessment
Files changed (5)
|
Code Review by Qodo
1. Long-running tools hit a lifetime cap on host calls
|
| const bytes = counted ? 2 * length : 0; | ||
| if (bytes > this.handLimit - this.handed) { | ||
| throw new RangeError(`the data this call handed the host passed its limit of ${Math.round(this.handLimit / MIB)} MB`); | ||
| } |
There was a problem hiding this comment.
1. Long-running tools hit a lifetime cap on host calls 🐞 Bug ≡ Correctness
readString adds every argument's size to this.handed and never takes it back, so handLimit (the memory budget) caps the total data passed to all host functions over the whole call. That data is freed as soon as each call returns, so this caps throughput, not memory. The prelude sends whole bodies through host functions: Response.text()/json() pass the body as latin1 to text_decode, and crypto_update sends bytes as JSON, which escapes control bytes to six characters each. As a result, a call that reads two 20 MB responses under a 64 MB budget, decodes one response over 32 MB, hashes a few MB of zero bytes, or runs a million localeCompare calls in a sort now gets RangeError, even though the response-body budget allowed the fetches and no memory was ever held.
Agent Prompt
## Issue description
`GuestRun.readString` adds each host-function argument's size to `this.handed` and never subtracts it. The memory budget therefore caps the total data passed to host functions over the whole call, even though each copy is freed once the binding returns. Ordinary tools fail with RangeError: ones that decode responses (`Response.text()` passes the whole body), hash binary data (JSON escaping makes it larger) or call Intl/localeCompare many times.
## Fix Focus Areas
- src/cognition/emergent/executor/QuickJSExecutor.ts[596-615]
- src/cognition/emergent/executor/QuickJSExecutor.ts[517-529]
## Recommended Fix
Bound what a single host-function invocation copies, not the running total. Reset `handed` to 0 at the start of each `install` callback, before `args.map(take)`, so the limit applies to the sum of one call's arguments. Another option is to add the bytes back once the binding's synchronous part has returned. Update the docs table and the integration test to match (for example, a single argument larger than the budget is refused).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
## [0.13.1](v0.13.0...v0.13.1) (2026-10-08) ### fix * **emergent:** bound what QuickJSExecutor copies out of the guest (#168) ([5dd510e](5dd510e))
Follow-up to #160 (the QuickJS executor), from its review.
What changes
String.prototype), and any other value crosses as its JSON, made in the guest by aJSON.stringifycaptured before guest code runs. What one call hands the host counts against its memory budget, two bytes a character; past it the host function throwsRangeError. Before this,install()copied each argument withdump(), so a guest could make the host copy up to about its whole memory on every call of a host function.JSON.stringifygets an error instead of an unbounded copy).timeoutMsthat is NaN, infinite or not positive is refused: the interrupt handler compares the clock with a deadline such a value never brings. The wait between pumps stays under Node's longest timer.dispose()drops the call'sWebAssembly.Memory. A host promise still settling after the call (a read without a ceiling has no abort) kept the run, and with it the memory, reachable.fetchsends bytes as bytes,URLSearchParamsas form text with the form content type when none is named, and any other body as its string, as fetch reads a body. It dropped every body that was not a string. Under a ceiling the broker still sends GET and HEAD only.GUEST_PRELUDEhas its own TSDoc.docs/architecture/EMERGENT_CAPABILITIES.md: the two new bounds in the limits table and the executor section, the body rule, the timeout rule.Tests
tests/emergent/quickjs-executor.integration.spec.ts, new block "what the guest hands the host": the argument budget (eight copies of a four-million-character path fit a 64 MB call, the ninth is refused), the cut thrown message, the result that is not JSON text, four bad timeouts, and the four body types reaching the host'sfetch. The dropped memory has no test: the run is internal and the drop shows only to a garbage collector.🤖 Generated with Claude Code
Summary by Sourcery
Bound QuickJS guest-to-host data transfer and harden executor lifecycle, timeout, result, error, and fetch handling.
Bug Fixes:
Enhancements:
Documentation:
Tests:
Chores: