Skip to content

fix(native-chat): close only the reopened chat when main closes its tab - #22922

Merged
brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-close-reopened-chat-closes-current
Sep 27, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
brennanb2025/fix-close-reopened-chat-closes-current

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​232 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​225
Prod 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​56 $\color{#cf222e}{\Huge{\mathbf{−}}}$​20 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​36

ELI5

After /clear in a Claude chat, you can reopen the conversation from before the clear in its own tab from Agent Session History. Closing that reopened tab used to close your current chat too, and stop its Claude. Now it closes only the tab you closed.

What Changed

Before: close the reopened tab (with its × on the desktop, or from a paired phone) and both chat tabs disappear. The current chat's Claude is stopped, the workspace drops to the empty landing screen, and after a restart neither chat comes back (the persisted visible-chat list is empty).

After: only the reopened tab closes. The current chat stays open, keeps answering, and still comes back after a restart.

Mechanism. After /clear, the current chat keeps the window tab id that was derived from its first session (structured-agent-session-<S>). When that first session is reopened from history, the window has to give it a suffixed id (structured-agent-session-<S>:history-1) because the plain name is taken. When main closed a chat tab in the window, it rebuilt the window id from the session id (structuredAgentSessionTabId(S)), and that rebuilt id names the current chat, not the reopened one. The window closed the current chat, which retired its session on the host and emptied the persisted visible-chat list.

  • Main now sends its own session-tab id (agent-session:<S>) to the window, as it already did for browser, editor and every other tab kind (orca-runtime-close-mobile-session-tab.ts).
  • The window translates that id once, on arrival, through the host-to-window tab mapping it already keeps for mirrored chat tabs (hooks/ipc-events/host-session-tab-target.ts, reading tracking-mappings.ts). Both close handlers and the focus handler use the translated id for the pin check and the close or focus. An id with no mapping passes through unchanged, so browser and editor closes behave exactly as before.
  • The "this window already started this close" marker is keyed on the id actually sent to main (agent-session:<S>), because main now echoes that id back instead of a window id. The one existing caller that already used that form (suppressCancelledStructuredSessionTabs) now agrees with it, and the unused tabId argument is gone.
  • The same translation lets a host-directed focus of a chat tab (session.tabs.activate with navigation: 'host' or 'all') reach the reopened tab. Before, it found no window tab and did nothing.

Why

The window tab id cannot be derived from the session id: /clear keeps the old id on a tab showing a new session, and a reopened session gets a suffix. The only fact that names the right window tab is the correspondence the window records when it applies the host's tab snapshot, so this change addresses the window through that record instead of adding another guard beside a rebuilt name. Main's own tab id is also what every other tab kind already sends.

Deviations from the usual shape of this pattern, stated plainly:

  • The window and main use different ids for the same chat tab, so the window has to translate main's id through its mapping. There is no single shared id.
  • There is no incarnation or generation check on the tab id. A close names the tab, not a particular version of it.
  • An id the window has no mapping for closes nothing. The window answers "not found", and main already treats that as an already-completed close. Guessing a tab from the session id would bring the original bug back.

Alternatives considered: matching the window tab by the session id it shows (its entityId) would also have fixed the close, but it bakes the host id format into the window and bypasses the mapping that the rest of the mirror already uses. Keeping the derived id and adding a special case for suffixed tabs would leave the root cause (a derived name) in place.

Not changed, follow-up: clicking a chat's completion notification focuses by the tab id taken from the chat's pane key. That id is also derived from the session, so after /clear it can focus the current chat instead of a reopened one. It carries no host tab id, so this translation does not apply to it; it only affects focus and destroys nothing.

Remote / version skew: closeSessionTab and focusEditorTab are messages from main to its own window in the same app, so both sides always ship together. No RPC parameters, stream frames or published snapshots change, so phones and paired clients see exactly what they did before. Headless and remote-server hosts have no window and are unaffected.

Linked Issue

No filed issue; found while reviewing #22709 (no file overlap with it).

Visual Proof

Repro: open a Claude chat, send ZETA, /clear, send ETA, then Agent Session History → ZETA row → Resume in Worktree. Close the reopened tab.

Before (main): the reopened ZETA tab next to the current ETA chat

before: reopened tab

Before (main): after closing only the reopened tab, both chats are gone and the workspace is deselected

before: after desktop close

After: closing the reopened tab leaves the current ETA chat open

after: after desktop close

After: the current chat still answers (OMEGA) and survives an app restart

after: current chat still answers

after: after restart

Phone-style close through the runtime (session.tabs.close for the reopened tab's host id), before vs after:

before: after phone close

after: after phone close

Host-directed focus of the reopened tab (session.tabs.activate with navigation: 'host') while the current chat is active, before (stays on the current chat) vs after (shows the reopened MU conversation):

before: after focus

after: after focus

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Live Electron QA on macOS: a background dev build with an isolated profile per arm, driven over CDP. Each arm started from a fresh profile set up the same way (chat flags on, one scratch repo, relaunched). Main's bundle was checked in each arm to confirm which code was running.

  • Desktop close: on main, both tabs close, the host tab list goes to [], and the persisted visible-chat list goes to []. With the fix, only the reopened tab closes, the host keeps the current session, the persisted list keeps it, the current chat answers a new message, and it is restored after a restart.
  • Phone-style close through the runtime: on main, both tabs close. With the fix, only the reopened tab closes.
  • Host-directed focus: on main, it stays on the current chat. With the fix, the reopened tab comes to the front. First-party phone and web clients currently send navigation: 'caller' for tab activation, so today only a caller that explicitly asks for desktop navigation reaches this path.

New test tests/e2e/structured-session-reopened-tab-close.unit.test.ts runs the real main close/focus path (OrcaRuntimeService.closeMobileSessionTab / activateMobileSessionTab) against the real window store, snapshot mirror, IPC bridges and retirement code. Only the process boundary is stubbed. Its four cases: the mirror produces the suffixed tab; a desktop close; a phone close; a host-directed focus.

  • On main, three of the four cases fail: the desktop close retires [S, clear-X] on the host, the phone close removes the wrong tab, and the focus never reaches the reopened tab.
  • Taking out each part of the fix separately turns a case red: main's relay (desktop and phone), the window translation (phone), the marker key (the echo is answered "not found" instead of acknowledged), and the focus translation (focus).

Also run: pnpm tc, oxlint on the changed files, pnpm run check:code-quality:changed, and the related vitest suites (session-tab close routing, mobile session tabs, local structured-session mirror, tab slices, IPC event bridges).

Not caused by this PR: the src/renderer/src/hooks/useIpcEvents-close-routing-* tests fail on a clean origin/main (acf8e67), without this change, with Cannot find package '@/lib/parked-terminal-host-hydration'. The same tests passed on the earlier base 8009939.

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Security: no new surface. Cross-platform: no platform-specific code. SSH/remote: see "Remote / version skew" above. Mobile: phone closes of a reopened chat now close the right desktop tab; nothing a phone reads changes. Performance: one small map lookup per close or focus message.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

After /clear the current chat keeps the window tab id derived from its first
session, so reopening that first session from history lands in a suffixed tab.
Main closed chat tabs in the window by re-deriving that id from the session,
which named the current chat instead of the reopened one: closing the reopened
tab (on the desktop or from a phone) also closed the current chat and stopped
its Claude.

Main now sends its own tab id, as it already does for every other tab kind, and
the window translates it through the host-to-window tab mapping it keeps for
mirrored chat tabs. The same translation lets a host-directed focus reach the
reopened tab. The close handoff marker is keyed on the id actually sent.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review status: ready to merge (still in draft)

Verdict: no changes needed. One review pass came back clean, and the head is still ce7caacf8f. Independent readiness checks at the start and end of the review both passed. Live Electron QA passed against origin/main and the PR head. CI has 18 passing, 12 skipped by path, 0 failing and 0 pending. The PR is still a draft, so it needs to be marked ready before merge.

Does it fix the reported problem? Yes. Closing a chat reopened from Agent Session History, after /clear, now closes only that tab, whether it's closed on the desktop or from a paired phone. The current chat keeps running, keeps its place in the persisted visible-chat list, and comes back after a restart.

Is the fix architecturally sound? The bug came from main rebuilding the window's tab id from the session id. After /clear and a reopen, that rebuilt id names the current chat, not the reopened one. The PR removes the rebuild instead of adding a guard next to it. Main sends its own tab id. The window translates it once, through the host-to-window table it already fills whenever it applies main's tab snapshot. The "window started this close" marker is keyed on the same id that main echoes back. This is the standard shape when two processes hold different ids for one tab, and the PR body already states the two-id deviation. The review considered replacing the design with one shared tab id. That belongs to the chat-tab identity redesign in #22709, which shares no files with this PR and keeps main's tab id format, so the two don't conflict.

Regressions and performance: none found. Browser, editor and terminal ids aren't in the chat table, so they pass through unchanged. The pinned-tab guard now checks the tab actually being closed. The launch-cancellation path already sent main's id, and its marker now matches. Nothing sent to or from phones or paired clients changes. The added cost is one small in-memory lookup per close or focus message.

Checked and not a problem:

  • A close for a brand-new chat that arrives before the window has recorded its mapping. Main publishes a chat to the window over the same ordered channel it later uses to close it, and a phone can only close a chat after that same publication reaches it. The window records the mapping as it applies each snapshot, so this ordering could not be broken in practice. Guessing the tab from the session id would bring the original bug back.
  • The mapping is written inside the synchronous store update, so it commits together with the tab list.

Live Electron QA (background dev build, fresh profile per arm, verified which build was running)

Before, on origin/main: the reopened tab next to the current chat.

before: reopened tab

Before: after closing only the reopened tab, both chats are gone. The host tab list and the persisted visibleSessionIds are both [].

before: after desktop close

After, on the PR head: the same starting state.

after: reopened tab

After: only the reopened tab closed. The host and visibleSessionIds keep the current session.

after: after desktop close

After: the current chat still answers (OMEGA).

after: current chat still answers

After: the current chat is restored after an app restart.

after: after restart

Phone-style path (PR head): the current chat is active; a host-directed focus of the reopened tab brings it forward; a session.tabs.close for the reopened tab removes only that tab.

after: current active before phone

after: after phone focus

after: after phone close

Found during review, not caused by this PR

  • Clicking a chat's completion notification focuses a tab id taken from the chat's pane key, which is also built from the session id. After /clear it can focus the current chat instead of a reopened one. It only changes focus and destroys nothing. This is already listed as a follow-up in the PR body.
  • Closing a reopened tab clears the launch draft stored under the current chat's session-derived id (tabs-close-actions.ts). This line is unchanged on main, and the effect is minor because the draft is normally used up at launch.

@brennanb2025
brennanb2025 marked this pull request as ready for review September 25, 2026 21:59
@coderabbitai

coderabbitai Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 20 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 7c84e766-701e-4368-a9a4-9c70a47ab36b

📥 Commits

Reviewing files that changed from the base of the PR and between acf8e67 and ce7caac.

📒 Files selected for processing (10)
  • src/main/runtime/orca-runtime-close-mobile-session-tab.ts
  • src/main/runtime/orca-runtime-structured-session-restore.test.ts
  • src/renderer/src/hooks/ipc-events/host-session-tab-target.ts
  • src/renderer/src/hooks/ipc-events/session-tab-ipc-bridge.ts
  • src/renderer/src/hooks/ipc-events/terminal-ui-routing-ipc-bridge.ts
  • src/renderer/src/runtime/structured-agent-session-tab-retirement.test.ts
  • src/renderer/src/runtime/structured-agent-session-tab-retirement.ts
  • src/renderer/src/runtime/web-session-tabs-sync/tracking-mappings.ts
  • src/renderer/src/store/slices/tabs/tabs-close-actions.ts
  • tests/e2e/structured-session-reopened-tab-close.unit.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

  • Main relays its own session-tab id — orca-runtime-close-mobile-session-tab.ts:305 sends tab.id (agent-session:<sessionId>) for agent-session closes, matching browser/editor and every other tab kind.
  • Window translates once on arrival — new host-session-tab-target.ts plus the inverse lookup resolveLocalTabIdForHostSessionTab in tracking-mappings.ts map main's id to the mirrored local tab; both close handlers in session-tab-ipc-bridge.ts and the focus handler in terminal-ui-routing-ipc-bridge.ts:150 use it.
  • Retirement keyed on the host id — structured-agent-session-tab-retirement.ts:30 derives agent-session:<sessionId> for the marker and RPC, and the now-unused tabId argument is removed. This is load-bearing: main echoes the host id, and the reentrant phone-close path needs the marker to match that echo.
  • Tests — tests/e2e/structured-session-reopened-tab-close.unit.test.ts exercises the real main close/focus path against the real store, mirror, IPC bridges and retirement across four cases (suffix mirror, desktop close, phone close, host-directed focus).

I verified the new test passes on head, and that reverting main's relay to structuredAgentSessionTabId(tab.sessionId) fails the desktop and phone cases. pnpm tc, the changed-lines code-quality gate, and the surrounding suites (tab retirement, close-routing session tabs, close-request, structured-session restore, local structured-session sync, tab slices) all pass.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Live Electron QA on a second Mac (M4 Air): pass

The PR head ce7caacf8f was checked out in a fresh worktree on a separate M4 MacBook Air and run as a background dev build there, driven over CDP. Every arm started from a fresh profile set up the same way. The main-process bundle was checked in each arm to confirm which code was running. The origin/main arms ran at 6627c6503c.

Scenario origin/main PR head
Close the reopened chat tab on the desktop Both chat tabs close; host tab list and persisted visibleSessionIds become [] Only the reopened tab closes; host and visibleSessionIds keep the current session
Current chat after that close Gone Still answers a new message (OMEGA), and is restored after an app restart
Host-directed focus of the reopened tab (navigation: 'host') Stays on the current chat Brings the reopened tab forward
Phone-style session.tabs.close of the reopened tab Both tabs close; visibleSessionIds becomes [] Only the reopened tab closes

Desktop close

origin/main: the reopened tab next to the current chat.

main: reopened tab

origin/main: after closing only the reopened tab, both chats are gone.

main: after desktop close

PR head: the same starting state.

PR: reopened tab

PR head: only the reopened tab closed.

PR: after desktop close

PR head: the current chat still answers (OMEGA).

PR: current chat still answers

PR head: the current chat is restored after an app restart.

PR: after restart

Phone-style focus and close

origin/main: current chat active; after the host-directed focus it is still on the current chat; after the close, both tabs are gone.

main: before phone

main: after phone focus

main: after phone close

PR head: current chat active; the focus brings the reopened tab forward; the close removes only that tab.

PR: before phone

PR: after phone focus

PR: after phone close

@brennanb2025
brennanb2025 merged commit aeb96ab into main Sep 27, 2026
67 checks passed
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.

1 participant