| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestPlanSegmentsIsOneLegByDefault(t *testing.T) { |
| 9 | task := task{ID: "x", Prompt: "fix it", MaxSteps: 12} |
| 10 | for _, count := range []int{0, 1} { |
| 11 | got := planSegments(task, count, nil) |
| 12 | if len(got) != 1 || got[0].resume || got[0].prompt != "fix it" || got[0].maxSteps != 12 { |
| 13 | t.Fatalf("count %d = %+v, want the unsegmented run", count, got) |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // The step budget is the task's contract; splitting must not quietly hand a |
| 19 | // segmented arm more work than the control arm gets. |
| 20 | func TestPlanSegmentsPreservesTheStepBudget(t *testing.T) { |
| 21 | for _, steps := range []int{12, 13, 20, 7} { |
| 22 | got := planSegments(task{Prompt: "p", MaxSteps: steps}, 3, nil) |
| 23 | total := 0 |
| 24 | for _, seg := range got { |
| 25 | if seg.maxSteps < 1 { |
| 26 | t.Fatalf("steps %d produced an empty leg: %+v", steps, got) |
| 27 | } |
| 28 | total += seg.maxSteps |
| 29 | } |
| 30 | if total != steps { |
| 31 | t.Fatalf("steps %d split into %d, want the budget preserved", steps, total) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestPlanSegmentsResumesEveryLegAfterTheFirst(t *testing.T) { |
| 37 | got := planSegments(task{Prompt: "fix it", MaxSteps: 9}, 3, nil) |
| 38 | if len(got) != 3 { |
| 39 | t.Fatalf("legs = %d, want 3", len(got)) |
| 40 | } |
| 41 | if got[0].resume || got[0].prompt != "fix it" { |
| 42 | t.Fatalf("leg 1 = %+v, want the task itself on a fresh session", got[0]) |
| 43 | } |
| 44 | for _, seg := range got[1:] { |
| 45 | if !seg.resume { |
| 46 | t.Fatalf("leg %d must resume: %+v", seg.index, seg) |
| 47 | } |
| 48 | if strings.Contains(seg.prompt, "fix it") { |
| 49 | t.Fatalf("leg %d restates the task, which hides the degradation being measured: %q", seg.index, seg.prompt) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestPlanSegmentsDeliversSteering(t *testing.T) { |
| 55 | steers, err := parseSteers("also handle empty input@2") |
| 56 | if err != nil { |
| 57 | t.Fatalf("parseSteers: %v", err) |
| 58 | } |
| 59 | got := planSegments(task{Prompt: "fix it", MaxSteps: 9}, 3, steers) |
| 60 | if got[1].prompt != "also handle empty input" { |
| 61 | t.Fatalf("leg 2 prompt = %q, want the steering message", got[1].prompt) |
| 62 | } |
| 63 | if !got[1].resume { |
| 64 | t.Fatalf("a steered leg still resumes: %+v", got[1]) |
| 65 | } |
| 66 | if got[2].prompt != continuationPrompt { |
| 67 | t.Fatalf("leg 3 = %q, want the plain continuation", got[2].prompt) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestParseSteersRejectsLegOneAndMalformedSpecs(t *testing.T) { |
| 72 | for _, bad := range []string{"msg@1", "msg@0", "msg", "@2", "msg@x"} { |
| 73 | if _, err := parseSteers(bad); err == nil { |
| 74 | t.Fatalf("%q must be rejected", bad) |
| 75 | } |
| 76 | } |
| 77 | if got, err := parseSteers(""); err != nil || got != nil { |
| 78 | t.Fatalf("empty spec = %v, %v", got, err) |
| 79 | } |
| 80 | } |
| 81 |