Files
pipelines/internal/cli/decide_test.go
T
alan a6aff47117 Add a human decision stage that pauses a run for an operator's choice
A new registered stage type `human` (internal/stage/human.go) has no model,
agent, tools or configs: its required `prompt` block carries the operator
instructions and its non-error edges are the selectable options. It parses,
validates, renders, exports and imports like every other type (no parser
special-case, no config keys).

At runtime the runner parks the run instead of executing: the step and the run
persist `waiting` with self-contained {prompt, options} evidence, and the run
returns without starting later stages, capturing file diffs, scanning artifacts,
holding a goroutine/timer or applying a stage timeout. The wait is durable: a
restart leaves the waiting rows untouched (RecoverRuns excludes them) and a
rebuilt runtime over the same database still accepts the decision.

`pipelines decide <run-id> [stage] --option KEY [--feedback TEXT] [--json]` and
the TUI `c` picker resolve the decision from the pinned graph in one store
transaction, record the choice and feedback as the step's evidence and resume
along edges[KEY]. An unknown option is refused with the valid list, a stage that
is not waiting is refused, and a repeated decision for an already-decided visit
does not double-apply. Cancel and retry keep working while waiting and after a
decision; a human stage is never killed by a stage timeout or max_visits, and a
`notify` policy can fire an `on=waiting` event.

Docs: the authoring skill references (format.md, operations.md), docs/cli.md and
README describe the stage, the command and the TUI picker. Tests cover stage
validation, the parsing round-trip, the store transitions, a websocket wait/decide
integration, cancel/retry, waiting notifications and a persistence-layer rebuild
(second Open + newRunner over one database, no process restart).
2026-10-16 16:47:28 +01:00

86 lines
2.8 KiB
Go

package cli_test
import (
"context"
"encoding/json"
"strconv"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const cliHumanSource = "# stage gate\n\n```output\nDecision evidence.\n```\n\n```prompt\nApprove the release.\n```\n\n```edges\ngo -> #success\nhalt -> #failure\nerror -> #failure\n```\n\n```configs\ntype=human\n```\n\n"
func TestCLIDecideWaitingHumanRun(t *testing.T) {
run, store, _ := setup(t)
ctx := context.Background()
if _, err := store.Submit(ctx, "human-ws", cliHumanSource); err != nil {
t.Fatal(err)
}
output, err := run("run", "human-ws", "ship it", "--json", "--yes")
if err != nil {
t.Fatal(err)
}
var started protocol.Message
if err := json.Unmarshal([]byte(output), &started); err != nil || started.RunDetail == nil {
t.Fatalf("run did not return detail: %v %s", err, output)
}
runID := started.RunDetail.ID
waitFor := func(status string) protocol.RunDetail {
t.Helper()
for i := 0; i < 100; i++ {
text, err := run("inspect", strconv.FormatInt(runID, 10), "--json")
if err != nil {
t.Fatal(err)
}
var detail protocol.RunDetail
if err := json.Unmarshal([]byte(text), &detail); err != nil {
t.Fatal(err)
}
if detail.Status == status {
return detail
}
}
t.Fatalf("run never reached %s", status)
return protocol.RunDetail{}
}
waiting := waitFor(protocol.RunStatusWaiting)
if len(waiting.Steps) != 1 || waiting.Steps[0].Status != protocol.StepStatusWaiting {
t.Fatalf("waiting step not visible: %+v", waiting.Steps)
}
if prompt := protocol.WaitingPrompt(waiting.Steps[0].Output); !strings.Contains(prompt, "Approve the release") {
t.Fatalf("waiting prompt not visible: %+v", waiting.Steps[0].Output)
}
if _, err := run("decide", strconv.FormatInt(runID, 10)); err == nil || !strings.Contains(err.Error(), "--option") {
t.Fatalf("missing --option was accepted: %v", err)
}
if _, err := run("decide", strconv.FormatInt(runID, 10), "--option", "launch"); err == nil || !strings.Contains(err.Error(), "valid options") {
t.Fatalf("unknown option was accepted: %v", err)
}
output, err = run("decide", strconv.FormatInt(runID, 10), "gate", "--option", "go", "--feedback", "approved", "--json")
if err != nil {
t.Fatal(err)
}
var decided protocol.RunDetail
if err := json.Unmarshal([]byte(output), &decided); err != nil {
t.Fatal(err)
}
final := waitFor("succeeded")
gate := final.Steps[0]
if gate.Outcome != "go" || gate.Target != protocol.SuccessEnd || gate.Output["feedback"] != "approved" || gate.Diagnostic != "" {
t.Fatalf("decision evidence not recorded: %+v", gate)
}
if gate.Verdict == nil || gate.Verdict.Option != "go → success" || gate.Verdict.Reason != "approved" {
t.Fatalf("derived verdict missing the decision: %+v", gate.Verdict)
}
if _, err := run("help", "decide"); err != nil {
t.Fatal(err)
}
}