* fix(webui): accept the merge task action The action whitelist omitted merge, so the dashboard's Merge button on a pr_open task was refused with 400 unknown action. Closes: archie-core-slpoz * fix(workflow): give each parallel branch its own outputs Branches shared the task's Outputs map, so concurrent declared-output writes raced (or were lost when the map started nil). Each branch now writes a copy; join merges new outputs in branch-name order and refuses one written by two branches as a second write. Closes: archie-core-3a7w2 * fix(postgres): dispatch captures oldest first and keep pending ones ListUndispatchedCaptures ordered newest first and listed captures no binding can match (failed HMAC), so sustained intake starved older work and bad signatures held the batch. It is now oldest first and lists only dispatchable captures. Count retention no longer evicts a capture that is still pending for an armed binding. The daemon lists only sources with an enabled workflow, so captures waiting on a disabled one do not hold the oldest-first batch. Closes: archie-core-2et4b, archie-core-o0s0z * fix(ui): refuse renaming a workflow onto an existing id Saving upserts by id, so editing a workflow's id to another workflow's id removed the open one and overwrote the other. The editor now refuses that save and says why. Closes: archie-core-wpoz8 * fix(workflow): reject unknown step settings at save Step factories decoded settings with yaml.Node.Decode, which drops keys it does not know, and settings-less steps ignored settings entirely, so a misspelt key (readOnly for read_only) saved and silently did nothing. Every factory now decodes strictly and settings-less steps refuse settings; ParseDefinition runs the factories, so the save refuses them. Closes: archie-core-8b5nk * fix(postgres): keep a stored capture when its prune fails InsertCapture committed the capture, then returned the prune's error, so intake answered 500 and a sender without a delivery header retried into a duplicate capture and a duplicate dispatch. A failed prune is now logged and the stored capture's id returned. Closes: archie-core-3miuh * fix(crondelivery): fail and advance a job no runner can deliver Router.Run returned nil for a kind with no runner, so the engine reported a successful run while the schedule never advanced and the job fired on every tick. The run now fails with the refusal, which the engine reports, and the schedule advances (a one-shot is removed). The router's own refusal event is gone with it. Closes: archie-core-s6ahh * fix(taskactions): stop the runs of cancelled callees CancelExecution cancelled a caller's waited-on callees in the store, but only the caller's run was stopped, so callee containers ran to the end. CancelExecution now returns the callees it cancelled (nested included) in place of the step ids nobody read, and every cancelling action stops their runs too. Closes: archie-core-9labz * fix(workflow): treat a zero json.Number as false in when Task inputs and step results decode numbers as json.Number, which truthy did not unpack, so when: inputs.count held for a count of 0. Closes: archie-core-uv270 * fix(webui): read a workflow's runs with a workflow-filtered page The workflows page and the canvas run picker filtered the newest 100 tasks of every workflow client-side, so on a busy instance a workflow's older runs were invisible. GET /api/tasks takes a workflow filter, carried through the State Store's Tasks request to the query, and both views ask for the selected workflow's runs. Closes: archie-core-tpiw1 * fix(ui): load the restored workflow into the editor Restore Defaults saved the shipped definition but left the open draft in the editor, still marked unsaved, so a later save re-applied it. The editor now reloads the restored definition. Closes: archie-core-85e7w
370 lines
13 KiB
Go
370 lines
13 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
task "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
// The events land in the table; the caller publishes the returned event to its
|
|
// bus after the write commits, and the daemon's event sink skips
|
|
// already-persisted rows by their assigned ID -- the EmitDurable convention.
|
|
|
|
// StartStep creates a step and moves it to running, appending stage_start in
|
|
// the same transaction. Org and workspace come from the execution row.
|
|
func (s *Store) StartStep(ctx context.Context, start task.StepStart) (int64, events.Event, error) {
|
|
if !task.ValidStepKind(start.Kind) {
|
|
return 0, events.Event{}, fmt.Errorf("%w: unknown step kind %q", storecontract.ErrInvalidStep, start.Kind)
|
|
}
|
|
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
q := postgresdb.New(tx)
|
|
|
|
execution, err := q.LockExecutionForStep(ctx, start.ExecutionID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
// The execution the caller names does not exist: its view of the run
|
|
// is stale, matching what guardTransition returns for a missing row.
|
|
return 0, events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
if execution.Attempt != int64(start.Attempt) {
|
|
// A step belongs to exactly one attempt; a caller naming one the
|
|
// execution no longer runs is describing a run that is over.
|
|
return 0, events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
if err := s.verifyCallStep(ctx, q, start); err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
|
|
depth, err := s.startParent(ctx, q, start)
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
// A step cannot reach running while its execution is not running, and
|
|
// never under a terminal parent. Both are the state machine's rules, so
|
|
// they refuse as illegal transitions.
|
|
if err := taskstate.CheckStepStart(execution.Status, depth.underTerminal); err != nil {
|
|
return 0, events.Event{}, fmt.Errorf("%w: %w", storecontract.ErrIllegalTransition, err)
|
|
}
|
|
|
|
stepID, err := q.InsertStepExecution(ctx, postgresdb.InsertStepExecutionParams{
|
|
OrgID: execution.OrgID, WorkspaceID: execution.WorkspaceID,
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt,
|
|
ParentID: stepParentID(start.ParentID), Depth: int32(depth.depth),
|
|
Kind: start.Kind, Name: start.Name, CalledExecutionID: start.CalledExecutionID,
|
|
})
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
n, err := q.StartStepExecution(ctx, stepID)
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
if n == 0 {
|
|
return 0, events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
|
|
event := stepEvent(events.KindStageStart, execution.ID,
|
|
execution.Owner, execution.Repo, execution.IssueNumber, execution.Workflow,
|
|
int(execution.Attempt), start.Name, nil)
|
|
eventID, err := insertEventQ(ctx, q, event)
|
|
if err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
event.ID = eventID
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return 0, events.Event{}, err
|
|
}
|
|
return stepID, event, nil
|
|
}
|
|
|
|
// verifyCallStep refuses a call step whose callee is not this execution's
|
|
// own.
|
|
func (s *Store) verifyCallStep(ctx context.Context, q *postgresdb.Queries, start task.StepStart) error {
|
|
if start.CalledExecutionID == 0 {
|
|
return nil
|
|
}
|
|
calleeParent, err := q.StepCalleeParent(ctx, start.CalledExecutionID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return storecontract.ErrStaleTransition
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if calleeParent != start.ExecutionID {
|
|
return storecontract.ErrStaleTransition
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// stepParent resolves the enclosing StepExecution a child step names: its
|
|
// row is locked in the same transaction, the tree must stay inside one
|
|
// execution and one attempt, and depth is derived from the parent, never
|
|
// supplied by the caller.
|
|
func (s *Store) startParent(ctx context.Context, q *postgresdb.Queries, start task.StepStart) (stepParentState, error) {
|
|
if start.ParentID == 0 {
|
|
return stepParentState{}, nil
|
|
}
|
|
parent, err := q.LockStepExecution(ctx, start.ParentID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return stepParentState{}, storecontract.ErrStaleTransition
|
|
}
|
|
if err != nil {
|
|
return stepParentState{}, err
|
|
}
|
|
if parent.ExecutionID != start.ExecutionID || parent.Attempt != int64(start.Attempt) {
|
|
// Trees never nest across executions, and a parent from another
|
|
// attempt is a tree the caller is not running.
|
|
return stepParentState{}, storecontract.ErrStaleTransition
|
|
}
|
|
return stepParentState{
|
|
depth: int(parent.Depth) + 1,
|
|
underTerminal: taskstate.StepTerminal(taskstate.StepStatus(parent.Status)),
|
|
}, nil
|
|
}
|
|
|
|
type stepParentState struct {
|
|
depth int
|
|
underTerminal bool
|
|
}
|
|
|
|
// FinishStep moves a step to its outcome and returns the stage_finish event.
|
|
// A missing, foreign or mismatched step is stale; a disallowed pair is
|
|
// illegal.
|
|
func (s *Store) FinishStep(ctx context.Context, finish task.StepFinish) (events.Event, error) {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
q := postgresdb.New(tx)
|
|
|
|
step, err := q.LockStepExecution(ctx, finish.StepID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
if err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
// The step belongs to another run, or the caller believes a state the row
|
|
// does not hold. Either way its view is stale, and writing would move
|
|
// another execution's record.
|
|
if step.ExecutionID != finish.ExecutionID || taskstate.StepStatus(step.Status) != finish.From {
|
|
return events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
// Staleness is decided first, the way guardTransition decides it: a caller
|
|
// that is wrong about the row's state gets the stale sentinel even when
|
|
// its pair is also unroutable.
|
|
if !taskstate.CanStepTransition(finish.From, finish.To) {
|
|
return events.Event{}, fmt.Errorf("%w: %s -> %s is not a step transition",
|
|
storecontract.ErrIllegalTransition, finish.From, finish.To)
|
|
}
|
|
execution, err := q.TaskByID(ctx, postgresdb.TaskByIDParams{ID: step.ExecutionID, ScopeOrg: scopeOrg(ctx)})
|
|
if err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
|
|
durationMS := int64(0)
|
|
if step.StartedAt.Valid {
|
|
durationMS = time.Since(step.StartedAt.Time).Milliseconds()
|
|
}
|
|
n, err := q.FinishStepExecution(ctx, postgresdb.FinishStepExecutionParams{
|
|
Status: string(finish.To), Detail: clip(finish.Detail, 4000), TokensUsed: finish.TokensUsed,
|
|
ID: finish.StepID, Status_2: string(finish.From),
|
|
Results: finish.Results,
|
|
})
|
|
if err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
if n == 0 {
|
|
return events.Event{}, storecontract.ErrStaleTransition
|
|
}
|
|
|
|
event := stepEvent(events.KindStageFinish, execution.ID,
|
|
execution.Owner, execution.Repo, execution.IssueNumber, execution.Workflow,
|
|
int(step.Attempt), step.Name,
|
|
stepEventData(finish.To, durationMS, finish.Detail))
|
|
eventID, err := insertEventQ(ctx, q, event)
|
|
if err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
event.ID = eventID
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return events.Event{}, err
|
|
}
|
|
return event, nil
|
|
}
|
|
|
|
// stepEvent builds the event row one step transition writes, stamped with the
|
|
// execution's identity the way TaskContext.Emit stamped it -- the same fields
|
|
// the timeline and the stage statistics read back.
|
|
func stepEvent(kind string, executionID int64, owner, repo string, issue int64, wf string, attempt int, stage string, data map[string]any) events.Event {
|
|
return events.Event{
|
|
Kind: kind, TaskID: executionID,
|
|
Repo: owner + "/" + repo, Issue: int(issue), Workflow: wf,
|
|
Attempt: attempt, Stage: stage, Data: data,
|
|
}
|
|
}
|
|
|
|
// stepEventData reads a stage_finish event's data the way the dashboard
|
|
// (stageOutcome) and the stage statistics (StageStats) do: duration always,
|
|
// the error text only when the step reported failure, and the interruption
|
|
// marker only for an interrupted step.
|
|
func stepEventData(to taskstate.StepStatus, durationMS int64, detail string) map[string]any {
|
|
data := map[string]any{"duration_ms": durationMS}
|
|
switch to {
|
|
case taskstate.StepFailed:
|
|
data["error"] = detail
|
|
case taskstate.StepInterrupted:
|
|
data["interrupted"] = true
|
|
case taskstate.StepSkipped:
|
|
// The detail is the when condition that skipped the step.
|
|
data["skipped"] = true
|
|
data["when"] = detail
|
|
}
|
|
return data
|
|
}
|
|
|
|
// ListSteps returns an execution's steps, for every attempt when attempt is
|
|
// 0.
|
|
func (s *Store) ListSteps(ctx context.Context, executionID int64, attempt int) ([]task.StepExecution, error) {
|
|
rows, err := s.queries().ListStepExecutions(ctx, postgresdb.ListStepExecutionsParams{
|
|
ScopeOrg: scopeOrg(ctx),
|
|
ExecutionID: executionID, Attempt: int64(attempt),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
steps := make([]task.StepExecution, len(rows))
|
|
for i, r := range rows {
|
|
steps[i] = task.StepExecution{
|
|
ID: r.ID, ExecutionID: r.ExecutionID, Attempt: int(r.Attempt),
|
|
ParentID: nullableInt64(r.ParentID), Depth: int(r.Depth),
|
|
Kind: r.Kind, Name: r.Name, Status: taskstate.StepStatus(r.Status),
|
|
Detail: r.Detail, TokensUsed: r.TokensUsed,
|
|
StartedAt: r.StartedAt.Time, FinishedAt: r.FinishedAt.Time,
|
|
CalledExecutionID: r.CalledExecutionID,
|
|
}
|
|
}
|
|
return steps, nil
|
|
}
|
|
|
|
// nullableInt64 reads a pgtype.Int8 the way stepParentID wrote it: absent
|
|
// means 0, a stage at the tree's root.
|
|
func nullableInt64(v pgtype.Int8) int64 {
|
|
if !v.Valid {
|
|
return 0
|
|
}
|
|
return v.Int64
|
|
}
|
|
|
|
// stepParentID encodes the nullable parent id an insert takes: 0 means "no
|
|
// parent" -- a stage at the tree's root -- which the column stores as NULL
|
|
// rather than as a reference to a row that does not exist.
|
|
func stepParentID(parentID int64) pgtype.Int8 {
|
|
return pgtype.Int8{Int64: parentID, Valid: parentID != 0}
|
|
}
|
|
|
|
// CancelExecution cancels the current attempt's open steps and moves the
|
|
// execution to `to`, in one transaction with events and an audit row, then
|
|
// cancels the callees it was waiting on. It returns the cancelled callees'
|
|
// ids, nested callees included.
|
|
func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
q := postgresdb.New(tx)
|
|
|
|
execution, err := q.LockExecutionForStep(ctx, taskID)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, storecontract.ErrStaleTransition
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !taskstate.CanTransition(execution.Status, to) {
|
|
return nil, fmt.Errorf("%w: %s -> %s is not a transition",
|
|
storecontract.ErrIllegalTransition, execution.Status, to)
|
|
}
|
|
// Read the wait:true callees before the sweep below closes them: an open
|
|
// call step is a callee the caller waits on, and one whose call step
|
|
// already closed -- wait:false, or one that ended first -- runs on.
|
|
waiting, err := q.WaitingCallSteps(ctx, postgresdb.WaitingCallStepsParams{
|
|
ExecutionID: taskID, Attempt: execution.Attempt,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
steps, err := q.CancelAttemptSteps(ctx, postgresdb.CancelAttemptStepsParams{
|
|
ExecutionID: taskID, Attempt: execution.Attempt,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, step := range steps {
|
|
if _, err := insertEventQ(ctx, q, stepEvent(events.KindStageFinish, execution.ID,
|
|
execution.Owner, execution.Repo, execution.IssueNumber, execution.Workflow,
|
|
int(execution.Attempt), step.Name, map[string]any{"cancelled": true})); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
n, err := q.TransitionTask(ctx, postgresdb.TransitionTaskParams{
|
|
ID: taskID, Status: to, ParkReason: clip(reason, 4000),
|
|
ParkClass: taskstate.ParkNeedsHuman, Status_2: execution.Status,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if n == 0 {
|
|
return nil, storecontract.ErrStaleTransition
|
|
}
|
|
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
|
TaskID: taskID, FromStatus: execution.Status, ToStatus: to, Detail: clip(reason, 4000),
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.cancelWaitedCallees(ctx, waiting, reason, to)
|
|
}
|
|
|
|
// cancelWaitedCallees cancels each callee the caller was waiting on and
|
|
// returns the ids of those it cancelled, nested callees included. Failures are
|
|
// reported; the caller's cancel stands.
|
|
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) ([]int64, error) {
|
|
var cancelled []int64
|
|
var failures []error
|
|
for _, call := range waiting {
|
|
// A callee is returned even when its cancel failed: stopping a run
|
|
// that already ended does nothing.
|
|
nested, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to)
|
|
if err != nil {
|
|
failures = append(failures, fmt.Errorf("cancel callee %d: %w", call.CalledExecutionID, err))
|
|
}
|
|
cancelled = append(cancelled, call.CalledExecutionID)
|
|
cancelled = append(cancelled, nested...)
|
|
}
|
|
return cancelled, errors.Join(failures...)
|
|
}
|