Reduce terminal PTY state churn (#2185)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-05-17 18:23:56 -07:00
committed by GitHub
co-authored by Orca
parent 21000624a8
commit caabf741fd
2 changed files with 169 additions and 64 deletions
@@ -700,6 +700,111 @@ describe('terminal slice behaviors', () => {
expect(store.getState().ptyIdsByTabId['tab-1']).toEqual(['pty-1', 'pty-2'])
})
it('preserves unrelated worktree tab arrays when recording a spawned PTY', () => {
const store = createTestStore()
const targetWorktreeId = 'repo1::/path/wt1'
const otherWorktreeId = 'repo1::/path/wt2'
store.setState({
repos: [
{ id: 'repo1', path: '/repo1', displayName: 'Repo 1', badgeColor: '#000', addedAt: 0 }
],
worktreesByRepo: {
repo1: [
makeWorktree({ id: targetWorktreeId, repoId: 'repo1', path: '/path/wt1' }),
makeWorktree({ id: otherWorktreeId, repoId: 'repo1', path: '/path/wt2' })
]
},
tabsByWorktree: {
[targetWorktreeId]: [makeTab({ id: 'tab-1', worktreeId: targetWorktreeId })],
[otherWorktreeId]: [makeTab({ id: 'tab-2', worktreeId: otherWorktreeId })]
}
})
const before = store.getState().tabsByWorktree
const beforeOtherTabs = before[otherWorktreeId]
store.getState().updateTabPtyId('tab-1', 'pty-fresh')
const after = store.getState().tabsByWorktree
expect(after[targetWorktreeId]).not.toBe(before[targetWorktreeId])
expect(after[otherWorktreeId]).toBe(beforeOtherTabs)
})
it('preserves unrelated worktree tab arrays when clearing a PTY', () => {
const store = createTestStore()
const targetWorktreeId = 'repo1::/path/wt1'
const otherWorktreeId = 'repo1::/path/wt2'
store.setState({
repos: [
{ id: 'repo1', path: '/repo1', displayName: 'Repo 1', badgeColor: '#000', addedAt: 0 }
],
worktreesByRepo: {
repo1: [
makeWorktree({ id: targetWorktreeId, repoId: 'repo1', path: '/path/wt1' }),
makeWorktree({ id: otherWorktreeId, repoId: 'repo1', path: '/path/wt2' })
]
},
tabsByWorktree: {
[targetWorktreeId]: [
makeTab({ id: 'tab-1', worktreeId: targetWorktreeId, ptyId: 'pty-fresh' })
],
[otherWorktreeId]: [makeTab({ id: 'tab-2', worktreeId: otherWorktreeId })]
},
ptyIdsByTabId: {
'tab-1': ['pty-fresh']
}
})
const before = store.getState().tabsByWorktree
const beforeOtherTabs = before[otherWorktreeId]
store.getState().clearTabPtyId('tab-1', 'pty-fresh')
const after = store.getState().tabsByWorktree
expect(after[targetWorktreeId]).not.toBe(before[targetWorktreeId])
expect(after[otherWorktreeId]).toBe(beforeOtherTabs)
})
it('changes only the owning worktree tab array when recording a PTY in a large session', () => {
const store = createTestStore()
const worktreeCount = 125
const targetIndex = 73
const worktrees = Array.from({ length: worktreeCount }, (_, index) =>
makeWorktree({
id: `repo1::/path/wt-${index}`,
repoId: 'repo1',
path: `/path/wt-${index}`
})
)
const tabsByWorktree = Object.fromEntries(
worktrees.map((worktree, index) => [
worktree.id,
[makeTab({ id: `tab-${index}`, worktreeId: worktree.id })]
])
)
store.setState({
repos: [
{ id: 'repo1', path: '/repo1', displayName: 'Repo 1', badgeColor: '#000', addedAt: 0 }
],
worktreesByRepo: { repo1: worktrees },
tabsByWorktree
})
const targetTabId = `tab-${targetIndex}`
const before = store.getState().tabsByWorktree
store.getState().updateTabPtyId(targetTabId, 'pty-fresh')
const after = store.getState().tabsByWorktree
const changedWorktreeIds = Object.keys(after).filter(
(worktreeId) => after[worktreeId] !== before[worktreeId]
)
expect(changedWorktreeIds).toEqual([`repo1::/path/wt-${targetIndex}`])
})
// Why: clicking a worktree in the sidebar triggers a generation bump on
// dead-PTY tabs which remounts TerminalPane and fresh-spawns a PTY. That
// fresh spawn calls updateTabPtyId → bumpWorktreeActivity. Without the
+64 -64
View File
@@ -995,42 +995,41 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
let worktreeId: string | null = null
let wasActivationSpawn = false
set((s) => {
const next = { ...s.tabsByWorktree }
for (const wId of Object.keys(next)) {
const found = next[wId].some((t) => t.id === tabId)
if (found) {
worktreeId = wId
}
next[wId] = next[wId].map((t) => {
if (t.id !== tabId) {
return t
}
const existingPtyIds = s.ptyIdsByTabId[tabId] ?? []
const nextPtyIds = existingPtyIds.includes(ptyId)
? existingPtyIds
: [...existingPtyIds, ptyId]
if (t.pendingActivationSpawn) {
wasActivationSpawn = true
}
// Why: consume pendingActivationSpawn here. The flag is set by
// setActiveWorktree when it bumps generation on all-dead tabs, and
// must be cleared on the first PTY that comes back or a later
// legitimate respawn (e.g. the user restarting a codex tab) would
// also be classified as activation and silently dropped from the
// recency sort.
const { pendingActivationSpawn: _unused, ...rest } = t
void _unused
return {
...rest,
// Why: tab.ptyId is the single-pane fallback used by legacy attach
// paths. In split panes, later pane spawns must not steal that
// primary binding from the original pane or remount/close flows can
// reattach the tab to the wrong PTY and appear to "reset" panes.
ptyId: t.ptyId ?? nextPtyIds[0] ?? null
}
})
}
const existingPtyIds = s.ptyIdsByTabId[tabId] ?? []
const nextPtyIds = existingPtyIds.includes(ptyId)
? existingPtyIds
: [...existingPtyIds, ptyId]
let nextTabsByWorktree = s.tabsByWorktree
for (const [wId, tabs] of Object.entries(s.tabsByWorktree)) {
const index = tabs.findIndex((t) => t.id === tabId)
if (index === -1) {
continue
}
worktreeId = wId
const tab = tabs[index]
if (tab.pendingActivationSpawn) {
wasActivationSpawn = true
}
// Why: consume pendingActivationSpawn here. The flag is set by
// setActiveWorktree when it bumps generation on all-dead tabs, and
// must be cleared on the first PTY that comes back or a later
// legitimate respawn (e.g. the user restarting a codex tab) would
// also be classified as activation and silently dropped from the
// recency sort.
const { pendingActivationSpawn: _unused, ...rest } = tab
void _unused
// Why: tab.ptyId is the single-pane fallback used by legacy attach
// paths. In split panes, later pane spawns must not steal that
// primary binding from the original pane or remount/close flows can
// reattach the tab to the wrong PTY and appear to "reset" panes.
const nextTabPtyId = tab.ptyId ?? nextPtyIds[0] ?? null
if (tab.pendingActivationSpawn || tab.ptyId !== nextTabPtyId) {
const nextTabs = [...tabs]
nextTabs[index] = { ...rest, ptyId: nextTabPtyId }
nextTabsByWorktree = { ...s.tabsByWorktree, [wId]: nextTabs }
}
break
}
// Why: when a brand-new tab in the active worktree receives its first
// PTY, the live-tab signal (+12) flips on. Normally we bump sortEpoch
// here so the sort reflects the new signal immediately. Suppress the
@@ -1041,10 +1040,10 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
const isActiveWorktree = worktreeId != null && s.activeWorktreeId === worktreeId
const shouldBumpSortEpoch = isFirstPty && isActiveWorktree && !wasActivationSpawn
return {
tabsByWorktree: next,
...(nextTabsByWorktree !== s.tabsByWorktree ? { tabsByWorktree: nextTabsByWorktree } : {}),
ptyIdsByTabId: {
...s.ptyIdsByTabId,
[tabId]: existingPtyIds.includes(ptyId) ? existingPtyIds : [...existingPtyIds, ptyId]
[tabId]: nextPtyIds
},
lastKnownRelayPtyIdByTabId: {
...s.lastKnownRelayPtyIdByTabId,
@@ -1068,35 +1067,36 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
let worktreeId: string | null = null
let wasActivationSpawn = false
set((s) => {
const next = { ...s.tabsByWorktree }
for (const wId of Object.keys(next)) {
if (next[wId].some((t) => t.id === tabId)) {
worktreeId = wId
const existingPtyIds = s.ptyIdsByTabId[tabId] ?? []
const remainingPtyIds = ptyId ? existingPtyIds.filter((id) => id !== ptyId) : []
let nextTabsByWorktree = s.tabsByWorktree
for (const [wId, tabs] of Object.entries(s.tabsByWorktree)) {
const index = tabs.findIndex((t) => t.id === tabId)
if (index === -1) {
continue
}
next[wId] = next[wId].map((t) => {
if (t.id !== tabId) {
return t
}
if (t.pendingActivationSpawn) {
wasActivationSpawn = true
}
const remainingPtyIds = ptyId
? (s.ptyIdsByTabId[tabId] ?? []).filter((id) => id !== ptyId)
: []
// Why: consume pendingActivationSpawn here too. Panes tearing down
// during a worktree switch (e.g. the previously-active worktree
// unmounting its panes) fire onExit → clearTabPtyId, which must
// not count as activity. Strip the flag on consumption so later
// legitimate exits still bump.
const { pendingActivationSpawn: _unused, ...rest } = t
void _unused
return { ...rest, ptyId: remainingPtyIds.at(-1) ?? null }
})
worktreeId = wId
const tab = tabs[index]
if (tab.pendingActivationSpawn) {
wasActivationSpawn = true
}
// Why: consume pendingActivationSpawn here too. Panes tearing down
// during a worktree switch (e.g. the previously-active worktree
// unmounting its panes) fire onExit → clearTabPtyId, which must
// not count as activity. Strip the flag on consumption so later
// legitimate exits still bump.
const { pendingActivationSpawn: _unused, ...rest } = tab
void _unused
const nextTabPtyId = remainingPtyIds.at(-1) ?? null
if (tab.pendingActivationSpawn || tab.ptyId !== nextTabPtyId) {
const nextTabs = [...tabs]
nextTabs[index] = { ...rest, ptyId: nextTabPtyId }
nextTabsByWorktree = { ...s.tabsByWorktree, [wId]: nextTabs }
}
break
}
const nextPtyIdsByTabId = { ...s.ptyIdsByTabId }
nextPtyIdsByTabId[tabId] = ptyId
? (nextPtyIdsByTabId[tabId] ?? []).filter((id) => id !== ptyId)
: []
nextPtyIdsByTabId[tabId] = remainingPtyIds
const nextPendingCodexPaneRestartIds = { ...s.pendingCodexPaneRestartIds }
const nextCodexRestartNoticeByPtyId = { ...s.codexRestartNoticeByPtyId }
if (ptyId) {
@@ -1119,7 +1119,7 @@ export const createTerminalSlice: StateCreator<AppState, [], [], TerminalSlice>
}
return {
tabsByWorktree: next,
...(nextTabsByWorktree !== s.tabsByWorktree ? { tabsByWorktree: nextTabsByWorktree } : {}),
ptyIdsByTabId: nextPtyIdsByTabId,
lastKnownRelayPtyIdByTabId: nextLastKnownRelay,
pendingCodexPaneRestartIds: nextPendingCodexPaneRestartIds,