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).
This commit is contained in:
2026-10-16 16:47:28 +01:00
parent 1b4674d22c
commit a6aff47117
32 changed files with 1782 additions and 87 deletions
+4
View File
@@ -13,6 +13,8 @@ func (m Model) activeFooter() string {
switch {
case m.editorChecking:
text = "Refreshing published state · draft retained"
case m.decisionOpen:
text = "↑/↓ choose · Tab feedback · Enter decide · Esc cancel"
case m.actionConfirm != nil || m.mutationCommand != nil:
text = "y confirm · n/Esc cancel · j/k read"
case m.draftDeletingPath != "":
@@ -59,6 +61,8 @@ func (m Model) activeFooter() string {
switch {
case m.editorChecking:
text = "Refreshing · draft retained"
case m.decisionOpen:
text = "Esc cancel · Enter decide · ↑↓ select"
case m.actionConfirm != nil || m.mutationCommand != nil:
text = "Esc cancel · y confirm · j/k read"
case m.draftDeletingPath != "":
+3
View File
@@ -104,6 +104,9 @@ func (m Model) homeLines() []string {
if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") {
runs = append(runs, "Working on "+r.CurrentStage)
}
if r.Status == protocol.RunStatusWaiting {
runs = append(runs, "Waiting on a person · stage "+r.CurrentStage)
}
runs = append(runs, "")
}
if len(m.runs) > limit {
+164
View File
@@ -0,0 +1,164 @@
package tui
import (
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
)
// lastWaitingStep returns the most recent step a run is waiting on (the last
// waiting step by sequence), or false when no step is waiting. The decision
// overlay and the run summary both read it, so the scan lives once.
func lastWaitingStep(r *protocol.RunDetail) (protocol.RunStep, bool) {
for i := len(r.Steps) - 1; i >= 0; i-- {
if r.Steps[i].Status == protocol.StepStatusWaiting {
return r.Steps[i], true
}
}
return protocol.RunStep{}, false
}
// waitingStage returns the stage the open run is waiting on plus its recorded
// prompt and selectable options. It prefers the self-contained evidence on the
// waiting step and falls back to the pinned graph's edges when a step recorded
// no options.
func (m Model) waitingStage() (string, []string, string) {
if m.runDetail == nil {
return "", nil, ""
}
if step, ok := lastWaitingStep(m.runDetail); ok {
return step.Stage, protocol.WaitingOptions(step.Output), protocol.WaitingPrompt(step.Output)
}
key := m.runDetail.CurrentStage
for _, declared := range m.runDetail.Graph.Stages {
if declared.Key == key {
return key, stage.HumanOptions(declared.Edges), declared.Prompt
}
}
return key, nil, ""
}
func (m Model) openDecision() (tea.Model, tea.Cmd) {
if m.runDetail == nil || m.runDetail.Status != protocol.RunStatusWaiting {
m.runError = "This run is not waiting for a decision."
return m, nil
}
key, options, _ := m.waitingStage()
if len(options) == 0 {
m.runError = "No selectable options recorded for the waiting stage."
return m, nil
}
m.decisionOpen = true
m.decisionRunID = m.runDetail.ID
m.decisionStage = key
m.decisionOptions = options
m.decisionIndex = 0
m.decisionFeedback = newInput()
m.decisionFeedback.Placeholder = "Optional feedback"
m.decisionFeedback.Width = max(1, min(60, m.width-12))
m.decisionFeedbackFocus = false
return m, nil
}
func (m *Model) closeDecision() {
m.decisionOpen = false
m.decisionRunID = 0
m.decisionStage = ""
m.decisionOptions = nil
m.decisionIndex = 0
m.decisionFeedback = newInput()
m.decisionFeedbackFocus = false
}
func (m Model) updateDecision(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc":
m.closeDecision()
return m, nil
case "tab":
m.decisionFeedbackFocus = !m.decisionFeedbackFocus
if m.decisionFeedbackFocus {
return m, m.decisionFeedback.Focus()
}
m.decisionFeedback.Blur()
return m, nil
case "f":
if !m.decisionFeedbackFocus {
m.decisionFeedbackFocus = true
return m, m.decisionFeedback.Focus()
}
case "up", "k":
if !m.decisionFeedbackFocus {
m.decisionIndex = max(0, m.decisionIndex-1)
return m, nil
}
case "down", "j":
if !m.decisionFeedbackFocus {
m.decisionIndex = min(m.decisionIndex+1, max(0, len(m.decisionOptions)-1))
return m, nil
}
case "enter":
if len(m.decisionOptions) == 0 {
return m, nil
}
option := m.decisionOptions[min(m.decisionIndex, len(m.decisionOptions)-1)]
feedback := strings.TrimSpace(m.decisionFeedback.Value())
request := protocol.Message{Type: protocol.DecideRun, RunID: m.decisionRunID, Stage: m.decisionStage, Option: option, Feedback: feedback}
m.closeDecision()
return m.runRequest(request)
}
if m.decisionFeedbackFocus {
var cmd tea.Cmd
m.decisionFeedback, cmd = m.decisionFeedback.Update(msg)
return m, cmd
}
return m, nil
}
func (m Model) decisionView(height int) string {
title := fmt.Sprintf("Decide %s · run #%d", m.decisionStage, m.decisionRunID)
intro := "Choose where the run goes next. The choice and feedback are recorded as the stage's evidence."
if m.width < 65 {
intro = "Choose where the run goes next."
}
// Size the option window to the panel body so every shown paragraph fits and
// the selected option is never clipped: each option costs a line plus a
// separator, and the feedback line always fits. The count of shown options
// goes in the controls, so a windowed list still says more exist.
dialogWidth := min(m.width, 76)
inner := max(1, dialogWidth-4)
introLines := len(strings.Split(ansi.Wrap(intro, inner, ""), "\n"))
capacity := max(1, (height-4-introLines)/2-1)
count := len(m.decisionOptions)
start, end := selectionWindow(count, m.decisionIndex, capacity)
paragraphs := []string{intro}
for i := start; i < end; i++ {
marker := " "
if i == m.decisionIndex {
marker = " › "
}
paragraphs = append(paragraphs, marker+m.decisionOptions[i])
}
paragraphs = append(paragraphs, "Feedback: "+m.decisionFeedback.View())
controls := "↑/↓ choose · Tab feedback · Enter decide · Esc cancel"
if end-start < count {
controls += fmt.Sprintf(" · %d/%d", end-start, count)
}
if m.width < 65 {
controls = "↑↓ select · Enter decide · Esc"
if end-start < count {
controls += fmt.Sprintf(" · %d/%d", end-start, count)
}
}
if m.decisionFeedbackFocus {
controls = "type feedback · Tab options · Enter decide · Esc cancel"
if m.width < 65 {
controls = "type feedback · Enter decide · Esc"
}
}
return wrappedDialog(m.width, height, 72, title, paragraphs, controls, 0)
}
+137
View File
@@ -0,0 +1,137 @@
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)
}
}
+13
View File
@@ -185,6 +185,13 @@ type Model struct {
runGraphFocus bool
runGraphTop int
runGraphPan int
decisionOpen bool
decisionRunID int64
decisionStage string
decisionOptions []string
decisionIndex int
decisionFeedback textinput.Model
decisionFeedbackFocus bool
editorOpen bool
editorInput textinput.Model
editorIndex int
@@ -582,6 +589,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.runForm != 0 {
return m.updateRunForm(msg)
}
if m.decisionOpen {
return m.updateDecision(msg)
}
if m.runOpen && (msg.String() != ":" || m.runSearchOpen) {
return m.updateRuns(msg)
}
@@ -784,6 +794,9 @@ func (m Model) View() string {
if m.actionConfirm != nil {
content = m.actionConfirmView(bodyHeight)
}
if m.decisionOpen {
content = m.decisionView(bodyHeight)
}
if m.mutationCommand != nil {
paragraphs := []string{"This opens a new session/editor and may modify a retained draft.", "Published definitions and previous run history are unchanged."}
if strings.Contains(m.mutationTitle, "debugging") {
+31
View File
@@ -3,6 +3,7 @@ package tui
import (
"fmt"
"maps"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/charmbracelet/lipgloss"
@@ -23,6 +24,12 @@ func resultLabel(r protocol.RunSummary) string {
if preview != "" {
return "Latest output: " + preview
}
if r.Status == protocol.RunStatusWaiting {
if r.CurrentStage != "" {
return "Waiting for a person · stage " + r.CurrentStage
}
return "Waiting for a person"
}
if r.Status == "running" || r.Status == "queued" {
return "In progress · awaiting stage output"
}
@@ -60,6 +67,7 @@ func runResultLines(r *protocol.RunDetail) []string {
if r.Diagnostic != "" {
lines = append(lines, "Stopped: "+ansi.Strip(r.Diagnostic))
}
lines = append(lines, waitingLines(r)...)
if r.Status == "succeeded" {
if preview := protocol.OutputPreview(r.Output); preview != "" {
lines = append(lines, "Result: "+ansi.Strip(preview))
@@ -96,6 +104,26 @@ func runResultLines(r *protocol.RunDetail) []string {
return lines
}
// waitingLines renders a run waiting on a human stage: the banner, the operator
// instructions recorded with the wait, and the selectable options, so the choice
// is visible without opening the stage view.
func waitingLines(r *protocol.RunDetail) []string {
if r.Status != protocol.RunStatusWaiting {
return nil
}
lines := []string{"⏸ Waiting for a person · stage " + r.CurrentStage}
if step, ok := lastWaitingStep(r); ok {
if prompt := strings.TrimSpace(protocol.WaitingPrompt(step.Output)); prompt != "" {
lines = append(lines, strings.Split(ansi.Strip(prompt), "\n")...)
}
if options := protocol.WaitingOptions(step.Output); len(options) > 0 {
lines = append(lines, "Options: "+strings.Join(options, ", "))
}
lines = append(lines, fmt.Sprintf("Decide: pipelines decide %d --option KEY [--feedback TEXT]", r.ID))
}
return lines
}
func completedWorkLines(r *protocol.RunDetail) []string {
var lines []string
for _, s := range r.Steps {
@@ -117,6 +145,9 @@ func completedWorkLines(r *protocol.RunDetail) []string {
}
func runOutputDocument(r *protocol.RunDetail, width int) []string {
if r.Status == protocol.RunStatusWaiting {
return waitingLines(r)
}
var notice []string
if r.Diagnostic != "" {
notice = append(notice, lipgloss.NewStyle().Foreground(themeError).Render("Stopped: "+ansi.Strip(r.Diagnostic)), "")
+11 -1
View File
@@ -101,6 +101,7 @@ func (m *Model) closeRuns() {
m.runGraphFocus = false
m.runGraphTop = 0
m.runGraphPan = 0
m.closeDecision()
}
func (m Model) visibleRuns() []protocol.RunSummary {
@@ -265,6 +266,8 @@ func runStatus(status string) string {
color = themeError
case "running", "queued":
color = themeAccent
case protocol.RunStatusWaiting:
color = themeAccent
}
return lipgloss.NewStyle().Foreground(color).Render(status)
}
@@ -640,6 +643,9 @@ func (m Model) runActions() string {
if m.runDetail != nil {
text += " · a evidence"
}
if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting {
text += " · c decide"
}
if m.selectedFailedRun() > 0 {
text += " · r retry failed"
}
@@ -718,7 +724,11 @@ func (m Model) runsDetailsView(height int) string {
lines := m.runSectionLines(m.runDetailWidth())
page := max(0, innerHeight-5)
top := max(0, min(m.runTop, max(0, len(lines)-page)))
footer := bar("o result · d diff · s progress · i input · t evidence · m details", fmt.Sprintf("%d–%d / %d", min(top+1, len(lines)), min(top+page, len(lines)), len(lines)), max(1, m.width-4), false)
footerHint := "o result · d diff · s progress · i input · t evidence · m details"
if r.Status == protocol.RunStatusWaiting {
footerHint += " · c decide"
}
footer := bar(footerHint, fmt.Sprintf("%d–%d / %d", min(top+1, len(lines)), min(top+page, len(lines)), len(lines)), max(1, m.width-4), false)
label := m.runSelectionLabel()
if m.runStepIndex > 0 && m.runStepIndex <= len(r.Steps) {
step := r.Steps[m.runStepIndex-1]
+3
View File
@@ -27,6 +27,9 @@ func (m Model) progress(status string) string {
color = themeError
case "canceled":
marker = "−"
case protocol.RunStatusWaiting:
marker = "⏸"
color = themeAccent
}
return lipgloss.NewStyle().Foreground(color).Render(marker + " " + status)
}
+7
View File
@@ -282,6 +282,9 @@ func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if m.runGraphFocus {
switch msg.String() {
case "c":
if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting {
return m.openDecision()
}
m.runGraphTop, m.runGraphPan = m.runGraphFollow()
return m, nil
case "j", "down":
@@ -418,6 +421,10 @@ func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
if m.runDetail != nil {
return m.runRequest(protocol.Message{Type: protocol.CancelRun, RunID: m.runDetail.ID})
}
case "c":
if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting {
return m.openDecision()
}
}
if m.runDetail != nil {
m.runTop = max(0, min(m.runTop, max(0, len(m.runSectionLines(m.runDetailWidth()))-m.runPageSize())))