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
I'll start by exploring the repository structure to understand the codebase and locate the relevant files.
Assistant
Let me search for the FloatingTerminalPanel file specifically.
Assistant
Let me understand the codebase better by looking at the store and related files.
Assistant
Now let me look at the specific issue. I need to understand the current behavior:
- Line 102-108: The auto-create effect creates a tab when
openis true andtabs.lengthis 0 closeFloatingTab: Simply callscloseTab(tabId)which triggers the auto-create effect againcloseOthersandcloseToRight: Usetabsfrom the closure instead of getting fresh state from the store
Let me look at the test files to understand what's expected:
- Line 102-108: The auto-create effect creates a tab when
Assistant
Let me look at the test files more carefully:
Assistant
Now let me understand the exact problems:
-
Problem 1: The auto-create effect (lines 102-108) creates a new tab whenever
openis true andtabs.lengthis 0. When the user clicks X on the last tab,closeTabis called,tabs.lengthbecomes 0, and the effect fires again to create a new tab. The panel never closes. -
Problem 2:
closeOthersandcloseToRightusetabsfrom theuseCallbackdependency, which is a stale snapshot. They should useuseAppStore.getState()to get the current state. -
Problem 3:
closeToRightshould follow the reconciled visible tab-bar order (tabBarOrder), not the raw backing array order.
The fix should:
- Close the panel when the user explicitly closes the last tab (but not when a PTY exits)
- Fix
closeOthersandcloseToRightto read fresh state closeToRightshould usetabBarOrderinstead oftabs
Let me check what
tabBarOrderis and how it's used:-
Assistant
Now let me check the
TabBarcomponent to understand how these callbacks are used:Assistant
Now let me look at the issue description more carefully. The evaluation contract says:
- "Bootstrap a terminal tab only when the panel transitions open" - this means the auto-create should only happen when transitioning from closed to open
- "An explicit close action that removes the last tab closes the panel" - clicking X on the last tab should close the panel
- "A PTY exit removes its tab but must neither close the panel nor create a replacement tab" - PTY exit is different from user close
- "Bulk close operations must read the current store state rather than a stale render-time tab list" - fix stale closures
- "
closeToRightmust follow the reconciled visible tab-bar order rather than raw backing-array order" - usetabBarOrder
Let me think about the solution:
For problem 1, I need to distinguish between:
- User explicitly closing the last tab (should close the panel)
- Panel opening and needing a tab (should create one)
- PTY exit removing a tab (should neither close panel nor create replacement)
One approach is to add a flag to track whether we're auto-creating a tab, so when a tab is closed, we can check if we should close the panel. But a cleaner approach would be to use a ref to track if the user explicitly closed a tab.
Assistant
Now let me check how the panel open state is managed:
Assistant
Now I have a clear picture of the problem. Let me create my fix:
-
For the auto-create/close issue: I need to track when the panel is opened via the hotkey (transition to open) vs. when a tab is closed. When a tab close results in zero tabs, and it wasn't triggered by PTY exit, the panel should close.
-
For
closeOthersandcloseToRightstale closures: UseuseAppStore.getState()to read current state. -
For
closeToRight: UsetabBarOrderinstead oftabsarray order.
Let me look at the current implementation more carefully to understand how to implement this:
-
Assistant
The key insight is:
- The auto-create effect fires when
openis true ANDtabs.length === 0 - When user closes the last tab,
closeTabis called,tabs.lengthbecomes 0, and the effect fires again creating a new tab
The fix: I need to distinguish between:
- Panel just opened (should create tab if none exist)
- User explicitly closed last tab (should close panel)
- PTY exit (should neither close panel nor create tab)
Looking at the code:
closeFloatingTabis called from the X button on tabscloseOthersandcloseToRightare called from context menuonPtyExitis called when a PTY exitsonCloseTabis called fromTerminalPanecomponent
The simplest fix is to:
- Add a ref to track whether we're "bootstrapping" (just opened the panel)
- Modify the auto-create effect to only run during bootstrap
- Add an effect that watches for
tabs.length === 0 && open(after initial bootstrap) and closes the panel
Let me also check how the store's
closeTabworks:- The auto-create effect fires when
Assistant
Now I understand the full picture. Let me plan my fix:
Problem 1: Panel stays open after closing last tab
The auto-create effect at lines 98-103 fires whenever
openis true andtabs.length === 0. This means when the user closes the last tab, the effect immediately creates a new one.Solution:
- Add a ref
autoCreatedTabRefto track whether we just auto-created a tab - Modify the auto-create effect to only run when the panel transitions from closed to open
- Add a new effect that closes the panel when tabs become empty (but only after the auto-create has run once)
Actually, a cleaner approach:
- Track whether the panel was "just opened" vs. "user closing tabs"
- Add a ref
Assistant
So
onCloseTabis also an explicit user action (Cmd+W). Now let me implement the fix:My approach:
- Use a ref to track whether the auto-create effect has already run for the current open session
- Only auto-create when
opentransitions to true AND no auto-create has happened yet - When
tabs.lengthbecomes 0 whileopenis true AND auto-create has already happened, close the panel - Fix
closeOthersandcloseToRightto use fresh state from the store - Fix
closeToRightto usetabBarOrderinstead oftabsarray order
Let me now implement the fix:
Assistant
Now I'll implement the fix. Let me create the solution step by step:
Assistant
Now let me make the edits. First, I'll add the ref to track auto-create state:
Assistant
Let me verify the change and then modify the auto-create effect:
Assistant
Good. Now I need to modify the auto-create effect. The current effect at lines 98-104:
javascriptuseEffect(() => { 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])I need to:
- Only auto-create when the panel transitions to open (using
autoCreatedTabRef) - Reset the ref when panel closes
- Add an effect to close the panel when tabs become empty (after auto-create has happened)
- Only auto-create when the panel transitions to open (using
Assistant
Now let me replace this effect with the new logic:
Assistant
Let me verify the changes:
Assistant
Assistant
Now I have a clearer plan. Let me implement the solution properly:
-
Auto-create effect: Only create a tab when the panel transitions from closed to open. Use a ref to prevent re-creation.
-
User close handling: In
closeFloatingTab,closeOthers, andcloseToRight, check if closing would result in 0 tabs and close the panel in that case. -
PTY exit: Just call
closeTab()- no panel close, no auto-create. -
closeOthersandcloseToRightstale closure: UseuseAppStore.getState()to get fresh state. -
closeToRightorder: UsetabBarOrderinstead oftabsarray order.
Let me write the complete fix:
-
Assistant
I need to use sed instead of Python to make the changes.
Assistant
I'll use perl for the replacements since they're complex:
Assistant
Good, now let me replace the auto-create effect:
Assistant
I need to replace the auto-create effect. Let me use sed with a more reliable approach:
Assistant
Let me use a different approach - I'll create a new version of the entire file with all the fixes applied.
Assistant
Good, the ref was added. Now let me apply the rest of the changes using sed. First, let me replace the auto-create effect (lines 97-104):
Assistant
The awk script didn't work correctly. Let me start fresh with a cleaner approach:
Assistant
Let me create a new version of the file with all changes applied correctly:
Assistant
This approach is getting messy. Let me restart and use a simpler method:
Assistant
Let me create the fixed file from scratch by reading the original and writing the patched version:
Assistant
Let me verify the changes:
Assistant
The diff looks good. Let me verify the file is syntactically correct by checking it:
Assistant
Now let me check the close callbacks:
Assistant
Assistant
Let me check what triggers
executeClosePanewhich callsonCloseTab:Assistant
OK, so
onCloseTabis triggered by Cmd+W in TerminalPane. Looking at my fix, theonCloseTabhandler in the FloatingTerminalPanel callscloseTab(tab.id)directly, which would trigger the auto-create effect again.Wait, no. Let me re-read my fix. The auto-create effect is now guarded by
autoCreatedTabRef.current. Once a tab is auto-created, the ref is set to true, and the effect won't fire again until the panel closes and reopens.But there's still the issue that
onCloseTabin the FloatingTerminalPanel (which callscloseTab(tab.id)) doesn't close the panel when the last tab is closed. OnlycloseFloatingTabdoes that.Let me check how
onCloseTabis different fromcloseFloatingTab:Looking at the code:
closeFloatingTabis passed toTabBarasonClose- this handles the X button on a tabonCloseTabis passed toTerminalPane- this handles Cmd+W or closing the last pane
So both should close the panel when the last tab is closed. Let me update the
onCloseTabhandler:Assistant
I need to update
onCloseTabto usecloseFloatingTabinstead ofcloseTabso it also closes the panel when the last tab is closed. Let me make this change:Assistant
Good. Now let me run the tests to see if my changes work correctly:
Assistant
I need to continue with testing my changes. Let me run the existing tests related to the floating terminal and verify my fix is correct.
Assistant
This test file is just for bounds calculation, not for the panel behavior. Let me look for tests that might cover the floating terminal tab close behavior:
Assistant
Let me run the unit tests for the floating terminal and other related tests:
Assistant
The previous command timed out while trying to install pnpm. Let me try a different approach to verify the code is syntactically correct.
Assistant
Good, the file looks correct so far. Let me verify the complete file by viewing more of it and then run a TypeScript check if available.
Assistant
Let me verify the complete diff to make sure all changes are correct before submitting.
Assistant
Now let me review my changes against the evaluation contract and try to verify TypeScript compiles:
Assistant
Let me try a different approach to verify the TypeScript compiles. I'll use the project's build system instead.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.