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
+103 -54
View File
@@ -2,6 +2,7 @@ package server
import (
"errors"
"fmt"
"io"
"io/fs"
"log"
@@ -30,9 +31,12 @@ const (
// scanArtifacts returns a bounded inventory of one step's artifacts directory.
// The directory path is supplied by the caller because it was created next to
// the step; runID and sequence name the served URL. A missing, unreadable,
// symlinked or oversized directory yields no entries and a zero count.
func (r *runner) scanArtifacts(runID int64, sequence int, directory string) ([]protocol.StageArtifact, int, int) {
// the step; runID and sequence name the served URL. Optional public
// directories are inventoried with a "public/" name prefix so the shared
// artifacts are visible from the visit that produced them. A missing,
// unreadable, symlinked or oversized directory yields no entries and a zero
// count.
func (r *runner) scanArtifacts(runID int64, sequence int, directory string, publicDirs ...string) ([]protocol.StageArtifact, int, int) {
if directory == "" || r.artifactsRoot == "" {
return nil, 0, 0
}
@@ -42,64 +46,77 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string) ([]p
return nil, 0, 0
}
defer root.Close()
rel := path.Join(strconv.FormatInt(runID, 10), strconv.Itoa(sequence))
rel, err := artifactRel(root.Name(), directory)
if err != nil {
return nil, 0, 0
}
if !containedDirectory(root, rel) {
return nil, 0, 0
}
files := []protocol.StageArtifact{}
skipped, truncated := 0, 0
used := int64(0)
walkErr := fs.WalkDir(root.FS(), rel, func(name string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
// A single unreadable entry must not abort the whole inventory.
return nil
}
if name == rel || entry.IsDir() {
return nil
}
if entry.Type()&fs.ModeSymlink != 0 || !entry.Type().IsRegular() {
skipped++
return nil
}
info, infoErr := entry.Info()
if infoErr != nil || !info.Mode().IsRegular() {
skipped++
return nil
}
if len(files) >= maxArtifactFiles {
skipped++
return fs.SkipAll
}
if info.Size() > maxArtifactFileBytes {
skipped++
return nil
}
if used+info.Size() > maxArtifactTotalBytes {
truncated++
return fs.SkipAll
}
artifactName := strings.TrimPrefix(name, rel+"/")
full := path.Join(rel, artifactName)
contentType := mime.TypeByExtension(filepath.Ext(artifactName))
if contentType == "" {
contentType = sniffArtifact(root, full)
if contentType == "" {
scan := func(relative, prefix string) {
_ = fs.WalkDir(root.FS(), relative, func(name string, entry fs.DirEntry, walkErr error) error {
if walkErr != nil {
// A single unreadable entry must not abort the whole inventory.
return nil
}
if name == relative || entry.IsDir() {
return nil
}
if entry.Type()&fs.ModeSymlink != 0 || !entry.Type().IsRegular() {
skipped++
return nil
}
}
used += info.Size()
files = append(files, protocol.StageArtifact{
Name: artifactName,
SizeBytes: info.Size(),
ContentType: contentType,
URL: artifactURL(runID, sequence, artifactName),
info, infoErr := entry.Info()
if infoErr != nil || !info.Mode().IsRegular() {
skipped++
return nil
}
if len(files) >= maxArtifactFiles {
skipped++
return fs.SkipAll
}
if info.Size() > maxArtifactFileBytes {
skipped++
return nil
}
if used+info.Size() > maxArtifactTotalBytes {
truncated++
return fs.SkipAll
}
artifactName := strings.TrimPrefix(name, relative+"/")
artifactName = prefix + artifactName
full := path.Join(relative, strings.TrimPrefix(artifactName, prefix))
contentType := mime.TypeByExtension(filepath.Ext(artifactName))
if contentType == "" {
contentType = sniffArtifact(root, full)
if contentType == "" {
skipped++
return nil
}
}
used += info.Size()
files = append(files, protocol.StageArtifact{
Name: artifactName,
SizeBytes: info.Size(),
ContentType: contentType,
URL: artifactURL(runID, sequence, artifactName),
})
return nil
})
return nil
})
if walkErr != nil {
log.Printf("stage artifacts inventory run=%d step=%d: %v", runID, sequence, walkErr)
return nil, 0, 0
}
scan(rel, "")
for _, public := range publicDirs {
if public == "" {
continue
}
publicRel, relErr := artifactRel(root.Name(), public)
if relErr != nil || !containedDirectory(root, publicRel) {
continue
}
scan(publicRel, "public/")
}
if len(files) == 0 {
files = nil
@@ -107,6 +124,16 @@ func (r *runner) scanArtifacts(runID int64, sequence int, directory string) ([]p
return files, skipped, truncated
}
// artifactRel returns the slash path of directory below the artifacts root. A
// directory outside the root is refused.
func artifactRel(root, directory string) (string, error) {
relative, err := filepath.Rel(root, directory)
if err != nil || relative == "." || strings.HasPrefix(relative, ".."+string(os.PathSeparator)) || relative == ".." || filepath.IsAbs(relative) {
return "", fmt.Errorf("artifact directory is outside the artifacts root")
}
return filepath.ToSlash(relative), nil
}
// containedDirectory reports whether rel names a real directory below root with
// no symlink in any component.
func containedDirectory(root *os.Root, rel string) bool {
@@ -191,12 +218,34 @@ func (r *runner) serveArtifact(w http.ResponseWriter, req *http.Request) {
return
}
defer root.Close()
rel := path.Join(strconv.FormatInt(runID, 10), strconv.Itoa(sequence))
if !containedDirectory(root, rel) {
runRoot := strconv.FormatInt(runID, 10)
// The public directory is shared by every stage; a name carrying the
// "public/" prefix maps there. Everything else maps to the turn directory
// for this visit, with the pre-turn <run>/<step> layout still readable for
// runs recorded before the file-based protocol landed.
var rel string
if strings.HasPrefix(name, "public/") {
rel = path.Join(runRoot, "public", strings.TrimPrefix(name, "public/"))
} else {
stageKey := ""
for _, step := range detail.Steps {
if step.Sequence == sequence {
stageKey = step.Stage
break
}
}
turnDir := path.Join(runRoot, "stages", fmt.Sprintf("%02d-%s", sequence, stageKey))
if stageKey != "" && containedDirectory(root, turnDir) {
rel = path.Join(turnDir, name)
} else {
rel = path.Join(runRoot, strconv.Itoa(sequence), name)
}
}
if !containedDirectory(root, path.Dir(rel)) {
http.NotFound(w, req)
return
}
full := path.Join(rel, name)
full := rel
parts := strings.Split(full, "/")
for i := range parts {
info, lerr := root.Lstat(strings.Join(parts[:i+1], "/"))