Files
archie/modules/runs/steps_test.go
T
sam a88f5630fe feat(runs): the first step that sets an outcome ends the run (#2042)
Add workflow.finish (completed, parked, waiting_approval, declined). A failed
step parks the run unless it continues on failure, and a parked run retries
from a top-level step, replaying the earlier steps' recorded results. Adds the
retry-run operation and a Retry button on the run page.
2026-11-21 13:52:52 +11:00

358 lines
13 KiB
Go

package runs
import (
"cmp"
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"strings"
"testing"
"github.com/google/uuid"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/deliveries"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// actionJudge decides each action at the level named for it and remembers
// every request, so a test sees what a step asked for.
type actionJudge struct {
levels map[string]judgement.Level
fail error
// failAction limits fail to the action with this name; empty fails them all.
failAction string
assessed []judgement.Request
decisions []judgement.Decision
executed int
}
func (j *actionJudge) Assess(_ context.Context, req judgement.Request) (judgement.Decision, error) {
if j.fail != nil && (j.failAction == "" || j.failAction == req.Action) {
return judgement.Decision{}, j.fail
}
j.assessed = append(j.assessed, req)
d := judgement.Decision{Key: req.Key, Level: j.levels[req.Action], Action: judgement.Action{
Connector: req.Connector, Name: req.Action, Input: req.Input, Digest: req.Action,
}}
j.decisions = append(j.decisions, d)
return d, nil
}
func (j *actionJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) {
j.executed++
return "done", nil
}
func (j *actionJudge) ExecuteApproved(context.Context, judgement.Decision, judgement.Approval, jsontext.Value, string) (string, error) {
j.executed++
return "done", nil
}
func (j *actionJudge) ForRun(context.Context, uuid.UUID) ([]judgement.Decision, error) {
return j.decisions, nil
}
// askCounter approves every request at once and counts them. failFirst makes
// that many requests fail first, as an approvals store that is briefly down.
type askCounter struct {
asked int
failFirst int
}
func (a *askCounter) Ask(_ context.Context, _ uuid.UUID, _ string, d judgement.Decision, _ []governance.PathStep) (approvals.Request, error) {
a.asked++
if a.failFirst > 0 {
a.failFirst--
return approvals.Request{}, errors.New("approvals unavailable")
}
return approvals.Request{ID: uuid.New(), State: approvals.StatePending, Decision: d}, nil
}
func (a *askCounter) Escalate(context.Context, uuid.UUID, int, governance.PathStep) error { return nil }
func (a *askCounter) Expire(context.Context, uuid.UUID) error { return nil }
func (a *askCounter) Accept(context.Context, uuid.UUID) error { return nil }
func (a *askCounter) Discard(context.Context, uuid.UUID, string) error { return nil }
// onePersonPath sends every request to one person.
type onePersonPath struct{}
func (onePersonPath) AgentModelForAlias(context.Context, string) (governance.Model, error) {
return governance.Model{}, errors.New("no agent in these workflows")
}
func (onePersonPath) AgentProvider(context.Context, governance.Model) (chat.Provider, chat.ModelInfo, error) {
return nil, chat.ModelInfo{}, errors.New("no agent in these workflows")
}
func (onePersonPath) EscalationPath(context.Context, string, bool) ([]governance.PathStep, error) {
return []governance.PathStep{{Person: "p"}}, nil
}
func (onePersonPath) RecurrenceThreshold(context.Context) (int, error) { return 0, nil }
func (onePersonPath) AttemptsPerRun(context.Context) (int, error) { return 1, nil }
type notices struct{ sent []deliveries.Notification }
func (n *notices) Notify(_ context.Context, msg deliveries.Notification) error {
n.sent = append(n.sent, msg)
return nil
}
// TestDocumentStepsRun guards the workflow step contract on the engine: a
// step's settings are rendered from the run's inputs and earlier results, its
// condition decides whether it runs, a failure ends the run unless the step
// continues, and every action judgement holds is put to people exactly once,
// by an approval step or before the run ends. The first step that sets an
// outcome ends the run, a failed step parks it, and a parked run retried from a
// step keeps the results of the steps before it.
func TestDocumentStepsRun(t *testing.T) {
input := jsontext.Value(`{"event":{"fields":{"host":"web-1"}}}`)
tests := []struct {
name string
doc string
levels map[string]judgement.Level
fail error
failAction string
failFirst int
// retry retries the parked run from the named step once the failure
// has cleared; the counts below cover both attempts.
retry string
wantState string
wantDetail string
wantInput string
wantAssessed int
wantAsked int
wantExecuted int
wantNotice string
}{
{
name: "an action's input is rendered from the run's inputs",
doc: `inputs: {event: {type: object}}
steps:
- {type: connector.action, settings: {connector: host, name: clean, input: {host: "{{ inputs.event.fields.host }}"}}}
`,
levels: map[string]judgement.Level{"clean": judgement.ActQuietly},
wantInput: `{"host":"web-1"}`, wantAssessed: 1, wantExecuted: 1,
},
{
name: "a step whose condition doesn't hold is skipped",
doc: `inputs: {event: {type: object}}
steps:
- {type: connector.action, when: "inputs.event.fields.missing", settings: {connector: host, name: clean}}
`,
},
{
name: "a failing step parks the run and later steps do not run",
doc: `steps:
- {type: connector.action, settings: {connector: host, name: clean}}
- {type: run.notify}
`,
fail: errors.New("judgement is down"), wantState: store.StateParked, wantDetail: "judgement is down",
},
{
name: "a retry from a step keeps the results of the steps before it",
doc: `steps:
- {id: first, type: connector.action, settings: {connector: host, name: clean}}
- {id: second, type: connector.action, settings: {connector: host, name: restart}}
- {type: run.notify, settings: {title: "{{ steps.first.summary }} then {{ steps.second.summary }}"}}
`,
levels: map[string]judgement.Level{"clean": judgement.ActQuietly, "restart": judgement.ActQuietly},
fail: errors.New("judgement is down"), failAction: "restart", retry: "second",
wantState: store.StateSucceeded, wantAssessed: 2, wantExecuted: 2, wantNotice: "done then done",
},
{
name: "a finish step ends the run with its status and detail",
doc: `steps:
- {type: workflow.finish, settings: {status: declined, detail: not worth fixing}}
- {type: run.notify, settings: {title: too late}}
`,
wantState: store.StateDeclined, wantDetail: "not worth fixing",
},
{
name: "a finish step whose condition does not hold lets the run go on",
doc: `inputs: {event: {type: object}}
steps:
- {type: workflow.finish, when: "inputs.event.fields.missing", settings: {status: parked, detail: stop}}
- {type: run.notify, settings: {title: carried on}}
`,
wantNotice: "carried on",
},
{
name: "a finish step can leave the run waiting for approval",
doc: `steps:
- {type: workflow.finish, settings: {status: waiting_approval, detail: needs a person}}
`,
wantState: store.StateWaitingApproval, wantDetail: "needs a person",
},
{
name: "a finish step may name the steps before it",
doc: `steps:
- {id: clean, type: connector.action, settings: {connector: host, name: clean}}
- {type: workflow.finish, settings: {detail: "cleaned: {{ steps.clean.summary }}"}}
`,
levels: map[string]judgement.Level{"clean": judgement.ActQuietly},
wantAssessed: 1, wantExecuted: 1, wantState: store.StateSucceeded, wantDetail: "cleaned: done",
},
{
name: "a run that reaches the end of its steps completes with the last summary",
doc: `steps:
- {id: clean, type: connector.action, settings: {connector: host, name: clean}}
`,
levels: map[string]judgement.Level{"clean": judgement.ActQuietly},
wantAssessed: 1, wantExecuted: 1, wantState: store.StateSucceeded, wantDetail: "done",
},
{
name: "a failing step that continues lets the run go on",
doc: `steps:
- {type: connector.action, on_failure: continue, settings: {connector: host, name: clean}}
- {type: run.notify, settings: {title: carried on}}
`,
fail: errors.New("judgement is down"), wantNotice: "carried on",
},
{
name: "an action held after the last approval step is put to people before the run ends",
doc: `steps:
- {type: connector.action, settings: {connector: host, name: clean}}
`,
levels: map[string]judgement.Level{"clean": judgement.ProposeAndWait},
wantAssessed: 2, wantAsked: 1, wantExecuted: 1,
},
{
name: "a held action is settled when the last approval step is skipped",
doc: `inputs: {skip: {type: bool}}
steps:
- {type: connector.action, settings: {connector: host, name: clean}}
- {type: approval.wait, when: "inputs.skip"}
`,
levels: map[string]judgement.Level{"clean": judgement.ProposeAndWait},
wantAssessed: 2, wantAsked: 1, wantExecuted: 1,
},
{
name: "an approval that fails and continues is settled before the run ends",
doc: `steps:
- {type: connector.action, settings: {connector: host, name: clean}}
- {type: approval.wait, on_failure: continue}
`,
levels: map[string]judgement.Level{"clean": judgement.ProposeAndWait},
failFirst: 1,
wantAssessed: 2, wantAsked: 2, wantExecuted: 1,
},
{
name: "a later approval step asks only about what was held since the last",
doc: `steps:
- {type: connector.action, settings: {connector: host, name: clean}}
- {type: approval.wait}
- {type: connector.action, settings: {connector: host, name: restart}}
- {type: approval.wait}
`,
levels: map[string]judgement.Level{"clean": judgement.ProposeAndWait, "restart": judgement.ProposeAndWait},
wantAssessed: 4, wantAsked: 2, wantExecuted: 2,
},
{
name: "a later step reads an earlier step's result",
doc: `steps:
- {id: clean, type: connector.action, settings: {connector: host, name: clean}}
- {type: run.notify, settings: {title: "cleaned: {{ steps.clean.summary }}"}}
`,
levels: map[string]judgement.Level{"clean": judgement.ActQuietly},
wantAssessed: 1, wantExecuted: 1, wantNotice: "cleaned: done",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := ParseDocument(tt.doc)
if err != nil {
t.Fatalf("ParseDocument: %v", err)
}
judge := &actionJudge{levels: tt.levels, fail: tt.fail, failAction: tt.failAction}
ask := &askCounter{failFirst: tt.failFirst}
told := &notices{}
wf := Deps{Settings: onePersonPath{}, Toolbox: anyBox{}, Judge: judge, Approver: ask, Notifier: told}.Compile(doc)
work := &signalAfter{arrives: 1}
run := store.Run{ID: uuid.New()}
rc := newContext(t.Context(), work, run, uuid.New(), nil)
if _, err = wf(rc, input); err != nil {
t.Fatalf("run error = %v", err)
}
if tt.retry != "" {
if rc.ending == nil || rc.ending.State != store.StateParked {
t.Fatalf("ending = %+v, want the run parked before its retry", rc.ending)
}
judge.fail = nil
kept := stepsBeforeRetry(t, work.steps, tt.retry)
rc = newContext(t.Context(), work, run, uuid.New(), kept)
work.steps = kept
if _, err = wf(rc, input); err != nil {
t.Fatalf("retry error = %v", err)
}
}
wantState := cmp.Or(tt.wantState, store.StateSucceeded)
if rc.ending == nil || rc.ending.State != wantState || !strings.Contains(rc.ending.Detail, tt.wantDetail) {
t.Fatalf("ending = %+v, want %s with detail containing %q", rc.ending, wantState, tt.wantDetail)
}
if len(judge.assessed) != tt.wantAssessed || ask.asked != tt.wantAsked || judge.executed != tt.wantExecuted {
t.Fatalf("assessed %d, asked %d, executed %d; want %d, %d, %d",
len(judge.assessed), ask.asked, judge.executed, tt.wantAssessed, tt.wantAsked, tt.wantExecuted)
}
if tt.wantInput != "" {
var got, want any
if err := json.Unmarshal(judge.assessed[0].Input, &got); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(tt.wantInput), &want); err != nil {
t.Fatal(err)
}
if a, b := mustJSON(t, got), mustJSON(t, want); a != b {
t.Fatalf("action input = %s, want %s", a, b)
}
}
if tt.wantNotice != "" && (len(told.sent) != 1 || told.sent[0].Title != tt.wantNotice) {
t.Fatalf("notices = %+v, want one titled %q", told.sent, tt.wantNotice)
}
})
}
}
// stepsBeforeRetry is what a retry from the named step keeps: the steps
// recorded before it began, and the markers written before it was entered. It
// mirrors store.Retry.
func stepsBeforeRetry(t *testing.T, steps []store.Step, from string) []store.Step {
t.Helper()
first, marker := -1, -1
for _, s := range steps {
if s.Name == enteredPrefix+from {
marker = s.Seq
if err := json.Unmarshal(s.Output, &first); err != nil {
t.Fatal(err)
}
}
}
if marker < 0 {
t.Fatalf("the run never reached step %q", from)
}
var kept []store.Step
for _, s := range steps {
if (s.Seq < first) || (s.Seq >= markerBase && s.Seq < marker) {
kept = append(kept, s)
}
}
return kept
}
func mustJSON(t *testing.T, v any) string {
t.Helper()
b, err := json.Marshal(v, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
return string(b)
}