Files
pipelines/internal/tui/decision_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

138 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
import (
"context"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
)
func decisionModel() Model {
m := New(context.Background(), "")
m.width = 100
m.height = 30
m.runOpen = true
m.keys.runs = true
m.requestedRunID = 8
m.runDetail = &protocol.RunDetail{
RunSummary: protocol.RunSummary{ID: 8, Name: "release", Version: 1, Status: protocol.RunStatusWaiting, CurrentStage: "gate"},
Steps: []protocol.RunStep{{
Sequence: 1, Stage: "gate", Status: protocol.StepStatusWaiting,
Output: protocol.NewWaitingEvidence("Approve the release?", []string{"go", "halt"}),
}},
}
return m
}
func TestWaitingRunRendersPromptAndOptions(t *testing.T) {
m := decisionModel()
text := ansi.Strip(strings.Join(m.runSectionLines(m.runDetailWidth()), "\n"))
for _, want := range []string{"Waiting for a person · stage gate", "Approve the release?", "Options: go, halt"} {
if !strings.Contains(text, want) {
t.Fatalf("waiting run view missing %q:\n%s", want, text)
}
}
detail := ansi.Strip(m.runsDetailsView(24))
if !strings.Contains(detail, "Waiting for a person") {
t.Fatalf("run detail omitted the waiting banner:\n%s", detail)
}
}
func TestDecisionOverlaySelectsAndEmits(t *testing.T) {
m := decisionModel()
updated, _ := m.openDecision()
m = updated.(Model)
if !m.decisionOpen || m.decisionStage != "gate" || strings.Join(m.decisionOptions, ",") != "go,halt" {
t.Fatalf("overlay did not open with the recorded options: %+v", m.decisionOptions)
}
if view := ansi.Strip(m.decisionView(24)); !strings.Contains(view, "Decide gate") || !strings.Contains(view, "halt") {
t.Fatalf("overlay view missing the choice:\n%s", view)
}
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown})
m = updated.(Model)
if m.decisionIndex != 1 {
t.Fatalf("down did not move the selection: %d", m.decisionIndex)
}
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyTab})
m = updated.(Model)
if !m.decisionFeedbackFocus {
t.Fatal("Tab did not focus the feedback field")
}
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyEsc})
m = updated.(Model)
if m.decisionOpen {
t.Fatal("Esc did not close the overlay")
}
// Enter emits a DecideRun request; with no connection the send path records
// the attempt and the overlay closes.
updated, _ = m.openDecision()
m = updated.(Model)
m.decisionFeedback.SetValue("looks good")
before := len(m.lines)
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyEnter})
m = updated.(Model)
if m.decisionOpen {
t.Fatal("Enter did not close the overlay")
}
if len(m.lines) == before {
t.Fatal("Enter did not attempt to send the decision")
}
if !strings.Contains(strings.Join(m.lines, "\n"), "not connected") {
t.Fatalf("decision path did not run: %v", m.lines)
}
}
// TestDecisionOverlayKeepsSelectionVisibleAtNarrowViewport proves the overlay
// windows its option list like the palette: at the smallest reviewed viewport
// the selected option stays visible with its marker and a count says more exist.
func TestDecisionOverlayKeepsSelectionVisibleAtNarrowViewport(t *testing.T) {
m := decisionModel()
m.width = 40
m.height = 16
m.runDetail.Steps[0].Output = protocol.NewWaitingEvidence("Approve the release?", []string{"abort", "deploy", "revise"})
updated, _ := m.openDecision()
m = updated.(Model)
// Move onto the option a fixed, unwindowed list would clip at this viewport.
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown})
m = updated.(Model)
updated, _ = m.updateDecision(tea.KeyMsg{Type: tea.KeyDown})
m = updated.(Model)
view := ansi.Strip(m.View())
if !strings.Contains(view, "› revise") {
t.Fatalf("selected option is not visible with a marker at 40x16:\n%s", view)
}
if !strings.Contains(view, "2/3") {
t.Fatalf("option count affordance missing at 40x16:\n%s", view)
}
if strings.Contains(view, "Enter d…") {
t.Fatalf("controls are still truncated at 40x16:\n%s", view)
}
}
// TestDecidedHumanStepRendersNeutralFeedback locks the fix for the review
// finding: a completed decision shows its feedback as a normal output field,
// never as the error-style "Stopped:" line reserved for real stops.
func TestDecidedHumanStepRendersNeutralFeedback(t *testing.T) {
m := decisionModel()
m.runDetail.Status = "succeeded"
output := map[string]any{"option": "go", "feedback": "looks good"}
m.runDetail.Steps[0] = protocol.RunStep{
Sequence: 1, Stage: "gate", Status: "completed", Outcome: "go", Target: "worker",
Output: output,
Verdict: protocol.NewStageVerdict("go", "worker", output, nil, "", ""),
}
m.runStepIndex = 1
view := ansi.Strip(strings.Join(m.runSectionLines(m.runDetailWidth()), "\n"))
if strings.Contains(view, "Stopped:") {
t.Fatalf("a deliberate decision must not render as a stop:\n%s", view)
}
if !strings.Contains(view, "looks good") {
t.Fatalf("decision feedback is not visible:\n%s", view)
}
}