package headless import ( "os" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // settled waits until the task is ready to merge, done, or needs its // person, and returns it. func (s *session) settled(t *testing.T, id string, timeout time.Duration) wire.Task { t.Helper() var k wire.Task s.wait(t, "ready to merge or needs you", timeout, func(c *thintest.Conversation) bool { k = c.Tasks[id] return k.Stage == wire.StageReadyToMerge || k.Stage == wire.StageDone || k.NeedsYou != "" || k.Stage == wire.StageCancelled }) if fresh, err := s.env.Client.Task(s.ctx, id); err == nil { k = fresh } return k } // oneSHA checks that the branch, the checks and the verdict all refer to // the task's newest candidate, and returns the judging steps. func oneSHA(t *testing.T, k wire.Task) (checks, review wire.Step) { t.Helper() var eng wire.Step for _, s := range k.Steps { switch { case s.Specialist == wire.SpecialistEngineering && s.Pushed: eng = s case s.Specialist == wire.SpecialistChecks && s.CandidateSHA == k.CandidateSHA: checks = s case s.Specialist == wire.SpecialistReview && s.CandidateSHA == k.CandidateSHA: review = s } } if k.CandidateSHA == "" || eng.CandidateSHA != k.CandidateSHA || checks.CandidateSHA != k.CandidateSHA || checks.State != wire.StepDone { 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) } if k.Stage == wire.StageReadyToMerge && (review.CandidateSHA != k.CandidateSHA || review.Verdict != wire.VerdictApprove || k.Verdict != wire.VerdictApprove) { t.Fatalf("ready to merge without an approval of %s: %+v", k.CandidateSHA, review) } return checks, review } func witnessTask(t *testing.T, k wire.Task, checks, review wire.Step, since time.Time) { steps := []map[string]any{} for _, s := range k.Steps { d := 0.0 if s.StartedAt != nil && s.FinishedAt != nil { d = s.FinishedAt.Sub(*s.StartedAt).Seconds() } 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}) } 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} } // Scenario 2, complete: Engineering pushes; the repository's own checks // fail on that SHA and an automatic fix round mends it from their // output; review asks for one more change and a second round makes it; // checks pass and review approves the newest SHA, whose branch, checks // and verdict agree, so the task is ready to merge, and Equipo merges that // SHA into main through the forge (a merge commit, the branch deleted) and // says so in one line. // // Live, the task is the one 3_engineering pushed in this run, judged with // no new message (or a new one from EQUIPO_STAGING_REVIEW_WITNESS): it // ends ready to merge or needing its person, on one SHA, and checks that // could not run say why. func scenarioReview(t *testing.T, env *Env) { witness := os.Getenv("EQUIPO_STAGING_REVIEW_WITNESS") if env.Live && witness == "" && pushedTask == "" { t.Skip("live: needs this run's 3_engineering task or EQUIPO_STAGING_REVIEW_WITNESS") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) 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" if env.Live { text = witness } start := time.Now() var sent wire.Sent var id string if env.Live && witness == "" { id = pushedTask } else { sent = s.send(t, text) id = s.taskOf(t, sent.Turn.ID) } k := s.settled(t, id, 40*time.Minute) checks, review := oneSHA(t, k) witnessTask(t, k, checks, review, start) if env.Live { 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) if k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" { t.Fatalf("task ended %s", k.Stage) } // Honest checks: what could not run says why, and is never a pass. if k.Checks == wire.ChecksCannotRun && (k.ChecksReason == "" || checks.Checks != wire.ChecksCannotRun) { t.Fatalf("checks that could not run: task %s (%q), step %+v", k.Checks, k.ChecksReason, checks) } return } // Approved with checks passed on the branch's head: Equipo merges it. s.wait(t, "merged", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) merged := time.Now() k, _ = env.Client.Task(s.ctx, id) checks, _ = oneSHA(t, k) 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 != "" { t.Fatalf("not merged %+v", k) } var kinds []string for _, st := range k.Steps { kinds = append(kinds, st.Specialist+":"+firstOf(st.Checks, st.Verdict, st.State)) } 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) { t.Fatalf("steps %v, want %v", kinds, want) } var stages []string s.conv.Locked(func(c *thintest.Conversation) { stages = c.Stages[id] }) for _, want := range []string{wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageMerging, wire.StageDone} { if !slices.Contains(stages, want) { t.Fatalf("stages %v lack %s", stages, want) } } bare := env.ForgePath("amadan.net/oficina/sample") mergedOnForge(t, bare, k) readme := forgeShow(t, bare, "main:README.md") if !strings.Contains(readme, "the panes") || !strings.Contains(readme, "keeps your place, always") { t.Fatalf("README on main: %s", readme) } // The checks ran the repository's own gate on that SHA, with a .git at it. log, err := env.Client.StepLog(s.ctx, checks.ID) 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") { t.Fatalf("checks %+v log %+v %v", checks, log, err) } failed := k.Steps[1] if flog, _ := env.Client.StepLog(s.ctx, failed.ID); failed.ExitCode == nil || *failed.ExitCode == 0 || !strings.Contains(flog.Tail, "still says teh panes") { t.Fatalf("failed checks %+v %+v", failed, flog) } if f := k.Steps[4].Findings; len(f) != 1 || f[0].Path != "README.md" || f[0].Line != 1 { t.Fatalf("review findings %+v", f) } // The coordinator says so at the outcome level, once. s.mergedLine(t, id, "Merged [Make edge swipe go back](/tasks/"+id+") into Sample.") Witness[t.Name()].(map[string]any)["merged"] = k.Merged Witness[t.Name()].(map[string]any)["merged_s"] = merged.Sub(start).Seconds() if needs, _ := env.Client.NeedsYou(s.ctx); len(needs.Items) != 0 { t.Fatalf("done still needs you: %+v", needs) } // Every model turn is measured: the coordinator's turn, each // Engineering round and each review, with its round and how many // model requests it made. 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"} if got := env.Usage(t, id, sent.Turn.ID); !slices.Equal(got, want) { t.Fatalf("usage rows\n got %v\nwant %v", got, want) } // Each fix round resumed Engineering's kept session on the same // connection (the fake says so, and read it from the cache); the // first round started fresh. var engs []wire.Step for _, st := range k.Steps { if st.Specialist == wire.SpecialistEngineering { engs = append(engs, st) } } if len(engs) != 3 || strings.Contains(engs[0].Summary, "(resumed session)") || engs[0].Account == "" { t.Fatalf("first round %+v", engs) } for _, st := range engs[1:] { if !strings.HasSuffix(st.Summary, "(resumed session)") || st.Account != engs[0].Account { t.Fatalf("fix round %d did not resume on %s: %q on %s", st.Round, engs[0].Account, st.Summary, st.Account) } } } func firstOf(v ...string) string { for _, s := range v { if s != "" { return s } } return "" } // Checks are honest about what this runner cannot check: a change to an // iOS file needs macOS, and with no Mac runner it is not checked on Linux // (never a pass, and since 2026-11-28 not judged needs_macos there // either). The task waits under Needs you for a Mac runner: no gate ran, // no review ran on an unchecked change, and nothing merged. func scenarioChecksHonest(t *testing.T, env *Env) { if env.Live { t.Skip("local only: the live Woodstar witness covers it") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) 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") id := s.taskOf(t, sent.Turn.ID) k := s.settled(t, id, 5*time.Minute) if k.NeedsYou != wire.NeedsMacRunner || k.Stage == wire.StageReadyToMerge || k.Checks != "" { t.Fatalf("not waiting for a Mac: %+v", k) } var checks, review wire.Step for _, st := range k.Steps { switch st.Specialist { case wire.SpecialistChecks: checks = st case wire.SpecialistReview: review = st } } if checks.State != wire.StepQueued || checks.Placement != wire.PlacementMac || checks.RunnerID != "" || checks.Gate != "" || checks.ExitCode != nil || checks.CandidateSHA != k.CandidateSHA { t.Fatalf("checks %+v", checks) } if review.ID != "" { t.Fatalf("reviewed before any checks: %+v", review) } if k.Merged != nil || forgeHead(t, env.ForgePath("amadan.net/oficina/sample"), k.Branch) != k.CandidateSHA { t.Fatalf("merged without checks: %+v", k) } if _, err := env.Client.CancelTask(s.ctx, id); err != nil { t.Fatal(err) } } // The live witness: tasks already pushed (EQUIPO_STAGING_REVIEW_TASKS) // are judged after the upgrade, with no new message: review runs on the // exact pushed SHA and checks report honestly; each ends ready to merge // or needing its person, all on one SHA. func scenarioReviewWitness(t *testing.T, env *Env) { ids := os.Getenv("EQUIPO_STAGING_REVIEW_TASKS") if !env.Live || ids == "" { t.Skip("staging only, with EQUIPO_STAGING_REVIEW_TASKS") } s := open(t, env) s.keep() start := time.Now() for _, id := range strings.Split(ids, ",") { k := s.settled(t, id, 40*time.Minute) checks, review := oneSHA(t, k) witnessTask(t, k, checks, review, start) Witness[t.Name()+"/"+id] = Witness[t.Name()] 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) if k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" { t.Fatalf("task %s ended %s", id, k.Stage) } } }