oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package headless
2
3import (
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// asked asks a question through person API v1 and waits for it to end,
16// returning it with its timing as the client saw it.
17func (s *session) asked(t *testing.T, task, text, specialist string, timeout time.Duration) (wire.Question, askClock) {
18 t.Helper()
19 start := time.Now()
20 q, err := s.env.Client.Ask(s.ctx, task, text, specialist, key())
21 if err != nil {
22 t.Fatalf("ask: %v", err)
23 }
24 s.env.Turn(t)
25 var out wire.Question
26 var clock askClock
27 s.wait(t, "question "+q.ID+" ends", timeout, func(c *thintest.Conversation) bool {
28 out = c.Questions[q.ID]
29 if first, ok := c.FirstDelta[q.ID]; ok && clock.FirstText == 0 {
30 clock.FirstText = first.Sub(start)
31 }
32 if first, ok := c.FirstActivity[q.ID]; ok && clock.FirstActivity == 0 {
33 clock.FirstActivity = first.Sub(start)
34 }
35 return wire.QuestionTerminal(out.State)
36 })
37 clock.Answered = time.Since(start)
38 return out, clock
39}
40
41type askClock struct{ FirstActivity, FirstText, Answered time.Duration }
42
43func (c askClock) seconds() map[string]float64 {
44 return map[string]float64{"first_activity_s": c.FirstActivity.Seconds(), "first_text_s": c.FirstText.Seconds(), "answered_s": c.Answered.Seconds()}
45}
46
47// Ask the implementer: while a task waits at Ready to merge (its checks
48// could not run here, so Equipo asks before merging), its creator asks
49// Engineering and then Review about their work. Each answer comes from
50// the step's own resumed session (it knows what that session said), in
51// a read-only tree of the step's commit: it reads the change with the
52// runner's git tools and a file with Read, and a write is refused. The
53// answer streams as deltas and activity into the task's thread (never
54// the conversation), and a follow-up continues the same session. A
55// question can be cancelled. When the task ends (merged), the runner
56// forgets the sessions it kept, and nothing more can be asked.
57func scenarioAsk(t *testing.T, env *Env) {
58 if env.Live {
59 t.Skip("local only: 4b_witness asks on staging")
60 }
61 s := open(t, env)
62 s.keep()
63 s.wait(t, "runner online", 30*time.Second, func(c *thintest.Conversation) bool { return c.Runner.Online })
64 // The change adds a file under .github too, a sensitive path, so Equipo asks
65 // before merging: the task waits at Ready to merge while its creator
66 // asks (a task Equipo merges ends at once). It touched the iOS app
67 // until 2026-11-28, when such a change began waiting for a Mac instead
68 // of having its checks judged on Linux. Review tries to write too
69 // (4_checks_honest did this until then): it is read-only.
70 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")
71 id := s.taskOf(t, sent.Turn.ID)
72 k := s.settled(t, id, 5*time.Minute)
73 if k.Stage != wire.StageReadyToMerge {
74 t.Fatalf("not ready: %+v", k)
75 }
76 var eng, rev wire.Step
77 for _, st := range k.Steps {
78 switch st.Specialist {
79 case wire.SpecialistEngineering:
80 eng = st
81 case wire.SpecialistReview:
82 rev = st
83 }
84 }
85 if !eng.Askable || !rev.Askable {
86 t.Fatalf("sessions not kept: engineering %v review %v", eng.Askable, rev.Askable)
87 }
88 // Review read the change and could not write anything.
89 if !strings.Contains(rev.Summary, "Write to README.md was denied.") || rev.Family != "claude" || rev.Account == "" {
90 t.Fatalf("review %+v", rev)
91 }
92 if strings.Contains(forgeShow(t, env.ForgePath("amadan.net/oficina/sample"), k.CandidateSHA+":README.md"), "\nx") {
93 t.Fatal("review wrote")
94 }
95 if n := env.KeptSessions(t); n != 2 {
96 t.Fatalf("%d kept sessions, want 2", n)
97 }
98 // Engineering, from its own session, on its commit, read-only.
99 q1, c1 := s.asked(t, id, "What did you change and why? ask:git ask:read:README.md", "", time.Minute)
100 if q1.State != wire.QuestionAnswered || q1.StepID != eng.ID || q1.Specialist != wire.SpecialistEngineering {
101 t.Fatalf("engineering question %+v", q1)
102 }
103 // The session's last reply, as it wrote it: the headline line, then
104 // the summary the runner kept apart from it.
105 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: ") {
106 t.Fatalf("engineering answer %q (its step said %q)", q1.Answer, eng.Summary)
107 }
108 // Pinned to the attempt it asked about and the session the runner
109 // named for it (Mac runners slice F, task 13).
110 if attempt, session, kept := env.AskPin(t, q1.ID); attempt != eng.Attempt || session == "" || session != kept {
111 t.Fatalf("question pinned to attempt %d session %q; the runner kept %q", attempt, session, kept)
112 }
113 // The answer's usage is recorded, as a turn's is.
114 if got := env.AskUsage(t, q1.ID); len(got) != 1 || got[0] != "replied/true" {
115 t.Fatalf("answer usage %v", got)
116 }
117 // With the step's round and how many model requests it made, beside
118 // the steps' own rows.
119 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") {
120 t.Fatalf("task usage %v", got)
121 }
122 // The question's own namespace is recorded against it, for the proof
123 // of its exit.
124 if ns := env.AskNamespace(t, q1.ID); ns == "" {
125 t.Fatal("no namespace recorded for the question")
126 }
127 if c1.FirstText == 0 || c1.FirstActivity == 0 {
128 t.Fatalf("the answer did not stream: %+v", c1)
129 }
130 var acts []wire.Activity
131 var resources map[string]int
132 s.conv.Locked(func(c *thintest.Conversation) {
133 acts = c.Activity[q1.ID]
134 resources = map[string]int{}
135 for k, v := range c.DeltaResources {
136 resources[k] = v
137 }
138 })
139 if len(acts) == 0 || acts[0].Tool != "read_change" || resources[wire.ResourceQuestion] == 0 {
140 t.Fatalf("activity %+v deltas %v", acts, resources)
141 }
142 // Review, addressed by name, from its own session; it cannot write.
143 q2, c2 := s.asked(t, id, "Review, anything you were unsure about? ask:write:README.md", "", time.Minute)
144 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.") {
145 t.Fatalf("review question %+v (review said %q)", q2, rev.Summary)
146 }
147 // A follow-up continues Engineering's session: it knows its answer.
148 q3, _ := s.asked(t, id, "And then?", wire.SpecialistEngineering, time.Minute)
149 if !strings.HasPrefix(q3.Answer, "From my session: "+q1.Answer) {
150 t.Fatalf("follow-up %q", q3.Answer)
151 }
152 bare := env.ForgePath("amadan.net/oficina/sample")
153 if forgeHead(t, bare, k.Branch) != k.CandidateSHA || strings.Contains(forgeShow(t, bare, k.CandidateSHA+":README.md"), "\nx") {
154 t.Fatal("asking changed the branch")
155 }
156 // Cancel stops a question through the runner.
157 q4, err := env.Client.Ask(s.ctx, id, "Take your time. asksleep:20000", "", key())
158 if err != nil {
159 t.Fatal(err)
160 }
161 s.wait(t, "running", time.Minute, func(c *thintest.Conversation) bool { return c.Questions[q4.ID].State == wire.QuestionRunning })
162 time.Sleep(time.Second)
163 if _, err := env.Client.CancelQuestion(s.ctx, q4.ID); err != nil {
164 t.Fatal(err)
165 }
166 s.wait(t, "cancelled", time.Minute, func(c *thintest.Conversation) bool { return c.Questions[q4.ID].State == wire.QuestionCancelled })
167 if got := env.AskUsage(t, q4.ID); len(got) != 1 || !strings.HasPrefix(got[0], "cancelled/") {
168 t.Fatalf("cancelled answer usage %v", got)
169 }
170 Witness[t.Name()] = map[string]any{"task": id, "engineering": c1.seconds(), "review": c2.seconds()}
171
172 // The person merges; the task is done, and for 14 days (Paul,
173 // 2026-11-08) Engineering can still be asked about the merged work,
174 // from its own kept session.
175 if out, err := exec.Command("git", "--git-dir="+bare, "update-ref", "refs/heads/main", k.CandidateSHA).CombinedOutput(); err != nil {
176 t.Fatalf("merge: %v %s", err, out)
177 }
178 s.wait(t, "done", time.Minute, func(c *thintest.Conversation) bool { return c.Tasks[id].Stage == wire.StageDone })
179 time.Sleep(2 * time.Second)
180 if n := env.KeptSessions(t); n == 0 {
181 t.Fatal("the sessions went with the merge")
182 }
183 after, _ := s.asked(t, id, "Now that it is merged, what did you change?", "", time.Minute)
184 if after.State != wire.QuestionAnswered || after.Specialist != wire.SpecialistEngineering || !strings.Contains(after.Answer, "From my session:") {
185 t.Fatalf("asked about merged work: %+v", after)
186 }
187}
188
189// The staging witness (EQUIPO_STAGING_ASK_TASK, else the task this run's
190// 3_engineering pushed, which 4_review has seen judged): on a task whose
191// steps ran with their sessions kept, ask Engineering what it changed and
192// Review what it was unsure about, then ask Engineering to fix a typo,
193// which it must decline from here. Answers and timings go to the report.
194func scenarioAskWitness(t *testing.T, env *Env) {
195 id := os.Getenv("EQUIPO_STAGING_ASK_TASK")
196 if id == "" {
197 id = pushedTask
198 }
199 if !env.Live || id == "" {
200 t.Skip("staging only, with EQUIPO_STAGING_ASK_TASK or this run's 3_engineering task")
201 }
202 s := open(t, env)
203 s.keep()
204 k, err := env.Client.Task(s.ctx, id)
205 if err != nil {
206 t.Fatal(err)
207 }
208 if os.Getenv("EQUIPO_STAGING_ASK_TASK") == "" {
209 // Only a Claude step keeps its session (a Codex step keeps none,
210 // by design), and where this run's steps ran is the runner's call.
211 // The newest finished step of each is the one a question goes to.
212 newest := map[string]wire.Step{}
213 for _, st := range k.Steps {
214 if st.State == wire.StepDone && (st.Specialist == wire.SpecialistEngineering || st.Specialist == wire.SpecialistReview) {
215 newest[st.Specialist] = st
216 }
217 }
218 for _, name := range []string{wire.SpecialistEngineering, wire.SpecialistReview} {
219 st := newest[name]
220 if st.Family != "claude" {
221 t.Skipf("live: this run's %s ran on %s (%s), which keeps no session to ask", st.Specialist, st.Account, st.Family)
222 }
223 if !st.Askable {
224 t.Fatalf("a finished Claude step kept no session: %+v", st)
225 }
226 }
227 }
228 before := k.CandidateSHA
229 out := map[string]any{"task": id}
230 for _, q := range []struct{ name, text, specialist string }{
231 {"engineering", "What did you change and why?", wire.SpecialistEngineering},
232 {"review", "Anything you were unsure about?", wire.SpecialistReview},
233 {"write", "Please fix the typo \"teh\" in the README yourself.", wire.SpecialistEngineering},
234 } {
235 got, clock := s.asked(t, id, q.text, q.specialist, 10*time.Minute)
236 t.Logf("%s (%s): %s %s in %s: %s", q.name, got.StepID, got.State, got.Reason, clock.Answered.Round(time.Millisecond), got.Answer)
237 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}
238 if got.State != wire.QuestionAnswered {
239 t.Errorf("%s not answered: %+v", q.name, got)
240 }
241 }
242 Witness[t.Name()] = out
243 if k, _ = env.Client.Task(s.ctx, id); k.CandidateSHA != before {
244 t.Fatalf("asking moved the candidate from %s to %s", before, k.CandidateSHA)
245 }
246}
247