Files
pipelines/internal/findings/findings_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

144 lines
6.2 KiB
Go

package findings
import (
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const reviewOutput = "# outcome\n\nrejected\n\n# handoff\n\nTwo issues.\n\n# findings\n\n" +
"- important introduced internal/cache.go:42: eviction never runs\n" +
"- nit pre-existing -: odd naming in helpers\n" +
"- minor introduced: missing doc comment\n" +
"* blocker new `cmd/main.go`: panics on empty input\n" +
"- this line is prose and ignored\n" +
"\n```\n- critical introduced x.go:1: inside a fence\n```\n" +
"\n# details\n\n- critical introduced y.go:2: outside the section\n"
func TestParseNewFindingsAndIgnoresProse(t *testing.T) {
report := Parse(reviewOutput)
if len(report.New) != 4 || len(report.Changes) != 0 {
t.Fatalf("report = %+v", report)
}
want := []New{
{Severity: "important", Origin: "introduced", Location: "internal/cache.go:42", Summary: "eviction never runs"},
{Severity: "nit", Origin: "pre-existing", Location: "", Summary: "odd naming in helpers"},
{Severity: "minor", Origin: "introduced", Location: "", Summary: "missing doc comment"},
{Severity: "critical", Origin: "introduced", Location: "cmd/main.go", Summary: "panics on empty input"},
}
for i := range want {
if report.New[i] != want[i] {
t.Errorf("new[%d] = %+v, want %+v", i, report.New[i], want[i])
}
}
if got := Parse("# outcome\n\nsuccess\n"); len(got.New)+len(got.Changes) != 0 {
t.Fatalf("no section should parse empty: %+v", got)
}
}
func TestParseStatusChanges(t *testing.T) {
report := Parse("# findings\n\n- F1 fixed: added eviction\n- f2 dismissed — style only\n- F3: settled - agreed with operator\n- F4 open: fix incomplete\n- F5 maybe: unknown word\n")
want := []Change{{"F1", "fixed", "added eviction"}, {"F2", "dismissed", "style only"}, {"F3", "settled", "agreed with operator"}, {"F4", "open", "fix incomplete"}}
if len(report.Changes) != len(want) {
t.Fatalf("changes = %+v", report.Changes)
}
for i := range want {
if report.Changes[i] != want[i] {
t.Errorf("change[%d] = %+v, want %+v", i, report.Changes[i], want[i])
}
}
}
func TestApplyAssignsStableIDsAndEnforcesClosedItems(t *testing.T) {
ledger, out := Apply(nil, Parse(reviewOutput), "review", 2)
if strings.Join(out.Added, ",") != "F1,F2,F3,F4" || len(ledger) != 4 {
t.Fatalf("added %v ledger %d", out.Added, len(ledger))
}
ledger, out = Apply(ledger, Parse("# findings\n- F1 fixed: evicts now\n- F2 dismissed: out of scope\n- F9 fixed: nope\n"), "implement", 3)
if strings.Join(out.Changed, ",") != "F1,F2" || len(out.Ignored) != 1 {
t.Fatalf("outcome %+v", out)
}
// A reviewer re-raising the dismissed F2 is ignored; re-raising the fixed F1 reopens it;
// reopening the dismissed F2 by id is ignored; a genuinely new item gets F5.
ledger, out = Apply(ledger, Parse("# findings\n- nit pre-existing -: odd naming in helpers\n- important introduced internal/cache.go:42: eviction never runs\n- F2 open: still bad\n- minor introduced a.go:1: new thing\n"), "review", 4)
if strings.Join(out.Added, ",") != "F5" || strings.Join(out.Changed, ",") != "F1" || len(out.Ignored) != 2 {
t.Fatalf("outcome %+v", out)
}
byID := map[string]protocol.Finding{}
for _, item := range ledger {
byID[item.ID] = item
}
if byID["F1"].Status != "open" || byID["F2"].Status != "dismissed" || byID["F2"].Reason != "out of scope" || byID["F1"].UpdatedSequence != 4 {
t.Fatalf("ledger %+v", ledger)
}
// Before rolls the ledger back to the state a retry at sequence 4 starts from.
before := Before(ledger, 4)
if len(before) != 4 {
t.Fatalf("before = %d items", len(before))
}
for _, item := range before {
if item.ID == "F1" && (item.Status != "fixed" || item.UpdatedSequence != 3) {
t.Fatalf("F1 before 4 = %+v", item)
}
if item.ID == "F3" && (item.Status != "open" || item.UpdatedSequence != 0) {
t.Fatalf("F3 before 4 = %+v", item)
}
}
if got := Before(ledger, 2); len(got) != 0 {
t.Fatalf("before 2 = %+v", got)
}
// IDs continue after the highest existing one.
_, out = Apply(before, Parse("# findings\n- minor introduced b.go:1: another\n"), "review", 4)
if strings.Join(out.Added, ",") != "F5" {
t.Fatalf("added %v", out.Added)
}
}
func TestGateMatching(t *testing.T) {
ledger, _ := Apply(nil, Parse(reviewOutput), "review", 1)
if got := (Gate{MinSeverity: "important", Origin: "introduced"}).Matching(ledger); len(got) != 2 {
t.Fatalf("important introduced = %+v", got)
}
if got := (Gate{}).Matching(ledger); len(got) != 2 {
t.Fatalf("defaults = %+v", got)
}
if got := (Gate{MinSeverity: "nit", Origin: "any"}).Matching(ledger); len(got) != 4 {
t.Fatalf("nit any = %+v", got)
}
if got := (Gate{MinSeverity: "critical", Origin: "pre-existing"}).Matching(ledger); len(got) != 0 {
t.Fatalf("critical pre-existing = %+v", got)
}
ledger, _ = Apply(ledger, Parse("# findings\n- F1 fixed: done\n- F4 settled: accepted\n"), "implement", 2)
if got := (Gate{}).Matching(ledger); len(got) != 0 {
t.Fatalf("after fixes = %+v", got)
}
}
func TestRenderMemoryIsBoundedAndListsOpenFirst(t *testing.T) {
ledger, _ := Apply(nil, Parse(reviewOutput), "review", 1)
ledger, _ = Apply(ledger, Parse("# findings\n- F2 dismissed: not ours\n"), "implement", 2)
text := RenderMemory(ledger, 4096)
if !strings.Contains(text, "F4 · critical") || strings.Index(text, "F4 · critical") > strings.Index(text, "F1 · important") {
t.Fatalf("open findings not ordered by severity:\n%s", text)
}
if !strings.Contains(text, "Closed (do not re-raise):\n- F2 dismissed") || !strings.Contains(text, "never re-raise") {
t.Fatalf("closed list or instructions missing:\n%s", text)
}
var many []protocol.Finding
for i := 0; i < 150; i++ {
many, _ = Apply(many, Report{New: []New{{Severity: "minor", Origin: "introduced", Location: "f.go:" + strings.Repeat("1", i%7+1), Summary: strings.Repeat("x", 40) + string(rune('a'+i%26)) + strings.Repeat("y", i)}}}, "review", 1)
}
small := RenderMemory(many, 2048)
if len(small) > 2048 || !strings.Contains(small, "more in findings.md") {
t.Fatalf("bounded render is %d bytes:\n%s", len(small), small)
}
if RenderMemory(nil, 4096) != "" {
t.Fatal("empty ledger renders a section")
}
file := RenderFile(ledger)
if !strings.Contains(file, "## Dismissed") || !strings.Contains(file, "#2 implement → dismissed: not ours") {
t.Fatalf("file:\n%s", file)
}
}