Skip to content

fix(macos): list windows raised above the normal window layer - #594

Closed
MateuszJuszczyk wants to merge 2 commits into
vercel-labs:mainfrom
MateuszJuszczyk:fix/macos-raised-window-layer
Closed

MateuszJuszczyk wants to merge 2 commits into
vercel-labs:mainfrom
MateuszJuszczyk:fix/macos-raised-window-layer

Conversation

@MateuszJuszczyk

Copy link
Copy Markdown

Problem

macOS window inventory commands miss real application windows when a window manager moves them off the normal window layer. With Sunlight 0.2.3 running, the active Zen window sits on kCGWindowLayer 2 (window-server data: owner Zen, number 13458, layer 2, alpha 1, onscreen true, bounds 0,33 1512x949), while accessibility reports the same window normally (AXWindow / AXStandardWindow, 0,33 1512x949).

  • agent-desktop list-windows --app zen returns {"ok":true,"data":[]}.
  • agent-desktop list-windows fails with TIMEOUT, detail.kind: global_window_inventory_unstable, last_code: APP_UNRESPONSIVE, last_kind: frontmost_window_join.

Root cause

records_from_dictionaries read kCGWindowLayer at crates/macos/src/system/window/cg_window.rs:129 and dropped every window whose layer was not 0 at crates/macos/src/system/window/cg_window.rs:130, before the accessibility side was consulted. A raised window therefore never became a candidate.

  • Scoped path: list_windows_until captures through the same function (crates/macos/src/system/window/window_inventory.rs:51), so --app <name> filters the only real window out and returns an empty list before narrow_to_real_windows (window_inventory.rs:160).
  • Global path: assemble_global_windows reads the frontmost application's AXFocusedWindow (crates/macos/src/system/window/window_inventory_global.rs:133) and requires that window number to join exactly one CoreGraphics record. matching_focus_windows (window_inventory_global.rs:283) finds none, and window_inventory_global.rs:144 reports frontmost_window_join, which stabilize_global_with retries until the deadline. That is the observed 74/120 attempts and the timeout.

The layer filter was there to keep menus, panels, status items and the Dock out of the list, but the layer alone cannot tell a raised application window from system chrome.

Fix

  • CoreGraphics records keep kCGWindowLayer and no longer filter on it (crates/macos/src/system/window/cg_window.rs).
  • New crates/macos/src/system/window/window_admission.rs decides admission from accessibility: a raised window is kept only when its owning process publishes that exact window number as an AXStandardWindow. Menus, panels, status items and the Dock stay excluded because they are never standard windows of their owner.
  • crates/macos/src/system/window/window_ax_state.rs collects each owner's standard window ids from the same AXWindows pass the scoped inventory already performs, so admitting a raised window costs no extra round trip there. The global inventory probes only owners that actually have a raised window, which keeps the existing rule that background owners are not probed.
  • Layer-0 windows are unaffected: they are admitted without any accessibility probe.

Tests

  • window_admission_tests.rs: a layer-2 window matching an AX standard window is kept; a layer-101 overlay of the same owner is excluded; layer-0 windows need no probe; a failed probe drops only that owner's raised windows; each owner is probed once.
  • window_inventory_admission_tests.rs: a layer-2 window is listed and joins the AX focus number; a layer-101 window without the standard subrole is excluded; normal-layer windows are unchanged.
  • cg_window_tests.rs: raised layers survive CoreGraphics parsing and reach admission.
  • window_ax_state.rs: subrole gate test.

cargo fmt --all -- --check, cargo clippy -p agent-desktop-core -p agent-desktop-macos -p agent-desktop -p agent-desktop-ffi --all-targets -- -D warnings, cargo test for those packages, and the source-rule scripts all pass.

Manual verification

macOS 27.0.1, Sunlight 0.2.3, Zen window on layer 2, read-only commands only. "Before" is upstream main (b43ac4f) built from source, so both binaries differ only by this branch.

Command Before After
list-windows --app zen {"ok":true,"data":[]} w-13458, 1512x949 at 0,33, focused when Zen is frontmost
list-windows TIMEOUT after 8 s, last_kind: frontmost_window_join, 120 attempts ok:true, 150 windows, ~1.4 s
snapshot --app zen app could not be resolved complete:true, 73 refs

In the global list, the only window on a non-zero layer is the layer-2 Zen window. A Zen notification window on layer 101 and an unrelated window on layer 1000 are still excluded. list-windows --app Claude and snapshot --app Claude are unchanged (Claude's windows are on layer 0). Latency on layer-0 apps is unchanged within noise (list-windows --app Finder 186 ms → 185 ms, snapshot --app Claude 298 ms → 307 ms, median of 10).

Out of scope

The reported snapshot --app Claude timeout did not reproduce here: Claude's windows are on layer 0 and the snapshot completes in about 0.3 s, so it is unrelated to this filter. Separately, the macOS app inventory lists browser helper processes as extra instances of Zen/Claude, which makes windowless --app lookups ambiguous; that is also unrelated to this change.

🤖 Generated with Claude Code

Window managers such as Sunlight, yabai and Aerospace raise the window the
user is working in above kCGNormalWindowLevel. The CoreGraphics inventory
dropped every window whose layer was not 0, so a real application window on
layer 2 never reached the window list. With no record left for the frontmost
window, the global inventory could not join the accessibility focused window
and every attempt retried until the deadline, so `list-windows` failed with
TIMEOUT while `list-windows --app <name>` returned an empty list.

Keep the layer on each CoreGraphics record and decide admission on the
accessibility side: a raised window is kept only when its owning process
publishes its exact window number as an AXStandardWindow. Menus, panels,
status items and the Dock stay excluded because they are never standard
windows of their owner, and layer-0 windows keep their existing behavior.

The scoped inventory already reads each owner's accessibility window state,
so it admits raised windows from that read. The global inventory probes only
the owners that actually have a raised window, which keeps the existing rule
that background owners are not probed.
@vercel

vercel Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@MateuszJuszczyk is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

A hung background app owning a raised window could spend the whole global
inventory deadline in its accessibility admission probe, so list-windows
timed out even with a healthy foreground app. The frontmost owner is now
probed first with the full deadline; background owners get 250 ms each
from a shared 1 s budget, and a probe that runs out only drops that
owner's raised windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lahfir

lahfir commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

closing as superseded by #702 on main (cg_window now lists on-screen layers 1..19). thanks @MateuszJuszczyk

@lahfir lahfir closed this Nov 28, 2026
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.

3 participants