cua icon indicating copy to clipboard operation
cua copied to clipboard

[Feature]: Damage-driven settle — wake at the first changed frame instead of sleeping a fixed constant

Open hyprcat opened this issue 3 months ago • 1 comments

Primary area

Cua Driver

User problem

Action tools on macOS pace themselves with fixed sleeps: the 800ms WebKit DOM-focus settle after clicking a text input, and fixed post-launch waits. A fixed sleep costs its full constant on every action even when the app settled long ago. In our measurements the actual "first repaint after the action" lands at ~76ms p50 — so the 800ms path overpays by ~10x on the common case, and every agent action stacks that latency.

(#3490 makes these constants configurable, but a knob only trades safety margin for speed; it can't adapt per action.)

Desired outcome

Post-action settles wake at the first changed frame and keep the old constant only as the timeout cap. ScreenCaptureKit makes this cheap: an SCStream only delivers a frame when display content actually changed — frame arrival IS the damage signal. Sketch of the implementation we run in a Swift driver mirroring this tree:

  • One persistent, lazily-started SCStream per display, scaled tiny (~256px wide), cursor off, minimal queue depth — cost is negligible while active.
  • Each delivered frame is confirmed with a cheap FNV-1a digest of the scaled pixels (a repaint can redraw identical content).
  • wait_for_change(baseline, timeout): resolve at the first frame whose digest differs from the pre-action baseline, else at the old fixed duration.
  • Lifecycle: idle-stop after ~30s with no callers; restart transparently on next use.
  • Degradation: no Screen Recording grant / stream failure → return "unavailable" fast and fall back to the existing fixed sleep, unchanged.
  • One correctness caveat worth encoding in the API docs: a changed frame is NOT application readiness (a caret blink satisfies it) — this only converts fixed-duration sleeps into "wake at first repaint, cap at the old duration". Callers needing semantic readiness still poll AX.
  • Arm the watcher on the display containing the acted-on element — a main-display default silently rides out the full cap for windows on other monitors (we shipped that bug and measured it).

Measured effect in the Swift driver: the 800ms WebKit settle resolves at ~76ms p50; combined with the watch-deadline change (#3488), click wire time went from ~1.4s to ~551ms p50 with click correctness verified via an independent AX referee.

Current workaround

Lower the settle constants via the #3490 knobs and accept the reduced margin.

Affected components and platforms

cua-driver platform-macos (ScreenCaptureKit). The concept ports: DXGI dirty rects on Windows, damage events on Linux.

🤖 Generated with Claude Code

hyprcat avatar Aug 31 '26 13:08 hyprcat

Thanks for the detailed sketch and the numbers, especially the note that a changed frame is not the same as application readiness.

One part of the premise has moved. On current main, the WebKit DOM-focus wait after a click on a web text input is no longer a plain 800ms sleep. wait_for_web_focus in platform-macos/src/tools/click.rs returns as soon as the accessibility tree reports the field focused and stable, and 800ms (CUA_DRIVER_WEBKIT_SETTLE_MS) is only the cap. The cap and the other pacing values became configurable through #3490.

What I could not find is the damage-driven signal you propose. A search of libs/cua-driver for SCStream and wait_for_change returns nothing, so there is no ScreenCaptureKit frame watcher. I did not trace the post-launch waits or the other fixed sleeps in detail, so I can't say which of them still use constants.

The idea stays open as an enhancement. Your points on arming the watcher for the display that holds the target window, and on falling back cleanly when Screen Recording is not granted, would carry over into any implementation.

f-trycua avatar Nov 21 '26 19:11 f-trycua