Add repeat blocks, per-run inputs, skipIfNotFound, and orphaned-run recovery

Repeat. A `repeat` block runs its steps several times, with the count either
fixed in the config or taken from an input the user sets on the dashboard. The
block is unrolled in resolveSteps before the runner sees it, so the runner needs
no loop, the run's total step count stays honest, and every iteration appears in
the log as its own line — a failure on the third purchase reads as "(3/5)"
rather than as an indistinguishable repeat of the first.

Counts are clamped server-side against the automation's declared min/max, and
expansion is capped at 400 steps and three levels of nesting. Each iteration can
be a purchase, so the number is not taken on trust from the client, and the
confirmation dialog names it before anything runs.

skipIfNotFound on a click or type step tolerates an element that is not on the
page — a cookie banner, a modal that only sometimes appears. Only absence is
tolerated. That distinction needed a new NotFoundError: previously a missing
element, an unreachable dashboard, a missing tab and a covered button all
surfaced as the same DashboardError, and skipping that whole class would mean a
step quietly passing while the extension was down.

Orphaned runs are now reaped. Only one run executes at a time, so a run left in
'running' when its runner went away blocked every future run — restarting the
daemon mid-run deadlocked the queue, which is exactly what happened. The
heartbeat decides: a runner that is gone, or up and reporting idle, is not
driving that run whatever the status column says. Gated on the busy flag rather
than elapsed time alone, since a run sitting in a waitFor gate or a sign-in wait
can legitimately go minutes without progress.

Lucid Trading is scaffolded with no automations yet. One match pattern covers
both its hosts — `*.` matches the apex as well as subdomains, confirmed against
a live tab. Its signed-out pattern is `//lucidtrading.com/` rather than
`lucidtrading.com/dashboard`: the leading slashes anchor it to the start of the
host, and without them the substring also matches dash.lucidtrading.com, which
would abort every step while properly signed in.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-08-30 13:22:45 -05:00
co-authored by Claude Opus 5
parent a2066fbf04
commit 22db6eae8d
9 changed files with 367 additions and 70 deletions
+43 -4
View File
@@ -452,6 +452,7 @@ db.exec(`
step_index INTEGER NOT NULL DEFAULT 0,
total_steps INTEGER NOT NULL DEFAULT 0,
log TEXT NOT NULL DEFAULT '[]',
inputs TEXT NOT NULL DEFAULT '{}',
error TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
@@ -472,6 +473,13 @@ try {
// Column already exists
}
// Migration: per-run inputs the user set on the dashboard.
try {
db.exec("ALTER TABLE autobuyer_runs ADD COLUMN inputs TEXT NOT NULL DEFAULT '{}'");
} catch {
// Column already exists
}
export type RunStatus = 'queued' | 'running' | 'done' | 'error' | 'cancelled';
export interface RunRow {
@@ -481,6 +489,7 @@ export interface RunRow {
step_index: number;
total_steps: number;
log: string; // JSON { at: number; step: string; ok: boolean; detail?: string }[]
inputs: string; // JSON Record<string, number>
error: string | null;
created_at: number;
updated_at: number;
@@ -488,11 +497,11 @@ export interface RunRow {
const RUN_HISTORY = 30;
export function createRun(automationId: string, totalSteps: number): RunRow {
export function createRun(automationId: string, totalSteps: number, inputs: Record<string, number> = {}): RunRow {
const now = Date.now();
const res = db.prepare(
'INSERT INTO autobuyer_runs (automation_id, total_steps, created_at, updated_at) VALUES (?, ?, ?, ?)'
).run(automationId, totalSteps, now, now);
'INSERT INTO autobuyer_runs (automation_id, total_steps, inputs, created_at, updated_at) VALUES (?, ?, ?, ?, ?)'
).run(automationId, totalSteps, JSON.stringify(inputs), now, now);
db.prepare(`
DELETE FROM autobuyer_runs
@@ -515,9 +524,39 @@ export function countActiveRuns(): number {
return row.n;
}
/** Grace period before a 'running' run with nobody behind it is written off.
* Long enough to cover the gap between the runner claiming a run and its next
* heartbeat reporting busy. */
const ORPHAN_GRACE_MS = 30_000;
/** Fail runs that were left mid-flight when their runner went away.
*
* Only one run executes at a time, so a stale 'running' row blocks every future
* run — kill the daemon during a run (or restart it, which is the same thing)
* and nothing would ever be claimed again. The heartbeat says whether anything
* is actually executing: a runner that is gone, or up and idle, is not driving
* this run no matter what its status column claims.
*/
export function reapOrphanedRuns(): number {
const hb = getRunnerHeartbeat();
const alive = hb !== null && Date.now() - hb.at < RUNNER_TIMEOUT_MS;
if (alive && hb!.busy) return 0;
const now = Date.now();
return db.prepare(`
UPDATE autobuyer_runs
SET status = 'error',
error = 'the runner stopped while this run was in progress',
updated_at = ?
WHERE status = 'running' AND updated_at < ?
`).run(now, now - ORPHAN_GRACE_MS).changes;
}
/** The runner takes the oldest queued run. Only one runs at a time — two
* processes driving the same physical mouse would interleave clicks. */
export function claimRun(): RunRow | undefined {
reapOrphanedRuns();
const running = db.prepare("SELECT 1 FROM autobuyer_runs WHERE status = 'running'").get();
if (running) return undefined;
@@ -565,7 +604,7 @@ export const RUNNER_TIMEOUT_MS = 7000;
/** The runner version this server's step vocabulary requires. A running process
* doesn't reload when the source changes, so an older one silently fails on
* steps it predates — the dashboard warns instead. */
export const RUNNER_EXPECTED_VERSION = '0.9.0';
export const RUNNER_EXPECTED_VERSION = '0.11.0';
export interface RunnerHeartbeat {
at: number;