Files
archived-orca/src/main/ipc/session.ts
T
Brennan Benson 4fb159a95f perf: restore terminal history without synchronous IPC (#41660)
* perf: restore terminal history without synchronous IPC

* fix: follow moved terminal topology imports

* Consume queued terminal commands when startup dispatches
2026-11-12 16:12:57 -08:00

104 lines
4.7 KiB
TypeScript

import { ipcMain } from 'electron'
import type { Store } from '../persistence/loading-store/store'
import type { OrcaRuntimeService } from '../runtime/orca-runtime'
import { parseTerminalSurfaceCloseTarget } from '../../shared/workspace-layout/terminal-surfaces/terminal-surface-close-target'
import { isFrozenOrcadSourceSessionPartition } from '../persistence/migrating-orcad-catalog/source/catalog/orcad-source-visibility'
import { isWslMoveWindowWriteHeld } from '../persistence/moving-wsl-projects/quiescence/wsl-move-window-write-hold'
import { visibleSessionHostIds } from '../persistence/migrating-orcad-catalog/retained-source/orcad-retained-source-session'
import { markAgentLaunchesClosedByUser } from '../agent-launch/agent-launch-pane-attachment'
import { windowViewPatchForMain, windowViewSaveForMain } from '../persistence/window-save-view'
import { reportWindowSaveDrift } from '../persistence/window-save-divergence'
import { windowSaveKeepingMainSleepRecords } from '../persistence/window-save-sleep-records'
import { ORCA_LAYOUT_OWNER_IS_MAIN } from '../../shared/layout-owner-switch'
import type {
WorkspaceSessionPatch,
WorkspaceSessionState
} from '../../shared/workspace-session/workspace-session-state-types'
export function registerSessionHandlers(store: Store, runtime: OrcaRuntimeService): void {
// Why: renderer saves would change a fenced host's frozen source partition, or write back what a
// WSL move rewrote under this window before main reloads it.
const isFenced = (hostId?: string | null): boolean =>
isFrozenOrcadSourceSessionPartition(store, hostId) || isWslMoveWindowWriteHeld()
// Optional hostId preserves local-partition behavior for older renderers.
ipcMain.handle('session:get', (_event, hostId?: string | null) => {
return store.getWorkspaceSession(hostId)
})
// Repo catalogs omit folder-only SSH hosts; enumerate session partitions directly (#12723).
ipcMain.handle('session:list-host-ids', () => {
return visibleSessionHostIds(store, store.getWorkspaceSessionHostIds())
})
// Main owns layout membership; merge the window's view and facts, then report layout drift.
const sessionForMain = (args: WorkspaceSessionState, hostId: string | null | undefined) => {
const stored = store.getWorkspaceSession(hostId)
reportWindowSaveDrift(stored, args, hostId, 'session:set')
return windowViewSaveForMain(stored, args, hostId, (session, partition) =>
runtime.shutdownReconnectFactsFor(session, partition)
)
}
const patchForMain = (args: WorkspaceSessionPatch, hostId: string | null | undefined) => {
const stored = store.getWorkspaceSession(hostId)
reportWindowSaveDrift(stored, { ...stored, ...args }, hostId, 'session:patch')
return windowViewPatchForMain(stored, args, hostId, (session, partition) =>
runtime.shutdownReconnectFactsFor(session, partition)
)
}
ipcMain.handle('session:set', (_event, args: WorkspaceSessionState, hostId?: string | null) => {
if (!isFenced(hostId)) {
store.setWorkspaceSession(
ORCA_LAYOUT_OWNER_IS_MAIN
? sessionForMain(args, hostId)
: windowSaveKeepingMainSleepRecords(store.getWorkspaceSession(hostId), args, 'set'),
hostId
)
}
})
ipcMain.handle('session:patch', (_event, args: WorkspaceSessionPatch, hostId?: string | null) => {
if (!isFenced(hostId)) {
store.patchWorkspaceSession(
ORCA_LAYOUT_OWNER_IS_MAIN
? patchForMain(args, hostId)
: windowSaveKeepingMainSleepRecords(store.getWorkspaceSession(hostId), args, 'patch'),
hostId
)
}
})
// Window saves cannot shrink main-owned membership; closes must commit explicitly.
ipcMain.handle(
'session:close-terminal-surface',
(_event, args: { worktreeId?: unknown; target?: unknown; reason?: unknown } | undefined) => {
const target = parseTerminalSurfaceCloseTarget(args?.target)
if (typeof args?.worktreeId !== 'string' || !target) {
throw new Error('invalid_terminal_surface')
}
// Only main may close tabs for process exit.
const reason = args.reason === 'cleanup' ? 'cleanup' : 'user'
if (reason === 'user') {
markAgentLaunchesClosedByUser(args.worktreeId, target)
}
return runtime.closeTerminalSurfaceFromRenderer({
worktreeId: args.worktreeId,
target,
reason
})
}
)
ipcMain.handle('session:flush', () => {
// Lifecycle success requires durable writes; Store.flush only logs failures.
return store.flushPendingOrThrowAsync()
})
ipcMain.handle(
'session:read-terminal-scrollback',
(_event, args: { ref?: unknown } | undefined) =>
typeof args?.ref === 'string' ? store.readTerminalScrollbackSnapshotAsync(args.ref) : null
)
}