# Browser tab driver

- `tabs.py` of the browser application drives the App's built-in tabs for `computer_use(type="browser")`.
- It is the only way a built-in tab is operated: the model, a Program's web tab commands and page resources all go through it.
- A call names a tab and an element; page context tokens, sessions, frames, leases and revision checks do not exist.

1. **`create_tab(url)`** App window connected?
2. **Yes: A tab in the App** the user can see it; has the App's sign-ins
3. **No: The driver's own browser** no window; none of the App's sign-ins

1. **Run ends** a scheduled run or a check-in Closes the tabs that run opened
2. **Ninth tab** in one conversation Closes its least recently used
3. **Archived** or deleted conversation Closes every tab it opened
4. **30 min idle** the driver's own browser The browser quits
5. **The user's own tabs** Never closed

## Tabs

| Item |  |
| --- | --- |
| Identity | A tab is `(window_id, tab_id)` as the renderer lists it (`surface_context.capture_pages_for_listing`, which carries each tab's CDP `target_id`). |
| Id | The driver names it `t` plus the first four base32 letters of the SHA-1 of `window_id` and `tab_id` (one letter more on a collision), so every process, the worker and a forked workflow subprocess alike, gives the same tab the same id in every turn, conversation and sub-agent, and listing again in any order does not rename. |
| Moved tab | A tab that moved to another window keeps its id: a listed tab whose `target_id` belongs to a known tab takes that tab over. |
| Changed target | A tab whose `target_id` changed (the App restarted) forgets its refs, document baselines and screenshots. |
| Binding | Each listing binds every page again, so the binding a tab had is released when a new one replaces it. |
| Default tab | An action without `tab` goes to the tab its agent used last (see below), else to the focused tab, else to the one visible tab. |
| create_tab, close_tab, screenshot | `create_tab` asks the renderer for a background tab (`surface_context.open_page`) and makes it the current one; `close_tab` and `get_state` with `include="screenshot"` go through the tab's binding (`request_close_tab`, `request_bound_screenshot`), and a binding that expired is renewed by listing again. |
| Ownership | A tab an agent opened is recorded in `browser_tabs_opened` (kept across restarts) with its conversation and, in `opened_in`, the execution that opened it. `list_tabs` lists the conversation's tabs and the current one; `list_tabs(all=true)` adds the rest. The guide tells the model to reuse an open page and close its tabs when done. Archiving or deleting a conversation closes every tab it opened (`close_conversation_tabs`). The user's own tabs are never closed. |
| User browsers | The user's own browsers (`app="chrome"`, `app="safari"`, `app="firefox"`, …; [User browsers](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/computer-use/user-browsers.html)) do not use this driver, but their tabs get ids of the same form (`openprogram/programs/tools/computer_use/computer_use/providers/browser/user.py`): in a Chromium browser the connection stamps each page with a key that names the id, and after the extension reconnects a new key takes over the id of a page gone from the same address. |
| Run's tabs | When a scheduled run or a check-in ends, the tabs that run opened close, however it ended: finished, failed, stopped or cut off (`close_run_tabs`, called when the execution settles and when the Worker hears a scheduled run's ending). These runs start clean and end with their answer, so nothing they opened serves the next one. Only they are named on a tab's record (`opened_in`). |
| Who waits for the closing | In the Worker the run's ending does not wait for it. A scheduled run's own process closes its tabs and waits before it exits, since a thread is cut off by the exit. A run whose process was killed is ended by the Worker as soon as it sees the process gone, and its tabs close then. |
| Limit | A conversation keeps at most `TABS_PER_CONVERSATION` (8) tabs its agents opened: once one more has opened, the least recently used closes (`_keep_within_limit`) and the result's `note` says so. The new tab and a tab in front of the user stay; only tabs the turn can list count; failing to tidy never fails the open. This bounds whatever opens pages over and over, such as a monitor or a Goal running for days. |

## Connection

