Files
orca/src/main/ipc/session.ts
T
OrcaWin 82412dab8b Persist profile state in SQLite with background writes (#22612)
Migrate profile state to SQLite and move writes and backups into a background worker. Acknowledge terminal, SSH and automation changes only after durable saves. Preserve JSON import, recovery, rollback and compatibility exports.

Validate migration, worker failures, maintenance, cross-profile moves and terminal lifetime races with unit, integration and end-to-end coverage.
2026-09-25 22:47:33 -07:00

60 lines
2.3 KiB
TypeScript

import { ipcMain } from 'electron'
import type { Store } from '../persistence'
import type {
WorkspaceSessionPatch,
WorkspaceSessionState
} from '../../shared/workspace-session-state-types'
export function registerSessionHandlers(store: Store): void {
// Why: hostId is an optional second arg so an older renderer that invokes
// these channels without it keeps reading/writing the 'local' partition
// exactly as before. Channel names stay stable.
ipcMain.handle('session:get', (_event, hostId?: string | null) => {
return store.getWorkspaceSession(hostId)
})
// Why a census channel: boot used to infer which partitions exist from the repo catalog, which
// cannot name an SSH target whose only workspace is a folder — the runtime wrote that partition
// and no reader ever enumerated it (#12723).
ipcMain.handle('session:list-host-ids', () => {
return store.getWorkspaceSessionHostIds()
})
ipcMain.handle('session:set', (_event, args: WorkspaceSessionState, hostId?: string | null) => {
store.setWorkspaceSession(args, hostId)
})
ipcMain.handle('session:patch', (_event, args: WorkspaceSessionPatch, hostId?: string | null) => {
store.patchWorkspaceSession(args, hostId)
})
ipcMain.handle('session:flush', () => {
// Why: durable lifecycle RPCs must propagate disk failures instead of
// returning success through Store.flush(), which intentionally only logs.
return store.flushPendingOrThrowAsync()
})
// Older renderers block on the reply; main remains free to await the writer.
ipcMain.on('session:set-sync', (event, args: WorkspaceSessionState, hostId?: string | null) => {
void (async () => {
try {
store.setWorkspaceSession(args, hostId)
await store.flushPendingOrThrowAsync({ drainToStableGeneration: false })
} catch (error) {
console.error('[persistence] Failed to flush legacy session checkpoint:', error)
} finally {
// This legacy response has always been best effort, including on disk errors.
event.returnValue = true
}
})()
})
ipcMain.on(
'session:read-terminal-scrollback-sync',
(event, args: { ref?: unknown } | undefined) => {
event.returnValue =
typeof args?.ref === 'string' ? store.readTerminalScrollbackSnapshot(args.ref) : null
}
)
}