13c4035175
Later visits' input.md held only the previous stage's output, so the operator's task reached only the first stage; when a Prepare stage summarised (paths, HEAD), Code never saw the task or its acceptance checklist and either blocked (run #202) or did part of it (#195, #197, #198). Later inputs now carry the run prompt under '# Run task' (headings demoted) before '# Previous turn output'.
136 lines
3.6 KiB
Go
136 lines
3.6 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 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
|
|
}
|