Files
pipelines/internal/pipeline/human_test.go
T

255 lines
8.8 KiB
Go

package pipeline
import (
"context"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const humanSource = "# stage gate\n\n```output\nDecision evidence.\n```\n\n```prompt\nReview the screenshots and choose.\n```\n\n```edges\ndeploy -> worker\nrevise -> worker\nerror -> #failure\n```\n\n```configs\ntype=human\n```\n\n# stage worker\n\n```output\nWork result.\n```\n\n```prompt\nDo the work.\n```\n\n```edges\nsuccess -> #success\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=2\ntimeout=1h\n```\n\n"
func TestHumanStageAuthoringRoundTrip(t *testing.T) {
graph, err := Parse(humanSource)
if err != nil {
t.Fatal(err)
}
if len(graph.Stages) != 2 || graph.Stages[0].Type != "human" {
t.Fatalf("human type not parsed: %+v", graph.Stages)
}
gate := graph.Stages[0]
if strings.TrimSpace(gate.Prompt) == "" || gate.Edges["deploy"] != "worker" {
t.Fatalf("human stage fields lost: %+v", gate)
}
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "human.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "human", humanSource); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
definition, err := store.Get(ctx, items[0].ID, 1)
if err != nil {
t.Fatal(err)
}
if definition.Source != humanSource {
t.Fatal("immutable source was rewritten")
}
if definition.Graph.Stages[0].Type != "human" {
t.Fatalf("human type lost in stored graph: %+v", definition.Graph.Stages[0])
}
}
func humanRun(t *testing.T, ctx context.Context, store *Store) (int64, string) {
t.Helper()
if _, err := store.Submit(ctx, "humangate", humanSource); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
definition, err := store.Get(ctx, items[0].ID, 1)
if err != nil {
t.Fatal(err)
}
id, _, err := store.CreateRun(ctx, definition, "humangate-run", "workspace", false, nil, map[string]any{"prompt": "task"})
if err != nil {
t.Fatal(err)
}
if err := store.BeginStep(ctx, id, 1, "gate", map[string]any{"prompt": "task"}); err != nil {
t.Fatal(err)
}
evidence := protocol.NewWaitingEvidence("Review the screenshots and choose.", []string{"deploy", "revise"})
if err := store.MarkWaiting(ctx, id, 1, "gate", evidence); err != nil {
t.Fatal(err)
}
return id, "gate"
}
func TestMarkWaitingPersistsDurableWait(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "wait.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
id, _ := humanRun(t, ctx, store)
detail, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if detail.Status != protocol.RunStatusWaiting || detail.CurrentStage != "gate" || detail.FinishedAt != "" {
t.Fatalf("run did not report waiting: %+v", detail.RunSummary)
}
if len(detail.Steps) != 1 || detail.Steps[0].Status != protocol.StepStatusWaiting || detail.Steps[0].FinishedAt != "" {
t.Fatalf("step did not persist waiting: %+v", detail.Steps)
}
if prompt := protocol.WaitingPrompt(detail.Steps[0].Output); prompt == "" {
t.Fatalf("waiting evidence lost its prompt: %+v", detail.Steps[0].Output)
}
if options := protocol.WaitingOptions(detail.Steps[0].Output); len(options) != 2 {
t.Fatalf("waiting evidence lost its options: %+v", options)
}
// Recovery from persisted rows must leave a waiting run and step untouched.
if err := store.RecoverRuns(ctx); err != nil {
t.Fatal(err)
}
recovered, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if recovered.Status != protocol.RunStatusWaiting || recovered.Steps[0].Status != protocol.StepStatusWaiting {
t.Fatalf("RecoverRuns disturbed a waiting run: %+v", recovered.RunSummary)
}
}
func TestResolveDecisionValidUnknownRepeatAndNotWaiting(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "decide.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
id, _ := humanRun(t, ctx, store)
if _, _, err := store.ResolveDecision(ctx, id, "", "launch", ""); err == nil || !strings.Contains(err.Error(), "valid options") || !strings.Contains(err.Error(), "deploy") {
t.Fatalf("unknown option not refused with the valid list: %v", err)
}
step, applied, err := store.ResolveDecision(ctx, id, "", "deploy", "looks good, ship it")
if err != nil || !applied {
t.Fatalf("valid decision failed: %+v %v %v", step, applied, err)
}
if step.Outcome != "deploy" || step.Target != "worker" {
t.Fatalf("decision did not route along the edge: %+v", step)
}
detail, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if detail.Status != "running" || detail.Steps[0].Status != "completed" || detail.Steps[0].Outcome != "deploy" {
t.Fatalf("decision not persisted: %+v", detail)
}
if detail.Steps[0].Diagnostic != "" {
t.Fatalf("a deliberate decision must not be recorded as a stop: %+v", detail.Steps[0])
}
if detail.Steps[0].Output["option"] != "deploy" || detail.Steps[0].Output["feedback"] != "looks good, ship it" {
t.Fatalf("feedback not recorded in the output: %+v", detail.Steps[0])
}
verdict := detail.Steps[0].Verdict
if verdict == nil || verdict.Option != "deploy → worker" || verdict.Reason != "looks good, ship it" {
t.Fatalf("derived verdict missing the choice or feedback: %+v", verdict)
}
// A repeated decision for the already-decided visit must not double-apply.
repeat, applied, err := store.ResolveDecision(ctx, id, "", "deploy", "second try")
if err != nil || applied {
t.Fatalf("repeat decision double-applied: applied=%v err=%v", applied, err)
}
if repeat.Sequence != step.Sequence {
t.Fatalf("repeat decision pointed at another visit: %+v", repeat)
}
// A different option for the same decided visit is refused.
if _, _, err := store.ResolveDecision(ctx, id, "", "revise", ""); err == nil || !strings.Contains(err.Error(), "not waiting") {
t.Fatalf("deciding a decided visit with another option was accepted: %v", err)
}
// A run whose graph has no human stage cannot be decided.
if _, err := store.Submit(ctx, "plain", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
plainItems, _ := store.List(ctx)
var plain protocol.PipelineSummary
for _, p := range plainItems {
if p.Name == "plain" {
plain = p
}
}
plainDef, err := store.Get(ctx, plain.ID, 1)
if err != nil {
t.Fatal(err)
}
plainRun, _, err := store.CreateRun(ctx, plainDef, "plain-run", "workspace", false, nil, map[string]any{"prompt": "task"})
if err != nil {
t.Fatal(err)
}
if _, _, err := store.ResolveDecision(ctx, plainRun, "", "success", ""); err == nil {
t.Fatal("decision on a run with no human stage was accepted")
}
}
func TestCancelRunningRunRecordsIntentUntilExecutionReturns(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "cancel-running.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "humangate", humanSource); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
definition, err := store.Get(ctx, items[0].ID, 1)
if err != nil {
t.Fatal(err)
}
id, _, err := store.CreateRun(ctx, definition, "run", "workspace", false, nil, map[string]any{"prompt": "task"})
if err != nil {
t.Fatal(err)
}
if err := store.BeginStep(ctx, id, 1, "gate", map[string]any{"prompt": "task"}); err != nil {
t.Fatal(err)
}
if err := store.Cancel(ctx, id); err != nil {
t.Fatal(err)
}
detail, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if detail.Status != "running" || detail.Steps[0].Status != "running" || !detail.CancelRequested || detail.FinishedAt != "" {
t.Fatalf("stop request falsely settled execution: %+v %+v", detail.RunSummary, detail.Steps)
}
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "gate", Status: "completed", Outcome: "success"}); err != nil {
t.Fatal(err)
}
if err := store.FinishRun(ctx, id, "succeeded", "success", "", nil); err != nil {
t.Fatal(err)
}
detail, err = store.GetRun(ctx, id)
if err != nil || detail.Status != "canceled" || detail.Steps[0].Status != "canceled" {
t.Fatalf("late success beat cancellation: %+v %v", detail, err)
}
if err := store.Cancel(ctx, id); err == nil {
t.Fatal("canceling an already-canceled run succeeded")
}
}
func TestCancelWaitingRun(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "cancel.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
id, _ := humanRun(t, ctx, store)
if err := store.CancelWaiting(ctx, id); err != nil {
t.Fatal(err)
}
detail, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if detail.Status != "canceled" || detail.Steps[0].Status != "canceled" {
t.Fatalf("waiting run not canceled: %+v %+v", detail.RunSummary, detail.Steps)
}
if err := store.CancelWaiting(ctx, id); err == nil {
t.Fatal("canceling an already-canceled run succeeded")
}
}