# Working behind the user

While an agent operates an application, a browser or a page, the user goes on working in something else. Seven things of theirs stay as they are.

| Kept | Meaning |
| --- | --- |
| Front application | The application the user is in stays the one that takes their keys. |
| Window order | No window moves in front of, or behind, where the user left it. |
| Tab in view | The tab they are looking at, in their own browser and in the App, stays the one shown. |
| Pointer | The real pointer does not move. |
| Keyboard focus | What they type goes where they were typing. |
| Clipboard | What they copied is still there. |
| Space | No desktop switches, and no window changes Space. |

## Two layers

| Layer | What it covers |
| --- | --- |
| Delivery that takes nothing | Each target sends its input so that nothing is activated or raised: Accessibility and per-process events for applications ([macOS delivery](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/macos.html)), trusted input into hidden pages for the built-in browser ([Browser tab driver](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/browser/tab-driver.html)), background tabs in the user's Chrome or Edge. |
| Giving back what is taken | An application that starts, or opens a page, a document or a dialog, may activate itself, and no option of the sender stops it. Measured on macOS 15: Chrome comes in front of every application whenever a tab opens in front, whether the tab was opened from the command line, by `open -g` or by AppleScript. `openprogram/host/_platform/front.py` answers all of these in one place. |

## Giving the front back

The window server changes the front application first and tells the applications afterwards, with events. An application brings its windows forward and draws its menu bar, and the one before it dims its title bars, only when its own event arrives. The events are held back, so nothing of the change is drawn.

1. A step starts: `front.kept(operated)` records the front application and the window stack. One event tap serves every step that is running.
2. The event telling the user's application that it lost the front reaches the tap before the application.
3. An operated application is in front: inside the tap the front goes back to the user's application, and the event is dropped.
4. The events that follow are dropped too: the operated application's "gained" and "lost", and the user's "gained". Neither application learns of it.
5. For 0.5 s the window stack is read every 4 ms. An application that moved its own window forward anyway has the windows that stood over it raised again, deepest first.
6. The step returns at once; the watch goes on for 0.8 s, since an application may activate itself a moment later.

Measured on macOS 15 in three cases: Calculator starting in front, Calculator activated while running, and Chrome opening a tab in front.

| Events | Result |
| --- | --- |
| Dropped | The user's window on top in every reading; its title bar unchanged by one pixel; the front back within 2.8 ms of the event |
| Let through | Calculator's window on top; the title bar dimmed |

| Rule |  |
| --- | --- |
| Whose change it is | `operated(pid)` names the processes a step operates: the application a window call names (`app`, `front.named`), the user's Chrome or Edge, or the App for the built-in browser (`front.bundles`). Any other application that comes to the front is one the user switched to: it is left there and becomes the one the front is given back to. |
| User already there | When the user is in the operated application as the step starts, nothing is watched. |
| Bounded | A step that keeps taking the front is answered six times, then left. |
| Events held back | An active tap on the annotated session for event types 19 (lost the front), 20 (gained it) and 13 with subtype 1 or 2 (the pair an application gets while it starts). An event is dropped only after the front has really been given back, so no application is left believing something untrue. |
| Grant | The tap needs the Accessibility grant and is not asked for without it, since asking would put a prompt in front of the user. |
| Without the tap | The front is read every 50 ms, given back, and the windows raised again; the operated application's window is then on screen for 15 to 43 ms. With the tap, this reading acts only on a theft it sees twice. |
| Reading the front | `GetFrontProcess`, 0.6 ms a call. `NSWorkspace` keeps its answer current from the main run loop, which a worker thread does not turn. |
| Giving it back | `SetFrontProcessWithOptions` with the front-window-only option, 2 ms. `NSRunningApplication.activateWithOptions` called from a worker thread waits one second before it takes effect. |
| Window order | `AXRaise` on each window, found by its window-server id; the windows are looked up as the step starts, so that raising is one call. Windows that touch none of the operated application's are left alone. A 0.25 s messaging timeout is set so that a busy application is not waited for. Without the Accessibility grant the front application is still given back and the order is left. |
| Record | Each time is logged with the application's name, and kept in the block's `Watch.taken`. |

