Skip to content

Launch the vendor runtime lazily: handshake with no runtime, launch at the first call (board #105) - #106

Merged
SSFSKIM merged 6 commits into
mainfrom
feat/lazy-runtime
Oct 10, 2026
Merged

SSFSKIM merged 6 commits into
mainfrom
feat/lazy-runtime

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Board #105. Every Claude Code session starts its plugin MCP servers at launch, and cua serve used to launch the vendor runtime (cua_node, node_repl, sandbox) in openConnection, before any call: one idle stack per open session. Now the runtime launches lazily.

What changes

  • src/mcp/lazy-runtime.mjs (new). It is an upstream for createServer with the same send/onMessage/onExit/terminate shape as spawnUpstream, with the launch behind it.
    • Handshake without a runtime. initialize and tools/list are answered from a record of the runtime's own answers, kept under $CUA_HOME/state/handshake/<release>/<surfaces>@<protocolVersion>.json. The record is written atomically on the first live handshake. A record miss (the first connection after an install, or a new client protocol version) launches at the handshake as before and records the answers.
    • The launch. It happens on the first tools/call that reaches the runtime (js, js_reset, or the turn_ended of a task such a call opened). The runtime is opened with the client's own initialize params, and cua sends notifications/initialized itself, so the client's is never forwarded. Calls arriving during the launch are held in order.
    • Before a launch. ping is answered locally, other requests get -32601, and notifications are dropped.
    • A failed launch answers the held calls with its reason (cua: the runtime could not be started (<code>): …), then fails the connection.
    • A close while a launch has not spawned yet abandons it: the held calls are answered connection_closing, and their task completes with nothing to tell a runtime. terminate of a connection that never launched confirms {confirmed: true, steps: []}.
  • openConnection. It still resolves the runtime, claims the run entry, resolves secrets and runs the sandbox checks (assertSandboxFits, assertSandboxConfines) at open, against the planned launch. A misconfiguration therefore still fails before the handshake. buildLaunch → prepareLaunch → mkdir → spawnUpstream moved into the lazy start; prepareLaunch is called once per launch.
  • MAWS client mode. serve still dials the backends at start. The waitForHellos moved from serve into the launch, so the launch keeps preferring the first backend's instance. The wait is abandoned at close.
  • createServer changed by one line: upstream.closing?.() at the start of close. The HTTP agent goes through openConnection too, so its sessions are lazy as well (README updated).
  • README: the first call now carries the launch; a new "Process footprint" note; the MAWS hello wait now happens at the launch; agent sessions; state/handshake in the CUA_HOME row. Spec Decision Log: board cua serve launches the vendor runtime lazily, on the first call that needs it #105.
  • Plugin version 0.7.0 → 0.8.0, rebased onto main after PR Socket peer authentication on the client-mode relay; the #95 spec (board #95) #104 (fb9db34), keeping its --disable-sigusr1 shim args.

Why a record rather than a rebuilt tool list

The vendor launcher builds the js/js_reset descriptions from its instruction files. The input schemas and the initialize result, though, come from the node_repl binary. A rebuilt answer could drift from what the runtime would say; a record of the runtime's own answers is exact by construction. The cost is one launch at the handshake per release, surface set and protocol version.

Verification

New test/serve-lazy.test.mjs uses a counting prepareLaunch with in-process serve and a scratch CUA_HOME running the fake upstream. It proves:

  • Zero launches for initialize, tools/list, ping, secrets_list, devices_list, a no-op end_task and profiles_list's registry part.
  • Exactly one launch at the first js, whose answer is unchanged; none at the second js.
  • The runtime sees initialize (the client's params), then notifications/initialized once, then the call.
  • Real cua serve processes with a recorded handshake exit 0 on EOF, SIGTERM, SIGINT and SIGHUP, with no launch, no stderr and an empty run/.
  • The MAWS hello gates the launch, not the handshake.
  • Unit paths: a failed launch, a close during a launch, and cold recording.

Live check: a scratch CUA_HOME (an APFS clone of the installed 26.928.40906 release) with a scratch HOME, browser surface, real bin/cua.mjs serve:

  • The cold handshake launched (3.0 s) and wrote [email protected].
  • The warm handshake was answered in 79 ms with no child process, and its initialize and tools/list were identical to the cold ones.
  • The first js launched the runtime and returned the API document (19,776 chars); the second returned 4; end_task returned ended; exit 0; run/ empty.

The handshake record holds only the runtime's raw initialize/tools/list answers (lazy-runtime.mjs: record = {initialize: pendingRecord.initialize, tools: msg.result}, taken from the runtime's reply before createServer rewrites it). cua's per-connection transforms (withHostNotes, server.mjs:250; modelTools(..., {surfaces, platform, devices, inAppBrowser}), server.mjs:252) are applied to every reply, recorded or live. This is pinned by the test 'the record is the runtime's raw answers; a MAWS session and a plain one replaying it each get their own descriptions'.

Gates on the rebased head (HOME=<scratch>):

$ HOME=/tmp/lazy-home npm test
# tests 1085
# suites 0
# pass 1084
# fail 0
# cancelled 0
# skipped 1
# todo 0
# duration_ms 37844.98375

$ HOME=/tmp/lazy-home npm run test:mods
 32 pass
 0 fail
Ran 32 tests across 1 file. [0.50s]

$ claude plugin validate .
  ❯ root: CLAUDE.md at the plugin root is not loaded as project context. To ship context with your plugin, use a skill (skills/<name>/SKILL.md) instead.
✔ Validation passed with warnings

(The validate warning was already on main.)

Review

An independent review (doperpowers reviewer-medium) found three issues, all fixed in 6c95622:

  • A spawn error reported before the exit handler was attached left the first js held forever. A handshake that cannot be sent now fails the launch with runtime_exited.
  • profiles_list now waits for the MAWS hellos, which the handshake no longer does. Before this, a profiles_list arriving before the first hello could report maws_unreachable while MAWS was up.
  • A test assertion that could never fail was replaced with a real check.

Out of scope

  • Idle shutdown of a running runtime (a later ticket).
  • Pre-recording the handshake at cua install / cua runtime use, so that even the first connection of a release launches nothing.
  • The native acceptance script (scripts/accept-native.mjs) was not run: it drives the owner's GUI with the real home. The live check above covered the browser surface with scratch homes.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T09:48:57.213429Z a20b6f0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

cua serve answers initialize and tools/list from the runtime's recorded
handshake (state/handshake/<release>), launches the runtime on the first
js/js_reset and keeps it for the connection. A record miss launches at
the handshake as before and records the answers. The MAWS hello wait
moves from serve start to the launch; sandbox checks stay at the open on
the planned launch. A close abandons a launch that has not spawned yet.
…s, clean unlaunched close

A counting prepareLaunch proves no launch for the handshake, tools/list,
ping, secrets_list, devices_list, a no-op end_task and profiles_list's
registry part; one at the first js and none at the second. Real cua serve
processes with a recorded handshake exit 0 on EOF, SIGTERM, SIGINT and
SIGHUP with nothing launched or left. The MAWS hello wait now gates the
launch, not the handshake.
README: the first call carries the launch, a Process footprint note, the
MAWS hello wait at the launch, agent sessions and state/handshake in
CUA_HOME. Spec Decision Log: board #105 with the live scratch-home check.
…med per connection

A plain session records; a MAWS session with its own host notes replays
the same record with no launch and gets its own instructions and the
MAWS rule in profiles_list. The record carries none of cua's tools, host
notes or js rules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant