package headless import ( "os" "os/exec" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/thintest" "amadan.net/oficina/equipo/internal/wire" ) // asked asks a question through person API v1 and waits for it to end, // returning it with its timing as the client saw it. func (s *session) asked(t *testing.T, task, text, specialist string, timeout time.Duration) (wire.Question, askClock) { t.Helper() start := time.Now() q, err := s.env.Client.Ask(s.ctx, task, text, specialist, key()) if err != nil { t.Fatalf("ask: %v", err) } s.env.Turn(t) var out wire.Question var clock askClock s.wait(t, "question "+q.ID+" ends", timeout, func(c *thintest.Conversation) bool { out = c.Questions[q.ID] if first, ok := c.FirstDelta[q.ID]; ok && clock.FirstText == 0 { clock.FirstText = first.Sub(start) } if first, ok := c.FirstActivity[q.ID]; ok && clock.FirstActivity == 0 { clock.FirstActivity = first.Sub(start) } return wire.QuestionTerminal(out.State) }) clock.Answered = time.Since(start) return out, clock } type askClock struct{ FirstActivity, FirstText, Answered time.Duration } func (c askClock) seconds() map[string]float64 { return map[string]float64{"first_activity_s": c.FirstActivity.Seconds(), "first_text_s": c.FirstText.Seconds(), "answered_s": c.Answered.Seconds()} } // Ask the implementer: while a task waits at Ready to merge (its checks // could not run here, so Equipo asks before merging), its creator asks // Engineering and then Review about their work. Each answer comes from // the step's own resumed session (it knows what that session said), in // a read-only tree of the step's commit: it reads the change with the // runner's git tools and a file with Read, and a write is refused. The // answer streams as deltas and activity into the task's thread (never // the conversation), and a follow-up continues the same session. A // question can be cancelled. When the task ends (merged), the runner // forgets the sessions it kept, and nothing more can be asked. func scenarioAsk(t *testing.T, env *Env) { if env.Live { t.Skip("local only: 4b_witness asks on staging") } s := open(t, env) s.keep() s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online }) // The change adds a file under .github too, a sensitive path, so Equipo asks // before merging: the task waits at Ready to merge while its creator // asks (a task Equipo merges ends at once). It touched the iOS app // until 2026-11-28, when such a change began waiting for a Mac instead // of having its checks judged on Linux. Review tries to write too // (4_checks_honest did this until then): it is read-only. sent := s.send(t, "tool:record_task:oficina/sample|Fix the README typo|The README says teh panes.\ntool:start_task:$task|create:.github/notes.md|Reading notes.; edit:README.md|teh panes|the panes review:write:README.md") id := s.taskOf(t, sent.Turn.ID) k := s.settled(t, id, 5*time.Minute) if k.Stage != wire.StageReadyToMerge { t.Fatalf("not ready: %+v", k) } var eng, rev wire.Step for _, st := range k.Steps { switch st.Specialist { case wire.SpecialistEngineering: eng = st case wire.SpecialistReview: rev = st } } if !eng.Askable || !rev.Askable { t.Fatalf("sessions not kept: engineering %v review %v", eng.Askable, rev.Askable) } // Review read the change and could not write anything. if !strings.Contains(rev.Summary, "Write to README.md was denied.") || rev.Family != "claude" || rev.Account == "" { t.Fatalf("review %+v", rev) } if strings.Contains(forgeShow(t, env.ForgePath("amadan.net/oficina/sample"), k.CandidateSHA+":README.md"), "\nx") { t.Fatal("review wrote") } if n := env.KeptSessions(t); n != 2 { t.Fatalf("%d kept sessions, want 2", n) } // Engineering, from its own session, on its commit, read-only. q1, c1 := s.asked(t, id, "What did you change and why? ask:git ask:read:README.md", "", time.Minute) if q1.State != wire.QuestionAnswered || q1.StepID != eng.ID || q1.Specialist != wire.SpecialistEngineering { t.Fatalf("engineering question %+v", q1) } // The session's last reply, as it wrote it: the headline line, then // the summary the runner kept apart from it. if !strings.HasPrefix(q1.Answer, "From my session: Headline: "+eng.Headline+"\n\n"+eng.Summary) || !strings.Contains(q1.Answer, "The change: +") || !strings.Contains(q1.Answer, "the panes") || !strings.Contains(q1.Answer, "README.md starts: ") { t.Fatalf("engineering answer %q (its step said %q)", q1.Answer, eng.Summary) } // Pinned to the attempt it asked about and the session the runner // named for it (Mac runners slice F, task 13). if attempt, session, kept := env.AskPin(t, q1.ID); attempt != eng.Attempt || session == "" || session != kept { t.Fatalf("question pinned to attempt %d session %q; the runner kept %q", attempt, session, kept) } // The answer's usage is recorded, as a turn's is. if got := env.AskUsage(t, q1.ID); len(got) != 1 || got[0] != "replied/true" { t.Fatalf("answer usage %v", got) } // With the step's round and how many model requests it made, beside // the steps' own rows. if got := env.Usage(t, id, sent.Turn.ID); !slices.Contains(got, "ask/0/turns=true/cached=false") || !slices.Contains(got, "engineering/0/turns=true/cached=false") || !slices.Contains(got, "review/0/turns=true/cached=false") { t.Fatalf("task usage %v", got) } // The question's own namespace is recorded against it, for the proof // of its exit. if ns := env.AskNamespace(t, q1.ID); ns == "" { t.Fatal("no namespace recorded for the question") } if c1.FirstText == 0 || c1.FirstActivity == 0 { t.Fatalf("the answer did not stream: %+v", c1) } var acts []wire.Activity var resources map[string]int s.conv.Locked(func(c *thintest.Conversation) { acts = c.Activity[q1.ID] resources = map[string]int{} for k, v := range c.DeltaResources { resources[k] = v } }) if len(acts) == 0 || acts[0].Tool != "read_change" || resources[wire.ResourceQuestion] == 0 { t.Fatalf("activity %+v deltas %v", acts, resources) } // Review, addressed by name, from its own session; it cannot write. q2, c2 := s.asked(t, id, "Review, anything you were unsure about? ask:write:README.md", "", time.Minute) if q2.State != wire.QuestionAnswered || q2.StepID != rev.ID || !strings.HasPrefix(q2.Answer, "From my session: "+rev.Summary) || !strings.Contains(q2.Answer, "Write to README.md was denied. I can't change anything from here.") { t.Fatalf("review question %+v (review said %q)", q2, rev.Summary) } // A follow-up continues Engineering's session: it knows its answer. q3, _ := s.asked(t, id, "And then?", wire.SpecialistEngineering, time.Minute) if !strings.HasPrefix(q3.Answer, "From my session: "+q1.Answer) { t.Fatalf("follow-up %q", q3.Answer) } bare := env.ForgePath("amadan.net/oficina/sample") if forgeHead(t, bare, k.Branch) != k.CandidateSHA || strings.Contains(forgeShow(t, bare, k.CandidateSHA+":README.md"), "\nx") { t.Fatal("asking changed the branch") } // Cancel stops a question through the runner. q4, err := env.Client.Ask(s.ctx, id, "Take your time. asksleep:20000", "", key()) if err != nil { t.Fatal(err) } s.wait(t, "running", time.Minute, func(c *thintest.Conversation) bool { return c.Questions[q4.ID].State == wire.QuestionRunning }) time.Sleep(time.Second) if _, err := env.Client.CancelQuestion(s.ctx, q4.ID); err != nil { t.Fatal(err) } s.wait(t, "cancelled", time.Minute, func(c *thintest.Conversation) bool { return c.Questions[q4.ID].State == wire.QuestionCancelled }) if got := env.AskUsage(t, q4.ID); len(got) != 1 || !strings.HasPrefix(got[0], "cancelled/") { t.Fatalf("cancelled answer usage %v", got) } Witness[t.Name()] = map[string]any{"task": id, "engineering": c1.seconds(), "review": c2.seconds()} // The person merges; the task is done, and for 14 days (Paul, // 2026-11-08) Engineering can still be asked about the merged work, // from its own kept session. if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/main", k.CandidateSHA).CombinedOutput(); err != nil { t.Fatalf("merge: %v %s", err, out) } s.wait(t, "done", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) time.Sleep(2 * time.Second) if n := env.KeptSessions(t); n == 0 { t.Fatal("the sessions went with the merge") } after, _ := s.asked(t, id, "Now that it is merged, what did you change?", "", time.Minute) if after.State != wire.QuestionAnswered || after.Specialist != wire.SpecialistEngineering || !strings.Contains(after.Answer, "From my session:") { t.Fatalf("asked about merged work: %+v", after) } } // The staging witness (EQUIPO_STAGING_ASK_TASK, else the task this run's // 3_engineering pushed, which 4_review has seen judged): on a task whose // steps ran with their sessions kept, ask Engineering what it changed and // Review what it was unsure about, then ask Engineering to fix a typo, // which it must decline from here. Answers and timings go to the report. func scenarioAskWitness(t *testing.T, env *Env) { id := os.Getenv("EQUIPO_STAGING_ASK_TASK") if id == "" { id = pushedTask } if !env.Live || id == "" { t.Skip("staging only, with EQUIPO_STAGING_ASK_TASK or this run's 3_engineering task") } s := open(t, env) s.keep() k, err := env.Client.Task(s.ctx, id) if err != nil { t.Fatal(err) } if os.Getenv("EQUIPO_STAGING_ASK_TASK") == "" { // Only a Claude step keeps its session (a Codex step keeps none, // by design), and where this run's steps ran is the runner's call. // The newest finished step of each is the one a question goes to. newest := map[string]wire.Step{} for _, st := range k.Steps { if st.State == wire.StepDone && (st.Specialist == wire.SpecialistEngineering || st.Specialist == wire.SpecialistReview) { newest[st.Specialist] = st } } for _, name := range []string{wire.SpecialistEngineering, wire.SpecialistReview} { st := newest[name] if st.Family != "claude" { t.Skipf("live: this run's %s ran on %s (%s), which keeps no session to ask", st.Specialist, st.Account, st.Family) } if !st.Askable { t.Fatalf("a finished Claude step kept no session: %+v", st) } } } before := k.CandidateSHA out := map[string]any{"task": id} for _, q := range []struct{ name, text, specialist string }{ {"engineering", "What did you change and why?", wire.SpecialistEngineering}, {"review", "Anything you were unsure about?", wire.SpecialistReview}, {"write", "Please fix the typo \"teh\" in the README yourself.", wire.SpecialistEngineering}, } { got, clock := s.asked(t, id, q.text, q.specialist, 10*time.Minute) t.Logf("%s (%s): %s %s in %s: %s", q.name, got.StepID, got.State, got.Reason, clock.Answered.Round(time.Millisecond), got.Answer) out[q.name] = map[string]any{"question": q.text, "step": got.StepID, "state": got.State, "reason": got.Reason, "answer": got.Answer, "timing": clock.seconds(), "runner_timing": got.Timing} if got.State != wire.QuestionAnswered { t.Errorf("%s not answered: %+v", q.name, got) } } Witness[t.Name()] = out if k, _ = env.Client.Task(s.ctx, id); k.CandidateSHA != before { t.Fatalf("asking moved the candidate from %s to %s", before, k.CandidateSHA) } }