Files
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

168 lines
8.0 KiB
Go

package turn
import (
"fmt"
"strings"
"testing"
)
func paragraph(label string, bytes int) string {
line := label + ": detail about the change, the reasoning, the commands run and their output.\n"
return strings.Repeat(line, bytes/len(line)+1)
}
// feedbackLoopEntries is a 4-stage run with a revisit: plan, code, review sends
// the work back, and code is about to run its second visit.
func feedbackLoopEntries() []MemoryEntry {
return []MemoryEntry{
{Stage: "plan", Visit: 1, Outcome: "success", Output: "# handoff\n\nPlan: add the cache layer in two steps.\n\n# plan\n\n" + paragraph("plan", 5000)},
{Stage: "code", Visit: 1, Outcome: "success", Output: "# handoff\n\nImplemented cache.go; tests pass.\n\n# log\n\n" + paragraph("code", 9000), PublicFiles: []string{"public/coverage.html"}},
{Stage: "review", Visit: 1, Outcome: "changes_requested", Output: "# summary\n\nLong preamble.\n\n" + paragraph("review", 7000) + "\n# Handoff\n\n## Findings\n\n1. cache.go:42 leaks the lock on the error path.\n2. Missing test for eviction.\n\n# appendix\n\n" + paragraph("appendix", 3000)},
}
}
func TestEssentialMemoryRevisitShowsReviewerFindingsWithinCap(t *testing.T) {
entries := feedbackLoopEntries()
changed := []string{}
for i := range 20 {
changed = append(changed, fmt.Sprintf("internal/cache/file%02d.go", i))
}
memory := RenderEssential(Essential{
Task: "# Goal\n\nAdd a cache to the lookup service.",
StageKey: "code",
Entries: entries,
ChangedFiles: changed,
InputPath: "/run/stages/04-code/input.md",
HistoryPath: "/run/stages/04-code/history.md",
})
if len(memory) > EssentialCap {
t.Fatalf("essential memory is %d bytes, over the %d cap:\n%s", len(memory), EssentialCap, memory)
}
for _, want := range []string{
"### Goal",
"Add a cache to the lookup service.",
"## Previous stage: review · visit 1 · outcome changes_requested",
"cache.go:42 leaks the lock on the error path.",
"Missing test for eviction.",
"- plan · visit 1 · outcome success",
"- code · visit 1 · outcome success (this stage's previous visit; detail in history.md)",
"- review · visit 1 · outcome changes_requested",
"3 turns completed.",
"- internal/cache/file00.go",
"- … and 12 more",
"/run/stages/04-code/history.md",
"/run/stages/04-code/input.md",
} {
if !strings.Contains(memory, want) {
t.Fatalf("essential memory missing %q:\n%s", want, memory)
}
}
// The findings come before the ledger: they are the first thing a
// re-entered stage reads after the task.
if strings.Index(memory, "leaks the lock") > strings.Index(memory, "## Stage ledger") {
t.Fatalf("reviewer findings are not prominent:\n%s", memory)
}
for _, unwanted := range []string{"Long preamble.", "appendix:", "code: detail", "plan: detail"} {
if strings.Contains(memory, unwanted) {
t.Fatalf("essential memory carried non-handoff text %q:\n%s", unwanted, memory)
}
}
// The full, previous behaviour stays reachable through history.md.
history := RenderHistory(entries)
for _, want := range []string{"## plan · visit 1", "## code · visit 1", "## review · visit 1", "Long preamble.", "appendix: detail", "public/coverage.html"} {
if !strings.Contains(history, want) {
t.Fatalf("history missing %q", want)
}
}
if len(history) < 5*len(memory) {
t.Fatalf("history (%d) is not much larger than essential memory (%d)", len(history), len(memory))
}
}
func TestEssentialMemoryFallsBackToTruncatedOutputWithPointer(t *testing.T) {
entries := []MemoryEntry{{Stage: "code", Visit: 1, Outcome: "success", Output: "Started with the parser.\n" + paragraph("work", 20000)}}
memory := RenderEssential(Essential{Task: "Fix the parser.", StageKey: "review", Entries: entries, InputPath: "in.md", HistoryPath: "hist.md"})
if len(memory) > EssentialCap {
t.Fatalf("essential memory is %d bytes, over the cap", len(memory))
}
for _, want := range []string{"Fix the parser.", "no `# handoff` section", "Started with the parser.", "_[truncated:", "full text in input.md]_"} {
if !strings.Contains(memory, want) {
t.Fatalf("fallback memory missing %q:\n%s", want, memory)
}
}
}
func TestEssentialMemoryCapsEveryPart(t *testing.T) {
long := strings.Repeat("x", 300)
entries := []MemoryEntry{}
for i := range 30 {
entries = append(entries, MemoryEntry{Stage: fmt.Sprintf("stage-%02d-%s", i, long), Visit: 1, Outcome: long, Output: "# handoff\n\n" + paragraph("h", 9000), PublicFiles: []string{"public/" + long}})
}
files := []string{}
for i := range 200 {
files = append(files, fmt.Sprintf("%03d/%s", i, long))
}
memory := RenderEssential(Essential{Task: paragraph("task", 50000), StageKey: "next", Entries: entries, ChangedFiles: files, InputPath: "/in.md", HistoryPath: "/history.md"})
if len(memory) > EssentialCap {
t.Fatalf("essential memory is %d bytes, over the %d cap", len(memory), EssentialCap)
}
for _, want := range []string{"## Task", "task: detail", "## Previous stage:", "h: detail", "earlier stages in history.md", "and 192 more"} {
if !strings.Contains(memory, want) {
t.Fatalf("capped memory missing %q:\n%s", want, memory)
}
}
}
func TestEssentialMemoryFirstVisit(t *testing.T) {
memory := RenderEssential(Essential{Task: "Do it.", StageKey: "plan", InputPath: "in.md", HistoryPath: "hist.md"})
for _, want := range []string{"## Task\n\nDo it.", "first stage of the run"} {
if !strings.Contains(memory, want) {
t.Fatalf("first-visit memory missing %q:\n%s", want, memory)
}
}
}
func TestHandoffSection(t *testing.T) {
cases := []struct {
name, markdown, want string
ok bool
}{
{"absent", "# summary\n\ntext\n", "", false},
{"empty", "# handoff\n\n# next\n", "", false},
{"stops at same level", "# handoff\nkeep\n## sub\nalso keep\n# other\ndrop\n", "keep\n## sub\nalso keep", true},
{"lower level runs to end", "# a\n## Handoff ##\nkeep\n", "keep", true},
{"fenced heading ignored", "```\n# handoff\nno\n```\n# handoff\nyes\n", "yes", true},
{"crlf", "# handoff\r\nkeep\r\n# next\r\n", "keep", true},
}
for _, tc := range cases {
got, ok := HandoffSection(tc.markdown)
if ok != tc.ok || got != tc.want {
t.Fatalf("%s: HandoffSection = %q, %v; want %q, %v", tc.name, got, ok, tc.want, tc.ok)
}
}
}
// TestEssentialMemorySizeOnRealisticRun records what a late stage receives on
// a realistic run, before (memory.md was the full history and the stage was
// told to read it and input.md) and after (only the capped essential memory is
// inlined; input.md and history.md are on demand).
func TestEssentialMemorySizeOnRealisticRun(t *testing.T) {
task := "Add retry with backoff to the uploader, keep the public API, and add tests.\n" + paragraph("task", 1200)
entries := []MemoryEntry{
{Stage: "plan", Visit: 1, Outcome: "success", Output: "# handoff\n\nTwo-step plan; see Plan.\n\n# Plan\n\n" + paragraph("plan", 3500)},
{Stage: "code", Visit: 1, Outcome: "success", Output: "# handoff\n\nAdded backoff.go and tests.\n\n# Log\n\n" + paragraph("code", 6500)},
{Stage: "review", Visit: 1, Outcome: "changes_requested", Output: "# handoff\n\n1. jitter unbounded. 2. no cancel test.\n\n# Review\n\n" + paragraph("review", 4500)},
{Stage: "code", Visit: 2, Outcome: "success", Output: "# handoff\n\nFixed both findings.\n\n# Log\n\n" + paragraph("code2", 5000)},
{Stage: "review", Visit: 2, Outcome: "success", Output: "# handoff\n\nApproved; land it.\n\n# Review\n\n" + paragraph("review2", 3000)},
}
history := RenderHistory(entries)
input := RenderInput(task, entries[len(entries)-1].Output)
memory := RenderEssential(Essential{Task: task, StageKey: "land", Entries: entries, ChangedFiles: []string{"uploader/backoff.go", "uploader/backoff_test.go"}, InputPath: "input.md", HistoryPath: "history.md"})
before := len(history) + len(input)
t.Logf("late stage context: before %d bytes (memory.md %d + input.md %d), after %d bytes inlined (input.md %d and history.md %d on demand)", before, len(history), len(input), len(memory), len(input), len(history))
if len(memory) > EssentialCap || len(memory)*5 > before {
t.Fatalf("essential memory %d bytes is not a large reduction from %d", len(memory), before)
}
}