# Application target

## Shape

```
computer_use(type="window", app, title?, action, args={element | x,y, …})
```

Actions come in three groups:

| Item |  |
| --- | --- |
| Reading | `list_apps`, `list_windows`, `get_state` (`include` = all / tree / screenshot, `window` or `window_id`), `zoom` (a region of the last screenshot at full pixel density), `find` (text and role search over the last tree). |
| Acting | , Codex's target API: `click` (`mouse_button`, `click_count`, `modifiers`, `seconds` held), `type_text` (inserts; `clear` empties the field first, `submit` presses Return after; when that later key fails, the result counts the text as done, gives the error and reads the state), `press_key`, `set_value`, `scroll` (pages or pixels), `drag` (from `element` or x,y to `to_element` or `to_x`,`to_y`), `perform_secondary_action`, `select_text`, `paste`; and from the Claude app's `app_*` set: `copy`, `focus`, `menu`, `show_window`; and `open_file` (macOS). |
| Several steps | (`actions`): up to 20 acting steps on one app, in order, stopping at the first error; one settle and one state at the end. A final `get_state` step chooses what that state is: the tree alone, the full tree, or another window. Any other action among several steps (`find`, `zoom`, a list) answers `BAD_ARGUMENT:` naming the steps allowed there. |

## Results and the tree

Only `list_apps` needs no grant. A step in `actions` cannot name a different app, because the grant the call carries names only the call's app.

| Item |  |
| --- | --- |
| Acting result | Every acting call returns `Done: <summary>` followed by the Accessibility tree **diff** against the state the model last saw: `- line` for elements that vanished, `+ index line` for new or changed ones. |
| Whole tree | When more than half the lines (and at least twelve) changed, the whole tree is returned instead, because a diff of a rebuilt view is longer than the view. |
| Full tree | `get_state(full=true)` returns the full tree on demand. |
| Rendering | The tree is capped at 1500 nodes and rendered one element per line: index, role (`AXTextField` → `text field`, with the subrole in parentheses), label, value, placeholder, flags (`focused`, `selected`, `checked`, `disabled`, `secure`, `settable` …) and the element's secondary actions other than the primary press. |
| Claimed content | Content another target operates itself (a page of the user's Chrome whose tab this conversation's connection has attached; a window that does not report its address, as on Linux and Windows, stays whole) is left out of the tree with a line naming that target; the window's own controls stay (`registry.claim`, the one-route rule of [Machines and targets](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/machines-and-targets.html)). |
| Bidirectional marks | Bidirectional marks some apps wrap numbers in are dropped from the rendering and from `find`. |

The tree covers the window, the menus, popovers and dialogs beside it, and the menu bar with only open menus expanded. Closed menus are read by the `menu` action, so every read covers the same ground and a diff never shows a whole menu bar appearing or vanishing.

Results that changed nothing are marked: `menu` listings, `copy` and an already-visible `show_window` return a `Report`, and the tool returns it without settling or reading the state again.

## Stable indices

| Item |  |
| --- | --- |
| Basis | `tree.number` gives indices against the window's previous `Snapshot` (its index-free lines, their indices, and the next unused index). |
| Equal line | A line that is equal keeps its index. |
| Changed block | Within a block of changed lines, a line with the same depth, role and label as an earlier one is that element with a new value or state and keeps its index wherever the block moved it, so lines added above it do not take its place; what is left pairs in place when depth and role hold (a renamed button). |
| New line | Any other new line takes the next unused index, and indices freed by removed elements are never reused. |
| Guarantee | The model's earlier indices therefore never point at a different element after an insertion above them, which Codex's positional numbering does not guarantee. |

## Replaced elements

