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.
This commit is contained in:
Neil
2026-11-10 02:55:34 -08:00
parent 2cf08c221e
commit e9561c3c50
4 changed files with 79 additions and 3 deletions
@@ -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<WorktreeRunActions | null> {
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<null>((resolve) => setTimeout(() => resolve(null), RELAUNCH_ATTEMPT_TIMEOUT_MS))
])
if (config && config.actions.length > 0) {
return config
}
}
return null
}
@@ -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)
@@ -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<string, string>): Map<string, string> {
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')
})
})
@@ -19,6 +19,8 @@ export type RunActionRun = {
type RunsByWorktree = Record<string, Record<string, RunActionRun>>
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