| Item |  |
| --- | --- |
| Owner thread | Playwright's sync API belongs to one thread, so one owner thread holds the connection to the App (`attach_to_app` on `desktop_app_ws_url`) and runs every page call; callers wait on it with their context variables. |
| Leaves the App as it is | Playwright treats adopted pages as a test browser: each one, the App's own window included, is told the system is light and the page focused, and downloads move to Playwright's folder. `attach_to_app` connects with `no_defaults`, so none of that reaches the App. It is the only caller of `connect_over_cdp`; a contract test keeps it so. |
| Focus | A background page fires no focus events, so the driver turns focus emulation on for each tab it operates, and for no other page. |
| Finding a page | A page is found by matching `Target.getTargetInfo` against the tab's `target_id`. |
| Reconnect | A connection does not always learn of tabs the App opens after it, so a target that is not found makes the driver connect again and look once more; a dropped connection reconnects on the next call. |
| Recording | When a page listener is first attached, the driver records its console, page errors and requests (the last 300 of each), and counts its document navigations and in-flight fetch/XHR requests. |
| No App window | With no App window to open a tab in (`open_page` answers `desktop_unavailable`: the App is closed, or the process cannot reach it), `create_tab` starts a browser of the driver's own with no window (`host/resources/background_browser.py`: the Chromium the runtime ships, else the installed Chrome or Edge as a program, with a fresh profile). |
| Own browser's tabs | Window id `background`; the same ids, actions, locks and ownership records; no preview and none of the App's sign-ins, which the result of `create_tab` says. They live in the process that opened them, on the same Playwright as the App connection: the browser closes with its last tab, after `OWN_IDLE_SECONDS` (30 min) without use, or with the process. A listing does not wait for an App that is not running, and reports one that is running and does not answer. |

## Several agents

