Merge lane-go: titles, verdicts, human stage, line result format, stale-client guard
Integrates nine commits of reviewed work from the lane-go branch after the automated resolver failed on this merge five times (each attempt dying on the malformed-result defect whose fix is contained in this same merge). Integration decisions: - Search: kept main's shipped design (Store.Search + SearchResponse.Results) and dropped lane-go's parallel, older attempt (an unused Message.Matches field, an absent runs.search, and two partial TUI plumbing blocks). lane-go itself did not compile: protocol.SearchMatch was undefined and model.go referenced searchNav, requestSearch and applyRunNav, which exist nowhere. Those partial TUI blocks are removed; the queued TUI omni-search item will implement the surface properly on main's design. - Both sides' additions are kept where they are independent: run titles and provenance, the human decision stage and its CLI/TUI surface, the derived stage verdict, the stale-client guard, the line-oriented result format, and main's search command. - The search key's help label is now "/ omni search"; the two tests asserting the old label are updated rather than the label reverted. - docs/cli.md and the packaged operations reference carry both sides' text. Verified: go build ./... , go vet ./... and the full go test ./... suite pass.
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/runinspect"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -143,3 +144,104 @@ func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) {
|
||||
t.Fatal("wait lost terminal state", output)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCLIRunTitlesExplicitDerivedAndRename is the end-to-end title check: it
|
||||
// drives the real CLI against the real websocket server and store, starts one
|
||||
// run with --title and one without, and verifies the title is visible in the
|
||||
// human `runs` list, in `inspect`, and after a `title` rename. Stage execution is
|
||||
// stubbed because the environment must not call a model, so the without-flag run
|
||||
// exercises the server-side prompt fallback rather than the shared summariser.
|
||||
func TestCLIRunTitlesExplicitDerivedAndRename(t *testing.T) {
|
||||
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "cli-titles.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "workflow", pipeline.Template([]string{"worker"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := Handler(ctx, store)
|
||||
r := handler.(*runHandler).runner
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
|
||||
}
|
||||
http := httptest.NewServer(handler)
|
||||
defer http.Close()
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect"
|
||||
run := func(args ...string) (string, error) {
|
||||
var out bytes.Buffer
|
||||
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
|
||||
err := app.Run(ctx, append([]string{"--url", url}, args...))
|
||||
return out.String(), err
|
||||
}
|
||||
|
||||
output, err := run("run", "workflow", "first prompt line\nmore detail", "--title", "Operator chosen title", "--yes", "--json", "--wait")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var started protocol.Message
|
||||
if err := json.Unmarshal([]byte(output), &started); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
explicitID := started.RunDetail.ID
|
||||
if started.RunDetail.Title != "Operator chosen title" || started.RunDetail.TitleSource != summarise.SourceExplicit {
|
||||
t.Fatalf("explicit --title = %+v", started.RunDetail)
|
||||
}
|
||||
|
||||
output, err = run("run", "workflow", "Derived title from prompt\nsecond line", "--yes", "--json", "--wait")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(output), &started); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if started.RunDetail.Title != "Derived title from prompt" || started.RunDetail.TitleSource != summarise.SourceFallback {
|
||||
t.Fatalf("derived title = %+v", started.RunDetail)
|
||||
}
|
||||
|
||||
output, err = run("runs")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("pipelines runs:\n%s", output)
|
||||
for _, want := range []string{
|
||||
fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", explicitID, "Operator chosen title", "workflow", "succeeded"),
|
||||
fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", started.RunDetail.ID, "Derived title from prompt", "workflow", "succeeded"),
|
||||
} {
|
||||
if !strings.Contains(output, want) {
|
||||
t.Fatalf("runs output missing %q:\n%s", want, output)
|
||||
}
|
||||
}
|
||||
|
||||
output, err = run("inspect", fmt.Sprint(explicitID), "--json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var detail protocol.RunDetail
|
||||
if err := json.Unmarshal([]byte(output), &detail); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Title != "Operator chosen title" || detail.Name != "workflow" || detail.Version != 1 {
|
||||
t.Fatalf("inspect omitted title/name/version: %+v", detail)
|
||||
}
|
||||
|
||||
output, err = run("title", fmt.Sprint(explicitID), "Renamed after the fact")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("pipelines title: %s", strings.TrimSpace(output))
|
||||
output, err = run("inspect", fmt.Sprint(explicitID), "--json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(output), &detail); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Title != "Renamed after the fact" || detail.TitleSource != summarise.SourceExplicit {
|
||||
t.Fatalf("rename not visible in inspect: %+v", detail)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
+298
-80
@@ -18,6 +18,7 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
// activeRun records one in-flight run's cancellation seam and the resolved
|
||||
@@ -35,8 +36,16 @@ type runner struct {
|
||||
mu sync.Mutex
|
||||
active map[int64]activeRun
|
||||
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
|
||||
wg sync.WaitGroup
|
||||
schedulerWG sync.WaitGroup
|
||||
// title generates the operator-facing run title from the run prompt. When nil
|
||||
// the store derives a fallback from the prompt with no model call, so tests
|
||||
// and unconfigured servers stay deterministic.
|
||||
title func(context.Context, string) summarise.Title
|
||||
// verdict generates the bounded one-line reason for a completed stage visit
|
||||
// from its output map. When nil the store derives the reason at read time with
|
||||
// no model call. It is best-effort: any failure must never fail the stage.
|
||||
verdict func(context.Context, map[string]any) string
|
||||
wg sync.WaitGroup
|
||||
schedulerWG sync.WaitGroup
|
||||
}
|
||||
|
||||
func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
|
||||
@@ -158,13 +167,19 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
}
|
||||
}
|
||||
}
|
||||
// Decide admission and idempotency under the lock, but generate the title
|
||||
// only for a request that will actually start a run: a rejected or replayed
|
||||
// request must not pay for a bounded model generation, and a live one must
|
||||
// not hold the lock (stalling listings and cancellation) while a model is
|
||||
// called. An unavailable or slow model falls back to the prompt's first line.
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.ctx.Err() != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, r.ctx.Err()
|
||||
}
|
||||
owners, err := r.store.ActiveSessionOwners(r.ctx)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
// Refuse only an orphaned session: one whose owning run is not active now.
|
||||
@@ -172,41 +187,56 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
|
||||
// workspace) must not block new work.
|
||||
for _, owner := range owners {
|
||||
if _, ok := r.active[owner.RunID]; !ok {
|
||||
r.mu.Unlock()
|
||||
return nil, fmt.Errorf("an agent may still be active; restart recovery must interrupt it before new work")
|
||||
}
|
||||
}
|
||||
// Idempotent repeats are accepted even while the original run is active.
|
||||
if len(r.active) > 0 {
|
||||
existing, err := r.store.RunByKey(r.ctx, request.RequestKey)
|
||||
if err != nil {
|
||||
// Idempotent repeats are accepted even while the original run is active, and
|
||||
// a replay reuses the stored title instead of generating a new one.
|
||||
existing, err := r.store.RunByKey(r.ctx, request.RequestKey)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
if existing == 0 {
|
||||
if err := r.checkWorkspaceFree(workspace); err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
if existing == 0 {
|
||||
// The workspace is the only scope a run reserves: refuse a second
|
||||
// run only when an active run is already changing this exact
|
||||
// resolved directory. One active run per workspace keeps this scan
|
||||
// finding at most one match, so the message is deterministic.
|
||||
for id, entry := range r.active {
|
||||
if entry.workspace == workspace {
|
||||
return nil, fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace)
|
||||
}
|
||||
}
|
||||
}
|
||||
r.mu.Unlock()
|
||||
|
||||
var runTitle summarise.Title
|
||||
if existing == 0 {
|
||||
// An explicit operator title skips the model call; otherwise the
|
||||
// configured generator names the run, falling back to the prompt's
|
||||
// first line when it is unavailable or slow.
|
||||
if explicit, ok := summarise.ExplicitTitle(request.Title); ok {
|
||||
runTitle = explicit
|
||||
} else if r.title != nil {
|
||||
prompt, _ := request.Input["prompt"].(string)
|
||||
runTitle = r.title(r.ctx, prompt)
|
||||
}
|
||||
}
|
||||
id, created, err := r.store.CreateRun(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, retryOf...)
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
// Another run may have reserved this workspace while the title was
|
||||
// generated, so re-check before reserving it; a race on the request key is
|
||||
// handled by the store's idempotent insert.
|
||||
if existing == 0 {
|
||||
if err := r.checkWorkspaceFree(workspace); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, runTitle, retryOf...)
|
||||
if err != nil {
|
||||
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 {
|
||||
@@ -215,6 +245,35 @@ 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
|
||||
// message is deterministic. Callers must hold r.mu.
|
||||
func (r *runner) checkWorkspaceFree(workspace string) error {
|
||||
for id, entry := range r.active {
|
||||
if entry.workspace == workspace {
|
||||
return fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *runner) getRun(id int64) (*protocol.RunDetail, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
@@ -270,52 +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)
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
r.mu.Lock()
|
||||
detail, err := r.store.GetRun(r.ctx, request.RunID)
|
||||
if err != nil {
|
||||
r.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
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)
|
||||
@@ -327,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
|
||||
@@ -349,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
|
||||
@@ -472,6 +680,9 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
step.Diagnostic = err.Error()
|
||||
step.Outcome = "error"
|
||||
step.Output = stage.Values{"error": err.Error(), "stage": key, "input": input}
|
||||
if len(result.Output) > 0 {
|
||||
step.Output["salvage"] = result.Output
|
||||
}
|
||||
}
|
||||
// A failed or timed-out step carries the session pointer even when the
|
||||
// bounded transcript would otherwise be only its stage_limits envelope.
|
||||
@@ -479,8 +690,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
|
||||
@@ -493,6 +704,17 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
if panicked {
|
||||
step.Target = protocol.FailureEnd
|
||||
}
|
||||
// Generate the one-line verdict reason once, at completion. It is
|
||||
// best-effort: a summariser failure or timeout returns NoSummary and must
|
||||
// never fail the stage or the run, so every error is swallowed. A failed
|
||||
// step still gets a verdict from its error envelope.
|
||||
if r.verdict != nil {
|
||||
reasonCtx, reasonCancel := context.WithTimeout(context.Background(), summarise.DefaultTimeout)
|
||||
if line := r.verdict(reasonCtx, step.Output); line != "" && line != summarise.NoSummary {
|
||||
step.Verdict = &protocol.StageVerdict{Reason: line, ReasonSource: protocol.VerdictReasonModel}
|
||||
}
|
||||
reasonCancel()
|
||||
}
|
||||
save, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
persistErr := r.store.FinishStep(save, id, step)
|
||||
cancel()
|
||||
@@ -506,15 +728,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 {
|
||||
@@ -526,23 +743,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)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,194 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
// TestRunTitleGeneratedPersistedAndFallback proves the insert path stores a
|
||||
// generated title with its provenance in list and detail responses, and falls
|
||||
// back to a derived bounded title when no generator is wired, without failing
|
||||
// run creation.
|
||||
func TestRunTitleGeneratedPersistedAndFallback(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "titles.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "titled", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
||||
}
|
||||
r.title = func(context.Context, string) summarise.Title {
|
||||
return summarise.Title{Text: "Fix the flaky login test", Source: summarise.SourceModel, Model: "openai/gpt"}
|
||||
}
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "model-title", Workspace: t.TempDir(), Input: map[string]any{"prompt": "prompt body"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Title != "Fix the flaky login test" || detail.TitleSource != summarise.SourceModel || detail.TitleModel != "openai/gpt" {
|
||||
t.Fatalf("model title not stored with provenance: %+v", detail)
|
||||
}
|
||||
list, err := store.ListRuns(ctx)
|
||||
if err != nil || len(list) != 1 || list[0].Title != "Fix the flaky login test" {
|
||||
t.Fatalf("run list omitted the title: %v %v", list, err)
|
||||
}
|
||||
|
||||
// No generator: the fallback is derived from the prompt's first line and run
|
||||
// creation still succeeds.
|
||||
r.title = nil
|
||||
fallback, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "fallback-title", Workspace: t.TempDir(), Input: map[string]any{"prompt": "Fix the widget\nand more detail"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
detail, err = store.GetRun(ctx, fallback.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Title != "Fix the widget" || detail.TitleSource != summarise.SourceFallback || detail.TitleModel != "" {
|
||||
t.Fatalf("fallback title = %+v, want the prompt's first line", detail)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTitleGeneratorSkippedForReplayAndRejection proves the bounded
|
||||
// generation runs only for a request that starts a run: an idempotent replay and
|
||||
// a request refused for an already-reserved workspace must not pay for it.
|
||||
func TestRunTitleGeneratorSkippedForReplayAndRejection(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "skip.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "skip", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
release := make(chan struct{})
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
<-release
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
||||
}
|
||||
calls := 0
|
||||
r.title = func(context.Context, string) summarise.Title {
|
||||
calls++
|
||||
return summarise.Title{Text: "Generated", Source: summarise.SourceModel}
|
||||
}
|
||||
workspace := t.TempDir()
|
||||
first, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-key", Workspace: workspace, Input: map[string]any{"prompt": "first"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("first start generated %d titles, want 1", calls)
|
||||
}
|
||||
// A replay while the first run is active reuses the stored title.
|
||||
replay, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-key", Workspace: workspace, Input: map[string]any{"prompt": "first"}})
|
||||
if err != nil || replay.ID != first.ID {
|
||||
t.Fatalf("replay = %+v, %v; want run %d", replay, err, first.ID)
|
||||
}
|
||||
// A new key in the already-reserved workspace is refused.
|
||||
if _, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-other", Workspace: workspace, Input: map[string]any{"prompt": "second"}}); err == nil {
|
||||
t.Fatal("workspace conflict was not refused")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("replay/rejection generated %d titles, want 1", calls)
|
||||
}
|
||||
close(release)
|
||||
r.wg.Wait()
|
||||
}
|
||||
|
||||
func TestHandlerWiresRunTitleGenerator(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "wire.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
configured := HandlerWithOptions(ctx, Options{RunTitles: summarise.GenerateTitle}, store).(*runHandler)
|
||||
bare := HandlerWithOptions(ctx, Options{}, store).(*runHandler)
|
||||
defer func() { cancel(); WaitRuns(configured); WaitRuns(bare) }()
|
||||
if configured.runner.title == nil {
|
||||
t.Fatal("configured RunTitles was not applied to the runner")
|
||||
}
|
||||
if bare.runner.title != nil {
|
||||
t.Fatal("unconfigured RunTitles must keep deterministic fallback title generation")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunnerExplicitTitleSkipsGeneratorAndRenames proves an explicit --title is
|
||||
// stored verbatim and skips the model generator, and that setTitle later
|
||||
// replaces it with an explicit provenance without touching the run's input.
|
||||
func TestRunnerExplicitTitleSkipsGeneratorAndRenames(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "explicit.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "explicit", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
|
||||
}
|
||||
generated := 0
|
||||
r.title = func(context.Context, string) summarise.Title {
|
||||
generated++
|
||||
return summarise.Title{Text: "Generated", Source: summarise.SourceModel}
|
||||
}
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "explicit-key", Workspace: t.TempDir(), Title: "Operator chosen title", Input: map[string]any{"prompt": "prompt body"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
if generated != 0 {
|
||||
t.Fatalf("explicit title still called the generator %d times", generated)
|
||||
}
|
||||
if run.Title != "Operator chosen title" || run.TitleSource != summarise.SourceExplicit || run.TitleModel != "" {
|
||||
t.Fatalf("explicit title = %+v", run)
|
||||
}
|
||||
|
||||
renamed, err := r.setTitle(run.ID, " Renamed after the fact ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if renamed.Title != "Renamed after the fact" || renamed.TitleSource != summarise.SourceExplicit || renamed.Input["prompt"] != "prompt body" {
|
||||
t.Fatalf("rename = %+v", renamed)
|
||||
}
|
||||
if _, err := r.setTitle(run.ID, " "); err == nil {
|
||||
t.Fatal("blank rename was accepted")
|
||||
}
|
||||
if _, err := r.setTitle(run.ID+999, "ghost"); err == nil {
|
||||
t.Fatal("renaming an unknown run was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerRoutingPersistenceAndIdempotency(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -129,3 +315,110 @@ func TestRunnerRoutingPersistenceAndIdempotency(t *testing.T) {
|
||||
t.Fatalf("cycle not bounded: %+v", detail)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStepVerdictGeneratedOnceAtCompletion proves the wired seam runs exactly
|
||||
// once per completed stage visit, from the step's own output, and the generated
|
||||
// line is stored with model provenance.
|
||||
func TestStepVerdictGeneratedOnceAtCompletion(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "verdict.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "verdict", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "ran the checks"}}, nil
|
||||
}
|
||||
calls := 0
|
||||
r.verdict = func(_ context.Context, output map[string]any) string {
|
||||
calls++
|
||||
if output["summary"] != "ran the checks" {
|
||||
t.Errorf("verdict seam got output %v", output)
|
||||
}
|
||||
return "chosen because every check passed"
|
||||
}
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "verdict-once", Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
if calls != 1 {
|
||||
t.Fatalf("verdict seam ran %d times, want exactly once for one step", calls)
|
||||
}
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
verdict := detail.Steps[0].Verdict
|
||||
if verdict == nil || verdict.Reason != "chosen because every check passed" || verdict.ReasonSource != protocol.VerdictReasonModel {
|
||||
t.Fatalf("stored verdict = %+v", verdict)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStepVerdictFailureNeverFailsStage proves a best-effort failure (the
|
||||
// NoSummary sentinel) still lets the run reach its normal terminal status, with
|
||||
// the verdict derived at read time from the step's output.
|
||||
func TestStepVerdictFailureNeverFailsStage(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "verdict-fail.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "verdict-fail", pipeline.Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
||||
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "ran the checks"}}, nil
|
||||
}
|
||||
r.verdict = func(context.Context, map[string]any) string { return summarise.NoSummary }
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "verdict-fail", Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}})
|
||||
if 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" {
|
||||
t.Fatalf("summariser failure changed the run status: %+v", detail)
|
||||
}
|
||||
verdict := detail.Steps[0].Verdict
|
||||
if verdict == nil || verdict.Reason != "ran the checks" || verdict.ReasonSource != protocol.VerdictReasonFallback {
|
||||
t.Fatalf("read-time verdict after a summariser failure = %+v", verdict)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerWiresStepVerdictGenerator(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "verdict-wire.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
configured := HandlerWithOptions(ctx, Options{StepVerdicts: summarise.GenerateVerdictReason}, store).(*runHandler)
|
||||
bare := HandlerWithOptions(ctx, Options{}, store).(*runHandler)
|
||||
defer func() { cancel(); WaitRuns(configured); WaitRuns(bare) }()
|
||||
if configured.runner.verdict == nil {
|
||||
t.Fatal("configured StepVerdicts was not applied to the runner")
|
||||
}
|
||||
if bare.runner.verdict != nil {
|
||||
t.Fatal("unconfigured StepVerdicts must keep deterministic read-time derivation")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
// Options configures the HTTP handler.
|
||||
@@ -31,6 +32,15 @@ type Options struct {
|
||||
// it from the first store; when neither is available chat_sessions returns an
|
||||
// empty list rather than the endpoint's whole session list.
|
||||
OwnedSessions func(context.Context) ([]pipeline.SessionOwner, error)
|
||||
// RunTitles generates the operator-facing title stored with each new run.
|
||||
// When nil, run creation stores the prompt-derived fallback without a model
|
||||
// call, which keeps tests and unconfigured servers deterministic.
|
||||
RunTitles func(context.Context, string) summarise.Title
|
||||
// StepVerdicts generates the bounded one-line reason stored with each
|
||||
// completed stage visit. When nil, the store derives the verdict's reason at
|
||||
// read time with no model call, which keeps tests and unconfigured servers
|
||||
// deterministic.
|
||||
StepVerdicts func(context.Context, map[string]any) string
|
||||
}
|
||||
|
||||
// Handler serves the persistent connection with default options. Canceling ctx
|
||||
@@ -53,6 +63,12 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
if runErr == nil && options.ArtifactsRoot != "" {
|
||||
runs.artifactsRoot = options.ArtifactsRoot
|
||||
}
|
||||
if runErr == nil && options.RunTitles != nil {
|
||||
runs.title = options.RunTitles
|
||||
}
|
||||
if runErr == nil && options.StepVerdicts != nil {
|
||||
runs.verdict = options.StepVerdicts
|
||||
}
|
||||
if runErr == nil {
|
||||
runs.schedulerWG.Add(1)
|
||||
go runs.scheduleLoop()
|
||||
@@ -96,6 +112,16 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
defer connCancel()
|
||||
var writeMu sync.Mutex
|
||||
write := func(message protocol.Message) error {
|
||||
// Every server frame (responses, the welcome and chat events) carries
|
||||
// the build identity and protocol so an older client fails fast or
|
||||
// warns once instead of silently misreading a field. build is
|
||||
// computed once per handler.
|
||||
if message.Build == nil {
|
||||
message.Build = &build
|
||||
}
|
||||
if message.Protocol == 0 {
|
||||
message.Protocol = protocol.ProtocolVersion
|
||||
}
|
||||
writeMu.Lock()
|
||||
defer writeMu.Unlock()
|
||||
writeCtx, cancel := context.WithTimeout(connCtx, 5*time.Second)
|
||||
@@ -133,7 +159,6 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
response.Status = systemStatus(r, started, runErr)
|
||||
} else if request.Type == protocol.Ping {
|
||||
response.Time = time.Now().UTC().Format(time.RFC3339)
|
||||
response.Build = &build
|
||||
} else if request.Type == protocol.ListTemplates {
|
||||
response.Type = protocol.Templates
|
||||
response.Templates = examples.Catalog()
|
||||
@@ -208,7 +233,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
response.Error = err.Error()
|
||||
}
|
||||
}
|
||||
} 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.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 +245,12 @@ 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)
|
||||
case protocol.ListRuns:
|
||||
response.Type = protocol.Runs
|
||||
response.Runs, err = runs.listRuns()
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"github.com/coder/websocket/wsjson"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
@@ -77,6 +79,9 @@ func TestPersistentConnection(t *testing.T) {
|
||||
if response.Build == nil || response.Build.Version != wantBuild.Version || response.Build.Commit != wantBuild.Commit {
|
||||
t.Fatalf("pong missing server build identity: %+v", response.Build)
|
||||
}
|
||||
if response.Protocol != protocol.ProtocolVersion {
|
||||
t.Fatalf("pong protocol = %d, want %d", response.Protocol, protocol.ProtocolVersion)
|
||||
}
|
||||
stamp, err := time.Parse(time.RFC3339, response.Time)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -97,3 +102,38 @@ func TestPersistentConnection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestWelcomeEnvelopeCarriesServerIdentity proves a client that never pings
|
||||
// still learns the server build and protocol from the unsolicited welcome.
|
||||
func TestWelcomeEnvelopeCarriesServerIdentity(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "identity.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
handler := Handler(ctx, store)
|
||||
srv := httptest.NewServer(handler)
|
||||
defer srv.Close()
|
||||
defer func() { cancel(); WaitRuns(handler) }()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
var welcome protocol.Message
|
||||
if err := wsjson.Read(ctx, conn, &welcome); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if welcome.Type != protocol.PipelineList {
|
||||
t.Fatalf("first frame = %q, want welcome pipelines list", welcome.Type)
|
||||
}
|
||||
wantBuild := buildinfo.Current()
|
||||
if welcome.Build == nil || welcome.Build.Version != wantBuild.Version || welcome.Build.Commit != wantBuild.Commit {
|
||||
t.Fatalf("welcome missing server build identity: %+v", welcome.Build)
|
||||
}
|
||||
if welcome.Protocol != protocol.ProtocolVersion {
|
||||
t.Fatalf("welcome protocol = %d, want %d", welcome.Protocol, protocol.ProtocolVersion)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user