From 4e69d8a5bdeccff68e7c7c6df7c5f4252aa987d7 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:59:26 -0800 Subject: [PATCH] Construct presentation and sleeping record transitions at their writers --- .../agent-status-launch-actions.ts | 24 ++++----- .../agent-status/agent-status-live-reducer.ts | 20 +++----- .../agent-status-provider-session-actions.ts | 13 ++--- .../agent-status-recovery-actions.ts | 50 ++++++------------- .../agent-status-sleeping-records.ts | 18 +++---- .../terminals/terminal-tab-presentation.ts | 41 +++++---------- 6 files changed, 59 insertions(+), 107 deletions(-) diff --git a/src/renderer/src/store/slices/agent-status/agent-status-launch-actions.ts b/src/renderer/src/store/slices/agent-status/agent-status-launch-actions.ts index 25cfbecaacd..8f9fce3cf90 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-launch-actions.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-launch-actions.ts @@ -1,4 +1,4 @@ -import { annotateRecordReplacement } from '@/store/keyed-record-transitions' +import { replaceRecordEntries } from '@/store/keyed-record-transitions' import type { AgentStatusSlice } from './agent-status-slice-contract' import type { AgentStatusRuntime } from './agent-status-runtime' import type { AgentLaunchConfigRegistryEntry } from './agent-status-contract' @@ -63,13 +63,15 @@ export function createAgentStatusLaunchActions( }) : null if (refreshedRecord) { - nextSleepingAgentSessions = { - ...s.sleepingAgentSessionsByPaneKey, - [paneKey]: { - ...refreshedRecord, - capturedAt: existingSleepingRecord.capturedAt - } - } + nextSleepingAgentSessions = replaceRecordEntries(s.sleepingAgentSessionsByPaneKey, [ + [ + paneKey, + { + ...refreshedRecord, + capturedAt: existingSleepingRecord.capturedAt + } + ] + ]) } } if (!registryChanged && nextSleepingAgentSessions === s.sleepingAgentSessionsByPaneKey) { @@ -86,11 +88,7 @@ export function createAgentStatusLaunchActions( : {}), ...(nextSleepingAgentSessions !== s.sleepingAgentSessionsByPaneKey ? { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( - s.sleepingAgentSessionsByPaneKey, - nextSleepingAgentSessions, - [paneKey] - ) + sleepingAgentSessionsByPaneKey: nextSleepingAgentSessions } : {}) } diff --git a/src/renderer/src/store/slices/agent-status/agent-status-live-reducer.ts b/src/renderer/src/store/slices/agent-status/agent-status-live-reducer.ts index 1e66877be3f..fcccc9a42c2 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-live-reducer.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-live-reducer.ts @@ -1,4 +1,4 @@ -import { annotateRecordReplacement } from '@/store/keyed-record-transitions' +import { replaceRecordEntries } from '@/store/keyed-record-transitions' import { projectAgentPaneConversation } from './agent-pane-conversation' import type { AppState } from '../../types' import type { AgentStatusEntry } from '../../../../../shared/agent-status/contracts/agent-status-types' @@ -77,13 +77,13 @@ export function reduceAgentStatusLiveUpdate( } const recordChange = liveResumeRecordChange(existingSleepingRecord, liveRecoveryRecord) if (recordChange === 'clear') { - nextSleepingAgentSessions = { ...state.sleepingAgentSessionsByPaneKey } - delete nextSleepingAgentSessions[paneKey] + nextSleepingAgentSessions = replaceRecordEntries(state.sleepingAgentSessionsByPaneKey, [ + [paneKey, null] + ]) } else if (recordChange) { - nextSleepingAgentSessions = { - ...state.sleepingAgentSessionsByPaneKey, - [paneKey]: recordChange.set - } + nextSleepingAgentSessions = replaceRecordEntries(state.sleepingAgentSessionsByPaneKey, [ + [paneKey, recordChange.set] + ]) } const previousLive = state.agentStatusByPaneKey const nextLive = { ...previousLive, [paneKey]: entry } @@ -117,11 +117,7 @@ export function reduceAgentStatusLiveUpdate( ? { agentConversationsByPaneKey: nextConversations } : {}), retainedAgentsByPaneKey: nextRetainedAgents, - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( - state.sleepingAgentSessionsByPaneKey, - nextSleepingAgentSessions, - [paneKey, ...evictedPaneKeys] - ), + sleepingAgentSessionsByPaneKey: nextSleepingAgentSessions, agentLaunchConfigByPaneKey: nextLaunchConfigs, migrationUnsupportedByPtyId: migrationUnsupported.next, retentionSuppressedPaneKeys: nextRetentionSuppressedPaneKeys, diff --git a/src/renderer/src/store/slices/agent-status/agent-status-provider-session-actions.ts b/src/renderer/src/store/slices/agent-status/agent-status-provider-session-actions.ts index e862f581a7d..67e98d8c511 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-provider-session-actions.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-provider-session-actions.ts @@ -1,4 +1,4 @@ -import { annotateRecordReplacement } from '@/store/keyed-record-transitions' +import { replaceRecordEntries } from '@/store/keyed-record-transitions' import type { AgentStatusSlice } from './agent-status-slice-contract' import type { AgentStatusRuntime } from './agent-status-runtime' import type { @@ -156,14 +156,9 @@ export function createAgentStatusProviderSessionActions( ? { agentConversationsByPaneKey: nextConversations } : {}), retainedAgentsByPaneKey: nextRetained, - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( - s.sleepingAgentSessionsByPaneKey, - { - ...s.sleepingAgentSessionsByPaneKey, - [paneKey]: record - }, - [paneKey] - ), + sleepingAgentSessionsByPaneKey: replaceRecordEntries(s.sleepingAgentSessionsByPaneKey, [ + [paneKey, record] + ]), agentLaunchConfigByPaneKey: nextLaunchConfigs, acknowledgedAgentsByPaneKey: removePaneKeys( s.acknowledgedAgentsByPaneKey, diff --git a/src/renderer/src/store/slices/agent-status/agent-status-recovery-actions.ts b/src/renderer/src/store/slices/agent-status/agent-status-recovery-actions.ts index 1371b58d2dc..aa4d404f929 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-recovery-actions.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-recovery-actions.ts @@ -1,4 +1,4 @@ -import { annotateRecordReplacement } from '@/store/keyed-record-transitions' +import { replaceRecordEntries } from '@/store/keyed-record-transitions' import { sleepingAgentRecordIndex } from '../../terminals/sleeping-agent-record-index' import type { SleepingAgentSessionRecord } from '../../../../../shared/agent-session/lifecycle/agent-session-resume' @@ -36,21 +36,17 @@ export function createAgentStatusRecoveryActions( paneKeys, records ) - let next = { ...replaced.records } + const updates = new Map() let changed = replaced.changed for (const record of Object.values(records)) { - if (next[record.paneKey] !== record) { - next[record.paneKey] = record + if ((updates.get(record.paneKey) ?? replaced.records[record.paneKey]) !== record) { + updates.set(record.paneKey, record) changed = true } } return changed ? { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( - replaced.records, - next, - Object.keys(records) - ) + sleepingAgentSessionsByPaneKey: replaceRecordEntries(replaced.records, updates) } : s }) @@ -59,14 +55,12 @@ export function createAgentStatusRecoveryActions( captureAllSleepingAgentSessions: (mode) => { set((s) => { const capturedAt = Date.now() - const next: Record = { - ...s.sleepingAgentSessionsByPaneKey - } + const updates = new Map() let changed = false for (const entry of Object.values(s.agentStatusByPaneKey)) { const record = capturedResumeRecord({ entry, - existing: next[entry.paneKey], + existing: updates.get(entry.paneKey) ?? s.sleepingAgentSessionsByPaneKey[entry.paneKey], mode, capturedAt, worktreeIdOf: () => entry.worktreeId ?? findAgentPaneWorktreeId(s, entry.paneKey), @@ -74,16 +68,15 @@ export function createAgentStatusRecoveryActions( launchConfigOf: () => getLaunchConfigForEntry(s, entry) }) if (record) { - next[record.paneKey] = record + updates.set(record.paneKey, record) changed = true } } return changed ? { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( + sleepingAgentSessionsByPaneKey: replaceRecordEntries( s.sleepingAgentSessionsByPaneKey, - next, - Object.keys(s.agentStatusByPaneKey) + updates ) } : s @@ -97,13 +90,9 @@ export function createAgentStatusRecoveryActions( s.sleepingAgentSessionsByPaneKey[record.paneKey] !== replaces ? s : { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( + sleepingAgentSessionsByPaneKey: replaceRecordEntries( s.sleepingAgentSessionsByPaneKey, - { - ...s.sleepingAgentSessionsByPaneKey, - [record.paneKey]: record - }, - [record.paneKey] + [[record.paneKey, record]] ) } ) @@ -113,7 +102,6 @@ export function createAgentStatusRecoveryActions( clearSleepingAgentSessionsByWorktree: (worktreeId) => { set((s) => { let changed = false - const next = { ...s.sleepingAgentSessionsByPaneKey } const removed: string[] = [] for (const [paneKey, record] of sleepingAgentRecordIndex.entries( s.sleepingAgentSessionsByPaneKey, @@ -122,7 +110,6 @@ export function createAgentStatusRecoveryActions( if (record.worktreeId === worktreeId) { changed = true removed.push(paneKey) - delete next[paneKey] } } if (!changed) { @@ -134,10 +121,9 @@ export function createAgentStatusRecoveryActions( delete nextLaunch[paneKey] } return { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( + sleepingAgentSessionsByPaneKey: replaceRecordEntries( s.sleepingAgentSessionsByPaneKey, - next, - removed + removed.map((key) => [key, null] as const) ), ...(nextLaunch !== s.agentLaunchConfigByPaneKey ? { agentLaunchConfigByPaneKey: nextLaunch } @@ -149,14 +135,11 @@ export function createAgentStatusRecoveryActions( pruneSleepingAgentSessions: (validWorktreeIds) => { set((s) => { let changed = false - const next: Record = {} const removed: string[] = [] for (const [paneKey, record] of Object.entries(s.sleepingAgentSessionsByPaneKey)) { if (!validWorktreeIds.has(record.worktreeId)) { changed = true removed.push(paneKey) - } else { - next[paneKey] = record } } if (!changed) { @@ -168,10 +151,9 @@ export function createAgentStatusRecoveryActions( delete nextLaunch[paneKey] } return { - sleepingAgentSessionsByPaneKey: annotateRecordReplacement( + sleepingAgentSessionsByPaneKey: replaceRecordEntries( s.sleepingAgentSessionsByPaneKey, - next, - removed + removed.map((key) => [key, null] as const) ), ...(nextLaunch !== s.agentLaunchConfigByPaneKey ? { agentLaunchConfigByPaneKey: nextLaunch } diff --git a/src/renderer/src/store/slices/agent-status/agent-status-sleeping-records.ts b/src/renderer/src/store/slices/agent-status/agent-status-sleeping-records.ts index 79eb7a45ee1..7dba67cdcb6 100644 --- a/src/renderer/src/store/slices/agent-status/agent-status-sleeping-records.ts +++ b/src/renderer/src/store/slices/agent-status/agent-status-sleeping-records.ts @@ -1,4 +1,4 @@ -import { annotateRecordReplacement } from '@/store/keyed-record-transitions' +import { replaceRecordEntries } from '@/store/keyed-record-transitions' import { sleepingAgentRecordIndex } from '../../terminals/sleeping-agent-record-index' import type { AppState } from '../../types' import type { AgentStatusEntry } from '../../../../../shared/agent-status/contracts/agent-status-types' @@ -78,9 +78,7 @@ export function removeSleepingRecordsReplacedByManualWorktreeSleep( replacements?: Readonly> ): { records: Record; changed: boolean } { const allowedPaneKeys = paneKeys ? new Set(paneKeys) : null - let next = records - let changed = false - const removed = new Set() + const deletions: [string, null][] = [] for (const [paneKey, record] of sleepingAgentRecordIndex.entries(records, worktreeId)) { if (record.worktreeId !== worktreeId || (allowedPaneKeys && !allowedPaneKeys.has(paneKey))) { continue @@ -89,12 +87,10 @@ export function removeSleepingRecordsReplacedByManualWorktreeSleep( if (!replacements?.[paneKey] && isDurableSleepingCapture(record)) { continue } - if (next === records) { - next = { ...records } - } - delete next[paneKey] - removed.add(paneKey) - changed = true + deletions.push([paneKey, null]) + } + return { + records: deletions.length > 0 ? replaceRecordEntries(records, deletions) : records, + changed: deletions.length > 0 } - return { records: annotateRecordReplacement(records, next, removed), changed } } diff --git a/src/renderer/src/store/terminals/terminal-tab-presentation.ts b/src/renderer/src/store/terminals/terminal-tab-presentation.ts index 350fa9a1358..1ad069bd654 100644 --- a/src/renderer/src/store/terminals/terminal-tab-presentation.ts +++ b/src/renderer/src/store/terminals/terminal-tab-presentation.ts @@ -1,7 +1,4 @@ -import { - annotateTabBucketReplacement, - annotateRecordReplacement -} from '@/store/keyed-record-transitions' +import { replaceTabBuckets, replaceRecordEntries } from '@/store/keyed-record-transitions' import { isDecorativeAgentTitleFrameChange } from '../../../../shared/agent-status/title/agent-decorative-title-signature' import { scheduleRuntimeGraphSync } from '@/runtime/sync-runtime-graph/graph-sync-scheduler' import { classifyTitleActivity } from '@/lib/pane-agent-evidence' @@ -65,7 +62,9 @@ export function createTerminalTabPresentationActions( return s } const ownerTabs = tabs.map((tab) => (tab.id === tabId ? { ...tab, aiVaultTitle } : tab)) - const nextTabsByWorktree = { ...s.tabsByWorktree, [ownerWorktreeId]: ownerTabs } + const nextTabsByWorktree = replaceTabBuckets(s.tabsByWorktree, [ + [ownerWorktreeId, ownerTabs] + ]) const unifiedTabs = s.unifiedTabsByWorktree[ownerWorktreeId] ?? [] const nextUnifiedTabs = unifiedTabs.map((tab) => tab.contentType === 'terminal' && tab.entityId === tabId ? { ...tab, aiVaultTitle } : tab @@ -76,11 +75,9 @@ export function createTerminalTabPresentationActions( scheduleRuntimeGraphSync() return { tabsByWorktree: nextTabsByWorktree, - unifiedTabsByWorktree: annotateTabBucketReplacement( - s.unifiedTabsByWorktree, - { ...s.unifiedTabsByWorktree, [ownerWorktreeId]: nextUnifiedTabs }, - [ownerWorktreeId] - ) + unifiedTabsByWorktree: replaceTabBuckets(s.unifiedTabsByWorktree, [ + [ownerWorktreeId, nextUnifiedTabs] + ]) } }) }, @@ -142,11 +139,7 @@ export function createTerminalTabPresentationActions( nextTabs[tabIndex] = tabWithoutLaunchAgent scheduleRuntimeGraphSync() return { - tabsByWorktree: annotateTabBucketReplacement( - s.tabsByWorktree, - { ...s.tabsByWorktree, [ownerWorktreeId]: nextTabs }, - [ownerWorktreeId] - ) + tabsByWorktree: replaceTabBuckets(s.tabsByWorktree, [[ownerWorktreeId, nextTabs]]) } }) }, @@ -170,11 +163,9 @@ export function createTerminalTabPresentationActions( const isActive = ownerWorktreeId !== null && ownerWorktreeId === s.activeWorktreeId const shouldBump = classificationChanged && ownerWorktreeId !== null && !isActive return { - runtimePaneTitlesByTabId: annotateRecordReplacement( - s.runtimePaneTitlesByTabId, - { ...s.runtimePaneTitlesByTabId, [tabId]: { ...currentByPane, [paneId]: title } }, - [tabId] - ), + runtimePaneTitlesByTabId: replaceRecordEntries(s.runtimePaneTitlesByTabId, [ + [tabId, { ...currentByPane, [paneId]: title }] + ]), ...(shouldBump ? { sortEpoch: s.sortEpoch + 1 } : {}) } }) @@ -188,12 +179,6 @@ export function createTerminalTabPresentationActions( const prevTitle = currentByPane[paneId] const nextByPane = { ...currentByPane } delete nextByPane[paneId] - const next = { ...s.runtimePaneTitlesByTabId } - if (Object.keys(nextByPane).length > 0) { - next[tabId] = nextByPane - } else { - delete next[tabId] - } // Clearing a classified title changes smart-sort’s verdict, like setRuntimePaneTitle. const hadClassification = classifyTitleActivity(prevTitle ?? '') !== null // Click-driven teardown must not re-rank active worktrees; skip orphaned owners too. @@ -203,8 +188,8 @@ export function createTerminalTabPresentationActions( const isActive = ownerWorktreeId !== null && ownerWorktreeId === s.activeWorktreeId const shouldBump = hadClassification && ownerWorktreeId !== null && !isActive return { - runtimePaneTitlesByTabId: annotateRecordReplacement(s.runtimePaneTitlesByTabId, next, [ - tabId + runtimePaneTitlesByTabId: replaceRecordEntries(s.runtimePaneTitlesByTabId, [ + [tabId, Object.keys(nextByPane).length > 0 ? nextByPane : null] ]), ...(shouldBump ? { sortEpoch: s.sortEpoch + 1 } : {}) }