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.
315 lines
11 KiB
Go
315 lines
11 KiB
Go
package postgres
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
task "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
// The step-execution writes (docs/prds/execution-tree-state-machine.md): each
|
|
// of StartStep and FinishStep is one transaction -- guarded row write plus the
|
|
// domain event row -- under the step transition table, with the execution's
|
|
// own status gating whether a step may run at all.
|
|
|
|
// runningExecution enqueues and claims one task, the state a step runs in.
|
|
func runningExecution(t *testing.T, s *Store) *workflow.Task {
|
|
t.Helper()
|
|
seed, err := s.EnqueueChatTask(t.Context(), "acme", "widgets", "t", "b", "implement", "")
|
|
if err != nil || seed == nil {
|
|
t.Fatalf("EnqueueChatTask: %+v %v", seed, err)
|
|
}
|
|
claimed, err := s.ClaimNext(t.Context())
|
|
if err != nil || claimed == nil {
|
|
t.Fatalf("ClaimNext: %+v %v", claimed, err)
|
|
}
|
|
return claimed
|
|
}
|
|
|
|
func TestStartStepRecordsAStageEnteringRunning(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
|
|
stepID, startEvent, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "implement",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep: %v", err)
|
|
}
|
|
if startEvent.Kind != events.KindStageStart || startEvent.Stage != "implement" ||
|
|
startEvent.TaskID != execution.ID || startEvent.Attempt != execution.Attempt ||
|
|
startEvent.Repo != "acme/widgets" || startEvent.ID == 0 {
|
|
t.Fatalf("stage_start event = %+v, want the transition's row", startEvent)
|
|
}
|
|
|
|
eventsForTask, err := s.TaskEvents(t.Context(), execution.ID)
|
|
if err != nil {
|
|
t.Fatalf("TaskEvents: %v", err)
|
|
}
|
|
if n := len(eventsForTask); n != 1 {
|
|
t.Fatalf("events after StartStep = %d, want exactly the one the transition wrote", n)
|
|
}
|
|
|
|
step, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepSucceeded, Detail: "implemented",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("FinishStep: %v", err)
|
|
}
|
|
if step.Kind != events.KindStageFinish || step.Stage != "implement" || step.ID == 0 {
|
|
t.Fatalf("stage_finish event = %+v, want the transition's row", step)
|
|
}
|
|
if _, ok := step.Data["duration_ms"]; !ok {
|
|
t.Errorf("stage_finish data = %v, want duration_ms: the stage statistics read it", step.Data)
|
|
}
|
|
if _, has := step.Data["error"]; has {
|
|
t.Errorf("stage_finish data = %v, want no error key for a succeeded step", step.Data)
|
|
}
|
|
}
|
|
|
|
func TestFinishStepFailedCarriesTheErrorTheDashboardRenders(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
|
|
stepID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "review",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep: %v", err)
|
|
}
|
|
step, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepFailed, Detail: "review found 3 findings",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("FinishStep: %v", err)
|
|
}
|
|
if step.Data["error"] != "review found 3 findings" {
|
|
t.Errorf("stage_finish data = %v, want the error text stageOutcome renders", step.Data)
|
|
}
|
|
}
|
|
|
|
func TestStartStepRefusesWhatTheStateMachineForbids(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
seed func(s *Store, taskID int64) error
|
|
start task.StepStart
|
|
want error
|
|
}{
|
|
{
|
|
name: "an execution that is not running",
|
|
start: task.StepStart{Kind: task.StepKindStage, Name: "plan", Attempt: 1},
|
|
want: storecontract.ErrIllegalTransition,
|
|
},
|
|
{
|
|
name: "an execution that does not exist",
|
|
start: task.StepStart{Kind: task.StepKindStage, Name: "plan", Attempt: 1},
|
|
want: storecontract.ErrStaleTransition,
|
|
},
|
|
{
|
|
name: "an attempt the execution no longer runs",
|
|
start: task.StepStart{Kind: task.StepKindStage, Name: "plan", Attempt: 99},
|
|
want: storecontract.ErrStaleTransition,
|
|
},
|
|
{
|
|
name: "an unknown step kind",
|
|
start: task.StepStart{Kind: "quantum", Name: "plan", Attempt: 1},
|
|
want: storecontract.ErrInvalidStep,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
if tt.name == "an execution that is not running" {
|
|
// Back on the queue: nothing may run under it. The seed rides
|
|
// the legal running->queued edge, not a hand-written pair.
|
|
if err := s.Transition(t.Context(), execution.ID, taskstate.Running, taskstate.Queued, "requeue"); err != nil {
|
|
t.Fatalf("seed queued: %v", err)
|
|
}
|
|
}
|
|
start := tt.start
|
|
start.ExecutionID = execution.ID
|
|
if tt.name == "an execution that does not exist" {
|
|
start.ExecutionID = execution.ID + 999999
|
|
}
|
|
if _, _, err := s.StartStep(t.Context(), start); !errors.Is(err, tt.want) {
|
|
t.Fatalf("StartStep = %v, want %v", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStartStepUnderATerminalParentIsIllegal(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
|
|
parentID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "review",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep parent: %v", err)
|
|
}
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: parentID, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepSucceeded, Detail: "done",
|
|
}); err != nil {
|
|
t.Fatalf("FinishStep parent: %v", err)
|
|
}
|
|
|
|
_, _, err = s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt,
|
|
ParentID: parentID, Kind: task.StepKindAgent, Name: "review:2",
|
|
})
|
|
if !errors.Is(err, storecontract.ErrIllegalTransition) {
|
|
t.Fatalf("StartStep under a terminal parent = %v, want ErrIllegalTransition", err)
|
|
}
|
|
}
|
|
|
|
func TestStepDepthIsDerivedFromTheParent(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
|
|
parentID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "implement",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep stage: %v", err)
|
|
}
|
|
childID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt,
|
|
ParentID: parentID, Kind: task.StepKindAgent, Name: "implement",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep agent child: %v", err)
|
|
}
|
|
var depths map[int64]int
|
|
if err := s.pool.QueryRow(t.Context(),
|
|
`SELECT jsonb_object_agg(id, depth) FROM step_executions WHERE execution_id = $1`,
|
|
execution.ID).Scan(&depths); err != nil {
|
|
t.Fatalf("read depths: %v", err)
|
|
}
|
|
if depths[parentID] != 0 || depths[childID] != 1 {
|
|
t.Fatalf("depths = %v, want the stage at 0 and its child at 1", depths)
|
|
}
|
|
}
|
|
|
|
func TestFinishStepRefusesStaleAndForeign(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
other := runningExecution(t, s)
|
|
|
|
stepID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "plan",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep: %v", err)
|
|
}
|
|
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID, ExecutionID: other.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepSucceeded,
|
|
}); !errors.Is(err, storecontract.ErrStaleTransition) {
|
|
t.Fatalf("FinishStep naming another execution = %v, want ErrStaleTransition", err)
|
|
}
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID, ExecutionID: execution.ID,
|
|
From: taskstate.StepSucceeded, To: taskstate.StepFailed,
|
|
}); !errors.Is(err, storecontract.ErrStaleTransition) {
|
|
t.Fatalf("FinishStep with an untruthful from = %v, want ErrStaleTransition", err)
|
|
}
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepPending,
|
|
}); !errors.Is(err, storecontract.ErrIllegalTransition) {
|
|
t.Fatalf("FinishStep off-table pair = %v, want ErrIllegalTransition", err)
|
|
}
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: stepID + 999999, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepSucceeded,
|
|
}); !errors.Is(err, storecontract.ErrStaleTransition) {
|
|
t.Fatalf("FinishStep on a missing step = %v, want ErrStaleTransition", err)
|
|
}
|
|
}
|
|
|
|
// TestListStepsOrdersByAttemptThenID is the dashboard run detail's
|
|
// authoritative read (docs/prds/execution-tree-state-machine.md): every
|
|
// attempt in one call when attempt is 0, one attempt's steps when it names
|
|
// one, both ordered oldest first.
|
|
func TestListStepsOrdersByAttemptThenID(t *testing.T) {
|
|
s := storeFor(t)
|
|
execution := runningExecution(t, s)
|
|
|
|
firstStepID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "plan",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep: %v", err)
|
|
}
|
|
if _, err := s.FinishStep(t.Context(), task.StepFinish{
|
|
StepID: firstStepID, ExecutionID: execution.ID,
|
|
From: taskstate.StepRunning, To: taskstate.StepSucceeded, Detail: "planned", TokensUsed: 42,
|
|
}); err != nil {
|
|
t.Fatalf("FinishStep: %v", err)
|
|
}
|
|
secondStepID, _, err := s.StartStep(t.Context(), task.StepStart{
|
|
ExecutionID: execution.ID, Attempt: execution.Attempt, Kind: task.StepKindStage, Name: "implement",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("StartStep: %v", err)
|
|
}
|
|
|
|
steps, err := s.ListSteps(t.Context(), execution.ID, execution.Attempt)
|
|
if err != nil {
|
|
t.Fatalf("ListSteps: %v", err)
|
|
}
|
|
if len(steps) != 2 {
|
|
t.Fatalf("ListSteps len = %d, want 2", len(steps))
|
|
}
|
|
if steps[0].ID != firstStepID || steps[0].Name != "plan" || steps[0].Status != taskstate.StepSucceeded ||
|
|
steps[0].Detail != "planned" || steps[0].TokensUsed != 42 || steps[0].StartedAt.IsZero() || steps[0].FinishedAt.IsZero() {
|
|
t.Fatalf("steps[0] = %+v, want the finished plan step", steps[0])
|
|
}
|
|
if steps[1].ID != secondStepID || steps[1].Name != "implement" || steps[1].Status != taskstate.StepRunning ||
|
|
steps[1].StartedAt.IsZero() || !steps[1].FinishedAt.IsZero() {
|
|
t.Fatalf("steps[1] = %+v, want the still-running implement step", steps[1])
|
|
}
|
|
|
|
// attempt 0 lists every attempt; there is only one here, so the result is
|
|
// the same, proving the "list everything" branch is reachable and correct
|
|
// rather than coincidentally equal to a hardcoded attempt.
|
|
all, err := s.ListSteps(t.Context(), execution.ID, 0)
|
|
if err != nil {
|
|
t.Fatalf("ListSteps(attempt=0): %v", err)
|
|
}
|
|
if len(all) != 2 {
|
|
t.Fatalf("ListSteps(attempt=0) len = %d, want 2", len(all))
|
|
}
|
|
|
|
// A different execution's steps never appear, however queried.
|
|
other := runningExecution(t, s)
|
|
otherSteps, err := s.ListSteps(t.Context(), other.ID, 0)
|
|
if err != nil {
|
|
t.Fatalf("ListSteps other execution: %v", err)
|
|
}
|
|
if len(otherSteps) != 0 {
|
|
t.Fatalf("ListSteps other execution = %+v, want none", otherSteps)
|
|
}
|
|
}
|
|
|
|
// A late FinishStep for a step another path already finished is stale, the
|
|
// PRD's named wire behaviour: errors.Is matches on the client too.
|
|
func TestFinishStepOffTablePairIsIllegalOverTheTable(t *testing.T) {
|
|
if !taskstate.CanStepTransition(taskstate.StepPending, taskstate.StepRunning) {
|
|
t.Fatal("pending -> running must be in the step transition table")
|
|
}
|
|
if taskstate.CanStepTransition(taskstate.StepSucceeded, taskstate.StepRunning) {
|
|
t.Fatal("succeeded -> running must not be in the step transition table: terminal is terminal")
|
|
}
|
|
}
|