oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner"
13 "amadan.net/oficina/equipo/internal/runner/connect"
14 "amadan.net/oficina/equipo/internal/runner/provider/claude"
15 "amadan.net/oficina/equipo/internal/wire"
16)
17
18// fixRig is a runner with two healthy Claude connections of the
19// person's (claude-1 nearly spent, claude-2 roomy) and Engineering's
20// kept session of round 0, made on claude-1, for commit fixBase.
21type fixRig struct {
22 t *testing.T
23 state *State
24 conns *Conns
25 e *Engineer
26 kept string
27}
28
29const fixBase = "0123456789abcdef0123456789abcdef01234567"
30
31func newFixRig(t *testing.T) *fixRig {
32 dir := t.TempDir()
33 bindings := filepath.Join(dir, "bindings.json")
34 if err := os.MkdirAll(filepath.Join(dir, "state"), 0700); err != nil {
35 t.Fatal(err)
36 }
37 state, err := OpenState(context.Background(), filepath.Join(dir, "state"))
38 if err != nil {
39 t.Fatal(err)
40 }
41 t.Cleanup(func() { state.Close() })
42 var bs []map[string]any
43 for _, id := range []string{"claude-1", "claude-2"} {
44 home := filepath.Join(dir, "accounts", id)
45 if err := os.MkdirAll(home, 0700); err != nil {
46 t.Fatal(err)
47 }
48 identity := map[string]string{"account_id": id + "@example.com", "home_id": "h-" + id, "provider_version": "1"}
49 bs = append(bs, map[string]any{"id": id, "local_id": id, "label": id, "person_id": connPerson, "provider": "claude", "family": "claude", "model": "claude-opus-5-5", "account_id": id + "@example.com", "provider_home": home, "kind": "subscription", "expected_identity": identity})
50 }
51 raw, _ := json.Marshal(bs)
52 if err := os.WriteFile(bindings, raw, 0600); err != nil {
53 t.Fatal(err)
54 }
55 cfg := runner.Config{AccountBindingsPath: bindings, ClaudePath: "/bin/true", CodexPath: "/bin/true"}
56 conns := &Conns{Runner: cfg, State: state}
57 for id, used := range map[string]float64{"claude-1": 0.9, "claude-2": 0.1} {
58 conns.Validated(id)
59 conns.Observe(id, connect.Limit{Headroom: &wire.Headroom{Utilization: &used}}, "")
60 }
61 r := &fixRig{t: t, state: state, conns: conns, kept: filepath.Join(dir, "sessions", "steps", "s0")}
62 r.e = &Engineer{O: ClaudeOptions{Runner: cfg, State: state, SessionRoot: filepath.Join(dir, "sessions")}, Conns: conns}
63 round0 := stepRow{DispatchCommand: wire.DispatchCommand{Person: connPerson, TaskID: "tk1", StepID: "s0", Attempt: 1, Specialist: wire.SpecialistEngineering}}
64 if err := state.keepSession(context.Background(), round0, wire.SpecialistEngineering, "claude-1", "", "", "sess-0", r.kept, fixBase, 10); err != nil {
65 t.Fatal(err)
66 }
67 path := keptTranscript(r.kept, "sess-0")
68 if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
69 t.Fatal(err)
70 }
71 if err := os.WriteFile(path, []byte(`{"type":"user"}`+"\n"), 0600); err != nil {
72 t.Fatal(err)
73 }
74 return r
75}
76
77// round is the dispatch of Engineering's fix round 1 on fixBase.
78func fixRound() stepRow {
79 return stepRow{DispatchCommand: wire.DispatchCommand{Person: connPerson, TaskID: "tk1", StepID: "s1", Attempt: 1, Specialist: wire.SpecialistEngineering, Round: 1, BaseSHA: fixBase, Feedback: "Fix round 1 of 2. Review asked for changes."}, Base: fixBase}
80}
81
82// plan is where the round runs and whether it resumes.
83func (r *fixRig) plan(row stepRow, avoid []string) (string, bool) {
84 r.t.Helper()
85 kept, ok := r.e.fixSession(context.Background(), row, row.Base)
86 b, resume, hold, _, err := r.e.fixPlan(context.Background(), row, avoid, kept, ok)
87 if err != nil {
88 r.t.Fatal(err)
89 }
90 if hold != nil {
91 r.t.Fatalf("held: %+v", hold)
92 }
93 if resume && kept.SessionID != "sess-0" {
94 r.t.Fatalf("resumes %+v", kept)
95 }
96 return b.LocalID, resume
97}
98
99// A fix round resumes the kept session of the round that made its
100// commit, on that session's connection, while that connection is
101// healthy, even when another has more headroom (the warm cache). When
102// the connection cannot run it, the session is gone or the round is
103// not one a session can carry, it starts fresh where Choose puts it.
104func TestFixRoundResumesOnlyOnItsHealthyConnection(t *testing.T) {
105 r := newFixRig(t)
106 if on, resume := r.plan(fixRound(), nil); on != "claude-1" || !resume {
107 t.Fatalf("healthy: on %s resume %v", on, resume)
108 }
109 // Refused in this attempt already (it failed over): fresh, elsewhere.
110 if on, resume := r.plan(fixRound(), []string{"claude-1"}); on != "claude-2" || resume {
111 t.Fatalf("after a refusal: on %s resume %v", on, resume)
112 }
113 // Not a round a session carries: no feedback (an older instance), a
114 // rebase, a republish, the first round, another commit.
115 for name, change := range map[string]func(*stepRow){
116 "no feedback": func(s *stepRow) { s.Feedback = "" },
117 "rebase": func(s *stepRow) { s.Rebase = true },
118 "republish": func(s *stepRow) { s.Republish = true },
119 "round 0": func(s *stepRow) { s.Round = 0 },
120 "other base": func(s *stepRow) { s.Base = strings.Repeat("f", 40) },
121 } {
122 row := fixRound()
123 change(&row)
124 if on, resume := r.plan(row, nil); on != "claude-2" || resume {
125 t.Fatalf("%s: on %s resume %v", name, on, resume)
126 }
127 }
128 // Another task's or person's session is never resumed.
129 row := fixRound()
130 row.TaskID = "tk2"
131 if _, resume := r.plan(row, nil); resume {
132 t.Fatal("resumed another task's session")
133 }
134 // The kept connection limited: failover wins over the cache.
135 r.conns.Limit("claude-1", connect.Limit{Limited: true, ResetsAt: time.Now().Add(time.Hour)})
136 if on, resume := r.plan(fixRound(), nil); on != "claude-2" || resume {
137 t.Fatalf("limited: on %s resume %v", on, resume)
138 }
139 r.conns.Observe("claude-1", connect.Limit{}, "")
140 if on, resume := r.plan(fixRound(), nil); on != "claude-1" || !resume {
141 t.Fatalf("back: on %s resume %v", on, resume)
142 }
143 // Signed out (sign in again): fresh, elsewhere.
144 r.conns.SignInAgain("claude-1")
145 if on, resume := r.plan(fixRound(), nil); on != "claude-2" || resume {
146 t.Fatalf("sign in again: on %s resume %v", on, resume)
147 }
148 r.conns.Validated("claude-1")
149 // Unhealthy by the last refresh: fresh.
150 r.e.Healthy = func(id string) bool { return id != "claude-1" }
151 if _, resume := r.plan(fixRound(), nil); resume {
152 t.Fatal("resumed on an unhealthy connection")
153 }
154 r.e.Healthy = nil
155 // The session gone: fresh.
156 if err := os.Remove(keptTranscript(r.kept, "sess-0")); err != nil {
157 t.Fatal(err)
158 }
159 if on, resume := r.plan(fixRound(), nil); on != "claude-2" || resume {
160 t.Fatalf("session gone: on %s resume %v", on, resume)
161 }
162}
163
164// The resumed message carries the feedback and the same rules and
165// reply, not the whole brief again.
166func TestFixPromptIsTheNextMessage(t *testing.T) {
167 e := &Engineer{}
168 row := fixRound()
169 row.Brief, row.Branch = "The original brief.", "equipo/tk1-x"
170 p := e.fixPrompt(row, claude.LinuxWorkspaceRoot)
171 for _, want := range []string{"[Equipo: engineering]", row.Feedback, "equipo/tk1-x", "do not run git", "Headline: "} {
172 if !strings.Contains(p, want) {
173 t.Fatalf("prompt lacks %q:\n%s", want, p)
174 }
175 }
176 if strings.Contains(p, row.Brief) {
177 t.Fatalf("prompt repeats the brief:\n%s", p)
178 }
179}
180