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" "git.a-wan.net/alan/pipelines/internal/turn" ) // TestFeedbackLoopRevisitGetsReviewerHandoffInEssentialMemory runs a real // 4-stage pipeline with a review loop (plan, code, review -> changes -> code, // review, land) and checks what each visit's memory.md and history.md hold: // memory.md stays within its cap and leads the re-entered code stage with the // reviewer's handoff, while history.md keeps every earlier turn in full. func TestFeedbackLoopRevisitGetsReviewerHandoffInEssentialMemory(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "loop.db")) if err != nil { t.Fatal(err) } defer store.Close() source := strings.Replace(pipeline.Template([]string{"plan", "code", "review", "land"}, true), "success -> land\n", "success -> land\nchanges -> code\n", 1) if _, err := store.Submit(ctx, "loop", source); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil || len(items) != 1 { t.Fatalf("submit/list: %+v %v", items, err) } runner, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } filler := strings.Repeat("Long log line with commands, output and reasoning.\n", 200) type seen struct{ memory, history string } visits := map[string][]seen{} reviews := 0 runner.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) { memory, err := os.ReadFile(def.Turn.Memory) if err != nil { t.Fatal(err) } history, err := os.ReadFile(def.Turn.History) if err != nil { t.Fatal(err) } visits[def.Key] = append(visits[def.Key], seen{string(memory), string(history)}) body := "# handoff\n\n" + def.Key + " handoff\n\n# log\n\n" + def.Key + " full log\n" + filler outcome := "success" if def.Key == "review" { reviews++ if reviews == 1 { outcome = "changes" body = "# notes\n\n" + filler + "\n# handoff\n\nFINDING: lock leaks on the error path in cache.go.\n" } } return stage.Result{Outcome: outcome, Output: stage.Values{"markdown": body}}, nil } run, err := runner.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "loop", Workspace: t.TempDir(), Input: map[string]any{"prompt": "Add a cache."}}) if err != nil { t.Fatal(err) } runner.wg.Wait() detail, err := store.GetRun(ctx, run.ID) if err != nil { t.Fatal(err) } if detail.Status != "succeeded" || len(detail.Steps) != 6 { t.Fatalf("run = %q steps=%d: %+v", detail.Status, len(detail.Steps), detail.RunSummary) } for key, list := range visits { for i, visit := range list { if len(visit.memory) > turn.EssentialCap { t.Fatalf("%s visit %d memory.md is %d bytes, over the cap", key, i+1, len(visit.memory)) } } } if len(visits["code"]) != 2 { t.Fatalf("code ran %d times, want 2", len(visits["code"])) } revisit := visits["code"][1] for _, want := range []string{ "Add a cache.", "## Previous stage: review · visit 1 · outcome changes", "FINDING: lock leaks on the error path in cache.go.", "- code · visit 1 · outcome success (this stage's previous visit; detail in history.md)", } { if !strings.Contains(revisit.memory, want) { t.Fatalf("re-entered code memory.md missing %q:\n%s", want, revisit.memory) } } if strings.Contains(revisit.memory, "Long log line") { t.Fatalf("re-entered code memory.md carried a full log:\n%s", revisit.memory) } for _, want := range []string{"## plan · visit 1", "plan full log", "## code · visit 1", "code full log", "## review · visit 1", "FINDING: lock leaks"} { if !strings.Contains(revisit.history, want) { t.Fatalf("re-entered code history.md missing %q", want) } } land := visits["land"][0] if !strings.Contains(land.memory, "## Previous stage: review · visit 2 · outcome success") || !strings.Contains(land.memory, "review handoff") { t.Fatalf("land memory.md lacks the last review handoff:\n%s", land.memory) } if len(land.history) < 10*len(land.memory) { t.Fatalf("land history %d bytes vs memory %d bytes: essential memory is not compact", len(land.history), len(land.memory)) } }