cua icon indicating copy to clipboard operation
cua copied to clipboard

[Feature]: Warm session caches at daemon start; stale-while-revalidate for enumeration reads

Open hyprcat opened this issue 3 months ago • 1 comments

Primary area

Cua Driver

User problem

Two related once-per-session gaps:

  1. Cold first call. The daemon starts with every cache empty, so the first tool call of a session pays the full cold path (shareable-content fetch, app-directory scan, event-source creation) inside the action instead of at session start. First impressions of driver latency are set by exactly that call.
  2. Enumeration reads recompute identical answers. list_windows / accessibility-tree reads re-run the full enumeration on every call even when called in a tight agent loop. Measured against the 0.16 binary under adversarial load (6 CPU spinners, fresh connection per request): list_windows p50 6–11ms where a cache-backed answer serves in ~1.2ms.

Desired outcome

  1. Warm-up at serve start: after the socket binds, pre-heat the session-lifetime caches in the background — installed-app scan (cache landing in #3492), running-app list, event source; gate anything TCC-adjacent on the preflight (CGPreflightScreenCaptureAccess) so warm-up never raises a permission prompt on an ungranted machine. Every warmed path is the same code the first call would run — behavior identical, timing moved.
  2. Stale-while-revalidate on enumeration replies (opt-in or short-TTL): serve a recent reply instantly while one background task recomputes, with a hard bound on staleness (we use TTL 100ms, stale cap 20×TTL, single-flight refresh). Actuation paths keep resolving live — only the read-tool replies ride the cache.

Current workaround

None for warm-up; callers self-throttle enumeration reads.

Affected components and platforms

cua-driver daemon startup (all platforms) + macOS read tools.


Negative result worth having on record (so nobody re-runs this expecting a win): we also built a persistent full-res SCStream "screenshot frame server" — the classic remote-desktop trick of serving the latest frame instead of running a one-shot capture. Measured on Apple Silicon/macOS 15 against a driver that already caches capture plans (as this tree does since the 2s TTL cache): parity, not a win — warm one-shot 146–180ms vs stream 141–185ms, because PNG encode of a native-res Retina frame dominates both paths once session negotiation is amortized. The honest latency lever for screenshots is the encode, not the capture.

🤖 Generated with Claude Code

hyprcat avatar Aug 31 '26 13:08 hyprcat

Thank you for the measurements, and for writing down the SCStream frame-server result. Knowing that capture is not the bottleneck for screenshots is useful.

Here is where current main stands, from reading the code. The installed-app scan is now memoized with a 30 second TTL (INSTALLED_CACHE_TTL in platform-macos/src/apps/mod.rs, from #3492). Capture plans are cached for 2 seconds in platform-macos/src/capture.rs. I did not find a warm-up task that runs after the daemon binds its socket, and I did not find stale-while-revalidate on list_windows or accessibility-tree replies. I only searched for these by name and did not trace the full daemon startup, so I may have missed a path.

Both ideas still look open to me. The TCC gating you describe (preflight before any warm-up) is the part that most needs care, so it would be good to keep that in the design. I'm leaving this open as an enhancement for the Cua Driver owner to triage.

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