# macOS delivery

If a previously observed control disappears and cannot be safely rebound, the tool reads a fresh full state and returns `observation_required`. The Agent chooses its next action from that state. The missing-control step and later batch steps are not dispatched; earlier completed steps are retained and never replayed. A refresh that itself fails remains an error.

## Background delivery

How this fits with everything else that can take the screen: [Working behind the user](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/working-behind-the-user.html).

| Item |  |
| --- | --- |
| Element actions | Element actions go through Accessibility: `AXPress` / `AXConfirm` / `AXPick`, then `AXSelected`, then focus, then a pointer click at the element's centre as the last resort; `AXShowMenu` for right clicks; `AXValue` for `set_value`; `AXSelectedTextRange` (UTF-16 offsets) for `select_text`; page scrolling actions, then the scrollbar's value, then wheel events for `scroll`. |
| Rows of a list | A click on a row, or on a cell up to three levels inside one, sets `AXSelected` on the row, which is what a click on it does. A press that returns an error counts when the row is selected afterwards. Measured on Outlook: a message cell lists `AXPress` and answers it with -25205, and its row takes `AXSelected` in 0.2 s with the reading pane following. No mouse-down is sent, so the application has nothing to activate itself on. |
| Pointer reach | A pointer click or hover on an element first checks that its centre lies inside the window cut by its scroll and web areas (`_reachable_center`); if not, it runs `AXScrollToVisible`, and where the element lacks it (native lists and tables) sets each enclosing scroll area's scroll bars to centre it, then reads the frame again once two reads agree. |
| Out of view | An element still out of view, or with no frame, is refused with `NotDispatched`. |

| Item |  |
| --- | --- |
| Real pointer events | Pointer events that have to be real (coordinate clicks, drags, wheel) are posted with the recipe the open-source cua-driver established: A focus-without-raise record posted to the target process serial number. A `mouseMoved` primer. The event, stamped with click state, button, subtype 3, target pid, window id and a click group. `CGEventSetWindowLocation` with the point relative to the window's top left (`window_location`: the screen point minus the window's origin, in points, as KWWK computer-use-core and cua-driver set it). `SLEventPostToPid` for delivery. |
| Window location | AppKit places the click in the window from the event's screen location; a probe window took a click at the same point whether `window_location` held the window-relative or the screen point. |
| Keyboard | Keyboard events carry an `SLSEventAuthenticationMessage` so the receiving process accepts them; without SkyLight the code falls back to `CGEventPostToPid`. |
| Result | The result is a click the application handles as if the user clicked, while the real cursor stays where it is and the front application does not change. |

AppKit still orders the clicked window to the front of its level when the application handles the mouse-down, so a coordinate click or drag first lists the other applications' windows stacked over the target and overlapping it (the front application's own windows excluded), and afterwards raises them again with `AXRaise`, deepest first, which puts the stack back as it was.

| Item |  |
| --- | --- |
| Choosing a window | A window is chosen by `window` (title substring or index) or `window_id`; without either, the window the model read last stays selected while it is open, then the focused or main window. |
| Following | Each state records the app's windows (window id, or title without one); when the focused or main window is one that was not there at the last read, the read follows it and says which window it opened over, so a dialog or document a step brought up is what the next steps act on. |
| Dialogs | A new window with a dialog subrole (`AXDialog`, `AXSystemDialog`) or a new file dialog is followed even when the focus stays on the window under it, as it does when a background app opens its out-of-process file dialog. |

| Item |  |
| --- | --- |
| Identifier | The system Open and Save dialogs carry `AXIdentifier` `open-panel` or `save-panel`, as a window, an `AXSheet` or a child window of the window under them. |
| Sandboxed app | A sandboxed app's dialog is drawn by `com.apple.appkit.xpc.openAndSavePanelService`, and the window server lists it twice with the same bounds: as the service's off-screen layer-0 window, which holds the Accessibility tree, and as the app's on-screen window that shows it (`panel_hosts`). |
| Same bounds | A window of the app with those bounds is a file dialog whatever its identifier, and the service's window reports the app window's id, so captures, `list_windows` and `window_id` all name the window on screen. |
| Listing | The app lists the dialog among its windows while the dialog's link to it holds; when it stops listing a dialog that is still on screen, the dialog's window is taken from the service's `AXWindows` by window id, so `list_windows`, `window=<title>` and the states keep reaching it. |
| What works | Its Accessibility elements are reachable and `AXPress` and `AXValue` work, SkyLight-routed clicks land, but key events posted to the app pid, to the service pid, or after a focus-without-raise never arrive, and setting the search field's `AXValue` does not run the search. |
| Keys refused | While a file dialog is the focused window or in it, stands as a window of its own, or is on screen without Accessibility reaching it, `press_key`, `type_text` and `paste` fail with that explanation and point at `open_file` instead of sending keys to the window under it; a sheet on another document window leaves keys to the focused one alone. |
| State of a dialog | A state of the dialog says it is the file dialog, points at `open_file` and always adds the OCR text, since the file browser exposes no rows; a state of another window of the app says a file dialog is open. |

`open_file` replaces the dialog:

1. A modal dialog holds back the request to open a document, so it first presses an open dialog's Cancel button (`AXCancelButton`, else a button titled Cancel) and waits for the dialog to close. It fails with what to click when Accessibility does not reach it.
2. `NSWorkspace openURLs:withApplicationAtURL:configuration:` with `activates` off opens the document in the app, launching it in the background if needed. The front app and its windows stay where they are.
3. It waits for a window whose `AXDocument` is the file's URL or whose title names it.

