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 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/runner/provider/claude"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// cliTranscript is where the pinned CLI (2.1.312) writes a session's
15// transcript, modelled here from task 1's static read (S6) and not from
16// claude.ProjectDir: under $CLAUDE_CONFIG_DIR/projects, in the directory
17// CLAUDE_CODE_PROJECT_DIR_NAME names when CLAUDE_CONFIG_DIR is set and
18// the name is valid, otherwise the cwd's realpath with every character
19// but [A-Za-z0-9] replaced by "-" (short ASCII paths only here).
20func cliTranscript(home, cwd, session string, env map[string]string) string {
21 name := env["CLAUDE_CODE_PROJECT_DIR_NAME"]
22 if env["CLAUDE_CONFIG_DIR"] == "" || name == "" || len(name) > 64 || strings.Trim(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-") != "" {
23 if real, err := filepath.EvalSymlinks(cwd); err == nil {
24 cwd = real
25 }
26 name = strings.Map(func(r rune) rune {
27 if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' {
28 return r
29 }
30 return '-'
31 }, cwd)
32 }
33 return filepath.Join(home, "projects", name, session+".jsonl")
34}
35
36// asLaunch plays this runner as a Mac (darwin) or Linux launch.
37func asLaunch(t *testing.T, darwin bool) {
38 t.Helper()
39 was := darwinLaunch
40 darwinLaunch = darwin
41 t.Cleanup(func() { darwinLaunch = was })
42}
43
44// TestTranscriptPathForRealCwd: the runner looks for a transcript where
45// the CLI puts it. The CLI names the project directory from its cwd's
46// realpath (a /var alias names /private/var), unless the launch pins the
47// name; a Mac launch pins "-workspace", so its transcripts sit where a
48// Linux launch's do, whatever the worktree.
49func TestTranscriptPathForRealCwd(t *testing.T) {
50 env := map[string]string{"CLAUDE_CONFIG_DIR": "/home/runner"}
51 long := "/Users/paulequipo/" + strings.Repeat("a", 250)
52 for cwd, want := range map[string]string{
53 "/workspace": "-workspace",
54 "/Users/paulequipo/x/wt": "-Users-paulequipo-x-wt",
55 "/private/var/folders/ab/T/wt": "-private-var-folders-ab-T-wt",
56 "/Users/José/wt": "-Users-Jos--wt",
57 "/Users/a/\U0001F600/wt": "-Users-a----wt",
58 // 200 characters stay whole; past that, the first 200 and the
59 // base-36 Java hash of the path (values from an independent
60 // implementation of the CLI's rule).
61 "/Users/paulequipo/" + strings.Repeat("c", 182): "-Users-paulequipo-" + strings.Repeat("c", 182),
62 long: "-Users-paulequipo-" + strings.Repeat("a", 182) + "-2yzpv6",
63 "/Users/paulequipo/" + strings.Repeat("b", 183): "-Users-paulequipo-" + strings.Repeat("b", 182) + "-m8crao",
64 } {
65 if got := claude.ProjectDir(cwd, env); got != want {
66 t.Errorf("ProjectDir(%.40q) = %q, want %q", cwd, got, want)
67 }
68 }
69 // The pinned name, only with CLAUDE_CONFIG_DIR and only when valid.
70 pinned := map[string]string{"CLAUDE_CONFIG_DIR": "/home/runner"}
71 claude.PinProjectDir(pinned)
72 if claude.ProjectDirEnv != "CLAUDE_CODE_PROJECT_DIR_NAME" || claude.ProjectDirName != "-workspace" || pinned[claude.ProjectDirEnv] != claude.ProjectDirName {
73 t.Fatalf("pin %v", pinned)
74 }
75 if got := claude.ProjectDir("/Users/paulequipo/x/wt", pinned); got != "-workspace" {
76 t.Fatalf("pinned: %q", got)
77 }
78 if got := claude.ProjectDir("/Users/paulequipo/x/wt", map[string]string{claude.ProjectDirEnv: "-workspace"}); got != "-Users-paulequipo-x-wt" {
79 t.Fatalf("pin without CLAUDE_CONFIG_DIR honoured: %q", got)
80 }
81 for _, bad := range []string{"", "a/b", "a.b", "con", "COM1", "lpt9", strings.Repeat("x", 65)} {
82 if got := claude.ProjectDir("/w", map[string]string{"CLAUDE_CONFIG_DIR": "/h", claude.ProjectDirEnv: bad}); got != "-w" {
83 t.Errorf("invalid pin %q honoured: %q", bad, got)
84 }
85 }
86 // The cwd the CLI names is its realpath.
87 base, _ := filepath.EvalSymlinks(t.TempDir())
88 real := filepath.Join(base, "private", "var", "wt")
89 if err := os.MkdirAll(real, 0700); err != nil {
90 t.Fatal(err)
91 }
92 if err := os.Symlink(filepath.Join(base, "private", "var"), filepath.Join(base, "var")); err != nil {
93 t.Fatal(err)
94 }
95 alias := filepath.Join(base, "var", "wt")
96 if got := claude.RealCwd(alias); got != real {
97 t.Fatalf("RealCwd(%s) = %s", alias, got)
98 }
99 if got := claude.RealCwd("/no/such/dir"); got != "/no/such/dir" {
100 t.Fatalf("RealCwd of a missing dir: %s", got)
101 }
102 if claude.ProjectDir(claude.RealCwd(alias), env) != claude.ProjectDir(real, env) || claude.ProjectDir(alias, env) == claude.ProjectDir(real, env) {
103 t.Fatal("alias and real path name the same project directory")
104 }
105 home := filepath.Join(base, "home")
106 // Linux: the CLI's cwd is /workspace.
107 asLaunch(t, false)
108 if got := transcriptPath(home, claude.LinuxWorkspaceRoot, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") || got != cliTranscript(home, "/workspace", "s", cliProjectEnv()) {
109 t.Fatalf("Linux transcript %s", got)
110 }
111 // A Mac: the real worktree is the cwd, and the name is pinned.
112 asLaunch(t, true)
113 for _, cwd := range []string{real, alias} {
114 if got := transcriptPath(home, cwd, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") || got != cliTranscript(home, cwd, "s", cliProjectEnv()) {
115 t.Fatalf("Mac transcript for %s: %s", cwd, got)
116 }
117 }
118 // Equipo's own store keeps one layout on both.
119 if got := keptTranscript(home, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") {
120 t.Fatalf("kept %s", got)
121 }
122}
123
124// TestResumeAcrossWorktrees: a session kept from worktree A resumes in
125// worktree B (a fix round's new attempt), and again in worktree C after
126// the runner restarts, each time where the CLI looks for it. On Linux
127// every worktree is /workspace inside the namespace.
128func TestResumeAcrossWorktrees(t *testing.T) {
129 for _, darwin := range []bool{true, false} {
130 t.Run(map[bool]string{true: "darwin", false: "linux"}[darwin], func(t *testing.T) {
131 asLaunch(t, darwin)
132 base, _ := filepath.EvalSymlinks(t.TempDir())
133 cwd := map[string]string{}
134 for _, name := range []string{"A", "B", "C"} {
135 cwd[name] = claude.LinuxWorkspaceRoot
136 if darwin {
137 cwd[name] = filepath.Join(base, "steps", "tk1-"+name, "wt")
138 if err := os.MkdirAll(cwd[name], 0700); err != nil {
139 t.Fatal(err)
140 }
141 }
142 }
143 env := cliProjectEnv()
144 ctx := context.Background()
145 if err := os.MkdirAll(filepath.Join(base, "state"), 0700); err != nil {
146 t.Fatal(err)
147 }
148 state, err := OpenState(ctx, filepath.Join(base, "state"))
149 if err != nil {
150 t.Fatal(err)
151 }
152 sessions := filepath.Join(base, "sessions")
153 keep := filepath.Join(sessions, "steps", "s0")
154 // Round 0 runs in A: the CLI writes its transcript, and the
155 // runner keeps it once the CLI has exited.
156 h1 := filepath.Join(base, "run-1")
157 cli := cliTranscript(h1, cwd["A"], "sess-0", env)
158 if cli != filepath.Join(h1, "projects", "-workspace", "sess-0.jsonl") {
159 t.Fatalf("the CLI keeps A's session at %s, not projects/-workspace", cli)
160 }
161 writeLine(t, cli, `{"type":"user","n":1}`)
162 n, err := keepTranscript(h1, cwd["A"], "sess-0", keep)
163 if err != nil || n == 0 {
164 t.Fatalf("keep from A: %d %v", n, err)
165 }
166 round0 := stepRow{DispatchCommand: wire.DispatchCommand{Person: connPerson, TaskID: "tk1", StepID: "s0", Attempt: 1, Specialist: wire.SpecialistEngineering}}
167 if err := state.keepSession(ctx, round0, wire.SpecialistEngineering, "claude-1", "", "", "sess-0", keep, fixBase, n); err != nil {
168 t.Fatal(err)
169 }
170 // Round 1 resumes it in B, in a fresh runtime home.
171 h2 := filepath.Join(base, "run-2")
172 if err := placeTranscript(keep, h2, cwd["B"], "sess-0"); err != nil {
173 t.Fatal(err)
174 }
175 if got := readText(t, cliTranscript(h2, cwd["B"], "sess-0", env)); got != `{"type":"user","n":1}`+"\n" {
176 t.Fatalf("B's CLI finds %q", got)
177 }
178 writeLine(t, cliTranscript(h2, cwd["B"], "sess-0", env), `{"type":"user","n":2}`)
179 if _, err := keepTranscript(h2, cwd["B"], "sess-0", keep); err != nil {
180 t.Fatal(err)
181 }
182 // The runner restarts: a new state handle and engine find the
183 // kept session from the record alone, and C resumes it whole.
184 state.Close()
185 state, err = OpenState(ctx, filepath.Join(base, "state"))
186 if err != nil {
187 t.Fatal(err)
188 }
189 defer state.Close()
190 e := &Engineer{O: ClaudeOptions{State: state, SessionRoot: sessions}}
191 k, ok := e.fixSession(ctx, fixRound(), fixBase)
192 if !ok || k.SessionID != "sess-0" || k.Dir != keep {
193 t.Fatalf("after a restart: %+v %v", k, ok)
194 }
195 h3 := filepath.Join(base, "run-3")
196 if err := placeTranscript(k.Dir, h3, cwd["C"], k.SessionID); err != nil {
197 t.Fatal(err)
198 }
199 at := cliTranscript(h3, cwd["C"], "sess-0", env)
200 if at != filepath.Join(h3, "projects", "-workspace", "sess-0.jsonl") || readText(t, at) != `{"type":"user","n":1}`+"\n"+`{"type":"user","n":2}`+"\n" {
201 t.Fatalf("C's CLI finds %s: %q", at, readText(t, at))
202 }
203 })
204 }
205}
206
207func writeLine(t *testing.T, path, line string) {
208 t.Helper()
209 if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
210 t.Fatal(err)
211 }
212 f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
213 if err != nil {
214 t.Fatal(err)
215 }
216 defer f.Close()
217 if _, err := f.WriteString(line + "\n"); err != nil {
218 t.Fatal(err)
219 }
220}
221
222func readText(t *testing.T, path string) string {
223 t.Helper()
224 raw, err := os.ReadFile(path)
225 if err != nil {
226 return "missing: " + err.Error()
227 }
228 return string(raw)
229}
230