Skip to content

Phase E: remote control of a user's Mac (HTTP server mode, launchd agent, relay) - #52

Merged
SSFSKIM merged 49 commits into
mainfrom
feat/phase-e
Oct 6, 2026
Merged

SSFSKIM merged 49 commits into
mainfrom
feat/phase-e

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Closes #11

Phase E of docs/doperpowers/specs/2026-10-06-remote-and-linux-design.md. A Mac enrolled with cua remote enroll and running cua agent install gives a remote agent (for example Claude Code in a cloud VM) the same cua_repl tool surface it has locally. It is served over MCP Streamable HTTP, either on the Mac's own address (--http) or through cua-relay (relay/), which the Mac dials out to.

What it includes:

  • Credentials: two leg credentials derived from one enrolment secret. Rotating the secret takes effect without restarting the agent and ends the sessions opened with the old credential.
  • Responses: each response is routed to the request that asked for it.
  • Resumability: a named priming event, a retry: hint and a 90 s replay grace, so a call in flight across a relay restart is delivered by Last-Event-ID.
  • Session limits: idle close after 15 min and a cap of 1 with eviction, including eviction of a session whose client left mid-task.
  • launchd: a job in the GUI session, KeepAlive {SuccessfulExit: false}.
  • Doctor: rows agent.installed, agent.running, agent.enrolled and agent.console, plus a skip status.
  • Locked screen: a console_locked refusal while the screen is locked.

The design, every decision taken during execution, and what the live runs found are in the spec's Decision Log, Surprises and Outcomes & Retrospective. The run is reported in docs/doperpowers/specs/2026-10-06-phase-e-execution-report.md. Phase F (Linux) branched from E1's 4595f84 and merges after this.

Validation Evidence

  • Unit tests: npm test at 5c6310f gives # tests 635 / # pass 635 / # fail 0 (main's baseline is 515/515).
  • Without node_modules: npm test in a git archive HEAD copy gives # tests 635 / # pass 607 / # fail 0 / # skipped 28. Each skip says "needs the ws package: run npm ci". This is the plugin-copy condition: ws is loaded only on the relay path.
  • Acceptance 1–7, live (MacBook as client, Mac mini as device): see docs/evidence/2026-10-06-phase-e-remote-acceptance.md. Highlights:
    1. Item 1, over curl, on the LAN and through the relay:
      • initialize returns 200 application/json with an Mcp-Session-Id, and tools/list lists the five tools.
      • A request without the bearer gets 401; without the session header, 400; an initialize carrying a session header, 400.
      • DELETE returns 200, after which the session id returns 404 and its run/ entry is gone.
    2. Items 2 and 3, with claude -p --strict-mcp-config --mcp-config naming the server cua_repl:
      • TextEdit: a marker was typed and read back through the accessibility tree, and the document was closed without saving.
      • Chrome: the school profile was bound, a tab opened on this repo's page, the title read, and the tab closed.
      • The credential occurs 0 times in every transcript.
    3. Item 4: with hooks disabled, Claude Code showed Allow Computer Use to use "Calculator"?. Accepting it let the call proceed.
    4. Item 5, a launchd job installed over SSH:
      • Doctor's four agent.* rows pass.
      • The agent log shows elicitation/create … answered accept after 90 ms and … 264 ms.
      • The agent survives DELETE.
      • With the screen locked (proven on the MacBook): doctor reads agent.console fail, and js is answered console_locked.
    5. Item 6, cua-relay behind a public ngrok TLS tunnel:
      • Items 1–3 pass through it.
      • On a relay restart mid-js, the agent logs connection lost (code 1006), then 502, 502, then reconnected. The client then sends GET … Last-Event-ID: 2-0, the in-flight result is delivered, and the next call runs on the same session.
      • Neither claude -p nor interactive Claude Code sends DELETE on exit.
    6. Item 7:
      • A session left alone closes at 900 s.
      • At cap 1, an Idle session is evicted for a new one; while a session has an open js call, a new initialize gets 503 cua: session limit reached (1).
      • At cap 2, two sessions each ran a native action, and SIGTERM closed both, leaving run/ clean.
  • Final code, live, through the relay:
    • enroll --rotate: the old credential gets 401 and the new one 200, with the agent's pid unchanged.
    • Items 2 and 3 re-run.
    • A js call and end_task.

Unresolved Review Findings

Each of these was triaged and deliberately left. The first six are in tech-debt-tracker.md; the last three are recorded in the spec.

  • Retained events outside the buffer cap. src/mcp/http.mjs: a POST stream that drops and is never resumed keeps its events, outside the 16 MB server-message cap, until the session ends. The idle close bounds this.
  • Import cycle. src/mcp/server.mjs and src/mcp/connection.mjs import each other. It is documented and safe, because nothing at top level crosses it.
  • Log noise in the relay link. src/remote/relay-link.mjs: after it refuses an oversized body, it logs "unknown channel" for each later frame. The shipped relay stops at the same 4 MB first.
  • ws maxPayload. It stays at the 100 MiB default. A single event over about 75 MB would close the device link.
  • No write backpressure. Neither the LAN path nor the relay's WebSocket sends apply backpressure. Only credential holders reach either, and the relay cuts a client after 32 MB unsent.
  • Keepalive comments through SSE-normalising proxies. They are probably dropped by proxies such as ngrok's edge, but they still keep the upstream leg busy.
  • 0.0.0.0 is accepted by --http. This is guidance only; the bearer guards.
  • relayEndpoint loses a relay's path prefix. It derives https://<relay host>/d/<id>/mcp.
  • Eviction after GET exhaustion. A Claude Code client whose GET reconnection was exhausted by a long outage can be evicted mid-task by a second client on a cap-1 device. This is recorded in the Decision Log.

