noracu 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/README.md +2 -2
- package/README.zh-CN.md +2 -2
- package/SECURITY.md +1 -1
- package/docs/architecture.md +1 -1
- package/docs/tools.md +3 -3
- package/imctl/pyproject.toml +1 -1
- package/imctl/src/imctl/__init__.py +1 -1
- package/package.json +1 -1
- package/src/mac-ax.js +84 -22
- package/src/server.mjs +38 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.2] — 2026-11-06
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- A physical click (`mac_click` on an element without a semantic action, or
|
|
7
|
+
with `mechanism: "physical"`) is no longer reported as `delivered_verified`
|
|
8
|
+
when something else happened between the hit test and the click. Right before
|
|
9
|
+
sending, the window on top at the point is checked again; anything that came
|
|
10
|
+
on top since refuses the click (`occluded`, nothing sent). After the click,
|
|
11
|
+
`delivered_verified` needs the element to still be there, to have changed,
|
|
12
|
+
and its own window to still be on top at the click point; a vanished element
|
|
13
|
+
or another window there gives `delivered_unverified` with a note. `mac_drag`
|
|
14
|
+
re-checks both ends the same way. Found on a device run that covers, moves
|
|
15
|
+
or closes the target in that gap: 0.2.1 reported all three as
|
|
16
|
+
`delivered_verified`; this version reports them as `delivered_unverified`,
|
|
17
|
+
and an undisturbed click is still `delivered_verified`.
|
|
18
|
+
|
|
19
|
+
### Security
|
|
20
|
+
- An `element_index` read with `surface: "app"` (or `mac_find` with
|
|
21
|
+
`surface: "app"`) is bound to the CGWindowID of the window it was in: a
|
|
22
|
+
window closed and reopened with the same name, text and position gives
|
|
23
|
+
`element_no_longer_valid`.
|
|
24
|
+
- An element in a window that has no CGWindowID is refused with
|
|
25
|
+
`policy_denied` instead of being matched by title and frame, since a closed
|
|
26
|
+
and reopened window could not be told apart. Finder's desktop (the only such
|
|
27
|
+
window seen on the test Mac) is exempt. `mac_ax_state` and `mac_find` mark
|
|
28
|
+
such a window with `window_identity: "unverifiable"` and a hint. A physical
|
|
29
|
+
click whose hit test is inconclusive and whose window has no CGWindowID is
|
|
30
|
+
refused (`occluded`).
|
|
31
|
+
|
|
32
|
+
### Tests
|
|
33
|
+
- `NORACU_TEST_PHYSICAL_GAP_MS` (test hook, never set in normal use) waits
|
|
34
|
+
between the last check and the click and reports it on stderr. The local test
|
|
35
|
+
suite uses it to cover, move and close the target in that gap, and to close
|
|
36
|
+
and reopen a window under an app-level index.
|
|
37
|
+
|
|
3
38
|
## [0.2.1] — 2026-11-06
|
|
4
39
|
|
|
5
40
|
### Fixed
|
package/README.md
CHANGED
|
@@ -151,8 +151,8 @@ noracu runs with the privileges of your MCP client. It clicks, types, pastes, an
|
|
|
151
151
|
- **`NORACU_DISABLE_EVAL=1`** makes `browser_evaluate` return an error when called (it stays listed in `tools/list`). Snapshot, click, and type still work.
|
|
152
152
|
- **`file://` is refused** unless `NORACU_ALLOW_FILE_URL=1`, so a browser agent can't read `~/Documents` by navigating to it. (The initial navigation URL is scheme-checked; redirects are not — see SECURITY.md.)
|
|
153
153
|
- **Focus guard:** pass `app` to `mac_paste` / `mac_key` (and `mac_type` when it sends keystrokes) and input is refused unless that app is frontmost. Omit `app` and the guard is off. `mac_type`'s default accessibility path writes into a named element or the app's focused element and sends no keystrokes at all; it never falls back to keystrokes when the focus is a button, list or sheet.
|
|
154
|
-
- **Stale-element protection:** an `element_index` is not a coordinate. It expires after 30 s, and on the Mac it is never reused by a later snapshot. A Mac element is re-identified inside the very window or open menu it was seen in (by window ID), at the same place in that window, under the same labelled ancestors and in the same table row; an element without a label or identifier must still be exactly where it was. A browser click re-checks tag/name/href/position. Otherwise `element_no_longer_valid`.
|
|
155
|
-
- **Hit test before physical clicks:** a Mac click or drag that has to use the pointer is sent only when the element at that point is the target — or, when accessibility cannot tell, when the topmost on-screen window there is that same window (by window ID, not covered by another window of the same app); otherwise `occluded`, with what is on top. A click at raw x,y goes out only when the topmost on-screen window at that point belongs to `app` (default: the frontmost app; the menu bar counts as the frontmost app's); `force: true` skips that check. Keystrokes typed after such a click go out only if the element's app is then frontmost.
|
|
154
|
+
- **Stale-element protection:** an `element_index` is not a coordinate. It expires after 30 s, and on the Mac it is never reused by a later snapshot. A Mac element is re-identified inside the very window or open menu it was seen in (by window ID), at the same place in that window, under the same labelled ancestors and in the same table row; an element without a label or identifier must still be exactly where it was. An element read with `surface: "app"` is bound to its window's ID too, and a window without an ID (other than Finder's desktop) is refused with `policy_denied`, so a window closed and reopened with the same content never inherits old indexes. A browser click re-checks tag/name/href/position. Otherwise `element_no_longer_valid`.
|
|
155
|
+
- **Hit test before physical clicks:** a Mac click or drag that has to use the pointer is sent only when the element at that point is the target — or, when accessibility cannot tell, when the topmost on-screen window there is that same window (by window ID, not covered by another window of the same app); otherwise `occluded`, with what is on top. Right before the click is sent, the window on top at the point is checked again; afterwards the result is `delivered_verified` only if the element is still there, changed, and its own window is still on top at the point. A click at raw x,y goes out only when the topmost on-screen window at that point belongs to `app` (default: the frontmost app; the menu bar counts as the frontmost app's); `force: true` skips that check. Keystrokes typed after such a click go out only if the element's app is then frontmost.
|
|
156
156
|
- **Background windows:** semantic Mac actions also work on windows you are not looking at. That is the point (no stolen focus), but it means an agent can act where you do not see it — watch the results' `delivery` and keep high-stakes apps closed.
|
|
157
157
|
- **Guarded keys, menus and quits:** `mac_key` refuses quit / log-out / lock / force-quit / delete-immediately / empty-Trash chords unless `force: true`; `mac_menu_command` applies the same chord list to menu shortcuts and refuses Apple-menu items unless `force: true`. Neither `mac_app` nor `mac_menu_command` ever quits Finder, the Dock, loginwindow, other session processes or the MCP host itself; `force: true` is needed on `mac_app` to force-quit.
|
|
158
158
|
- **DevTools ports are opt-in:** `mac_app` / `browser_launch` with `cdp_port` need `NORACU_ALLOW_CDP=1` (`NORACU_ALLOW_CDP=any` to attach to a port `noracu` did not open itself; always refused under `NORACU_READ_ONLY=1`), bind to 127.0.0.1 only, canonicalize `.app` paths and refuse an app that is already running, and give Chrome-family browsers a separate `0700` profile under `~/.noracu/cdp-profiles`. While the port is open, any local process can control that app (127.0.0.1 has no per-user isolation).
|
package/README.zh-CN.md
CHANGED
|
@@ -151,8 +151,8 @@ noracu 以你 MCP client 的权限运行。它会在本机点击、输入、粘
|
|
|
151
151
|
- **`NORACU_DISABLE_EVAL=1`** 让 `browser_evaluate` 被调用时报错(仍列在 `tools/list` 中)。snapshot、click、type 保留。
|
|
152
152
|
- **`file://` 被拒**,除非 `NORACU_ALLOW_FILE_URL=1`,这样浏览器 agent 无法通过导航到 `~/Documents` 来读本地文件。(仅校验初始导航 URL;重定向不校验——见 SECURITY.md。)
|
|
153
153
|
- **焦点守卫:** 给 `mac_paste` / `mac_key`(以及以按键方式输入时的 `mac_type`)传 `app`,当该应用不在最前时输入被拒。不传 `app` 则守卫关闭。`mac_type` 默认走辅助功能写入指定元素或该应用的焦点元素,根本不发按键;焦点是按钮、列表或 sheet 时,它也不会退回到发按键。
|
|
154
|
-
- **失效元素保护:** `element_index` 不是坐标。它 30 秒后过期;Mac 上的索引不会被之后的快照复用。Mac 元素只在它被看到的那个窗口或打开的菜单里重新识别(按窗口 ID
|
|
155
|
-
- **物理点击前做命中检测:** 必须用指针的 Mac 点击或拖动,只有该点上的元素就是目标时才发送——辅助功能无法判断时,要求该点最上层的屏幕窗口正是元素所在的同一个窗口(按窗口 ID,未被同一应用的另一个窗口遮挡);否则返回 `occluded`
|
|
154
|
+
- **失效元素保护:** `element_index` 不是坐标。它 30 秒后过期;Mac 上的索引不会被之后的快照复用。Mac 元素只在它被看到的那个窗口或打开的菜单里重新识别(按窗口 ID),并且要在窗口里的同一位置、同样的有名祖先之下、同一表格行中;没有名称也没有标识符的元素必须仍在原处。用 `surface: "app"` 读到的元素同样绑定所在窗口的 ID;没有窗口 ID 的窗口(Finder 桌面除外)一律以 `policy_denied` 拒绝,所以关掉再重开的同内容窗口不会继承旧索引。browser 点击重校验 tag/name/href/位置。否则返回 `element_no_longer_valid`。
|
|
155
|
+
- **物理点击前做命中检测:** 必须用指针的 Mac 点击或拖动,只有该点上的元素就是目标时才发送——辅助功能无法判断时,要求该点最上层的屏幕窗口正是元素所在的同一个窗口(按窗口 ID,未被同一应用的另一个窗口遮挡);否则返回 `occluded` 并说明上面是什么。点击发出前会再查一次该点最上层的窗口;发出后,只有元素仍在、状态有变化、且该点最上层仍是元素自己的窗口,结果才是 `delivered_verified`。按原始坐标 x,y 点击时,只有该点最上层的屏幕窗口属于 `app`(默认为前台应用;菜单栏算前台应用的)才发送;`force: true` 跳过这项检查。这样的点击之后再敲的键,只在元素所在应用确实位于前台时才发送。
|
|
156
156
|
- **后台窗口:** 语义 Mac 动作对你没在看的窗口同样有效。这正是目的(不抢焦点),但也意味着 agent 可能在你看不到的地方动作——留意结果里的 `delivery`,高风险应用请先关掉。
|
|
157
157
|
- **危险按键、菜单与退出受保护:** `mac_key` 拒绝退出 / 注销 / 锁屏 / 强制退出 / 立即删除 / 清空废纸篓类组合键,除非 `force: true`;`mac_menu_command` 对菜单快捷键应用同一列表,且拒绝苹果菜单项,除非 `force: true`。`mac_app` 和 `mac_menu_command` 都从不退出 Finder、Dock、loginwindow 等会话进程和 MCP 宿主本身;`mac_app` 强制退出需要 `force: true`。
|
|
158
158
|
- **DevTools 端口需显式开启:** `mac_app` / `browser_launch` 带 `cdp_port` 需要 `NORACU_ALLOW_CDP=1`(连接非 `noracu` 自己打开的端口需 `NORACU_ALLOW_CDP=any`;`NORACU_READ_ONLY=1` 下始终拒绝),只绑定 127.0.0.1,规范化 `.app` 路径并拒绝已在运行的应用,且在 `~/.noracu/cdp-profiles` 下给 Chrome 系浏览器单独的 `0700` profile。端口打开期间,任何本地进程都能控制该应用(127.0.0.1 没有按用户隔离)。
|
package/SECURITY.md
CHANGED
|
@@ -9,7 +9,7 @@ Treat every tool call as privileged.
|
|
|
9
9
|
| # | Threat | Vector | Status | Mitigation / notes |
|
|
10
10
|
|---|---|---|---|---|
|
|
11
11
|
| 1 | **Prompt injection → arbitrary code execution** | Observed content (web page text, AX tree titles, OCR output) contains instructions that trick the agent into calling tools with malicious arguments | **Mitigated** | The Mac accessibility engine `src/mac-ax.js` is a static script that takes its whole request as JSON on stdin — parameters are data, never script source (regression-tested in `test/e2e.mjs`); the remaining inline JXA (scroll wheel) takes clamped numbers through argv |
|
|
12
|
-
| 2 | **Arbitrary local control** | `mac_click` / `mac_type` / `mac_paste` can operate password managers, banking pages, terminals | **Inherent** | Use client-side confirmation gates for high-stakes targets; see recommendations below. Optional `app` focus guard on `mac_paste` / `mac_key` (and `mac_type` keystrokes) refuses input unless that app — compared by process, not by name — is frontmost; `mac_type` keystrokes after a click on an `element_index` go out only if the element's app is then frontmost. An `element_index` is re-identified inside the window it came from (by CGWindowID) before any action, and a physical click or drag (both ends) is sent only when a hit test shows the point reaches the element, or — when the hit test is inconclusive — when the topmost on-screen window at the point is that same window (by CGWindowID, not covered by another window of the same app; `occluded` otherwise). A `mac_click` at raw x,y is sent only when the topmost on-screen window at the point belongs to `app` (default: the frontmost app; the menu bar counts as the frontmost app's), unless `force: true`; a short gap remains between that check and the click. `mac_key` refuses quit / log-out / lock / force-quit / delete-immediately / empty-Trash chords without `force`; `mac_menu_command` applies the same chord list, refuses Apple-menu items without `force`, and never quits the MCP host or a session process. |
|
|
12
|
+
| 2 | **Arbitrary local control** | `mac_click` / `mac_type` / `mac_paste` can operate password managers, banking pages, terminals | **Inherent** | Use client-side confirmation gates for high-stakes targets; see recommendations below. Optional `app` focus guard on `mac_paste` / `mac_key` (and `mac_type` keystrokes) refuses input unless that app — compared by process, not by name — is frontmost; `mac_type` keystrokes after a click on an `element_index` go out only if the element's app is then frontmost. An `element_index` is re-identified inside the window it came from (by CGWindowID) before any action, and a physical click or drag (both ends) is sent only when a hit test shows the point reaches the element, or — when the hit test is inconclusive — when the topmost on-screen window at the point is that same window (by CGWindowID, not covered by another window of the same app; `occluded` otherwise). Right before a physical click or drag is sent, the window on top at each point is checked again (by CGWindowID); afterwards a physical click counts as `delivered_verified` only if the element is still there, changed, and its own window is still on top at the click point. An element read with `surface: "app"` is bound to its window's CGWindowID, and an element in a window without a CGWindowID (other than Finder's desktop) is refused with `policy_denied`. A `mac_click` at raw x,y is sent only when the topmost on-screen window at the point belongs to `app` (default: the frontmost app; the menu bar counts as the frontmost app's), unless `force: true`; a short gap remains between that check and the click. `mac_key` refuses quit / log-out / lock / force-quit / delete-immediately / empty-Trash chords without `force`; `mac_menu_command` applies the same chord list, refuses Apple-menu items without `force`, and never quits the MCP host or a session process. |
|
|
13
13
|
| 3 | **Arbitrary JS in browser** | `browser_evaluate` executes any JavaScript in the page context | **Partially mitigated** | Set `NORACU_DISABLE_EVAL=1`; `browser_evaluate` then returns an error on every call (it stays listed in `tools/list`) |
|
|
14
14
|
| 4 | **Input validation gaps** | Malformed arguments (NaN coordinates, oversized strings, `javascript:` URLs) | **Mitigated** | `validateArgs` validates every tool's arguments (types, ranges, enums, string lengths, URL schemes) before any subprocess runs; `tools/list` emits `required` arrays derived from non-optional arguments |
|
|
15
15
|
| 5 | **Screenshot privacy** | `mac_screenshot` / `phone_screenshot` capture the full screen / phone display | **Inherent** | Run in a dedicated macOS user account or virtual display for untrusted automation. `NORACU_READ_ONLY=1` prevents side effects (and skips `enableWebAccessibility` on apps), not screen secrecy: `mac_screenshot` still shows visible notification banners on screen, and Notification Center accessibility reads via `mac_ax_state` / `mac_find` are not blocked by bundle id |
|
package/docs/architecture.md
CHANGED
|
@@ -22,7 +22,7 @@ Accessibility goes through `src/mac-ax.js`, a static JXA script run as `osascrip
|
|
|
22
22
|
|---|---|
|
|
23
23
|
| `mac_ax_state` | engine `snapshot`: walks a window (or `surface`: menu bar, open menu, sheet, alert, dialog, popover, focused element, whole app) to a logical depth (layout wrappers without content do not count and are hidden by `compact`); `interactive` keeps actionable elements; `skeleton` maps 3 levels with `children_count`, `root` drills into one element and extends the same snapshot. Chromium/Electron apps get `AXManualAccessibility` switched on when read unless `web_accessibility: false` or `NORACU_READ_ONLY=1` (never `AXEnhancedUserInterface`, which alters window behaviour). When the app refuses it (Google Chrome: -25205), a window is searched for an `AXWebArea` (≤1.5 s): found → `exposed` (pages are in the tree anyway, e.g. Chrome started with `--force-renderer-accessibility`), else `unsupported`, and the server adds `web_accessibility_hint` |
|
|
24
24
|
| `mac_find` / `mac_get` | engine `find` (role / title / value / text / AXIdentifier, substring or exact; honours `web_accessibility` like `mac_ax_state`) / `get` (one element, live: full value, states, actions, settable attributes) |
|
|
25
|
-
| `mac_click` | element_index: the element's own action (`AXPress` / `AXConfirm` / `AXPick` / `AXOpen`, `AXShowMenu` for right-click, or selecting a row) when it has one; otherwise `cliclick` at its center after a system-wide hit test — or, when inconclusive, confirming the topmost on-screen window there is that same window by CGWindowID (`occluded` otherwise). Raw x,y: `cliclick` only when engine `frontmost` with `at` reports the topmost on-screen window there belongs to `app` (default: the frontmost app; the Window Server menu bar strip, layer 24, counts as the frontmost app's) — `occluded` otherwise, `force: true` skips the check |
|
|
25
|
+
| `mac_click` | element_index: the element's own action (`AXPress` / `AXConfirm` / `AXPick` / `AXOpen`, `AXShowMenu` for right-click, or selecting a row) when it has one; otherwise `cliclick` at its center after a system-wide hit test — or, when inconclusive, confirming the topmost on-screen window there is that same window by CGWindowID (`occluded` otherwise; refused when the element's window has no CGWindowID, except Finder's desktop). Right before `cliclick`, engine `frontmost` with `at` re-checks that the element's window is still on top; after the click, `observe_after` with `physical` reports `delivered_verified` only when the element is still there, changed, and `cgTopAt` still finds its window at the point. `NORACU_TEST_PHYSICAL_GAP_MS` (test hook) waits between that re-check and the click. Raw x,y: `cliclick` only when engine `frontmost` with `at` reports the topmost on-screen window there belongs to `app` (default: the frontmost app; the Window Server menu bar strip, layer 24, counts as the frontmost app's) — `occluded` otherwise, `force: true` skips the check |
|
|
26
26
|
| `mac_type` | `AXSelectedText` (insert at the caret) or `AXValue` (`replace`) on element_index or the focused element; keystrokes (`cliclick t:`) only when accessibility reports no focus or a text view it cannot write |
|
|
27
27
|
| `mac_set_value` | by role: `AXValue` for text (never read back or echoed on `AXSecureTextField`), sliders, combo boxes; `AXPress` for toggles (only when the state differs); `AXExpanded` / `AXDisclosing`; `AXSelected`; pop-up and menu buttons open their menu, press the one matching option, or close it again |
|
|
28
28
|
| `mac_scroll` | no index: wheel event at the frontmost window; scroll area: wheel event at its center (hit-tested); other element: `AXScrollToVisible` |
|
package/docs/tools.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Tool reference
|
|
2
2
|
|
|
3
3
|
> Auto-generated from the running server (`node scripts/gen-tools.mjs`) — always in sync with the implementation.
|
|
4
|
-
> noracu v0.2.
|
|
4
|
+
> noracu v0.2.2 · 46 tools
|
|
5
5
|
|
|
6
6
|
## Mac layer (computer-use)
|
|
7
7
|
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
| Tool | Arguments | Description |
|
|
11
11
|
|---|---|---|
|
|
12
12
|
| `mac_screenshot` | region:object?, window_id:integer?, display:integer? | Capture the Mac screen as PNG base64: the main display, display (index from mac_windows), or one window by window_id (CGWindowID from mac_windows; works when covered), optionally cropped to region {x,y,w,h}. Returns {scale, logical_size, screen}; click coordinates are in points (pixels / scale). |
|
|
13
|
-
| `mac_ax_state` | app:string?, interactive:boolean?, compact:boolean?, skeleton:boolean?, root:integer?, snapshot_id:string?, surface:string?, window:string?, window_id:integer?, depth:number?, max_nodes:integer?, web_accessibility:boolean? | Read an app's accessibility tree through the macOS Accessibility API (default: the frontmost app's focused window). Returns {app, snapshot_id, window, complete, elements:[{i, d, role, title, value, states, actions, frame}]}; i is the element_index for mac_click, mac_get, mac_set_value, mac_type, mac_scroll and mac_drag — unique across snapshots (an index never names an element of a later snapshot) and valid 30 s; an action first re-identifies the element and returns element_no_longer_valid if it is gone, moved or replaced. Options: interactive (only actionable elements), compact (default true: skip empty layout wrappers), skeleton (shallow map with children_count) then root (an element_index to drill into; adds new indexes to the same snapshot), surface (window\|menubar\|menu\|sheet\|alert\|dialog\|popover\|focused\|app), window (index or title) or window_id (from mac_windows), depth (logical depth, default 12, 1-40), max_nodes (default 400), web_accessibility (default true, false under NORACU_READ_ONLY=1: sets AXManualAccessibility on Electron/Chromium apps to expose web content, never AXEnhancedUserInterface which alters window behaviour; the result's web_accessibility is enabled / already_on, exposed when the app refused but its pages are in the tree anyway, or unsupported with a web_accessibility_hint — Google Chrome refuses it). ax_empty when the app has no readable window. |
|
|
14
|
-
| `mac_click` | element_index:integer?, snapshot_id:string?, x:number?, y:number?, app:string?, force:boolean?, button:string?, count:number?, mechanism:string?, timeout_ms:integer? | Click a UI element by element_index (from mac_ax_state / mac_find; valid 30 s) or at raw {x,y}. With element_index the element is re-identified first (element_no_longer_valid if gone or changed). mechanism auto (default): its own accessibility action (press / show menu / select — no pointer, no focus change, works in background windows) when it has one, else a physical click that is sent only if a hit test shows the point reaches the element (occluded otherwise); semantic / physical force one path. count 2-3 and button middle are physical. A raw {x,y} click is sent only when the window on top at that point belongs to app (default: the frontmost app; the menu bar counts as the frontmost app's) — otherwise occluded names what covers it and nothing is sent; force:true skips that check. Results state delivery (delivered_verified / delivered_unverified / not_delivered / unknown) and whether a retry is safe, plus newly opened sheets, dialogs or menus. |
|
|
13
|
+
| `mac_ax_state` | app:string?, interactive:boolean?, compact:boolean?, skeleton:boolean?, root:integer?, snapshot_id:string?, surface:string?, window:string?, window_id:integer?, depth:number?, max_nodes:integer?, web_accessibility:boolean? | Read an app's accessibility tree through the macOS Accessibility API (default: the frontmost app's focused window). Returns {app, snapshot_id, window, complete, elements:[{i, d, role, title, value, states, actions, frame}]}; i is the element_index for mac_click, mac_get, mac_set_value, mac_type, mac_scroll and mac_drag — unique across snapshots (an index never names an element of a later snapshot) and valid 30 s; an action first re-identifies the element and returns element_no_longer_valid if it is gone, moved or replaced. Options: interactive (only actionable elements), compact (default true: skip empty layout wrappers), skeleton (shallow map with children_count) then root (an element_index to drill into; adds new indexes to the same snapshot), surface (window\|menubar\|menu\|sheet\|alert\|dialog\|popover\|focused\|app), window (index or title) or window_id (from mac_windows), depth (logical depth, default 12, 1-40), max_nodes (default 400), web_accessibility (default true, false under NORACU_READ_ONLY=1: sets AXManualAccessibility on Electron/Chromium apps to expose web content, never AXEnhancedUserInterface which alters window behaviour; the result's web_accessibility is enabled / already_on, exposed when the app refused but its pages are in the tree anyway, or unsupported with a web_accessibility_hint — Google Chrome refuses it). window_identity unverifiable marks a window without a window ID: element_index actions in it are refused. ax_empty when the app has no readable window. |
|
|
14
|
+
| `mac_click` | element_index:integer?, snapshot_id:string?, x:number?, y:number?, app:string?, force:boolean?, button:string?, count:number?, mechanism:string?, timeout_ms:integer? | Click a UI element by element_index (from mac_ax_state / mac_find; valid 30 s) or at raw {x,y}. With element_index the element is re-identified first (element_no_longer_valid if gone or changed). mechanism auto (default): its own accessibility action (press / show menu / select — no pointer, no focus change, works in background windows) when it has one, else a physical click that is sent only if a hit test shows the point reaches the element (occluded otherwise); semantic / physical force one path. count 2-3 and button middle are physical. A physical click is re-checked right before it is sent, and is delivered_verified only if the element is still there, changed, and its window is still on top at the point. A raw {x,y} click is sent only when the window on top at that point belongs to app (default: the frontmost app; the menu bar counts as the frontmost app's) — otherwise occluded names what covers it and nothing is sent; force:true skips that check. Results state delivery (delivered_verified / delivered_unverified / not_delivered / unknown) and whether a retry is safe, plus newly opened sheets, dialogs or menus. |
|
|
15
15
|
| `mac_drag` | **from_index**:integer, **to_index**:integer, snapshot_id:string? | Drag from one UI element to another (sliders, selections, list items) with the physical pointer; from_index/to_index come from mac_ax_state or mac_find (valid 30 s). Both ends are re-identified and hit-tested first (either one gone or covered: nothing sent). Check the drop target afterwards. |
|
|
16
16
|
| `mac_scroll` | direction:string?, pages:number?, element_index:integer?, snapshot_id:string? | Scroll up/down/left/right by a number of pages (default 1). Without element_index: the frontmost window (the pointer moves there). With element_index: a scroll area is scrolled at its own center (hit-tested; reports when the position did not change), any other element is scrolled into view (AXScrollToVisible). |
|
|
17
17
|
| `mac_key` | **key**:string, app:string?, force:boolean? | Press a key or modifier chord, e.g. 'return', 'esc', 'cmd-c', 'cmd-shift-v', 'arrow-up'. Named keys (return, esc, tab, space, delete, arrows, home/end, page keys, F1-F16), with or without modifiers, go out as HID key events with each modifier pressed and released around the key; chords on a character key (cmd-c) go through cliclick. Pass app to refuse input unless that app is frontmost (focus guard). Chords that quit, log out, lock, force-quit, delete immediately or empty the Trash (cmd-q, alt-cmd-q, cmd-shift-q, cmd-alt-esc, ctrl-cmd-q, cmd-alt-delete, cmd-shift-delete…) are refused unless force:true. |
|
package/imctl/pyproject.toml
CHANGED
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "imctl"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.2"
|
|
8
8
|
description = "Control iPhone Mirroring on macOS from the CLI — CGEvent input, Vision OCR, pixel verification"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { text = "MIT" }
|
package/package.json
CHANGED
package/src/mac-ax.js
CHANGED
|
@@ -296,11 +296,38 @@ function axWindowId(w) {
|
|
|
296
296
|
if (!AXGW) return null;
|
|
297
297
|
try { const r = Ref('I'); return Number($._AXUIElementGetWindow(w, r)) === 0 && Number(r[0]) > 0 ? Number(r[0]) : null; } catch (e) { return null; }
|
|
298
298
|
}
|
|
299
|
+
// The CGWindowID of the window an element is in: the element itself when it is a window, else its
|
|
300
|
+
// AXWindow, else the nearest AXWindow up its AXParent chain. null when none has an id.
|
|
301
|
+
function windowIdOf(el) {
|
|
302
|
+
if (!el) return null;
|
|
303
|
+
if (attr(el, 'AXRole') === 'AXWindow') return axWindowId(el);
|
|
304
|
+
const aw = attr(el, 'AXWindow');
|
|
305
|
+
if (aw && aw.el) { const id = axWindowId(aw.el); if (id) return id; }
|
|
306
|
+
let c = el;
|
|
307
|
+
for (let k = 0; k < 64 && c; k++) {
|
|
308
|
+
const p = attr(c, 'AXParent');
|
|
309
|
+
c = p && p.el ? p.el : null;
|
|
310
|
+
if (c && attr(c, 'AXRole') === 'AXWindow') return axWindowId(c);
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
299
314
|
function windowInfo(w, n) {
|
|
300
315
|
const v = readNode(w);
|
|
301
316
|
return { kind: 'window', n, wid: axWindowId(w), title: typeof v.AXTitle === 'string' ? v.AXTitle : '', subrole: v.AXSubrole || '', frame: frameOf(v), focused: v.AXFocused === true, main: attr(w, 'AXMain') === true, minimized: attr(w, 'AXMinimized') === true };
|
|
302
317
|
}
|
|
303
318
|
function rectNear(a, b, tol) { return a && b && Math.abs(a[0] - b[0]) <= tol && Math.abs(a[1] - b[1]) <= tol && Math.abs(a[2] - b[2]) <= tol && Math.abs(a[3] - b[3]) <= tol; }
|
|
319
|
+
// Finder's desktop: the one window without a CGWindowID seen on the test Mac (wid probe 2026-11-06; its
|
|
320
|
+
// frame is the bounding box of all displays). It is never closed and reopened, so its elements keep the
|
|
321
|
+
// title/frame match; any other window without an id is refused (a closed and reopened window with the
|
|
322
|
+
// same title and frame could not be told apart).
|
|
323
|
+
function isFinderDesktop(app, c) {
|
|
324
|
+
if (!(app && app.bundle === 'com.apple.finder' && c && c.title === '' && !c.subrole && c.frame)) return false;
|
|
325
|
+
const ds = displays().map(d => d.frame);
|
|
326
|
+
if (!ds.length) return false;
|
|
327
|
+
const x0 = Math.min(...ds.map(f => f[0])), y0 = Math.min(...ds.map(f => f[1]));
|
|
328
|
+
const x1 = Math.max(...ds.map(f => f[0] + f[2])), y1 = Math.max(...ds.map(f => f[1] + f[3]));
|
|
329
|
+
return rectNear([x0, y0, x1 - x0, y1 - y0], c.frame, 2) || ds.some(f => rectNear(f, c.frame, 2));
|
|
330
|
+
}
|
|
304
331
|
|
|
305
332
|
// Pick the window a snapshot is about: an explicit index/title, else the focused or main window,
|
|
306
333
|
// else the only window. Several candidates and no way to choose → AMBIGUOUS_WINDOW with the list.
|
|
@@ -448,8 +475,12 @@ function walk(rootEl, rootPath, opt) {
|
|
|
448
475
|
const kids = v.AXChildren || [];
|
|
449
476
|
const atLimit = ldepth >= maxDepth;
|
|
450
477
|
const show = !(opt.compact && wrapper) && (!opt.interactive || interactive || (skeleton && atLimit && kids.length));
|
|
478
|
+
// app-level snapshots (no window container): an element under a window records that window's
|
|
479
|
+
// CGWindowID, so a window closed and reopened with the same content is not taken for the old one
|
|
480
|
+
const wwid = opt.trackWindow ? (role === 'AXWindow' ? axWindowId(el) : anc.wwid) : null;
|
|
451
481
|
if (opt.find ? matches(v, opt.find) : show) {
|
|
452
482
|
const node = makeNode(v, ldepth, path, actions, interactive);
|
|
483
|
+
if (wwid) node.fp.wwid = wwid;
|
|
453
484
|
if (anc.chain.length) node.fp.chain = chainKey(anc.chain);
|
|
454
485
|
node._shape = anc.shape.slice();
|
|
455
486
|
node._accs = anc.accs;
|
|
@@ -466,7 +497,7 @@ function walk(rootEl, rootPath, opt) {
|
|
|
466
497
|
const own = { texts: [], seen: 0, full: false, cut: false, closed: false, noItem: (isRoot && Boolean(opt.rootIsContainer)) || ITEM_SKIP_ROLES.has(role) };
|
|
467
498
|
if (nodes.length && nodes[nodes.length - 1]._el === el) nodes[nodes.length - 1]._own = own;
|
|
468
499
|
const cell = CELL_ROLES.has(role);
|
|
469
|
-
const childAnc = { shape: anc.shape.concat(kids.length), chain: name ? anc.chain.concat(name) : anc.chain, accs: [own].concat(anc.accs).slice(0, ITEM_LEVELS), cells: [cell].concat(anc.cells.map(c => c || cell)).slice(0, ITEM_LEVELS) };
|
|
500
|
+
const childAnc = { shape: anc.shape.concat(kids.length), chain: name ? anc.chain.concat(name) : anc.chain, accs: [own].concat(anc.accs).slice(0, ITEM_LEVELS), cells: [cell].concat(anc.cells.map(c => c || cell)).slice(0, ITEM_LEVELS), wwid };
|
|
470
501
|
if (atLimit) own.cut = true;
|
|
471
502
|
for (let i = 0; i < kids.length; i++) {
|
|
472
503
|
if (visit(kids[i], path.concat(i), nextDepth, inMenuBar || role === 'AXMenuBar', childAnc) === 'stop') return 'stop';
|
|
@@ -475,7 +506,7 @@ function walk(rootEl, rootPath, opt) {
|
|
|
475
506
|
}
|
|
476
507
|
const pre = opt.evidence || { shape: [], chain: [] };
|
|
477
508
|
const seen = {};
|
|
478
|
-
visit(rootEl, rootPath.slice(), 0, false, { shape: pre.shape.slice(), chain: pre.chain.slice(), accs: [], cells: [] });
|
|
509
|
+
visit(rootEl, rootPath.slice(), 0, false, { shape: pre.shape.slice(), chain: pre.chain.slice(), accs: [], cells: [], wwid: opt.trackWindow ? windowIdOf(rootEl) : null });
|
|
479
510
|
// Elements that share their fingerprint with another one in the container (several "Delete" buttons,
|
|
480
511
|
// one per list item) are told apart by their surroundings: the path shape, and the item key. Uniqueness
|
|
481
512
|
// is known only when the whole container was walked: a snapshot that stopped early, or one drilled into
|
|
@@ -701,7 +732,7 @@ function openMenus(appEl) {
|
|
|
701
732
|
if (mb && mb.el) for (const item of children(mb.el)) if (attr(item, 'AXSelected') === true) for (const k of children(item)) if (attr(k, 'AXRole') === 'AXMenu') found.push(k);
|
|
702
733
|
return found;
|
|
703
734
|
}
|
|
704
|
-
function containerEl(appEl, c) {
|
|
735
|
+
function containerEl(appEl, c, app) {
|
|
705
736
|
if (!c || c.kind === 'app') return { el: appEl, shift: [0, 0] };
|
|
706
737
|
if (c.kind === 'menubar') { const mb = attr(appEl, 'AXMenuBar'); return mb && mb.el ? { el: mb.el, shift: [0, 0] } : { error: 'menubar_absent' }; }
|
|
707
738
|
if (c.kind === 'menu') {
|
|
@@ -719,8 +750,11 @@ function containerEl(appEl, c) {
|
|
|
719
750
|
for (const w of ws) if (axWindowId(w) === c.wid) return { el: w, shift: shiftOf(w) };
|
|
720
751
|
return { error: 'window_absent' };
|
|
721
752
|
}
|
|
722
|
-
// no id was available when the element was seen:
|
|
723
|
-
//
|
|
753
|
+
// no id was available when the element was seen: a window closed and reopened with the same title,
|
|
754
|
+
// subrole and frame could not be told apart from it, so refuse — except Finder's desktop, which is
|
|
755
|
+
// never reopened. (Reddit r/mcp review 2026-11-06)
|
|
756
|
+
if (!isFinderDesktop(app, c)) return { error: 'window_unverifiable' };
|
|
757
|
+
// the desktop: same title, subrole and frame; a changed frame is a stale reference
|
|
724
758
|
const infos = ws.map((w, i) => windowInfo(w, i));
|
|
725
759
|
const hits = infos.filter(x => x.title === c.title && x.subrole === c.subrole && (!c.frame || rectNear(x.frame, c.frame, POS_TOL)));
|
|
726
760
|
if (hits.length === 1) return { el: ws[hits[0].n], shift: [0, 0] };
|
|
@@ -729,14 +763,27 @@ function containerEl(appEl, c) {
|
|
|
729
763
|
return { error: 'container_unknown' };
|
|
730
764
|
}
|
|
731
765
|
const SEARCH_MAX_DEPTH = 150, SEARCH_MAX_VISIT = 20000;
|
|
766
|
+
// An element seen in an app-level snapshot recorded the CGWindowID of the window it was in (fp.wwid):
|
|
767
|
+
// found again, it must still be in that very window — a window closed and reopened with the same content
|
|
768
|
+
// has a new id and is a stale reference (Reddit r/mcp review 2026-11-06).
|
|
732
769
|
function resolveTarget(t, budgetMs) {
|
|
770
|
+
const r = resolveTargetCore(t, budgetMs);
|
|
771
|
+
if (r.error || !t || !t.fp || !t.fp.wwid) return r;
|
|
772
|
+
const live = windowIdOf(r.el);
|
|
773
|
+
if (live === t.fp.wwid) return r;
|
|
774
|
+
return { error: { code: 'STALE_REF', message: live ? 'the element\'s window was closed and another one opened in its place since the snapshot' : 'the element is no longer in the window it was seen in', details: { kind: 'window_replaced' } } };
|
|
775
|
+
}
|
|
776
|
+
function resolveTargetCore(t, budgetMs) {
|
|
733
777
|
if (!$.AXIsProcessTrusted()) return { error: permError() };
|
|
734
778
|
const apps = runningApps().filter(a => a.pid === t.pid);
|
|
735
779
|
if (!apps.length) return { error: { code: 'STALE_REF', message: `the app (pid ${t.pid}) is no longer running`, details: { kind: 'app_gone' } } };
|
|
736
780
|
const app = apps[0];
|
|
737
781
|
const appEl = appElement(app);
|
|
738
782
|
const kind = t.container && t.container.kind || 'app';
|
|
739
|
-
const c = containerEl(appEl, t.container);
|
|
783
|
+
const c = containerEl(appEl, t.container, app);
|
|
784
|
+
if (c.error === 'window_unverifiable') {
|
|
785
|
+
return { error: { code: 'POLICY_DENIED', message: 'the element\'s window had no window ID when it was read, so a window closed and reopened with the same title and frame could not be told apart from it — element_index actions in this window are refused (use the app\'s menu with mac_menu_command, or mac_click x,y, which checks the window on top)', details: { kind: 'window_unverifiable' } } };
|
|
786
|
+
}
|
|
740
787
|
if (c.error) {
|
|
741
788
|
const amb = /ambiguous/.test(c.error);
|
|
742
789
|
const what = kind === 'menu' ? 'the menu the element was in is closed' : 'the element\'s window is gone';
|
|
@@ -877,6 +924,7 @@ function opSnapshot(req) {
|
|
|
877
924
|
const t0 = now();
|
|
878
925
|
const rv = readNode(root);
|
|
879
926
|
if (rv.__err === -25204) return fail({ code: 'APP_UNRESPONSIVE', message: `"${app.name}" did not answer accessibility in time (it may be busy or hung)` });
|
|
927
|
+
opt.trackWindow = !container || container.kind === 'app';
|
|
880
928
|
const w = walk(root, basePath, opt);
|
|
881
929
|
const out = {
|
|
882
930
|
ok: true, app: app.name, bundle_id: app.bundle, pid: app.pid, surface, complete: w.complete,
|
|
@@ -885,6 +933,7 @@ function opSnapshot(req) {
|
|
|
885
933
|
if (req.root) out.root_path = basePath;
|
|
886
934
|
if (winInfo) out.window = { title: winInfo.title, frame: winInfo.frame, n: winInfo.n, window_id: winInfo.wid };
|
|
887
935
|
if (webAx) out.web_accessibility = webAxStatus(webAx, root, basePath, surface === 'window');
|
|
936
|
+
if (container && container.kind === 'window' && !container.wid && !isFinderDesktop(app, container)) out.window_identity = 'unverifiable';
|
|
888
937
|
return out;
|
|
889
938
|
}
|
|
890
939
|
function containerOut(c) {
|
|
@@ -921,9 +970,10 @@ function opFind(req) {
|
|
|
921
970
|
else { const w = pickWindow(appEl, req); if (w.error) return fail(w.error); root = w.el; container = w.info; }
|
|
922
971
|
const f = { role: req.role, title: req.title, value: req.value, id: req.id, text: req.text, exact: Boolean(req.exact), limit: clamp(req.limit, 1, 50, 10) };
|
|
923
972
|
if (!(f.role || f.title || f.value || f.id || f.text)) return fail({ code: 'INVALID_ARGS', message: 'give at least one of role, title, value, id, text' });
|
|
924
|
-
const w = walk(root, basePath, { depth: 60, maxNodes: 100000, budgetMs: clamp(req.budget_ms, 500, 20000, 8000), maxVisit: 30000, find: f, pruneMenuBar: surface !== 'menubar', rootIsContainer: true });
|
|
973
|
+
const w = walk(root, basePath, { depth: 60, maxNodes: 100000, budgetMs: clamp(req.budget_ms, 500, 20000, 8000), maxVisit: 30000, find: f, pruneMenuBar: surface !== 'menubar', rootIsContainer: true, trackWindow: surface === 'app' });
|
|
925
974
|
const out = { ok: true, app: app.name, bundle_id: app.bundle, pid: app.pid, surface, container: containerOut(container), nodes: w.nodes, complete: w.complete, visited: w.visited };
|
|
926
975
|
if (webAx) out.web_accessibility = webAxStatus(webAx, root, basePath, surface === 'window');
|
|
976
|
+
if (container && container.kind === 'window' && !container.wid && !isFinderDesktop(app, container)) out.window_identity = 'unverifiable';
|
|
927
977
|
return out;
|
|
928
978
|
}
|
|
929
979
|
|
|
@@ -1240,6 +1290,10 @@ function opPhysicalPrep(req) {
|
|
|
1240
1290
|
const x = Math.round(fr[0] + fr[2] / 2), y = Math.round(fr[1] + fr[3] / 2);
|
|
1241
1291
|
if (!onSomeDisplay(x, y)) return fail(notDelivered({ code: 'NOT_ACTIONABLE', message: `the element's center (${x},${y}) is off every display — scroll it into view (mac_scroll with element_index) or move the window`, details: { kind: 'offscreen' } }));
|
|
1242
1292
|
if (attr(r.el, 'AXWindow') && attr(attr(r.el, 'AXWindow').el, 'AXMinimized') === true) return fail(notDelivered({ code: 'NOT_ACTIONABLE', message: 'the element\'s window is minimized', details: { kind: 'minimized' } }));
|
|
1293
|
+
// the element's window id: its AXWindow or the window up its AXParent chain (R20); else, for a window
|
|
1294
|
+
// snapshot, the snapshot's window
|
|
1295
|
+
let ewid = windowIdOf(r.el);
|
|
1296
|
+
if (!ewid && req.target && req.target.container && req.target.container.kind === 'window') ewid = (r.containerEl && axWindowId(r.containerEl)) || req.target.container.wid || null;
|
|
1243
1297
|
const h = hitTest(r.el, x, y);
|
|
1244
1298
|
if (h.verdict === 'intercepted') return fail(notDelivered({ code: 'OCCLUDED', message: `something else is on top of the element at ${x},${y} — bring its window to the front or close what covers it`, details: { occluder: h.occluder } }));
|
|
1245
1299
|
if (h.verdict === 'unknown') {
|
|
@@ -1251,22 +1305,14 @@ function opPhysicalPrep(req) {
|
|
|
1251
1305
|
// the app's own window, but not the one the element is in (a floating panel, another document)
|
|
1252
1306
|
// the element's own window id; else its AXWindow's; else (a window snapshot) the snapshot's window —
|
|
1253
1307
|
// an unknown window id must not skip the check (R19)
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
let c = r.el;
|
|
1259
|
-
for (let k = 0; k < 64 && c && !ewid; k++) {
|
|
1260
|
-
const p = attr(c, 'AXParent');
|
|
1261
|
-
c = p && p.el ? p.el : null;
|
|
1262
|
-
if (c && attr(c, 'AXRole') === 'AXWindow') ewid = axWindowId(c);
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
if (!ewid && req.target && req.target.container && req.target.container.kind === 'window') ewid = (r.containerEl && axWindowId(r.containerEl)) || req.target.container.wid || null;
|
|
1308
|
+
// without the element's window id the topmost window cannot be confirmed to be that window: refuse
|
|
1309
|
+
// (R20 follow-up) — except Finder's desktop, which sits below every window and has no id
|
|
1310
|
+
const desktop = req.target && req.target.container && isFinderDesktop(r.app, req.target.container);
|
|
1311
|
+
if (!ewid && !desktop) return fail(notDelivered({ code: 'OCCLUDED', message: `the hit test at ${x},${y} was inconclusive and the element's window has no window ID, so the window on top cannot be confirmed to be its own — use the element's semantic action (mechanism auto) or bring its window to the front and take a new snapshot`, details: { occluder: { app: top.owner, layer: top.layer, window_unverifiable: true } } }));
|
|
1266
1312
|
if (ewid && top.wid && top.wid !== ewid) return fail(notDelivered({ code: 'OCCLUDED', message: `another window of "${top.owner}" is on top at ${x},${y} — bring the element's window to the front or close what covers it`, details: { occluder: { app: top.owner, layer: top.layer, other_window: true } } }));
|
|
1267
1313
|
h.reason = (h.reason ? h.reason + '; ' : '') + 'the topmost window there is the app\'s own';
|
|
1268
1314
|
}
|
|
1269
|
-
return { ok: true, point: [x, y], hit: h.verdict, reason: h.reason, pid: r.app.pid, before: observeState(r.el, r.appEl), role: w.v.AXRole, title: nameOf(w.v).slice(0, 60), how: r.how };
|
|
1315
|
+
return { ok: true, point: [x, y], hit: h.verdict, reason: h.reason, pid: r.app.pid, wid: ewid || null, before: observeState(r.el, r.appEl), role: w.v.AXRole, title: nameOf(w.v).slice(0, 60), how: r.how };
|
|
1270
1316
|
}
|
|
1271
1317
|
const ABSENT_CODES = new Set(['STALE_REF', 'WINDOW_NOT_FOUND', 'APP_NOT_FOUND']);
|
|
1272
1318
|
function resolveOrError(target, budgetMs) {
|
|
@@ -1287,7 +1333,7 @@ function resolveOrError(target, budgetMs) {
|
|
|
1287
1333
|
if (readNode(appEl).__err === -25204 || windowsOf(appEl).some(w => readNode(w).__err === -25204)) {
|
|
1288
1334
|
return { error: { code: 'APP_UNRESPONSIVE', message: 'the app did not answer accessibility in time' } };
|
|
1289
1335
|
}
|
|
1290
|
-
const c = containerEl(appEl, target.container);
|
|
1336
|
+
const c = containerEl(appEl, target.container, apps[0]);
|
|
1291
1337
|
if (!c.error && c.el) {
|
|
1292
1338
|
if (readNode(c.el).__err === -25204) return { error: { code: 'APP_UNRESPONSIVE', message: 'the app did not answer accessibility in time' } };
|
|
1293
1339
|
const skipMenuBar = (target.container && target.container.kind || 'app') !== 'menubar';
|
|
@@ -1306,6 +1352,11 @@ function opObserveAfter(req) {
|
|
|
1306
1352
|
if (!ABSENT_CODES.has(r.error.code)) {
|
|
1307
1353
|
return { ok: true, delivery: 'delivered_unverified', retry: 'unsafe', changed: [], surfaces: [], note: `could not read the element after the action (${r.error.code})` };
|
|
1308
1354
|
}
|
|
1355
|
+
// gone after a physical click: the click cannot be tied to it — something else may have closed the
|
|
1356
|
+
// window between the hit test and the click (Reddit r/mcp review 2026-11-06), so it is not proof
|
|
1357
|
+
if (req.physical === true) {
|
|
1358
|
+
return { ok: true, delivery: 'delivered_unverified', retry: 'unsafe', changed: ['gone'], surfaces: [], note: 'the element is gone after the click — that may not have been this click (it could have closed between the hit test and the click)' };
|
|
1359
|
+
}
|
|
1309
1360
|
// gone after the click: that is itself an observed change
|
|
1310
1361
|
const apps = runningApps().filter(a => a.pid === req.target.pid);
|
|
1311
1362
|
if (!apps.length) {
|
|
@@ -1318,7 +1369,18 @@ function opObserveAfter(req) {
|
|
|
1318
1369
|
try { after = observeState(r.el, r.appEl); }
|
|
1319
1370
|
catch (e) { return { ok: true, delivery: 'delivered_unverified', retry: 'unsafe', changed: [], surfaces: [], note: 'could not read the element after the action (INTERNAL)' }; }
|
|
1320
1371
|
}
|
|
1321
|
-
|
|
1372
|
+
const v = verdict(req.before || {}, after, {}, req.keys);
|
|
1373
|
+
// a physical click: if another window is now on top at the click point (one that covered the element,
|
|
1374
|
+
// or the element's window moved away, between the hit test and the click — or one this click opened),
|
|
1375
|
+
// the click cannot be tied to the element's change: not proof (Reddit r/mcp review 2026-11-06)
|
|
1376
|
+
if (req.physical === true && Array.isArray(req.point) && req.wid) {
|
|
1377
|
+
const top = cgTopAt(req.point[0], req.point[1]);
|
|
1378
|
+
if (!top || top.wid !== req.wid) {
|
|
1379
|
+
v.delivery = 'delivered_unverified';
|
|
1380
|
+
v.note = top ? `after the click, a window of "${top.owner}" is on top at ${req.point[0]},${req.point[1]}, not the element's — the click may have landed there (or opened it)` : `after the click, no window is on top at ${req.point[0]},${req.point[1]} — the element's window moved or closed`;
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
return v;
|
|
1322
1384
|
}
|
|
1323
1385
|
|
|
1324
1386
|
// Text into an element through accessibility: AXSelectedText inserts at the caret (replacing any
|
package/src/server.mjs
CHANGED
|
@@ -607,6 +607,7 @@ async function macAxState(args = {}) {
|
|
|
607
607
|
o.truncated = !res.complete;
|
|
608
608
|
if (res.web_accessibility) o.web_accessibility = res.web_accessibility;
|
|
609
609
|
if (res.web_accessibility === "unsupported") o.web_accessibility_hint = WEB_AX_HINT;
|
|
610
|
+
if (res.window_identity) { o.window_identity = res.window_identity; o.window_identity_hint = WINDOW_ID_HINT; }
|
|
610
611
|
o.elements = elements;
|
|
611
612
|
if (!res.complete) o.hint = "partial tree: raise depth or max_nodes, use interactive:true, or drill into one element with root";
|
|
612
613
|
return { text: JSON.stringify(o) };
|
|
@@ -614,6 +615,9 @@ async function macAxState(args = {}) {
|
|
|
614
615
|
|
|
615
616
|
// web_accessibility "unsupported": the app refused AXManualAccessibility and no web content was found in
|
|
616
617
|
// the window (Google Chrome does this); "exposed": it refused, but its pages are in the tree anyway.
|
|
618
|
+
// window_identity "unverifiable": the window has no CGWindowID (seen only on Finder's desktop, which is
|
|
619
|
+
// exempt), so element_index actions in it are refused with policy_denied.
|
|
620
|
+
const WINDOW_ID_HINT = "this window has no window ID, so a closed and reopened window with the same title could not be told apart from it: element_index actions here are refused (policy_denied). Use mac_menu_command, or mac_click x,y (checked against the window on top)";
|
|
617
621
|
const WEB_AX_HINT = "this window's web content is not exposed to accessibility (the app refuses AXManualAccessibility, as Google Chrome does). For web pages use the browser_* tools (a separate Playwright browser), or have the user restart Chrome with --force-renderer-accessibility and read again";
|
|
618
622
|
|
|
619
623
|
async function macFind(args = {}) {
|
|
@@ -628,6 +632,7 @@ async function macFind(args = {}) {
|
|
|
628
632
|
const o = { app: res.app, pid: res.pid, snapshot_id: snap.id, count: matches.length, complete: res.complete };
|
|
629
633
|
if (res.web_accessibility) o.web_accessibility = res.web_accessibility;
|
|
630
634
|
if (res.web_accessibility === "unsupported") o.web_accessibility_hint = WEB_AX_HINT;
|
|
635
|
+
if (res.window_identity) { o.window_identity = res.window_identity; o.window_identity_hint = WINDOW_ID_HINT; }
|
|
631
636
|
o.matches = matches;
|
|
632
637
|
return { text: JSON.stringify(o) };
|
|
633
638
|
}
|
|
@@ -698,14 +703,41 @@ const CLICK_VERB = { left: "c:", right: "rc:", middle: "mc:" };
|
|
|
698
703
|
// or above the main one) must be written "=-N"
|
|
699
704
|
const cc = (v) => { const n = Math.round(Number(v)); return n < 0 ? `=${n}` : String(n); };
|
|
700
705
|
const ccPoint = (x, y) => `${cc(x)},${cc(y)}`;
|
|
706
|
+
// The hit test runs in the engine a few hundred milliseconds before cliclick sends the click. Right before
|
|
707
|
+
// sending, the window server is asked again whose window is on top at each point: it must still be the
|
|
708
|
+
// element's window (by CGWindowID; by app when the element's window has none, e.g. Finder's desktop).
|
|
709
|
+
// Something that came on top in between refuses the input, nothing sent (Reddit r/mcp review 2026-11-06).
|
|
710
|
+
async function lateTopCheck(points) {
|
|
711
|
+
for (const { point: [x, y], pid, wid } of points) {
|
|
712
|
+
const r = await frontmost({ at: [x, y] });
|
|
713
|
+
if (!r || r.top === undefined) return { error: `focus_unknown: could not re-check the window on top at ${x},${y} right before the input — nothing was sent (delivery=not_delivered, retry=safe)` };
|
|
714
|
+
const top = r.top;
|
|
715
|
+
if (!top) { if (wid) return { error: `occluded: the element's window is no longer on screen at ${x},${y} (it moved or closed after the hit test) — nothing was sent (delivery=not_delivered, retry=safe)` }; continue; }
|
|
716
|
+
if (top.pid !== pid || (wid && top.wid !== wid)) return { error: `occluded: a window of "${top.owner}" came on top at ${x},${y} between the hit test and the input — nothing was sent (delivery=not_delivered, retry=safe)` };
|
|
717
|
+
}
|
|
718
|
+
return {};
|
|
719
|
+
}
|
|
720
|
+
// Test hook (never set in normal use): NORACU_TEST_PHYSICAL_GAP_MS=<1-10000> waits that long after the
|
|
721
|
+
// late re-check and before cliclick, and says so on stderr, so a device test can cover, move or close the
|
|
722
|
+
// target in the worst possible moment and check the result is never reported as verified.
|
|
723
|
+
const TEST_GAP_MS = (() => { const n = Number(process.env.NORACU_TEST_PHYSICAL_GAP_MS); return Number.isInteger(n) && n > 0 && n <= 10000 ? n : 0; })();
|
|
724
|
+
async function testGap(what) {
|
|
725
|
+
if (!TEST_GAP_MS) return;
|
|
726
|
+
process.stderr.write(`[noracu] test gap: ${what} in ${TEST_GAP_MS} ms\n`);
|
|
727
|
+
await sleep(TEST_GAP_MS);
|
|
728
|
+
}
|
|
729
|
+
|
|
701
730
|
async function physicalClickAt(target, label, button, count, timeoutMs) {
|
|
702
731
|
const prep = await axEngine({ op: "physical_prep", target, timeout_ms: timeoutMs });
|
|
703
732
|
if (!prep.ok) return { error: axErrorText(prep) };
|
|
704
733
|
const [x, y] = prep.point;
|
|
734
|
+
const late = await lateTopCheck([{ point: prep.point, pid: prep.pid, wid: prep.wid }]);
|
|
735
|
+
if (late.error) return { error: late.error };
|
|
736
|
+
await testGap(`physical click at ${x},${y}`);
|
|
705
737
|
const r = await cliclick(Array(count).fill(`${CLICK_VERB[button] || "c:"}${ccPoint(x, y)}`));
|
|
706
738
|
if (!r.ok) return { error: cliFail(r) };
|
|
707
739
|
await sleep(250);
|
|
708
|
-
const after = await axEngine({ op: "observe_after", target, before: prep.before });
|
|
740
|
+
const after = await axEngine({ op: "observe_after", target, before: prep.before, physical: true, point: prep.point, wid: prep.wid || null });
|
|
709
741
|
const res = after.ok ? after : { delivery: "delivered_unverified", retry: "unsafe" };
|
|
710
742
|
return { text: actText(`clicked ${button} x${count} on ${label} @ ${x},${y}`, { ...res, mechanism: "physical", hit: prep.hit, reason: prep.reason }) + await frontmostNote() };
|
|
711
743
|
}
|
|
@@ -783,6 +815,9 @@ async function macDrag(args) {
|
|
|
783
815
|
const prepTo = await axEngine({ op: "physical_prep", target: targetOf(t.item) });
|
|
784
816
|
if (!prepTo.ok) return { error: `drop target: ${axErrorText(prepTo)}` };
|
|
785
817
|
from = prep.point; to = prepTo.point; note = `from ${itemLabel(args.from_index, f.item)} to ${itemLabel(args.to_index, t.item)}`;
|
|
818
|
+
const late = await lateTopCheck([{ point: from, pid: prep.pid, wid: prep.wid }, { point: to, pid: prepTo.pid, wid: prepTo.wid }]);
|
|
819
|
+
if (late.error) return { error: late.error };
|
|
820
|
+
await testGap(`drag ${from} → ${to}`);
|
|
786
821
|
}
|
|
787
822
|
const r = await cliclick([`dd:${ccPoint(from[0], from[1])}`, `dm:${ccPoint(to[0], to[1])}`, `du:${ccPoint(to[0], to[1])}`]);
|
|
788
823
|
return r.ok ? { text: `dragged ${from} → ${to} (${note}; physical — check the drop target: delivery=delivered_unverified, retry=unsafe)` } : { error: cliFail(r) };
|
|
@@ -1972,8 +2007,8 @@ function inputSchemaFromArgs(args, required) {
|
|
|
1972
2007
|
const TOOLS = [
|
|
1973
2008
|
// Mac computer-use
|
|
1974
2009
|
{ name: "mac_screenshot", desc: "Capture the Mac screen as PNG base64: the main display, display (index from mac_windows), or one window by window_id (CGWindowID from mac_windows; works when covered), optionally cropped to region {x,y,w,h}. Returns {scale, logical_size, screen}; click coordinates are in points (pixels / scale).", args: { region: "object?", window_id: "integer?", display: "integer?" }, fn: macScreenshot },
|
|
1975
|
-
{ name: "mac_ax_state", desc: "Read an app's accessibility tree through the macOS Accessibility API (default: the frontmost app's focused window). Returns {app, snapshot_id, window, complete, elements:[{i, d, role, title, value, states, actions, frame}]}; i is the element_index for mac_click, mac_get, mac_set_value, mac_type, mac_scroll and mac_drag — unique across snapshots (an index never names an element of a later snapshot) and valid 30 s; an action first re-identifies the element and returns element_no_longer_valid if it is gone, moved or replaced. Options: interactive (only actionable elements), compact (default true: skip empty layout wrappers), skeleton (shallow map with children_count) then root (an element_index to drill into; adds new indexes to the same snapshot), surface (window|menubar|menu|sheet|alert|dialog|popover|focused|app), window (index or title) or window_id (from mac_windows), depth (logical depth, default 12, 1-40), max_nodes (default 400), web_accessibility (default true, false under NORACU_READ_ONLY=1: sets AXManualAccessibility on Electron/Chromium apps to expose web content, never AXEnhancedUserInterface which alters window behaviour; the result's web_accessibility is enabled / already_on, exposed when the app refused but its pages are in the tree anyway, or unsupported with a web_accessibility_hint — Google Chrome refuses it). ax_empty when the app has no readable window.", args: { app: "string?", interactive: "boolean?", compact: "boolean?", skeleton: "boolean?", root: "integer?", snapshot_id: "string?", surface: "string?", window: "string?", window_id: "integer?", depth: "number?", max_nodes: "integer?", web_accessibility: "boolean?" }, fn: macAxState },
|
|
1976
|
-
{ name: "mac_click", desc: "Click a UI element by element_index (from mac_ax_state / mac_find; valid 30 s) or at raw {x,y}. With element_index the element is re-identified first (element_no_longer_valid if gone or changed). mechanism auto (default): its own accessibility action (press / show menu / select — no pointer, no focus change, works in background windows) when it has one, else a physical click that is sent only if a hit test shows the point reaches the element (occluded otherwise); semantic / physical force one path. count 2-3 and button middle are physical. A raw {x,y} click is sent only when the window on top at that point belongs to app (default: the frontmost app; the menu bar counts as the frontmost app's) — otherwise occluded names what covers it and nothing is sent; force:true skips that check. Results state delivery (delivered_verified / delivered_unverified / not_delivered / unknown) and whether a retry is safe, plus newly opened sheets, dialogs or menus.", args: { element_index: "integer?", snapshot_id: "string?", x: "number?", y: "number?", app: "string?", force: "boolean?", button: "string?", count: "number?", mechanism: "string?", timeout_ms: "integer?" }, fn: macClick },
|
|
2010
|
+
{ name: "mac_ax_state", desc: "Read an app's accessibility tree through the macOS Accessibility API (default: the frontmost app's focused window). Returns {app, snapshot_id, window, complete, elements:[{i, d, role, title, value, states, actions, frame}]}; i is the element_index for mac_click, mac_get, mac_set_value, mac_type, mac_scroll and mac_drag — unique across snapshots (an index never names an element of a later snapshot) and valid 30 s; an action first re-identifies the element and returns element_no_longer_valid if it is gone, moved or replaced. Options: interactive (only actionable elements), compact (default true: skip empty layout wrappers), skeleton (shallow map with children_count) then root (an element_index to drill into; adds new indexes to the same snapshot), surface (window|menubar|menu|sheet|alert|dialog|popover|focused|app), window (index or title) or window_id (from mac_windows), depth (logical depth, default 12, 1-40), max_nodes (default 400), web_accessibility (default true, false under NORACU_READ_ONLY=1: sets AXManualAccessibility on Electron/Chromium apps to expose web content, never AXEnhancedUserInterface which alters window behaviour; the result's web_accessibility is enabled / already_on, exposed when the app refused but its pages are in the tree anyway, or unsupported with a web_accessibility_hint — Google Chrome refuses it). window_identity unverifiable marks a window without a window ID: element_index actions in it are refused. ax_empty when the app has no readable window.", args: { app: "string?", interactive: "boolean?", compact: "boolean?", skeleton: "boolean?", root: "integer?", snapshot_id: "string?", surface: "string?", window: "string?", window_id: "integer?", depth: "number?", max_nodes: "integer?", web_accessibility: "boolean?" }, fn: macAxState },
|
|
2011
|
+
{ name: "mac_click", desc: "Click a UI element by element_index (from mac_ax_state / mac_find; valid 30 s) or at raw {x,y}. With element_index the element is re-identified first (element_no_longer_valid if gone or changed). mechanism auto (default): its own accessibility action (press / show menu / select — no pointer, no focus change, works in background windows) when it has one, else a physical click that is sent only if a hit test shows the point reaches the element (occluded otherwise); semantic / physical force one path. count 2-3 and button middle are physical. A physical click is re-checked right before it is sent, and is delivered_verified only if the element is still there, changed, and its window is still on top at the point. A raw {x,y} click is sent only when the window on top at that point belongs to app (default: the frontmost app; the menu bar counts as the frontmost app's) — otherwise occluded names what covers it and nothing is sent; force:true skips that check. Results state delivery (delivered_verified / delivered_unverified / not_delivered / unknown) and whether a retry is safe, plus newly opened sheets, dialogs or menus.", args: { element_index: "integer?", snapshot_id: "string?", x: "number?", y: "number?", app: "string?", force: "boolean?", button: "string?", count: "number?", mechanism: "string?", timeout_ms: "integer?" }, fn: macClick },
|
|
1977
2012
|
{ name: "mac_drag", desc: "Drag from one UI element to another (sliders, selections, list items) with the physical pointer; from_index/to_index come from mac_ax_state or mac_find (valid 30 s). Both ends are re-identified and hit-tested first (either one gone or covered: nothing sent). Check the drop target afterwards.", args: { from_index: "integer", to_index: "integer", snapshot_id: "string?" }, required: ["from_index", "to_index"], fn: macDrag },
|
|
1978
2013
|
{ name: "mac_scroll", desc: "Scroll up/down/left/right by a number of pages (default 1). Without element_index: the frontmost window (the pointer moves there). With element_index: a scroll area is scrolled at its own center (hit-tested; reports when the position did not change), any other element is scrolled into view (AXScrollToVisible).", args: { direction: "string?", pages: "number?", element_index: "integer?", snapshot_id: "string?" }, fn: macScroll },
|
|
1979
2014
|
{ name: "mac_key", desc: "Press a key or modifier chord, e.g. 'return', 'esc', 'cmd-c', 'cmd-shift-v', 'arrow-up'. Named keys (return, esc, tab, space, delete, arrows, home/end, page keys, F1-F16), with or without modifiers, go out as HID key events with each modifier pressed and released around the key; chords on a character key (cmd-c) go through cliclick. Pass app to refuse input unless that app is frontmost (focus guard). Chords that quit, log out, lock, force-quit, delete immediately or empty the Trash (cmd-q, alt-cmd-q, cmd-shift-q, cmd-alt-esc, ctrl-cmd-q, cmd-alt-delete, cmd-shift-delete…) are refused unless force:true.", args: { key: "string", app: "string?", force: "boolean?" }, fn: macKey },
|