perf(terminal): group park-exempt sleeping records once per map; index captured panes by leaf

Each mounted worktree's exemption selector rescanned every sleeping record
after a record-map change (worktrees x records), and pane reconciliation
compared every captured pane against every fallback pane (panes^2 per tab).
This commit is contained in:
Brennan Benson
2026-11-08 15:02:11 -08:00
parent a30768edd0
commit cb5671ef86
2 changed files with 60 additions and 33 deletions
@@ -38,28 +38,40 @@ export function fallbackParkedPaneCandidates(
}))
}
function leafPtyKey(leafId: string, ptyId: string | null): string {
return `${leafId}\u0000${ptyId ?? ''}\u0000${ptyId === null ? 0 : 1}`
}
export function resolveParkedTerminalPaneCandidates(
tab: ParkableTerminalTabModel,
state: ParkedPaneFallbackState
): ParkedTerminalPaneCapture[] {
const captured = capturedPanesByTabId.get(tab.id)
const fallback = fallbackParkedPaneCandidates(tab, state)
if (!captured) {
return fallback
}
// Index captured panes once so a many-split tab stays linear in its pane count.
const capturedLeafPtyKeys = new Set(
captured.panes.map((candidate) => leafPtyKey(candidate.leafId, candidate.ptyId))
)
const capturedIsCurrent =
captured !== undefined &&
captured.panes.length > 0 &&
(tab.ptyId === null || captured.panes.some((pane) => pane.ptyId === tab.ptyId)) &&
(fallback.length === 0 ||
(captured.panes.length === fallback.length &&
fallback.every((pane) =>
captured.panes.some(
(candidate) => candidate.leafId === pane.leafId && candidate.ptyId === pane.ptyId
)
)))
fallback.every((pane) => capturedLeafPtyKeys.has(leafPtyKey(pane.leafId, pane.ptyId)))))
if (capturedIsCurrent) {
return captured.panes
}
const priorByLeafId = new Map<string, ParkedTerminalPaneCapture>()
for (const candidate of captured.panes) {
if (!priorByLeafId.has(candidate.leafId)) {
priorByLeafId.set(candidate.leafId, candidate)
}
}
return fallback.map((pane) => {
const prior = captured?.panes.find((candidate) => candidate.leafId === pane.leafId)
const prior = priorByLeafId.get(pane.leafId)
return prior
? {
...pane,
@@ -12,36 +12,51 @@ type SleepingRecordParkExemptionState = {
sleepingAgentSessionsByPaneKey?: Record<string, SleepingAgentSessionRecord>
}
/** Exempt only consumable sleeping records: parked panes cannot cold-restore, but finished notes would pin panes forever. Memoize by record-map identity to avoid rescanning every worktree on unrelated writes; iterate without allocating Object.values. */
let exemptTabIdsByWorktreeCache: {
records: Record<string, SleepingAgentSessionRecord>
byWorktreeId: ReadonlyMap<string, ReadonlySet<string>>
} | null = null
/** Group consumable records by worktree once per record map; each mounted worktree then reads its bucket instead of rescanning every record. */
function exemptTabIdsByWorktree(
records: Record<string, SleepingAgentSessionRecord>
): ReadonlyMap<string, ReadonlySet<string>> {
if (exemptTabIdsByWorktreeCache?.records === records) {
return exemptTabIdsByWorktreeCache.byWorktreeId
}
const byWorktreeId = new Map<string, Set<string>>()
for (const paneKey in records) {
const record = records[paneKey]
if (!record || activationTreatsNoteAsFinished(record)) {
continue
}
// Why: malformed pane keys must yield no owner instead of a truncated tab id.
const tabId =
record.tabId ??
parsePaneKey(record.paneKey)?.tabId ??
parseLegacyNumericPaneKey(record.paneKey)?.tabId
if (tabId) {
let owned = byWorktreeId.get(record.worktreeId)
if (!owned) {
owned = new Set()
byWorktreeId.set(record.worktreeId, owned)
}
owned.add(tabId)
}
}
exemptTabIdsByWorktreeCache = { records, byWorktreeId }
return byWorktreeId
}
/** Exempt only consumable sleeping records: parked panes cannot cold-restore, but finished notes would pin panes forever. Memoize by record-map identity to avoid rescanning every worktree on unrelated writes. */
export const selectSleepingRecordParkExemptTabIds = createWorktreeRecordSelector<
SleepingRecordParkExemptionState,
ReadonlySet<string>
>({
readSources: (state) => [state.sleepingAgentSessionsByPaneKey],
empty: EMPTY_TAB_IDS,
build: ({ sleepingAgentSessionsByPaneKey }, worktreeId) => {
if (!sleepingAgentSessionsByPaneKey) {
return EMPTY_TAB_IDS
}
let owned: Set<string> | null = null
for (const paneKey in sleepingAgentSessionsByPaneKey) {
const record = sleepingAgentSessionsByPaneKey[paneKey]
if (!record || record.worktreeId !== worktreeId) {
continue
}
if (activationTreatsNoteAsFinished(record)) {
continue
}
// Why: malformed pane keys must yield no owner instead of a truncated tab id.
const tabId =
record.tabId ??
parsePaneKey(record.paneKey)?.tabId ??
parseLegacyNumericPaneKey(record.paneKey)?.tabId
if (tabId) {
owned ??= new Set()
owned.add(tabId)
}
}
return owned ?? EMPTY_TAB_IDS
}
build: ({ sleepingAgentSessionsByPaneKey }, worktreeId) =>
sleepingAgentSessionsByPaneKey
? (exemptTabIdsByWorktree(sleepingAgentSessionsByPaneKey).get(worktreeId) ?? EMPTY_TAB_IDS)
: EMPTY_TAB_IDS
})