| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Engineering is told its time limit, and to run targeted tests rather |
| 15 | // than the repository's full gate (the Checks step runs that): the |
| 16 | // stopped-jobs audit's example 2 spent 17 of its 20 minutes in make ci. |
| 17 | func TestStepPromptStatesLimit(t *testing.T) { |
| 18 | for _, c := range []struct { |
| 19 | max time.Duration |
| 20 | says string |
| 21 | }{{0, "20 minutes"}, {35 * time.Minute, "35 minutes"}} { |
| 22 | e := &Engineer{MaxStep: c.max} |
| 23 | for name, prompt := range map[string]string{"step": e.stepPrompt(stepRow{}, false, claude.LinuxWorkspaceRoot), "fix": e.fixPrompt(stepRow{}, claude.LinuxWorkspaceRoot), "rebase": e.rebasePrompt(stepRow{}, nil, claude.LinuxWorkspaceRoot)} { |
| 24 | if !strings.Contains(prompt, c.says) { |
| 25 | t.Errorf("%s prompt at %v does not state %q:\n%s", name, c.max, c.says, prompt) |
| 26 | } |
| 27 | if !strings.Contains(prompt, "Run targeted tests; the Checks step runs the full gate") || strings.Contains(prompt, "documented gates") { |
| 28 | t.Errorf("%s prompt still asks for the full gate:\n%s", name, prompt) |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // A step that runs out of time after changing the tree keeps its work: |
| 35 | // Equipo commits and pushes it to the task branch as for a finished step, |
| 36 | // and reports it done with the reason step_timeout, so the instance runs |
| 37 | // checks on it and knows it is unfinished. One that changed nothing |
| 38 | // fails step_timeout as before. |
| 39 | func TestTimeoutKeepsChanges(t *testing.T) { |
| 40 | r := newPiRig(t, fakePiInstall(t), bigBudget) |
| 41 | r.urgent() |
| 42 | r.e.MaxStep = 3 * time.Second |
| 43 | r.e.Spoken = func() int { return wire.ProtocolUnfinishedWork } |
| 44 | remote := r.e.Forge.(*hubForge).dir |
| 45 | // The rig's remote names a task branch at HEAD; a hub names main. |
| 46 | gitOut(t, remote, "--git-dir="+remote, "symbolic-ref", "HEAD", "refs/heads/main") |
| 47 | rep := r.engineering("tk1", "e1", "edit:README.md|teh|the sleep:60000") |
| 48 | if rep.State != wire.StepDone || rep.Reason != "step_timeout" || !rep.Pushed || rep.CandidateSHA == "" || !rep.Termination { |
| 49 | t.Fatalf("timed out with changes: %+v", rep) |
| 50 | } |
| 51 | if got := gitOut(t, remote, "--git-dir="+remote, "rev-parse", "refs/heads/equipo/tk1-fix"); got != rep.CandidateSHA { |
| 52 | t.Fatalf("branch at %q, want the reported candidate %q", got, rep.CandidateSHA) |
| 53 | } |
| 54 | if msg := gitOut(t, remote, "--git-dir="+remote, "log", "-1", "--format=%B", rep.CandidateSHA); !strings.Contains(msg, "ran out of time") { |
| 55 | t.Fatalf("commit not marked unfinished: %q", msg) |
| 56 | } |
| 57 | // Its own rig: a metered step that timed out leaves its spend unknown, |
| 58 | // which holds the connection's next step. |
| 59 | r = newPiRig(t, fakePiInstall(t), bigBudget) |
| 60 | r.urgent() |
| 61 | r.e.MaxStep = 3 * time.Second |
| 62 | r.e.Spoken = func() int { return wire.ProtocolUnfinishedWork } |
| 63 | rep = r.engineering("tk2", "e2", "nothing to do sleep:60000") |
| 64 | if rep.State != wire.StepFailed || rep.Reason != "step_timeout" || rep.Pushed || rep.CandidateSHA != "" { |
| 65 | t.Fatalf("timed out with nothing: %+v", rep) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // An instance that does not know unfinished work (below |
| 70 | // ProtocolUnfinishedWork), or none connected, would take the pushed work |
| 71 | // for finished: the step fails step_timeout as before and pushes nothing. |
| 72 | func TestTimeoutKeepsNothingForAnOlderInstance(t *testing.T) { |
| 73 | for _, spoken := range []int{0, wire.ProtocolStuckTasks, wire.ProtocolChangeFiles} { |
| 74 | r := newPiRig(t, fakePiInstall(t), bigBudget) |
| 75 | r.urgent() |
| 76 | r.e.MaxStep = 3 * time.Second |
| 77 | r.e.Spoken = func() int { return spoken } |
| 78 | remote := r.e.Forge.(*hubForge).dir |
| 79 | gitOut(t, remote, "--git-dir="+remote, "symbolic-ref", "HEAD", "refs/heads/main") |
| 80 | rep := r.engineering("tk1", "e1", "edit:README.md|teh|the sleep:60000") |
| 81 | if rep.State != wire.StepFailed || rep.Reason != "step_timeout" || rep.Pushed || rep.CandidateSHA != "" { |
| 82 | t.Fatalf("protocol %d: %+v", spoken, rep) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // EQUIPO_RUNNER_MAX_STEP sets Engineering's step limit, a Go duration of |
| 88 | // 5 to 60 minutes (the stall sweep takes back a step running 90); any |
| 89 | // other value, or none, leaves the default 20. |
| 90 | func TestMaxStepFromEnv(t *testing.T) { |
| 91 | for value, want := range map[string]time.Duration{ |
| 92 | "": DefaultMaxStep, |
| 93 | "40m": 40 * time.Minute, |
| 94 | "5m": 5 * time.Minute, |
| 95 | "1h": time.Hour, |
| 96 | "4m59s": DefaultMaxStep, |
| 97 | "61m": DefaultMaxStep, |
| 98 | "-30m": DefaultMaxStep, |
| 99 | "forty": DefaultMaxStep, |
| 100 | "40": DefaultMaxStep, |
| 101 | } { |
| 102 | o := OptionsFromEnv(func(k string) string { |
| 103 | if k == "EQUIPO_RUNNER_MAX_STEP" { |
| 104 | return value |
| 105 | } |
| 106 | return "" |
| 107 | }) |
| 108 | if o.MaxStep != want { |
| 109 | t.Errorf("EQUIPO_RUNNER_MAX_STEP=%q: %v, want %v", value, o.MaxStep, want) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // A runner that stopped after committing a timed-out step's work, before |
| 115 | // the push, pushes it on recovery and still reports it unfinished. |
| 116 | func TestTimeoutKeptAcrossRestart(t *testing.T) { |
| 117 | e, f, r, _ := hubRig(t) |
| 118 | r.Specialist, r.StepID, r.Branch, r.Person = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix", "p@x" |
| 119 | base := gitOut(t, f.dir, "rev-parse", "main") |
| 120 | gitDir := e.tree(r, base).GitDir |
| 121 | gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) |
| 122 | tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") |
| 123 | r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "unfinished") |
| 124 | for phase, reason := range map[string]string{phaseCommittedUnfinished: "step_timeout", phaseCommittedNoShell: wire.ReasonShellUnavailable, phaseCommitted: ""} { |
| 125 | r.Phase = phase |
| 126 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 127 | if rep.State != wire.StepDone || !rep.Pushed || rep.Reason != reason || rep.CandidateSHA != r.Candidate { |
| 128 | t.Fatalf("%s: %+v", phase, rep) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // The person's Retry of a timed-out step whose push failed publishes the |
| 134 | // same commit as its attempt finished it: still unfinished (or still a |
| 135 | // round without a working shell). |
| 136 | func TestRepublishKeepsHowTheStepFinished(t *testing.T) { |
| 137 | for phase, reason := range map[string]string{phaseCommittedUnfinished: "step_timeout", phaseCommittedNoShell: wire.ReasonShellUnavailable, phaseCommitted: ""} { |
| 138 | e, f, r, _ := hubRig(t) |
| 139 | ctx := context.Background() |
| 140 | r.Specialist, r.StepID, r.Branch, r.Person = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix", "p@x" |
| 141 | base := gitOut(t, f.dir, "rev-parse", "main") |
| 142 | gitDir := e.tree(r, base).GitDir |
| 143 | gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) |
| 144 | tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") |
| 145 | cand := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "unfinished") |
| 146 | if err := e.O.State.AcceptDispatch(ctx, "d1", r.DispatchCommand); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | if _, err := e.O.State.db.Exec("UPDATE steps SET phase=?, state=?, final_state=?, final_reason='push_failed' WHERE step_id=? AND attempt=1", phase, runDone, wire.StepFailed, r.StepID); err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | again := r |
| 153 | again.Attempt, again.FromAttempt, again.Republish, again.BaseSHA, again.CandidateSHA = 2, 1, true, base, cand |
| 154 | if err := e.O.State.AcceptDispatch(ctx, "d2", again.DispatchCommand); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | rep := e.Exec(ctx, again, func(wire.StepReport) {}) |
| 158 | if rep.State != wire.StepDone || !rep.Pushed || rep.Reason != reason || rep.CandidateSHA != cand { |
| 159 | t.Fatalf("%q: %+v", phase, rep) |
| 160 | } |
| 161 | if got, _ := e.O.State.stepPhase(ctx, r.StepID, 2); got != phase { |
| 162 | t.Fatalf("%q: republish left phase %q", phase, got) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // A timed-out step's commit that could not be pushed stops step_timeout, |
| 168 | // not push_failed: it is tried afresh, never republished, so no runner |
| 169 | // (an older one included) can publish it as finished work. |
| 170 | func TestUnfinishedPushFailureIsATimeout(t *testing.T) { |
| 171 | rep := wire.StepReport{State: wire.StepFailed, Reason: "push_failed", LogTail: "git: refused"} |
| 172 | if got := withCommitReason(rep, phaseCommittedUnfinished); got.State != wire.StepFailed || got.Reason != "step_timeout" || got.LogTail != rep.LogTail { |
| 173 | t.Fatalf("unfinished: %+v", got) |
| 174 | } |
| 175 | for _, phase := range []string{phaseCommitted, phaseCommittedNoShell} { |
| 176 | if got := withCommitReason(rep, phase); got.Reason != "push_failed" { |
| 177 | t.Fatalf("%s: %+v", phase, got) |
| 178 | } |
| 179 | } |
| 180 | conflict := wire.StepReport{State: wire.StepFailed, Reason: "push_conflict"} |
| 181 | if got := withCommitReason(conflict, phaseCommittedUnfinished); got.Reason != "step_timeout" { |
| 182 | t.Fatalf("conflict: %+v", got) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Unfinished work is queued as the step failing step_timeout, which an |
| 187 | // older runner replaying the outbox (rolled back) and an older instance |
| 188 | // both read safely; the sender says it is done, pushed, to an instance |
| 189 | // at ProtocolUnfinishedWork only. A plain timeout stays a timeout. |
| 190 | func TestUnfinishedReportQueuedSafely(t *testing.T) { |
| 191 | e, _, r, _ := hubRig(t) |
| 192 | ctx := context.Background() |
| 193 | s := e.O.State |
| 194 | cand := strings.Repeat("2", 40) |
| 195 | r.Specialist, r.StepID = wire.SpecialistEngineering, "s9" |
| 196 | other := r |
| 197 | other.StepID, other.TaskID = "s10", "tk2" |
| 198 | r.StepID = "s9" |
| 199 | for _, d := range []stepRow{r, other} { |
| 200 | if err := s.AcceptDispatch(ctx, "d-"+d.StepID, d.DispatchCommand); err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | } |
| 204 | if err := s.setStepPhase(ctx, r, phaseCommittedUnfinished, "", cand); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | if err := s.ReportStep(ctx, r, wire.StepReport{TaskID: r.TaskID, StepID: "s9", Attempt: 1, State: wire.StepDone, Reason: "step_timeout", CandidateSHA: cand, Pushed: true, Termination: true, Findings: []wire.Finding{}}); err != nil { |
| 208 | t.Fatal(err) |
| 209 | } |
| 210 | if err := s.ReportStep(ctx, other, wire.StepReport{TaskID: other.TaskID, StepID: "s10", Attempt: 1, State: wire.StepFailed, Reason: "step_timeout", Termination: true, Findings: []wire.Finding{}}); err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | frames, _, err := s.Unacked(ctx, 0) |
| 214 | if err != nil || len(frames) != 2 { |
| 215 | t.Fatalf("queued %d: %v", len(frames), err) |
| 216 | } |
| 217 | read := func(f wire.Frame) *wire.StepReport { |
| 218 | body, err := f.Decode() |
| 219 | if err != nil { |
| 220 | t.Fatal(err) |
| 221 | } |
| 222 | return body.(*wire.StepReport) |
| 223 | } |
| 224 | for _, f := range frames { |
| 225 | if v := read(f); v.State != wire.StepFailed || v.Reason != "step_timeout" || v.Pushed || v.CandidateSHA != "" { |
| 226 | t.Fatalf("queued as %+v", v) |
| 227 | } |
| 228 | } |
| 229 | for _, c := range []struct { |
| 230 | protocol int |
| 231 | step string |
| 232 | done bool |
| 233 | }{{wire.ProtocolUnfinishedWork, "s9", true}, {wire.ProtocolChangeFiles, "s9", false}, {wire.ProtocolUnfinishedWork, "s10", false}} { |
| 234 | for _, f := range frames { |
| 235 | if read(f).StepID != c.step { |
| 236 | continue |
| 237 | } |
| 238 | sent, err := s.unfinishedReport(ctx, f, c.protocol, false) |
| 239 | if err != nil { |
| 240 | t.Fatal(err) |
| 241 | } |
| 242 | v := read(sent) |
| 243 | if done := v.State == wire.StepDone && v.Pushed && v.CandidateSHA == cand && v.Reason == "step_timeout"; done != c.done || !c.done && v.State != wire.StepFailed { |
| 244 | t.Fatalf("%s at %d: %+v", c.step, c.protocol, v) |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |