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.
This commit is contained in:
1 parent
fe2a3a7565
commit
41823ab336
5 files changed
+253
-4
No files matched your search
@@ -115,6 +115,48 @@ func (q *Queries) InsertStepExecution(ctx context.Context, arg InsertStepExecuti
|
||||
return id, err
|
||||
}
|
||||
|
||||
const interruptAttemptSteps = `-- name: InterruptAttemptSteps :many
|
||||
UPDATE step_executions
|
||||
SET status = 'interrupted', finished_at = now()
|
||||
WHERE execution_id = $1 AND attempt = $2 AND status = 'running'
|
||||
RETURNING id, name
|
||||
`
|
||||
|
||||
type InterruptAttemptStepsParams struct {
|
||||
ExecutionID int64
|
||||
Attempt int64
|
||||
}
|
||||
|
||||
type InterruptAttemptStepsRow struct {
|
||||
ID int64
|
||||
Name string
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (q *Queries) InterruptAttemptSteps(ctx context.Context, arg InterruptAttemptStepsParams) ([]InterruptAttemptStepsRow, error) {
|
||||
rows, err := q.db.Query(ctx, interruptAttemptSteps, arg.ExecutionID, arg.Attempt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []InterruptAttemptStepsRow
|
||||
for rows.Next() {
|
||||
var i InterruptAttemptStepsRow
|
||||
if err := rows.Scan(&i.ID, &i.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockExecutionForStep = `-- name: LockExecutionForStep :one
|
||||
|
||||
SELECT id, status, org_id, workspace_id, owner, repo, issue_number, workflow, attempt
|
||||
@@ -158,6 +200,54 @@ func (q *Queries) LockExecutionForStep(ctx context.Context, id int64) (LockExecu
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockRunningExecutions = `-- name: LockRunningExecutions :many
|
||||
SELECT id, org_id, workspace_id, owner, repo, issue_number, workflow, attempt
|
||||
FROM tasks WHERE status = 'running' FOR UPDATE
|
||||
`
|
||||
|
||||
type LockRunningExecutionsRow struct {
|
||||
ID int64
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
Owner string
|
||||
Repo string
|
||||
IssueNumber int64
|
||||
Workflow string
|
||||
Attempt int64
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (q *Queries) LockRunningExecutions(ctx context.Context) ([]LockRunningExecutionsRow, error) {
|
||||
rows, err := q.db.Query(ctx, lockRunningExecutions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []LockRunningExecutionsRow
|
||||
for rows.Next() {
|
||||
var i LockRunningExecutionsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.Owner,
|
||||
&i.Repo,
|
||||
&i.IssueNumber,
|
||||
&i.Workflow,
|
||||
&i.Attempt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockStepExecution = `-- name: LockStepExecution :one
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user