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).
165 lines
5.2 KiB
Go
165 lines
5.2 KiB
Go
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)
|
||
}
|