| Item |  |
| --- | --- |
| Cause | Apps, web pages above all, rebuild their accessibility elements while the window still shows the same controls, on their own or after another agent's step. |
| Dead element | The element the backend kept from the last read is then dead although its line is still there. |
| Detection | `_element` in each backend detects that, reads the same window again and matches it with `tree.rebind`: the fresh tree is numbered against what the model last saw, and the index is taken while its line reads the same, or keeps its depth, role and name (label or placeholder). |
| Step | The step then runs on the live element. |
| Still fails | An unnamed control that changed, a line that is gone and a window that closed answer `ELEMENT_GONE:` before any input, followed by the window's current full state, so the next step picks the element afresh. |
| Last view | The model's last view (`WindowState.snapshot`) is kept through the repair, so the state returned after the step reports everything that changed since that view. |

## One agent at a time

| Item |  |
| --- | --- |
| Scope | Agents of one conversation (the main turn and the agents it starts) each keep their own reading of a window, and the window is one. |
| Locks | `providers/window/provider.py` asks the locks module for the length of a call. On macOS every step reaches the application in the background, so a call holds this computer shared and its application alone. |
| Locks on Linux X11 and Windows | On Linux X11 and Windows the driver picks foreground delivery while a step runs, so an action that may be delivered with the real pointer or in the foreground holds the whole computer alone, an element step included: on X11 `click`, `scroll`, `press_key`, `type_text`, `hover` and `drag`; on Windows `click`, `drag`, `press_key`, `type_text`, `paste` and `focus`, whose UI Automation SetFocus can bring the window to the front. Two agents on two applications take turns for these steps. |
| Waiting | Another agent's call waits its turn, first come first served across processes (a `q-*` ticket file per waiting request keeps the order), and once its waits reach 60 s over the whole call answers `SCREEN_BUSY:` with nothing sent. |
| Identity | The application is the backend's identity for it where it has one (`app_key`, the bundle id on macOS, so a name, a bundle id and a path are one application), else the name as written. On macOS the name an application runs under (`Code` for Visual Studio Code) finds its bundle whether it runs or not, so `get_state` can launch it by that name. |
| Other agents | Each application records its acting calls with the agent that made them; a result starts with a note when another agent acted since this agent's last result, and the changes it lists include theirs. |
| No lock | `list_apps` and `list_windows` take no lock. |
| Code steps | Each code step takes its own locks. |

## Settling

After an action the tool polls a cheap fingerprint of the first 400 elements every 0.2 seconds, starting after 0.3 seconds, and stops when two fingerprints agree, after 1.5 seconds, or after 5 seconds while a busy indicator or an unfinished progress indicator is present — Codex's "about a second, up to five while loading".

## Menu bar

| Item |  |
| --- | --- |
| Path | `menu` walks `AXMenuBar` by title, case-insensitively and ignoring a trailing `…` or `...`. |
| Listing | A path that ends at a submenu lists its items with shortcut glyphs (`⌃⌥⇧⌘` from `AXMenuItemCmdModifiers`), `[checked]` and `[disabled]`; a leaf is pressed with `AXPress`. |
| Side effect | Reading the menu tree through Accessibility opens nothing on screen. |
| copy | `copy` presses the app's own menu item whose shortcut is ⌘C, found by shortcut rather than by its localized title, and falls back to posting ⌘C; when that item exists but is disabled (nothing selected, or focus on a control) it reports so at once. |
| Pasteboard | Otherwise it waits for the pasteboard change count, reads the string, and restores the previous pasteboard. |

| Item |  |
| --- | --- |
| No prior state | `copy`, `press_key`, `type_text` and `paste` without an `element` act on the app's current focus and need no earlier `get_state`; `menu`, `show_window` and `wait` never need one. |
| hover and wait | From Claude's computer-use tool come `hover` (three `mouseMoved` events posted to the process, approaching the point, then a 0.8-second rest; the real cursor stays put) and `wait` (up to 10 seconds, cancellable, followed by the usual settle and tree diff; with `text` it reads the tree every half second until the text shows, adding a sparse window's screenshot text when the tree lacks it, up to `seconds` or 10, and text that never shows is not an error). |
| focus | `focus` sets `AXFocused` and waits up to half a second for the app to report it, since apps apply focus on a later run-loop turn. |

