# Moving tabs between windows

A desktop tab can move to another OpenProgram window, or to a new window at the release point. When the move ends the tab exists in exactly one window; any failure leaves the source as it was.

1. **Source** `prepare(payload)`
2. **Main process** validate, issue token: `prepared`
3. **Destination** `inspect(token)`, `accept(placement)`
4. **Main process** lock views, reparent: `destination-staged`
5. **Destination** insert tab, journal, `destination-ready`
6. **Main process** `awaiting-source`, then `remove-source`
7. **Source** remove tab, journal, `source-removed`
8. **Main process** persist decision, reveal window: `committed`

The destination inserts before the source removes, so a live tab always exists.

| Step | Behaviour |
| --- | --- |
| Token | Opaque, single use; expires after 15 seconds unless the move completes |
| Prepare | Requested synchronously when the drag leaves the strip |
| Inspect | Only the window under the pointer; not the source, not a second window |
| Reject | `duplicate` (the destination focuses its own copy) or `group-full`: the source is told, nothing changes. A payload the destination cannot validate is not accepted; its token expires. |
| Commit | `committed` is written to `tab-transfers.json` in the app data directory by atomic replace before success is reported. Uncertain durability: same decision rewritten, backoff 100 ms to 5 s. Write fails: roll back. |
| New window | Release outside every window creates a hidden torn-off window at that point. It claims the token, accepts like any destination, and fades in only once the tab is staged. Concurrent detaches share one window boot. |
| Empty source | Closes after its journal is finalized |
| Hover | The window under the pointer gets enter and leave events and shows the drop position only then |

## What moves

| Source kind | Contents |
| --- | --- |
| `tab` | One ungrouped tab |
| `segment` | One member of a compound group; the rest stay |
| `group` | A whole compound group with members, visible members and focused member |

| Aspect | Value |
| --- | --- |
| Tab kinds | Session, file, web, new-tab page, application, built-in page |
| Position | Group id, member index and order, visible members, focused member, canvas layout |
| Also carried | Unsaved file drafts; per chat: composer draft and settings, pending project, channel choice, whether active; each tab's page history |
| Limits | Payload 20 MiB; file drafts 3 per transfer, 2 MiB each, 6 MiB total; strings and nested values bounded. A malformed payload gets no token. |

## Web pages keep their state

A web tab is an Electron `WebContentsView`. The coordinator moves that same object between windows and never calls `loadURL`, so redirects, sign-in, form values, scroll position, page memory and the DevTools target survive. It stays hidden until the destination reports new bounds.

Before reparenting: clear the agent action cue and control overlay, record each view's bounds and visibility, check the source still owns every view. A view in an active transfer is locked against a second transfer or an agent command.

## Journals and recovery

1. Before changing its tab store, each renderer writes a journal to window-keyed local storage (`openprogram.tabTransferJournal:<windowId>`): before and after layout, session state, file drafts, native-view bookkeeping.
2. The main process records which roles opened a journal; the durable decision lists them as required.
3. After a reload a renderer asks for pending decisions, applies `committed` or `rolled-back`, and finalizes its role.
4. A window gone: another live window adopts the orphaned role.
5. All required roles finalized: the decision is deleted.

## Rollback

| Trigger | Result |
| --- | --- |
| Expiry, or destination or source failure | Roll back |
| Escape, or a drag ending without a target | Roll back |
| A participating window destroyed or closing | Roll back |
| Commit decision cannot be written | Roll back |
| Already committed | Never rolls back |

| Stage | Rollback does |
| --- | --- |
| No journal opened yet | Discard the token, tell the source |
| Journal opened | Write `rolled-back`; destination undoes its insertion (2-second limit); every view returns to the source with original bounds and visibility; a torn-off window made for this move closes and its state is discarded |
| Source window gone | The moved views are closed, never left ownerless |

Ordinary close rules do not apply: removing the source copy creates no close record, deletes no draft, and opens no fallback new tab before the destination acknowledges.

## Routing after a move

Menu commands and agent web-tab commands go to the focused window only. Native-view messages are accepted only from the renderer whose window owns the view.

**Code and related pages**

| Path | Role |
| --- | --- |
| `apps/desktop/main/web-views/transfers.js` | Main-process coordinator |
| `tab-transfer-validation.js`, `tab-transfer-store.js` | Desktop validation and durable decisions |
| `apps/web/lib/tabs/transfer/` | Renderer drag coordinator and journal |
| `apps/web/lib/tabs/store/transfers.ts` | Tab-store insertion and removal |

[Tab strip, groups and split layout](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/workspace/center-tabs-and-split-layout.html) in one window; [main and torn-off windows](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/desktop/window-lifecycle.html).
