| 1 | package headless |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/thintest" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // taskOf waits for the task a turn recorded (its task card) and returns |
| 16 | // its id. |
| 17 | func (s *session) taskOf(t *testing.T, turn string) string { |
| 18 | t.Helper() |
| 19 | var id string |
| 20 | s.wait(t, "task card", 3*time.Minute, func(c *thintest.Conversation) bool { |
| 21 | for _, k := range cards(c, turn, wire.CardTask) { |
| 22 | if _, ok := c.Tasks[k.TaskID]; ok { |
| 23 | id = k.TaskID |
| 24 | return true |
| 25 | } |
| 26 | } |
| 27 | return wire.Terminal(c.Turns[turn].State) && c.Turns[turn].State != wire.TurnReplied |
| 28 | }) |
| 29 | if id == "" { |
| 30 | t.Fatalf("turn %s recorded no task", turn) |
| 31 | } |
| 32 | return id |
| 33 | } |
| 34 | |
| 35 | // step waits for the task's step to reach one of states and returns it. |
| 36 | func (s *session) step(t *testing.T, task string, timeout time.Duration, states ...string) wire.Step { |
| 37 | t.Helper() |
| 38 | var out wire.Step |
| 39 | s.wait(t, "step "+strings.Join(states, "|"), timeout, func(c *thintest.Conversation) bool { |
| 40 | // The newest Engineering step (checks and review follow it). |
| 41 | var last *wire.Step |
| 42 | for i := range c.Tasks[task].Steps { |
| 43 | if s := c.Tasks[task].Steps[i]; s.Specialist == wire.SpecialistEngineering { |
| 44 | last = &c.Tasks[task].Steps[i] |
| 45 | } |
| 46 | } |
| 47 | if last == nil { |
| 48 | return false |
| 49 | } |
| 50 | out = c.Steps[last.ID] |
| 51 | if out.ID == "" || out.Revision < last.Revision { |
| 52 | out = *last |
| 53 | } |
| 54 | return slices.Contains(states, out.State) |
| 55 | }) |
| 56 | return out |
| 57 | } |
| 58 | |
| 59 | // forgeHead reads a branch tip in the local forge. |
| 60 | func forgeHead(t *testing.T, bare, branch string) string { |
| 61 | out, err := exec.Command("git", "--git-dir="+bare, "rev-parse", "--verify", "-q", "refs/heads/"+branch).Output() |
| 62 | if err != nil { |
| 63 | return "" |
| 64 | } |
| 65 | return strings.TrimSpace(string(out)) |
| 66 | } |
| 67 | |
| 68 | func forgeShow(t *testing.T, bare string, args ...string) string { |
| 69 | t.Helper() |
| 70 | out, err := exec.Command("git", append([]string{"--git-dir=" + bare, "show"}, args...)...).Output() |
| 71 | if err != nil { |
| 72 | t.Fatalf("git show %v: %v", args, err) |
| 73 | } |
| 74 | return string(out) |
| 75 | } |
| 76 | |
| 77 | // pushedTask is the task a live 3_engineering pushed in this run, for the |
| 78 | // scenarios that judge and ask about it without a new message. |
| 79 | var pushedTask string |
| 80 | |
| 81 | // Scenario 2, to the branch: the person reports a bug; the task is noted |
| 82 | // at once, investigated with a finding, and Engineering's step commits in |
| 83 | // a task tree, records the candidate SHA and pushes the task's own branch |
| 84 | // through forge. Nothing merges. |
| 85 | func scenarioEngineering(t *testing.T, env *Env) { |
| 86 | s := open(t, env) |
| 87 | s.keep() |
| 88 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 89 | // Review is held (reviewsleep) so the branch and main are read before |
| 90 | // Equipo merges the task (slice 4c). |
| 91 | text := "think Fix the README typo in sample.\ntool:record_task:oficina/sample|Fix the README typo|The README says teh panes.\ntool:read_file:oficina/sample|README.md\ntool:add_finding:$task|The README has teh for the.|oficina/sample|README.md|5|Found it: a typo in the README.\ntool:start_task:$task|Fix the typo. reviewsleep:20000 edit:README.md|teh panes|the panes" |
| 92 | if env.Live { |
| 93 | text = env.Witness |
| 94 | } |
| 95 | sent := s.send(t, text) |
| 96 | start := time.Now() |
| 97 | id := s.taskOf(t, sent.Turn.ID) |
| 98 | noted := time.Since(start) |
| 99 | step := s.step(t, id, 25*time.Minute, wire.StepDone, wire.StepFailed, wire.StepCancelled) |
| 100 | // The task's own event (with its link) may follow the step's. |
| 101 | if step.Pushed { |
| 102 | s.wait(t, "the task's link", 30*time.Second, func(c *thintest.Conversation) bool { return c.Tasks[id].URL != "" }) |
| 103 | } |
| 104 | var task wire.Task |
| 105 | var stages []string |
| 106 | s.conv.Locked(func(c *thintest.Conversation) { task, stages = c.Tasks[id], c.Stages[id] }) |
| 107 | t.Logf("task %s noted after %s; stages %v; step %s %s %s pushed=%v after %s; branch %s", id, noted.Round(time.Millisecond), stages, step.State, step.Reason, step.CandidateSHA, step.Pushed, time.Since(start).Round(time.Second), task.Branch) |
| 108 | Witness[t.Name()] = map[string]any{"task": id, "stages": stages, "branch": task.Branch, "candidate_sha": step.CandidateSHA, "base_sha": step.BaseSHA, "pushed": step.Pushed, "state": step.State, "reason": step.Reason, "noted_ms": noted.Milliseconds(), "pushed_s": time.Since(start).Seconds(), "url": task.URL, "summary": step.Summary, "findings": task.Findings} |
| 109 | if step.State != wire.StepDone || !step.Pushed || !step.Terminated || len(step.CandidateSHA) != 40 { |
| 110 | t.Fatalf("step %+v", step) |
| 111 | } |
| 112 | // The client records the stages its events show. A live model can |
| 113 | // investigate before the first event arrives, so live runs may first |
| 114 | // see investigated; work must never be the first thing seen. |
| 115 | first := []string{wire.StageNoted} |
| 116 | if env.Live { |
| 117 | first = append(first, wire.StageInvestigated) |
| 118 | } |
| 119 | if !slices.Contains(first, stages[0]) || !slices.Contains(stages, wire.StageInProgress) { |
| 120 | t.Fatalf("stages %v", stages) |
| 121 | } |
| 122 | if !wire.ValidTaskBranch(task.Branch) || !strings.HasPrefix(task.Branch, "equipo/"+id) || task.URL == "" || len(task.Findings) == 0 { |
| 123 | t.Fatalf("task %+v", task) |
| 124 | } |
| 125 | noRepoQuestion(t, s, sent.Turn.ID) |
| 126 | if env.Live { |
| 127 | pushedTask = id |
| 128 | return |
| 129 | } |
| 130 | if !slices.Contains(stages, wire.StageInvestigated) || task.Findings[0].Path != "README.md" || task.Findings[0].Line != 5 { |
| 131 | t.Fatalf("finding %+v stages %v", task.Findings, stages) |
| 132 | } |
| 133 | bare := env.ForgePath("amadan.net/oficina/sample") |
| 134 | if got := forgeHead(t, bare, task.Branch); got != step.CandidateSHA { |
| 135 | t.Fatalf("forge branch at %q, want %s", got, step.CandidateSHA) |
| 136 | } |
| 137 | if forgeHead(t, bare, "main") != step.BaseSHA { |
| 138 | t.Fatal("main moved") |
| 139 | } |
| 140 | if parent := strings.TrimSpace(forgeShow(t, bare, "--no-patch", "--format=%P", step.CandidateSHA)); parent != step.BaseSHA { |
| 141 | t.Fatalf("parent %s, base %s", parent, step.BaseSHA) |
| 142 | } |
| 143 | if readme := forgeShow(t, bare, step.CandidateSHA+":README.md"); !strings.Contains(readme, "the panes") || strings.Contains(readme, "teh panes") { |
| 144 | t.Fatalf("README at the candidate: %s", readme) |
| 145 | } |
| 146 | log, err := env.Client.StepLog(s.ctx, step.ID) |
| 147 | if err != nil || !strings.Contains(log.Tail, "Edit README.md") || !strings.Contains(log.Tail, "Changed: README.md") { |
| 148 | t.Fatalf("log %+v %v", log, err) |
| 149 | } |
| 150 | // The headlines: the coordinator's on the finding, Engineering's |
| 151 | // split off its reply (and out of the summary). |
| 152 | if !env.Live && (task.Findings[0].Headline != "Found it: a typo in the README." || step.Headline != "Made the change the task asks for." || strings.Contains(step.Summary, "Headline:")) { |
| 153 | t.Fatalf("headlines: finding %q, step %q, summary %q", task.Findings[0].Headline, step.Headline, step.Summary) |
| 154 | } |
| 155 | if !strings.Contains(step.Summary, "Replaced") { |
| 156 | t.Fatalf("summary %q", step.Summary) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Scenario 4: the runner is killed mid-step. Retry is refused while the |
| 161 | // attempt's end is unknown; the restarted runner proves it (its |
| 162 | // namespaces died with it, verified at start) and reports the step |
| 163 | // failed with termination; Equipo then retries it on its own (needs-you |
| 164 | // triage), and attempt 2 runs to a pushed branch. |
| 165 | func scenarioStepCrash(t *testing.T, env *Env) { |
| 166 | s := open(t, env) |
| 167 | s.keep() |
| 168 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 169 | text := "tool:record_task:oficina/sample|Fix the README typo|teh\ntool:start_task:$task|Slowly. stepsleep:4000 edit:README.md|teh panes|the panes" |
| 170 | if env.Live { |
| 171 | if env.CrashWitness == "" { |
| 172 | t.Fatal("EQUIPO_STAGING_CRASH_WITNESS is not set") |
| 173 | } |
| 174 | text = env.CrashWitness |
| 175 | } |
| 176 | sent := s.send(t, text) |
| 177 | id := s.taskOf(t, sent.Turn.ID) |
| 178 | // Live, the step may queue behind the earlier tasks' checks and review. |
| 179 | running := s.step(t, id, 10*time.Minute, wire.StepRunning) |
| 180 | if !env.Live { |
| 181 | // What Engineering started shows while it runs: the fake reads the |
| 182 | // file it will edit before it sleeps. |
| 183 | s.wait(t, "step activity while running", time.Minute, func(c *thintest.Conversation) bool { |
| 184 | for _, a := range c.StepActivity[running.ID] { |
| 185 | if n := len(a.Activity); n > 0 && a.Activity[n-1].Kind == wire.ActivityRead && a.Activity[n-1].Subject == "README.md" { |
| 186 | return c.Steps[running.ID].State == wire.StepRunning |
| 187 | } |
| 188 | } |
| 189 | return false |
| 190 | }) |
| 191 | } |
| 192 | // Let the step's process start before the kill. |
| 193 | time.Sleep(1500 * time.Millisecond) |
| 194 | env.KillRunner(t) |
| 195 | if _, err := env.Client.RetryTask(s.ctx, id); thintest.Status(err) != 409 { |
| 196 | t.Fatalf("retry while the runner is down: %v", err) |
| 197 | } |
| 198 | env.StartRunner(t) |
| 199 | // The restarted runner proves the attempt ended (runner_restarted): a |
| 200 | // passing stop, so Equipo retries it once on its own (needs-you |
| 201 | // triage), with a receipt, and nothing waits on the person. |
| 202 | s.wait(t, "attempt 2", 2*time.Minute, func(c *thintest.Conversation) bool { return c.Steps[running.ID].Attempt == 2 }) |
| 203 | h, err := env.Client.Housekeeping(s.ctx) |
| 204 | if err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | retried := 0 |
| 208 | for _, a := range h.Actions { |
| 209 | if a.Detector == "triage_retry" && a.Subject.ID == id && a.State == wire.ActionDone { |
| 210 | retried++ |
| 211 | } |
| 212 | } |
| 213 | if retried != 1 { |
| 214 | t.Fatalf("automatic retries %+v", h.Actions) |
| 215 | } |
| 216 | done := s.step(t, id, 25*time.Minute, wire.StepDone, wire.StepFailed) |
| 217 | if done.State != wire.StepDone || done.Attempt != 2 || !done.Pushed { |
| 218 | t.Fatalf("attempt 2: %+v", done) |
| 219 | } |
| 220 | if !env.Live { |
| 221 | // The step keeps its last lines: attempt 2's read and edit. |
| 222 | k, err := env.Client.Task(s.ctx, id) |
| 223 | if err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | lines := k.Steps[0].Activity |
| 227 | if len(lines) != 2 || lines[0].Kind != wire.ActivityRead || lines[1].Kind != wire.ActivityEdit || lines[1].Subject != "README.md" { |
| 228 | t.Fatalf("kept activity %+v", lines) |
| 229 | } |
| 230 | if got := forgeHead(t, env.ForgePath("amadan.net/oficina/sample"), wire.TaskBranch(id, "Fix the README typo")); got != done.CandidateSHA { |
| 231 | t.Fatalf("branch at %q, want %s", got, done.CandidateSHA) |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Cancel during a step: the task ends only when the runner reports the |
| 237 | // attempt's termination; nothing is pushed. Live, the person asks for a |
| 238 | // real change (EQUIPO_STAGING_CANCEL_WITNESS) and cancels it a second |
| 239 | // after the real Engineering step starts, long before it could push. |
| 240 | func scenarioStepCancel(t *testing.T, env *Env) { |
| 241 | s := open(t, env) |
| 242 | s.keep() |
| 243 | text, running, ended := "tool:record_task:oficina/sample|Slow change|x\ntool:start_task:$task|stepsleep:20000 edit:README.md|teh|the", time.Minute, time.Minute |
| 244 | if env.Live { |
| 245 | if env.CancelWitness == "" { |
| 246 | t.Fatal("EQUIPO_STAGING_CANCEL_WITNESS is not set") |
| 247 | } |
| 248 | // The live step may queue behind the earlier task's checks and |
| 249 | // review, and ends only when the runner has stopped a real |
| 250 | // process tree. |
| 251 | text, running, ended = env.CancelWitness, 10*time.Minute, 3*time.Minute |
| 252 | } |
| 253 | sent := s.send(t, text) |
| 254 | id := s.taskOf(t, sent.Turn.ID) |
| 255 | s.step(t, id, running, wire.StepRunning) |
| 256 | time.Sleep(time.Second) |
| 257 | acc, err := env.Client.CancelTask(s.ctx, id) |
| 258 | if err != nil || acc.Attempt != 1 { |
| 259 | t.Fatalf("cancel: %+v %v", acc, err) |
| 260 | } |
| 261 | st := s.step(t, id, ended, wire.StepCancelled, wire.StepFailed, wire.StepDone) |
| 262 | if st.State != wire.StepCancelled || !st.Terminated || st.Pushed { |
| 263 | t.Fatalf("cancelled step %+v", st) |
| 264 | } |
| 265 | s.wait(t, "task cancelled", 30*time.Second, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageCancelled }) |
| 266 | if env.Live { |
| 267 | return |
| 268 | } |
| 269 | if forgeHead(t, env.ForgePath("amadan.net/oficina/sample"), wire.TaskBranch(id, "Slow change")) != "" { |
| 270 | t.Fatal("a cancelled step pushed") |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // A step that asks to write outside /workspace is denied by the |
| 275 | // permission callback, and the tree commits only what it changed inside. |
| 276 | func scenarioStepContained(t *testing.T, env *Env) { |
| 277 | if env.Live { |
| 278 | t.Skip("local only: the fake CLI asks to write outside /workspace; a live model is never steered to") |
| 279 | } |
| 280 | s := open(t, env) |
| 281 | s.keep() |
| 282 | sent := s.send(t, "tool:record_task:oficina/sample|Contained|x\ntool:start_task:$task|escape:/etc/hostname escape:/home/runner/.claude/x edit:README.md|teh|the") |
| 283 | id := s.taskOf(t, sent.Turn.ID) |
| 284 | st := s.step(t, id, 2*time.Minute, wire.StepDone, wire.StepFailed) |
| 285 | if st.State != wire.StepDone || !strings.Contains(st.Summary, "Write to /etc/hostname was denied.") || !strings.Contains(st.Summary, "Write to /home/runner/.claude/x was denied.") { |
| 286 | t.Fatalf("step %+v", st) |
| 287 | } |
| 288 | if _, err := os.Stat("/etc/hostname.equipo"); err == nil { |
| 289 | t.Fatal("wrote outside") |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // A step whose only requests the permission callback denied commits |
| 294 | // nothing and stops tools_refused, not no_changes: the person reads that |
| 295 | // Equipo refused the commands it needed, not that nothing needed changing. |
| 296 | func scenarioStepRefused(t *testing.T, env *Env) { |
| 297 | if env.Live { |
| 298 | t.Skip("local only: the fake CLI asks to write outside /workspace; a live model is never steered to") |
| 299 | } |
| 300 | s := open(t, env) |
| 301 | s.keep() |
| 302 | sent := s.send(t, "tool:record_task:oficina/sample|Refused|x\ntool:start_task:$task|escape:/etc/hostname") |
| 303 | id := s.taskOf(t, sent.Turn.ID) |
| 304 | st := s.step(t, id, 2*time.Minute, wire.StepDone, wire.StepFailed) |
| 305 | if st.State != wire.StepFailed || st.Reason != "tools_refused" || st.Pushed { |
| 306 | t.Fatalf("step %+v", st) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // A grant shows up at once when the person acts on the grant card: the |
| 311 | // card names the namespace, the runner checks its reach, indexes only |
| 312 | // the new repository, and the card resolves itself. |
| 313 | func scenarioGrant(t *testing.T, env *Env) { |
| 314 | if env.Live { |
| 315 | t.Skip("no live grant on staging") |
| 316 | } |
| 317 | s := open(t, env) |
| 318 | s.keep() |
| 319 | // The person names the repository: in a namespace Equipo knows |
| 320 | // nothing of, that is what raises the card (#48, review R1). |
| 321 | sent := s.send(t, "Fix the index in recipes/book. tool:find:recipes/book") |
| 322 | var card wire.Card |
| 323 | s.wait(t, "access card", time.Minute, func(c *thintest.Conversation) bool { |
| 324 | if k := cards(c, sent.Turn.ID, wire.CardAccess); len(k) == 1 { |
| 325 | card = k[0] |
| 326 | } |
| 327 | return card.ID != "" |
| 328 | }) |
| 329 | if card.Name != "recipes/book" || card.Choice != "equipo-test" { |
| 330 | t.Fatalf("card %+v", card) |
| 331 | } |
| 332 | s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) |
| 333 | if reply := s.reply(t, sent.Turn.ID).Text; strings.Contains(reply, "amadan agent grant") { |
| 334 | t.Fatalf("the reply repeats the card: %s", reply) |
| 335 | } |
| 336 | env.GrantNow(t, "amadan.net/recipes/book") |
| 337 | start := time.Now() |
| 338 | if _, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceCheck, card.Revision); err != nil { |
| 339 | t.Fatal(err) |
| 340 | } |
| 341 | s.wait(t, "card resolved", 45*time.Second, func(c *thintest.Conversation) bool { return c.Cards[card.ID].State == wire.CardResolved }) |
| 342 | t.Logf("grant seen %s after the check", time.Since(start).Round(time.Millisecond)) |
| 343 | list, err := env.Client.Projects(s.ctx) |
| 344 | if err != nil || !slices.Contains(list.Allowed, "amadan.net/recipes/book") { |
| 345 | t.Fatalf("allowlist %+v %v", list.Allowed, err) |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // A repository the agent reaches but nobody has pushed to yet, through |
| 350 | // the coordinator's own tools on the real runner: status offers it, |
| 351 | // add_project adds it, find names it and the read tools say it has no |
| 352 | // commits yet. Nothing calls it unreachable or puts up an access card. |
| 353 | func scenarioEmptyRepo(t *testing.T, env *Env) { |
| 354 | if env.Live || env.GrantEmpty == nil { |
| 355 | t.Skip("local forge only") |
| 356 | } |
| 357 | const repo = "amadan.net/paulca/threatmin" |
| 358 | env.GrantEmpty(t, repo) |
| 359 | s := open(t, env) |
| 360 | s.keep() |
| 361 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 362 | deadline := time.Now().Add(45 * time.Second) |
| 363 | for found, empty := env.Indexed(t, repo); !found || !empty; found, empty = env.Indexed(t, repo) { |
| 364 | if time.Now().After(deadline) { |
| 365 | t.Fatalf("not catalogued as empty: found %v", found) |
| 366 | } |
| 367 | time.Sleep(200 * time.Millisecond) |
| 368 | } |
| 369 | sent := s.send(t, "Look at threatmin. tool:status\ntool:add_project:paulca/threatmin\ntool:find:threatmin\ntool:read_file:paulca/threatmin|README.md\ntool:search_code:paulca/threatmin|main") |
| 370 | s.waitTurn(t, sent.Turn.ID, wire.TurnReplied) |
| 371 | r := s.reply(t, sent.Turn.ID).Text |
| 372 | if !strings.Contains(r, `"paulca/threatmin"`) || !strings.Contains(r, "[add_project: Added paulca/threatmin.") || strings.Count(r, "no commits yet") < 3 { |
| 373 | t.Fatalf("reply %s", r) |
| 374 | } |
| 375 | for _, bad := range []string{"Could not read that", "not one Equipo's agent can reach", "amadan agent grant"} { |
| 376 | if strings.Contains(r, bad) { |
| 377 | t.Fatalf("reply says %q: %s", bad, r) |
| 378 | } |
| 379 | } |
| 380 | if _, ok := s.project("paulca/threatmin"); !ok { |
| 381 | t.Fatal("no project") |
| 382 | } |
| 383 | s.conv.Locked(func(c *thintest.Conversation) { |
| 384 | if k := cards(c, sent.Turn.ID, wire.CardAccess); len(k) != 0 { |
| 385 | t.Fatalf("access card %+v", k) |
| 386 | } |
| 387 | }) |
| 388 | } |
| 389 | |
| 390 | // Engineering in a repository with no commits (Paul's barber mockups in |
| 391 | // an empty paulca repository): the step starts from an empty tree, its |
| 392 | // commit is the repository's initial commit, and the push creates the |
| 393 | // task branch only; the default branch stays unborn until a merge. |
| 394 | // Review reads that commit and approves it. With no trusted base, checks |
| 395 | // are unavailable and the task waits for the person. Reads of the |
| 396 | // repository still say it has no commits yet. |
| 397 | func scenarioEmptyEngineering(t *testing.T, env *Env) { |
| 398 | if env.Live || env.GrantEmpty == nil { |
| 399 | t.Skip("local forge only") |
| 400 | } |
| 401 | const repo = "amadan.net/paulca/barber" |
| 402 | env.GrantEmpty(t, repo) |
| 403 | s := open(t, env) |
| 404 | s.keep() |
| 405 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 406 | deadline := time.Now().Add(45 * time.Second) |
| 407 | for found, empty := env.Indexed(t, repo); !found || !empty; found, empty = env.Indexed(t, repo) { |
| 408 | if time.Now().After(deadline) { |
| 409 | t.Fatalf("not catalogued as empty: found %v", found) |
| 410 | } |
| 411 | time.Sleep(200 * time.Millisecond) |
| 412 | } |
| 413 | sent := s.send(t, "tool:record_task:paulca/barber|Create two clickable barber app mockups|Two screens that link to each other.\ntool:start_task:$task|Make them. create:index.html|<a href=book.html>Book a cut</a>; create:book.html|<a href=index.html>Back</a>") |
| 414 | id := s.taskOf(t, sent.Turn.ID) |
| 415 | k := s.settled(t, id, 10*time.Minute) |
| 416 | checks, review := oneSHA(t, k) |
| 417 | var eng wire.Step |
| 418 | for _, st := range k.Steps { |
| 419 | if st.Specialist == wire.SpecialistEngineering && st.Pushed { |
| 420 | eng = st |
| 421 | } |
| 422 | } |
| 423 | t.Logf("task %s: %s needs_you=%q checks %s verdict %s on %s", id, k.Stage, k.NeedsYou, k.Checks, k.Verdict, k.CandidateSHA) |
| 424 | if eng.State != wire.StepDone || eng.BaseSHA != "" || len(eng.CandidateSHA) != 40 || eng.CandidateSHA != k.CandidateSHA { |
| 425 | t.Fatalf("engineering %+v", eng) |
| 426 | } |
| 427 | bare := env.ForgePath(repo) |
| 428 | if got := forgeHead(t, bare, k.Branch); got != eng.CandidateSHA { |
| 429 | t.Fatalf("forge branch at %q, want %s", got, eng.CandidateSHA) |
| 430 | } |
| 431 | if got := forgeHead(t, bare, "main"); got != "" { |
| 432 | t.Fatalf("main was created at %s", got) |
| 433 | } |
| 434 | if parents := strings.TrimSpace(forgeShow(t, bare, "--no-patch", "--format=%P", eng.CandidateSHA)); parents != "" { |
| 435 | t.Fatalf("the first commit has parents %q", parents) |
| 436 | } |
| 437 | if page := forgeShow(t, bare, eng.CandidateSHA+":book.html"); !strings.Contains(page, "<a href=index.html>Back</a>") { |
| 438 | t.Fatalf("book.html %q", page) |
| 439 | } |
| 440 | if review.State != wire.StepDone || review.Verdict != wire.VerdictApprove || checks.State != wire.StepDone || checks.Checks != wire.ChecksNone || checks.Reason != "" || checks.Gate != "" || checks.ExitCode != nil { |
| 441 | t.Fatalf("review %+v checks %+v", review, checks) |
| 442 | } |
| 443 | if k.Stage != wire.StageReadyToMerge || k.NeedsYou != wire.NeedsMerge || k.Merged != nil { |
| 444 | t.Fatalf("task ended %s", k.Stage) |
| 445 | } |
| 446 | read := s.send(t, "tool:read_file:paulca/barber|index.html") |
| 447 | s.waitTurn(t, read.Turn.ID, wire.TurnReplied) |
| 448 | if r := s.reply(t, read.Turn.ID).Text; !strings.Contains(r, "no commits yet") { |
| 449 | t.Fatalf("read after the task branch: %s", r) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // A fix round in a repository with no commits (threatmin's round 2 on |
| 454 | // staging, which stopped with push_failed): the first round pushes the |
| 455 | // task branch's initial commit; review asks for one more file and the fix |
| 456 | // round, its base the task branch's tip, pushes again as a fast-forward |
| 457 | // of that tip (the forge refuses any other). Both rounds pushed, nothing |
| 458 | // stopped, and the default branch stays unborn. |
| 459 | func scenarioEmptyFixRound(t *testing.T, env *Env) { |
| 460 | if env.Live || env.GrantEmpty == nil { |
| 461 | t.Skip("local forge only") |
| 462 | } |
| 463 | const repo = "amadan.net/paulca/parlour" |
| 464 | env.GrantEmpty(t, repo) |
| 465 | bare := env.ForgePath(repo) |
| 466 | if out, err := exec.Command("git", "--git-dir="+bare, "config", "receive.denyNonFastForwards", "true").CombinedOutput(); err != nil { |
| 467 | t.Fatalf("git config: %v %s", err, out) |
| 468 | } |
| 469 | s := open(t, env) |
| 470 | s.keep() |
| 471 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 472 | deadline := time.Now().Add(45 * time.Second) |
| 473 | for found, empty := env.Indexed(t, repo); !found || !empty; found, empty = env.Indexed(t, repo) { |
| 474 | if time.Now().After(deadline) { |
| 475 | t.Fatalf("not catalogued as empty: found %v", found) |
| 476 | } |
| 477 | time.Sleep(200 * time.Millisecond) |
| 478 | } |
| 479 | sent := s.send(t, "tool:record_task:paulca/parlour|Create the parlour mockup|One page and its hours.\ntool:start_task:$task|review:need:hours.txt|open late|create=hours.txt~open late; create:index.html|<h1>Parlour</h1>") |
| 480 | id := s.taskOf(t, sent.Turn.ID) |
| 481 | k := s.settled(t, id, 10*time.Minute) |
| 482 | var engs []wire.Step |
| 483 | for _, st := range k.Steps { |
| 484 | if st.State == wire.StepFailed { |
| 485 | t.Fatalf("a step stopped: %+v", st) |
| 486 | } |
| 487 | if st.Specialist == wire.SpecialistEngineering { |
| 488 | engs = append(engs, st) |
| 489 | } |
| 490 | } |
| 491 | t.Logf("task %s: %s needs_you=%q fix rounds %d verdict %s on %s", id, k.Stage, k.NeedsYou, k.FixRounds, k.Verdict, k.CandidateSHA) |
| 492 | if len(engs) != 2 || k.FixRounds != 1 { |
| 493 | t.Fatalf("engineering rounds %+v (fix rounds %d)", engs, k.FixRounds) |
| 494 | } |
| 495 | first, fix := engs[0], engs[1] |
| 496 | if !first.Pushed || first.BaseSHA != "" || !fix.Pushed || fix.State != wire.StepDone || fix.BaseSHA != first.CandidateSHA || fix.CandidateSHA != k.CandidateSHA { |
| 497 | t.Fatalf("rounds: first %+v fix %+v", first, fix) |
| 498 | } |
| 499 | if got := forgeHead(t, bare, k.Branch); got != fix.CandidateSHA { |
| 500 | t.Fatalf("forge branch at %q, want the fix round's %s", got, fix.CandidateSHA) |
| 501 | } |
| 502 | if parents := strings.TrimSpace(forgeShow(t, bare, "--no-patch", "--format=%P", fix.CandidateSHA)); parents != first.CandidateSHA { |
| 503 | t.Fatalf("the fix round's parents %q, want the first round's %s", parents, first.CandidateSHA) |
| 504 | } |
| 505 | if got := forgeHead(t, bare, "main"); got != "" { |
| 506 | t.Fatalf("main was created at %s", got) |
| 507 | } |
| 508 | if k.Verdict != wire.VerdictApprove || k.Stage != wire.StageReadyToMerge && k.NeedsYou == "" { |
| 509 | t.Fatalf("task ended %s, verdict %s", k.Stage, k.Verdict) |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // The first merge into an empty repository, through the real runner and |
| 514 | // both hubs. Before the hub update the person's Merge is refused: the |
| 515 | // step stops with merge_unborn_default, the task stays Ready to merge |
| 516 | // and waits on the platform (no retry), and the default branch stays |
| 517 | // unborn. After it the same Merge creates the default branch at the |
| 518 | // reviewed tip, the task is done, and the repository is no longer empty: |
| 519 | // its files read. |
| 520 | func scenarioEmptyMerge(t *testing.T, env *Env) { |
| 521 | if env.Live || env.GrantEmpty == nil || env.HubUpdate == nil { |
| 522 | t.Skip("local forge only") |
| 523 | } |
| 524 | const repo = "amadan.net/paulca/salon" |
| 525 | env.GrantEmpty(t, repo) |
| 526 | s := open(t, env) |
| 527 | s.keep() |
| 528 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 529 | indexed := func(wantEmpty bool) { |
| 530 | deadline := time.Now().Add(45 * time.Second) |
| 531 | for found, empty := env.Indexed(t, repo); !found || empty != wantEmpty; found, empty = env.Indexed(t, repo) { |
| 532 | if time.Now().After(deadline) { |
| 533 | t.Fatalf("catalogued: found %v empty %v, want empty %v", found, empty, wantEmpty) |
| 534 | } |
| 535 | time.Sleep(200 * time.Millisecond) |
| 536 | } |
| 537 | } |
| 538 | indexed(true) |
| 539 | sent := s.send(t, "tool:record_task:paulca/salon|Create the salon mockup|One page.\ntool:start_task:$task|Make it. create:index.html|<h1>Salon</h1>") |
| 540 | id := s.taskOf(t, sent.Turn.ID) |
| 541 | k := s.settled(t, id, 10*time.Minute) |
| 542 | if k.Stage != wire.StageReadyToMerge || len(k.CandidateSHA) != 40 { |
| 543 | t.Fatalf("task %s on %q", k.Stage, k.CandidateSHA) |
| 544 | } |
| 545 | cand, bare := k.CandidateSHA, env.ForgePath(repo) |
| 546 | // No checks: Equipo asks; the person chooses Merge on the card. |
| 547 | card := s.mergeCard(t, id) |
| 548 | if _, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 549 | t.Fatal(err) |
| 550 | } |
| 551 | s.wait(t, "waiting on the platform", 2*time.Minute, func(c *thintest.Conversation) bool { |
| 552 | k := c.Tasks[id] |
| 553 | return k.Triage != nil && k.Triage.Kind == wire.TriageWaiting |
| 554 | }) |
| 555 | k, _ = env.Client.Task(s.ctx, id) |
| 556 | last := k.Steps[len(k.Steps)-1] |
| 557 | if last.Specialist != wire.SpecialistMerge || last.State != wire.StepFailed || last.Reason != wire.ReasonMergeUnbornDefault || k.Stage != wire.StageReadyToMerge || k.Triage.Reason != wire.ReasonMergeUnbornDefault { |
| 558 | t.Fatalf("before the hub update: %s %+v triage %+v", k.Stage, last, k.Triage) |
| 559 | } |
| 560 | if got := forgeHead(t, bare, "main"); got != "" { |
| 561 | t.Fatalf("main was created at %s", got) |
| 562 | } |
| 563 | env.HubUpdate(t) |
| 564 | s.wait(t, "runner online again", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 565 | if _, err := env.Client.MergeTask(s.ctx, id); err != nil { |
| 566 | t.Fatal(err) |
| 567 | } |
| 568 | s.wait(t, "merged", 2*time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 569 | k, _ = env.Client.Task(s.ctx, id) |
| 570 | if k.Merged == nil || k.Merged.SHA != cand || k.Merged.Into != "main" { |
| 571 | t.Fatalf("merged %+v", k.Merged) |
| 572 | } |
| 573 | if got := forgeHead(t, bare, "main"); got != cand { |
| 574 | t.Fatalf("main at %q, want the reviewed tip %s", got, cand) |
| 575 | } |
| 576 | indexed(false) |
| 577 | read := s.send(t, "tool:read_file:paulca/salon|index.html") |
| 578 | s.waitTurn(t, read.Turn.ID, wire.TurnReplied) |
| 579 | if r := s.reply(t, read.Turn.ID).Text; !strings.Contains(r, "1\t<h1>Salon</h1>") || strings.Contains(r, "no commits yet") { |
| 580 | t.Fatalf("read after the first merge: %s", r) |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // threatmin's state on the hub (amadan-13): a change task in an empty |
| 585 | // repository pushed its branch, a hub rebuild pointed HEAD at it, and |
| 586 | // there is no main. Through the real runner, the person's Merge reads |
| 587 | // that as an unborn default named main, the updated hub creates main at |
| 588 | // the reviewed tip and keeps the task branch (branch_closed false), and |
| 589 | // the task is done: the kept branch reads as merged and the repository's |
| 590 | // files read. |
| 591 | func scenarioEmptyMergeRebuilt(t *testing.T, env *Env) { |
| 592 | if env.Live || env.GrantEmpty == nil || env.HubUpdate == nil || env.RebuildHead == nil { |
| 593 | t.Skip("local forge only") |
| 594 | } |
| 595 | const repo = "amadan.net/paulca/studio" |
| 596 | env.GrantEmpty(t, repo) |
| 597 | env.HubUpdate(t) |
| 598 | s := open(t, env) |
| 599 | s.keep() |
| 600 | s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) |
| 601 | indexed := func(wantEmpty bool) { |
| 602 | deadline := time.Now().Add(45 * time.Second) |
| 603 | for found, empty := env.Indexed(t, repo); !found || empty != wantEmpty; found, empty = env.Indexed(t, repo) { |
| 604 | if time.Now().After(deadline) { |
| 605 | t.Fatalf("catalogued: found %v empty %v, want empty %v", found, empty, wantEmpty) |
| 606 | } |
| 607 | time.Sleep(200 * time.Millisecond) |
| 608 | } |
| 609 | } |
| 610 | indexed(true) |
| 611 | sent := s.send(t, "tool:record_task:paulca/studio|Add the studio home page|One page.\ntool:start_task:$task|Make it. create:index.html|<h1>Studio</h1>") |
| 612 | id := s.taskOf(t, sent.Turn.ID) |
| 613 | k := s.settled(t, id, 10*time.Minute) |
| 614 | if k.Stage != wire.StageReadyToMerge || len(k.CandidateSHA) != 40 { |
| 615 | t.Fatalf("task %s on %q", k.Stage, k.CandidateSHA) |
| 616 | } |
| 617 | cand, bare := k.CandidateSHA, env.ForgePath(repo) |
| 618 | env.RebuildHead(t, repo, k.Branch) |
| 619 | if ls, _ := exec.Command("git", "ls-remote", "--symref", bare).Output(); !strings.HasPrefix(string(ls), "ref: refs/heads/"+k.Branch+"\tHEAD\n") || strings.Contains(string(ls), "refs/heads/main") { |
| 620 | t.Fatalf("not threatmin's state:\n%s", ls) |
| 621 | } |
| 622 | card := s.mergeCard(t, id) |
| 623 | if _, err := env.Client.Answer(s.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 624 | t.Fatal(err) |
| 625 | } |
| 626 | s.wait(t, "merged", 2*time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 627 | k, _ = env.Client.Task(s.ctx, id) |
| 628 | if k.Merged == nil || k.Merged.SHA != cand || k.Merged.Into != "main" { |
| 629 | t.Fatalf("merged %+v", k.Merged) |
| 630 | } |
| 631 | if forgeHead(t, bare, "main") != cand || forgeHead(t, bare, k.Branch) != cand { |
| 632 | t.Fatalf("main %q, kept branch %q, want both at %s", forgeHead(t, bare, "main"), forgeHead(t, bare, k.Branch), cand) |
| 633 | } |
| 634 | indexed(false) |
| 635 | read := s.send(t, "tool:read_file:paulca/studio|index.html") |
| 636 | s.waitTurn(t, read.Turn.ID, wire.TurnReplied) |
| 637 | if r := s.reply(t, read.Turn.ID).Text; !strings.Contains(r, "1\t<h1>Studio</h1>") { |
| 638 | t.Fatalf("read after the first merge: %s", r) |
| 639 | } |
| 640 | } |
| 641 | |