From e9561c3c500cd333dbb49d5617c906e60fe588aa Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 02:55:34 -0800 Subject: [PATCH] fix(run-actions): persisted runs load at startup (TDZ), relaunch after a rollover waits for the new host to answer Found live on an Orca Cloud rollover (m4air): the persisted-run reader referenced a const declared below the module-level read, so every restart started with no runs and nothing relaunched. The relaunch also read orca.yaml before the replacement host served RPCs; it now retries with a bound. --- .../src/lib/run-actions/run-action-config.ts | 23 ++++++++ .../lib/run-actions/run-action-controller.ts | 3 +- .../lib/run-actions/run-actions-store.test.ts | 52 +++++++++++++++++++ .../src/lib/run-actions/run-actions-store.ts | 4 +- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/renderer/src/lib/run-actions/run-actions-store.test.ts diff --git a/src/renderer/src/lib/run-actions/run-action-config.ts b/src/renderer/src/lib/run-actions/run-action-config.ts index 9d37f8aab13..4f852591e32 100644 --- a/src/renderer/src/lib/run-actions/run-action-config.ts +++ b/src/renderer/src/lib/run-actions/run-action-config.ts @@ -121,3 +121,26 @@ export function pickPrimaryRunAction( : undefined return liveAction ?? pickDefaultRunAction(actions) } + +const RELAUNCH_RETRY_DELAYS_MS = [0, 3_000, 6_000, 12_000, 24_000, 48_000] +const RELAUNCH_ATTEMPT_TIMEOUT_MS = 10_000 + +/** A host that just restarted may not serve repo reads yet; give it a little while to answer. */ +export async function loadRunActionsOnceHostAnswers( + worktreeId: string +): Promise { + for (const delayMs of RELAUNCH_RETRY_DELAYS_MS) { + if (delayMs > 0) { + await new Promise((resolve) => setTimeout(resolve, delayMs)) + } + // A read queued behind a reconnecting host can hang; bound each attempt and try again. + const config = await Promise.race([ + loadWorktreeRunActions(worktreeId, { background: true }), + new Promise((resolve) => setTimeout(() => resolve(null), RELAUNCH_ATTEMPT_TIMEOUT_MS)) + ]) + if (config && config.actions.length > 0) { + return config + } + } + return null +} diff --git a/src/renderer/src/lib/run-actions/run-action-controller.ts b/src/renderer/src/lib/run-actions/run-action-controller.ts index 9e4154a5407..2d515fe0acb 100644 --- a/src/renderer/src/lib/run-actions/run-action-controller.ts +++ b/src/renderer/src/lib/run-actions/run-action-controller.ts @@ -15,6 +15,7 @@ import { type RunActionRun } from './run-actions-store' import { + loadRunActionsOnceHostAnswers, loadWorktreeRunActions, resolveRunActionTarget, type RunActionTarget, @@ -299,7 +300,7 @@ export async function relaunchInterruptedRunActions(worktreeId: string): Promise if (byAction.size === 0) { return 0 } - const config = await loadWorktreeRunActions(worktreeId) + const config = await loadRunActionsOnceHostAnswers(worktreeId) let started = 0 for (const run of byAction.values()) { const action = config?.actions.find((candidate) => candidate.id === run.actionId) diff --git a/src/renderer/src/lib/run-actions/run-actions-store.test.ts b/src/renderer/src/lib/run-actions/run-actions-store.test.ts new file mode 100644 index 00000000000..934704549e8 --- /dev/null +++ b/src/renderer/src/lib/run-actions/run-actions-store.test.ts @@ -0,0 +1,52 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' + +const RUN = { + worktreeId: 'repo::/w', + repoId: 'repo', + actionId: 'web', + actionName: 'Web', + tabId: 'tab-1', + status: 'running', + exitCode: null, + startedAt: 1 +} + +afterEach(() => { + vi.unstubAllGlobals() + vi.resetModules() +}) + +function stubStorage(initial: Record): Map { + const data = new Map(Object.entries(initial)) + vi.stubGlobal('localStorage', { + getItem: (key: string) => data.get(key) ?? null, + setItem: (key: string, value: string) => void data.set(key, value) + }) + return data +} + +describe('run-actions store persistence', () => { + it('loads runs persisted by an earlier window at module load', async () => { + stubStorage({ 'orca.runActions.v1': JSON.stringify({ 'repo::/w': { web: RUN } }) }) + const store = await import('./run-actions-store') + expect(store.getRunActionRun('repo::/w', 'web')).toEqual(RUN) + }) + + it('drops malformed persisted entries', async () => { + stubStorage({ + 'orca.runActions.v1': JSON.stringify({ + 'repo::/w': { web: { ...RUN, status: 'weird' }, ok: { ...RUN, actionId: 'ok' } } + }) + }) + const store = await import('./run-actions-store') + expect(store.listRunActionRuns().map((run) => run.actionId)).toEqual(['ok']) + }) + + it('writes every change back', async () => { + const data = stubStorage({}) + const store = await import('./run-actions-store') + store.setRunActionRun({ ...RUN, status: 'running' }) + store.patchRunActionRun('repo::/w', 'web', { status: 'stopped' }) + expect(JSON.parse(data.get('orca.runActions.v1')!)['repo::/w'].web.status).toBe('stopped') + }) +}) diff --git a/src/renderer/src/lib/run-actions/run-actions-store.ts b/src/renderer/src/lib/run-actions/run-actions-store.ts index efc60bcd67f..363fb0310d3 100644 --- a/src/renderer/src/lib/run-actions/run-actions-store.ts +++ b/src/renderer/src/lib/run-actions/run-actions-store.ts @@ -19,6 +19,8 @@ export type RunActionRun = { type RunsByWorktree = Record> const STORAGE_KEY = 'orca.runActions.v1' +// Declared before `runs`: reading persisted runs at module load needs it (no temporal dead zone). +const STATUSES: readonly RunActionStatus[] = ['running', 'stopping', 'stopped', 'exited', 'failed'] let runs: RunsByWorktree = readPersisted() const listeners = new Set<() => void>() @@ -33,8 +35,6 @@ function readPersisted(): RunsByWorktree { } } -const STATUSES: readonly RunActionStatus[] = ['running', 'stopping', 'stopped', 'exited', 'failed'] - function readRun(value: unknown): RunActionRun | null { if (typeof value !== 'object' || value === null) { return null