| Item |  |
| --- | --- |
| Refused paths | It refuses relative or missing paths, bundles and launchers (`.app`, `.command`, shell scripts, `.workflow`, installer packages, location files), files with an execute bit, and terminal apps, which run what they open. |
| Minimized | A minimized window or a hidden app still gives its tree; the screenshot is unavailable until `show_window` un-minimizes it (`AXMinimized`) or unhides the app, neither of which activates it. |
| Other Space | A window on another Space is read and operated where it is. |
| Lookup | `AXWindows` lists only the current Space's windows, so the app's off-screen ordinary windows (layer 0, at least 80×60) that no listed window covers are looked up by SkyLight's remote token: `_AXUIElementCreateWithRemoteToken` with the pid, `0`, `'coco'` and an element id, trying ids from zero (at most 4000) until each window id has its element, as AltTab does; the result is kept per app until that set of ids changes. |
| Delivery there | ScreenCaptureKit captures such a window and SkyLight's pointer route reaches it, but the window server drops key events for it whatever their route, so `type_text` and `paste` there set the focused element's `AXSelectedText` instead, `press_key` reports that keys do not arrive, and `show_window` says the window stays on its Space. |
| Moving | Moving it is not possible: since macOS 14.5 only the Dock may (`SLSMoveWindowsToManagedSpace` from any other process needs System Integrity Protection relaxed, as yabai documents). |

`paste` writes the pasteboard (plain text, or HTML rendered from Markdown with `markdown-it`), sends Cmd+V to the target process, and restores the previous pasteboard contents when the change count shows nothing else wrote in between.

## A busy application

| Item |  |
| --- | --- |
| Serial handling | A macOS app handles one Accessibility request at a time, in order, and answers when it is done. |
| Timeout error | A request whose handler outlasts the messaging timeout returns `kAXErrorCannotComplete` although the app received it once and is carrying it out: a button that saves for seconds, a command that runs a modal dialog, a menu that stays open. |
| Measured | Measured on macOS 15, a press handled within the timeout returns 0, a longer one returns the error after the timeout (1.5 s by default) and is delivered exactly once, and while the handler blocks the app every read fails the same way, `AXWindows` included, so the app reads as having no windows. |
| Timeout scope | A timeout set on an element applies to that element only. |

| Item |  |
| --- | --- |
| Sending | `MacComputer._taken` sends every request that changes the app (`_perform`, `_set`) with `ACTION_TIMEOUT` (1 s) set on the element. |
| Counted as taken | `kAXErrorCannotComplete` after that whole wait counts as taken, so the step is reported as made and nothing falls back to a pointer click or to typed keys, which would do it twice; the same code at once is a request that could not be made and stays a failure. |
| Probe | `_answers` asks the app element for its role with `ANSWER_TIMEOUT` (0.5 s) and is false only when that went unanswered. |
| Waiting | `_await_answer` repeats it for up to `UNANSWERED_MAX` (10 s) and then raises `NotAnswering`, a `NotDispatched`. |
| Order | Requests are answered in order, so the answer comes after the app has finished the step it was sent before, or while it runs the dialog or menu that step opened. |

| Item |  |
| --- | --- |
| Waits | `get_state`, `list_windows` and `act` wait that way before they ask the app anything, outside the backend's lock, and `settle` waits before it reads the window; `wait` never does, so the model can let the app finish. |
| Silent step | `providers/window/provider.py` returns a step whose app stayed silent as done, without state and with the instruction not to repeat it. |
| Refused step | A step refused because the app is silent sent nothing (`action_dispatched: false`), and among several steps the ones before it stay made without a second wait. |
| Other failure | Any other failure to read the state after a step that was sent (the app quit, the screen locked) is reported after the step's `Done`, never as a step that did not happen. |

## Refusals

| Item |  |
| --- | --- |
| Refused targets | The tool never types into `AXSecureTextField` elements or while macOS reports secure keyboard input (`kCGSSessionSecureInputPID`), and refuses to target Keychain Access, Passwords, loginwindow, SecurityAgent and OpenProgram's own bundles. |
| Refusal | The refusal is returned as an error that asks the user to do the step; no approval scope overrides it. |

| Item |  |
| --- | --- |
| Locked screen | A locked screen stops reads and steps too. macOS answers no Accessibility query about a window while the session is locked, so every application would read as having no windows. |
| Checks | `get_state`, `act`, `list_windows` and `zoom` check `CGSSessionScreenIsLocked` first (`_unlocked`) and fail as not dispatched with a message that the screen is locked and the windows are still open; `list_apps` and `find`, which ask nothing of a window, still answer. |

## Implementation status

| State | Scope |
| --- | --- |
| Implemented | windows on other Spaces, tested from Desktop 1 against Calculator on Desktop 2 (listing, screenshot, element presses computing 7×6, a coordinate click, a scroll, the key errors), with the pointer and the front app unchanged and the window left on its Space; inserting text there is not covered, since Calculator has no text field. |
| Implemented on macOS | following a newly opened window, file-dialog detection with the key refusal, and `open_file`, tested against Preview and PDF Expert (Open dialog followed, keys refused, Cancel pressed, a PDF opened and its text read) with the front app unchanged throughout, plus unit tests. |
| Implemented on macOS | following a dialog that leaves the focus where it was, telling the out-of-process dialog by the window list, reading it from the service process, refusing keys while it is open beside the focused window, and cancelling it from `open_file` have unit tests; of these only the window-list detection has run live, against PDF Expert's Open dialog. Not implemented on Linux and Windows. |

[Computer and browser use](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/computer-and-browser-use.html)
