perf(renderer): scope sidebar agent freshness by worktree (#8052)

This commit is contained in:
Neil
2026-07-10 20:51:15 -07:00
committed by GitHub
parent 5e100914d3
commit fadb3f3730
3 changed files with 172 additions and 9 deletions
@@ -16,6 +16,10 @@ import {
selectRetainedAgentEntriesForWorktree,
selectTerminalLayoutsForWorktree
} from './worktree-agent-row-selectors'
import {
createWorktreeAgentFreshnessSelector,
EMPTY_WORKTREE_AGENT_FRESHNESS_SIGNATURE
} from './worktree-agent-freshness-selector'
export { buildWorktreeAgentRows } from './worktree-agent-rows'
export {
@@ -36,6 +40,10 @@ export {
* selector work on high-frequency agent status pings.
*/
export function useWorktreeAgentRows(worktreeId: string, active = true): DashboardAgentRow[] {
const selectAgentFreshness = useMemo(
() => createWorktreeAgentFreshnessSelector(worktreeId),
[worktreeId]
)
const tabs = useAppStore((s) => (active ? s.tabsByWorktree[worktreeId] : undefined))
// Why: narrow the subscriptions to only THIS worktree's entries via
// useShallow. Subscribing to the whole agentStatusByPaneKey map would make
@@ -66,19 +74,16 @@ export function useWorktreeAgentRows(worktreeId: string, active = true): Dashboa
const runtimeAgentOrchestrationByPaneKey = useAppStore(
useShallow((s) => (active ? selectRuntimeAgentOrchestrationForWorktree(s, worktreeId) : {}))
)
// Why: agentStatusEpoch is included in the dependency array (but not in the
// computation itself) so the memo recomputes when freshness boundaries
// expire, even if no new PTY data arrives — same rationale as
// useDashboardData.
const agentStatusEpoch = useAppStore((s) => (active ? s.agentStatusEpoch : 0))
const agentFreshnessSignature = useAppStore((s) =>
active ? selectAgentFreshness(s) : EMPTY_WORKTREE_AGENT_FRESHNESS_SIGNATURE
)
return useMemo<DashboardAgentRow[]>(() => {
if (!active) {
return []
}
// Why: Date.now() is read inside the memo (not as a dep) so stale-decay
// recalculates whenever agentStatusEpoch ticks — same pattern as
// useDashboardData.
// Why: Date.now() is read inside the memo so stale-decay recalculates when
// this worktree's freshness signature changes, even without new PTY data.
const now = Date.now()
const entries =
migrationUnsupported.length > 0
@@ -113,6 +118,6 @@ export function useWorktreeAgentRows(worktreeId: string, active = true): Dashboa
ptyIdsByTabId,
terminalLayoutsByTabId,
runtimeAgentOrchestrationByPaneKey,
agentStatusEpoch
agentFreshnessSignature
])
}
@@ -0,0 +1,105 @@
import { describe, expect, it, vi } from 'vitest'
import {
AGENT_STATUS_STALE_AFTER_MS,
type AgentStatusEntry
} from '../../../../shared/agent-status-types'
import { makePaneKey } from '../../../../shared/stable-pane-id'
import type { TerminalTab } from '../../../../shared/types'
import { createWorktreeAgentFreshnessSelector } from './worktree-agent-freshness-selector'
const WT_1_PANE = makePaneKey('tab-1', '11111111-1111-4111-8111-111111111111')
const WT_2_PANE = makePaneKey('tab-2', '22222222-2222-4222-8222-222222222222')
function makeTab(id: string, worktreeId: string): TerminalTab {
return {
id,
worktreeId,
ptyId: null,
title: 'Claude',
customTitle: null,
color: null,
sortOrder: 0,
createdAt: 0
}
}
function makeEntry(paneKey: string, worktreeId: string, updatedAt: number): AgentStatusEntry {
return {
paneKey,
worktreeId,
state: 'working',
stateStartedAt: updatedAt,
updatedAt,
stateHistory: [],
prompt: 'working',
agentType: 'claude'
}
}
function makeState(agentStatusEpoch: number, wt1UpdatedAt: number, wt2UpdatedAt: number) {
return {
agentStatusEpoch,
tabsByWorktree: {
'wt-1': [makeTab('tab-1', 'wt-1')],
'wt-2': [makeTab('tab-2', 'wt-2')]
},
agentStatusByPaneKey: {
[WT_1_PANE]: makeEntry(WT_1_PANE, 'wt-1', wt1UpdatedAt),
[WT_2_PANE]: makeEntry(WT_2_PANE, 'wt-2', wt2UpdatedAt)
},
migrationUnsupportedByPtyId: {},
retainedAgentsByPaneKey: {}
}
}
describe('createWorktreeAgentFreshnessSelector', () => {
it('changes only the worktree whose row crosses the stale boundary', () => {
let now = AGENT_STATUS_STALE_AFTER_MS - 1
const state = makeState(1, now - 1, 0)
const selectWt1 = createWorktreeAgentFreshnessSelector('wt-1', () => now)
const selectWt2 = createWorktreeAgentFreshnessSelector('wt-2', () => now)
const firstWt1 = selectWt1(state)
const firstWt2 = selectWt2(state)
now = AGENT_STATUS_STALE_AFTER_MS + 1
const nextState = { ...state, agentStatusEpoch: 2 }
// The global scheduler bumps one epoch for all cards. Zustand compares the
// selected string by value, so the unrelated card now skips its render.
expect(selectWt1(nextState)).toBe(firstWt1)
expect(selectWt2(nextState)).not.toBe(firstWt2)
})
it('does constant work for same-epoch status-map churn', () => {
const readNow = vi.fn(() => 1_000)
const selector = createWorktreeAgentFreshnessSelector('wt-1', readNow)
const state = makeState(3, 900, 900)
const first = selector(state)
const sameStatePing = {
...state,
agentStatusByPaneKey: {
...state.agentStatusByPaneKey,
[WT_2_PANE]: { ...state.agentStatusByPaneKey[WT_2_PANE], updatedAt: 950 }
}
}
expect(selector(sameStatePing)).toBe(first)
expect(readNow).toHaveBeenCalledTimes(1)
})
it.each(['working', 'blocked', 'waiting'] as const)(
'wakes a %s row when it becomes stale',
(state) => {
let now = AGENT_STATUS_STALE_AFTER_MS - 1
const initial = makeState(10, 0, now)
initial.agentStatusByPaneKey[WT_1_PANE].state = state
const selector = createWorktreeAgentFreshnessSelector('wt-1', () => now)
const fresh = selector(initial)
now = AGENT_STATUS_STALE_AFTER_MS + 1
expect(selector({ ...initial, agentStatusEpoch: 11 })).not.toBe(fresh)
}
)
})
@@ -0,0 +1,53 @@
import { isExplicitAgentStatusFresh } from '@/lib/agent-status'
import type { AppState } from '@/store/types'
import { AGENT_STATUS_STALE_AFTER_MS } from '../../../../shared/agent-status-types'
import { selectLiveAgentStatusEntriesForWorktree } from './worktree-agent-row-selectors'
export const EMPTY_WORKTREE_AGENT_FRESHNESS_SIGNATURE = ''
type WorktreeAgentFreshnessState = Pick<
AppState,
| 'agentStatusByPaneKey'
| 'agentStatusEpoch'
| 'migrationUnsupportedByPtyId'
| 'retainedAgentsByPaneKey'
| 'tabsByWorktree'
>
function buildWorktreeAgentFreshnessSignature(
state: WorktreeAgentFreshnessState,
worktreeId: string,
now: number
): string {
let signature = ''
for (const entry of selectLiveAgentStatusEntriesForWorktree(state, worktreeId)) {
if (entry.state !== 'working' && entry.state !== 'blocked' && entry.state !== 'waiting') {
continue
}
signature += `${entry.paneKey}\0${
isExplicitAgentStatusFresh(entry, now, AGENT_STATUS_STALE_AFTER_MS) ? '1' : '0'
}\0`
}
return signature
}
export function createWorktreeAgentFreshnessSelector(
worktreeId: string,
readNow: () => number = Date.now
): (state: WorktreeAgentFreshnessState) => string {
let cachedGlobalEpoch: number | null = null
let cachedSignature = EMPTY_WORKTREE_AGENT_FRESHNESS_SIGNATURE
return (state) => {
// Why: the status slice bumps the epoch for state/freshness changes;
// same-epoch map writes are fresh same-state metadata and cannot alter decay.
if (cachedGlobalEpoch === state.agentStatusEpoch) {
return cachedSignature
}
cachedGlobalEpoch = state.agentStatusEpoch
// Why: the global epoch wakes every card, but only cards whose effective
// fresh/stale row state changed should render and rebuild their row trees.
cachedSignature = buildWorktreeAgentFreshnessSignature(state, worktreeId, readNow())
return cachedSignature
}
}