feat(desktop): undo closing a chat tab, and fork instead of switching provider mid-chat

Merged
HA
2mo ago
+976-149

Summary

  • Closing a chat tab now shows a toast for 10 seconds with Undo (also ⌘Z / Ctrl+Z). Undo brings the tab back with its live agent session, like Gmail's undo-send.

  • Chats closed while a toast is showing merge into one "N chats closed" toast, so Close All gives a single undo.

  • Picking another provider's model (e.g. Claude → OpenAI) in a chat that already has messages no longer swaps the agent in place. A dialog says the chat can't switch provider partway through and offers Fork to new chat. That opens a new tab on the chosen agent, seeded with the conversation, and leaves the original chat open and running.

  • When the agent is waiting on a question or approval and you send a message from the chat box instead, the pending request is cancelled and your message goes out next. Before, it waited in the queue behind the open question.

  • Fix: the free-text "Something else?" answer in the agent question form dropped a space typed at the end, so you couldn't type multi-word answers. The input rendered a trimmed value. It now shows the raw text and trims answers on submit.

Why / Context

Before this change, closing a chat pane called stopChat immediately, so the agent session was gone for good. A misclick on the tab × or Close All lost the whole conversation.

How It Works

  • The chat-v3 registry entry's onAfterClose now calls useUndoableChatClose (usePaneRegistry/hooks/useUndoableChatClose) instead of stopping the session.
  • The hook marks the session closed right away (markChatSessionClosed) so useAutoAdoptChatSessions doesn't re-adopt it during the window. It then shows ChatClosedToast via toast.custom.
  • The session stop runs from sonner's onAutoClose / onDismiss, not from a separate timer. Sonner pauses on hover, so with a separate timer the session could stop while Undo was still offered.
  • Each batch is settled once (a settled guard). Undo unmarks the sessions and re-adds each pane (same id and data) as a tab. A new close while a toast is showing dismisses the old toast as already settled, and opens a new toast with all the chats.
  • Leaving the workspace (hook unmount) dismisses the pending toast, which stops its sessions. Without this, Undo would restore into an unmounted store.
  • No toast when sessionId is null (chat never started) or when the session is already marked closed. The second case covers switching a chat to the terminal view, which stops the chat itself before replacing the pane.
  • ⌘Z / Ctrl+Z is one shared UNDO hotkey backed by a small undo stack (stores/undo-stack.ts), registered once by UndoMount. The archive-workspace toast and the chat toast push entries while they're showing, and one press undoes only the newest. This replaces UNDO_ARCHIVE_WORKSPACE; a saved override for that id moves to UNDO (overrides store version: 1 migration).
    • It ignores text fields, so text undo in the composer and editors still works.
    • Exception: xterm's textarea when ⌘ is held. A terminal that takes focus after a close doesn't swallow ⌘Z, and Ctrl+Z still reaches the shell on Windows and Linux.

Review follow-ups

  • Undo puts a chat that was alone in its tab back at the same index, with its tab id and title. It reads the tab list as it was just before the close, then calls insertTab. Close All restores newest first, so the indices line up.
  • A stop that fails after the toast expires shows "Couldn't stop the chat", instead of the chat silently coming back through auto-adopt.
  • The stop/undo/merge rules live in createClosedChatBatches (useUndoableChatClose/utils/closedChatBatches). It's a plain function with the toast, store and session actions passed in, and it has one test per rule.
  • The countdown bar pauses on the same signals as sonner's timer: pointer over the toast stack, pointer pressed, or window hidden. The Toaster uses expand, so data-expanded is always true and can't be used for this.
  • A stop request can't be sent while the page unloads, so a chat closed right before a quit or reload kept running and came back on the next launch. Pending closes are now kept in localStorage (pending-chat-stops-v1, registered in the persisted-key registry). On load those sessions start out marked closed, and the workspace stops them when it mounts.
    • Bound: one entry per pending chat, cleared when its toast settles or when its workspace next mounts; entries older than a day are ignored.

Cross-provider fork

  • AcpChatPane's agent-switch handler builds the same target as before (preset, model, mode, handoff prompt from the transcript).
  • If there's a transcript and the pane passes onForkAgent, it shows an alert confirm instead of closing the session. On confirm, ChatPane adds a new chat-v3 tab with that target as pendingPrompt (same data shape onSwitchAgent uses).
  • An empty chat (no transcript) keeps the existing in-place switch.

Manual QA Checklist

