Skip to content

#237: end_task frees the engine; engine_busy names its holders; a desktop gate for engine tests - #252

Merged
SSFSKIM merged 13 commits into
mainfrom
fix/237-engine-busy-after-end-task
Nov 1, 2026
Merged

SSFSKIM merged 13 commits into
mainfrom
fix/237-engine-busy-after-end-task

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

After a session's end_task, cua's macOS engine kept counting that session's REPL host as connected, so cua engine build and approve from any other session refused engine_busy. That lasted until the first session's runtime stopped, which could be up to 15 minutes later. It cost two owner slots. Now end_task closes the session's engine connection, engine_busy names who holds the engine, and a busy engine build refuses in well under a second, before it compiles anything.

Cause. cua's engine client opens one connection per REPL host, lazily, on the first engine-bound request (src/services/engine/service.mjs), and nothing ever closed it. end_task only hid the cursor, and EngineClient.close() had no caller. The runtime's idle stop, 15 minutes after the last call, was the only thing that closed it.

Fix:

  • end_task closes the connection (service.mjs, client.mjs). The turn-ended hook sends the cursor hide, if the session has a cursor, and closes the connection. Neither is waited for, so end_task's reply isn't delayed. The next engine-bound request reconnects lazily: milliseconds against a running engine.
    • The release starts a new client generation. A connect still in flight at end_task closes its socket instead of publishing the connection, and requests still queued from the old task fail with a Released transport error.
    • js_reset is unchanged.
  • The engine finishes two things for a closed connection (Server.swift, Activation.swift, Cursor/CursorHub.swift). The connection leaves the open map at once, so status and the shutdown's busy count drop at the close. Two things still finish:
    • Own engine brings the driven app to the front in MAWS sessions (MacBook opt-in build) #200's after-action watch. It runs to its natural end under its unchanged rules, because end_task usually follows a click closely and a late raise still has to be repaired. The next execute on any connection cancels it; ping, status and cursor don't. Server.retire publishes the watch and removes the connection in one step under the server lock. Concurrent drains wait for each other.
    • A cursor fade under way. The 0.44 s fade plays out before the panel closes.
    • Everything else goes at once, as before.
  • Holders (Server.swift, Methods.swift, src/runtime/engine.mjs). The engine records each connection's peer pid at accept (LOCAL_PEERPID; the uid check is unchanged) and the session's cursor label. status adds holders: [{pid, label}], and engine_busy and cua engine status name them: pid 42091 (MAWS). An engine without holder support still gives the count.
  • cua engine build checks busy first. It runs the same status read before the Swift build, staging and signing. The check at swap time stays the authority against a client that connects meanwhile.
  • Desktop gate for the engine's unit tests (DesktopGate.swift, test target EngineTestGate).
    • What happened: while this PR was being built, a new engine unit test ran an execute on a real Server and launched TextEdit on the owner's Mac. It was killed after about 30 s.
    • The gate: every engine call that acts on the real desktop now goes through one gate. That covers app launch and activation, CGEvent posts, AX actions and attribute writes (including the enablement write), the screen-update hold, the general pasteboard, the cursor panel's ordering, the permission prompts, and ScreenCaptureKit captures.
    • Closed in tests: the gate is closed whenever XCTest is loaded. An observer registered at bundle load turns any refusal into a failure of the running test, so no test can drive the desktop by accident. Proving tests check that an unstubbed launch, post, activation, AX write and prompt each fail instead of acting.
    • Open everywhere else: in the shipped, probe and debug engines it is open, at the cost of one flag read, and call names are built lazily. CI's engine-swift job runs the test bundle, so it runs with the gate closed.

Plugin 0.20.4 (its own commit).

