Files
pipelines/internal/pipeline/notify.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

64 lines
1.9 KiB
Go

package pipeline
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"regexp"
"strings"
)
func parseNotify(text string) (*protocol.NotificationPolicy, error) {
values := map[string]string{}
for _, line := range strings.Split(text, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
key, value, ok := strings.Cut(line, "=")
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
if !ok || value == "" {
return nil, fmt.Errorf("invalid notify setting %q", line)
}
if _, exists := values[key]; exists {
return nil, fmt.Errorf("duplicate notify setting %s", key)
}
switch key {
case "on", "destination", "artifacts":
default:
return nil, fmt.Errorf("unknown notify setting %s", key)
}
values[key] = value
}
// Accept legacy destination syntax without rewriting immutable sources.
// All stage notification hooks now use the server's global connection.
if destination := values["destination"]; destination != "" && !regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`).MatchString(destination) {
return nil, fmt.Errorf("invalid legacy notify destination")
}
p := &protocol.NotificationPolicy{Destination: "system"}
parse := func(text string, allowed map[string]bool) ([]string, error) {
var result []string
seen := map[string]bool{}
for _, item := range strings.Split(text, ",") {
item = strings.TrimSpace(item)
if !allowed[item] || seen[item] {
return nil, fmt.Errorf("invalid or duplicate notify value %q", item)
}
seen[item] = true
result = append(result, item)
}
return result, nil
}
var err error
p.On, err = parse(values["on"], map[string]bool{"success": true, "failure": true, protocol.NotifyWaiting: true})
if err != nil {
return nil, err
}
if values["artifacts"] != "" {
p.Artifacts, err = parse(values["artifacts"], map[string]bool{"markdown": true, "files": true})
if err != nil {
return nil, err
}
}
return p, nil
}