Driven over CDP against the dev app (real clicks and key events), signed in as the local dev admin, with no-project chat sessions:

  • Close one chat tab → "Chat closed" toast with the chat title, Undo ⌘Z and a countdown bar
  • Undo → tab returns with its transcript, and the restored session answers a new prompt
  • Right-click a tab → Close All with 2 live chats → one "2 chats closed" toast; Undo restores both
  • Closing two tabs one after the other merges into one "2 chats closed" toast
  • ⌘Z with focus on the page body restores both chats and dismisses the toast; a restored chat still answers
  • Hover the toast for 13s → toast stays and the claude process stays alive; on mouse leave it closes and the process exits
  • Let the toast expire → tab stays closed (checked for 30s more, no auto-adopt); the agent process exits only after the window (alive at 5s, gone at 14s)
  • Close a chat, then go to New Workspace while the toast is up → toast dismissed, session stopped
  • Claude chat with one exchange → model picker → OpenAI tab → GPT-6.1 Sol → "Continue in a new Codex chat?" dialog; the Claude chat is unchanged behind it
  • Fork to new chat → new Codex tab opens with the handoff prompt containing the conversation; the Claude tab stays open
  • Cancel → no new tab; the Claude chat keeps its conversation and model
  • Empty Claude chat → pick a Codex model → switches in place, no dialog
  • Question form → "Something else?" → a space typed at the end stays (" " → " x"); before the fix it disappeared at once
  • Countdown bar: runs with the pointer away, freezes while the toast is hovered, resumes on leave
  • Close a chat, then reload at once → the entry is saved, the agent process stops after the reload (2 → 1), the list is cleared, and the chat isn't re-adopted (checked 20s later)
  • Close then ⌘Z after the refactor → chat restored with its conversation, saved list cleared
  • Agent shows a question form → type a message in the chat box and press Enter → form cancelled, message sent without queuing, agent answers it
  • Close the 4th of 5 tabs → ⌘Z → it returns as the 4th tab, active, with its conversation
  • Close a chat whose neighbour is a terminal → focus lands in xterm → ⌘Z still restores it (before the fix it did nothing)
  • Close a chat, then type in another chat's composer → ⌘Z undoes the text and leaves the toast up
  • No console errors during any of the above

Testing

  • bun run typecheck (apps/desktop): no errors in changed files. The worktree has unrelated pre-existing auth-type errors from unbuilt packages.
  • bunx biome check on changed paths: clean
  • bun test src/renderer/hotkeys (apps/desktop): 155 pass
  • bun test for useUndoableChatClose and persisted-keys: pass
  • bun run check:i18n: clean; new strings translated for all 16 locales

Known Limitations

  • Not verified in the app: the failed-stop toast (I couldn't make a stop fail on demand) and archive-undo through the shared hotkey (no cloud workspace locally). Both are covered by typecheck and the undo-stack and migration unit tests.
  • Not from this PR, but visible while testing: the new Codex chat stores the chosen model (chatModelId: gpt-6.1-sol) but runs on Codex's default (5.4). An in-place switch behaves the same, so the model-apply step for Codex needs a separate look. This may come from the local codex-cli 0.160.0.
  • Undo restores a chat that shared its tab (a split), or one from the right pane area, as a new center tab. Exact restore there needs the closing store and layout in onAfterClose (a packages/panes change).

Summary by cubic

Closing a chat tab no longer stops its agent session right away. A toast offers Undo (⌘Z/Ctrl+Z) for 10 seconds; the stop runs when the toast closes or is dismissed. Chats closed during the window merge into one "N chats closed" toast, so Close All gives a single undo.

Switching a chat that already has messages to another agent's model now asks first and, on confirm, opens a new tab seeded with the conversation, leaving the original open. Empty chats still switch in place. Also fixes the "Something else?" answer input dropping trailing spaces; it now shows raw text and trims on submit.

When the agent waits on a question or approval and you send a message instead, the pending request is cancelled and your message goes out next.

Undo

  • UNDO_ARCHIVE_WORKSPACE and REOPEN_CLOSED_CHATS are now one shared UNDO hotkey backed by an undo stack, so one press undoes only the newest action; saved override bindings migrate.
  • A chat that was alone in its tab returns at the same index with its tab id and title; Close All restores newest first. Split and side-pane chats come back as center tabs.
  • Sessions are marked closed at once so auto-adopt doesn't reclaim them; leaving the workspace stops them directly. ⌘Z still works when a terminal has focus.
  • Quits and reloads during the window save a per-session stop to localStorage (pending-chat-stops-v1); the record survives a quit while the stop is in flight so the next mount retries it, and is removed once the stop finishes. Entries older than a day are ignored, and a failed stop shows "Couldn't stop the chat".
  • The countdown pauses on hover, pointer press, or when the window is hidden, matching sonner's timer.

Summary by CodeRabbit

  • New Features
    • Closing chats now shows a countdown notification with an Undo option. Reopen recently closed chats before they’re stopped, including multiple chats from one notification.
    • The countdown pauses while you interact with the notification or the app is hidden.
    • Use ⌘Z on macOS or Ctrl+Z on Windows and Linux to undo the latest chat closure or workspace archive.
    • When switching providers, fork a conversation into a new chat while keeping the current chat open.
  • Improvements
    • Whitespace is trimmed from text answers before submission. Sending a prompt also cancels active approvals.
  • Localization
    • Chat-closure notifications, actions, and provider-switch prompts are available in supported languages.