Files
alan e37b864c40 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 .
2026-10-19 22:27:44 +01:00

86 lines
2.5 KiB
Go

package protocol
import (
"encoding/json"
"sort"
"strings"
"unicode"
)
// NormalizeText reduces text to a single clean line. Zero-width formatting
// runes (the zero-width space, the joiners, the word joiner, the byte-order
// mark, the Mongolian vowel separator and the soft hyphen) are dropped, control
// characters become spaces, and every run of whitespace - including the
// non-breaking space variants - is collapsed, so the result has no leading,
// trailing or repeated spaces. It is the shared primitive behind OutputPreview
// and search snippets, so a retained preview and a snippet stay single-line and
// safe to render in a terminal.
func NormalizeText(text string) string {
text = strings.Map(func(r rune) rune {
if isZeroWidth(r) {
return -1
}
if unicode.IsControl(r) {
return ' '
}
return r
}, text)
// strings.Fields splits on unicode.IsSpace, which covers the non-breaking
// space variants, and single spaces join the surviving fields.
return strings.Join(strings.Fields(text), " ")
}
// isZeroWidth reports whether r is an invisible zero-width rune that must not
// survive into a one-line preview.
func isZeroWidth(r rune) bool {
switch r {
case '\u180e', '\u00ad', '\u200b', '\u200c', '\u200d', '\u2060', '\ufeff':
return true
}
return false
}
// OutputPreview quotes retained output, never infers accomplishments from a
// successful process or invents a schema for technology-neutral workflows.
func OutputPreview(output map[string]any) string {
keys := []string{"summary", "markdown", "result", "report", "message", "accomplished", "artifacts", "evidence", "reason", "error"}
seen := map[string]bool{}
remaining := make([]string, 0, len(output))
for key := range output {
remaining = append(remaining, key)
}
sort.Strings(remaining)
keys = append(keys, remaining...)
for _, key := range keys {
if seen[key] || key == "input" {
continue
}
seen[key] = true
value, exists := output[key]
if !exists || value == nil {
continue
}
text, ok := value.(string)
if !ok {
encoded, err := json.Marshal(value)
if err != nil {
continue
}
text = string(encoded)
}
text = NormalizeText(text)
if text == "" || text == "[]" || text == "{}" {
continue
}
if key != "summary" && key != "markdown" && key != "result" && key != "report" && key != "message" {
text = key + ": " + text
}
runes := []rune(text)
if len(runes) > 280 {
text = string(runes[:279]) + "…"
}
return text
}
return ""
}