Repository navigation
Idle runtime stop: CUA_RUNTIME_IDLE_MS (plugin 0.9.0) - #108
Conversation
The connection's runtime (lazy-runtime.mjs) gains stop(): it tears the running runtime down and returns to the state before a launch, the handshake kept, so the next call that needs a runtime launches afresh; a stopped runtime's messages and exit no longer reach the connection, and requests that arrive while it stops are held for the fresh one. createServer arms an idle timer (CUA_RUNTIME_IDLE_MS, default 15 min, 0 disables) when the last js, js_reset, end_task or profiles_list live check completes, clears it at the next, and on expiry completes an open task and stops the runtime: the normal end's path, the MCP connection and MAWS dial kept. Calls for the runtime that arrive during the stop wait for it; the next js/js_reset result opens with a notice that the runtime restarted after idling and its REPL state is gone. The stopped runtime's working directory is removed; an unconfirmed idle teardown makes the close code 1.
Served in process against the fake installed release: the default 15 min timer arms only after a completed call while a runtime runs, the stop completes the open task and tears the runtime down (its working directory gone, the run record and app approvals kept), a no-op end_task follows, and the next js relaunches once with the restart notice first; CUA_RUNTIME_IDLE_MS=0 arms nothing and a bad value is invalid_setting. Under createServer with fake runtimes: a running call or live profiles_list check is never idle and the next call clears the timer; calls racing the stop's completion and its teardown wait and run on the fresh runtime, the dying one's messages and exit dropped; a close during the stop refuses the held call and an unconfirmed idle teardown makes the close code 1.
The HTTP agent's sessions open through openConnection, so they read the same setting; agent install now carries it into the launchd job or systemd unit when set, refused as agent run refuses it. The parser lives in lazy-runtime.mjs so the launchd and doctor modules stay off the MCP server's import graph.
README: the Process footprint note gains the idle rule (what counts as use, the normal end's cleanup, what stays, the restart notice, calls racing the stop), the agent's sessions say they stop too, and the configuration table lists CUA_RUNTIME_IDLE_MS. Spec Decision Log: board #107.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: deb3c24080
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const teardown = await dying.terminate(options); | ||
| runtime = null; | ||
| spawned = null; | ||
| state = 'idle'; | ||
| stopped(); |
There was a problem hiding this comment.
Refuse relaunch after an unconfirmed idle teardown
When dying.terminate() returns confirmed: false, this still resets the runtime to idle, invokes cleanup that removes its working directory, and allows held or future calls to launch a replacement. Since an unconfirmed result explicitly means owned processes may still be alive, the replacement can overlap with the old runtime under the same session and compete for desktop/browser control. Keep the connection failed or otherwise prevent relaunch unless teardown is confirmed.
Useful? React with 👍 / 👎.
| function arm() { | ||
| disarm(); | ||
| if (idleMs > 0 && !callsRunning && !idleStop && !terminal() && upstream.running) cancelIdle = idleTimer(stopIdle, idleMs); |
There was a problem hiding this comment.
Track forwarded requests before arming the idle timer
The timer only checks callsRunning, which excludes requests sent through passThrough. With a short configured idle interval, a live tools/list page or another forwarded MCP request can still be pending when the timer stops the runtime; lazyRuntime then discards the dying runtime's response, while its entry in upstreamPending is neither replayed nor settled, so the client waits indefinitely. Count all in-flight upstream requests or explicitly settle/replay them when stopping.
Useful? React with 👍 / 👎.
| if (!/^\d{1,10}$/.test(value)) fail('invalid_setting', 'CUA_RUNTIME_IDLE_MS must be a whole number of milliseconds (0 never stops the runtime)'); | ||
| return Number(value); |
There was a problem hiding this comment.
Reject idle delays that overflow Node timers
CUA_RUNTIME_IDLE_MS accepts ten-digit values, including values above Node's maximum timer delay of 2,147,483,647 ms. Node clamps such a setTimeout delay to 1 ms, so a setting such as 2592000000 intended to retain the runtime for 30 days instead tears it down almost immediately after each call. Reject values above the timer limit or implement long delays without passing an overflowing value to setTimeout.
Useful? React with 👍 / 👎.
|
Review (doperpowers:reviewer-medium) found three issues, fixed in 49d6058, f596082 and 8016ee7:
The reviewer found no faults in the stop's state machine, the race handling, or close during each phase of the stop. Gates after the fixes: |
Closes #107. Follow-up to #105 (plugin 0.8.0, the lazy launch).
What
A launched runtime stops after
CUA_RUNTIME_IDLE_MS(default 15 min,0disables) with nojs,js_reset,end_taskorprofiles_listlive check. Plugin 0.8.0 → 0.9.0.src/mcp/server.mjs): it arms when the last counted call completes, or when a handshake launched the runtime, and only while a runtime is running. The next counted call clears it, so a running call never counts as idle.turn_ended, which releases attachments and leases asend_taskdoes. ThenlazyRuntime.stop()(src/mcp/lazy-runtime.mjs) tears the runtime down within the close's budget and returns to the state before a launch, keeping the handshake record. The stopped runtime's messages and exit are dropped, and its working directory underrun/is removed. The run record, the app-approval file (approvals are per connection), the MCP connection and the MAWS dial all stay. One diagnostics line:runtime stopped after N min idle.js/js_resetlaunches through cua serve launches the vendor runtime lazily, on the first call that needs it #105's lazy path. Its result opens with a text block saying the runtime restarted after idling and its REPL state (variables, app handles, tabs) is gone; the fresh runtime's API document follows. After a stop,end_taskwith no task open answers the usual noop.js,js_resetandend_taskthat arrive while the stop runs, during the completion or the teardown, wait for it (and can still be cancelled), then run on the fresh runtime. None of them getstask_endingor an error from the dying runtime. A close during the stop refuses the held calls. An unconfirmed idle teardown is logged and makes the close code 1.openConnection, andcua agent installcarries the variable into the job when it is set.Verify
Unit tests in
test/serve-lazy.test.mjsuse a countingprepareLaunchand a fake clock injected asidleTimer. They cover:turn_ended, then teardown; the runtime's pid is gone;run/keeps only the record;0disables, and bad values are refused;Live check with the real runtime, in a scratch HOME and a scratch
CUA_HOME(an APFS clone ofruntimes/<release>andcurrent.jsononly), withCUA_RUNTIME_IDLE_MS=4000, computer surface:jslaunched in 4.1 s and returned the API document (19,774 characters);runtime stopped after 4 s idle, andrun/held only<session>.pid;end_taskreturned noop;jsrelaunched in 457 ms and returned the notice, then the 19,774-character API document;end_taskended the task, and the process exited 0 withrun/empty.Gates:
HOME=<scratch> npm testnpm run test:modsclaude plugin validate .