Plan item 4 of docs/prds/execution-tree-state-machine.md (archie-core-1216), plus the callee half of CancelExecution that plan item 5 deferred until the tree could decide it. Every agent call a stage makes records itself: AgentStage's bridge, the baseline-fix auto-fix run, the adversarial reviewer and a workflow.call each start a child StepExecution of kind agent or call under the stage's own step -- TaskContext.StepID, which the engine sets before the stage body starts and clears when it returns -- and finish it with the call's own outcome and the tokens it accounted. A child's recording write failure parks the execution the same way a stage's does: an agent call that ran unrecorded is the invisibility the step table exists to end. A stage invoked outside the engine's recorded run -- the stage unit tests -- records no child and runs unchanged. The call step is what makes the callee half of the PRD's Cancellation section decidable, and this lands it: a call step closes when its callee ends, so an open call step is a callee the caller waits on (wait:true) and a closed one is a wait:false callee that runs on. CancelExecution reads the open call steps before its sweep closes them and cancels each named callee through its own CancelExecution in the caller's write's aftermath -- the callee's own steps, its own events, its own move to the same destination, and the callee's own wait:true callees beyond it, the depth the enqueue enforces bounding the recursion. A callee that cannot be cancelled is reported and the caller's own record stands. StartStep refuses a call step whose callee is not this run's own (the callee's call_parent_task_id must name the recording execution): a compromised container could otherwise record a step naming a foreign execution and cancel it. StartStepRequest gains called_execution_id (field 6; non-breaking, the column and its named consumer already exist) and StepStart carries it. PRD decisions recorded here: - The reviewer records as one agent child named "review": the Reviewer contract runs one subagent per review, the PRD model's review:1/review:2 are illustrative of depth-1 children. A completed report closes it succeeded, a not-run review failed with the skip reason; the contract carries no usage, so tokens are 0. - A failed-callee call step closes as failed with the callee's detail when the wait ends otherwise; the single open case is the caller itself being cancelled, whose CancelExecution already swept this step -- a late write must not pretend the wait is still the caller's business. - A call step's callee is cancelled to the same destination the caller's action named (parked stays parked, closed_wont_do ends the whole chain), and a callee that cannot be cancelled is reported while the caller's own record stands either way. - The agent child's detail is the result summary on success and the error's text on failure, with the call's own TokensUsed -- the PRD's only usage field, untracked per call for the reviewer contract.
82 lines
4.2 KiB
SQL
82 lines
4.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: StepCalleeParent :one
|
|
-- The caller linkage a call step's callee must carry: the callee's
|
|
-- call_parent_task_id names the execution whose tree records the call. A
|
|
-- call step naming a task that is not this execution's callee is refused,
|
|
-- because a cancel of that execution would otherwise cancel work the
|
|
-- caller never started.
|
|
SELECT call_parent_task_id FROM tasks WHERE id = $1;
|
|
|
|
-- 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;
|
|
|
|
-- name: WaitingCallSteps :many
|
|
-- The call steps of the execution's current attempt that are still open and
|
|
-- name a callee: a call step closes when the callee ends, so an open one is
|
|
-- a callee the caller waits on (wait:true). The callee sweep of CancelExecution
|
|
-- walks these, because a callee whose call step already closed -- wait:false,
|
|
-- or one that ended before the cancel arrived -- runs on.
|
|
SELECT id, called_execution_id FROM step_executions
|
|
WHERE execution_id = $1 AND attempt = $2
|
|
AND kind = 'call' AND status IN ('pending', 'running') AND called_execution_id <> 0;
|