Skip to content

#200: route-3 clicks keep the person's window order and front app (delivery guard + after-action watch) - #243

Merged
SSFSKIM merged 22 commits into
mainfrom
fix/200-background-raise
Nov 1, 2026
Merged

SSFSKIM merged 22 commits into
mainfrom
fix/200-background-raise

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

cua's own macOS engine brought the driven app's window to the front on a background click (route 3). This PR keeps the window order and the frontmost app as the person left them, while the click still lands.

Cause. Route 3 posts an AppKit-defined event (subtype 1) that arms _mouseActivationInProgress. The activation click consumes that arm, so the action's own left mouse-down, without ⌘-only flags, is an ordering click. -[NSWindow _handleMouseDownEvent:] then calls _NSDoDeferredWindowOrdering, which raises the window. An inactive app's window is never key, and NSTextView refuses the first mouse. So no posting variant both delivers and avoids the raise: re-arming, skipping the activation click, a 2→1 pair and AX keying were each ruled out by reading AppKit. The first, re-arming, was also built and reverted.

Fix: an activation delivery guard (FakeActivation.deliver, native/engine/Sources/Engine/Activation.swift).

  • One screen-update hold stays open across the events. Before posting, it takes one stable sample of the frontmost process and the window order.
  • After posting it waits for 3 stable samples, at least 60 ms (deliveryGuardMinimumMs), polling every 2 ms. It raises the person's window back and reactivates their app only when the target (or bound) app took the front. The hold is capped at 100 ms.
  • If the person switched to a third app meanwhile (otherAppFront), that is never undone.
  • After the release, an asynchronous watch keeps repairing until post + 1 s. It never repairs after the person's own HID input or after a third app takes the front. It ends when the next request on that connection arrives, at the next pointer action, or when the connection closes. The action returns at the release, so the watch adds no latency.
  • Held and shared focusing clicks go through the same guard.
  • Meta: cua/delivery (restores, frontmostRestored, otherAppFront, restoreLatencyMs, guardMs, screenHoldMs, capped, raiseLeft, holdStartedAt, releasedAt, postedAt, repairedAfterRelease, …) and cua/afterActionRepairs, keyed by postedAt. The engine service forwards both.
  • actionInterval again brackets only the events' own posting, not the guard, so speed comparisons against earlier runs and against the vendor keep their meaning.

Host note (macOS and Linux computer notes, not the first-call document, to keep facade parity): "- Leave the person's windows as they are: unless asked, don't raise, activate, hide or unhide an app to reach a covered or background window; say what is in the way instead." The 10-23 symptom was agents raising apps on purpose, which the engine fix doesn't touch.

Plugin 0.20.2 (own commit 39fb837, plugin.json and marketplace.json): plugin users get the new host note and the delivery meta.

Ordering race

The engine posts the activation event and the mouse events from one thread, each through CGEventPostToPid. That sends a one-way mach_msg to the window server's event port: one FIFO, the same path for type 13 and mouse events. HIToolbox pulls them in arrival order, so the app cannot see the mouse-down before the event that precedes it. Caveat: a third-party active event tap can delay or reorder events on their way through.

Measured (live, MacBook, 2026-11-01; docs/evidence/2026-10-30-macos-agent-cursor/item-200/item-200.md)

Build 318e5aa67691 (d18797a), cases a–e: PASS. 140/140 delivered; every raise was restored inside the hold; 0 late, 0 unguarded, 0 left; 1 of 140 capped.

case post → first raise, ms (min / median / p95 / max) post → release, ms (min / median / max)
a TextEdit coordinate click 6.7 / 15.0 / 36.6 / 43.6 (n 39) 60 / 62 / 78
b click inside a standing activation 8.6 / 11.3 / 20.4 / 20.4 (20) 61 / 66 / 78
c drag 10.6 / 16.1 / 26.9 / 26.9 (19) 60 / 62 / 72
d double click 7.0 / 18.5 / 32.6 / 35.5 (20) 60 / 63 / 67
e Finder row 11.3 / 19.0 / 28.9 / 31.6 (20) 60 / 63 / 69

The shipping build bf458b9 (build 07724139591e), with the after-action watch (item-200/final/):

