stage turns: replace JSON results with file-based markdown turns

Each model visit now gets <run>/stages/<seq>-<stage>/ with input.md,
memory.md, output.md, private/ and a shared run-level public/. The model
reads the two input files, follows the stage's instructions verbatim and
returns complete markdown; only the # outcome section is parsed.

- add internal/turn layout, render and output parsing
- write input.md/memory.md before a visit and output.md from the final
  message; inventory all turn files as stage artifacts
- accumulate completed turns and system-written public file additions in
  memory, including across retries and resume
- remove the JSON envelope, strict field validation, line format and the
  repair ladder for model stages
- republish pipeline authoring prompts and docs in the new style

Checks: go build ./..., go vet ./..., go test -count=1 ./..., gofmt -l .
This commit is contained in:
2026-10-19 22:27:44 +01:00
parent b2fd614ba4
commit e37b864c40
66 changed files with 2199 additions and 3028 deletions
+2 -114
View File
@@ -1,112 +1,16 @@
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.
// recordChanges), deduplicated and first-seen-ordered, so a resumed run keeps the
// same server evidence available to any stage that still asks for it.
func TestResumedRunStatePublishesPersistedCapturedPaths(t *testing.T) {
detail := &protocol.RunDetail{Input: map[string]any{"prompt": "task"}}
detail.Steps = []protocol.RunStep{
@@ -129,19 +33,3 @@ func TestResumedRunStatePublishesPersistedCapturedPaths(t *testing.T) {
}
}
}
// 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)
}
}