Plan item 5 of docs/prds/execution-tree-state-machine.md (archie-core-1217).
Until now a stop was a context cancel plus a caller-side transition, and the
step rows a worker still held running could never be told: the execution moved
while the work carried on, and the old interrupt-first ordering existed only
because the record could not defend itself. The step table can: the PRD's
ordering -- the store is the record, the context cancel is the delivery --
became safe the moment a worker's next step write fails ErrStaleTransition
against a cancelled step.
- storecontract.ExecutionCanceller and the CancelExecution RPC: one
transaction moves every non-terminal StepExecution of the execution's
current attempt to cancelled (one event row each, in the same
transaction), and the execution itself to the status the operator action
names, under the shared transition table with its audit row. Staleness is
decided before legality, the way guardTransition decides it. Earlier
attempts are never rewritten. Deliberately administrative: the
deny-by-default grant rule covers it, pinned by a test.
- taskactions.Service routes stop, cancel, abandon and reject through it and
the old Transition/CancelTask combinations are deleted; the daemon's
in-memory cancel is demoted to the delivery hook the PRD names, called
after the store record.
- The gateway's chat controller records first and delivers second, the same
order, and records even when no runtime is wired here (a standalone
gateway cannot reach the daemon's goroutines): the record is what unsticks
a lost-cancel task, and the delivery is best effort. StopRunning and
TaskRuntime.CancelRunningTasks are deleted outright -- no production
caller remained once the task_action tool owned stopping, and the daemon's
CancelRunningTasks/runningTasks.stopAll go with them.
- The gateway's chat adapter writes through CancelExecution instead of a
bare Transition, so chat /cancel and the dashboard's action service land
on the same path.
PRD decisions recorded here:
- Each cancelled step's event row is stage_finish with {cancelled: true} and
the reason as its detail -- the exact-one-event-per-step-transition rule
covers the pending -> cancelled transition too, even for a step that never
ran.
- A parked cancel carries the needs_human class, matching the Transition
path this replaces; the transient class stays with parkRunningTask's
unwind parks, which now meet ErrStaleTransition for cancelled runs and are
ignored there.
- Cancelling a wait:true callee is deferred to #1216's agent-call children:
the callee's own tree and the wait flag are what make
"cancelling a caller cancels each callee it waits on" decidable, and the
callee writes it cannot fake are this issue's half of the contract.
- RecoverStale's interrupted sweep is plan item 6, untouched here.
46 lines
2.3 KiB
SQL
46 lines
2.3 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;
|