| Entry | Used by |
| --- | --- |
| `kept(operated)` | The window, built-in browser and user browser providers (`providers/window/provider.py`, `providers/browser/built_in.py`, `providers/browser/user.py`) around every call, so structured steps and code mode are both covered. |
| `watch_for(operated, seconds)` | The self-update switch, for the 30 s in which the desktop shell relaunches. |
| `front.py <bundle path> <seconds>` | `scripts/local-instances/install-dev-app.sh`, which runs the file by itself; it needs only the standard library there, and without the Accessibility grant it reads the front every 50 ms. |

## What can take the screen, and the answer

| Source | Answer |
| --- | --- |
| An application is started for a step | Started with `open -g`; one that activates itself anyway gives the front back. |
| A coordinate click orders the clicked window forward in its level | The windows stacked over it are raised again ([macOS delivery](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/macos.html)). |
| The user's browser opens the extension's connect page | The front is given back. The page stays open as a tab of that browser. |
| The extension focuses the browser's window once connected | The connect link carries the extension's own `background=true` when a token is set. |
| Playwright opens tabs in front and brings a selected tab forward | `playwright_background_tabs.cjs` opens them inactive and drops the call. |
| Connecting without a token | The user approves the connection on that page, so the browser stays in front for that one call. |
| An agent's Cmd/Ctrl+F, L, K or P in a built-in page | The App takes these as its own shortcuts only from a page that is on screen in the focused window and has the keyboard; otherwise the key stays in the page. |
| A page asks to confirm leaving | A page nobody is looking at leaves without the dialog. |
| A site asks for camera, location or notifications | From a page nobody is looking at: refused this time, not remembered, so the user is asked when they open it. |
| An agent's click in the user's own tab opens a popup | The popup opens beside the tab; the tab in view and the route stay. |
| The App is reinstalled or relaunched by an update | It reopens in front only when the user was in it as it closed. |
| An MCP server needs a sign-in when the worker connects it by itself | No browser opens; the link stays in `/api/mcp/auth/pending`, and reconnecting the server opens it. |

## What the user still sees

| Shown | Why |
| --- | --- |
| Agent cursor and window marker | Click-through overlays shown without activation, so the user can tell which window an agent holds ([Computer use interface](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/interface.html)). |
| Page preview in the conversation | Inside the App, for the conversation on screen; hidden once the user hides it. |
| Notifications | Only while no App window has the focus; the App comes forward when one is clicked. |
| What a command opens | A shell command an agent runs to show something (`open report.html`) opens it in front, as asked. |

## Linux and Windows

Neither system has a place where the events can be held, so the front is read, given back, and the windows put back in order.

|  | Windows | Linux, X11 |
| --- | --- | --- |
| Reading the front | `GetForegroundWindow` every 20 ms | `xprop -spy` reports each change of the active window and the stacking order |
| Giving it back | `SetForegroundWindow` from the foreground thread's input queue | `windowactivate` |
| Window order | `SetWindowPos` without activating | `windowraise`, which a window manager may decline |

| Rule |  |
| --- | --- |
| Whose change it is | Applications are told apart by process. A backend names the process it resolved (`front.note`); while it starts one (`front.starting`), a process that had no window as the step began is the new one. Browsers are told by executable (`front.programs`). |
| Forward on purpose | Input those systems give only to the front window brings the window forward for the step ([Linux and Windows backends](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/linux-windows.html)); the approval card says so. The guard stands aside (`front.forward`), and the backend puts back the front window, the pointer and the window's place in the order. |
| Wayland | An application cannot take the focus without a token from the compositor; nothing runs. |

Measured in a Debian container with Xvfb and Openbox: a running application that activated itself lost the front after 8.6 ms and the stacking order read the same afterwards; a window brought forward on purpose was left alone; a user's own switch was left alone.

## Implementation status

| State | Scope |
| --- | --- |
| Implemented on macOS | giving the front back unseen for application steps, the user's browser and the built-in browser, measured live against Calculator starting and activating and against Chrome opening a tab (front application, window order and the user's title bar); the App's reopening behind the user; the shortcut, leave-page, permission and popup rules of built-in pages; no browser for unattended MCP sign-ins. Unit tests cover the rules with a stand-in desktop. |
| Implemented on Linux (X11) | giving the front back and the window order, measured as above. |
| Implemented, not yet run on Windows | giving the front back and the window order. The macOS machines this is built on have no Windows guest; the rules have unit tests with a stand-in desktop. |
| Not implemented | on Windows: `paste` restores only text to the clipboard. |