Residue

  • A hosted relay for real use. v1 ran the relay behind a temporary tunnel. Real use needs a stable host behind nginx or Caddy, or Tailscale HTTPS/Funnel, which needs the owner to enable it in the tailnet admin console. The proxy requirements are in relay/README.md.
  • Acceptance from a cloud or Linux client, and through nginx. Item 6's client was the MacBook over the internet. Phase F's VM is a candidate client.
  • A real reboot and login for the launchd job. Item 5 used the bootout/bootstrap cycle. Also not yet exercised on the device itself through launchd: the locked-screen refusal, and the onConsole: false branch.
  • scripts/accept-native.mjs requires doctor.ok. On an enrolled Mac with a locked screen, agent.console now fails, so that acceptance script refuses. Exempting the agent.* rows, or documenting this, is a small follow-up.

SSFSKIM added 30 commits October 6, 2026 02:54
…nt by --mcp-config, the GUI-session vehicle for the LAN proof
…routing, small resumability, idle, cap and eviction
…pped before its first response can be resumed
…pending server requests busy only while a stream is open, cancellation keeps a dispatched request's route
…equests busy only with a stream open, cancellation ends only a withdrawn request's stream, release failures logged, listener errors logged
…vanished client does not keep the session busy
…ion and 2025-11-25 (the live client's first contact was refused)
…tener; claude -p leaves its session to the idle close
…over cua's own plist reader

src/remote/launchd.mjs writes ~/Library/LaunchAgents/com.ssfskim.cua.agent.plist by templating with XML escaping,
checks it by reading it back, and loads it with launchctl bootstrap gui/<uid> (an installed job is booted out and
waited for first); status reads the plist and launchctl print. src/remote/console.mjs reads the IORegistry root from
ioreg -a (IOConsoleUsers, IOConsoleLocked): no compiled helper. launchctl, the user home and the registry read are
parameters, so the tests never touch the real launchd domain, LaunchAgents or console.
…p status, cua agent install|uninstall|status

js and js_reset over HTTP read the console and answer the tool error console_locked while this user's session is off
the console or locked (CUA_AGENT_CONSOLE_CHECK=off turns it off; an unreadable console lets the call through);
messages reach a session's connection in POST arrival order while a call waits for the check. Doctor gains
agent.installed, agent.running, agent.enrolled and agent.console, and the status skip (never fails ok, never counted
as blocked).
…ting 0, agent.console skips on an unenrolled Mac; acceptance 4 observation
…ps on a Mac never enrolled, server requests and their answers in the agent log

The launchd job restarts a crash or a refusal but not a deliberate stop (exit 0); cua's plist reader takes a KeepAlive
boolean or a dictionary of conditions. On a Mac never enrolled with no job, agent.console reads skip like the other
three rows and the console is not read. The HTTP layer logs each server-initiated request when it reaches the client
(method and id) and its answer (accept, decline, cancel, result or error, and the wait), never content. plist.mjs
drops an unused callback parameter.
…, the job's console setting, a light address module

parsePlist refuses a character reference past Unicode as plist_invalid and keeps whitespace-only <string>/<key> text;
agent status and doctor report an unreadable plist path as invalid (agent.installed fail with the error code).
agent.console reads CUA_AGENT_CONSOLE_CHECK from the installed job's environment when there is a job, and uses the
seam's uid for launchd and the console alike. parseAddress moves to src/remote/address.mjs and surfacesFrom to
src/mcp/surface.mjs, so launchd.mjs and doctor no longer load the HTTP stack; install refuses port 0. One isDict in
plist.mjs. runAgent takes a createHttp seam, and a test pins that it wires the real checkConsole by default.
…est (hashes checked before the device map, 4001/4003, ping with two-miss close)
…handler, watchdog, 1-30 s backoff, stop on 4001/4003), both paths served together, ws loaded only on the relay path; relay README
…dler's stream and its answer waits for a resume
SSFSKIM added 19 commits October 6, 2026 04:47
…fic bounds, no-transform, refused upgrade sockets destroyed, install refuses --relay without ws)
…ompleted streams, 4 MB request and 32 MB unsent-response bounds, no-transform on SSE, install refuses --relay without ws, a stop failure never hides the refusal; test hardening
…drops empty-data events, so the in-flight replay hung)
…, so an SSE-normalising proxy keeps it and the client records its id; the relay disconnect test holds with a 1 ms grace; http.mjs lines re-wrapped
…ME Remote control section, relay README rotate step
… GET replaces, pending approvals bounded by their call, wss relay URLs, relay endpoint hint, agent limits into the job)
…); newer GET replaces the standing stream; pending approvals bounded by their call; abandoned mid-task sessions evictable after 60 s; relay URLs wss: or loopback ws:
…how suggest the relay endpoint and say when the job needs install for --relay; doctor fails a relay URL the agent would refuse
…Ls, enroll's relay endpoint line, CUA_AGENT_* set at agent install, abandoned-session eviction; evidence: the LAN address was the mini's en0, not its tailnet address
…tried, rotation ends open sessions, the exhausted-GET eviction case recorded)
…ed, never thrown; a rotated client credential ends every open session; one log line and one terminate per relay URL change
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

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.

Spike: remote control through cua serve (remote Mac GUI and Chrome tabs)

1 participant