case delivered raises, all restored in the hold late / unguarded / left / flashes / capped post → first raise, ms (min / median / p95 / max) post → release, ms
a TextEdit coordinate click 40/40 39 0 / 0 / 0 / 0 / 0 8.7 / 17.7 / 29.3 / 39.0 60–67
f Electron button that refuses the first mouse 20/20 19 0 / 0 / 0 / 0 / 0 8.6 / 13.3 / 33.6 / 33.6 60–67

Both PASS. On f, the fixture's counters show what the page sees from route 3's activation: on cua, appDidBecomeActive and windowFocus once per click, plus appDidResignActive 5 and windowBlur 3 over the 20 clicks; on the vendor, 1 of each. Route 3 activates the app for the click by design (#188); this PR changes when that is visible, not that it happens.

Vendor, same cells. On TextEdit's text the vendor's "no raise" was no delivery: a 1/40, b 0/20, c 5/20, d 1/20. Its click there is a ⌘-click, which AppKit neither orders front nor treats as a plain click. Finder (e) and the Electron button (f) delivered 20/20, each with 20 raises undone by its window-ordering observer in 5–62 ms with no screen hold, so they were visible flashes. The correction is on #200.

Speed. The guard adds about 30 ms per route-3 click over the vendor's bare post (release at post + 60–78 ms). In-cell, cua measured 4–140 ms against the vendor's 229–1038 ms (M2b hold3), so cua stays well ahead.

Limits

Tests

  • Engine: ClickDeliveryTests and FocusHoldTests (GuardSeam covering target above, mouse-down timing, windows above, person input, hold/release, repairs outside the hold, a third app's front, the watch's cancellation). Engine suite 640/640 at the last engine change (12ae081).
  • Services: the cua/delivery and cua/afterActionRepairs forwarding.
  • Live-check kit: scripts/cursor-sitting/raise.mjs (cases a–f, the 5 ms window-order watcher, the judge), with 190 tests in test/cursor-sitting/raise.test.mjs and the runbook section "Item 200".
  • CI: every gate GREEN on efe9497 (run 41272573701). The test gate is GREEN on bf458b9 (run 41273003311), which changes only the kit and its test. The head adds evidence only.
  • Reviews: the engine and the kit, by reviewer-high and reviewer-medium. Every finding was fixed and re-reviewed. The last round had no findings.

Docs

Native-engine design spec: the #200 Decision Log entry (amended for the watch), the Interfaces meta line and route 3's text. Research F §17, with inline corrections in §5.4/§5.6. Tech-debt row.

Shared files with #211 (stale draft, not rebased)

native/engine/Sources/Engine/Input.swift and docs/doperpowers/specs/2026-10-18-own-native-engine-design.md.

Closes #200

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

A route-3 click raised the driven window above the user's frontmost one
at its own mouse down, after the guard had ended. AppKit (macOS 26.6.2)
answers subtype 1 by arming _mouseActivationInProgress for one mouse
down. The activation click consumed it, so the action's single left
mouse down without ⌘ alone reached a key window of an app that believes
itself active. -[NSWindow _handleMouseDownEvent:isDelayedEvent:] answers
that with _NSDoDeferredWindowOrdering, the window server's
_safeFrontWithClick. Route 3 now posts the standing activation's subtype
1 again, for the click's window, immediately before each such mouse down
(clicks, a double click's first press, a drag's down, a click inside a
held or shared activation), so AppKit takes the mouse down as the
activation's own: the window made key, the click delivered, no ordering
request. Nothing changes where no fake activation stands, nor on routes
1, 2, 2' and 4.

Sminos-Session: cursor235#b93055dd (cua on macbook)
The 10-23 raises were agents raising or activating apps to reach a
covered window. The computer surfaces' host notes now say not to raise,
activate, hide or unhide an app for that unless asked, and to say what
is in the way instead. The first-call document is unchanged, keeping the
facade's parity with the vendor's.

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

A route-3 click's own mouse down raises the target's window above the
user's frontmost one (AppKit's ordering click), after the activation's
guard has ended. From outside the app nothing both delivers a click to a
view that refuses the first mouse (TextEdit's text) and avoids that
raise: the activation click exists to make the window key, a re-armed
subtype 1 arms nothing in an app already active, and Accessibility can
neither key the window without ordering it nor while the arm is set
(#200's readings).

So route 3's pointer actions now post their events through a delivery
guard. It keeps the activation's screen-update hold open across the
events (the activation's guard then settles on one sample), answers the
target's window above the user's with the user's window raised back and
the target's process in front with the user's app made frontmost again,
and ends on three stable samples before the hold is released. A click
inside a held or shared activation takes a hold of its own; never a
second hold for one action. At the 100 ms cap the guard ends and leaves
the order as it is, reported; a third app in front is left alone. The
meta's `delivery` (cua/delivery) carries the restores, whether the
frontmost app was restored, the first restore's latency, the guard's
and the hold's times, the cap and any raise left.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…eading's corrections in F §17

The design's Decision Log records why route 3 restores rather than
prevents the click's raise (AppKit's ordering click; the activation click
as the vendor's sacrificial first mouse; the rejected alternatives), the
guard's parameters and costs, and Interfaces gains the delivery meta.
F §17 records the #200 reading of the vendor: an enforcer always exists
on the IPC path, a failed enforcement is rethrown (not retried with ⌘),
the enforcement guard ends before the click, a WindowOrderingObserver
re-raises the user's window on every ordering event, and §5.6's tap
masks were wrong.

Sminos-Session: cursor235#b93055dd (cua on macbook)
`cursor-sitting.mjs raise drive --engine cua|vendor` places the harness
windows itself (TextEdit at the left, Chrome for Testing over its right
half, Finder's and the Electron fixture's where TextEdit's is; harness
windows only, through Accessibility), activates Chrome, and runs six
cases, each on its own session after checking the arrangement: route-3
clicks in TextEdit's text under Chrome and clear of it, an element click
then a coordinate click inside the standing activation, a drag, a double
click on a word, a plain click on a Finder row, and a coordinate click
on the fixture's push button with its focus counters recorded.

A watcher compiled into the harness cache samples the two windows' order
every 5 ms (CGWindowListCreate under the time-constraint policy: the
MacBook held 5 ms at a load average of 70). Each sample keeps its
action's route, activation meta, cua/actionInterval and cua/delivery,
its delivery read back at once (the selected range by the approved
bundle's text-view probe; the alignment, Finder's selection and the
fixture's count by observation), and what the watcher saw: post to raise,
post to the hold's release, late raises. A scene that breaks stops the
run. `raise judge` reports per case and engine; cua passes on every
sample delivered with no late, unguarded or left raise. The vendor's
rows are recorded only. The runbook gets the item with its timing.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…at the cap, and reports what it saw

Three corrections to route 3's delivery guard (6e94492), from review:

- The window checked is the action's. A held or shared activation keeps its
  first holder's window, so a later click or drag on another window of the
  same process (a dialog) raised that window while the guard sampled the
  first one and reported nothing. deliverPointer now passes the window its
  events name (fields 91/92, stampedWindow), falling back to the
  activation's when they name none.

- Nothing is repaired once the cap has passed. The cap was read only before
  the sleep, so a sample could re-activate the user's app or raise its
  window after the cap's timer had released the display: a visible repair.
  The deadline is now read after the sleep, the reads and any restore
  before them; past it the guard ends with `capped`, on a sample that only
  reads (no sleep), so `raiseLeft` rests on the order after the last repair.

- `frontmostRestored` and `raiseLeft` come from observation, not from the
  re-activation's return value. A restore counts only once a later sample
  sees the user's app frontmost again (kept if the target comes back after);
  a guard that ends with the target or the bound app in front, at the cap or
  after a refused re-activation, reports `raiseLeft`. An accepted but
  ineffective re-activation is no longer reported as a restore.

A third app in front is still left alone (`otherAppFront`).

Sminos-Session: cursor235#b93055dd (cua on macbook)
…st the cap, and checks the action's window

Sminos-Session: cursor235#b93055dd (cua on macbook)
…kets, and a clean end

Three findings of the review of 1108451:
- A cell that observes and then acts keeps only the action's meta, so a
  raise before the screen hold began counted as guarded. The delivery
  guard now reports `holdStartedAt` (Unix ms: the activation's hold when
  the action ran it, else the guard's own), read as releasedAt less the
  hold's monotonic span; a raise before it is unguarded.
- The outlived check carried the last order forward across watcher gaps.
  The watcher now writes each change with the read before it, and a
  raise is late or outlived only when a read past the release and its
  10 ms grace shows it; a gap that leaves it unknown is undecided, which
  makes the case incomplete and is reported apart.
- A case that stopped in its tail could still pass. A pass now needs the
  last raise check passed, the session closed, the watcher stopped by
  the kit, and its record reaching the close.

Sminos-Session: cursor235#b93055dd (cua on macbook)
The no-restore branch took the end record's writing time as proof that
the watcher read past the release's grace. It now reads the end record's
lastRead: a raise last seen at 1085 with a release at 1080 and the end
written at 1100 is undecided (incomplete), not outlived.

Sminos-Session: cursor235#b93055dd (cua on macbook)
… s window, unless the person acted

The first live sample: the app raised its window 38 ms after the click,
42 ms into the hold, and the guard's samples (about 20 ms apart under
load) found it only past the 100 ms cap, where it repaired nothing and
left the raise. The cap stays 100 ms; after the release the guard keeps
repairing (the user's window raised back, the user's app made frontmost
again) until 1 s after the events, only while the person has given no
input since them (CGEventSource seconds since any HID input) and never
over a third app's front. Such a repair is reported apart
(repairedAfterRelease, its latency; personInput when the person's input
stopped it). The poll inside the hold sleeps 2 ms. The kit's judge counts
a raise the engine repaired after the release, undone inside the window,
as a flash: counted and reported, not failed.

Sminos-Session: cursor235#b93055dd (cua on macbook)
The rerun's first sample: the guard settled 14 ms after the click (three
2 ms samples) and released, and TextEdit raised at 21 ms, after it. The
first live samples saw TextEdit raise 21 and 38 ms after the click, so
the guard now watches at least 60 ms after the events, inside the hold
when it lasts, else in the repair window after it.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…ss on 318e5aa67691, the vendor half recorded

cua's engine at d18797a (build 318e5aa67691): cases a-e PASS, 140/140
delivered, every raise restored under the screen hold (0 late, 0
unguarded, 0 left); post->raise 6.7-43.6 ms, post->release 60-78 ms. f
INCOMPLETE (the kit's frame reader found no button on the cua run). The
vendor half: TextEdit raised nothing and mostly did not deliver (a 1/40,
b 0/20, c 5/20, d 1/20); Finder and the Electron button delivered 20/20
each with 20 raises undone, without a hold, in 5-62 ms. The two earlier
cua runs that stopped at a#0 (the cap, then the 14 ms settle) are in the
same files, their attempts before the judged last. The quiet checks are
the quiet-*.txt files.

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

After each delivery guard returns, a watch samples every 10 ms on another thread until
1 s after the events, under the guard's rules (the user's window raised back, the user's
app made frontmost again over the target's or the bound app's, never over a third app,
nothing after the person's input). The action still answers at the release.

The watch ends when the connection's next execute begins (at its very start) and when
any pointer action begins; a sample runs under the watch's lock, which the cancel takes,
so no repair starts once the next request has begun. Its repairs are recorded on the
connection and carried as afterActionRepairs {repairs, actions: [{postedAt, repairs,
latencyMs}]} on every later input result and observation (cua/afterActionRepairs in the
service), keyed by the delivery's new postedAt, plus a route3.afterAction.repair signpost.
The spec's Interfaces line and the #200 Decision Log entry are amended.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…nd matches AXValue too

element-frame walks the windows again every 100 ms for up to 3 s after setting
AXManualAccessibility (the first live check read 13 elements and no button right after),
and matches the name on AXTitle, AXDescription or AXValue; its answer says how many walks.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…fixture, found by the engine first

The kit keeps cua/afterActionRepairs from every reply, and the judge counts a late or
outlived raise as a flash when a later reply reports the watch's repair of that action
(keyed by its delivery's postedAt) and the watcher saw it undone inside 1 s of the post;
never undone there, it stays a FAIL. The report adds the watch's repairs and post to its
first repair. Each case ends its tail with one observation, so the last action's repairs
are reported.

Case f now starts on a fresh Electron fixture for each engine: the kit brings it down and
up through the scenes CLI (its accessibility record must be off), places it, activates
Chrome, then the engine under test must find the fixture's button by its index (a missing
button stops the case) before the frame reader and the clicks.

Sminos-Session: cursor235#b93055dd (cua on macbook)
Sminos-Session: cursor235#b93055dd (cua on macbook)
…used re-activation is no repair; the interval is the events'

- A closed connection cancels its after-action watch (Engine.connectionClosed, from
  Server.unregister); a watch that ends on its own clears the connection's live watch,
  and holds the connection's record weakly, so nothing keeps either alive.
- A re-activation the system refuses ends the watch without counting a repair.
- deliverPointer stamps the action interval around the events' own posting inside the
  delivery guard, as InputMeta.posting's contract says: no longer from before the guard's
  setup to its end. The guard's time stays in delivery.guardMs.

Sminos-Session: cursor235#b93055dd (cua on macbook)
…hile its watch may run; the judge runs from postedAt

- raiseOwners judges a raise with the cell whose span holds it, unless that cell's own
  hold had not begun (or it has none) and an earlier guarded action's hold had, with the
  raise inside its repair window after its post: the engine's watch runs until the next
  request reaches it, which a host-side cell's start precedes, so a raise its watch
  repaired there is that action's flash, not an unguarded raise.
- callWatch's post is the delivery's postedAt (else the interval's start), for the repair
  window and every post-relative time.

Sminos-Session: cursor235#b93055dd (cua on macbook)
… cell of its session has completed; the engine's watch is cancelled by the next request

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

f on cua PASS (20/20 delivered, every raise restored inside the hold, 0 flashes) and a on cua PASS (40/40, likewise); f on the vendor recorded (20/20 delivered, 20 raises on the display). The first cua observation of a freshly launched Electron fixture lacked the button (14 lines) and had it 2 s later (27 lines); the vendor's first had it. That diagnostic ran as a temporary kit change, reverted, its diff kept beside the records.

Sminos-Session: cursor235#b93055dd (cua on macbook)
@chatgpt-codex-connector

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 ⚠️ Failed 2026-11-01T05:15:25.494813Z
We couldn’t complete this review. Comment @codex review to try again.
fffb9e9 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

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.

…elivery meta

Sminos-Session: cursor235#b93055dd (cua on macbook)
@SSFSKIM
SSFSKIM merged commit d409b72 into main Nov 1, 2026
6 checks passed
SSFSKIM added a commit that referenced this pull request Nov 1, 2026
Brings main's #235 (cua's own macOS agent cursor, #240), #236's fix
(#241), #200 (#243) and #244 (#247) under M3, so M3's gate judges the
tree that ships. A merge, not a rebase: the spec and the evidence cite
this branch's commit SHAs.

The overlapping files and how each was resolved:
- src/runtime/launch.mjs (conflict): both sides kept. M3's Linux branch
  (cua's own service: CUA_SKY_SERVICE, CUA_ENGINE_BIN and
  CUA_DESKTOP_MODE for the cua selection, the vendor service and helper
  otherwise) beside #240's macOS branch, which adds the agent cursor's
  environment (cursorEnv) to cua's engine. The env header comment merged
  cleanly with both sides' entries.
- src/runtime/doctor-engine.mjs (auto-merged): #240's macOS row (the
  cursor switch in factsReport, cursorWords, settingsOf's cursor check)
  and M3's Linux row (service cua, capabilities, the IsEnabled row)
  touch separate functions; kept as merged.
- src/cua-api/sky.mjs and test/cua-api/sky.test.mjs (auto-merged):
  #241's fix for #236 (setupError exported, then/toJSON/symbols read
  undefined after a failed setup, the declared vendor difference and
  its settle test) beside M3.2's truncated-field scenario. Compatible
  by design: cua's Linux service's setup always resolves with the 20
  methods and keeps a failure for each method to refuse with, so the
  facade's setupError stays unset there; a setup that does throw now
  fails the bootstrap with its own error instead of hanging.
- tech-debt-tracker.md (conflict): every row from both sides kept,
  main's five newer rows first (74 at the base, +2 M3, +6 main = 82).
- .claude-plugin/plugin.json, marketplace.json (conflict): main's
  0.20.3 here; M3's own bump follows in its own commit.

Focused suites (services, facade, doctor, selection, harness, accept,
cursor) on the merged tree: 1948 pass, 8 fail; the same 8 fail on both
parents in this VM (umask 077 modes, macOS-only and env tests).

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.

Own engine brings the driven app to the front in MAWS sessions (MacBook opt-in build)

1 participant