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).
178 lines
5.5 KiB
Go
178 lines
5.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.NotificationPolicy, step protocol.RunStep) {
|
|
if policy == nil {
|
|
return
|
|
}
|
|
event := "success"
|
|
if step.Status == protocol.StepStatusWaiting || step.Outcome == protocol.StepStatusWaiting {
|
|
event = protocol.NotifyWaiting
|
|
} else if step.Status != "completed" || step.Outcome == "error" || step.Target == protocol.FailureEnd {
|
|
event = "failure"
|
|
}
|
|
enabled := false
|
|
for _, on := range policy.On {
|
|
if on == event {
|
|
enabled = true
|
|
}
|
|
}
|
|
if !enabled {
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.ctx, 30*time.Second)
|
|
defer cancel()
|
|
summary := fmt.Sprintf("%s · run #%d · stage %s · %s (%s)", name, id, step.Stage, event, step.Outcome)
|
|
if len(summary) > 1800 {
|
|
summary = summary[:1800]
|
|
for !utf8.ValidString(summary) {
|
|
summary = summary[:len(summary)-1]
|
|
}
|
|
}
|
|
title := fmt.Sprintf("Pipeline stage %s", event)
|
|
receipts := []protocol.NotificationReceipt{}
|
|
persist := func() bool {
|
|
save, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := r.store.SaveNotifications(save, id, step.Sequence, receipts); err != nil {
|
|
log.Printf("notification audit run=%d step=%d: %v", id, step.Sequence, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
send := func(filename string, data []byte) {
|
|
receipts = append(receipts, protocol.NotificationReceipt{Destination: "system", Status: "sending", Filename: filename})
|
|
if !persist() {
|
|
return
|
|
}
|
|
receipt := &receipts[len(receipts)-1]
|
|
result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data)
|
|
if err != nil {
|
|
receipt.Status = "failed"
|
|
receipt.Diagnostic = err.Error()
|
|
} else {
|
|
receipt.Status = "sent"
|
|
receipt.MessageID, _ = result["message_id"].(string)
|
|
receipt.AttachmentURL, _ = result["attachment_url"].(string)
|
|
}
|
|
persist()
|
|
}
|
|
reject := func(filename, diagnostic string) {
|
|
receipts = append(receipts, protocol.NotificationReceipt{Destination: "system", Status: "failed", Filename: filename, Diagnostic: diagnostic})
|
|
persist()
|
|
}
|
|
sentArtifact := false
|
|
for _, kind := range policy.Artifacts {
|
|
switch kind {
|
|
case "markdown":
|
|
if markdown, ok := step.Output["markdown"].(string); ok && strings.TrimSpace(markdown) != "" {
|
|
sentArtifact = true
|
|
send("digest.md", []byte(markdown))
|
|
}
|
|
case "files":
|
|
raw, exists := step.Output["artifacts"]
|
|
if !exists {
|
|
continue
|
|
}
|
|
var names []string
|
|
switch files := raw.(type) {
|
|
case []string:
|
|
names = files
|
|
case []any:
|
|
for _, value := range files {
|
|
name, ok := value.(string)
|
|
if !ok {
|
|
reject("", "output.artifacts must be an array of relative file paths")
|
|
names = nil
|
|
break
|
|
}
|
|
names = append(names, name)
|
|
}
|
|
default:
|
|
reject("", "output.artifacts must be an array of relative file paths")
|
|
}
|
|
if len(names) > 10 {
|
|
reject("", "at most 10 file artifacts can be notified")
|
|
continue
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, name := range names {
|
|
if seen[name] {
|
|
continue
|
|
}
|
|
seen[name] = true
|
|
data, err := notificationArtifact(workspace, name)
|
|
if err != nil {
|
|
reject(name, err.Error())
|
|
continue
|
|
}
|
|
sentArtifact = true
|
|
send(filepath.Base(name), data)
|
|
}
|
|
}
|
|
}
|
|
// No produced artifacts still merits an event, especially on failures.
|
|
if !sentArtifact {
|
|
send("", nil)
|
|
}
|
|
}
|
|
|
|
func notificationArtifact(workspace, name string) ([]byte, error) {
|
|
if name == "" || filepath.IsAbs(name) || filepath.Clean(name) != name || strings.ContainsAny(name, "\\\x00\r\n") {
|
|
return nil, fmt.Errorf("artifact must be a clean workspace-relative file path")
|
|
}
|
|
for _, part := range strings.Split(filepath.ToSlash(name), "/") {
|
|
lower := strings.ToLower(part)
|
|
if strings.HasPrefix(part, ".") || lower == "secrets" || lower == "node_modules" || strings.HasSuffix(lower, ".pem") || strings.HasSuffix(lower, ".key") || strings.HasSuffix(lower, ".db") || strings.HasSuffix(lower, ".db-wal") || strings.HasSuffix(lower, ".db-shm") || strings.HasSuffix(lower, ".sqlite") || strings.HasSuffix(lower, ".sqlite3") {
|
|
return nil, fmt.Errorf("artifact path is protected")
|
|
}
|
|
}
|
|
root, err := filepath.EvalSymlinks(workspace)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("artifact workspace unavailable")
|
|
}
|
|
// os.Root prevents symlink traversal outside the workspace; disallow symlinks
|
|
// altogether so aliases cannot bypass protected path names within it either.
|
|
current := root
|
|
for _, part := range strings.Split(name, string(os.PathSeparator)) {
|
|
current = filepath.Join(current, part)
|
|
info, err := os.Lstat(current)
|
|
if err != nil || info.Mode()&os.ModeSymlink != 0 {
|
|
return nil, fmt.Errorf("artifact is missing or uses a symlink")
|
|
}
|
|
}
|
|
scoped, err := os.OpenRoot(root)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("artifact workspace unavailable")
|
|
}
|
|
defer scoped.Close()
|
|
file, err := scoped.OpenFile(name, os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot read artifact")
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
|
return nil, fmt.Errorf("artifact must be a regular file at most 1 MiB")
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(file, (1<<20)+1))
|
|
if err != nil || len(data) > 1<<20 {
|
|
return nil, fmt.Errorf("cannot read artifact within size limit")
|
|
}
|
|
return data, nil
|
|
}
|