mirror of
https://github.com/stablyai/orca.git
synced 2026-10-30 16:47:34 +00:00
* fix(layout): an SSH pane's terminal binds in its own session partition (dark, layout-owner build only) Main keeps its own layout over a window save, so a binding only a save carries is lost. A window or runtime spawn of an SSH pane bound in the local partition; it now binds where the pane's layout lives. (cherry picked from commitbeb2275b45) * test(layout): the SSH binding test uses the shared spawn commit dependencies (cherry picked from commit672b641897) * fix(layout): main moves a tab between a local and an SSH workspace itself (dark, layout-owner build only) moveTabToWorkspace across two partitions was refused as other_partition and left to the window's save. Main now reads both partitions, moves the tabs as within one, and writes both in one durable mutation. (cherry picked from commitee27a0bb07) * chore(layout): one binding write for the partition choice; shipped builds keep their exact replaced-binding read * fix(layout): window saves keep main's layout and give it only the window's view and facts (dark, layout-owner build only) windowViewSaveForMain/windowViewPatchForMain replace windowSaveForMain: main's own layout joined with the window's view, facts and carried fields, written only into the records that change. A paired server's partition passes through. A visited workspace main holds keeps its terminal row (D1). The P10 pending case is on. (cherry picked from commit0c2fc15974) * fix(layout): the save divergence detector becomes a report-only drift check behind its log (dark, layout-owner build only) It no longer wraps the write: it compares the window's raw save with main's layout model, and only when ORCA_LAYOUT_SAVE_DIVERGENCE_LOG is set. (cherry picked from commit6bc1e5556f) * fix(layout): tab reconcile and terminal reconnect stop writing tab structure (dark, layout-owner build only) Main's frames draw the tab bar and rows: reconcile still returns its counts but writes nothing, and reconnect still advertises restored terminals but leaves the rows to main. (cherry picked from commitef8eb625ca) * fix(layout): a layout command main cannot apply is a logged refusal, not the window's own change (dark, layout-owner build only) other_partition, a thrown call, and host_not_owned for a workspace that is not a paired server's are refused and logged: a window save no longer carries the change, so drawing it would last only until the next frame. A twin main may route to a paired server keeps today's path, as does the first terminal's host_not_owned fallback. (cherry picked from commit2c513b2d10) * fix(layout): the window's refusal code type keeps main's host refusals (cherry picked from commit36d46bdcc2) * fix(layout): the quit checkpoint records only the quit capture's resume records in main (dark, layout-owner build only) Main keeps its own resume records over a window save, so the quit capture's changes, which reach main as async commands, could miss the quit. The capture now sends only what it changed with the synchronous stage, and main writes them through the record-only sleep command before staging; a wake main recorded that the window had not seen stays. (cherry picked from commit2a0f433c34) * test(layout): the quit capture test builds a typed resume record (cherry picked from commitd92d1246ee) * chore(layout): one layout write per save handler * chore(layout): shipped builds keep their exact quit capture * fix(layout): the drift check compares structure only, not records main writes itself (dark, layout-owner build only) Pane bindings and resume records, default-tab marks, closed-tab records and terminal row owners are main's own writes; a save that has not caught up with them asks main for nothing, so they are no longer reported. (cherry picked from commit93e8b2627c) * test(layout): relaunch first-terminal cases, and the side-by-side resume case moves its tab the way the window does (cherry picked from commitab381332b1) * test(layout): the relaunch case also checks a reopen that cleared read-only (cherry picked from commitbbf3d040c8) * docs(layout): the drift check names the record kinds it ignores * revert(layout): drop the SSH binding partition change; SSH terminal starts are refused before any binding is saved getProvider refuses every non-local host, so neither spawn path reaches persistPtyBinding for an SSH pane, with the switch on or off. * test: run A2's two real-SQLite session tests in the Node runtime project
131 lines
5.1 KiB
TypeScript
131 lines
5.1 KiB
TypeScript
import { ipcMain } from 'electron'
|
|
import {
|
|
LOCAL_EXECUTION_HOST_ID,
|
|
normalizeExecutionHostId,
|
|
type ExecutionHostId
|
|
} from '../../shared/execution-host'
|
|
import type { SleepingAgentSessionRecord } from '../../shared/agent-session-resume'
|
|
import { applyCommandToSession } from '../persistence/terminal-topology/terminal-pane-admission'
|
|
import type { PersistedUIState } from '../../shared/persisted-ui-state-types'
|
|
import type { WorkspaceSessionState } from '../../shared/workspace-session-state-types'
|
|
import type { Store } from '../persistence'
|
|
import { isFrozenOrcadSourceSessionPartition } from '../ssh/orcad-retained-source'
|
|
import { windowViewSaveForMain } from '../persistence/window-save-view'
|
|
import { reportWindowSaveDrift } from '../persistence/window-save-divergence'
|
|
import { ORCA_LAYOUT_OWNER_IS_MAIN } from '../../shared/layout-owner-switch'
|
|
|
|
type StageBeforeUnloadSyncArgs = {
|
|
sessions: { state: WorkspaceSessionState; hostId?: ExecutionHostId }[]
|
|
ui: Partial<PersistedUIState>
|
|
/** Layout-owner builds: the resume records the quit capture changed. */
|
|
sleepRecords?: SleepingAgentSessionRecord[]
|
|
}
|
|
|
|
/**
|
|
* The partition with the quit capture's resume records written, each workspace's through the
|
|
* record-only sleep command a live capture sends: only what the capture changed, so a wake main
|
|
* recorded after the window last looked stays. Records of panes the partition lacks change nothing.
|
|
*/
|
|
function withQuitSleepRecords(
|
|
hostId: string | undefined,
|
|
session: WorkspaceSessionState,
|
|
records: readonly SleepingAgentSessionRecord[]
|
|
): WorkspaceSessionState {
|
|
const byWorkspace = Map.groupBy(records, (record) => record.worktreeId)
|
|
let current = session
|
|
for (const [workspace, workspaceRecords] of byWorkspace) {
|
|
const change = applyCommandToSession(
|
|
normalizeExecutionHostId(hostId) ?? LOCAL_EXECUTION_HOST_ID,
|
|
current,
|
|
{
|
|
type: 'sleep',
|
|
workspace,
|
|
paneKeys: workspaceRecords.map((record) => record.paneKey),
|
|
records: workspaceRecords,
|
|
recordOnly: true
|
|
}
|
|
)
|
|
current = change.session ?? current
|
|
}
|
|
return current
|
|
}
|
|
|
|
export type ShutdownCheckpointResult = { ok: boolean }
|
|
|
|
/** Matches the will-quit teardown budget so a stalled disk can't strand a restart. */
|
|
export const SHUTDOWN_CHECKPOINT_FLUSH_DEADLINE_MS = 20_000
|
|
|
|
function flushStagedStateWithDeadline(store: Store): Promise<ShutdownCheckpointResult> {
|
|
const controller = new AbortController()
|
|
let timer: ReturnType<typeof setTimeout> | null = null
|
|
const deadline = new Promise<ShutdownCheckpointResult>((resolve) => {
|
|
timer = setTimeout(() => {
|
|
controller.abort()
|
|
console.error('[app] Timed out persisting staged renderer state')
|
|
resolve({ ok: false })
|
|
}, SHUTDOWN_CHECKPOINT_FLUSH_DEADLINE_MS)
|
|
})
|
|
// Why not drain to stable: Store retries a superseded staged write without
|
|
// chasing unrelated live mutations, which the deadline would otherwise cut off.
|
|
const flush = store
|
|
.flushPendingOrThrowAsync({ signal: controller.signal, drainToStableGeneration: false })
|
|
.then((): ShutdownCheckpointResult => ({ ok: true }))
|
|
.catch((error): ShutdownCheckpointResult => {
|
|
console.error('[app] Failed to persist staged renderer state:', error)
|
|
return { ok: false }
|
|
})
|
|
return Promise.race([flush, deadline]).finally(() => {
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Switch point: main keeps its own layout over the window's and takes its view and facts, with the
|
|
* quit capture's resume records written into main's first.
|
|
*/
|
|
function stagedSessionForMain(
|
|
store: Store,
|
|
state: WorkspaceSessionState,
|
|
hostId: ExecutionHostId | undefined,
|
|
sleepRecords: readonly SleepingAgentSessionRecord[] = []
|
|
): WorkspaceSessionState {
|
|
const stored = withQuitSleepRecords(hostId, store.getWorkspaceSession(hostId), sleepRecords)
|
|
reportWindowSaveDrift(stored, state, hostId, 'shutdown-checkpoint')
|
|
return windowViewSaveForMain(stored, state, hostId)
|
|
}
|
|
|
|
export function registerRendererShutdownCheckpointHandler(store: Store): void {
|
|
// Why: beforeunload cannot await, so the sync reply only reports staging.
|
|
// Durability is joined out-of-band by paths that are about to navigate.
|
|
let pendingCheckpoint: Promise<ShutdownCheckpointResult> = Promise.resolve({ ok: true })
|
|
|
|
ipcMain.on('app:stage-before-unload-sync', (event, args: StageBeforeUnloadSyncArgs) => {
|
|
let ok = true
|
|
try {
|
|
for (const { state, hostId } of args.sessions) {
|
|
if (!isFrozenOrcadSourceSessionPartition(store, hostId)) {
|
|
store.stageWorkspaceSessionBeforeUnload(
|
|
ORCA_LAYOUT_OWNER_IS_MAIN
|
|
? stagedSessionForMain(store, state, hostId, args.sleepRecords)
|
|
: state,
|
|
hostId
|
|
)
|
|
}
|
|
}
|
|
store.updateUI(args.ui)
|
|
} catch (error) {
|
|
console.error('[app] Failed to stage renderer state before unload:', error)
|
|
ok = false
|
|
}
|
|
pendingCheckpoint = ok ? flushStagedStateWithDeadline(store) : Promise.resolve({ ok: false })
|
|
event.returnValue = { ok }
|
|
})
|
|
|
|
ipcMain.handle(
|
|
'app:await-before-unload-checkpoint',
|
|
(): Promise<ShutdownCheckpointResult> => pendingCheckpoint
|
|
)
|
|
}
|