cua icon indicating copy to clipboard operation
cua copied to clipboard

cua-driver macOS: list_windows z_index diverges from live stacking — documented "take the maximum z_index" picks an occluded window

Open AceSilent opened this issue 3 months ago • 5 comments

Summary

On macOS, list_windows documents:

z_index (integer or null; higher values are closer to the front; …) To select a frontmost candidate, take the maximum integer z_index

Measured against a live daemon, that contract does not hold for the default (all-windows) enumeration: z_index can disagree with WindowServer's live stacking order, so the documented "take the maximum integer z_index" recipe selects a window that is actually stacked behind a sibling of the same app (an occluded dialog, in our capture). The driver's own get_accessibility_tree returns the correct live order at the same moment, i.e. the driver disagrees with itself.

This is the implementation side of #2199 (which defined the semantics): the macOS derivation doesn't deliver the defined semantics when the enumeration includes off-screen windows.

Environment

  • cua-driver-rs 0.22.2 stable (macOS daemon cua-driver serve, standard permission mode)
  • macOS Darwin 25.6.0, arm64
  • Derivation unchanged at current HEAD 70ae3b4 (see "Root cause" below), so this is not already fixed in nightly.

Measured evidence (read-only tools only)

Real desktop, one app (WeCom / 企业微信, pid 1385) with two overlapping on-screen layer-0 windows. Its main window had been raised above its "software update" dialog. Four sources sampled back-to-back:

window window_id list_windows z_index CG OnScreenOnly position (front→back) AX (System Events) driver get_accessibility_tree order
main window 95 56 18 — in front AXMain=true, first before 30195
update dialog 30195 101 ← max-z winner 19 — behind AXMain=false after 95

So CGWindowListCopyWindowInfo(.optionOnScreenOnly) (documented front-to-back), the AX tree, and the driver's own get_accessibility_tree all agree the main window is in front — only list_windows.z_index ranks the occluded dialog higher. A control pair (two Finder windows, z 136 vs 126) matched all four sources, so the direction convention itself is fine; the ordering source is what's off.

Additional characterization (3 samples, 3 s apart, 138 records each):

  • Every z_index was byte-identical across samples while disagreeing with the live order the whole time — the value behaves like a sticky snapshot that does not follow re-raising.
  • Numerically, z_index + (position in a CGWindowList all-windows dump) ≈ const across the board, confirming it is a reverse index into the all-windows enumeration, not into the on-screen stacking.
  • z_index: null never occurred (0/138), so callers cannot exercise the documented null path on macOS at all.

Root cause

libs/cua-driver/rust/crates/platform-macos/src/windows.rs:

  • all_windows_with_space_snapshot() enumerates with kCGWindowListExcludeDesktopElements only — no kCGWindowListOptionOnScreenOnly — and that is what list_windows uses by default (tools/list_windows.rs, on_screen_only=false).
  • z_index is then z_index_from_front_to_back(total, idx) — a reverse index into that array ("// z_index: CGWindowList front-to-back → assign reverse index.").

Apple only promises front-to-back ordering for the on-screen list. Once off-screen windows are included, the relative order of the on-screen subset is not the live z-order — measured above, it diverges after a same-app re-raise and stays diverged. So the reverse index inherits an ordering CoreGraphics never promised.

Suggested fix

Derive z_index from the kCGWindowListOptionOnScreenOnly enumeration order (join by window_id), and report z_index: null for off-screen windows — they have no meaningful stacking position, and the tool contract already defines null as "stacking order is unavailable; callers must not infer one". That makes list_windows consistent with get_accessibility_tree, which is already live-correct, and makes the documented "take the maximum integer z_index" recipe actually pick the frontmost window.

A regression test idea matching #2199's test seam: feed the enumerator a fixture where the all-windows array order differs from the on-screen array order for the same on-screen ids, and assert z_index follows the on-screen order.

Repro (read-only)

  1. Open two overlapping windows of one app; raise the older one (click its title bar) so live stacking no longer matches creation order. Wait a few seconds.
  2. cua-driver call list_windows '{"pid": <pid>}' → note which window has the max z_index.
  3. cua-driver call get_accessibility_tree '{}' → note that app's window order (live-correct).
  4. Independent ground truth, front-to-back:
import CoreGraphics
import Foundation
let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as! [[String: Any]]
for (i, w) in list.enumerated() {
    print(i, w[kCGWindowNumber as String] as! Int, w[kCGWindowLayer as String] as! Int,
          w[kCGWindowOwnerName as String] as? String ?? "")
}

Steps 3 and 4 agree with each other and with what is visibly on top; step 2 ranks the re-raised window below its sibling.

AceSilent avatar Aug 31 '26 09:08 AceSilent

