From 7dad69cfc286b07df2e85fec3814fae30260e4e3 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:21:03 -0800 Subject: [PATCH] Preserve unopened chat statuses during capacity eviction (#43061) --- .../agent-status-capacity-eviction.ts | 14 ++++++- .../agent-status-live-map-leak.test.ts | 41 +++++++++++++++++++ .../agent-status-projection-capacity.ts | 2 +- .../agent-status-projection-rows.ts | 2 +- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/renderer/src/store/slices/agent-status/agent-status-capacity-eviction.ts b/src/renderer/src/store/slices/agent-status/agent-status-capacity-eviction.ts index f28b94e2525..75bccad1496 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-capacity-eviction.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-capacity-eviction.ts @@ -1,3 +1,5 @@ +import type { Tab } from '../../../../../shared/workspace-layout/tab-groups/tab-types' +import { structuredAgentSessionPaneKey } from '../../../../../shared/agent-session/view/structured-agent-session-projection' import type { AppState } from '../../types' import type { AgentStatusEntry } from '../../../../../shared/agent-status/contracts/agent-status-types' import type { TerminalPaneLayoutNode } from '../../../../../shared/workspace-layout/terminal-tab-types' @@ -37,11 +39,19 @@ function rootHasLeaf(root: TerminalPaneLayoutNode, leafId: string): boolean { return leaves.has(leafId) } -// Only exact rooted leaves prove placement; legacy colon-containing owners still match full keys. +// Existing chat tabs and exact terminal leaves prove placement without mounting the workspace. export function classifyPaneKeyLiveness( - layouts: AppState['terminalLayoutsByTabId'] + layouts: AppState['terminalLayoutsByTabId'], + representedTabs?: Pick, 'get'> ): (paneKey: string) => PaneLiveness { return (paneKey) => { + const tab = representedTabs?.get(paneKey) + if ( + tab?.contentType === 'agent-session' && + structuredAgentSessionPaneKey(tab.id, tab.entityId) === paneKey + ) { + return 'live' + } for (const tabId of paneKeyCandidateTabIds(paneKey)) { const root = layouts[tabId]?.root if (root && rootHasLeaf(root, paneKey.slice(tabId.length + 1))) { diff --git a/src/renderer/src/store/slices/agent-status/agent-status-live-map-leak.test.ts b/src/renderer/src/store/slices/agent-status/agent-status-live-map-leak.test.ts index 1955ed3797a..0984e04749b 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-live-map-leak.test.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-live-map-leak.test.ts @@ -1,5 +1,6 @@ /** Regression for #9872: missed pane teardown must not let heavy live status rows grow unbounded. */ import { afterEach, describe, expect, it, vi } from 'vitest' +import type { Tab } from '../../../../../shared/workspace-layout/tab-groups/tab-types' import type { ParsedAgentStatusPayload } from '../../../../../shared/agent-status/contracts/agent-status-types' import { AGENT_STATUS_STALE_AFTER_MS } from '../../../../../shared/agent-status/contracts/agent-status-freshness' import type { SleepingAgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-resume' @@ -171,6 +172,46 @@ describe('agentStatusByPaneKey stays bounded (leak regression #9872)', () => { expect(live.get('tab-b:leaf-b')?.state).toBe('blocked') }) + it('keeps an unopened native chat visible when orphan statuses exceed capacity', () => { + const store = createTestStore() + seedWorktree(store) + store.setState({ + unifiedTabsByWorktree: { + 'wt-1': [ + { + id: 'chat-tab', + entityId: 'chat-session', + worktreeId: 'wt-1', + contentType: 'agent-session', + agentSessionAgent: 'claude', + label: 'Chat', + customLabel: null, + color: null, + groupId: 'group-1', + sortOrder: 0, + createdAt: 1 + } satisfies Tab + ] + } + }) + setAgentAt(store, 'chat-tab:fa0095aa-0b8f-4e0b-a035-f6c949783135', { + state: 'done', + prompt: 'Finished', + agentType: 'claude' + }) + + churnFreshUnprovable(store, MAX_LIVE_AGENT_STATUSES + 1) + + expect( + store.getState().agentStatusByPaneKey.get('chat-tab:fa0095aa-0b8f-4e0b-a035-f6c949783135') + ?.state + ).toBe('done') + expect(store.getState().agentStatusByPaneKey.projection.keys('wt-1')).toContain( + 'chat-tab:fa0095aa-0b8f-4e0b-a035-f6c949783135' + ) + expect(store.getState().agentStatusByPaneKey.get('gone-0:leaf-0')).toBeUndefined() + }) + it('bounds the map when fresh unprovable rows dominate, without evicting a live pane', () => { const store = createTestStore() seedWorktree(store) diff --git a/src/renderer/src/store/slices/agent-status/agent-status-projection-capacity.ts b/src/renderer/src/store/slices/agent-status/agent-status-projection-capacity.ts index cbc4577964f..580df4bde18 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-projection-capacity.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-projection-capacity.ts @@ -40,7 +40,7 @@ export function projectionEvictionKeys( } drafts.set(statuses, draft) } - const classify = classifyPaneKeyLiveness(layouts) + const classify = classifyPaneKeyLiveness(layouts, data.represented) for (const [key, row] of statuses.replacementsSince(draft.offset)) { draft.dead.delete(key) draft.fresh.delete(key) diff --git a/src/renderer/src/store/slices/agent-status/agent-status-projection-rows.ts b/src/renderer/src/store/slices/agent-status/agent-status-projection-rows.ts index 04d300008aa..d4ce885b081 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-projection-rows.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-projection-rows.ts @@ -76,7 +76,7 @@ export function stageProjectionRows( changedKeys.add(key) } const prepared = new Map(), - classifier = classifyPaneKeyLiveness(state.terminalLayoutsByTabId) + classifier = classifyPaneKeyLiveness(state.terminalLayoutsByTabId, represented) for (const key of changedKeys) { const accepted = state.agentStatusByPaneKey.get(key), entry = sourceEntry(key)