Files
archie/internal/infrastructure/postgres/queries/steps.sql
T
sam 23e5c3f68f feat(statestore): RecoverStale interrupts the steps a crash left running
Plan item 6 of docs/prds/execution-tree-state-machine.md (archie-core-1214),
the crash-recovery sweep the step table made decidable. RecoverStale used to be
one bare UPDATE of tasks running -> queued; the steps an interrupted execution
left running stayed running forever, so the next attempt started under steps
that were never closed.

Recovery is now one transaction: the running executions are locked, every step
each one's current attempt left running moves to the step transition table's
interrupted outcome with one stage_finish event row carrying the
interruption marker -- the same document stageOutcome reads, so the attempt
view renders the interruption the way the pre-step-table producer wrote it --
each execution's audit row lands in the same write, and the existing
running->queued requeue query runs last inside it. Both edges are pinned in
SQL and pinned to their tables (the execution pair beside claim and
BeginRemediation, the step pair in its own test, because the two tables are
different tables), so a query edit cannot move a write off a table.

Deliberately running-only, per the PRD's letter: pending rows never persist
outside StartStep's own transaction, so a crashed attempt leaves only running
rows, and a finished step is never rewritten. The interrupted steps' events
render as the PRD's interrupted -- distinct from cancelled, which an operator
or parent asked for, and from failed, which the step reported.

The wait:true-callee and agent-child sweeps ride #1216's tree, and the
dashboard's ListSteps read is plan item 7; both untouched here, and the
RecoverStale signature and RPC are unchanged.
2026-09-27 06:47:29 +10:00

64 lines
3.2 KiB
SQL

-- Step-execution queries (docs/prds/execution-tree-state-machine.md). The
-- store method owns the transaction that pairs each step transition with its
-- event row; these are only the guarded row writes and reads.
-- name: LockExecutionForStep :one
-- FOR UPDATE locks the execution's row, so the status check, the step insert
-- and the event append are one serialized operation per execution -- the way
-- guardTransition locks a task's row for its transition. The row also carries
-- every field the transition's event needs (org, workspace, the task
-- identity) and the attempt the step must belong to.
SELECT id, status, org_id, workspace_id, owner, repo, issue_number, workflow, attempt
FROM tasks WHERE id = $1 FOR UPDATE;
-- name: InsertStepExecution :one
INSERT INTO step_executions (org_id, workspace_id, execution_id, attempt, parent_id, depth, kind, name, called_execution_id, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 'pending')
RETURNING id;
-- name: StartStepExecution :execrows
-- The pending -> running half of StartStep, guarded by the row's own status:
-- the step transition table routes the pair, so a row that is not pending is
-- stale rather than silently restarted.
UPDATE step_executions SET status = 'running', started_at = now()
WHERE id = $1 AND status = 'pending';
-- name: LockStepExecution :one
-- FOR UPDATE, shared by a start's parent check and a finish's guarded write:
-- the row is held for the caller's transaction, so the state a check reads is
-- the state the guarded write sees.
SELECT id, org_id, workspace_id, execution_id, attempt, parent_id, depth, kind, name, called_execution_id, status, detail, tokens_used, started_at, finished_at
FROM step_executions WHERE id = $1 FOR UPDATE;
-- name: FinishStepExecution :execrows
UPDATE step_executions
SET status = $1, detail = $2, tokens_used = $3, finished_at = now()
WHERE id = $4 AND status = $5;
-- name: CancelAttemptSteps :many
-- The half of CancelExecution that cancels the current attempt's
-- non-terminal steps, returning them so the store writes one event row per
-- transition in the same transaction. Earlier attempts' rows never match:
-- history is never rewritten.
UPDATE step_executions
SET status = 'cancelled', finished_at = now()
WHERE execution_id = $1 AND attempt = $2 AND status IN ('pending', 'running')
RETURNING id, name;
-- name: LockRunningExecutions :many
-- The executions a crashed or replaced daemon left running, locked for the
-- recovery transaction so the step sweep and the requeue cannot race a
-- concurrent write. The row carries the identity the steps' events need.
SELECT id, org_id, workspace_id, owner, repo, issue_number, workflow, attempt
FROM tasks WHERE status = 'running' FOR UPDATE;
-- name: InterruptAttemptSteps :many
-- The crash-recovery step edge (docs/prds/execution-tree-state-machine.md):
-- the steps an interrupted execution left running move to the table's
-- interrupted outcome, guarded by the row's own status so only the pair the
-- step transition table routes is written. Returning them lets the store
-- write one event row per transition in the same transaction.
UPDATE step_executions
SET status = 'interrupted', finished_at = now()
WHERE execution_id = $1 AND attempt = $2 AND status = 'running'
RETURNING id, name;