mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
Construct shutdown and orphan cleanup transitions from ordered operations
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
import {
|
||||
annotateRecordReplacement,
|
||||
annotateTabBucketReplacement
|
||||
} from '@/store/keyed-record-transitions'
|
||||
import { replaceRecordEntries, replaceTabBuckets } from '@/store/keyed-record-transitions'
|
||||
import { isRemoteRuntimePtyId } from '../../../../shared/remote-runtime/remote-runtime-pty-id'
|
||||
import { parsePaneKey } from '../../../../shared/workspace-layout/stable-pane-id'
|
||||
import '@/components/terminal-pane/pty-transport/delivery/pty-dispatcher'
|
||||
@@ -113,21 +110,11 @@ export function createTerminalPaneHibernationActions(
|
||||
for (const ptyId of exitGuardPtyIds) {
|
||||
delete next[ptyId]
|
||||
}
|
||||
const nextSleeping = { ...s.sleepingAgentSessionsByPaneKey }
|
||||
for (const key of sleepingRecordKeys) {
|
||||
const replaced = replacedSleepingRecords[key]
|
||||
if (replaced) {
|
||||
nextSleeping[key] = replaced
|
||||
} else {
|
||||
delete nextSleeping[key]
|
||||
}
|
||||
}
|
||||
return {
|
||||
suppressedPtyExitIds: next,
|
||||
sleepingAgentSessionsByPaneKey: annotateRecordReplacement(
|
||||
sleepingAgentSessionsByPaneKey: replaceRecordEntries(
|
||||
s.sleepingAgentSessionsByPaneKey,
|
||||
nextSleeping,
|
||||
sleepingRecordKeys
|
||||
sleepingRecordKeys.map((key) => [key, replacedSleepingRecords[key] ?? null] as const)
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -137,13 +124,9 @@ export function createTerminalPaneHibernationActions(
|
||||
...s.suppressedPtyExitIds,
|
||||
...Object.fromEntries(exitGuardPtyIds.map((ptyId) => [ptyId, true] as const))
|
||||
},
|
||||
sleepingAgentSessionsByPaneKey: annotateRecordReplacement(
|
||||
sleepingAgentSessionsByPaneKey: replaceRecordEntries(
|
||||
s.sleepingAgentSessionsByPaneKey,
|
||||
{
|
||||
...s.sleepingAgentSessionsByPaneKey,
|
||||
...sleepingAgentSessionRecords
|
||||
},
|
||||
sleepingRecordKeys
|
||||
Object.entries(sleepingAgentSessionRecords)
|
||||
)
|
||||
}))
|
||||
if (expectedRuntimePtyIds.length > 0) {
|
||||
@@ -215,8 +198,8 @@ export function createTerminalPaneHibernationActions(
|
||||
const existingPtyIds = s.ptyIdsByTabId[opts.tabId] ?? []
|
||||
const shutdownPtyIdSet = new Set(rendererShutdownPtyIds)
|
||||
const remainingPtyIds = existingPtyIds.filter((ptyId) => !shutdownPtyIdSet.has(ptyId))
|
||||
const nextTabsByWorktree = { ...s.tabsByWorktree }
|
||||
const tabs = nextTabsByWorktree[worktreeId] ?? []
|
||||
const tabs = s.tabsByWorktree[worktreeId] ?? []
|
||||
const replacements: [string, typeof tabs][] = []
|
||||
const tabIndex = tabs.findIndex((candidate) => candidate.id === opts.tabId)
|
||||
if (tabIndex !== -1) {
|
||||
const nextTabs = [...tabs]
|
||||
@@ -224,7 +207,7 @@ export function createTerminalPaneHibernationActions(
|
||||
...nextTabs[tabIndex],
|
||||
ptyId: remainingPtyIds.at(-1) ?? null
|
||||
}
|
||||
nextTabsByWorktree[worktreeId] = nextTabs
|
||||
replacements.push([worktreeId, nextTabs])
|
||||
}
|
||||
const nextCodexRestartNoticeByPtyId = { ...s.codexRestartNoticeByPtyId }
|
||||
for (const ptyId of exitGuardPtyIds) {
|
||||
@@ -232,11 +215,8 @@ export function createTerminalPaneHibernationActions(
|
||||
}
|
||||
const nextLastKnownRelay =
|
||||
remainingPtyIds.length === 0
|
||||
? { ...s.lastKnownRelayPtyIdByTabId }
|
||||
? replaceRecordEntries(s.lastKnownRelayPtyIdByTabId, [[opts.tabId, null]])
|
||||
: s.lastKnownRelayPtyIdByTabId
|
||||
if (remainingPtyIds.length === 0) {
|
||||
delete nextLastKnownRelay[opts.tabId]
|
||||
}
|
||||
let nextRuntimePaneTitlesByTabId = s.runtimePaneTitlesByTabId
|
||||
const numericPaneId = Number(opts.leafId)
|
||||
if (
|
||||
@@ -245,32 +225,16 @@ export function createTerminalPaneHibernationActions(
|
||||
) {
|
||||
const nextByPane = { ...s.runtimePaneTitlesByTabId[opts.tabId] }
|
||||
delete nextByPane[numericPaneId]
|
||||
nextRuntimePaneTitlesByTabId = { ...s.runtimePaneTitlesByTabId }
|
||||
if (Object.keys(nextByPane).length > 0) {
|
||||
nextRuntimePaneTitlesByTabId[opts.tabId] = nextByPane
|
||||
} else {
|
||||
delete nextRuntimePaneTitlesByTabId[opts.tabId]
|
||||
}
|
||||
nextRuntimePaneTitlesByTabId = replaceRecordEntries(s.runtimePaneTitlesByTabId, [
|
||||
[opts.tabId, Object.keys(nextByPane).length > 0 ? nextByPane : null]
|
||||
])
|
||||
}
|
||||
const nextLastTerminalInputAtByPaneKey = { ...s.lastTerminalInputAtByPaneKey }
|
||||
delete nextLastTerminalInputAtByPaneKey[opts.paneKey]
|
||||
return {
|
||||
tabsByWorktree: annotateTabBucketReplacement(s.tabsByWorktree, nextTabsByWorktree, [
|
||||
worktreeId
|
||||
]),
|
||||
ptyIdsByTabId: annotateRecordReplacement(
|
||||
s.ptyIdsByTabId,
|
||||
{
|
||||
...s.ptyIdsByTabId,
|
||||
[opts.tabId]: remainingPtyIds
|
||||
},
|
||||
[opts.tabId]
|
||||
),
|
||||
lastKnownRelayPtyIdByTabId: annotateRecordReplacement(
|
||||
s.lastKnownRelayPtyIdByTabId,
|
||||
nextLastKnownRelay,
|
||||
[opts.tabId]
|
||||
),
|
||||
tabsByWorktree: replaceTabBuckets(s.tabsByWorktree, replacements),
|
||||
ptyIdsByTabId: replaceRecordEntries(s.ptyIdsByTabId, [[opts.tabId, remainingPtyIds]]),
|
||||
lastKnownRelayPtyIdByTabId: nextLastKnownRelay,
|
||||
suppressedPtyExitIds: {
|
||||
...s.suppressedPtyExitIds,
|
||||
...Object.fromEntries(exitGuardPtyIds.map((ptyId) => [ptyId, true] as const))
|
||||
@@ -278,11 +242,7 @@ export function createTerminalPaneHibernationActions(
|
||||
codexRestartNoticeByPtyId: nextCodexRestartNoticeByPtyId,
|
||||
...(nextRuntimePaneTitlesByTabId !== s.runtimePaneTitlesByTabId
|
||||
? {
|
||||
runtimePaneTitlesByTabId: annotateRecordReplacement(
|
||||
s.runtimePaneTitlesByTabId,
|
||||
nextRuntimePaneTitlesByTabId,
|
||||
[opts.tabId]
|
||||
)
|
||||
runtimePaneTitlesByTabId: nextRuntimePaneTitlesByTabId
|
||||
}
|
||||
: {}),
|
||||
...removePanesAttention(s, new Set([opts.paneKey])),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {
|
||||
annotateTabBucketReplacement,
|
||||
annotateRecordReplacement
|
||||
} from '@/store/keyed-record-transitions'
|
||||
import type { TerminalTab } from '../../../../shared/workspace-layout/terminal-tab-types'
|
||||
import { replaceTabBuckets, replaceRecordEntries } from '@/store/keyed-record-transitions'
|
||||
import type {
|
||||
TerminalLayoutSnapshot,
|
||||
TerminalTab
|
||||
} from '../../../../shared/workspace-layout/terminal-tab-types'
|
||||
import type { SleepingAgentSessionRecord } from '../../../../shared/agent-session/lifecycle/agent-session-resume'
|
||||
import { clearTransientTerminalState } from './terminal-transient-state'
|
||||
import {
|
||||
@@ -48,20 +48,22 @@ export function commitTerminalShutdownState({
|
||||
set((state) => {
|
||||
const tabsByWorktree = keepIdentifiers
|
||||
? state.tabsByWorktree
|
||||
: {
|
||||
...state.tabsByWorktree,
|
||||
[worktreeId]: (state.tabsByWorktree[worktreeId] ?? []).map((tab, index) =>
|
||||
clearTransientTerminalState(tab, index)
|
||||
)
|
||||
}
|
||||
: replaceTabBuckets(state.tabsByWorktree, [
|
||||
[
|
||||
worktreeId,
|
||||
(state.tabsByWorktree[worktreeId] ?? []).map((tab, index) =>
|
||||
clearTransientTerminalState(tab, index)
|
||||
)
|
||||
]
|
||||
])
|
||||
// Use copy-on-write for already-exited workspaces; churning ptyIdsByTabId invalidates whole-map selectors and every sidebar live-PTY memo.
|
||||
const ptyIdsByTabId = copyOnWriteRecord(state.ptyIdsByTabId)
|
||||
const ptyReplacements: [string, string[]][] = []
|
||||
for (const tab of tabs) {
|
||||
// Why `!== undefined`: an absent key is not an empty array, and the spread this
|
||||
// replaces created the key. Only an already-empty entry can be skipped.
|
||||
const current = state.ptyIdsByTabId[tab.id]
|
||||
if (current === undefined || current.length > 0) {
|
||||
ptyIdsByTabId.set(tab.id, [])
|
||||
ptyReplacements.push([tab.id, []])
|
||||
}
|
||||
}
|
||||
const suppressedPtyExitIds = copyOnWriteRecord(state.suppressedPtyExitIds)
|
||||
@@ -88,12 +90,12 @@ export function commitTerminalShutdownState({
|
||||
codexRestartNoticeByPtyId.delete(ptyId)
|
||||
}
|
||||
|
||||
const runtimePaneTitlesByTabId = copyOnWriteRecord(state.runtimePaneTitlesByTabId)
|
||||
const titleDeletions: [string, null][] = []
|
||||
const pendingIssueCommandSplitByTabId = copyOnWriteRecord(
|
||||
state.pendingIssueCommandSplitByTabId
|
||||
)
|
||||
const terminalLayoutsByTabId = copyOnWriteRecord(state.terminalLayoutsByTabId)
|
||||
const lastKnownRelayPtyIdByTabId = copyOnWriteRecord(state.lastKnownRelayPtyIdByTabId)
|
||||
const layoutReplacements: [string, TerminalLayoutSnapshot][] = []
|
||||
const relayDeletions: [string, null][] = []
|
||||
const lastTerminalInputAtByPaneKey = copyOnWriteRecord(state.lastTerminalInputAtByPaneKey)
|
||||
|
||||
for (const tab of tabs) {
|
||||
@@ -105,46 +107,44 @@ export function commitTerminalShutdownState({
|
||||
}
|
||||
}
|
||||
if (!keepIdentifiers) {
|
||||
runtimePaneTitlesByTabId.delete(tab.id)
|
||||
lastKnownRelayPtyIdByTabId.delete(tab.id)
|
||||
if (tab.id in state.runtimePaneTitlesByTabId) {
|
||||
titleDeletions.push([tab.id, null])
|
||||
}
|
||||
if (tab.id in state.lastKnownRelayPtyIdByTabId) {
|
||||
relayDeletions.push([tab.id, null])
|
||||
}
|
||||
const layout = state.terminalLayoutsByTabId[tab.id]
|
||||
// Why the emptiness check: replacing an already-empty map with a fresh {} is
|
||||
// the same value with a new identity.
|
||||
if (layout?.ptyIdsByLeafId && Object.keys(layout.ptyIdsByLeafId).length > 0) {
|
||||
terminalLayoutsByTabId.set(tab.id, { ...layout, ptyIdsByLeafId: {} })
|
||||
layoutReplacements.push([tab.id, { ...layout, ptyIdsByLeafId: {} }])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
tabsByWorktree: annotateTabBucketReplacement(state.tabsByWorktree, tabsByWorktree, [
|
||||
worktreeId
|
||||
]),
|
||||
ptyIdsByTabId: annotateRecordReplacement(
|
||||
state.ptyIdsByTabId,
|
||||
ptyIdsByTabId.read(),
|
||||
tabs.map((tab) => tab.id)
|
||||
),
|
||||
lastKnownRelayPtyIdByTabId: annotateRecordReplacement(
|
||||
state.lastKnownRelayPtyIdByTabId,
|
||||
lastKnownRelayPtyIdByTabId.read(),
|
||||
tabs.map((tab) => tab.id)
|
||||
),
|
||||
runtimePaneTitlesByTabId: annotateRecordReplacement(
|
||||
state.runtimePaneTitlesByTabId,
|
||||
runtimePaneTitlesByTabId.read(),
|
||||
tabs.map((tab) => tab.id)
|
||||
),
|
||||
tabsByWorktree,
|
||||
ptyIdsByTabId:
|
||||
ptyReplacements.length > 0
|
||||
? replaceRecordEntries(state.ptyIdsByTabId, ptyReplacements)
|
||||
: state.ptyIdsByTabId,
|
||||
lastKnownRelayPtyIdByTabId:
|
||||
relayDeletions.length > 0
|
||||
? replaceRecordEntries(state.lastKnownRelayPtyIdByTabId, relayDeletions)
|
||||
: state.lastKnownRelayPtyIdByTabId,
|
||||
runtimePaneTitlesByTabId:
|
||||
titleDeletions.length > 0
|
||||
? replaceRecordEntries(state.runtimePaneTitlesByTabId, titleDeletions)
|
||||
: state.runtimePaneTitlesByTabId,
|
||||
suppressedPtyExitIds: suppressedPtyExitIds.read(),
|
||||
pendingPtyShutdownIds: pendingPtyShutdownIds.read(),
|
||||
pendingPaneAccountRestarts: pendingPaneAccountRestarts.read(),
|
||||
codexRestartNoticeByPtyId: codexRestartNoticeByPtyId.read(),
|
||||
pendingIssueCommandSplitByTabId: pendingIssueCommandSplitByTabId.read(),
|
||||
terminalLayoutsByTabId: annotateRecordReplacement(
|
||||
state.terminalLayoutsByTabId,
|
||||
terminalLayoutsByTabId.read(),
|
||||
tabs.map((tab) => tab.id)
|
||||
),
|
||||
terminalLayoutsByTabId:
|
||||
layoutReplacements.length > 0
|
||||
? replaceRecordEntries(state.terminalLayoutsByTabId, layoutReplacements)
|
||||
: state.terminalLayoutsByTabId,
|
||||
...removeTabsAttention(
|
||||
state,
|
||||
tabs.map((tab) => tab.id)
|
||||
@@ -166,10 +166,9 @@ export function commitTerminalShutdownState({
|
||||
).records
|
||||
: state.sleepingAgentSessionsByPaneKey
|
||||
return {
|
||||
sleepingAgentSessionsByPaneKey: annotateRecordReplacement(
|
||||
sleepingAgentSessionsByPaneKey: replaceRecordEntries(
|
||||
base,
|
||||
{ ...base, ...sleepingAgentSessionRecords },
|
||||
Object.keys(sleepingAgentSessionRecords)
|
||||
Object.entries(sleepingAgentSessionRecords)
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user