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 understanding the repository structure and the issue at hand. The issue is about a floating terminal panel that auto-creates a tab when the user closes the last one, instead of closing the panel. Let me explore the repository to understand the codebase.
Assistant
Found the repository structure. Let me look for the FloatingTerminalPanel.tsx file specifically.
Assistant
Let me read the FloatingTerminalPanel.tsx file to understand the issue.
Assistant
Now let me examine the file and understand the full context better:
Assistant
Now let me understand the issue better by examining the code. I can see the FloatingTerminalPanel.tsx file. Let me analyze the key areas:
Assistant
Assistant
Let me check how the createTab function works and how the store is structured.
Assistant
Good,
createTabis a store action that synchronously updates the store. Let me now look at how the store is structured and verify my understanding.Assistant
Now I have a good understanding of the codebase. Let me now look at the tests for the FloatingTerminalPanel if there are any.
Assistant
Let me run the existing tests to see what's currently passing:
Assistant
Let me implement the fix for FloatingTerminalPanel.tsx:
Assistant
Now I have a complete understanding of the issue. Let me implement the fix.
Summary of issues:
- Auto-create on tab removal (lines 95-103): The effect auto-creates a tab whenever
open=trueandtabs.length=0. This fires when the user closes the last tab, causing the panel to never close. - Stale closure in
closeOthers(lines 176-184): Uses capturedtabsarray inuseCallbackdependencies. - Stale closure in
closeToRight(lines 187-197): Uses capturedtabsarray AND doesn't respect tab-bar order.
Fix plan:
- Change the auto-create effect to only trigger on panel open transition (closed → open) using a ref to track the previous
openstate. - Fix
closeOthersto useuseAppStore.getState()for fresh state. - Fix
closeToRightto useuseAppStore.getState()and respecttabBarOrderordering.
- Auto-create on tab removal (lines 95-103): The effect auto-creates a tab whenever
Assistant
Let me first look at the exact lines I need to edit:
Assistant
Let me view the exact lines I need to edit more carefully:
Assistant
Now let me make the edits. I'll start with adding the
prevOpenRefto track panel open transitions:Assistant
Let me check the exact line content:
Assistant
Let me try using the replace command instead:
Assistant
Let me use the proper syntax for the edit command:
Assistant
Let me use the
editcommand with proper multiline syntax:Assistant
Let me check the file:
Assistant
Good, the lines were added. But there's a small issue - the
\nin the sed command may have created literal\ntext. Let me check and fix:Assistant
Let me verify the lines are correct, then continue with the other fixes:
Assistant
Good, lines were added correctly. Now let me fix the auto-create effect to only trigger on panel open transition. I need to replace lines 99-105:
Assistant
I realize there's an issue with my previous approach. The
prevOpenRef.current = openat render time would overwrite the ref before the effect runs. I need to move the ref update into the effect. Let me fix this properly:Assistant
I need to reconsider my approach. Let me remove the render-time ref update and put it inside the effect instead. Let me fix this properly:
Assistant
I need to fix the approach. The render-time
prevOpenRef.current = openis incorrect - it should be tracked inside the effect. Let me fix this:Assistant
Wait, line 68 is
prevOpenRef.current = open. Let me remove that line so we only have the ref declaration and manage the ref update inside the effect.Actually, the approach should be: use
useRefwith an initial value and update the ref inside the effect. The render-time lineprevOpenRef.current = openwas wrong because it would set it to the current value, defeating the purpose.Let me check the current state:
Assistant
Wait, I realized the approach is wrong. The render-time
prevOpenRef.current = openwas meant to always keep it up to date, but that means the ref always matches the current value. I need to check for a transition from false to true inside the effect, and update the ref there. Let me fix the auto-create effect:Assistant
The
prevOpenRef.current = openline was needed but I removed it. Let me add back the proper render-time update that captures the previous value:Assistant
Let me check what the current state of the file is around lines 65-72
Assistant
Good. Now I have:
prevOpenRef = useRef(open)- initialized with the initial valueconst prevOpenValue = prevOpenRef.current- captures the previous valueprevOpenRef.current = open- updates for the next render
Now I need to modify the auto-create effect (lines 99-107) to use
prevOpenValueinstead of checking justopen:Assistant
Let me find the exact line numbers for the auto-create effect.
Assistant
The bash history expansion is interfering with the
!character. Let me escape it.Assistant
Let me see the auto-create effect:
Assistant
Now I need to replace the auto-create effect. The effect should only trigger when the panel transitions from closed to open. Let me replace the effect:
Current lines 99-105:
useEffect(() => { if (!open || tabs.length > 0) { return } const tab = createTab(FLOATING_TERMINAL_WORKTREE_ID, undefined, undefined, { activate: false }) setActiveTabForWorktree(FLOATING_TERMINAL_WORKTREE_ID, tab.id) }, [createTab, open, setActiveTabForWorktree, tabs.length])New effect:
useEffect(() => { if (prevOpenValue === false && open && tabs.length === 0) { const tab = createTab(FLOATING_TERMINAL_WORKTREE_ID, undefined, undefined, { activate: false }) setActiveTabForWorktree(FLOATING_TERMINAL_WORKTREE_ID, tab.id) } }, [createTab, open, setActiveTabForWorktree, tabs.length])Wait, but I need to be careful about the dependencies. The
prevOpenValueis set at render time and is always up to date by the time the effect runs (since effects run after render). So I don't need it in the dependency array.Let me use bash to replace the lines:
Assistant
Now let me replace lines 99-105 with the new effect:
Assistant
Now I need to insert the new effect at line 99.
Assistant
Let me verify the changes:
Assistant
There's a duplicate line. Let me fix this:
Assistant
I see there's a duplicate dependency line. Let me fix it:
Assistant
Let me remove the duplicate line 105:
Assistant
Let me verify the fix and check indentation:
Assistant
The indentation is missing the leading 2 spaces for the
useEffectblock. Let me fix that:Assistant
I need to fix the indentation - the
useEffectis missing its leading 2 spaces:Assistant
Now let me fix the
closeOthersandcloseToRightcallbacks. First let me check their current state:Assistant
Let me see the current state of these callbacks:
Assistant
Now let me fix
closeOthersandcloseToRightto use fresh store state. Let me also check the tabBarOrder variable:Assistant
Now let me fix
closeOthersandcloseToRightto use fresh store state. ForcloseOthers:Assistant
Now let me replace the closeOthers function (lines 177-185) with the new version.
Assistant
Let me check the current state of the file:
Assistant
There's a duplicate
)on line 189. Let me fix that:Assistant
I need to remove the duplicate
)on line 189:Assistant
Now let me fix
closeToRightto use fresh store state and respect the tab-bar order:Assistant
Now let me find the current line numbers for closeToRight:
Assistant
The
closeToRightfunction spans from line 190 to the end of theuseCallback(looking at the original file, it was 9 lines: 190-197). Let me check.Assistant
Now I need to replace lines 190-201 with the new closeToRight implementation.
Assistant
Now let me verify the final state of the file and check for any syntax errors.
Assistant
The changes look correct. Let me verify the complete file for any syntax issues:
Assistant
Good, the file looks correct. Let me verify the changes are complete by running the TypeScript compiler to check for syntax errors:
Assistant
The TypeScript errors are expected - they're due to missing tsconfig settings (path aliases, JSX) when running
tscdirectly. Let me try running the project's test suite:Assistant
Let me run the tests to verify the changes don't break anything.
Assistant
The test failures are all related to the
better-sqlite3native module bindings, which is an environment issue unrelated to my changes. 641 test files passed with 6712 tests passing. Let me check if there are any tests specific to the FloatingTerminalPanel:Assistant
Let me check the related test files to see if they're testing the same patterns:
Assistant
Let me look at the test files to understand how they test closeOthers and closeToRight.
Assistant
Good, the test files don't specifically test the FloatingTerminalPanel's closeOthers and closeToRight implementations. Let me verify the syntax of my changes is correct:
Assistant
Let me do a final review of all the changes I've made and verify they're correct:
Assistant
Let me verify there are no TypeScript errors in the changed file:
Assistant
No TypeScript errors specific to my changes. Let me run the tests that might be related to floating terminal or tab bar functionality:
Assistant
Let me run the tests correctly. The config was passed twice, let me fix that.
Assistant
All tab-bar tests pass. Let me also run the tab-group tests:
Assistant
All related tests pass. Let me also run the terminal-related tests to be thorough:
Assistant
All terminal-related tests pass. The 5 failures are due to Electron not being installed correctly (environment issue), which is unrelated to my changes.
Let me do a final review of the changes:
Assistant
The changes look correct. Let me verify the complete solution one final time and summarize:
Assistant
All changes are complete. Here's a summary of what was fixed:
1. Auto-create on tab removal (root cause of the issue)
- Added
prevOpenRefto track the previousopenstate value at render time - Changed the auto-create effect to only trigger on panel open transition (
prevOpenValue === false && open && tabs.length === 0) instead of wheneveropenis true and there are no tabs - This means closing the last tab no longer auto-creates a replacement; the panel will close cleanly
2. Stale closure in
closeOthers- Changed from using the captured
tabsarray inuseCallbackdependencies to reading fresh state viauseAppStore.getState() - Removed
tabsfrom the dependency array, preventing stale closure bugs when rapidly closing tabs
3. Stale closure + wrong ordering in
closeToRight- Changed from using the captured
tabsarray to reading fresh state viauseAppStore.getState() - Now respects the
tabBarOrderfrom the store for proper tab-bar ordering instead of the raw backing array order
- Added
Assistant
The user is asking me to submit my changes. Let me call the submit command correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.