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.
117 lines
4.2 KiB
Go
117 lines
4.2 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"slices"
|
|
"testing"
|
|
|
|
task "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
)
|
|
|
|
// The stage-recording half of docs/prds/execution-tree-state-machine.md:
|
|
// workflow.Run records every stage it runs as a StepExecution through the
|
|
// store, and a failed step write parks the execution -- the step does not run
|
|
// unrecorded.
|
|
|
|
// TestRunRecordsEveryStageAsAStepExecution pins the recording shape: one
|
|
// StartStep before each stage's body and one FinishStep with that stage's
|
|
// outcome, both of kind stage, against the task's own execution and attempt.
|
|
func TestRunRecordsEveryStageAsAStepExecution(t *testing.T) {
|
|
store := &recordingStore{}
|
|
stage := func(name string) Stage {
|
|
return Stage{Name: name, Run: func(context.Context, *TaskContext) error { return nil }}
|
|
}
|
|
wf := Workflow{Name: "recording", Stages: []Stage{stage("plan"), stage("implement")}}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 41, Attempt: 3, Owner: "acme", Repo: "widgets", Status: StatusRunning},
|
|
Store: store, Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
|
|
Run(context.Background(), wf, tc)
|
|
|
|
if len(store.steps) != 2 || len(store.finishes) != 2 {
|
|
t.Fatalf("steps = %+v finishes = %+v, want one recorded start and finish per stage", store.steps, store.finishes)
|
|
}
|
|
for i, step := range store.steps {
|
|
if step.ExecutionID != 41 || step.Attempt != 3 {
|
|
t.Errorf("stage %d: ExecutionID/Attempt = %d/%d, want the run's own 41/3", i, step.ExecutionID, step.Attempt)
|
|
}
|
|
if step.Kind != task.StepKindStage {
|
|
t.Errorf("stage %d: kind = %q, want %q", i, step.Kind, task.StepKindStage)
|
|
}
|
|
if want := []string{"plan", "implement"}[i]; step.Name != want {
|
|
t.Errorf("stage %d: name = %q, want %q", i, step.Name, want)
|
|
}
|
|
}
|
|
if got := store.finishes[0].To; got != "succeeded" {
|
|
t.Errorf("stage finish to = %q, want succeeded", got)
|
|
}
|
|
}
|
|
|
|
// TestRunPublishesTheRecordedEvents pins the post-commit half: the events the
|
|
// store returned, already persisted, are what reach the bus -- not a second,
|
|
// unpersisted copy emitted beside the store write.
|
|
func TestRunPublishesThePersistedStepEvents(t *testing.T) {
|
|
store := &recordingStore{}
|
|
wf := Workflow{Name: "recording", Stages: []Stage{{
|
|
Name: "implement", Run: func(context.Context, *TaskContext) error { return nil },
|
|
}}}
|
|
bus := events.NewBus()
|
|
sub := bus.Subscribe(16)
|
|
defer sub.Close()
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 7, Attempt: 1, Owner: "acme", Repo: "widgets", Status: StatusRunning},
|
|
Store: store, Bus: bus, Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
|
|
Run(context.Background(), wf, tc)
|
|
|
|
var seen []string
|
|
ids := map[string]int64{}
|
|
for len(sub.C) > 0 {
|
|
e := <-sub.C
|
|
seen = append(seen, e.Kind)
|
|
ids[e.Kind] = e.ID
|
|
}
|
|
for _, want := range []string{events.KindStageStart, events.KindStageFinish} {
|
|
if !slices.Contains(seen, want) {
|
|
t.Errorf("bus saw %v, want %q among them: the persisted events are what reach the bus", seen, want)
|
|
}
|
|
if ids[want] == 0 {
|
|
t.Errorf("bus event %q carries no ID: an event without the store's assigned ID would be inserted a second time by the daemon's event sink", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRunParksWhenAStepWriteFails is the PRD's persistence rule: a failed
|
|
// step write parks the execution, and the step does not run unrecorded.
|
|
func TestRunParksWhenAStepWriteFails(t *testing.T) {
|
|
store := &recordingStore{stepEr: errStepWrite}
|
|
stageRan := false
|
|
wf := Workflow{Name: "recording", Stages: []Stage{{
|
|
Name: "implement",
|
|
Run: func(context.Context, *TaskContext) error { stageRan = true; return nil },
|
|
}}}
|
|
tc := &TaskContext{
|
|
Task: &Task{ID: 9, Attempt: 1, Owner: "acme", Repo: "widgets", Status: StatusRunning},
|
|
Store: store, Log: slog.New(slog.DiscardHandler),
|
|
}
|
|
|
|
Run(context.Background(), wf, tc)
|
|
|
|
if stageRan {
|
|
t.Fatal("the stage ran despite its recording write failing; the step must not run unrecorded")
|
|
}
|
|
if len(store.transitions) == 0 || store.transitions[0].to != StatusParked {
|
|
t.Fatalf("transitions = %+v, want the execution parked", store.transitions)
|
|
}
|
|
if len(store.finishes) != 0 {
|
|
t.Errorf("finishes = %+v, want none: the stage never ran", store.finishes)
|
|
}
|
|
}
|
|
|
|
var errStepWrite = errors.New("recording write failed")
|