Files
pipelines/internal/turn/essential_test.go
T
alan 34b8b94223 Engine: run findings ledger, findings gate, loop continuation, larger memory, loop lint, retry partial work, resume_session
- Runner-owned findings ledger per run: stages report a # findings section
  (new items and F<n> status changes); stable IDs, persisted (run_loop_state),
  in RunDetail.findings, findings.md, memory.md, inspect.
- type=findings gate stage routes open/clear on the ledger mechanically.
- retry --extra-visits N [--stage KEY] continues an exhausted loop; limits
  on_exhausted=decide parks the run on accept/continue/abort.
- Essential memory cap 20 KB, task never truncated, handoff and open findings
  ahead of the outcome ledger.
- Lint rejects limits.on outcomes on forward edges; warns on ineffective
  limits and resume_session.
- A resumed retry carries the failed visit's output.md and private/ under
  previous-attempt/.
- resume_session=true resumes a Claude stage's previous session on loop visits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:22:28 +00:00

200 lines
9.9 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", 5000), 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)
}
}
}
// TestEssentialMemoryNeverTruncatesTheTask proves a task larger than the
// budget is kept whole and the other sections share a bounded remainder, with
// the previous handoff and the findings ledger ahead of the outcome ledger.
func TestEssentialMemoryNeverTruncatesTheTask(t *testing.T) {
task := paragraph("task", 30000) + "THE END OF THE TASK"
entries := []MemoryEntry{
{Stage: "implement", Visit: 1, Outcome: "success", Output: "# handoff\n\n" + paragraph("handoff", 9000)},
{Stage: "review", Visit: 1, Outcome: "rejected", Output: "# handoff\n\nReview handoff: fix F1.\n"},
}
findings := func(limit int) string {
return "\n## Findings ledger (runner-owned)\n\n- F1 · important · introduced — leak\n"
}
memory := RenderEssential(Essential{Task: task, StageKey: "implement", Entries: entries, Findings: findings, InputPath: "/in.md", HistoryPath: "/history.md"})
if !strings.Contains(memory, "THE END OF THE TASK") || strings.Contains(memory[:strings.Index(memory, "THE END")], "_[truncated") {
t.Fatalf("task was truncated")
}
if len(memory) > len(task)+essentialMinRest+512 || len(memory) > EssentialMax {
t.Fatalf("memory %d bytes is not bounded by the task (%d) plus the minimum rest", len(memory), len(task))
}
handoff, ledger, own := strings.Index(memory, "Review handoff: fix F1."), strings.Index(memory, "## Findings ledger"), strings.Index(memory, "## This stage's previous visit")
if handoff < 0 || ledger < handoff {
t.Fatalf("handoff and findings missing or out of order:\n%s", memory[len(task)-200:])
}
if own >= 0 && own < ledger {
t.Fatalf("own handoff ahead of the findings ledger")
}
small := RenderEssential(Essential{Task: "Small task.", StageKey: "implement", Entries: entries, Findings: findings})
if len(small) > EssentialCap || !strings.Contains(small, "## Stage ledger") || !strings.Contains(small, "handoff: detail") {
t.Fatalf("small-task memory (%d bytes):\n%s", len(small), small)
}
}
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)
}
}