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:
@@ -0,0 +1,438 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
)
|
||||
|
||||
const humanPipelineSource = "# stage gate\n\n```output\nDecision evidence.\n```\n\n```prompt\nReview the screenshots and choose where the run goes.\n```\n\n```edges\ngo -> worker\nhalt -> #success\nerror -> #failure\n```\n\n```configs\ntype=human\n```\n\n# stage worker\n\n```output\nWork result.\n```\n\n```prompt\nDo the work.\n```\n\n```edges\nsuccess -> #success\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=2\ntimeout=1h\n```\n\n"
|
||||
|
||||
func successExecute(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
|
||||
}
|
||||
|
||||
// startHumanRun submits the human pipeline (once) and starts one run, returning
|
||||
// once the run goroutine has parked the run in the waiting state.
|
||||
func startHumanRun(t *testing.T, ctx context.Context, r *runner, store *pipeline.Store, key string) *protocol.RunDetail {
|
||||
t.Helper()
|
||||
items, _ := store.List(ctx)
|
||||
var pipelineID int64
|
||||
for _, p := range items {
|
||||
if p.Name == "human" {
|
||||
pipelineID = p.ID
|
||||
}
|
||||
}
|
||||
if pipelineID == 0 {
|
||||
if _, err := store.Submit(ctx, "human", humanPipelineSource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ = store.List(ctx)
|
||||
for _, p := range items {
|
||||
if p.Name == "human" {
|
||||
pipelineID = p.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
run, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: key, Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
return run
|
||||
}
|
||||
|
||||
func TestHumanStageWaitsAndDecisionResumes(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "human.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.wg.Wait()
|
||||
r.execute = successExecute
|
||||
run := startHumanRun(t, ctx, r, store, "human-go")
|
||||
|
||||
detail, err := r.getRun(run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != protocol.RunStatusWaiting || detail.CurrentStage != "gate" {
|
||||
t.Fatalf("run did not wait: %+v", detail.RunSummary)
|
||||
}
|
||||
if len(detail.Steps) != 1 || detail.Steps[0].Status != protocol.StepStatusWaiting {
|
||||
t.Fatalf("waiting step not persisted: %+v", detail.Steps)
|
||||
}
|
||||
if prompt := protocol.WaitingPrompt(detail.Steps[0].Output); !strings.Contains(prompt, "Review the screenshots") {
|
||||
t.Fatalf("waiting step omitted its prompt: %+v", detail.Steps[0].Output)
|
||||
}
|
||||
if options := protocol.WaitingOptions(detail.Steps[0].Output); strings.Join(options, ",") != "go,halt" {
|
||||
t.Fatalf("waiting step omitted its options: %+v", options)
|
||||
}
|
||||
if detail.Steps[0].FinishedAt != "" {
|
||||
t.Fatalf("waiting step must not be finished: %+v", detail.Steps[0])
|
||||
}
|
||||
|
||||
// No later step is created while waiting.
|
||||
if len(detail.Steps) != 1 {
|
||||
t.Fatalf("a later step ran while waiting: %+v", detail.Steps)
|
||||
}
|
||||
|
||||
// Unknown option is refused with the valid list.
|
||||
if _, err := r.decide(protocol.Message{RunID: run.ID, Option: "launch"}); err == nil || !strings.Contains(err.Error(), "valid options") || !strings.Contains(err.Error(), "go") {
|
||||
t.Fatalf("unknown option not refused with the list: %v", err)
|
||||
}
|
||||
|
||||
decided, err := r.decide(protocol.Message{RunID: run.ID, Option: "go", Feedback: "looks good"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decided.Status != "running" {
|
||||
t.Fatalf("decided run did not resume: %+v", decided.RunSummary)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err = store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "succeeded" || len(detail.Steps) != 2 {
|
||||
t.Fatalf("run did not resume to completion: %+v", detail.RunSummary)
|
||||
}
|
||||
gate := detail.Steps[0]
|
||||
if gate.Status != "completed" || gate.Outcome != "go" || gate.Target != "worker" || gate.Diagnostic != "" {
|
||||
t.Fatalf("decision evidence not recorded: %+v", gate)
|
||||
}
|
||||
if gate.Output["option"] != "go" || gate.Output["feedback"] != "looks good" {
|
||||
t.Fatalf("decision output missing option/feedback: %+v", gate.Output)
|
||||
}
|
||||
if gate.Verdict == nil || gate.Verdict.Option != "go → worker" || gate.Verdict.Reason != "looks good" {
|
||||
t.Fatalf("derived verdict missing the decision: %+v", gate.Verdict)
|
||||
}
|
||||
|
||||
// A repeated decision for the already-decided visit must be an idempotent
|
||||
// no-op (applied=false, no error) and must not create another step. Naming
|
||||
// the stage explicitly keeps this on the decided visit instead of relying on
|
||||
// current_stage having advanced past it.
|
||||
repeat, err := r.decide(protocol.Message{RunID: run.ID, Stage: "gate", Option: "go"})
|
||||
if err != nil {
|
||||
t.Fatalf("repeat decision for a decided visit was refused: %v", err)
|
||||
}
|
||||
if repeat == nil || repeat.Status != "succeeded" {
|
||||
t.Fatalf("repeat decision changed the run: %+v", repeat)
|
||||
}
|
||||
after, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(after.Steps) != 2 {
|
||||
t.Fatalf("repeat decision created another step: %+v", after.Steps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanDecisionToTerminalFinalizesRun(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "terminal.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.wg.Wait()
|
||||
r.execute = successExecute
|
||||
run := startHumanRun(t, ctx, r, store, "human-halt")
|
||||
if _, err := r.decide(protocol.Message{RunID: run.ID, Option: "halt", Feedback: "approved"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "succeeded" || len(detail.Steps) != 1 || detail.Steps[0].Target != protocol.SuccessEnd {
|
||||
t.Fatalf("decision to success did not finalize the run: %+v", detail.RunSummary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanStageCancelRetryAndRebuild(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
path := filepath.Join(t.TempDir(), "rebuild.db")
|
||||
store, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = successExecute
|
||||
run := startHumanRun(t, ctx, r, store, "human-rebuild")
|
||||
|
||||
// Cancel works while waiting.
|
||||
if err := r.cancel(run.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "canceled" || detail.Steps[0].Status != "canceled" {
|
||||
t.Fatalf("waiting run not canceled: %+v %+v", detail.RunSummary, detail.Steps)
|
||||
}
|
||||
if err := r.cancel(run.ID); err == nil {
|
||||
t.Fatal("canceling a canceled run succeeded")
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Rebuild runtime state from persisted rows: a second Open + newRunner over
|
||||
// the same database, with no process restart. RecoverRuns must leave a
|
||||
// waiting run waiting.
|
||||
store, err = pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
r2, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r2.wg.Wait()
|
||||
r2.execute = successExecute
|
||||
waiting := startHumanRun(t, ctx, r2, store, "human-rebuild-2")
|
||||
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err = pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r3, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r3.wg.Wait()
|
||||
r3.execute = successExecute
|
||||
recovered, err := r3.getRun(waiting.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if recovered.Status != protocol.RunStatusWaiting || recovered.Steps[0].Status != protocol.StepStatusWaiting {
|
||||
t.Fatalf("rebuilt runtime lost the wait: %+v %+v", recovered.RunSummary, recovered.Steps)
|
||||
}
|
||||
if _, err := r3.decide(protocol.Message{RunID: waiting.ID, Option: "go", Feedback: "after restart"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r3.wg.Wait()
|
||||
final, err := store.GetRun(ctx, waiting.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if final.Status != "succeeded" || final.Steps[0].Outcome != "go" || final.Steps[0].Output["feedback"] != "after restart" {
|
||||
t.Fatalf("rebuilt runtime refused or mis-recorded the decision: %+v", final)
|
||||
}
|
||||
|
||||
// Retry works after a decision that later fails.
|
||||
failing := startHumanRun(t, ctx, r3, store, "human-retry")
|
||||
r3.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{}, fmt.Errorf("provider failed")
|
||||
}
|
||||
if _, err := r3.decide(protocol.Message{RunID: failing.ID, Option: "go"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r3.wg.Wait()
|
||||
failed, err := store.GetRun(ctx, failing.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if failed.Status != "failed" {
|
||||
t.Fatalf("post-decision failure did not fail the run: %+v", failed.RunSummary)
|
||||
}
|
||||
r3.execute = successExecute
|
||||
retried, err := r3.retry(protocol.Message{RunID: failing.ID, RequestKey: "retry-human"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r3.wg.Wait()
|
||||
retriedDetail, err := store.GetRun(ctx, retried.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if retried.ID == failing.ID || retriedDetail.Status != protocol.RunStatusWaiting {
|
||||
t.Fatalf("retry after a decision did not start a fresh waiting run: %+v", retriedDetail.RunSummary)
|
||||
}
|
||||
}
|
||||
|
||||
const humanOnlySource = "# stage gate\n\n```output\nDecision evidence.\n```\n\n```prompt\nApprove the release.\n```\n\n```edges\ngo -> #success\nhalt -> #failure\nerror -> #failure\n```\n\n```configs\ntype=human\n```\n\n"
|
||||
|
||||
func TestHumanRunWebSocketWaitAndDecide(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "ws.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "human-ws", humanOnlySource); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
handler := Handler(ctx, store).(*runHandler)
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
srv := httptest.NewServer(handler)
|
||||
defer srv.Close()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
var snapshot protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &snapshot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := func(message protocol.Message) protocol.Message {
|
||||
t.Helper()
|
||||
if err := wsjson.Write(ctx, conn, message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return response
|
||||
}
|
||||
started := request(protocol.Message{Type: protocol.RunPipeline, ID: "start", PipelineID: items[0].ID, Version: 1, RequestKey: "ws-human", Input: map[string]any{"prompt": "ship it"}})
|
||||
if started.Type != protocol.RunStarted || started.RunDetail == nil {
|
||||
t.Fatalf("run not started: %+v", started)
|
||||
}
|
||||
runID := started.RunDetail.ID
|
||||
|
||||
var waiting protocol.Message
|
||||
for i := 0; i < 50; i++ {
|
||||
waiting = request(protocol.Message{Type: protocol.GetRun, ID: "poll", RunID: runID})
|
||||
if waiting.RunDetail != nil && waiting.RunDetail.Status == protocol.RunStatusWaiting {
|
||||
break
|
||||
}
|
||||
if waiting.Type == protocol.Error {
|
||||
t.Fatal(waiting.Error)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if waiting.RunDetail == nil || waiting.RunDetail.Status != protocol.RunStatusWaiting || waiting.RunDetail.CurrentStage != "gate" {
|
||||
t.Fatalf("run never reached waiting: %+v", waiting.RunDetail)
|
||||
}
|
||||
if len(waiting.RunDetail.Steps) != 1 || waiting.RunDetail.Steps[0].Status != protocol.StepStatusWaiting {
|
||||
t.Fatalf("waiting step not visible over websocket: %+v", waiting.RunDetail.Steps)
|
||||
}
|
||||
if prompt := protocol.WaitingPrompt(waiting.RunDetail.Steps[0].Output); !strings.Contains(prompt, "Approve the release") {
|
||||
t.Fatalf("prompt not visible over websocket: %+v", waiting.RunDetail.Steps[0].Output)
|
||||
}
|
||||
if options := protocol.WaitingOptions(waiting.RunDetail.Steps[0].Output); strings.Join(options, ",") != "go,halt" {
|
||||
t.Fatalf("options not visible over websocket: %+v", options)
|
||||
}
|
||||
|
||||
// Unknown option is refused verbatim over the wire.
|
||||
bad := request(protocol.Message{Type: protocol.DecideRun, ID: "bad", RunID: runID, Option: "launch"})
|
||||
if bad.Type != protocol.Error || !strings.Contains(bad.Error, "valid options") {
|
||||
t.Fatalf("unknown option not refused over websocket: %+v", bad)
|
||||
}
|
||||
|
||||
decided := request(protocol.Message{Type: protocol.DecideRun, ID: "decide", RunID: runID, Option: "go", Feedback: "approved"})
|
||||
if decided.Type != protocol.Run || decided.RunDetail == nil {
|
||||
t.Fatalf("decision not accepted over websocket: %+v", decided)
|
||||
}
|
||||
var final protocol.Message
|
||||
for i := 0; i < 50; i++ {
|
||||
final = request(protocol.Message{Type: protocol.GetRun, ID: "final", RunID: runID})
|
||||
if final.RunDetail != nil && final.RunDetail.Status != protocol.RunStatusWaiting && final.RunDetail.Status != "running" {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if final.RunDetail == nil || final.RunDetail.Status != "succeeded" {
|
||||
t.Fatalf("decided run did not complete over websocket: %+v", final.RunDetail)
|
||||
}
|
||||
if final.RunDetail.Steps[0].Outcome != "go" || final.RunDetail.Steps[0].Output["feedback"] != "approved" {
|
||||
t.Fatalf("decision evidence not persisted over websocket: %+v", final.RunDetail.Steps[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHumanStageWaitingNotification(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "notify.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
var calls atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
filename, _ := new(mime.WordDecoder).DecodeHeader(r.Header.Get("Filename"))
|
||||
ack := map[string]any{"id": "msg1", "event": "message"}
|
||||
if filename != "" {
|
||||
ack["attachment"] = map[string]string{"name": filename, "url": "https://ntfy.example.com/file/" + filename}
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(ack)
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/pipelines")
|
||||
|
||||
source := strings.Replace(humanPipelineSource, "```configs\ntype=human\n```", "```notify\non=waiting\n```\n\n```configs\ntype=human\n```", 1)
|
||||
if _, err := store.Submit(ctx, "human-notify", source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
var pipelineID int64
|
||||
for _, p := range items {
|
||||
if p.Name == "human-notify" {
|
||||
pipelineID = p.ID
|
||||
}
|
||||
}
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.wg.Wait()
|
||||
run, err := r.start(protocol.Message{PipelineID: pipelineID, Version: 1, RequestKey: "human-notify", Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
if calls.Load() != 1 {
|
||||
t.Fatalf("waiting notification fired %d times", calls.Load())
|
||||
}
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(detail.Steps[0].Notifications) != 1 || detail.Steps[0].Notifications[0].Status != "sent" {
|
||||
t.Fatalf("waiting notification receipt missing: %+v", detail.Steps[0].Notifications)
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,14 @@ import (
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func (r *runner) notifyStep(id int64, d *protocol.PipelineDetail, workspace string, policy *protocol.NotificationPolicy, step protocol.RunStep) {
|
||||
func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.NotificationPolicy, step protocol.RunStep) {
|
||||
if policy == nil {
|
||||
return
|
||||
}
|
||||
event := "success"
|
||||
if step.Status != "completed" || step.Outcome == "error" || step.Target == protocol.FailureEnd {
|
||||
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
|
||||
@@ -34,7 +36,7 @@ func (r *runner) notifyStep(id int64, d *protocol.PipelineDetail, workspace stri
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
summary := fmt.Sprintf("%s · run #%d · stage %s · %s (%s)", d.Name, id, step.Stage, event, step.Outcome)
|
||||
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) {
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestStageNotificationEventsAndArtifactAudit(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before := calls.Load()
|
||||
r.notifyStep(id, d, workspace, policy, step)
|
||||
r.notifyStep(id, d.Name, workspace, policy, step)
|
||||
detail, err := store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
+212
-70
@@ -234,15 +234,9 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
return nil, err
|
||||
}
|
||||
if created {
|
||||
ctx, cancel := context.WithCancel(r.ctx)
|
||||
r.active[id] = activeRun{cancel: cancel, workspace: workspace}
|
||||
r.wg.Add(1)
|
||||
go func() {
|
||||
defer r.wg.Done()
|
||||
defer cancel()
|
||||
defer func() { r.mu.Lock(); delete(r.active, id); r.mu.Unlock() }()
|
||||
r.launch(id, workspace, func(ctx context.Context) {
|
||||
r.run(ctx, id, d, workspace, request)
|
||||
}()
|
||||
})
|
||||
}
|
||||
detail, err := r.store.GetRun(r.ctx, id)
|
||||
if detail != nil {
|
||||
@@ -251,6 +245,22 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
return detail, err
|
||||
}
|
||||
|
||||
// launch registers an active run's cancellation seam and workspace, adds it to
|
||||
// the wait group and starts fn on a derived cancellable context that cleans up
|
||||
// the registration when it returns. A fresh run and a resumed decision both go
|
||||
// through it, so registration and teardown live once. Callers must hold r.mu.
|
||||
func (r *runner) launch(id int64, workspace string, fn func(context.Context)) {
|
||||
ctx, cancel := context.WithCancel(r.ctx)
|
||||
r.active[id] = activeRun{cancel: cancel, workspace: workspace}
|
||||
r.wg.Add(1)
|
||||
go func() {
|
||||
defer r.wg.Done()
|
||||
defer cancel()
|
||||
defer func() { r.mu.Lock(); delete(r.active, id); r.mu.Unlock() }()
|
||||
fn(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
// checkWorkspaceFree refuses a run when an active run is already changing the
|
||||
// same resolved workspace. The workspace is the only scope a run reserves, and
|
||||
// one active run per workspace keeps this scan finding at most one match, so the
|
||||
@@ -319,65 +329,174 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
|
||||
func (r *runner) cancel(id int64) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
entry, ok := r.active[id]
|
||||
if !ok {
|
||||
return fmt.Errorf("run is not active")
|
||||
r.mu.Unlock()
|
||||
if ok {
|
||||
entry.cancel()
|
||||
return nil
|
||||
}
|
||||
entry.cancel()
|
||||
return nil
|
||||
// A run that is waiting on a person has no live goroutine, so cancel it at
|
||||
// the persistence layer instead. This keeps cancel working across a restart.
|
||||
return r.store.CancelWaiting(r.ctx, id)
|
||||
}
|
||||
|
||||
// setTitle renames a run: it replaces the stored operator-facing title without
|
||||
// touching the run's input, steps or execution, then returns the updated detail.
|
||||
func (r *runner) setTitle(id int64, title string) (*protocol.RunDetail, error) {
|
||||
explicit, ok := summarise.ExplicitTitle(title)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("a title with usable text is required")
|
||||
// decide records an operator's choice for a waiting human stage and resumes the
|
||||
// run along the chosen edge. ResolveDecision owns validation and idempotency, so
|
||||
// decide does no option parsing of its own. A resumed run is registered and
|
||||
// launched exactly like a fresh one but starts from the persisted state.
|
||||
func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
if request.RunID <= 0 {
|
||||
return nil, fmt.Errorf("decide requires a positive run ID")
|
||||
}
|
||||
if err := r.store.SetRunTitle(r.ctx, id, explicit); err != nil {
|
||||
r.mu.Lock()
|
||||
detail, err := r.store.GetRun(r.ctx, request.RunID)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
return r.getRun(id)
|
||||
if _, busy := r.active[detail.ID]; busy {
|
||||
r.mu.Unlock()
|
||||
return nil, fmt.Errorf("run %d is already active; wait for it to pause before deciding", detail.ID)
|
||||
}
|
||||
if detail.Status == protocol.RunStatusWaiting {
|
||||
if err := r.checkWorkspaceFree(detail.Workspace); err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
step, applied, err := r.store.ResolveDecision(r.ctx, detail.ID, request.Stage, request.Option, request.Feedback)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
if applied {
|
||||
for i := range detail.Steps {
|
||||
if detail.Steps[i].Sequence == step.Sequence && detail.Steps[i].Stage == step.Stage {
|
||||
detail.Steps[i] = step
|
||||
break
|
||||
}
|
||||
}
|
||||
r.launch(detail.ID, detail.Workspace, func(ctx context.Context) {
|
||||
r.resume(ctx, detail.ID, detail, detail.Workspace, step)
|
||||
})
|
||||
}
|
||||
r.mu.Unlock()
|
||||
return r.getRun(request.RunID)
|
||||
}
|
||||
|
||||
func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, workspace string, request protocol.Message) {
|
||||
input := stage.Values(request.Input)
|
||||
key := d.Graph.Stages[0].Key
|
||||
visits := map[string]int{}
|
||||
budgets := map[string]*stage.Budget{}
|
||||
finish := func(status, outcome, diagnostic string) {
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := r.store.FinishRun(c, id, status, outcome, diagnostic, input); err != nil {
|
||||
log.Printf("finish run %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
original, copyErr := copyValues(stage.Values(request.Input))
|
||||
if copyErr != nil {
|
||||
finish("failed", "error", copyErr.Error())
|
||||
r.finishRunState(id, "failed", "error", copyErr.Error(), stage.Values(request.Input))
|
||||
return
|
||||
}
|
||||
memory := map[string]memoryValue{"input": {output: original}}
|
||||
input := stage.Values(request.Input)
|
||||
state := newRunState(d.Graph.Stages[0].Key, input)
|
||||
state.memory["input"] = memoryValue{output: original}
|
||||
r.runLoop(ctx, id, d.Graph, d.Name, workspace, request, state)
|
||||
}
|
||||
|
||||
// terminalFinish classifies a stage's resolved target. A terminal key yields the
|
||||
// run's final status, outcome and diagnostic; an empty target means the stage
|
||||
// named an undeclared outcome; any other value is a stage key and is not
|
||||
// terminal. resume and the loop tail share it, so terminal routing lives once.
|
||||
func terminalFinish(target, stageKey, outcome, diagnostic string) (status, finalOutcome, finalDiagnostic string, terminal bool) {
|
||||
switch target {
|
||||
case protocol.SuccessEnd:
|
||||
return "succeeded", "success", "", true
|
||||
case protocol.FailureEnd:
|
||||
if diagnostic == "" {
|
||||
diagnostic = fmt.Sprintf("stage %s returned outcome %q", stageKey, outcome)
|
||||
}
|
||||
return "failed", "error", diagnostic, true
|
||||
case "":
|
||||
return "failed", "error", "stage returned an undeclared outcome", true
|
||||
}
|
||||
return "", "", "", false
|
||||
}
|
||||
|
||||
// resume continues a decided run from the state already persisted for its run
|
||||
// detail: memory from the run input and the completed steps, visits counted per
|
||||
// stage, and the next sequence above the highest recorded one. It then runs the
|
||||
// shared loop from the decision's target; no full-table scan and no extra
|
||||
// per-step query are introduced.
|
||||
func (r *runner) resume(ctx context.Context, id int64, d *protocol.RunDetail, workspace string, step protocol.RunStep) {
|
||||
// The decision may route straight to a terminal, which has no stage to run.
|
||||
if status, outcome, diagnostic, terminal := terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic); terminal {
|
||||
r.finishRunState(id, status, outcome, diagnostic, stage.Values(step.Output))
|
||||
return
|
||||
}
|
||||
state := rebuildRunState(d, step)
|
||||
r.runLoop(ctx, id, d.Graph, d.Name, workspace, protocol.Message{WritePaths: d.WritePaths, AllowTools: d.AllowTools}, state)
|
||||
}
|
||||
|
||||
// runState is the loop's whole mutable state, extracted so a fresh run and a
|
||||
// resumed run share one implementation. nextSequence is set before each visit so
|
||||
// a wait that returns to the caller records where the run will continue.
|
||||
type runState struct {
|
||||
key string
|
||||
nextSequence int
|
||||
visits map[string]int
|
||||
memory map[string]memoryValue
|
||||
input stage.Values
|
||||
}
|
||||
|
||||
func newRunState(key string, input stage.Values) *runState {
|
||||
return &runState{key: key, nextSequence: 1, visits: map[string]int{}, memory: map[string]memoryValue{}, input: input}
|
||||
}
|
||||
|
||||
func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runState {
|
||||
state := newRunState(decided.Target, stage.Values(decided.Output))
|
||||
if original, err := copyValues(stage.Values(detail.Input)); err == nil {
|
||||
state.memory["input"] = memoryValue{output: original}
|
||||
}
|
||||
for _, step := range detail.Steps {
|
||||
state.visits[step.Stage]++
|
||||
if step.Sequence >= state.nextSequence {
|
||||
state.nextSequence = step.Sequence + 1
|
||||
}
|
||||
if step.Status == "completed" {
|
||||
if values, err := copyValues(stage.Values(step.Output)); err == nil {
|
||||
state.memory[step.Stage] = memoryValue{output: values, sequence: step.Sequence}
|
||||
}
|
||||
}
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
func (r *runner) finishRunState(id int64, status, outcome, diagnostic string, input stage.Values) {
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := r.store.FinishRun(c, id, status, outcome, diagnostic, input); err != nil {
|
||||
log.Printf("finish run %d: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, name, workspace string, request protocol.Message, state *runState) {
|
||||
finish := func(status, outcome, diagnostic string) {
|
||||
r.finishRunState(id, status, outcome, diagnostic, state.input)
|
||||
}
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
finish("failed", "error", "stage execution panicked")
|
||||
}
|
||||
}()
|
||||
for sequence := 1; sequence <= 30; sequence++ {
|
||||
budgets := map[string]*stage.Budget{}
|
||||
if state.key == "" {
|
||||
finish("failed", "error", "stage returned an undeclared outcome")
|
||||
return
|
||||
}
|
||||
for sequence := state.nextSequence; sequence <= 30; sequence++ {
|
||||
state.nextSequence = sequence + 1
|
||||
if ctx.Err() != nil {
|
||||
finish("canceled", "", ctx.Err().Error())
|
||||
return
|
||||
}
|
||||
visits[key]++
|
||||
if visits[key] > 5 {
|
||||
finish("failed", "error", "stage visit budget exceeded")
|
||||
return
|
||||
}
|
||||
key := state.key
|
||||
var def stage.Definition
|
||||
var selected protocol.Stage
|
||||
found := false
|
||||
for _, s := range d.Graph.Stages {
|
||||
for _, s := range graph.Stages {
|
||||
if s.Key == key {
|
||||
selected = s
|
||||
def = pipeline.StageDefinition(s)
|
||||
@@ -389,16 +508,27 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
finish("failed", "error", "missing stage")
|
||||
return
|
||||
}
|
||||
isHuman := stage.EffectiveType(def.Type) == stage.HumanType
|
||||
state.visits[key]++
|
||||
maxVisits := 5
|
||||
if selected.Limits != nil {
|
||||
maxVisits = selected.Limits.MaxVisits
|
||||
}
|
||||
if visits[key] > maxVisits {
|
||||
finish("failed", "error", fmt.Sprintf("stage %s visit limit reached (%d)", key, maxVisits))
|
||||
return
|
||||
// A human stage pauses for a person; neither the global visit budget nor
|
||||
// the per-stage max_visits may kill it. The shared 30-step run budget
|
||||
// still bounds every run.
|
||||
if !isHuman {
|
||||
if state.visits[key] > 5 {
|
||||
finish("failed", "error", "stage visit budget exceeded")
|
||||
return
|
||||
}
|
||||
if state.visits[key] > maxVisits {
|
||||
finish("failed", "error", fmt.Sprintf("stage %s visit limit reached (%d)", key, maxVisits))
|
||||
return
|
||||
}
|
||||
}
|
||||
var reads map[string]protocol.MemoryRead
|
||||
input, reads, copyErr = memoryInput(input, selected.Memory, memory)
|
||||
input, reads, copyErr := memoryInput(state.input, selected.Memory, state.memory)
|
||||
if copyErr != nil {
|
||||
finish("failed", "error", copyErr.Error())
|
||||
return
|
||||
@@ -411,6 +541,22 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
finish("failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
if isHuman {
|
||||
// A human step is O(1): one BeginStep plus one MarkWaiting, no
|
||||
// filediff capture, no artifacts directory and no workspace mutation.
|
||||
evidence := protocol.NewWaitingEvidence(def.Blocks["prompt"], stage.HumanOptions(def.Edges))
|
||||
if err := r.store.MarkWaiting(ctx, id, sequence, key, evidence); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
finish("canceled", "", ctx.Err().Error())
|
||||
return
|
||||
}
|
||||
finish("failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
waitStep := protocol.RunStep{Sequence: sequence, Stage: key, Status: protocol.StepStatusWaiting, Output: evidence}
|
||||
r.notifyStep(id, name, workspace, selected.Notify, waitStep)
|
||||
return
|
||||
}
|
||||
var tools []string
|
||||
explicitTools := false
|
||||
var toolErr error
|
||||
@@ -541,8 +687,8 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
step.Transcript = stage.RetainSession(step.Transcript, stageSession)
|
||||
}
|
||||
step.Target = def.Edges[step.Outcome]
|
||||
step.Control = &protocol.ControlEvidence{Visit: visits[key], MaxVisits: maxVisits, Memory: reads}
|
||||
if err == nil && selected.Limits != nil && visits[key] >= maxVisits {
|
||||
step.Control = &protocol.ControlEvidence{Visit: state.visits[key], MaxVisits: maxVisits, Memory: reads}
|
||||
if err == nil && selected.Limits != nil && state.visits[key] >= maxVisits {
|
||||
for _, outcome := range selected.Limits.On {
|
||||
if outcome == step.Outcome {
|
||||
step.Control.LimitReached = true
|
||||
@@ -579,15 +725,10 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
finish("failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
memory[key] = memoryValue{output: values, sequence: sequence}
|
||||
state.memory[key] = memoryValue{output: values, sequence: sequence}
|
||||
}
|
||||
for _, s := range d.Graph.Stages {
|
||||
if s.Key == key {
|
||||
r.notifyStep(id, d, workspace, s.Notify, step)
|
||||
break
|
||||
}
|
||||
}
|
||||
input = stage.Values(step.Output)
|
||||
r.notifyStep(id, name, workspace, selected.Notify, step)
|
||||
state.input = stage.Values(step.Output)
|
||||
if useAgent || errors.Is(err, stage.ErrStageCleanup) {
|
||||
sessions, checkErr := r.store.ActiveAgentSessionsForRun(context.Background(), id)
|
||||
if checkErr != nil || len(sessions) > 0 {
|
||||
@@ -599,23 +740,24 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
finish("canceled", "", ctx.Err().Error())
|
||||
return
|
||||
}
|
||||
if step.Target == protocol.SuccessEnd {
|
||||
finish("succeeded", "success", "")
|
||||
if status, outcome, diagnostic, terminal := terminalFinish(step.Target, step.Stage, step.Outcome, step.Diagnostic); terminal {
|
||||
finish(status, outcome, diagnostic)
|
||||
return
|
||||
}
|
||||
if step.Target == protocol.FailureEnd {
|
||||
diagnostic := step.Diagnostic
|
||||
if diagnostic == "" {
|
||||
diagnostic = fmt.Sprintf("stage %s returned outcome %q", step.Stage, step.Outcome)
|
||||
}
|
||||
finish("failed", "error", diagnostic)
|
||||
return
|
||||
}
|
||||
if step.Target == "" {
|
||||
finish("failed", "error", "stage returned an undeclared outcome")
|
||||
return
|
||||
}
|
||||
key = step.Target
|
||||
state.key = step.Target
|
||||
}
|
||||
finish("failed", "error", "total stage budget exceeded")
|
||||
}
|
||||
|
||||
// setTitle renames a run: it replaces the stored operator-facing title without
|
||||
// touching the run's input, steps or execution, then returns the updated detail.
|
||||
func (r *runner) setTitle(id int64, title string) (*protocol.RunDetail, error) {
|
||||
explicit, ok := summarise.ExplicitTitle(title)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("a title with usable text is required")
|
||||
}
|
||||
if err := r.store.SetRunTitle(r.ctx, id, explicit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.getRun(id)
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
} else {
|
||||
response.Pipelines = items
|
||||
}
|
||||
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
|
||||
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
|
||||
var err error
|
||||
switch request.Type {
|
||||
case protocol.GetRunEvidence:
|
||||
@@ -220,6 +220,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
case protocol.RetryRun:
|
||||
response.Type = protocol.RunStarted
|
||||
response.RunDetail, err = runs.retry(request)
|
||||
case protocol.DecideRun:
|
||||
response.Type = protocol.Run
|
||||
response.RunDetail, err = runs.decide(request)
|
||||
case protocol.SetRunTitle:
|
||||
response.Type = protocol.Run
|
||||
response.RunDetail, err = runs.setTitle(request.RunID, request.Title)
|
||||
|
||||
Reference in New Issue
Block a user