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" ) // committerCaptureSource builds an implementer -> committer pipeline whose // committer selects both the implementer's declared changed_files and the // runner-published input.server_changed_files. func committerCaptureSource() string { source := pipeline.Template([]string{"implementer", "committer"}, true) return strings.Replace(source, "# stage committer", "# stage committer\n\n```memory\n{\"changed_files\":\"implementer.changed_files\",\"server_changed_files\":\"input.server_changed_files\"}\n```", 1) } // runCommitterCapture runs committerCaptureSource once. seed writes the workspace // and returns the implementer's declared output; the returned map is exactly the // input the committer stage received. func runCommitterCapture(t *testing.T, key string, seed func(dir string) stage.Values) stage.Values { t.Helper() ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "changed-paths.db")) if err != nil { t.Fatal(err) } defer store.Close() if _, err := store.Submit(ctx, "commit", committerCaptureSource()); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil || len(items) != 1 { t.Fatalf("submit/list: %+v %v", items, err) } r, err := newRunner(ctx, store) if err != nil { t.Fatal(err) } dir := t.TempDir() var committerInput stage.Values r.execute = func(_ context.Context, def stage.Definition, input stage.Values) (stage.Result, error) { if def.Key == "implementer" { return stage.Result{Outcome: "success", Output: seed(dir)}, nil } committerInput = input return stage.Result{Outcome: "success", Output: stage.Values{"summary": "committed"}}, nil } run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: key, Workspace: dir, 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" || len(detail.Steps) != 2 { t.Fatalf("run = %q steps=%d, want succeeded/2: %+v", detail.Status, len(detail.Steps), detail.RunSummary) } if committerInput == nil { t.Fatal("committer stage did not run") } return committerInput } // TestCommitterReceivesServerCapturedChangedPaths proves the runner publishes the // server-captured changed-path set as input.server_changed_files, distinct from // and additive to the implementer's self-declared changed_files: a file the // implementer changed but did not declare is still handed to the committer. func TestCommitterReceivesServerCapturedChangedPaths(t *testing.T) { committerInput := runCommitterCapture(t, "captured", func(dir string) stage.Values { for name := range map[string]bool{"declared.txt": true, "undeclared.txt": true} { if err := os.WriteFile(filepath.Join(dir, name), []byte(name+"\n"), 0o600); err != nil { t.Fatal(err) } } return stage.Values{"changed_files": []any{"declared.txt"}} }) declared, ok := committerInput["changed_files"].([]any) if !ok || len(declared) != 1 || declared[0] != "declared.txt" { t.Fatalf("committer changed_files = %v, want the declared list", committerInput["changed_files"]) } captured, ok := committerInput["server_changed_files"].([]any) if !ok { t.Fatalf("committer did not receive server_changed_files: %v", committerInput) } seen := map[string]bool{} for _, path := range captured { seen[path.(string)] = true } // Additivity/distinctness: the captured set carries the path the declared // list omitted, plus the declared one, and nothing else. if !seen["undeclared.txt"] || !seen["declared.txt"] || len(seen) != 2 { t.Fatalf("server_changed_files = %v, want the two captured paths", captured) } } // TestResumedRunStatePublishesPersistedCapturedPaths proves the field is rebuilt // from every persisted completed step's captured paths (rebuildRunState → // recordChanges), deduplicated and first-seen-ordered, so a resumed run hands the // committer the same evidence as a live one. func TestResumedRunStatePublishesPersistedCapturedPaths(t *testing.T) { detail := &protocol.RunDetail{Input: map[string]any{"prompt": "task"}} detail.Steps = []protocol.RunStep{ {Sequence: 1, Stage: "implementer", Status: "completed", Output: stage.Values{"changed_files": []any{"a.go"}}, FileChanges: &protocol.StageFileChanges{Files: []protocol.FileChange{{Path: "a.go"}, {Path: "a.go"}, {Path: "b.go"}}}}, {Sequence: 2, Stage: "reviewer", Status: "completed", Output: stage.Values{}, FileChanges: &protocol.StageFileChanges{Files: []protocol.FileChange{{Path: "b.go"}, {Path: "c.go"}}}}, } state := rebuildRunState(detail, protocol.RunStep{Target: "committer", Output: stage.Values{}}) state.publishServerChangedFiles() got, ok := state.memory["input"].output[serverChangedFilesField].([]string) if !ok { t.Fatalf("resumed input memory has no %s list: %v", serverChangedFilesField, state.memory["input"].output) } want := []string{"a.go", "b.go", "c.go"} if len(got) != len(want) { t.Fatalf("captured paths = %v, want %v", got, want) } for i := range want { if got[i] != want[i] { t.Fatalf("captured paths = %v, want first-seen %v", got, want) } } } // TestCommitterServerChangedFilesEmptyWhenNothingCaptured proves the field is // present as an empty list when no changes were captured, so an unconditional // input.server_changed_files binding still resolves. func TestCommitterServerChangedFilesEmptyWhenNothingCaptured(t *testing.T) { committerInput := runCommitterCapture(t, "empty", func(string) stage.Values { return stage.Values{"changed_files": []any{}} }) captured, ok := committerInput["server_changed_files"].([]any) if !ok { t.Fatalf("server_changed_files not present: %v", committerInput) } if len(captured) != 0 { t.Fatalf("server_changed_files = %v, want an empty list", captured) } }