perf: defer scrollback pruning indexes until needed (#20297)

* perf: defer scrollback pruning indexes until needed

* test: wait for watcher reconciliation event delivery

---------

Co-authored-by: Orca Worker <orca-worker@localhost>
This commit is contained in:
OrcaWin
2026-09-12 18:08:31 -07:00
committed by GitHub
co-authored by Orca Worker
parent 25939947e9
commit 37a5b3798b
2 changed files with 12 additions and 9 deletions
@@ -626,8 +626,8 @@ describe('worktree git-common narrow watch (local native platforms)', () => {
expect(statCalls.filter((path) => path === worktreesDir)).toHaveLength(1)
await vi.waitFor(() => {
expect(subscribeMock).toHaveBeenCalledTimes(2)
expect(received.flat()).toContainEqual({ type: 'create', path: worktreesDir })
})
expect(received.flat()).toContainEqual({ type: 'create', path: worktreesDir })
})
it('resumes polling when the dir is still absent on show', async () => {
@@ -77,21 +77,24 @@ export function pruneLocalTerminalScrollbackBuffers(
session: WorkspaceSessionState,
repos: readonly RepoConnection[]
): WorkspaceSessionState {
const repoById = new Map(repos.map((repo) => [repo.id, repo] as const))
const worktreeIdByTabId = new Map<string, string>()
let repoById: Map<string, RepoConnection> | null = null
let worktreeIdByTabId: Map<string, string> | null = null
const tabsByWorktree = session.tabsByWorktree ?? {}
const terminalLayoutsByTabIdForRead = session.terminalLayoutsByTabId ?? {}
for (const [worktreeId, tabs] of Object.entries(tabsByWorktree)) {
for (const tab of tabs) {
worktreeIdByTabId.set(tab.id, worktreeId)
}
}
let terminalLayoutsByTabId: WorkspaceSessionState['terminalLayoutsByTabId'] | null = null
for (const [tabId, layout] of Object.entries(terminalLayoutsByTabIdForRead)) {
if (!layout.buffersByLeafId && !layout.scrollbackRefsByLeafId) {
continue
}
repoById ??= new Map(repos.map((repo) => [repo.id, repo] as const))
if (!worktreeIdByTabId) {
worktreeIdByTabId = new Map()
for (const [worktreeId, tabs] of Object.entries(tabsByWorktree)) {
for (const tab of tabs) {
worktreeIdByTabId.set(tab.id, worktreeId)
}
}
}
const worktreeId = worktreeIdByTabId.get(tabId)
if (shouldPreserveTerminalScrollbackBuffersForRepoMap(worktreeId, repoById)) {
const capped = capTerminalScrollbackLeafBuffers(layout.buffersByLeafId)