a6aff47117
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).
174 lines
5.4 KiB
Go
174 lines
5.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"maps"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/lipgloss"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func resultLabel(r protocol.RunSummary) string {
|
|
preview := ansi.Strip(r.ResultPreview)
|
|
if r.Status == "succeeded" {
|
|
if preview != "" {
|
|
return "Result: " + preview
|
|
}
|
|
return "Finished · no result recorded"
|
|
}
|
|
if r.Diagnostic != "" {
|
|
return "Stopped: " + ansi.Strip(r.Diagnostic)
|
|
}
|
|
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"
|
|
}
|
|
return "No result recorded"
|
|
}
|
|
|
|
func (m Model) latestPipelineRun(id int64, version int) *protocol.RunSummary {
|
|
var latest *protocol.RunSummary
|
|
for i := range m.runs {
|
|
r := &m.runs[i]
|
|
if r.PipelineID == id && (version == 0 || r.Version == version) && (latest == nil || r.ID > latest.ID) {
|
|
latest = r
|
|
}
|
|
}
|
|
return latest
|
|
}
|
|
|
|
func (m Model) pipelineResultLines() []string {
|
|
if m.detail == nil {
|
|
return nil
|
|
}
|
|
r := m.latestPipelineRun(m.detail.ID, m.detail.Version)
|
|
if r == nil {
|
|
return []string{"Results · This version has not run yet. No accomplishments recorded."}
|
|
}
|
|
lines := []string{fmt.Sprintf("Results · run #%d · %s · %s · %d completed visits · %s", r.ID, r.Label(), ansi.Strip(m.progress(r.Status)), r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt)), resultLabel(*r)}
|
|
if r.Status != "succeeded" && r.ResultPreview != "" && r.Diagnostic != "" {
|
|
lines = append(lines, "Partial output: "+ansi.Strip(r.ResultPreview))
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func runResultLines(r *protocol.RunDetail) []string {
|
|
lines := []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))
|
|
} else {
|
|
lines = append(lines, "Finished · no result recorded")
|
|
}
|
|
} else {
|
|
if r.Status == "running" || r.Status == "queued" {
|
|
lines = append(lines, "In progress · final result pending")
|
|
}
|
|
for i := len(r.Steps) - 1; i >= 0; i-- {
|
|
if r.Steps[i].Status == "completed" {
|
|
if preview := protocol.OutputPreview(r.Steps[i].Output); preview != "" {
|
|
lines = append(lines, "Latest completed output ("+r.Steps[i].Stage+"): "+ansi.Strip(preview))
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
completed := 0
|
|
failed := 0
|
|
for _, s := range r.Steps {
|
|
if s.Status == "completed" {
|
|
completed++
|
|
}
|
|
if s.Status == "failed" {
|
|
failed++
|
|
}
|
|
}
|
|
lines = append(lines, fmt.Sprintf("Progress: %d completed visits · %d failed · %d recorded", completed, failed, len(r.Steps)))
|
|
if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") {
|
|
lines = append(lines, "Working on: "+r.CurrentStage)
|
|
}
|
|
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 {
|
|
if s.Status != "completed" {
|
|
continue
|
|
}
|
|
line := fmt.Sprintf("%d. %s · %s", s.Sequence, s.Stage, s.Outcome)
|
|
if preview := protocol.OutputPreview(s.Output); preview != "" {
|
|
line += " — " + ansi.Strip(preview)
|
|
} else {
|
|
line += " — no output recorded"
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
if len(lines) == 0 {
|
|
lines = append(lines, "No completed stage output yet.")
|
|
}
|
|
return append([]string{"Completed stage outputs (reported, not independently verified):"}, lines...)
|
|
}
|
|
|
|
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)), "")
|
|
}
|
|
if r.Status == "succeeded" {
|
|
if len(r.Output) == 0 {
|
|
return []string{"Finished · no result recorded"}
|
|
}
|
|
return resultDocument(r.Output, width)
|
|
}
|
|
for i := len(r.Steps) - 1; i >= 0; i-- {
|
|
if r.Steps[i].Status == "completed" && len(r.Steps[i].Output) > 0 {
|
|
notice = append(notice, lipgloss.NewStyle().Foreground(themeMuted).Render("Partial result · "+r.Steps[i].Stage+" · final result not available"), "")
|
|
return append(notice, resultDocument(r.Steps[i].Output, width)...)
|
|
}
|
|
}
|
|
if len(r.Output) > 0 {
|
|
values := maps.Clone(r.Output)
|
|
delete(values, "input")
|
|
return append(notice, resultDocument(values, width)...)
|
|
}
|
|
return append(notice, "No output recorded yet.")
|
|
}
|