Files
pipelines/internal/server/retry_interrupted_test.go
T
alan ae4526d87c Retries: tell stages why the last attempt failed; resume interrupted runs
- turn.RenderPreviousAttempt: bounded "# Previous attempt" section (attempt
  number, failing stage, bounded diagnostic, partial-edits note).
- In-stage retries (stage/model_run.go) append it instead of resending an
  identical prompt; run retries (resume and restart) add it to the first
  model visit's input.md, numbering attempts along the retry chain.
- --input-file retry overrides render only the override keys in input.md,
  labelled "# Operator correction" (the step row keeps the merged input).
- Essential memory (memory.md) inlines a revisiting stage's own last handoff,
  bounded, besides the previous stage's handoff.
- Interrupted runs can be retried: resume reruns the interrupted visit, or
  starts the next stage after the last completed visit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:24:56 +00:00

112 lines
4.4 KiB
Go

package server
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
const threeStageModelSource = "# stage a\n\n```prompt\nDo A.\n```\n\n```tools\nread\n```\n\n```edges\nsuccess -> b\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n\n# stage b\n\n```prompt\nDo B.\n```\n\n```tools\nread\n```\n\n```edges\nsuccess -> c\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n\n# stage c\n\n```prompt\nDo C.\n```\n\n```tools\nread\n```\n\n```edges\nsuccess -> #success\nerror -> #failure\n```\n\n```configs\ntype=model\nprovider=opencode-go\nmodel=deepseek-v4.1-flash\nretries=0\n```\n"
// TestRetryResumesAnInterruptedRunAfterItsLastCompletedStep proves a run the
// server stopped (interrupted) can be retried: it resumes after its last
// completed step without rerunning that prefix, whether the stop came during a
// visit (the interrupted visit runs again) or between visits (the next stage
// starts with the completed visit's handoff).
func TestRetryResumesAnInterruptedRunAfterItsLastCompletedStep(t *testing.T) {
for _, midVisit := range []bool{true, false} {
name := "between-visits"
if midVisit {
name = "mid-visit"
}
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "interrupted.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "resume", threeStageModelSource); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
d, err := store.Get(ctx, items[0].ID, 1)
if err != nil {
t.Fatal(err)
}
workspace := t.TempDir()
id, _, err := store.CreateRun(ctx, d, "interrupted-"+name, workspace, true, nil, map[string]any{"prompt": "task"})
if err != nil {
t.Fatal(err)
}
if err := store.BeginStep(ctx, id, 1, "a", map[string]any{"prompt": "task"}); err != nil {
t.Fatal(err)
}
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "a", Status: "completed", Outcome: "success", Target: "b", Output: map[string]any{"handoff": "from-a"}}); err != nil {
t.Fatal(err)
}
if midVisit {
if err := store.BeginStep(ctx, id, 2, "b", map[string]any{"handoff": "from-a"}); err != nil {
t.Fatal(err)
}
}
if err := store.RecoverRuns(ctx); err != nil {
t.Fatal(err)
}
source, err := store.GetRun(ctx, id)
if err != nil || source.Status != "interrupted" {
t.Fatalf("source run was not interrupted: %+v %v", source, err)
}
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
calls := map[string]int{}
r.execute = func(_ context.Context, def stage.Definition, input stage.Values) (stage.Result, error) {
calls[def.Key]++
switch def.Key {
case "b":
if input["handoff"] != "from-a" {
t.Errorf("resumed stage lost the completed handoff: %#v", input)
}
data, err := os.ReadFile(def.Turn.Input)
if err != nil {
t.Fatalf("read input.md: %v", err)
}
if !strings.Contains(string(data), "# Previous attempt") || !strings.Contains(string(data), "This is attempt 2.") {
t.Errorf("resumed stage was not told about the previous attempt:\n%s", data)
}
return stage.Result{Outcome: "success", Output: stage.Values{"handoff": "from-b"}}, nil
case "c":
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
}
t.Errorf("unexpected stage %q", def.Key)
return stage.Result{}, nil
}
retried, err := r.retry(protocol.Message{RunID: id, RequestKey: "resume-" + name})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
retried, err = store.GetRun(ctx, retried.ID)
if err != nil || retried.Status != "succeeded" || retried.RetrySource != id {
t.Fatalf("interrupted run did not resume to success: %+v %v", retried, err)
}
if calls["a"] != 0 || calls["b"] != 1 || calls["c"] != 1 {
t.Fatalf("resume reran the completed prefix or skipped work: %v", calls)
}
if len(retried.Steps) == 0 || retried.Steps[0].Stage != "b" || retried.Steps[0].Sequence != 2 {
t.Fatalf("resume started at the wrong step: %+v", retried.Steps)
}
})
}
}