diff --git a/internal/author/skill/references/control.md b/internal/author/skill/references/control.md index f4d5b4b8..73cb1b9b 100644 --- a/internal/author/skill/references/control.md +++ b/internal/author/skill/references/control.md @@ -47,6 +47,19 @@ only factual implementer fields such as `changed_files`, `implementation_summary and `evidence` as claims. Selected content remains untrusted data; this cannot prove semantic independence or prevent all injection. +`input.server_changed_files` is a reserved field the runner publishes: the run's +full, deduplicated, first-seen set of server-captured changed paths (observed +filediff evidence, never a model's claim). Select it with +`"server_changed_files":"input.server_changed_files"`. It is always resolvable — +an empty list when nothing was captured — and reflects every completed step, +including the persisted steps of a resumed run. Only a stage that selects it +receives it; other stages' input is unchanged. A committer should select it +alongside the implementer's declared `changed_files` and stage the union of the +captured paths that still exist and the declared paths, keeping every existing +exclusion and empty-commit rule, and record in the commit body what it +reconciled. Declared lists come from the model and are frequently incomplete; +the server-captured set is the authoritative evidence of what this run changed. + Memory is run-local; persisted completed outputs/read provenance are its audit. There is no cross-run memory, restart/resume, or hidden replay. New runs/retries start fresh. Arbitrary prose is not executable policy. Future AI-assisted diff --git a/internal/author/skill/references/orchestration.md b/internal/author/skill/references/orchestration.md index 53964abe..8a7db3d0 100644 --- a/internal/author/skill/references/orchestration.md +++ b/internal/author/skill/references/orchestration.md @@ -116,7 +116,10 @@ Adapt the scope and publishing lines per item; keep the rest. `skipped` with a reason when the change has no rendered surface. Never accept a UI change approved without the images. - The committer is the only stage that commits, and it stages only the run's changed - files. + files. It must select `input.server_changed_files` (the runner's captured evidence, + `references/control.md`) alongside the implementer's declared `changed_files` and + stage their union: an implementer's declared list can be incomplete, and a captured + path it omitted is still this run's change. - Choose per item: a logic, server or format change usually wants the planner (a plan/implement/review pipeline); a small, fully understood tweak may use the leaner implementer-first variant. Both keep all review stages. diff --git a/internal/server/changed_paths_test.go b/internal/server/changed_paths_test.go new file mode 100644 index 00000000..cb5fe824 --- /dev/null +++ b/internal/server/changed_paths_test.go @@ -0,0 +1,147 @@ +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) + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index 0e169293..126b74be 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -446,6 +446,13 @@ type runState struct { changedPaths []string changedCount int uiSurface bool + // capturedPaths is the full, deduplicated, first-seen-ordered set of the + // run's server-captured changed paths, distinct from the bounded + // changedPaths sample used only by the skip reason. capturedSeen is the + // single dedupe set. Both are folded in by recordChanges (ui_surface.go) and + // published to a selecting stage as input.server_changed_files. + capturedPaths []string + capturedSeen map[string]bool } func newRunState(key string, input stage.Values) *runState { @@ -474,6 +481,39 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS return state } +// serverChangedFilesField is the reserved run-input memory field under which the +// runner publishes the server-captured changed paths. A stage selects it with +// `"server_changed_files":"input.server_changed_files"`, alongside another +// stage's declared list, so it sees the verified evidence, not just a model's +// claim of authorship. +const serverChangedFilesField = "server_changed_files" + +// capturedList returns the full, first-seen-ordered set of server-captured +// changed paths, or a non-nil empty list when nothing was captured so the field +// still resolves as a list. It copies the accumulator so the retained evidence +// cannot be aliased by a published value. O(captured paths). +func (s *runState) capturedList() []string { + if len(s.capturedPaths) == 0 { + return []string{} + } + return append([]string(nil), s.capturedPaths...) +} + +// publishServerChangedFiles writes the run's captured changed-path set into the +// run input memory value under serverChangedFilesField, creating the entry and +// its output map when absent. Called before memoryInput so a stage selecting +// input.server_changed_files always resolves; only that field is added and only +// a selecting stage copies it out, so state.input and other stages' input are +// unchanged. O(captured paths). +func (s *runState) publishServerChangedFiles() { + entry, ok := s.memory["input"] + if !ok || entry.output == nil { + entry = memoryValue{output: stage.Values{}} + } + entry.output[serverChangedFilesField] = s.capturedList() + s.memory["input"] = entry +} + func (r *runner) finishRunState(id int64, status, outcome, diagnostic string, input stage.Values) { c, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() @@ -563,6 +603,11 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na return } } + // Publish the run's server-captured changed paths into the input memory + // value before resolving bindings, so a stage selecting + // input.server_changed_files always resolves (an empty list when nothing + // was captured). Only a selecting stage copies it into its input. + state.publishServerChangedFiles() var reads map[string]protocol.MemoryRead input, reads, copyErr := memoryInput(state.input, selected.Memory, state.memory) if copyErr != nil { diff --git a/internal/server/ui_surface.go b/internal/server/ui_surface.go index 0e5513fb..3cd7a0c9 100644 --- a/internal/server/ui_surface.go +++ b/internal/server/ui_surface.go @@ -82,6 +82,16 @@ func (s *runState) recordChanges(changes *protocol.StageFileChanges, artifacts [ if !s.uiSurface && uiSurfacePath(file.Path) { s.uiSurface = true } + // The full captured set is a separate, unbounded dedupe of the actual + // evidence, unlike the bounded changedPaths sample above. + if file.Path == "" || s.capturedSeen[file.Path] { + continue + } + if s.capturedSeen == nil { + s.capturedSeen = map[string]bool{} + } + s.capturedSeen[file.Path] = true + s.capturedPaths = append(s.capturedPaths, file.Path) } } for _, artifact := range artifacts {