docs/prds/execution-tree-state-machine.md: step_executions (0017) is now the
durable per-step record; the dashboard's run detail (GET /api/tasks/{id}/
attempts) still folded stage_start/stage_finish events into a rail by hand,
and tasks.stage duplicated what StartStep/FinishStep already record properly.
Added the missing read:
- ListSteps(execution_id, attempt) -- attempt 0 lists every attempt of the
execution, oldest first, so the rail renders every attempt from one call.
New storecontract.StepReader (separate from StepRecorder: this is a
dashboard/API/dispatch action per the PRD's wire contract, never
task-scoped-grant callable), postgresdb.ListStepExecutions, staterpc
client/server/values.go plumbing, wired through state_store.go's Deps and
archieui/compose.go's Server.Steps the same optional-capability pattern
every other dashboard read uses.
- internal/webui/api_tasks_attempts.go: stageViewsFromSteps replaces
foldStages/finishStage/stageOutcome. Attempt bounds, event_count and which
attempt numbers exist stay sourced from events (an attempt can carry
events -- agent calls -- with no step), only the stage list itself moves.
The wire JSON contract is unchanged, so no frontend change was needed;
`task ui` confirms ui/dist still matches a fresh build.
- A running step past the task's own current attempt reads interrupted, not
running: RecoverStale (archie-core, still open per #1214) is what corrects
a crashed run's stale "running" row, and until it does this is the honest
read rather than trusting a row that may be stale.
Dropped tasks.stage and every reader, no fallback:
- Migration 0018 drops the column; tasks.sql's INSERT/UPDATE/RETURNING lists
and the sqlc-generated code no longer carry it.
- workflow.Task.Stage is gone; workflow.Run's in-flight "which stage is this"
bookkeeping moves to TaskContext.Stage (execution-time only, never
persisted -- park/finish's event tagging is its only reader).
- pb.Task.stage (wire field 10) is `[deprecated = true]`, not deleted (never
reuse a field number); staterpc's taskProto/taskValue no longer map it.
- taskactions.Task.Stage and the retry event's "previous_stage" data field
are gone: the operator-action package had no reader for the column beyond
echoing it.
- The chat /tasks command's per-task "workflow/stage" display drops to
workflow-only (ChatTaskSummary.Stage removed, workflowStage() deleted):
it was a genuine reader with no replacement in scope for this change.
Read the archie-web-ui skill and the state-store/daemon docs/development
pages first, per instruction. Stayed out of cancel/stop code throughout
(Pi's concurrent CancelExecution work); the only shared files touched
(workflow.go, daemon-adjacent SQL) got the smallest possible diff.
360 lines
9.5 KiB
Go
360 lines
9.5 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: steps.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const cancelAttemptSteps = `-- name: CancelAttemptSteps :many
|
|
UPDATE step_executions
|
|
SET status = 'cancelled', finished_at = now()
|
|
WHERE execution_id = $1 AND attempt = $2 AND status IN ('pending', 'running')
|
|
RETURNING id, name
|
|
`
|
|
|
|
type CancelAttemptStepsParams struct {
|
|
ExecutionID int64
|
|
Attempt int64
|
|
}
|
|
|
|
type CancelAttemptStepsRow struct {
|
|
ID int64
|
|
Name string
|
|
}
|
|
|
|
// 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.
|
|
func (q *Queries) CancelAttemptSteps(ctx context.Context, arg CancelAttemptStepsParams) ([]CancelAttemptStepsRow, error) {
|
|
rows, err := q.db.Query(ctx, cancelAttemptSteps, arg.ExecutionID, arg.Attempt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []CancelAttemptStepsRow
|
|
for rows.Next() {
|
|
var i CancelAttemptStepsRow
|
|
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 finishStepExecution = `-- name: FinishStepExecution :execrows
|
|
UPDATE step_executions
|
|
SET status = $1, detail = $2, tokens_used = $3, finished_at = now()
|
|
WHERE id = $4 AND status = $5
|
|
`
|
|
|
|
type FinishStepExecutionParams struct {
|
|
Status string
|
|
Detail string
|
|
TokensUsed int64
|
|
ID int64
|
|
Status_2 string
|
|
}
|
|
|
|
func (q *Queries) FinishStepExecution(ctx context.Context, arg FinishStepExecutionParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, finishStepExecution,
|
|
arg.Status,
|
|
arg.Detail,
|
|
arg.TokensUsed,
|
|
arg.ID,
|
|
arg.Status_2,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertStepExecution = `-- 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
|
|
`
|
|
|
|
type InsertStepExecutionParams struct {
|
|
OrgID string
|
|
WorkspaceID string
|
|
ExecutionID int64
|
|
Attempt int64
|
|
ParentID pgtype.Int8
|
|
Depth int32
|
|
Kind string
|
|
Name string
|
|
CalledExecutionID int64
|
|
}
|
|
|
|
func (q *Queries) InsertStepExecution(ctx context.Context, arg InsertStepExecutionParams) (int64, error) {
|
|
row := q.db.QueryRow(ctx, insertStepExecution,
|
|
arg.OrgID,
|
|
arg.WorkspaceID,
|
|
arg.ExecutionID,
|
|
arg.Attempt,
|
|
arg.ParentID,
|
|
arg.Depth,
|
|
arg.Kind,
|
|
arg.Name,
|
|
arg.CalledExecutionID,
|
|
)
|
|
var id int64
|
|
err := row.Scan(&id)
|
|
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 listStepExecutions = `-- name: ListStepExecutions :many
|
|
SELECT id, execution_id, attempt, parent_id, depth, kind, name, status, detail, tokens_used, started_at, finished_at
|
|
FROM step_executions
|
|
WHERE execution_id = $1::bigint AND ($2::bigint = 0 OR attempt = $2::bigint)
|
|
ORDER BY attempt, id
|
|
`
|
|
|
|
type ListStepExecutionsParams struct {
|
|
ExecutionID int64
|
|
Attempt int64
|
|
}
|
|
|
|
type ListStepExecutionsRow struct {
|
|
ID int64
|
|
ExecutionID int64
|
|
Attempt int64
|
|
ParentID pgtype.Int8
|
|
Depth int32
|
|
Kind string
|
|
Name string
|
|
Status string
|
|
Detail string
|
|
TokensUsed int64
|
|
StartedAt pgtype.Timestamptz
|
|
FinishedAt pgtype.Timestamptz
|
|
}
|
|
|
|
// attempt = 0 lists every attempt of the execution, oldest first; the index
|
|
// (execution_id, attempt, id) makes both this and the single-attempt form a
|
|
// straight index scan.
|
|
func (q *Queries) ListStepExecutions(ctx context.Context, arg ListStepExecutionsParams) ([]ListStepExecutionsRow, error) {
|
|
rows, err := q.db.Query(ctx, listStepExecutions, arg.ExecutionID, arg.Attempt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListStepExecutionsRow
|
|
for rows.Next() {
|
|
var i ListStepExecutionsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ExecutionID,
|
|
&i.Attempt,
|
|
&i.ParentID,
|
|
&i.Depth,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.Status,
|
|
&i.Detail,
|
|
&i.TokensUsed,
|
|
&i.StartedAt,
|
|
&i.FinishedAt,
|
|
); 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
|
|
FROM tasks WHERE id = $1 FOR UPDATE
|
|
`
|
|
|
|
type LockExecutionForStepRow struct {
|
|
ID int64
|
|
Status string
|
|
OrgID string
|
|
WorkspaceID string
|
|
Owner string
|
|
Repo string
|
|
IssueNumber int64
|
|
Workflow string
|
|
Attempt int64
|
|
}
|
|
|
|
// 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.
|
|
// 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.
|
|
func (q *Queries) LockExecutionForStep(ctx context.Context, id int64) (LockExecutionForStepRow, error) {
|
|
row := q.db.QueryRow(ctx, lockExecutionForStep, id)
|
|
var i LockExecutionForStepRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Status,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.Owner,
|
|
&i.Repo,
|
|
&i.IssueNumber,
|
|
&i.Workflow,
|
|
&i.Attempt,
|
|
)
|
|
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
|
|
`
|
|
|
|
// 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.
|
|
func (q *Queries) LockStepExecution(ctx context.Context, id int64) (StepExecution, error) {
|
|
row := q.db.QueryRow(ctx, lockStepExecution, id)
|
|
var i StepExecution
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.ExecutionID,
|
|
&i.Attempt,
|
|
&i.ParentID,
|
|
&i.Depth,
|
|
&i.Kind,
|
|
&i.Name,
|
|
&i.CalledExecutionID,
|
|
&i.Status,
|
|
&i.Detail,
|
|
&i.TokensUsed,
|
|
&i.StartedAt,
|
|
&i.FinishedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const startStepExecution = `-- name: StartStepExecution :execrows
|
|
UPDATE step_executions SET status = 'running', started_at = now()
|
|
WHERE id = $1 AND status = 'pending'
|
|
`
|
|
|
|
// 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.
|
|
func (q *Queries) StartStepExecution(ctx context.Context, id int64) (int64, error) {
|
|
result, err := q.db.Exec(ctx, startStepExecution, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|