perf(terminal): index sleeping-record and launch-agent tabs once per parking pass

goneSessionsKeepTabMounted scanned every sleeping record per eligible tab,
so a workspace switch with thousands of tabs did tabs x records work on the
renderer thread. Build both owner sets once per source identity instead.
This commit is contained in:
Brennan Benson
2026-11-08 14:59:43 -08:00
parent a30768edd0
commit 15e720c2a0
2 changed files with 47 additions and 4 deletions
@@ -0,0 +1,44 @@
/** Agent-owner tab sets for gone-session parking, built once per source identity so a pass over every tab stays linear. */
import type { TerminalTab } from '../../../../../../shared/workspace-layout/terminal-tab-types'
import type { SleepingAgentSessionRecord } from '../../../../../../shared/agent-session/lifecycle/agent-session-resume'
const launchAgentTabIdsByTabs = new WeakMap<readonly TerminalTab[], ReadonlySet<string>>()
const NO_TAB_IDS: ReadonlySet<string> = new Set()
export function launchAgentTabIds(tabs: readonly TerminalTab[] | undefined): ReadonlySet<string> {
if (!tabs) {
return NO_TAB_IDS
}
let tabIds = launchAgentTabIdsByTabs.get(tabs)
if (!tabIds) {
tabIds = new Set(tabs.flatMap((tab) => (tab.launchAgent ? [tab.id] : [])))
launchAgentTabIdsByTabs.set(tabs, tabIds)
}
return tabIds
}
let sleepingRecordTabIdsCache: {
records: Record<string, SleepingAgentSessionRecord>
tabIds: ReadonlySet<string>
} | null = null
/** Tabs a sleeping record names by tabId or by a `${tabId}:` pane-key prefix. */
export function sleepingRecordTabIds(
records: Record<string, SleepingAgentSessionRecord>
): ReadonlySet<string> {
if (sleepingRecordTabIdsCache?.records === records) {
return sleepingRecordTabIdsCache.tabIds
}
const tabIds = new Set<string>()
for (const paneKey in records) {
const recordTabId = records[paneKey]?.tabId
if (recordTabId !== undefined) {
tabIds.add(recordTabId)
}
for (let colon = paneKey.indexOf(':'); colon !== -1; colon = paneKey.indexOf(':', colon + 1)) {
tabIds.add(paneKey.slice(0, colon))
}
}
sleepingRecordTabIdsCache = { records, tabIds }
return tabIds
}
@@ -16,6 +16,7 @@ import {
resolveParkedTerminalPaneCandidates,
type ParkableTerminalTabModel
} from './terminal-parked-watcher-reconciliation'
import { launchAgentTabIds, sleepingRecordTabIds } from './parked-tab-agent-owner-index'
import { collapseParkedExitedLeaf, startParkedPtyWatcher } from './terminal-parked-pty-watcher'
import {
capturedPanesByTabId,
@@ -84,10 +85,8 @@ function goneSessionsKeepTabMounted(
}
return (
goneCount < panes.length ||
state.tabsByWorktree[worktreeId]?.some((t) => t.id === tab.id && t.launchAgent) === true ||
Object.entries(state.sleepingAgentSessionsByPaneKey).some(
([paneKey, record]) => record.tabId === tab.id || paneKey.startsWith(`${tab.id}:`)
)
launchAgentTabIds(state.tabsByWorktree[worktreeId]).has(tab.id) ||
sleepingRecordTabIds(state.sleepingAgentSessionsByPaneKey).has(tab.id)
)
}