codemode: JS scripts that call tools in a QuickJS sandbox #141

Merged
hermes merged 2 commits from feat/codemode into main 2026-10-09 10:51:24 +00:00
Collaborator

Adds pi-style codemode (opt-in: codemode = true in config).

The model sends raw JS (body of an async function) and it runs in a QuickJS sandbox (rquickjs 0.14) with no fs, network or timers. Only the script output goes back to the model.

  • tools.<name>(args) calls any registered tool, MCP included (- becomes _). It resolves to the text output and throws ToolError when the tool errors. Calls go through Registry::call, so the permission gate and the read-before-edit State work exactly as for direct calls.
  • Globals: text(), return, console.* (shown in <console_output>), describeTool(name). Several text items get ==> text N/M <== headers.
  • Description lists tools.x({a: string, b?: integer}): first sentence, with a 12k-byte budget; anything past it goes to describeTool.
  • Limits: 256 MB VM, 1 MB stack. timeout defaults to 300s and covers both JS loops (interrupt handler) and awaited tools. Agent abort stops the script (drop flag). A promise that never settles fails at once. Output over ~10k tokens is head/tail cut, with the full text in a temp file.
  • Runs on its own thread with a current-thread tokio runtime, because QuickJS values are !Send.
  • Registry snapshot is taken after MCP, lens and task are added, so codemode cannot call itself.

Not included: pi's models (classifier/image), image(), searchTools, describeNamespace. Inner tool calls emit no agent events, so the TUI shows only the codemode call.

Cost: hx prefix with codemode on adds +490 tok (1249 to about 1739 with the default tool set). More MCP tools grow it, up to the listing budget (~3k tok). Off by default, so main's prefix is unchanged. Not yet measured on a real session whether scripts save more than the 490 they cost.

New dependency: rquickjs (compiles QuickJS C, needs cc).

Tests: 4 unit tests (parallel calls + filtering, ToolError/throw/partial output, unknown tool, syntax error, hanging promise, tool timeout, infinite loop, signatures/ident). tests/all.sh green. Note: running all.sh with an exported CARGO_TARGET_DIR makes the hx-lens real-cargo tests race; that is not caused by this PR.

Adds pi-style `codemode` (opt-in: `codemode = true` in config). The model sends raw JS (body of an async function) and it runs in a QuickJS sandbox (rquickjs 0.14) with no fs, network or timers. Only the script output goes back to the model. - `tools.<name>(args)` calls any registered tool, MCP included (`-` becomes `_`). It resolves to the text output and throws `ToolError` when the tool errors. Calls go through `Registry::call`, so the permission gate and the read-before-edit State work exactly as for direct calls. - Globals: `text()`, `return`, `console.*` (shown in `<console_output>`), `describeTool(name)`. Several text items get `==> text N/M <==` headers. - Description lists `tools.x({a: string, b?: integer}): first sentence`, with a 12k-byte budget; anything past it goes to `describeTool`. - Limits: 256 MB VM, 1 MB stack. `timeout` defaults to 300s and covers both JS loops (interrupt handler) and awaited tools. Agent abort stops the script (drop flag). A promise that never settles fails at once. Output over ~10k tokens is head/tail cut, with the full text in a temp file. - Runs on its own thread with a current-thread tokio runtime, because QuickJS values are !Send. - Registry snapshot is taken after MCP, lens and task are added, so codemode cannot call itself. Not included: pi's `models` (classifier/image), `image()`, `searchTools`, `describeNamespace`. Inner tool calls emit no agent events, so the TUI shows only the codemode call. Cost: `hx prefix` with codemode on adds +490 tok (1249 to about 1739 with the default tool set). More MCP tools grow it, up to the listing budget (~3k tok). Off by default, so main's prefix is unchanged. Not yet measured on a real session whether scripts save more than the 490 they cost. New dependency: rquickjs (compiles QuickJS C, needs cc). Tests: 4 unit tests (parallel calls + filtering, ToolError/throw/partial output, unknown tool, syntax error, hanging promise, tool timeout, infinite loop, signatures/ident). `tests/all.sh` green. Note: running all.sh with an exported CARGO_TARGET_DIR makes the hx-lens real-cargo tests race; that is not caused by this PR.
hermes added 1 commit 2026-10-09 10:27:46 +00:00
hermes added 1 commit 2026-10-09 10:31:56 +00:00
hermes merged commit e4694e126b into main 2026-10-09 10:51:24 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#141