Files
pipelines/internal/turn/render.go
T
alan 00252da8f8 turn: keep turn headings structural in memory
Turn bodies were embedded verbatim, so a body's own ## headings looked like
sibling turns and could even duplicate the system's "Files added to public/"
line. Body headings are now demoted two levels (capped at H6) outside fences;
fenced code is untouched.
2026-10-19 23:35:18 +01:00

130 lines
3.3 KiB
Go

package turn
import (
"fmt"
"sort"
"strings"
)
// RenderInput renders the markdown a turn receives. The first turn receives the
// run prompt as-is. Later turns receive the previous turn's output verbatim
// under a heading; prompt is not repeated.
func RenderInput(prompt string, previousOutput string) string {
if strings.TrimSpace(previousOutput) == "" {
return prompt
}
return "# Previous turn output\n\n" + previousOutput
}
// MemoryEntry is one completed turn in a later turn's memory file. Output is
// the turn's markdown body, without the outcome section. PublicFiles are paths
// relative to the run directory, so the public prefix is retained.
type MemoryEntry struct {
Stage string
Visit int
Output string
PublicFiles []string
}
// RenderMemory 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 RenderMemory(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
}