From 529c06ff62c9f5bc941f1aa48ec6c8f18901c46c Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 3 Jul 2026 22:31:54 -0700 Subject: [PATCH] cleanup(terminal): dedupe pane teardown, reuse platform check, harden shadow-cursor tracking (#7320) Quality pass on terminal PRs merged 2026-07-03: - pane-split-close: extract a shared `teardownManagedPane` so `closeManagedPane` and `detachManagedPaneForExternalMove` can't drift apart on future edits (#7215). - terminal-link-activation: reuse the existing `isMacPlatform()` instead of a second inline `navigator.userAgent` copy, keeping one source of truth for Mac detection. - pty-connection: gate the shadow-cursor moves on empty CSI params so parameterized/ modified cursor keys (e.g. Ctrl+Left word-jump, `ESC[1;5D`) reset command tracking instead of silently desyncing the buffer used to infer a typed agent name; relocate the `getAuthoritativePaneAgent` JSDoc to the function it documents (#6954). No behavior change except the cursor-tracking hardening; all touched unit suites pass. --- .../terminal-pane/pty-connection.ts | 27 ++++++++++------ .../terminal-pane/terminal-link-activation.ts | 5 +-- .../src/lib/pane-manager/pane-split-close.ts | 31 +++++++------------ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/renderer/src/components/terminal-pane/pty-connection.ts b/src/renderer/src/components/terminal-pane/pty-connection.ts index ae6a81782..66e7e5f66 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.ts @@ -1247,14 +1247,9 @@ export function connectPanePty( ) return tab?.defaultTitle?.trim() || 'Terminal' } - /** - * Resolves the authoritative owner agent type for this pane, checking tab launch, - * pane startup, typed command ownership, and store state configuration. - * - * Why: launch ownership wins so Pi-compatible live titles/hooks can't repaint an - * OMP-owned pane back to Pi; command ownership covers manually typed `omp` - * in generic terminals where launch metadata does not exist. - */ + // Why: infer pane ownership from a manually typed agent command (e.g. `omp`) by + // shadowing the shell's current command line, for generic terminals where no + // launch metadata exists. Consumed by getAuthoritativePaneAgent below. let commandInferredPaneAgent: TuiAgent | null = null let pendingShellCommandLine = '' let pendingShellCommandCursor = 0 @@ -1355,9 +1350,13 @@ export function connectPanePty( return null } const params = data.slice(index + 2, cursor) - if (final === 'D') { + // Why: only emulate a bare one-column move. Parameterized/modified cursor keys + // (e.g. Ctrl+Left `\x1b[1;5D` = word-jump) move the real cursor by more than + // one, so tracking them as ±1 would desync the shadow line — fall through to + // reset instead of silently corrupting the sampled command. + if (final === 'D' && params === '') { movePendingShellCommandCursor(-1) - } else if (final === 'C') { + } else if (final === 'C' && params === '') { movePendingShellCommandCursor(1) } else if (final === 'H' || (final === '~' && params === '1')) { pendingShellCommandCursor = 0 @@ -1462,6 +1461,14 @@ export function connectPanePty( } } } + /** + * Resolves the authoritative owner agent type for this pane, checking tab launch, + * pane startup, typed command ownership, and store state configuration. + * + * Why: launch ownership wins so Pi-compatible live titles/hooks can't repaint an + * OMP-owned pane back to Pi; command ownership covers manually typed `omp` + * in generic terminals where launch metadata does not exist. + */ const getAuthoritativePaneAgent = (): AgentType | undefined => { const state = useAppStore.getState() const tab = (state.tabsByWorktree[deps.worktreeId] ?? []).find( diff --git a/src/renderer/src/components/terminal-pane/terminal-link-activation.ts b/src/renderer/src/components/terminal-pane/terminal-link-activation.ts index e85da9032..41def2095 100644 --- a/src/renderer/src/components/terminal-pane/terminal-link-activation.ts +++ b/src/renderer/src/components/terminal-pane/terminal-link-activation.ts @@ -1,6 +1,7 @@ +import { isMacPlatform } from './terminal-link-open-hints' + export function isTerminalLinkActivation( event: Pick | undefined ): boolean { - const isMac = navigator.userAgent.includes('Mac') - return isMac ? Boolean(event?.metaKey) : Boolean(event?.ctrlKey) + return isMacPlatform() ? Boolean(event?.metaKey) : Boolean(event?.ctrlKey) } diff --git a/src/renderer/src/lib/pane-manager/pane-split-close.ts b/src/renderer/src/lib/pane-manager/pane-split-close.ts index b7d8356e0..4bc601837 100644 --- a/src/renderer/src/lib/pane-manager/pane-split-close.ts +++ b/src/renderer/src/lib/pane-manager/pane-split-close.ts @@ -166,7 +166,7 @@ type CloseManagedPaneArgs = { setActivePaneId: (paneId: number | null) => void } -export function closeManagedPane(args: CloseManagedPaneArgs): void { +function teardownManagedPane(args: CloseManagedPaneArgs, reason: 'close' | 'detach'): void { const pane = args.panes.get(args.paneId) if (!pane) { return @@ -183,33 +183,24 @@ export function closeManagedPane(args: CloseManagedPaneArgs): void { args.managerOptions.onPaneClosed?.(args.paneId, { paneId: args.paneId, leafId: closedLeafId, - reason: 'close' + reason }) args.managerOptions.onLayoutChanged?.() } +export function closeManagedPane(args: CloseManagedPaneArgs): void { + teardownManagedPane(args, 'close') +} + export function detachManagedPaneForExternalMove(args: CloseManagedPaneArgs): boolean { - const pane = args.panes.get(args.paneId) - if (!pane || args.panes.size <= 1) { + // Why: refuse to detach the last pane — there is no other pane to fall back to. + if (!args.panes.has(args.paneId) || args.panes.size <= 1) { return false } - const closedLeafId = pane.leafId - args.releasePaneIdentity(args.paneId) - removePaneContainer(args, pane) - const nextActivePaneId = activateReplacementPane(args) - applyPaneOpacity(args.panes.values(), nextActivePaneId, args.styleOptions) - for (const p of args.panes.values()) { - safeFit(p) - } - updateMultiPaneState(args.getDragCallbacks()) // Why: pane-to-tab detach tears down only this renderer pane; the PTY is - // adopted by the new tab, so TerminalPane must skip process-close cleanup. - args.managerOptions.onPaneClosed?.(args.paneId, { - paneId: args.paneId, - leafId: closedLeafId, - reason: 'detach' - }) - args.managerOptions.onLayoutChanged?.() + // adopted by the new tab, so the 'detach' reason tells TerminalPane to skip + // process-close cleanup. + teardownManagedPane(args, 'detach') return true }