Measured (live, MacBook, 2026-11-01; docs/evidence/2026-11-01-engine-busy-end-task/)

  • Against the installed engine (build dd7a09235885, which predates holder naming; client at this branch; no GUI; a node MCP client drove this branch's cua serve):
    • Before each end_task, engine status read 1 other connection.
    • The first poll after each end_task read 0. It started 66 ms and 22 ms after the request, and the count was still 0 at +2 s.
    • The second list_apps cell reconnected.
    • engine build with the holder connected refused engine_busy in about 210 ms. The engine directory was untouched: no staging directory, mtimes unchanged. The run set CUA_SIGNING_IDENTITY because build checks for an identity first; nothing was signed.
  • Holder naming, against a scratch engine (this branch's debug build on a private socket under a scratch HOME, no TCC), with two stand-in holders:
    • status read 2 other connection(s): pid 73206 (MAWS), pid 73207 (cursor235).
    • engine stop refused engine_busy naming both.
    • The count went to 1 and then 0 as each holder left.

Limits

  • Per-connection engine state now ends at every end_task:
  • A session that never calls end_task still holds the engine until the 15-minute idle stop. Whoever is blocked now sees who to ask.
  • A build or approve within about 1 s of a task's last action can cut that action's late-raise repair: the shutdown cancels a closed connection's watch rather than waiting for it.
  • The vendor runtime's own connection lifetime across end_task was not read.

Tests

  • Engine (XCTest), full suite 665/665 with the gate closed:
    • the deferred watch: a late raise 200 ms after the close is repaired; an execute on another connection cancels it; the count drops at the close; retire publishes the watch before the connection is seen as closed; a concurrent drain waits;
    • the deferred fade;
    • holders, including the real LOCAL_PEERPID;
    • the re-enable after end_task;
    • DesktopGateTests.
  • JS:
    • the client's release (hide then close, a delayed ping, a request queued at release, lazy reconnect) and the service's end-task hook (test/services-engine-client.test.mjs, test/services-engine-service.test.mjs);
    • the CLI's holder naming and the early build check (test/runtime-engine*.test.mjs);
    • an end-to-end holders test against a real debug engine (test/native-engine.test.mjs).
  • Reviews (reviewer-high):
    • the first build: three P2 races, fixed and re-reviewed (correct);
    • the gate and the build check: one P2 (permission prompts outside the gate) and one P3 (eager call names), fixed and re-reviewed: correct.
  • CI: ci-gate on 76be59e: plugin, engine-swift, sandbox-live and test PASS (run 41291865985). parity-browser went red there on the known racy waitfor cell alone (parity-browser hosted gate: fix the two racy parity cells (waitfor deadline race, drag ended wait), then run it on PRs #168) and passed on its re-run (run 41293528519, GREEN). The PR's own checks are green (run 41291839928).

Docs

Closes #237

Sminos-Session: cursor235#b93055dd (cua on macbook)

…lose, waiting on neither)

The engine service's turn-ended handler now calls the client's release: the
session's {hide: true} (when it has a cursor) and the close are written at
once on the live connection, so the engine stops counting it and engine
build/approve proceed. The next engine-bound request reconnects as after any
close. hideCursor is replaced by release.

Sminos-Session: cursor235#b93055dd (cua on macbook)
… on the engine's side

A client's close no longer ends its connection's #200 after-action watch: the
watch runs to its natural end under its own rules, held by the engine's
ClosedConnectionWatches, and the next execute on any connection (or the
engine's shutdown) ends it first. The connection leaves the open map at once,
so status and shutdown's busy count drop at the close. A cursor fading at the
close keeps its panel until the fade has played out. Menu span, focus hold,
intervention drives and enablement still go at the close.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…bel)

The engine reads each admitted connection's peer pid at accept
(LOCAL_PEERPID; the uid check stays) and keeps the session's label from its
cursor requests, whatever the cursor switches. status adds holders: [{pid,
label}] for the other connections, keeping connections; shutdown's busy
refusal carries them too. cua engine stop/build/approve's engine_busy and
cua engine status name them (pid 42091 (MAWS)); an engine without holders
still gives the count.

Sminos-Session: cursor235#b93055dd (cua on macbook)
Condition 2's evidence on the EnablementAccess seam: the release at close,
one write at the next connection's first observation (which waits for the
page), none at its warm observations.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…down and holders

Sminos-Session: cursor235#b93055dd (cua on macbook)
…equests

A connect in flight at end_task could publish a live connection when its ping
answered and send the ended task's request after end_task returned, leaving
the engine busy again; requests queued on the tail could reconnect the same
way. release now starts a new generation: a connect of an older one closes its
socket instead of publishing, and an older queued request fails unsent
(Released). A request made after release still reconnects lazily.

Sminos-Session: cursor235#b93055dd (cua on macbook)
… or a drain

Two races in the deferred watch. The close removed the connection from the
open ones before adopting its watch, so a request or a shutdown in that gap
found neither; Server.retire now adopts the watch and removes the connection
in one step under the server's lock, before any other teardown. And
cancelAll emptied the registry before cancelling outside the lock, so a
second concurrent drain went ahead while the first still waited on a sample;
drains are now serialized, so none returns before the watches it raced stop.

Sminos-Session: cursor235#b93055dd (cua on macbook)
Sminos-Session: cursor235#b93055dd (cua on macbook)
… the unit tests

#237's incident: a unit test ran an execute on an engine nothing had stubbed
and the engine launched TextEdit. Every call by which the engine acts on the
desktop or another process's GUI now passes DesktopGate: the app launch,
re-activation, CGEvent posts, Accessibility actions and writes (AXOps'
defaults, route 1's action, the enablement write, the probes'), the
screen-update hold, general-pasteboard writes, and the cursor panel ordered
onto the screen. Open in the engine (a flag read); closed in any process with
XCTest loaded, where a call is refused (nothing done, or desktopGateClosed
naming the call), recorded, and made the running test's failure by an
observer the test bundle registers as it loads (Tests/EngineTestGate).
The proving test launches a bundle that does not exist and checks the gate's
record; the rest of the suite triggers no refusal.

Sminos-Session: cursor235#b93055dd (cua on macbook)
buildEngine reads the engine's status before the swift build, the staging
and the signing, and refuses with the same engine_busy naming the holders;
an engine that is not running is not busy and the build goes on. The
already-current early return comes first (it touches nothing). The check at
the swap stays the authority against a client that connects meanwhile.

Sminos-Session: cursor235#b93055dd (cua on macbook)
… names built lazily

The review of ce8fb2a..2f9a83e: approve, requireAccessibility and
requireScreenRecording could put up real permission dialogs from a unit test.
The Accessibility request with its prompt and CGRequestScreenCaptureAccess now
pass the gate (refused, each answers the non-prompting preflight), and so do
the screen's listing and capture, which can raise the capture confirmation.
The engine requests no other grant. The gate takes the refused call's name as
an autoclosure, so the engine never builds it.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…cratch engine)

Sminos-Session: cursor235#b93055dd (cua on macbook)
… build busy check

Sminos-Session: cursor235#b93055dd (cua on macbook)
@SSFSKIM
SSFSKIM merged commit a2c94e7 into main Nov 1, 2026
6 checks passed
SSFSKIM added a commit that referenced this pull request Nov 1, 2026
…71-m3

Main moved after the first merge (0982cab): #252 (#237: end_task frees
the macOS engine, engine_busy names its holders, engine tests cannot
touch the desktop). A merge again, not a rebase, so the gate judges the
tree that ships.

The overlapping files and how each was resolved:
- .claude-plugin/plugin.json, marketplace.json (conflict): this
  branch's 0.21.0 (cabbb11) kept. It is still the next minor on top of
  main's 0.20.4.
No other file overlaps. #252 touches the macOS engine client and
service (src/services/engine/{client,service}.mjs), src/runtime/
engine.mjs, src/cli.mjs's engine status line and native/engine/; none of
them is in cua's Linux service, its selection or the Linux engine.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
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.

macOS: end_task leaves the REPL host's engine connection open, so engine build/approve refuse engine_busy

1 participant