* 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
247 lines
7.9 KiB
Go
247 lines
7.9 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"maps"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/usage"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
// fakeStepStore records the step writes a branch makes.
|
|
type fakeStepStore struct {
|
|
started []task.StepStart
|
|
finished []task.StepFinish
|
|
events []events.Event
|
|
nextID int64
|
|
}
|
|
|
|
func (f *fakeStepStore) Update(context.Context, *task.Task) error { return nil }
|
|
|
|
func (f *fakeStepStore) Transition(context.Context, int64, string, string, string) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStepStore) InsertEvent(_ context.Context, e events.Event) (int64, error) {
|
|
f.events = append(f.events, e)
|
|
return 0, nil
|
|
}
|
|
|
|
func (f *fakeStepStore) StartStep(_ context.Context, s task.StepStart) (int64, events.Event, error) {
|
|
f.nextID++
|
|
f.started = append(f.started, s)
|
|
return f.nextID, events.Event{ID: f.nextID}, nil
|
|
}
|
|
|
|
func (f *fakeStepStore) FinishStep(_ context.Context, s task.StepFinish) (events.Event, error) {
|
|
f.finished = append(f.finished, s)
|
|
return events.Event{ID: s.StepID}, nil
|
|
}
|
|
|
|
func (f *fakeStepStore) RecordUsage(context.Context, usage.Record) error { return nil }
|
|
|
|
// TestRunBranchRecordsChildSteps pins the step contract a branch owes the
|
|
// canvas: every branch step is a stage row parented to the parallel step and
|
|
// named for its branch, and its outcome is recorded.
|
|
func TestRunBranchRecordsChildSteps(t *testing.T) {
|
|
boom := errors.New("boom")
|
|
tests := []struct {
|
|
name string
|
|
stage Stage
|
|
wantErr error
|
|
wantStatus taskstate.StepStatus
|
|
wantDetail string
|
|
}{
|
|
{
|
|
name: "a passing branch step",
|
|
stage: Stage{Name: "agent.run", Run: func(context.Context, *TaskContext) error { return nil }},
|
|
wantStatus: taskstate.StepSucceeded,
|
|
},
|
|
{
|
|
name: "a failing branch step",
|
|
stage: Stage{Name: "command.run", Run: func(context.Context, *TaskContext) error { return boom }},
|
|
wantErr: boom,
|
|
wantStatus: taskstate.StepFailed,
|
|
wantDetail: "boom",
|
|
},
|
|
{
|
|
name: "a branch step whose when was false",
|
|
stage: wrapStep(StepRecord{ID: "agent.run", When: "task.title"}, func(context.Context, *TaskContext) error { return nil }),
|
|
wantStatus: taskstate.StepSkipped,
|
|
wantDetail: "task.title",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
store := &fakeStepStore{}
|
|
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1}, Store: store, StepID: 7, workflowBranch: "docs", Log: slog.Default()}
|
|
err := runBranch(context.Background(), tc, []Stage{tt.stage})
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
|
}
|
|
if len(store.started) != 1 {
|
|
t.Fatalf("started %d steps, want 1", len(store.started))
|
|
}
|
|
start := store.started[0]
|
|
if start.Kind != task.StepKindStage || start.ParentID != 7 || start.Name != "docs/"+tt.stage.Name {
|
|
t.Fatalf("start = %#v, want a stage named %q under step 7", start, "docs/"+tt.stage.Name)
|
|
}
|
|
if len(store.finished) != 1 {
|
|
t.Fatalf("finished %d steps, want 1", len(store.finished))
|
|
}
|
|
finish := store.finished[0]
|
|
if finish.To != tt.wantStatus || finish.Detail != tt.wantDetail {
|
|
t.Fatalf("finish = %#v, want %s %q", finish, tt.wantStatus, tt.wantDetail)
|
|
}
|
|
if tc.StepID != 7 {
|
|
t.Fatalf("branch step leaked its step id: %d", tc.StepID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestSwitchRunsTheMatchingCase pins the switch contract: the case keyed by
|
|
// the referenced value runs, else the default case, else nothing, and its steps
|
|
// record under the case name.
|
|
func TestSwitchRunsTheMatchingCase(t *testing.T) {
|
|
src := `id: route
|
|
steps:
|
|
- switch:
|
|
on: task.title
|
|
cases:
|
|
bug: [{id: fix, type: mark}]
|
|
"true": [{id: yes, type: mark}]
|
|
%s`
|
|
tests := []struct {
|
|
name, title, fallback, want string
|
|
}{
|
|
{name: "a matching case", title: "bug", want: "bug/fix"},
|
|
{name: "a boolean-looking key", title: "true", want: "true/yes"},
|
|
{name: "no match falls to the default", title: "feature", fallback: " default: [{id: other, type: mark}]", want: "default/other"},
|
|
{name: "no match and no default runs nothing", title: "feature"},
|
|
}
|
|
registry := StepRegistry{"mark": func(yaml.Node) (Stage, error) {
|
|
return Stage{Run: func(context.Context, *TaskContext) error { return nil }}, nil
|
|
}}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
wf, err := ParseAndCompile(fmt.Sprintf(src, tt.fallback), registry)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store := &fakeStepStore{}
|
|
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Title: tt.title}, Store: store, StepID: 7, Log: slog.Default()}
|
|
if err := wf.Stages[0].Run(context.Background(), tc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got string
|
|
if len(store.started) > 0 {
|
|
got = store.started[0].Name
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("ran %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// lockedStepStore serialises the step writes parallel branches make.
|
|
type lockedStepStore struct {
|
|
mu sync.Mutex
|
|
fakeStepStore
|
|
}
|
|
|
|
func (f *lockedStepStore) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.fakeStepStore.StartStep(ctx, s)
|
|
}
|
|
|
|
func (f *lockedStepStore) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.fakeStepStore.FinishStep(ctx, s)
|
|
}
|
|
|
|
// TestParallelBranchesWriteOutputs pins how parallel branches write declared
|
|
// outputs: each branch writes its own, and two branches writing one output is
|
|
// refused as a second write would be, whatever order the branches finish in.
|
|
func TestParallelBranchesWriteOutputs(t *testing.T) {
|
|
src := `id: fan
|
|
repository: none
|
|
outputs:
|
|
a: {type: string}
|
|
b: {type: string}
|
|
c: {type: string}
|
|
z: {type: string}
|
|
steps:
|
|
- parallel:
|
|
one: [{type: agent.run, settings: {read_only: true, names: [a, c]}}]
|
|
two: [{type: agent.run, settings: {read_only: true, names: [%s]}}]
|
|
`
|
|
tests := []struct {
|
|
name, two, wantErr string
|
|
before, want map[string]any
|
|
}{
|
|
{name: "distinct outputs", two: "b", want: map[string]any{"a": "one", "b": "two", "c": "one"}},
|
|
{
|
|
name: "distinct outputs beside an earlier one", two: "b",
|
|
before: map[string]any{"z": "before"},
|
|
want: map[string]any{"a": "one", "b": "two", "c": "one", "z": "before"},
|
|
},
|
|
{name: "one output from two branches", two: "b, c", wantErr: `output "c" has already been written`},
|
|
{name: "a branch rewriting an earlier output", two: "z", before: map[string]any{"z": "before"}, wantErr: `output "z" has already been written`},
|
|
}
|
|
// A fake read-only agent.run, the step type a branch may run.
|
|
registry := StepRegistry{AgentRunStepName: func(node yaml.Node) (Stage, error) {
|
|
var s struct{ Names []string }
|
|
if err := node.Decode(&s); err != nil {
|
|
return Stage{}, err
|
|
}
|
|
return Stage{Run: func(_ context.Context, tc *TaskContext) error {
|
|
for _, name := range s.Names {
|
|
for range 100 {
|
|
tc.workflowInterface()
|
|
}
|
|
if err := tc.WriteOutput(name, tc.workflowBranch); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}}, nil
|
|
}}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
wf, err := ParseAndCompile(fmt.Sprintf(src, tt.two), registry)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Outputs: maps.Clone(tt.before)}, Store: &lockedStepStore{}, Log: slog.Default()}
|
|
tc.runInterface, tc.runInterfaceSet = wf.Interface, true
|
|
err = wf.Stages[0].Run(context.Background(), tc)
|
|
if tt.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
|
t.Fatalf("err = %v, want %q", err, tt.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !maps.Equal(tc.Task.Outputs, tt.want) {
|
|
t.Fatalf("outputs = %v, want %v", tc.Task.Outputs, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|