Skip to content

fix(browser): keep the pane's tabs when its window detaches - #64742

Open
endynamoo wants to merge 3 commits into
anomalyco:devfrom
endynamoo:browser-tabs-detach
Open

endynamoo wants to merge 3 commits into
anomalyco:devfrom
endynamoo:browser-tabs-detach

Conversation

@endynamoo

@endynamoo endynamoo commented Nov 18, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #64734

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Leaving a session deletes that session's Review-pane browser tabs, and a compaction does the same. The renderer detaches the session's binding, and the pane read that close as the user closing the browser: it deleted the tabs saved for that server and session, so the strip came back empty and nothing restored it, while the pages stayed alive in main.

One line: a closed binding keeps its saved tabs, and only a session that no longer exists deletes them — which main already does for browser.pane.missing. registered then restores the tabs from the saved inventory.

Nothing else changes. The close reason stays browser.pane.registration.closed, so ipc.ts still describes every close and the window keeps its retry with backoff. Kept deliberately: closing a session's shell tab keeps that session's browser tabs, so reopening the session brings them back; cleanup stays with main, which deletes them when the session is gone.

How did you verify your code works?

Reproduced on the 2.3.2 desktop build: three tabs, then a session switch — the strip went empty, browser.tabs.list still returned the same tab IDs, and the saved row was gone from the desktop's state table. Switching back re-registers the binding (previously with new page generations), which is the path this change fixes.

bun test in packages/gui-plugins passes (1142), bun run typecheck and bun run lint:changed are clean, and the test that encoded the old behaviour now asserts the opposite: a window's close keeps the saved tabs, a missing session forgets them.

Screenshots / recordings

No UI markup changed — the empty strip is the bug, and it keeps its tabs now.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

A window that navigated or detached its registration closed the binding with
`browser.pane.registration.closed` and, for `client.closed`, forgot the tabs
saved for that server and session. The renderer detaches on every session
switch, so leaving a session - or any compaction that remounts it - deleted the
session's saved tabs and left the pane's tab strip empty while the pages stayed
alive on the server.

Detaching or navigating is not the user closing the browser: it now suspends the
binding, which keeps the tabs, releases only the pages, and lets the strip keep
showing them. Only a session that no longer exists still forgets the saved tabs.
A window that registers with an empty inventory restores the tabs saved for its
session instead of letting the empty inventory win.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core fix looks right. Dropping client.closed from the forget condition stops a session switch or compaction from deleting the session's saved browser tabs. I couldn't verify it here: it needs the desktop app's built-in browser, which this sandbox can't run. With the PR, bun test src/browser in packages/gui-plugins passes and tsc -b is clean.

The public API is unchanged. PaneCloseReason keeps the same values, but client.closed and owner.closed now report browser.pane.suspended, which doesn't match how ipc.ts documents the two reasons.

Concerns:

  • Closing a session tab now also keeps its browser tabs. The window sends client.close whenever a session key leaves sessions.list(). That covers switching sessions and compaction, and also closing the shell tab. With this change, saved tabs are only deleted when the session no longer exists. Closing a session tab will keep its browser tabs (they come back when it reopens), and saved tab entries pile up over time. If that isn't intended, the window would need to tell main whether it is closing the tab or just moving to another session.
  • Two parts aren't needed for the fix. The new close reason and the createConnecting change don't affect this bug (details inline). Dropping them, and the test that covers the new reason, would keep the change to the one-line fix plus its test update.

if (reason) return close(state, reason, event.type === "client.closed" || reason === "browser.pane.missing")
// Only a session that no longer exists forgets its tabs. A window that navigated or detached its registration keeps
// them: they are saved per server and session, and the pane restores them when it registers again.
if (reason) return close(state, reason, reason === "browser.pane.missing")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix. But client.closed also arrives when the user closes the session's shell tab (model.ts session.removed → stop() → connection.close()), not only on a session switch. After this change, only a missing session clears the saved tabs: a closed session tab's browser tabs come back if it is reopened, and saved tab entries pile up. Is that intended? If not, the window would need to say whether it is closing the tab or just detaching.

if (event.type === "handshake.unsupported") return "browser.pane.unsupported"

if (event.type === "owner.closed" || event.type === "client.closed") return "browser.pane.registration.closed"
if (event.type === "owner.closed" || event.type === "client.closed") return "browser.pane.suspended"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this reason doesn't help the bug. connection.close() calls stop() (unsubscribes) before sending client.close, so the tabs sent with this event never reach the window. For owner.closed, the window has already navigated away or been destroyed. It also no longer matches ipc.ts, which documents suspended as idle eviction or the plugin going away, and window closes as registration.closed. I'd keep the old reason here.

}
// A window that just mounted registers with an empty inventory; the tabs saved for this server and session are what
// it restores from. A window that already shows tabs keeps its own inventory, which is newer than the saved one.
const restore = event.restore?.tabs.length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed for the fix. After session.removed the attachment is deleted, so it registers again with restore: undefined and the saved tabs are already used on the base. An attachment that sends an explicitly empty inventory had already saved that empty state through save-restore. I'd drop this change so the fix stays minimal.

A window that detached its binding - every session switch, and the remount a
compaction causes - closed it and the pane deleted the tabs saved for that
server and session. The strip came back empty and nothing restored it, while
the pages stayed alive in main.

A closed binding now keeps its saved tabs and releases only its pages; the tabs
return when the window registers again, and a window that registers with an
empty inventory restores them instead of letting the empty inventory win. The
close reason stays `browser.pane.registration.closed`, so the window still backs
off and retries, as `ipc.ts` documents.

