Derive a plain-language verdict for every completed stage visit

Each completed step now exposes a verdict: the chosen option (outcome -> target),
a bounded one-line reason with model/fallback/absent provenance, and the recorded
artifacts the reason refers to. The store persists only the model-generated line
in a new step_verdicts table and derives the fallback at read time (no backfill);
the server generates the line once at completion, best-effort; the TUI renders it
in the stage progress summary. The packaged debugging reference documents it.
This commit is contained in:
2026-10-16 13:54:48 +01:00
parent f3e0593f4d
commit e79ff9585f
14 changed files with 944 additions and 40 deletions
+16 -1
View File
@@ -39,7 +39,11 @@ type runner struct {
// 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
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
}
@@ -551,6 +555,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()
+107
View File
@@ -315,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")
}
}
+8
View File
@@ -36,6 +36,11 @@ type Options struct {
// 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
@@ -61,6 +66,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
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()