| Item |  |
| --- | --- |
| No lease | Agents of one conversation share its session id, and sub-agents run in parallel, so no tab is leased to one agent for longer than one action or one list of actions: a lease held across turns would make a conversation refuse its own turns. |
| Lock | Each `Tab` has a lock. A call that names its tab also holds that tab's lock file (`openprogram/programs/tools/computer_use/computer_use/routing/locks.py`), which holds across the worker's processes. It holds nothing of the machine: pages are driven in the background, so `app` and `screen` calls and browser calls do not wait for each other. |
| Lock span | An action takes it before its first page call and keeps it through settling and its result, so another agent's input never lands between an action and the state it reports. |
| Lists | A list of actions (several steps of one call to the browser provider, `openprogram/programs/tools/computer_use/computer_use/providers/browser/built_in.py`, inside `tabs.batch()`) keeps every lock it took until the list ends, so a form filled in one list is not interleaved. |
| Waiting | Waiting for a lock polls with cancellation checks, and after `TAB_WAIT_SECONDS` (60 s) the action fails with `tab_busy`, which the model reads as `SCREEN_BUSY`; two lists that each wait for the other's tab end that way rather than deadlock. |
| Agent | The agent a call acts for is `<session>/<execution>` (the `_VIEWER` context variable, carried into the owner thread). |
| Current tab | Each agent remembers its own current tab; an agent without one starts from the tab its conversation used last. When an agent ends, `tabs.forget` drops its current tab and its screenshots on every tab, and starts no driver to do so. |
| Snapshots | Snapshots are tracked per agent (`track` = `openprogram:<agent>`, and each agent's document baseline), so an agent's changes cover everything since it last looked, including what other agents did. |
| Write log | Each tab records its write actions with the agent that made them; a result adds a `note` when another agent acted on the tab since this agent's last result. |
| Owner thread | Only single page calls run on the owner thread. |
| Polling | `wait` and the settling after an action poll on the caller's thread with short owner calls (`_shows`, `_activity`, `_loaded`), so one agent's wait never holds up another agent's click on another tab. |

## Writes are sent once

| Item |  |
| --- | --- |
| Retry | Only `tab_gone`, which `_page` raises before any input, makes the driver list the tabs again and try the action once more. |
| Failed action | An action that failed after Playwright may have sent its input counts as a write, and its error says it may have taken effect when it timed out, with the page as it is; a tab that closed during the action is `tab_closed`. |
| After success | Once an action succeeded, any failure while settling or reading the page comes back as a successful result with a `note`, never as an error the caller would retry. |
| close_tab | `close_tab` treats the renderer's response timeout as `close_uncertain`, an expired binding as `tab_gone` (nothing was sent), and other failures as `close_failed`. |
| evaluate | `evaluate` counts as a write. |

## Tabs an action opens

The connection discovers targets (`Target.setDiscoverTargets`) and counts each new page target. When the count changed during an action, the driver lists the tabs and the result's `opened` names the new ones whose `opener_tab_id` is the acted-on tab or unknown. The agent's current tab stays where it was.

## Coordinates

| Item |  |
| --- | --- |
| Record | A screenshot records, per agent, the CSS-to-image ratio, the tab's last write, its URL, its scroll offset and the image. |
| Click | A click by `x`,`y` needs that agent's screenshot, and fails with `screenshot_stale` when the tab navigated or scrolled since. |
| Point check | When an agent wrote to the tab since, the driver captures the tab again and compares the 64 CSS px square around the point in both images (decoded with Core Graphics on macOS and by the driver's own PNG reader on Windows and Linux; up to 6 % of its pixels may differ by more than 32 per channel, for a caret or anti-aliasing). |
| Decision | The click goes ahead while the point still shows what the screenshot showed, so typing elsewhere or a key that did nothing costs no new screenshot. It is refused when it does not, or when the pixels cannot be compared. |
| Refusal | A refusal carries a new screenshot, recorded as the agent's, so the next `x`,`y` need no extra call. |

## Preview follow

| Item |  |
| --- | --- |
| Follow rule | The conversation's page preview follows the operations the driver reports: the frontend follows only a live `browser.resource` event whose `last_operation` is new and `dispatched` or `acknowledged`, never a snapshot. |
| Write action | Every write action (anything but `get_state`, `find`, `read_console_messages`, `read_network_requests`, `wait`, `list_tabs`, `create_tab` and `close_tab`) first retains the tab's resource row for the current execution (`publish_bound_page`), then reports `dispatched` with `follow` (`pages.report_browser_operation`), which stores the operation and sets the resource's control state to `active` before the input is sent. |
| After the action | After the action it reports `acknowledged`, or `failed` with the reason code, under the same operation id; a failed one does not set the control state again. |
| Operated | The page shows as operated only while the execution that set it is running: the resource projection turns `active` back into `idle` once that execution has ended. |
| Marker point | A click's outcome carries the marker point: before clicking, `_aim` scrolls the element into view and records the centre of its bounding box, or the CSS point a click by `x`,`y` maps to, with the viewport's `innerWidth` and `innerHeight`; `sanitize_operation` turns that into `point` with the viewport size as `width` and `height`, which `browser-marker-geometry` maps onto the screenshot. |
| Reads | Reads (`get_state`, `find`, `read_console_messages`, `read_network_requests`, `wait`) report an `acknowledged` operation without `follow`, so the preview follows them and the page is not marked operated, and `create_tab` reports the same for the new tab after publishing it, which makes it the tab followed. |
| List steps | Each step of a list reports on its own. |
| Reporting | Reporting never fails an action: errors are logged at debug level. |

## Workflow subprocesses

| Item |  |
| --- | --- |
| Driver | A forked child drops the parent's driver (`os.register_at_fork`), because its owner thread and connection belong to the parent, and starts its own. |
| Renderer calls | Its renderer calls (`capture_pages_for_listing`, `open_page`, `request_close_tab`, `request_bound_screenshot`) go through the parent's webtab bridge, which confines them to the originating window, authorizes only bindings it handed out and closes the tabs the child opened when the child ends. |
| Operation reports | The child holds no App connection, so its operation reports go to the worker as well: `pages.forward_operations_to_parent` hands each one to the child's event queue as `__op_browser_operation__`, and the worker reports it with `report_forwarded_operation`, which keeps only the operation's own fields and falls back to the execution it launched. |
| Locks | The `Tab` locks and the write log are per process, so a child and the worker on one tab interleave at single page calls. |

**Access and cancellation.** When the turn's surface has agent access disabled (`preview_status` `disabled`), every built-in browser action is refused with `page_access_disabled`. Waiting for a tab, `wait`, settling and each step of a list check for cancellation.

## Implementation status

| State | Scope |
| --- | --- |
| Implemented | the tab driver for `type="browser"`, with unit tests for choosing tabs, ref recovery, both snapshot protocols and the result shape, and two end-to-end runs in the App in which an agent filled and submitted a local two-page form (text, email, radio, checkboxes, select, textarea, a confirmation box) through background tabs. |
| Not built | A screenshot kept per run: what a run saw is the tool results on the run's own branch of the conversation. |
| Not built | A profile the driver's own browser keeps between starts: a page that needs a sign-in needs the App open. |

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