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.
This commit is contained in:
2026-10-19 23:35:18 +01:00
parent 0219173327
commit 00252da8f8
3 changed files with 76 additions and 10 deletions
+3 -1
View File
@@ -19,7 +19,9 @@ directories idempotently; it never deletes or rewrites existing files.
body (without the outcome section) under a `# Previous turn output` heading
afterwards. `memory.md` is plain
markdown: one `## <stage> · visit <n>` section per completed turn, with that
turn's body followed by a system-written list of files it added to `public/`.
turn's body — its own ATX headings demoted two levels, so the turn heading stays
the structural one — followed by a system-written list of files it added to
`public/`.
List entries are relative to the run directory, sorted, and unique.
The server writes `input.md` and `memory.md` before the model runs. The model
+59 -4
View File
@@ -26,9 +26,12 @@ type MemoryEntry struct {
PublicFiles []string
}
// RenderMemory renders completed turns in the order supplied. Each turn's output
// is embedded verbatim; the file list is system-written from PublicFiles and is
// sorted and deduplicated so the result is deterministic.
// 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 {
@@ -37,7 +40,7 @@ func RenderMemory(entries []MemoryEntry) string {
}
fmt.Fprintf(&b, "## %s · visit %d\n\n", entry.Stage, entry.Visit)
if entry.Output != "" {
b.WriteString(entry.Output)
b.WriteString(demoteHeadings(entry.Output))
if !strings.HasSuffix(entry.Output, "\n") {
b.WriteString("\n")
}
@@ -58,6 +61,58 @@ func RenderMemory(entries []MemoryEntry) string {
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
+14 -5
View File
@@ -42,7 +42,7 @@ func TestRenderMemory(t *testing.T) {
},
}
want := "## planner · visit 1\n\n" +
"# Plan\n\nDo the work.\n\n" +
"### Plan\n\nDo the work.\n\n" +
"Files added to public/:\n- public/a.md\n- public/b.md\n\n" +
"## reviewer · visit 2\n\n" +
"No public files.\n\n" +
@@ -55,16 +55,25 @@ func TestRenderMemory(t *testing.T) {
}
}
func TestRenderMemoryEmbedsOutputVerbatim(t *testing.T) {
output := "## Notes\n\n```md\n# outcome success\n```\n\nlast line without newline"
func TestRenderMemoryDemotesBodyHeadingsButNotFences(t *testing.T) {
output := "## Notes\n\n```md\n# outcome success\n```\n\n#### Deep\n\nlast line without newline"
entries := []MemoryEntry{{Stage: "build", Visit: 3, Output: output, PublicFiles: []string{"public/report.md"}}}
got := RenderMemory(entries)
if !strings.Contains(got, output) {
t.Fatalf("output was not embedded verbatim:\n%s", got)
if !strings.Contains(got, "#### Notes") {
t.Fatalf("body heading was not demoted:\n%s", got)
}
if !strings.Contains(got, "###### Deep") {
t.Fatalf("a deep heading was not capped at H6:\n%s", got)
}
if !strings.Contains(got, "```md\n# outcome success\n```") {
t.Fatalf("a fenced heading was changed:\n%s", got)
}
if !strings.Contains(got, "- public/report.md\n") {
t.Fatalf("public file list missing:\n%s", got)
}
if strings.Contains(got, "\n## Notes\n") {
t.Fatalf("a body heading still looks like a turn section:\n%s", got)
}
}
func TestRenderMemoryEmpty(t *testing.T) {