fix(automations): keep a run's Started time after its terminal is cleared (#42226)

A run whose agent terminal or chat session had opened, but which then failed (prompt delivery failed, or the launcher threw after a partial start), lost its Started time once dead-terminal retention cleared its pane key.

Runs now store an optional launchedAt: the dispatch-start instant, recorded only once the launch produced a terminal or chat session. The persistence layer keeps the first value through later rewrites, and the Started time prefers it, falling back to the existing rules for older records. A launch that fails before opening anything records nothing.

Tests cover delivery failure followed by retention, a partial start, and the no-evidence case; each fails without the change. The recent-runs rail test now asserts hardcoded date patterns instead of calling the production formatter.
This commit is contained in:
Jinjing
2026-11-13 18:45:44 -08:00
committed by GitHub
parent 26cc9d646b
commit 784a86b812
8 changed files with 151 additions and 14 deletions
@@ -4,6 +4,8 @@ import { mkdtempSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { AutomationService } from './automation-service'
import { AutomationRunLaunchError } from './launch/automation-launch'
import type { AutomationRun } from '../../shared/automations/history/automation-run'
vi.mock('electron', () => ({ app: { getPath: () => testState.dir } }))
@@ -80,4 +82,82 @@ describe('headless dispatch prompt delivery', () => {
service.stop()
}
)
it('records launch evidence once the agent terminal exists, and keeps it after retention clears the pane', async () => {
const { store, service, run, row, settle } = await heldLaunch('delivery')
await vi.waitFor(() => expect(row()?.terminalPaneKey).toBe(OPENED.terminalPaneKey))
await settle("Aider started, but Orca could not send this run's prompt.")
const launchedAt = row()?.launchedAt
expect(launchedAt).toEqual(expect.any(Number))
expect(row()?.dispatchedAt).toBeNull()
// Dead-PTY retention forgets the terminal but must not forget that the agent launched.
store.updateAutomationRun({
runId: run.id,
status: 'dispatch_failed',
error: row()?.error ?? null,
terminalSessionId: null,
terminalPaneKey: null,
terminalPtyId: null
})
expect(row()).toMatchObject({ terminalPaneKey: null, launchedAt })
service.stop()
})
})
/** Launches one automation per error, each with a launcher that throws that error. */
async function failingLaunches(errors: Error[]) {
const store = await createStore()
store.addRepo({ id: 'r1', path: '/repo', displayName: 'repo', badgeColor: '#fff', addedAt: 1 })
const pending = [...errors]
const service = new AutomationService(store, {
tickMs: 60_000,
allowRemoteHostScheduling: true,
terminalLauncher: {
launch: async () => {
throw pending.shift() ?? new Error('Unexpected launch.')
},
available: () => true
}
})
const rows: (AutomationRun | undefined)[] = []
for (const [index] of errors.entries()) {
const automation = store.createAutomation({
name: `Nightly ${index}`,
prompt: 'Fix the flaky test',
agentId: 'aider',
projectId: 'r1',
workspaceMode: 'new_per_run',
timezone: 'UTC',
rrule: 'FREQ=DAILY;BYHOUR=9;BYMINUTE=0',
dtstart: Date.UTC(2026, 4, 12)
})
const run = await service.runNow(automation.id)
await service.runInAutomationLane(automation.id, async () => undefined)
const row = () => store.listAutomationRuns(automation.id).find((entry) => entry.id === run.id)
await vi.waitFor(() => expect(row()?.status).toBe('dispatch_failed'))
rows.push(row())
}
service.stop()
return rows
}
describe('launch evidence when the launcher throws', () => {
beforeEach(() => {
testState.dir = mkdtempSync(join(tmpdir(), 'orca-automation-launch-evidence-'))
})
afterEach(async () => {
await closeTestStores()
rmSync(testState.dir, { recursive: true, force: true })
})
it('records a partial start that opened the agent terminal, but not a launch that opened nothing', async () => {
const [partial, preOpen] = await failingLaunches([
new AutomationRunLaunchError('Prompt paste failed.', OPENED),
new Error('No window to open the terminal in.')
])
expect(partial?.launchedAt).toEqual(expect.any(Number))
// Safety: a failure before any terminal or session opened leaves no launch evidence.
expect(preOpen?.launchedAt).toBeUndefined()
})
})
@@ -75,6 +75,11 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise
status: refusal ? 'skipped_unavailable' : 'dispatch_failed',
workspaceId: automation.workspaceId,
...(error instanceof AutomationRunLaunchError ? error.target : {}),
// A partial start produced the agent's terminal or session before failing, so it
// counts as launched at the dispatch-start time.
...(error instanceof AutomationRunLaunchError && openedAgent(error.target)
? { launchedAt: dispatchedAt }
: {}),
...(automation.surface === 'chat' ? { dispatchedAt, precheckResult } : {}),
...(refusal ?? { error: describeDispatchError(error) })
}).run
@@ -82,7 +87,11 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise
const launchRunTarget = launchRunTargetOf(launch)
if (launch.delivery) {
// A startup dialog can hold delivery, so link the terminal while the run is still dispatching.
const linked = ctx.transition({ status: 'dispatching', ...launchRunTarget })
const linked = ctx.transition({
status: 'dispatching',
...launchRunTarget,
launchedAt: dispatchedAt
})
if (!linked.applied) {
return linked.run
}
@@ -91,12 +100,18 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise
})
const { error } = await launch.delivery
if (error !== null) {
return ctx.transition({ status: 'dispatch_failed', ...launchRunTarget, error }).run
return ctx.transition({
status: 'dispatch_failed',
...launchRunTarget,
launchedAt: dispatchedAt,
error
}).run
}
}
const updated = ctx.transition({
status: 'dispatched',
dispatchedAt,
launchedAt: dispatchedAt,
...launchRunTarget,
// Watched completions omit the precheck result.
...(precheckResult ? { precheckResult } : {}),
@@ -143,6 +158,13 @@ export async function runAutomationLaunch(ctx: AutomationLaunchContext): Promise
return updated.run
}
/** The launch got as far as producing the agent's terminal or chat session. */
function openedAgent(target: AutomationLaunch | undefined): boolean {
return Boolean(
target && (target.terminalPaneKey || target.terminalSessionId || target.chatSessionId)
)
}
function launchRunTargetOf(launch: AutomationLaunch) {
return {
sessionKind: launch.sessionKind ?? 'terminal',
@@ -229,6 +229,10 @@ export function updateAutomationRun(
startedAt: current.startedAt ?? now,
dispatchedAt:
result.status === 'dispatched' ? (result.dispatchedAt ?? now) : current.dispatchedAt,
// Launch evidence is durable: the first recorded dispatch-start time survives later rewrites.
...((current.launchedAt ?? result.launchedAt) !== undefined
? { launchedAt: current.launchedAt ?? result.launchedAt }
: {}),
// Later updates (completion) keep the dispatch's note.
...(result.setupSkippedReason === 'setup-changed'
? {
@@ -2,7 +2,6 @@
import { cleanup, render } from '@testing-library/react'
import { afterEach, expect, it } from 'vitest'
import { makeRun } from '../automations-page-fixtures'
import { formatAutomationDateTime } from '../automation-page-parts'
import { AutomationRecentRunsRail } from './AutomationRecentRunsRail'
afterEach(() => {
@@ -10,8 +9,9 @@ afterEach(() => {
})
const noop = (): void => {}
const FIRST = Date.UTC(2026, 10, 1, 1, 0)
const LATEST = Date.UTC(2026, 10, 9, 1, 0)
// Local-time instants, so the expected text does not depend on the machine's time zone.
const FIRST = new Date(2026, 10, 1, 1, 0).getTime()
const LATEST = new Date(2026, 10, 9, 1, 0).getTime()
it('shows a folded run at its latest occurrence, the time the list and detail show', () => {
const folded = makeRun({
@@ -33,6 +33,7 @@ it('shows a folded run at its latest occurrence, the time the list and detail sh
/>
)
const text = container.querySelector('[data-automation-run-id="folded"]')?.textContent ?? ''
expect(text).toContain(formatAutomationDateTime(LATEST))
expect(text).not.toContain(formatAutomationDateTime(FIRST))
// ICU versions join date and time with ", " or " at ", and may use a narrow space before AM.
expect(text).toMatch(/Nov 9(?:,| at) 1:00\sAM/)
expect(text).not.toMatch(/Nov 1\b/)
})
@@ -54,6 +54,7 @@ describe('when a run launched', () => {
chatSessionId: null,
terminalPaneKey: null,
sessionKind: 'chat' as const,
launchedAt: undefined,
...overrides
})
@@ -65,6 +66,12 @@ describe('when a run launched', () => {
).toBeNull()
})
it('uses the recorded launch time of a run that failed after launching', () => {
// Delivery failed before dispatch, then retention cleared the pane: only launchedAt remains.
expect(getAutomationRunLaunchedAt(run({ sessionKind: 'terminal', launchedAt: 7 }))).toBe(7)
expect(getAutomationRunLaunchedAt(run({ launchedAt: 7 }))).toBe(7)
})
it('keeps a launched terminal run whose pane key retention cleared', () => {
// Terminal runs get dispatchedAt only at dispatch; retention later drops the pane key.
expect(getAutomationRunLaunchedAt(run({ sessionKind: 'terminal', dispatchedAt: 5 }))).toBe(5)
@@ -67,20 +67,32 @@ export const RUN_START_DRIFT_LABEL_MS = 60_000
/**
* When a run actually launched, or null.
* - Dispatched and completed runs launched at their dispatch time; older records without
* one keep their start time.
* - A failed terminal run gets `dispatchedAt` only once it was dispatched, so that time is
* durable launch evidence even after terminal retention clears its pane key.
* - A failed chat run launched only if it reached a chat session: the chat launcher stamps
* `dispatchedAt` even when it refuses or throws before starting anything.
* - `launchedAt` is the dispatch-start time, recorded only once the launch produced a terminal
* or session; it survives retention and later failures and is the evidence whenever present.
* - Older records: dispatched and completed runs use their dispatch (or start) time; a failed
* terminal run uses `dispatchedAt` (set only at dispatch) or, with a pane, `startedAt`; a
* failed chat run needs a chat session, because the chat launcher stamps `dispatchedAt` even
* when it refuses or throws before starting anything.
* - Skipped and missed runs never launched; their `startedAt` is an observation time.
*/
export function getAutomationRunLaunchedAt(
run: Pick<
AutomationRun,
'status' | 'dispatchedAt' | 'startedAt' | 'chatSessionId' | 'terminalPaneKey' | 'sessionKind'
| 'status'
| 'dispatchedAt'
| 'startedAt'
| 'chatSessionId'
| 'terminalPaneKey'
| 'sessionKind'
| 'launchedAt'
>
): number | null {
if (run.status.startsWith('skipped')) {
return null
}
if (run.launchedAt !== undefined) {
return run.launchedAt
}
if (run.status === 'completed' || run.status === 'dispatched') {
return run.dispatchedAt ?? run.startedAt
}
@@ -135,6 +135,14 @@ export type AutomationRun = {
errorParams?: Record<string, string | number>
startedAt: number | null
dispatchedAt: number | null
/**
* The dispatch-start time (taken just before the dispatcher runs, the same instant as
* `dispatchedAt`), recorded only once the launch produced a terminal or chat session, even
* if the run failed afterwards (prompt delivery, a partial start). It is not a measured
* open time. Survives terminal retention clearing the pane key. Absent on older records
* and on runs that never launched.
*/
launchedAt?: number
createdAt: number
/** Persist unique run numbers; retention makes retained count unsuitable. */
runNumber?: number
@@ -174,6 +182,8 @@ export type AutomationDispatchResult = {
errorParams?: Record<string, string | number> | null
/** With `dispatched`: when the agent was handed the run, if not now. */
dispatchedAt?: number
/** Dispatch-start time, set once the launch produced a terminal or session; kept once recorded. */
launchedAt?: number
setupSkippedReason?: AutomationRunSetupSkippedReason
setupSkippedContent?: SetupTrustMismatch
}
@@ -246,6 +246,7 @@ function isMigratedAutomationRun(value: unknown): value is AutomationRun {
'orcad_migration_dormant_automation_run_dispatched_invalid',
true
)
optionalFinite(record.launchedAt, 'orcad_migration_dormant_automation_run_launched_invalid')
requiredFinite(record.createdAt, 'orcad_migration_dormant_automation_run_created_invalid')
optionalPositiveInteger(record.runNumber, 'orcad_migration_dormant_automation_run_number_invalid')
optionalPositiveInteger(