Saved tabs are still cleaned up: the window marks the close `forget` when the
session itself is gone (`session.removed`), and main deletes the saved tabs
then, as it already did for `browser.pane.missing`.
@endynamoo

Copy link
Copy Markdown
Author

Thanks, both comments were right and the patch follows them.

  • The close reason is back to browser.pane.registration.closed for owner.closed/client.closed, so ipc.ts describes these closes accurately and the window keeps its retry-with-backoff path. Nothing claims they are idle evictions any more.
  • The row leak is closed rather than left open: close takes an optional forget, the window sets it from session.removed (the session's shell tab is gone), and main deletes the saved tabs only then — the handling browser.pane.missing already had.
  • What fixes the reported bug is what you named: the pane no longer forgets on a close, and createConnecting restores the saved tabs when a window registers with an empty inventory.

On confirming the path: yes, a session switch reaches it, through the re-register rather than session.removed. Measured on 2.3.2 — switching away disposes the session's drive, and switching back calls open() in model.ts, which closes the previous registration (that client.close is what removed the row) and registers again; the three tabs kept their IDs with new page generations, and the desktop's saved row was gone. A compaction boundary produces the same close and register. The removed-session case still forgets, as before.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't verify this: it needs the desktop app's built-in browser, which this sandbox can't run. With the PR, bun test src/browser in packages/gui-plugins passes and tsc -b is clean. The public API is unchanged: the new optional forget field on the browser pane's close input is internal to the built-in plugin and backwards compatible.

The close reason is back to browser.pane.registration.closed, so the docs in ipc.ts and the window's retry hold again. The createConnecting change is still there.

The new forget flag comes from session.removed, and that probably brings the bug back (inline). session.removed doesn't mean the session was deleted. It fires when a session key leaves sessions.list(), the list of sessions owned by open shell tabs, and switching sessions inside a shell tab drops the old session's key. On that path main deletes the saved tabs again, as before this PR. Main already deletes them itself when a session is gone (browser.pane.missing), so the window may not need to send forget at all.

sessions: Object.fromEntries(listed(state.sessions).filter((entry) => !sessionKeys.includes(entry[0]))),
},
effects: gone.flatMap(([key, attachment]) => [...stop(key, attachment.phase), detach(key)]),
effects: gone.flatMap(([key, attachment]) => [...stop(key, attachment.phase, true), detach(key)]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session.removed means the session's key left sessions.list() (sessions owned by open shell tabs), not that the session was deleted. host-apis.tsx builds those keys from each shell tab's sessionId/routeSessionId. Switching sessions inside a shell tab drops the old session's key, so this sends forget: true and main deletes the saved tabs: the bug from #64734 again. A deleted session is already handled by browser.pane.missing in main. Unless there is a signal that the session or its tab is really gone, I'd not forget here.

…loses

A window that closes its binding - every session switch, and the remount a
compaction causes - made the pane delete the tabs saved for that server and
session. A session switch therefore emptied the strip for good: the saved row
was gone, so the next registration had nothing to restore.

Only a session that no longer exists deletes its saved tabs now, as main already
does for `browser.pane.missing`. Everything else keeps them, so `registered`
restores them from the saved inventory.

The close reason stays `browser.pane.registration.closed`, so `ipc.ts` still
describes every close and the window keeps its retry with backoff.

Kept deliberately: closing a session's shell tab keeps that session's browser
tabs, and they come back if it is reopened. Row cleanup stays with main, which
deletes them when the session is gone.
@endynamoo

Copy link
Copy Markdown
Author

Right on all three, and the patch is now the minimal one.

  • forget is gone. You were right that session.removed doesn't mean the session was deleted: sessions.list() is built from the open shell tabs, and a tab contributes both tab.sessionId and tab.routeSessionId (runtime/server/runtime.tsx:253), so re-routing a tab drops the old key and would have sent forget: true mid-switch — the Review pane browser tabs are deleted when the session detaches (session switch, compaction) #64734 bug again. Cleanup stays with main's browser.pane.missing.
  • The createConnecting change is reverted too. As you said, the ?? base already restores from saved when the attachment registers without an inventory, and an explicitly empty inventory means the user emptied the pane, which save-restore had already recorded.
  • The close reason stays browser.pane.registration.closed.

So the diff is now one condition in transitionBinding plus its test. On the tradeoff you raised — closing a shell tab keeps that session's browser tabs — that is deliberate now: reopening the session restores them, and a deleted session is cleared by browser.pane.missing. If you'd rather close clear them immediately, it needs a signal that distinguishes closing the tab from switching sessions, which sessionKey leaving sessions.list() cannot do.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't verify this in the app: it needs the desktop app's built-in browser, which this sandbox can't run. Reading the code, it now removes only the deletion that caused the bug: a window closing its binding (on a session switch or a compaction) no longer deletes the session's saved browser tabs. With the PR, bun test src/browser in packages/gui-plugins passes and tsc -b is clean. The public API is unchanged.

The extra close reason, the createConnecting change and the forget flag are gone. The description says that keeping a closed session tab's browser tabs is deliberate.

One trade-off for maintainers to accept: main only deletes a session's saved tabs (browser.pane.missing) when that session is opened again and turns out to be gone. Rows for deleted sessions that are never reopened stay in storage.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review pane browser tabs are deleted when the session detaches (session switch, compaction)

1 participant