| Item |  |
| --- | --- |
| Screenshot | The screenshot is the window's own image captured through ScreenCaptureKit, scaled so the long side is at most 1568 pixels; coordinates the model sends are pixels of that image. |
| Coordinates | A step maps them to screen points with the scale that produced the image (2 on a Retina display, less when a large window was captured smaller) and the window's origin as the window server reports it at that moment (`screen_point`, after `_follow_window`), so a window the user moved since the read is still hit where the model pointed. |

## Text on the screenshot

| Item |  |
| --- | --- |
| Sparse window | A window whose own walk yields fewer than 12 elements (frame, title bar buttons and little else) is drawn without Accessibility. |
| Recognition | For such a window every full or screenshot-only read also runs Vision's accurate text recognizer on the screenshot it just took and lists each line as `"text" at (cx, cy), WxH` in screenshot pixels, top to bottom and left to right, at most 150 lines. |
| Languages | The first recognition language picks Vision's model, so the languages are a hint (the user's preferred languages, then Chinese, Japanese, Korean and English) and language detection chooses per image. |
| find fallback | `find` with `text` falls back to these lines when no element matches; for a window that was not sparse it captures and reads the window once at the same scale, so the positions stay valid for actions. |
| Not in the tree | OCR text has no indices and is not part of the tree diff: it is evidence for coordinate actions, and a failure of the recognizer leaves the read without it. |
| Shapes | The same reads list icons and controls without text (`ocr.detect_shapes`): Vision's contour detector runs twice, once for dark marks on a light window and once for light on dark, at most 1024 pixels on the long side. |
| Merging and dropping | Top-level outlines within 3 points of each other merge into one shape, and a shape is dropped when it covers a recognized line of two or more characters, is under 8 points on its short side, over 80 points on its long side, more than 4:1, or touches the screenshot's edge (the window's rounded corners and border). |
| One character | A one-character line does not rule a shape out, because the recognizer often reads an icon as a glyph. |
| Order | Shapes the two passes both find are kept once, and the list is sorted in 16-point rows, left to right, at most 60. |
| No name | A shape carries no name: the model reads the screenshot to tell a gear from a trash can. |

## Audio

| Item |  |
| --- | --- |
| Codex | Codex records computer audio with `start_audio_recording` / `stop_audio_recording` (60 seconds by default, 300 at most) and hands the clip to the model as audio input. |
| Kept | OpenProgram keeps the two actions and their limits. |
| Clip | The clip becomes a WAV on disk plus a transcript from the Settings → Voice engine, which every model can read. |
| Content block | `ToolReturn.audio` also carries the WAV as an `AudioContent` block. |
| Audio input | When the model lists `audio` among its inputs, the Gemini converter passes it as inline audio and the Chat Completions converter as `input_audio`, each in a user turn after the run of tool results, which must stay together. |
| Chat Completions | That Chat Completions turn also carries the run's screenshots for vision models, since a `tool` message holds only text. |
| Other converters | The Responses and Anthropic converters skip audio blocks, and tool-result media is not written to the conversation history, so the clip costs nothing for models that cannot hear it. |
| Capture | `audio.py` opens an `SCStream` whose filter is the display with only the target application included, asks for 16 kHz mono float samples, excludes the OpenProgram process, and attaches only an audio output: the 2×2, one-frame-a-second video side is never consumed. |
| Limit | Samples past the limit are dropped and a timer stops the stream half a second after it, so a forgotten recording releases the capture. |
| Storage | One recording runs per conversation; it is converted to 16-bit PCM on stop and saved under the state directory, keeping the newest 20. |
| Approval | The action keeps the per-application approval: only the named app's sound is in the clip. |
| Silence | A clip whose peak stays under 1e-4 is reported silent and not transcribed. |
| No window | An application without a window has no `SCRunningApplication` and cannot be recorded. |

