Files
pipelines/internal/turn/render.go
T
alan 538143554c Split stage memory into capped essential memory.md and full history.md
Every stage used to be told to read memory.md, the full output of every
earlier turn (19 KB average, 92 KB max), and paid for it on each model call.
memory.md is now essential memory, hard-capped at 4 KB and inlined in the
prompt: the task, the previous stage's # handoff section (or its truncated
output head), a one-line-per-stage outcome ledger and the server-captured
changed files. history.md keeps the old full memory on disk; read-capable
stages are pointed at it and input.md for on-demand detail, and it is never
inlined. The prompt asks each stage for a short # handoff section.
2026-11-04 13:00:48 +00:00

140 lines
3.8 KiB
Go

package turn
import (
"fmt"
"sort"
"strings"
)
// RenderInput is a visit's input.md: the run prompt on the first visit, and on
// later visits the run prompt under "# Run task" followed by the previous
// visit's body under "# Previous turn output". Every stage keeps the operator's
// task: a stage that summarised its own work must not be the only carrier of
// what the run is for.
func RenderInput(prompt string, previousOutput string) string {
if strings.TrimSpace(previousOutput) == "" {
return prompt
}
if strings.TrimSpace(prompt) == "" {
return "# Previous turn output\n\n" + previousOutput
}
task := strings.TrimRight(prompt, "\n")
return "# Run task\n\n" + demoteHeadings(task) + "\n\n# Previous turn output\n\n" + previousOutput
}
// MemoryEntry is one completed turn in a later turn's history.md and essential
// memory.md. Output is the turn's markdown body, without the outcome section.
// Outcome is the routed outcome word, empty when it is not known (an inherited
// retry prefix). PublicFiles are paths relative to the run directory, so the
// public prefix is retained.
type MemoryEntry struct {
Stage string
Visit int
Outcome string
Output string
PublicFiles []string
}
// RenderHistory renders a visit's history.md (the optional full memory, which
// was memory.md before essential memory existed). It renders completed turns in the order supplied. Each turn's body
// is embedded with its own headings demoted two levels, so the turn's "## Stage ·
// visit N" stays the structural section and a body heading can never look like a
// sibling turn (or like the system-written file list). The file list is
// system-written from PublicFiles, sorted and deduplicated, so the result is
// deterministic.
func RenderHistory(entries []MemoryEntry) string {
var b strings.Builder
for i, entry := range entries {
if i > 0 {
b.WriteString("\n")
}
fmt.Fprintf(&b, "## %s · visit %d\n\n", entry.Stage, entry.Visit)
if entry.Output != "" {
b.WriteString(demoteHeadings(entry.Output))
if !strings.HasSuffix(entry.Output, "\n") {
b.WriteString("\n")
}
b.WriteString("\n")
}
b.WriteString("Files added to public/:\n")
files := sortedUnique(entry.PublicFiles)
if len(files) == 0 {
b.WriteString("- _(none)_\n")
continue
}
for _, file := range files {
b.WriteString("- ")
b.WriteString(file)
b.WriteString("\n")
}
}
return b.String()
}
// demoteHeadings adds two '#' to every ATX heading outside a fenced code block,
// capped at H6. Fences and their contents are left exactly as written.
func demoteHeadings(markdown string) string {
var b strings.Builder
var fence fenceState
for _, chunk := range strings.SplitAfter(markdown, "\n") {
text := strings.TrimSuffix(chunk, "\n")
if fence.open() {
if fence.closes(text) {
fence = fenceState{}
}
b.WriteString(chunk)
continue
}
if marker, ok := openingFence(text); ok {
fence = marker
b.WriteString(chunk)
continue
}
b.WriteString(demoteATX(chunk))
}
return b.String()
}
func demoteATX(chunk string) string {
text := strings.TrimSuffix(chunk, "\n")
i := 0
for i < len(text) && i < 3 && text[i] == ' ' {
i++
}
j := i
for j < len(text) && text[j] == '#' {
j++
}
level := j - i
if level < 1 || level > 6 {
return chunk
}
if j < len(text) && text[j] != ' ' && text[j] != '\t' && text[j] != '\r' {
return chunk
}
add := 2
if level+add > 6 {
add = 6 - level
}
if add == 0 {
return chunk
}
suffix := chunk[len(text):]
return text[:j] + strings.Repeat("#", add) + text[j:] + suffix
}
func sortedUnique(values []string) []string {
if len(values) == 0 {
return nil
}
sorted := append([]string(nil), values...)
sort.Strings(sorted)
unique := sorted[:0]
for _, value := range sorted {
if len(unique) == 0 || unique[len(unique)-1] != value {
unique = append(unique, value)
}
}
return unique
}