diff --git a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts index 5ffb2b35cff..1f1a6126d20 100644 --- a/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts +++ b/src/renderer/src/components/terminal-pane/parking/output-watchers/terminal-parked-watcher-reconciliation.ts @@ -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() + 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, diff --git a/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts b/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts index 5039efbd75e..6894156d452 100644 --- a/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts +++ b/src/renderer/src/components/terminal-pane/parking/retention/sleeping-record-park-exemption.ts @@ -12,36 +12,51 @@ type SleepingRecordParkExemptionState = { sleepingAgentSessionsByPaneKey?: Record } -/** 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 + byWorktreeId: ReadonlyMap> +} | 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 +): ReadonlyMap> { + if (exemptTabIdsByWorktreeCache?.records === records) { + return exemptTabIdsByWorktreeCache.byWorktreeId + } + const byWorktreeId = new Map>() + 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 >({ readSources: (state) => [state.sleepingAgentSessionsByPaneKey], empty: EMPTY_TAB_IDS, - build: ({ sleepingAgentSessionsByPaneKey }, worktreeId) => { - if (!sleepingAgentSessionsByPaneKey) { - return EMPTY_TAB_IDS - } - let owned: Set | 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 })