**Backend modules**

| Module | Role |
| --- | --- |
| `openprogram/programs/tools/computer_use/computer_use/desktop/common/base.py` | what every backend shares: `ComputerUseError`, `Report`, `Observation`, `AudioClip`, argument checks, recording storage |
| `openprogram/programs/tools/computer_use/computer_use/desktop/backends.py` | `support()` and `backend()` for the host: `macos` on Darwin, `linux` on Linux, `windows` on Windows |
| `openprogram/programs/tools/computer_use/computer_use/desktop/common/keys.py` | xdotool chord parsing to macOS key codes and modifier flags (`super+shift+z`, `ctrl++`, `KP_0`, lone modifiers) |
| `openprogram/programs/tools/computer_use/computer_use/desktop/common/tree.py` | `Node`, rendering, signatures, stable numbering (`Snapshot`, `number`), re-matching a replaced element (`rebind`) and the diff |
| `openprogram/programs/tools/computer_use/computer_use/desktop/common/audio.py` | ScreenCaptureKit audio capture of one application, sample limit, WAV encoding |
| `openprogram/programs/tools/computer_use/computer_use/desktop/common/ocr.py` | Vision text recognition (tesseract TSV on Linux), box conversion to screenshot pixels, reading order, language hints, contour shapes |
| `openprogram/programs/tools/computer_use/computer_use/desktop/macos/skylight.py` | ctypes bindings to SkyLight for background pointer and keyboard routing |
| `openprogram/programs/tools/computer_use/computer_use/desktop/macos/` | `MacComputer` in `computer.py`, composed of one module per concern (`frameworks.py`, `apps.py`, `app_windows.py`, `state.py`, `accessibility.py`, `pointer.py`, `keyboard.py`, `clipboard.py`, `menus.py`, `actions.py`): app resolution, window selection, capture, tree walk, every action; the screen driver and the event loops beside it |
| `openprogram/programs/tools/computer_use/computer_use/desktop/linux/` | `LinuxComputer` in `computer.py`, composed the same way over `x11.py`: the same interface over AT-SPI, xdotool, Gdk capture and PipeWire/PulseAudio |
| `openprogram/programs/tools/computer_use/computer_use/desktop/windows/` | `WindowsComputer` in `computer.py`, composed the same way over `win32.py`: the same interface over UI Automation (comtypes), window messages, `SendInput` and `PrintWindow` |
| `openprogram/programs/tools/computer_use/computer_use/desktop/windows/audio.py` | WASAPI process-loopback capture of one process tree |
| `openprogram/programs/tools/computer_use/computer_use/providers/window/` | the `desktop` kind and the window provider: the target note per platform, several steps, `clear` and `submit`, `wait` with text, locks, coded errors, result assembly |

## Implementation status

| State | Scope |
| --- | --- |
| Implemented | the `type="window"` target on macOS with the reading and acting actions above, stable indices, adaptive settling, background pointer and keyboard delivery, tree diffs, per-app approval with the four scopes, system-access gating, web presentation, unit tests for keys, tree numbering, menu matching, several steps, rules and approval wiring. |
| Implemented | OCR text for windows with a sparse tree and as the `find` fallback, with a test that reads a rendered image through Vision. |
| Implemented | per-application audio recording with WAV output and Voice-engine transcription. |
| Implemented | recordings as audio input for Gemini and Chat Completions audio models; tool-result screenshots on Chat Completions vision models. |
| Implemented | outlines of icons and controls without text in sparse windows. |
| Not implemented | audio input over the Responses and Anthropic APIs, whose models take no audio; moving a window from another Space, and typing into it with keys ([macOS delivery](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/macos.html)); naming an icon from its outline (the model reads the screenshot). |

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