| 1 | package headless |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/thintest" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // settled waits until the task is ready to merge, done, or needs its |
| 15 | // person, and returns it. |
| 16 | func (s *session) settled(t *testing.T, id string, timeout time.Duration) wire.Task { |
| 17 | t.Helper() |
| 18 | var k wire.Task |
| 19 | s.wait(t, "ready to merge or needs you", timeout, func(c *thintest.Conversation) bool { |
| 20 | k = c.Tasks[id] |
| 21 | return k.Stage == wire.StageReadyToMerge || k.Stage == wire.StageDone || k.NeedsYou != "" || k.Stage == wire.StageCancelled |
| 22 | }) |
| 23 | if fresh, err := s.env.Client.Task(s.ctx, id); err == nil { |
| 24 | k = fresh |
| 25 | } |
| 26 | return k |
| 27 | } |
| 28 | |
| 29 | // oneSHA checks that the branch, the checks and the verdict all refer to |
| 30 | // the task's newest candidate, and returns the judging steps. |
| 31 | func oneSHA(t *testing.T, k wire.Task) (checks, review wire.Step) { |
| 32 | t.Helper() |
| 33 | var eng wire.Step |
| 34 | for _, s := range k.Steps { |
| 35 | switch { |
| 36 | case s.Specialist == wire.SpecialistEngineering && s.Pushed: |
| 37 | eng = s |
| 38 | case s.Specialist == wire.SpecialistChecks && s.CandidateSHA == k.CandidateSHA: |
| 39 | checks = s |
| 40 | case s.Specialist == wire.SpecialistReview && s.CandidateSHA == k.CandidateSHA: |
| 41 | review = s |
| 42 | } |
| 43 | } |
| 44 | if k.CandidateSHA == "" || eng.CandidateSHA != k.CandidateSHA || checks.CandidateSHA != k.CandidateSHA || checks.State != wire.StepDone { |
| 45 | t.Fatalf("checks and the branch do not refer to one SHA: task %s candidate %q, engineering %q, checks %+v", k.ID, k.CandidateSHA, eng.CandidateSHA, checks) |
| 46 | } |
| 47 | if k.Stage == wire.StageReadyToMerge && (review.CandidateSHA != k.CandidateSHA || review.Verdict != wire.VerdictApprove || k.Verdict != wire.VerdictApprove) { |
| 48 | t.Fatalf("ready to merge without an approval of %s: %+v", k.CandidateSHA, review) |
| 49 | } |
| 50 | return checks, review |
| 51 | } |
| 52 | |
| 53 | func witnessTask(t *testing.T, k wire.Task, checks, review wire.Step, since time.Time) { |
| 54 | steps := []map[string]any{} |
| 55 | for _, s := range k.Steps { |
| 56 | d := 0.0 |
| 57 | if s.StartedAt != nil && s.FinishedAt != nil { |
| 58 | d = s.FinishedAt.Sub(*s.StartedAt).Seconds() |
| 59 | } |
| 60 | steps = append(steps, map[string]any{"specialist": s.Specialist, "round": s.Round, "attempt": s.Attempt, "state": s.State, "reason": s.Reason, "candidate_sha": s.CandidateSHA, "verdict": s.Verdict, "review_label": s.ReviewLabel, "checks": s.Checks, "gate": s.Gate, "exit_code": s.ExitCode, "seconds": d, "summary": s.Summary, "findings": s.Findings}) |
| 61 | } |
| 62 | Witness[t.Name()] = map[string]any{"task": k.ID, "stage": k.Stage, "needs_you": k.NeedsYou, "branch": k.Branch, "candidate_sha": k.CandidateSHA, "checks": k.Checks, "checks_reason": k.ChecksReason, "verdict": k.Verdict, "review_label": k.ReviewLabel, "fix_rounds": k.FixRounds, "url": k.URL, "seconds": time.Since(since).Seconds(), "steps": steps} |
| 63 | } |
| 64 | |
| 65 | // Scenario 2, complete: Engineering pushes; the repository's own checks |
| 66 | // fail on that SHA and an automatic fix round mends it from their |
| 67 | // output; review asks for one more change and a second round makes it; |
| 68 | // checks pass and review approves the newest SHA, whose branch, checks |
| 69 | // and verdict agree, so the task is ready to merge, and Equipo merges that |
| 70 | // SHA into main through the forge (a merge commit, the branch deleted) and |
| 71 | // says so in one line. |
| 72 | // |
| 73 | // Live, the task is the one 3_engineering pushed in this run, judged with |
| 74 | // no new message (or a new one from EQUIPO_STAGING_REVIEW_WITNESS): it |
| 75 | // ends ready to merge or needing its person, on one SHA, and checks that |
| 76 | // could not run say why. |
| 77 | func scenarioReview(t *testing.T, env *Env) { |
| 78 | witness := os.Getenv("EQUIPO_STAGING_REVIEW_WITNESS") |
| 79 | if env.Live && witness == "" && pushedTask == "" { |
| 80 | t.Skip("live: needs this run's 3_engineering task or EQUIPO_STAGING_REVIEW_WITNESS") |
| 81 | } |
| 82 | s := open(t, env) |
| 83 | s.keep() |
| 84 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 85 | text := "tool:record_task:oficina/sample|Make edge swipe go back|Swiping from the edge does nothing.\ntool:start_task:$task|review:need:README.md|keeps your place, always|edit=README.md~keeps your place~keeps your place, always; edit:panes/swipe.go|return false|return true" |
| 86 | if env.Live { |
| 87 | text = witness |
| 88 | } |
| 89 | start := time.Now() |
| 90 | var sent wire.Sent |
| 91 | var id string |
| 92 | if env.Live && witness == "" { |
| 93 | id = pushedTask |
| 94 | } else { |
| 95 | sent = s.send(t, text) |
| 96 | id = s.taskOf(t, sent.Turn.ID) |
| 97 | } |
| 98 | k := s.settled(t, id, 40*time.Minute) |
| 99 | checks, review := oneSHA(t, k) |
| 100 | witnessTask(t, k, checks, review, start) |
| 101 | if env.Live { |
| 102 | t.Logf("task %s: %s needs_you=%q checks %s %s verdict %s (%s) on %s", id, k.Stage, k.NeedsYou, k.Checks, k.ChecksReason, k.Verdict, k.ReviewLabel, k.CandidateSHA) |
| 103 | if k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" { |
| 104 | t.Fatalf("task ended %s", k.Stage) |
| 105 | } |
| 106 | // Honest checks: what could not run says why, and is never a pass. |
| 107 | if k.Checks == wire.ChecksCannotRun && (k.ChecksReason == "" || checks.Checks != wire.ChecksCannotRun) { |
| 108 | t.Fatalf("checks that could not run: task %s (%q), step %+v", k.Checks, k.ChecksReason, checks) |
| 109 | } |
| 110 | return |
| 111 | } |
| 112 | // Approved with checks passed on the branch's head: Equipo merges it. |
| 113 | s.wait(t, "merged", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 114 | merged := time.Now() |
| 115 | k, _ = env.Client.Task(s.ctx, id) |
| 116 | checks, _ = oneSHA(t, k) |
| 117 | if k.Merged == nil || k.Merged.Into != "main" || k.Merged.Strategy != wire.MergeCommit || k.Merged.WithoutChecks || k.FixRounds != 2 || k.Checks != wire.ChecksPassed || k.Verdict != wire.VerdictApprove || k.ReviewLabel != wire.ReviewSameAccount || k.NeedsYou != "" { |
| 118 | t.Fatalf("not merged %+v", k) |
| 119 | } |
| 120 | var kinds []string |
| 121 | for _, st := range k.Steps { |
| 122 | kinds = append(kinds, st.Specialist+":"+firstOf(st.Checks, st.Verdict, st.State)) |
| 123 | } |
| 124 | if want := []string{"engineering:done", "checks:failed", "engineering:done", "checks:passed", "review:changes_requested", "engineering:done", "checks:passed", "review:approve", "merge:done"}; !slices.Equal(kinds, want) { |
| 125 | t.Fatalf("steps %v, want %v", kinds, want) |
| 126 | } |
| 127 | var stages []string |
| 128 | s.conv.Locked(func(c *thintest.Conversation) { stages = c.Stages[id] }) |
| 129 | for _, want := range []string{wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageMerging, wire.StageDone} { |
| 130 | if !slices.Contains(stages, want) { |
| 131 | t.Fatalf("stages %v lack %s", stages, want) |
| 132 | } |
| 133 | } |
| 134 | bare := env.ForgePath("amadan.net/oficina/sample") |
| 135 | mergedOnForge(t, bare, k) |
| 136 | readme := forgeShow(t, bare, "main:README.md") |
| 137 | if !strings.Contains(readme, "the panes") || !strings.Contains(readme, "keeps your place, always") { |
| 138 | t.Fatalf("README on main: %s", readme) |
| 139 | } |
| 140 | // The checks ran the repository's own gate on that SHA, with a .git at it. |
| 141 | log, err := env.Client.StepLog(s.ctx, checks.ID) |
| 142 | if err != nil || checks.Gate != "make ci" || checks.ExitCode == nil || *checks.ExitCode != 0 || !strings.Contains(log.Tail, k.CandidateSHA) || !strings.Contains(log.Tail, "README ok") { |
| 143 | t.Fatalf("checks %+v log %+v %v", checks, log, err) |
| 144 | } |
| 145 | failed := k.Steps[1] |
| 146 | if flog, _ := env.Client.StepLog(s.ctx, failed.ID); failed.ExitCode == nil || *failed.ExitCode == 0 || !strings.Contains(flog.Tail, "still says teh panes") { |
| 147 | t.Fatalf("failed checks %+v %+v", failed, flog) |
| 148 | } |
| 149 | if f := k.Steps[4].Findings; len(f) != 1 || f[0].Path != "README.md" || f[0].Line != 1 { |
| 150 | t.Fatalf("review findings %+v", f) |
| 151 | } |
| 152 | // The coordinator says so at the outcome level, once. |
| 153 | s.mergedLine(t, id, "Merged [Make edge swipe go back](/tasks/"+id+") into Sample.") |
| 154 | Witness[t.Name()].(map[string]any)["merged"] = k.Merged |
| 155 | Witness[t.Name()].(map[string]any)["merged_s"] = merged.Sub(start).Seconds() |
| 156 | if needs, _ := env.Client.NeedsYou(s.ctx); len(needs.Items) != 0 { |
| 157 | t.Fatalf("done still needs you: %+v", needs) |
| 158 | } |
| 159 | // Every model turn is measured: the coordinator's turn, each |
| 160 | // Engineering round and each review, with its round and how many |
| 161 | // model requests it made. |
| 162 | want := []string{"coordinator/0/turns=true/cached=false", "engineering/0/turns=true/cached=false", "engineering/1/turns=true/cached=true", "engineering/2/turns=true/cached=true", "review/1/turns=true/cached=false", "review/2/turns=true/cached=false"} |
| 163 | if got := env.Usage(t, id, sent.Turn.ID); !slices.Equal(got, want) { |
| 164 | t.Fatalf("usage rows\n got %v\nwant %v", got, want) |
| 165 | } |
| 166 | // Each fix round resumed Engineering's kept session on the same |
| 167 | // connection (the fake says so, and read it from the cache); the |
| 168 | // first round started fresh. |
| 169 | var engs []wire.Step |
| 170 | for _, st := range k.Steps { |
| 171 | if st.Specialist == wire.SpecialistEngineering { |
| 172 | engs = append(engs, st) |
| 173 | } |
| 174 | } |
| 175 | if len(engs) != 3 || strings.Contains(engs[0].Summary, "(resumed session)") || engs[0].Account == "" { |
| 176 | t.Fatalf("first round %+v", engs) |
| 177 | } |
| 178 | for _, st := range engs[1:] { |
| 179 | if !strings.HasSuffix(st.Summary, "(resumed session)") || st.Account != engs[0].Account { |
| 180 | t.Fatalf("fix round %d did not resume on %s: %q on %s", st.Round, engs[0].Account, st.Summary, st.Account) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func firstOf(v ...string) string { |
| 186 | for _, s := range v { |
| 187 | if s != "" { |
| 188 | return s |
| 189 | } |
| 190 | } |
| 191 | return "" |
| 192 | } |
| 193 | |
| 194 | // Checks are honest about what this runner cannot check: a change to an |
| 195 | // iOS file needs macOS, and with no Mac runner it is not checked on Linux |
| 196 | // (never a pass, and since 2026-11-28 not judged needs_macos there |
| 197 | // either). The task waits under Needs you for a Mac runner: no gate ran, |
| 198 | // no review ran on an unchecked change, and nothing merged. |
| 199 | func scenarioChecksHonest(t *testing.T, env *Env) { |
| 200 | if env.Live { |
| 201 | t.Skip("local only: the live Woodstar witness covers it") |
| 202 | } |
| 203 | s := open(t, env) |
| 204 | s.keep() |
| 205 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 206 | sent := s.send(t, "tool:record_task:oficina/sample|iOS edge swipe|Swipe back does nothing on iOS.\ntool:start_task:$task|edit:ios/App.swift|edgeSwipe = false|edgeSwipe = true") |
| 207 | id := s.taskOf(t, sent.Turn.ID) |
| 208 | k := s.settled(t, id, 5*time.Minute) |
| 209 | if k.NeedsYou != wire.NeedsMacRunner || k.Stage == wire.StageReadyToMerge || k.Checks != "" { |
| 210 | t.Fatalf("not waiting for a Mac: %+v", k) |
| 211 | } |
| 212 | var checks, review wire.Step |
| 213 | for _, st := range k.Steps { |
| 214 | switch st.Specialist { |
| 215 | case wire.SpecialistChecks: |
| 216 | checks = st |
| 217 | case wire.SpecialistReview: |
| 218 | review = st |
| 219 | } |
| 220 | } |
| 221 | if checks.State != wire.StepQueued || checks.Placement != wire.PlacementMac || checks.RunnerID != "" || checks.Gate != "" || checks.ExitCode != nil || checks.CandidateSHA != k.CandidateSHA { |
| 222 | t.Fatalf("checks %+v", checks) |
| 223 | } |
| 224 | if review.ID != "" { |
| 225 | t.Fatalf("reviewed before any checks: %+v", review) |
| 226 | } |
| 227 | if k.Merged != nil || forgeHead(t, env.ForgePath("amadan.net/oficina/sample"), k.Branch) != k.CandidateSHA { |
| 228 | t.Fatalf("merged without checks: %+v", k) |
| 229 | } |
| 230 | if _, err := env.Client.CancelTask(s.ctx, id); err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // The live witness: tasks already pushed (EQUIPO_STAGING_REVIEW_TASKS) |
| 236 | // are judged after the upgrade, with no new message: review runs on the |
| 237 | // exact pushed SHA and checks report honestly; each ends ready to merge |
| 238 | // or needing its person, all on one SHA. |
| 239 | func scenarioReviewWitness(t *testing.T, env *Env) { |
| 240 | ids := os.Getenv("EQUIPO_STAGING_REVIEW_TASKS") |
| 241 | if !env.Live || ids == "" { |
| 242 | t.Skip("staging only, with EQUIPO_STAGING_REVIEW_TASKS") |
| 243 | } |
| 244 | s := open(t, env) |
| 245 | s.keep() |
| 246 | start := time.Now() |
| 247 | for _, id := range strings.Split(ids, ",") { |
| 248 | k := s.settled(t, id, 40*time.Minute) |
| 249 | checks, review := oneSHA(t, k) |
| 250 | witnessTask(t, k, checks, review, start) |
| 251 | Witness[t.Name()+"/"+id] = Witness[t.Name()] |
| 252 | t.Logf("task %s: %s needs_you=%q checks %s %s verdict %s (%s) on %s", id, k.Stage, k.NeedsYou, k.Checks, k.ChecksReason, k.Verdict, k.ReviewLabel, k.CandidateSHA) |
| 253 | if k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" { |
| 254 | t.Fatalf("task %s ended %s", id, k.Stage) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |