ISSUE DESCRIPTION Floating terminal auto-creates tab after user closes the last one…
- User
ISSUE DESCRIPTION
Floating terminal auto-creates tab after user closes the last one
Floating terminal auto-creates tab after user closes the last one
Symptom
When the floating terminal has a single tab open and the user clicks the X button to close it, the panel auto-creates a new terminal tab instead of closing the panel. The user has to close the panel manually via the minimize button or by closing the newly created tab again.
Steps to reproduce
- Open the floating terminal (Cmd+Alt+T on Mac / Ctrl+Alt+T on Windows/Linux)
- The panel opens with one auto-created terminal tab
- Click the X button on that tab to close it
- Expected: Panel closes cleanly
- Actual: Panel stays open with a fresh auto-created tab
Root cause
The
closeTabcallback used for the X button triggers the auto-create effect (lines 102-108 inFloatingTerminalPanel.tsx) that spawns a tab whenever the panel is open with zero tabs. The panel never closes because a replacement tab is created before the close check can run.Additionally,
closeOthersandcloseToRightcontext menu actions had stale closure bugs—they readtabs.lengthfrom the captured array inuseCallbackdependencies instead of pulling fresh state viauseAppStore.getState(), causing these callbacks to operate on outdated tab lists.Expected behavior
- Clicking X on the last terminal tab should close the floating panel (symmetric with the auto-create on open)
- The panel should remain open if a PTY exits so the user can see exit status
- Multi-tab operations (
closeOthers,closeToRight) should operate on current state, not stale captures
Why it matters
The asymmetry breaks user expectations: the panel auto-creates a tab on open (hotkey press), but closing that tab doesn't reverse the action. Users expect the X button on the last tab to dismiss the panel, not spawn an orphaned replacement.
Environment
- All platforms (macOS, Linux, Windows)
- Floating terminal feature introduced in recent releases
Evaluation contract
Bootstrap a terminal tab only when the panel transitions open. An explicit close action that removes the last tab closes the panel; a PTY exit removes its tab but must neither close the panel nor create a replacement tab. Bulk close operations must read the current store state rather than a stale render-time tab list, and
closeToRightmust follow the reconciled visible tab-bar order rather than raw backing-array order. Assistant
Let me start by exploring the repository structure to understand the codebase and find the relevant file mentioned in the issue:
FloatingTerminalPanel.tsx.Assistant
I need to find the FloatingTerminalPanel.tsx file mentioned in the issue. Let me search for it more specifically.
Assistant
Found it. Let me open the main file and read it to understand the current implementation.
Assistant
Let me try a different approach to view the file:
Assistant
Now I have a good understanding of the code. Let me look at the store to understand how
closeTabworks, and also check if there are any tests for this component.Assistant
Let me look at the relevant tests and understand the store API:
Assistant
Now I understand the codebase well. Let me analyze the issues identified in the bug report:
Issue 1: Auto-create tab prevents panel from closing
- Lines 97-103: The
useEffectcreates a tab wheneveropenis true andtabs.length === 0. When user closes the last tab via X button (closeFloatingTab→closeTab), this effect fires and creates a new tab, preventing the panel from closing.
Issue 2: Stale closure in
closeOthersandcloseToRight- Lines 176-197: These
useCallbackhooks capturetabsfrom their dependency array, so they operate on stale tab lists. They should useuseAppStore.getState()to get fresh state.
Issue 3:
closeToRightshould follow tab-bar order- The issue mentions
closeToRightshould follow the reconciled visible tab-bar order rather than raw backing-array order.
- Lines 97-103: The
Assistant
I see that both
onPtyExitandonCloseTabcallcloseTabdirectly. According to the issue:- A PTY exit should remove its tab but must NOT close the panel and must NOT create a replacement tab.
- An explicit close action (clicking X) on the last tab should close the panel.
This means I need to distinguish between user-initiated close (X button) and PTY exit. Let me look at the current implementation more carefully.
Assistant
Now let me understand the full picture. The approach should be:
-
closeFloatingTab(called from X button / user action): Close the tab AND check if it's the last tab. If so, close the panel viaonOpenChange(false). -
The auto-create effect (lines 97-103): This should ONLY create a tab when the panel first opens (transitions from closed to open). But currently, it also fires when a tab is closed because
tabs.lengthchanges to 0. We need to prevent this from auto-creating when it was a user-initiated close.
The cleanest approach:
- Remove the auto-create logic from firing after a close. The effect should only fire when
opentransitions totrue. - Make
closeFloatingTabclose the panel if it's the last tab. - For PTY exit (
onPtyExit), just close the tab (don't close the panel).
Let me implement this:
-
Assistant
Now I have a clear understanding. Let me implement the fix:
-
Fix the auto-create effect (lines 97-103): Add a ref to track whether a user-initiated close is in progress, so the effect doesn't fire after a close. The auto-create should only happen when the panel transitions from closed to open.
-
Fix
closeFloatingTab: After closing a tab, check if it was the last tab, and if so, close the panel viaonOpenChange(false). -
Fix
closeOthers: UseuseAppStore.getState()to get fresh tab list. -
Fix
closeToRight: UseuseAppStore.getState()to get fresh state AND usetabBarOrderfor tab ordering.
-
Assistant
Now let me make the changes. I'll edit the file:
Assistant
I need to add a ref to prevent the auto-create effect from firing right after a user-initiated close. Let me add the
closingPanelRefafter line 73:Assistant
Now let me fix the auto-create effect. It currently fires when
openis true ANDtabs.length === 0. I need to add a guard so it doesn't fire when we're closing the panel:Assistant
Now let me fix
closeFloatingTabto close the panel when it's the last tab. Let me check the current state of the file:Assistant
Now I need to fix all three callbacks. Let me replace them:
Assistant
Let me verify the changes and see the new line numbers for
closeOthers:Assistant
Now let me fix
closeOthersto use fresh state:Assistant
Now let me fix
closeToRightto use fresh state andtabBarOrder:Assistant
Now I also need to add a cleanup effect that resets the
closingPanelRefwhen the panel reopens. Let me also verify my changes look correct:Assistant
Now I need to add an effect to reset
closingPanelRefwhen the panel reopens. This ensures that the next time the user opens the floating terminal via hotkey, the auto-create tab logic works correctly:Assistant
I'll add the ref reset effect right after the orchestration visibility effect:
Assistant
Wait, there's an issue with the approach. The
closingPanelRef.current = falseshould run whenopenchanges, but I need to be careful. Whenopenbecomesfalse, the ref should still betrueso that if the panel somehow stays visible, the auto-create doesn't fire. And whenopenbecomestrue, we reset it tofalseso the auto-create works.Actually, let me reconsider. The
closingPanelRefis a ref (not state), so the effect with[open]dependency won't re-render, but it will still run. Let me verify:Assistant
Let me verify the full file is correct:
Assistant
The code looks good. Let me now verify the changes compile correctly by running TypeScript check:
Assistant
No TypeScript errors related to our changes. Let me run existing tests related to the floating terminal:
Assistant
All floating terminal tests pass. Let me also run the broader tab-related tests to make sure nothing is broken:
Assistant
All tests pass. Let me also run the context menu tests that involve
closeOthersandcloseToRight:Assistant
All tests pass. Let me also run the IPC events test since it references FloatingTerminal:
Assistant
All tests pass. Let me clean up the backup file I created:
Assistant
Now let me review the complete diff to verify all changes:
Assistant
The diff looks correct. Let me verify that the changes address all the issues mentioned in the bug report by reviewing the checklist:
Assistant
I see. The
onPtyExitcallback currently just callscloseTabdirectly. But theonCloseTabon line 365 also just callscloseTab. These both need to be treated as non-user-initiated closes (don't close panel, don't auto-create).I should wrap these in a way that sets
closingPanelRef.current = trueto prevent auto-create, but does NOT close the panel:Assistant
Now let me verify the full diff again and run the tests:
Assistant
Let me run the tests again to make sure everything still passes:
Assistant
All 82 tests pass. Let me also run a broader set of tests to make sure there are no regressions:
Assistant
The failures appear to be related to native
better-sqlite3module binding issues, not my changes. Let me verify by checking if these failures existed before my changes:Assistant
Yes, the
better-sqlite3failures are pre-existing environment issues unrelated to my changes. Let me restore my changes:Assistant
Let me run the renderer-related tests to confirm my changes don't break anything:
Assistant
All 2547 renderer tests pass. My changes are solid.
Let me now verify the final state of the file one more time:
Assistant
The changes look correct. Let me do a final verification by reviewing the complete diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.