Files
pipelines/internal/server/changed_paths_test.go
T
alan 98713e55f5 server: hand the committer the captured changed-path set
The committer staged only the implementer's self-declared changed_files,
so an incomplete declaration silently left part of the approved change
uncommitted. The runner already records the run's real changed paths by
comparing the working tree around each step; publish that set to the
committer.

- runState gains an unbounded, deduplicated, first-seen-ordered captured
  path accumulator (capturedPaths/capturedSeen), distinct from the bounded
  changedPaths sample used only by the UI-surface skip reason.
- recordChanges folds each captured FileChange path into it in the existing
  single pass, so no second walk and no new I/O.
- publishServerChangedFiles writes the set into the input memory value as
  input.server_changed_files, called before memoryInput so the binding
  always resolves (an empty list when nothing was captured) and only stages
  that select it receive it. rebuildRunState rebuilds it from persisted
  completed steps, so a resumed run hands the committer the same evidence.
- Tests prove the committer receives the captured set distinct from and
  additive to the implementer's declared changed_files, that an omitted
  path is still present, and that the field is an empty list when nothing
  was captured.
- control.md and orchestration.md document the reserved field and instruct
  the committer to select input.server_changed_files alongside the declared
  changed_files and stage their union.

Commands run: go build ./...; go vet ./...; go test -count=1
./internal/server/... ./internal/stage/...; go test -count=1 ./internal/author/...;
go test -count=1 -run TestCommitterReceivesServerCapturedChangedPaths ./internal/server/.
All passed. go.mod/go.sum unchanged.
2026-10-16 23:44:56 +01:00

148 lines
6.0 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"
)
// 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)
}
}