Plan item 3 of docs/prds/execution-tree-state-machine.md (archie-core-1220), on top of the transition tables and ErrIllegalTransition enforcement (1214/1215, archie-core-qdbo.5): - Migration 0017 adds step_executions -- id, org_id, workspace_id, execution_id, attempt, parent_id, depth, kind, name, called_execution_id, status, detail, tokens_used, started_at, finished_at, indexed on (execution_id, attempt, id). Ownership fields are stamped from the execution's row; depth is derived from the parent, never supplied by a caller. Existing rows gain no backfilled steps. - task.Store (the workflow engine's mid-run facade) gains StartStep/FinishStep (5 methods, within the 8 cap), with storecontract.StepRecorder fronting them on the server; deadlineStore wraps both with its per-call timeout. - The store enforces each write as one transaction: the guarded row write under the shared step transition table plus the transition's own stage_start/stage_finish event row. Staleness is decided before legality, the way guardTransition decides it, and the store returns the persisted event for the caller's post-commit publish -- the EmitDurable convention (the daemon's event sink skips already-persisted rows by assigned ID), which is why workflow.Run stops emitting stage events beside the store and no longer discards step-write errors: a failed start or finish parks the execution, and the stage does not run unrecorded. An interrupted stage records interrupted rather than failed; a recording failure on that shutdown path is logged and left to RecoverStale, which owns the interrupted-execution sweep in plan item 6. - The run credential of docs/prds/orgs-and-access.md authorises StartStep and FinishStep on its own execution only: both requests carry the execution ID the grant check verifies (FinishStep carries it because the interceptor never reads the database), and FinishStep verifies handler-side that the step really is that execution's. authorizesTaskScopedCall's sanctioned widenings are now held as a lookup table so the next one stops growing a switch. - The PRD's wire contract is StartStep(execution, attempt, parent, kind, name) -> step id; the responses also carry the persisted event (needed for the post-commit publish) and FinishStep's request carries execution_id (needed for the grant check) -- both noted here as deliberate additions to the PRD's shorthand. called_execution_id exists in the table but not yet on the wire: its named consumer is the workflow-call recording of plan item 4 (#1216). Conformance drives both adapters; grant tests pin the own-execution scoping; the wire sentinel set gains ErrInvalidStep (InvalidArgument, "step execution invalid") for a step kind no consumer names.
166 lines
4.4 KiB
Go
166 lines
4.4 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 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 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 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
|
|
}
|