Fix duplicate hibernated agent resume (#6323)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-06-24 20:33:27 -07:00
committed by GitHub
co-authored by Orca
parent c5d67a923b
commit c328ac8a9c
2 changed files with 46 additions and 4 deletions
@@ -119,6 +119,30 @@ describe('resumeSleepingAgentSessionsForWorktree', () => {
expect(state.sleepingAgentSessionsByPaneKey[record.paneKey]).toBe(record)
})
it('skips hibernated stable panes after their live PTY binding is cleared', () => {
const paneKey = makePaneKey('tab-1', LEAF_ID)
const record = makeRecord({ paneKey, origin: 'worktree-sleep', state: 'done' })
useAppStore.setState({
tabsByWorktree: { 'wt-1': [makeTerminalTab('tab-1', 'wt-1')] },
terminalLayoutsByTabId: {
'tab-1': {
root: { type: 'leaf', leafId: LEAF_ID },
activeLeafId: LEAF_ID,
expandedLeafId: null
}
},
sleepingAgentSessionsByPaneKey: { [record.paneKey]: record }
} as never)
const launched = resumeSleepingAgentSessionsForWorktree('wt-1')
const state = useAppStore.getState()
expect(launched).toBe(0)
expect(state.tabsByWorktree['wt-1']).toHaveLength(1)
expect(state.pendingStartupByTabId).toEqual({})
expect(state.sleepingAgentSessionsByPaneKey[record.paneKey]).toBe(record)
})
it('skips legacy numeric pane-key records owned by a preserved tab wake hint', () => {
const record = makeRecord({ paneKey: 'tab-1:0', origin: 'worktree-sleep' })
useAppStore.setState({
@@ -11,7 +11,11 @@ import {
resolveTuiAgentLaunchEnv
} from '../../../shared/tui-agent-launch-defaults'
import type { SleepingAgentSessionRecord } from '../../../shared/agent-session-resume'
import type { TerminalTab } from '../../../shared/types'
import type {
TerminalLayoutSnapshot,
TerminalPaneLayoutNode,
TerminalTab
} from '../../../shared/types'
import { parseLegacyNumericPaneKey, parsePaneKey } from '../../../shared/stable-pane-id'
import { translate } from '@/i18n/i18n'
import { AGENT_STATUS_STALE_AFTER_MS } from '../../../shared/agent-status-types'
@@ -141,13 +145,27 @@ function hasRestorableLegacyTabPty(
return Boolean(tab.ptyId) || (ptyIdsByTabId[tab.id]?.length ?? 0) > 0
}
function layoutContainsLeaf(
node: TerminalPaneLayoutNode | null | undefined,
leafId: string
): boolean {
if (!node) {
return false
}
if (node.type === 'leaf') {
return node.leafId === leafId
}
return layoutContainsLeaf(node.first, leafId) || layoutContainsLeaf(node.second, leafId)
}
function hasMatchingStablePaneLayout(
tabId: string,
leafId: string,
terminalLayoutsByTabId: ReturnType<typeof useAppStore.getState>['terminalLayoutsByTabId']
terminalLayoutsByTabId: Record<string, TerminalLayoutSnapshot | undefined>
): boolean {
const ptyIdsByLeafId = terminalLayoutsByTabId[tabId]?.ptyIdsByLeafId
return Boolean(ptyIdsByLeafId && Object.hasOwn(ptyIdsByLeafId, leafId))
// Why: hibernation intentionally clears the live PTY binding after the pane
// exits, but the preserved leaf still owns cold-restore for its session.
return layoutContainsLeaf(terminalLayoutsByTabId[tabId]?.root, leafId)
}
function findSameWorktreeTab(