Construct presentation and sleeping record transitions at their writers

This commit is contained in:
Brennan Benson
2026-11-09 03:33:20 -08:00
parent de4cec17fe
commit 4e69d8a5bd
6 changed files with 59 additions and 107 deletions
@@ -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
}
: {})
}
@@ -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,
@@ -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,
@@ -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<string, SleepingAgentSessionRecord>()
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<string, SleepingAgentSessionRecord> = {
...s.sleepingAgentSessionsByPaneKey
}
const updates = new Map<string, SleepingAgentSessionRecord>()
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<string, SleepingAgentSessionRecord> = {}
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 }
@@ -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<Record<string, SleepingAgentSessionRecord>>
): { records: Record<string, SleepingAgentSessionRecord>; changed: boolean } {
const allowedPaneKeys = paneKeys ? new Set(paneKeys) : null
let next = records
let changed = false
const removed = new Set<string>()
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 }
}
@@ -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 } : {})
}