Skip to content

Idle runtime stop: CUA_RUNTIME_IDLE_MS (plugin 0.9.0) - #108

Merged
SSFSKIM merged 9 commits into
mainfrom
feat/idle-runtime
Oct 10, 2026
Merged

SSFSKIM merged 9 commits into
mainfrom
feat/idle-runtime

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Oct 10, 2026

Copy link
Copy Markdown
Owner

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, 0 disables) with no js, js_reset, end_task or profiles_list live check. Plugin 0.8.0 → 0.9.0.

  • Timer (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.
  • Stop: it takes the normal end's path. An open task is completed through turn_ended, which releases attachments and leases as end_task does. Then lazyRuntime.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 under run/ 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.
  • Relaunch: the next js/js_reset launches 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_task with no task open answers the usual noop.
  • Race: js, js_reset and end_task that 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 gets task_ending or 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.
  • HTTP agent: its sessions read the same setting through openConnection, and cua agent install carries the variable into the job when it is set.
  • Docs: README Process footprint note, agent sessions and configuration table; spec Decision Log entry for cua serve stops an idle vendor runtime and relaunches it on the next call #107.

Verify

Unit tests in test/serve-lazy.test.mjs use a counting prepareLaunch and a fake clock injected as idleTimer. They cover:

  • the timer arms only after a completed call, at the default;
  • a call during the window resets it;
  • the stop runs the normal cleanup: turn_ended, then teardown; the runtime's pid is gone; run/ keeps only the record;
  • the relaunch counts one launch and carries the notice once;
  • 0 disables, and bad values are refused;
  • the race, during both the completion and the teardown;
  • a close during the stop, and an unconfirmed idle teardown.

Live check with the real runtime, in a scratch HOME and a scratch CUA_HOME (an APFS clone of runtimes/<release> and current.json only), with CUA_RUNTIME_IDLE_MS=4000, computer surface:

  • the first js launched in 4.1 s and returned the API document (19,774 characters);
  • after 7 s, runtime stopped after 4 s idle, and run/ held only <session>.pid;
  • end_task returned noop;
  • the next js relaunched in 457 ms and returned the notice, then the 19,774-character API document;
  • end_task ended the task, and the process exited 0 with run/ empty.

Gates:

HOME=<scratch> npm test

# tests 1090
# pass 1089
# fail 0
# skipped 1

npm run test:mods

 32 pass
 0 fail
Ran 32 tests across 1 file. [0.56s]

claude plugin validate .

✔ Validation passed with warnings

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.
@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-10T10:34:50.604251Z deb3c24 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/mcp/lazy-runtime.mjs
Comment on lines +210 to +214
const teardown = await dying.terminate(options);
runtime = null;
spawned = null;
state = 'idle';
stopped();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread src/mcp/server.mjs
Comment on lines +170 to +172
function arm() {
disarm();
if (idleMs > 0 && !callsRunning && !idleStop && !terminal() && upstream.running) cancelIdle = idleTimer(stopIdle, idleMs);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread src/mcp/lazy-runtime.mjs Outdated
Comment on lines +78 to +79
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@SSFSKIM

SSFSKIM commented Oct 10, 2026

Copy link
Copy Markdown
Owner Author

Review (doperpowers:reviewer-medium) found three issues, fixed in 49d6058, f596082 and 8016ee7:

  • P2: CUA_RUNTIME_IDLE_MS values above 2147483647 overflowed setTimeout to 1 ms. They are now refused (invalid_setting), and the README lists the maximum.
  • P3: a js/js_reset held during an idle stop now counts as an open local task, so a concurrent devices_use cannot switch away under it.
  • P3: if the working directory could not be removed, the relaunch failed with EEXIST and took the connection down. The relaunch now reuses the leftover directory, which is removed at close.

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:
HOME=<scratch> npm test: # tests 1095 / # pass 1094 / # fail 0 / # skipped 1 (the skipped test is Linux-only)
npm run test:mods: 32 pass / 0 fail
claude plugin validate .: ✔ Validation passed with warnings (the existing root CLAUDE.md warning)

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.

cua serve stops an idle vendor runtime and relaunches it on the next call

1 participant