From 08675bbba2aedf5ea69f7bed61040da57fbb0459 Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Mon, 11 May 2026 23:01:49 -0400 Subject: [PATCH] Keep mobile terminal tap active Co-authored-by: Orca --- .../app/h/[hostId]/session/[worktreeId].tsx | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/mobile/app/h/[hostId]/session/[worktreeId].tsx b/mobile/app/h/[hostId]/session/[worktreeId].tsx index b32bd5873dd..0941e7b00c3 100644 --- a/mobile/app/h/[hostId]/session/[worktreeId].tsx +++ b/mobile/app/h/[hostId]/session/[worktreeId].tsx @@ -479,6 +479,7 @@ export default function SessionScreen() { const activeHandleRef = useRef(null) const activeSessionTabTypeRef = useRef<'terminal' | 'markdown' | 'file' | null>(null) const pendingActiveSessionTabIdRef = useRef(null) + const pendingActiveTerminalHandleRef = useRef(null) const markdownSaveSeqRef = useRef>(new Map()) const markdownSaveInFlightRef = useRef>(new Set()) const subscribeSeqRef = useRef>(new Map()) @@ -951,6 +952,7 @@ export default function SessionScreen() { const snapshotActive = nextTabs.find((tab) => tab.isActive) ?? nextTabs[0] ?? null const pendingActiveSessionTabId = pendingActiveSessionTabIdRef.current + const pendingActiveTerminalHandle = pendingActiveTerminalHandleRef.current let active = snapshotActive if (pendingActiveSessionTabId) { if (snapshotActive?.id === pendingActiveSessionTabId) { @@ -966,6 +968,34 @@ export default function SessionScreen() { } } } + if (pendingActiveTerminalHandle) { + const pendingTerminalTab = nextTabs.find( + (tab): tab is Extract => + tab.type === 'terminal' && tab.terminal === pendingActiveTerminalHandle + ) + const pendingTerminalExists = mergedTerminalsForActive.some( + (terminal) => terminal.handle === pendingActiveTerminalHandle + ) + if ( + snapshotActive?.type === 'terminal' && + snapshotActive.terminal === pendingActiveTerminalHandle + ) { + pendingActiveTerminalHandleRef.current = null + } else if (pendingTerminalTab) { + // Why: desktop active flags can lag a mobile terminal tap. Key by + // terminal handle too, because fallback PTY tabs may not yet have a + // stable session tab id during new-worktree startup. + active = pendingTerminalTab + } else if (pendingTerminalExists) { + setActiveSessionTabId(pendingActiveTerminalHandle) + activeSessionTabTypeRef.current = 'terminal' + setActiveHandle(pendingActiveTerminalHandle) + subscribeToTerminal(pendingActiveTerminalHandle) + return + } else { + pendingActiveTerminalHandleRef.current = null + } + } const currentHandle = activeHandleRef.current if ( currentHandle && @@ -1402,6 +1432,7 @@ export default function SessionScreen() { activeHandleRef.current = null activeSessionTabTypeRef.current = null pendingActiveSessionTabIdRef.current = null + pendingActiveTerminalHandleRef.current = null setActiveHandle(null) setTerminals([]) terminalsRef.current = [] @@ -1512,6 +1543,7 @@ export default function SessionScreen() { tab.type === 'terminal' && tab.terminal === handle ) pendingActiveSessionTabIdRef.current = matchingTab?.id ?? null + pendingActiveTerminalHandleRef.current = handle activeSessionTabTypeRef.current = 'terminal' setActiveSessionTabId(matchingTab?.id ?? null) const prev = activeHandleRef.current @@ -1551,6 +1583,7 @@ export default function SessionScreen() { triggerSelection() pendingActiveSessionTabIdRef.current = tab.id + pendingActiveTerminalHandleRef.current = null activeSessionTabTypeRef.current = tab.type setActiveSessionTabId(tab.id) const prev = activeHandleRef.current @@ -1983,6 +2016,7 @@ export default function SessionScreen() { if (activeHandleRef.current === target.handle) { const replacement = next[0] ?? null activeHandleRef.current = replacement?.handle ?? null + pendingActiveTerminalHandleRef.current = replacement?.handle ?? null setActiveHandle(replacement?.handle ?? null) if (replacement) { subscribeToTerminal(replacement.handle)