Plan item 4 of docs/prds/execution-tree-state-machine.md (archie-core-1216), plus the callee half of CancelExecution that plan item 5 deferred until the tree could decide it. Every agent call a stage makes records itself: AgentStage's bridge, the baseline-fix auto-fix run, the adversarial reviewer and a workflow.call each start a child StepExecution of kind agent or call under the stage's own step -- TaskContext.StepID, which the engine sets before the stage body starts and clears when it returns -- and finish it with the call's own outcome and the tokens it accounted. A child's recording write failure parks the execution the same way a stage's does: an agent call that ran unrecorded is the invisibility the step table exists to end. A stage invoked outside the engine's recorded run -- the stage unit tests -- records no child and runs unchanged. The call step is what makes the callee half of the PRD's Cancellation section decidable, and this lands it: a call step closes when its callee ends, so an open call step is a callee the caller waits on (wait:true) and a closed one is a wait:false callee that runs on. CancelExecution reads the open call steps before its sweep closes them and cancels each named callee through its own CancelExecution in the caller's write's aftermath -- the callee's own steps, its own events, its own move to the same destination, and the callee's own wait:true callees beyond it, the depth the enqueue enforces bounding the recursion. A callee that cannot be cancelled is reported and the caller's own record stands. StartStep refuses a call step whose callee is not this run's own (the callee's call_parent_task_id must name the recording execution): a compromised container could otherwise record a step naming a foreign execution and cancel it. StartStepRequest gains called_execution_id (field 6; non-breaking, the column and its named consumer already exist) and StepStart carries it. PRD decisions recorded here: - The reviewer records as one agent child named "review": the Reviewer contract runs one subagent per review, the PRD model's review:1/review:2 are illustrative of depth-1 children. A completed report closes it succeeded, a not-run review failed with the skip reason; the contract carries no usage, so tokens are 0. - A failed-callee call step closes as failed with the callee's detail when the wait ends otherwise; the single open case is the caller itself being cancelled, whose CancelExecution already swept this step -- a late write must not pretend the wait is still the caller's business. - A call step's callee is cancelled to the same destination the caller's action named (parked stays parked, closed_wont_do ends the whole chain), and a callee that cannot be cancelled is reported while the caller's own record stands either way. - The agent child's detail is the result summary on success and the error's text on failure, with the call's own TokensUsed -- the PRD's only usage field, untracked per call for the reviewer contract.
195 lines
7.2 KiB
Go
195 lines
7.2 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/agentexec"
|
|
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")
|
|
|
|
// The agent-call children (docs/prds/execution-tree-state-machine.md, "Model"):
|
|
// every agent call a stage makes is its own StepExecution of kind agent,
|
|
// parented to the stage's recorded step, with the call's own outcome and the
|
|
// tokens it accounted. Stage "implement" carries agent "implement" one level
|
|
// down, the way the model draws it.
|
|
func TestRunRecordsAgentChildrenUnderTheStageStep(t *testing.T) {
|
|
store := &recordingStore{}
|
|
wf := Workflow{Name: "recording", Stages: []Stage{{
|
|
Name: "implement",
|
|
Run: func(ctx context.Context, tc *TaskContext) error {
|
|
_, err := tc.RunAgentChild(ctx, "implement", func() (agentexec.Result, error) {
|
|
return agentexec.Result{TokensUsed: 120, Summary: "built"}, nil
|
|
})
|
|
return err
|
|
},
|
|
}}}
|
|
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 {
|
|
t.Fatalf("steps = %+v, want the stage and its agent child", store.steps)
|
|
}
|
|
stageStep, child := store.steps[0], store.steps[1]
|
|
if stageStep.Kind != StepKindStage || stageStep.Name != "implement" {
|
|
t.Errorf("stage step = %+v, want the recorded stage", stageStep)
|
|
}
|
|
if child.Kind != StepKindAgent || child.Name != "implement" {
|
|
t.Errorf("child step = %+v, want the agent call", child)
|
|
}
|
|
// The parent is the stage's own step: the store derives depth from it.
|
|
if child.ParentID == 0 || child.ParentID == stageStep.ExecutionID {
|
|
t.Errorf("child parent = %d, want the stage step's id, not the execution's", child.ParentID)
|
|
}
|
|
if len(store.finishes) != 2 {
|
|
t.Fatalf("finishes = %+v, want the stage's and the child's", store.finishes)
|
|
}
|
|
var childFinish StepFinish
|
|
for _, f := range store.finishes {
|
|
if f.StepID == child.ParentID+1 {
|
|
childFinish = f
|
|
}
|
|
}
|
|
if childFinish.To != "succeeded" || childFinish.TokensUsed != 120 {
|
|
t.Errorf("child finish = %+v, want succeeded with the call's own usage", childFinish)
|
|
}
|
|
}
|
|
|
|
// TestRunParksWhenAChildStepWriteFails: a child's recording write parks the
|
|
// execution the same way a stage's does -- an agent call that ran unrecorded
|
|
// is exactly the invisibility the step table exists to end.
|
|
func TestRunParksWhenAChildStepWriteFails(t *testing.T) {
|
|
store := &recordingStore{finishEr: errStepWrite}
|
|
wf := Workflow{Name: "recording", Stages: []Stage{{
|
|
Name: "implement",
|
|
Run: func(ctx context.Context, tc *TaskContext) error {
|
|
_, err := tc.RunAgentChild(ctx, "implement", func() (agentexec.Result, error) {
|
|
return agentexec.Result{}, nil
|
|
})
|
|
return err
|
|
},
|
|
}}}
|
|
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 len(store.transitions) == 0 || store.transitions[0].to != StatusParked {
|
|
t.Fatalf("transitions = %+v, want the execution parked after the child's recording failed", store.transitions)
|
|
}
|
|
}
|