I'd like to take this. Plan: in macOS windows.rs, rank z_index from one kCGWindowListOptionOnScreenOnly pass joined by window_id, so on-screen windows follow live stacking. Off-screen windows rank below every on-screen one, so z_index stays an integer. The doc comment on on_screen_windows_any_layer already notes that the all-windows list doesn't follow a raise.

Evidence: a unit test where the all-windows order and the on-screen order disagree, and a before/after with two overlapping windows of one app.

If you'd rather have null for off-screen windows, as the issue suggests, I'll do that instead. #2172 touches the same code, so I'll keep this change separate from it.

S3yon avatar Oct 16 '26 17:10 S3yon

Thanks for picking this up! Deriving the ranks from a single on-screen pass and joining by window_id sounds good.

I'd prefer z_index: null for off-screen windows. I'm already using null for these windows in my own project, and that approach has been working well in practice.

Assigning them lower integers would stop them from outranking on-screen windows, but would still imply a stacking order that hasn't actually been observed. For example, when filtering by PID and all of that app's windows are off-screen, max(z_index) would still produce a misleading "frontmost" candidate instead of allowing an explicit fallback. Using null also matches the contract in #2199.

The proposed regression test and before/after check sound good. It would be useful to also assert that off-screen windows get null, including a case where the app has no on-screen windows.

Keeping this separate from #2172 makes sense. Thanks again!

AceSilent avatar Oct 16 '26 17:10 AceSilent

@S3yon's #5395 covers this; linking it here.

f-trycua avatar Oct 18 '26 06:10 f-trycua

Switched to z_index: null for off-screen windows in #5395, with a test for an app that has no on-screen windows. The before/after with two overlapping TextEdit windows is in the PR description.

S3yon avatar Oct 18 '26 22:10 S3yon

Adding measurements from cua-driver 0.37.0 on macOS 27.2 that match this issue.

Environment

  • macOS 27.2 (26B123), on a laptop with one built-in display.
  • cua-driver 0.37.0, called through list_windows with no pid filter.
  • Reference order: CGWindowListCopyWindowInfo with kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, whose array order is front to back. We read it just before and just after each list_windows call. The two reads were equal every time.

What we expected

The tool description says: "For the frontmost window take the maximum integer z_index; null means stacking is unavailable". So for any two on-screen windows at layer 0, the one in front should carry the larger z_index.

What we saw

Windows are named by role only. "Browser A" is the window we raise; "Editor" is a TextEdit window that fully spans it. Rank is the position in the window server's front-to-back list (r4 is the frontmost layer 0 window).

Read 1: the editor raised with bring_to_front, then its app made active with open -a TextEdit. The editor is in front.

Rank Window Layer z_index
r4 Editor 0 18
r5 Terminal 0 26
r6 Mail 0 43
r7 Browser B 0 55
r8 The driver's own window 0 absent from list_windows
r9 Browser C 0 64
r10 Browser A 0 11

Read 2: Browser A then raised with bring_to_front alone. The window server puts it in front.

Rank Window Layer z_index
r4 Browser A 0 11
r5 Editor 0 18
r6 Terminal 0 26
r7 Mail 0 43
r8 Browser B 0 55
r9 The driver's own window 0 absent from list_windows
r10 Browser C 0 64

In read 2 the frontmost window carries the smallest z_index, and z_index rises from front to back. In read 1 the order of z_index matches neither direction. Each read was taken twice, with the same result.

An earlier session on the same Mac gave a second case: Browser A raised by bring_to_front over the editor. The window server put Browser A in front (r4) of the editor (r5), but z_index gave the editor 69 and Browser A 61. Terminal (r6) and Mail (r7) were also swapped: 21 and 38.

On a second Mac with the same macOS build and no raise before the read, z_index matched the window server's order for 11 of 11 windows.

Two smaller points

  1. list_windows leaves out the driver's own window, which the window server lists at layer 0 with bounds 0 0 1470 956 (the whole screen). If this is meant, a note in the tool description would help callers who compare the two lists.
  2. We could not tell from the description what z_index is derived from. Our guess, not proven, is that it follows which app was last made active rather than the window server's stack.

Steps to reproduce

  1. Open a browser window and a TextEdit window whose frame fully contains the browser window.
  2. Raise the TextEdit window with bring_to_front, then run open -a TextEdit.
  3. Raise the browser window with bring_to_front alone.
  4. Call list_windows with no pid filter, and read CGWindowListCopyWindowInfo with on-screen-only.
  5. Compare the order: the window server shows the browser in front; z_index gives the TextEdit window the larger value.

What we do meanwhile

We read the order from CGWindowListCopyWindowInfo ourselves and no longer use z_index for stacking.

Boxyboxy avatar Nov 14 '26 21:11 Boxyboxy