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
+71 -25
View File
@@ -9,6 +9,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -108,54 +109,59 @@ func TestArtifactsScanMissingDirectoryIsEmpty(t *testing.T) {
func TestArtifactAccessUsesOnlyCurrentAndEarlierVisits(t *testing.T) {
root := t.TempDir()
r := &runner{artifactsRoot: root}
for _, directory := range []string{"7/1", "7/2", "7/3", "8/1"} {
for _, directory := range []string{"7/stages/01-a", "7/stages/02-b/private", "7/stages/03-c", "7/public", "8/stages/01-a"} {
if err := os.MkdirAll(filepath.Join(root, directory), 0700); err != nil {
t.Fatal(err)
}
}
access := r.artifactAccess(7, 2, []string{"1", "3", "../8/1"}, filepath.Join(root, "7", "2"))
current, _ := filepath.EvalSymlinks(filepath.Join(root, "7", "2"))
prior, _ := filepath.EvalSymlinks(filepath.Join(root, "7", "1"))
if access.Write != current || len(access.Read) != 2 || access.Read[0] != access.Write || access.Read[1] != prior {
access := r.artifactAccess(7, 2, []string{"1", "3", "../8/1"}, filepath.Join(root, "7", "stages", "02-b"))
current, _ := filepath.EvalSymlinks(filepath.Join(root, "7", "stages", "02-b"))
private, _ := filepath.EvalSymlinks(filepath.Join(root, "7", "stages", "02-b", "private"))
public, _ := filepath.EvalSymlinks(filepath.Join(root, "7", "public"))
if access.Write != private || access.WriteShared != public || len(access.Read) != 2 || access.Read[0] != current || access.Read[1] != public {
t.Fatalf("artifact scope = %#v", access)
}
if got := r.artifactAccess(7, 2, nil, filepath.Join(root, "8", "1")); got.Write != "" || len(got.Read) != 0 {
if access.Read[1] == filepath.Join(root, "7", "stages", "01-a") {
t.Fatal("a prior turn's private directory was opened to this visit")
}
if got := r.artifactAccess(7, 2, nil, filepath.Join(root, "8", "stages", "01-a")); got.Write != "" || len(got.Read) != 0 {
t.Fatalf("other-run current scope accepted: %#v", got)
}
if got := r.artifactAccess(7, 2, nil, filepath.Join(root, "7", "3")); got.Write != "" || len(got.Read) != 0 {
if got := r.artifactAccess(7, 2, nil, filepath.Join(root, "7", "stages", "03-c")); got.Write != "" || len(got.Read) != 0 {
t.Fatalf("sibling current scope accepted: %#v", got)
}
if err := os.WriteFile(filepath.Join(root, "7", "4"), []byte("not a directory"), 0600); err != nil {
if err := os.WriteFile(filepath.Join(root, "7", "stages", "04-d"), []byte("not a directory"), 0600); err != nil {
t.Fatal(err)
}
if got := r.artifactAccess(7, 4, nil, filepath.Join(root, "7", "4")); got.Write != "" || len(got.Read) != 0 {
if got := r.artifactAccess(7, 4, nil, filepath.Join(root, "7", "stages", "04-d")); got.Write != "" || len(got.Read) != 0 {
t.Fatalf("file current scope accepted: %#v", got)
}
if err := os.Symlink(filepath.Join(root, "7", "2"), filepath.Join(root, "7", "5")); err != nil {
if err := os.Symlink(filepath.Join(root, "7", "stages", "02-b"), filepath.Join(root, "7", "stages", "05-e")); err != nil {
t.Fatal(err)
}
if got := r.artifactAccess(7, 5, nil, filepath.Join(root, "7", "5")); got.Write != "" || len(got.Read) != 0 {
if got := r.artifactAccess(7, 5, nil, filepath.Join(root, "7", "stages", "05-e")); got.Write != "" || len(got.Read) != 0 {
t.Fatalf("symlinked current scope accepted: %#v", got)
}
}
func TestRunnerPassesDerivedArtifactAccessToAgentClient(t *testing.T) {
root := t.TempDir()
for _, directory := range []string{"7/1", "7/2"} {
for _, directory := range []string{"7/stages/01-a/private", "7/stages/02-b/private", "7/public"} {
if err := os.MkdirAll(filepath.Join(root, directory), 0700); err != nil {
t.Fatal(err)
}
}
r := &runner{artifactsRoot: root}
client := r.agentClient(7, 2, []string{"1", "3"}, t.TempDir(), filepath.Join(root, "7", "2"), &stage.OperationLog{}, nil, []string{"read", "edit"})
if client.ArtifactAccess.Write == "" || len(client.ArtifactAccess.Read) != 2 {
current := filepath.Join(root, "7", "stages", "02-b")
client := r.agentClient(7, 2, []string{"1", "3"}, t.TempDir(), current, &stage.OperationLog{}, nil, []string{"read", "edit"})
if client.ArtifactAccess.Write == "" || client.ArtifactAccess.WriteShared == "" || len(client.ArtifactAccess.Read) != 2 {
t.Fatalf("runner did not pass scoped artifact access: %#v", client.ArtifactAccess)
}
if client.ArtifactAccess.Read[1] == filepath.Join(root, "7", "3") {
t.Fatal("runner passed a future visit to the client")
if client.ArtifactAccess.Read[1] == filepath.Join(root, "7", "stages", "01-a") {
t.Fatal("runner passed a prior turn's private directory to the client")
}
piClient := r.piClient(7, 2, []string{"1", "3"}, "pi", t.TempDir(), filepath.Join(root, "7", "2"), nil, []string{"read", "edit"})
if piClient.ArtifactAccess.Write != client.ArtifactAccess.Write || len(piClient.ArtifactAccess.Read) != len(client.ArtifactAccess.Read) || piClient.ArtifactAccess.Read[1] != client.ArtifactAccess.Read[1] {
piClient := r.piClient(7, 2, []string{"1", "3"}, "pi", t.TempDir(), current, nil, []string{"read", "edit"})
if !reflect.DeepEqual(piClient.ArtifactAccess, client.ArtifactAccess) {
t.Fatalf("runner passed different artifact scope to Pi client: %#v", piClient.ArtifactAccess)
}
}
@@ -183,11 +189,14 @@ func runWithArtifact(t *testing.T) (*runHandler, protocol.RunStep) {
t.Cleanup(func() { cancel(); WaitRuns(handler) })
handler.runner.artifactsRoot = t.TempDir()
handler.runner.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
if def.ArtifactsDir == "" {
t.Error("runner did not provide an artifacts directory")
if def.Turn.Private == "" || def.Turn.Public == "" {
t.Error("runner did not provide the turn artifact scopes")
return stage.Result{Outcome: "success"}, nil
}
if err := os.WriteFile(filepath.Join(def.ArtifactsDir, "report.txt"), []byte("artifact bytes"), 0600); err != nil {
if err := os.WriteFile(filepath.Join(def.Turn.Private, "report.txt"), []byte("artifact bytes"), 0600); err != nil {
return stage.Result{}, err
}
if err := os.WriteFile(filepath.Join(def.Turn.Public, "shared.txt"), []byte("shared bytes"), 0600); err != nil {
return stage.Result{}, err
}
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
@@ -206,14 +215,27 @@ func runWithArtifact(t *testing.T) (*runHandler, protocol.RunStep) {
t.Fatalf("unexpected run: %+v", detail)
}
step := detail.Steps[0]
if len(step.Artifacts) != 1 {
for _, name := range []string{"input.md", "memory.md", "output.md"} {
if _, ok := artifactNamed(step.Artifacts, name); !ok {
t.Fatalf("turn file %s not listed as an artifact: %+v", name, step.Artifacts)
}
}
artifact, ok := artifactNamed(step.Artifacts, "private/report.txt")
if !ok {
t.Fatalf("artifact not retained: %+v", step.Artifacts)
}
artifact := step.Artifacts[0]
if artifact.Name != "report.txt" || artifact.SizeBytes != int64(len("artifact bytes")) ||
shared, ok := artifactNamed(step.Artifacts, "public/shared.txt")
if !ok {
t.Fatalf("shared artifact not retained: %+v", step.Artifacts)
}
step.Artifacts = []protocol.StageArtifact{artifact, shared}
if artifact.SizeBytes != int64(len("artifact bytes")) ||
!strings.HasPrefix(artifact.ContentType, "text/plain") || artifact.URL == "" {
t.Fatalf("recorded artifact wrong: %+v", artifact)
}
if shared.SizeBytes != int64(len("shared bytes")) || shared.URL == "" {
t.Fatalf("recorded shared artifact wrong: %+v", shared)
}
return handler, step
}
@@ -292,6 +314,30 @@ func TestArtifactHTTPGetHeadRangeAndRefusals(t *testing.T) {
}
}
func TestArtifactHTTPGetPublicSharedFile(t *testing.T) {
handler, step := runWithArtifact(t)
var shared protocol.StageArtifact
for _, artifact := range step.Artifacts {
if artifact.Name == "public/shared.txt" {
shared = artifact
}
}
if shared.URL == "" {
t.Fatalf("public artifact missing: %+v", step.Artifacts)
}
srv := httptest.NewServer(handler)
defer srv.Close()
resp, err := http.Get(srv.URL + shared.URL)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK || string(body) != "shared bytes" {
t.Fatalf("public artifact GET = %d %q", resp.StatusCode, body)
}
}
func runIDFromURL(url string) int64 {
var id int64
fmt.Sscanf(url, "/artifacts/%d/", &id)
@@ -304,7 +350,7 @@ func TestArtifactHTTPRefusesEscapes(t *testing.T) {
root := handler.runner.artifactsRoot
// A symlink inside the step directory must never be followed.
stepDir := filepath.Join(root, fmt.Sprint(runID), "1")
stepDir := filepath.Join(root, fmt.Sprint(runID), "stages", "01-one")
secret := filepath.Join(t.TempDir(), "secret.txt")
if err := os.WriteFile(secret, []byte("secret"), 0600); err != nil {
t.Fatal(err)