| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 15 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 16 | "amadan.net/oficina/equipo/internal/wire" |
| 17 | ) |
| 18 | |
| 19 | // pathForge serves the index: every repository is a local bare repository, |
| 20 | // read over file:// like the local forge in the headless suite. |
| 21 | type pathForge struct { |
| 22 | countingForge |
| 23 | root string |
| 24 | } |
| 25 | |
| 26 | func (f *pathForge) path(repo string) string { |
| 27 | return filepath.Join(f.root, strings.ReplaceAll(strings.TrimPrefix(repo, "amadan.net/"), "/", "__")+".git") |
| 28 | } |
| 29 | |
| 30 | func (f *pathForge) Remote(_ context.Context, repo string, _ Use) (workspace.Remote, error) { |
| 31 | if !slices.Contains(f.repos, repo) { |
| 32 | return workspace.Remote{}, workspace.ErrGitPassword |
| 33 | } |
| 34 | return workspace.Remote{URL: f.path(repo)}, nil |
| 35 | } |
| 36 | |
| 37 | // A reachable repository with no commits yet is in the catalog, marked |
| 38 | // empty: it is not an access failure. A repository with commits but no |
| 39 | // branch at HEAD, and one the forge cannot serve, stay out as before. |
| 40 | // The first commit is seen on the next delta, and reads of an empty |
| 41 | // repository say it is empty rather than failing. |
| 42 | func TestIndexListsEmptyRepository(t *testing.T) { |
| 43 | root := t.TempDir() |
| 44 | f := &pathForge{root: filepath.Join(root, "forge")} |
| 45 | const ( |
| 46 | full = "amadan.net/oficina/sample" |
| 47 | empty = "amadan.net/paulca/threatmin" |
| 48 | headless = "amadan.net/paulca/detached" |
| 49 | gone = "amadan.net/paulca/gone" |
| 50 | ) |
| 51 | f.repos = []string{full, empty, headless, gone} |
| 52 | seed := filepath.Join(root, "seed") |
| 53 | os.MkdirAll(seed, 0700) |
| 54 | os.WriteFile(filepath.Join(seed, "README.md"), []byte("# Sample\n\nA sample.\n"), 0600) |
| 55 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 56 | hostileGit(t, seed, "add", "-A") |
| 57 | hostileGit(t, seed, "commit", "-q", "-m", "seed") |
| 58 | os.MkdirAll(f.root, 0700) |
| 59 | hostileGit(t, root, "clone", "-q", "--bare", seed, f.path(full)) |
| 60 | hostileGit(t, root, "init", "-q", "--bare", "-b", "main", f.path(empty)) |
| 61 | // Commits on dev only, HEAD naming a main that does not exist. |
| 62 | hostileGit(t, root, "clone", "-q", "--bare", seed, f.path(headless)) |
| 63 | hostileGit(t, f.path(headless), "branch", "-m", "main", "dev") |
| 64 | hostileGit(t, f.path(headless), "symbolic-ref", "HEAD", "refs/heads/main") |
| 65 | |
| 66 | os.MkdirAll(filepath.Join(root, "work"), 0700) |
| 67 | x := &Index{Forge: f, Clones: &Clones{Root: filepath.Join(root, "work"), Git: "git", Forge: f}} |
| 68 | ctx := context.Background() |
| 69 | x.Refresh(ctx) |
| 70 | byRepo := map[string]wire.RepoInfo{} |
| 71 | for _, e := range x.Catalog() { |
| 72 | byRepo[e.Repo] = e |
| 73 | } |
| 74 | if e, ok := byRepo[full]; !ok || e.Empty || e.DefaultBranch != "main" { |
| 75 | t.Fatalf("full repository: %+v", e) |
| 76 | } |
| 77 | e, ok := byRepo[empty] |
| 78 | if !ok || !e.Empty || e.DefaultBranch != "" || e.HeadSHA != "" || !slices.Equal(e.Names, []string{"threatmin"}) || e.Hints == nil { |
| 79 | t.Fatalf("empty repository: %+v (present %v)", e, ok) |
| 80 | } |
| 81 | if _, ok := byRepo[headless]; ok { |
| 82 | t.Fatal("a repository with commits but no default branch is listed") |
| 83 | } |
| 84 | if _, ok := byRepo[gone]; ok { |
| 85 | t.Fatal("a repository the forge cannot serve is listed") |
| 86 | } |
| 87 | for _, read := range []func() (string, error){ |
| 88 | func() (string, error) { return x.ReadFile(ctx, empty, "README.md", 1) }, |
| 89 | func() (string, error) { return x.SearchCode(ctx, empty, "x") }, |
| 90 | } { |
| 91 | if _, err := read(); !errors.Is(err, ErrEmptyRepo) { |
| 92 | t.Fatalf("read of an empty repository: %v", err) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // The first commit arrives; a delta (what a grant check or a new |
| 97 | // repository kicks) sees it without waiting for the hourly refresh. |
| 98 | hostileGit(t, seed, "push", "-q", f.path(empty), "main") |
| 99 | x.Delta(ctx) |
| 100 | for _, e := range x.Catalog() { |
| 101 | if e.Repo == empty && (e.Empty || e.DefaultBranch != "main" || len(e.HeadSHA) != 40) { |
| 102 | t.Fatalf("after the first commit: %+v", e) |
| 103 | } |
| 104 | } |
| 105 | if out, err := x.ReadFile(ctx, empty, "README.md", 1); err != nil || !strings.Contains(out, "1\t# Sample") { |
| 106 | t.Fatalf("read after the first commit: %q %v", out, err) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // The coordinator's read tools on an empty repository: the instance's |
| 111 | // "empty" answer reaches the model as it is, not as a failed read; and if |
| 112 | // the instance authorised a read the runner's own index finds empty, the |
| 113 | // model is told it is empty, never "Could not read that". |
| 114 | func TestBridgeReadsOfEmptyRepository(t *testing.T) { |
| 115 | const repo = "amadan.net/paulca/threatmin" |
| 116 | x := &Index{Clones: &Clones{Root: t.TempDir(), Git: "git"}} |
| 117 | x.catalog = []wire.RepoInfo{{Repo: repo, Names: []string{"threatmin"}, Hints: []string{}, Empty: true}} |
| 118 | answer := wire.ToolResult{Code: "empty", Repo: repo, Text: "paulca/threatmin has no commits yet."} |
| 119 | b := &bridge{Index: x, trip: func(context.Context, wire.ToolCall) wire.ToolResult { return answer }} |
| 120 | for _, name := range []string{wire.ToolReadFile, wire.ToolSearchCode} { |
| 121 | args := `{"repo":"paulca/threatmin","path":"README.md"}` |
| 122 | if name == wire.ToolSearchCode { |
| 123 | args = `{"repo":"paulca/threatmin","query":"x"}` |
| 124 | } |
| 125 | if text, isErr := b.Call(context.Background(), "p", "t", 1, name, "u1", json.RawMessage(args)); isErr || text != answer.Text { |
| 126 | t.Fatalf("%s, instance says empty: %q %v", name, text, isErr) |
| 127 | } |
| 128 | } |
| 129 | answer = wire.ToolResult{Code: "read", Repo: repo} |
| 130 | text, isErr := b.Call(context.Background(), "p", "t", 1, wire.ToolReadFile, "u2", json.RawMessage(`{"repo":"paulca/threatmin","path":"README.md"}`)) |
| 131 | if isErr || text != toolEmptyRead || text == toolReadError { |
| 132 | t.Fatalf("runner finds it empty: %q %v", text, isErr) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Engineering's first task in an empty repository pushes only its own |
| 137 | // task branch: the default branch still has no commits, so the |
| 138 | // repository stays empty for the index and its reads. A branch that is |
| 139 | // not Equipo's (someone pushed elsewhere) is history to read, as before. |
| 140 | func TestEmptyRepositoryWithTaskBranch(t *testing.T) { |
| 141 | root := t.TempDir() |
| 142 | f := &pathForge{root: filepath.Join(root, "forge")} |
| 143 | const ( |
| 144 | empty = "amadan.net/paulca/barber" |
| 145 | other = "amadan.net/paulca/other" |
| 146 | ) |
| 147 | f.repos = []string{empty, other} |
| 148 | os.MkdirAll(f.root, 0700) |
| 149 | seed := filepath.Join(root, "seed") |
| 150 | os.MkdirAll(seed, 0700) |
| 151 | os.WriteFile(filepath.Join(seed, "index.html"), []byte("<h1>Barber</h1>\n"), 0600) |
| 152 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 153 | hostileGit(t, seed, "add", "-A") |
| 154 | hostileGit(t, seed, "commit", "-q", "-m", "mockups") |
| 155 | for _, repo := range []string{empty, other} { |
| 156 | hostileGit(t, root, "init", "-q", "--bare", "-b", "main", f.path(repo)) |
| 157 | } |
| 158 | hostileGit(t, seed, "push", "-q", f.path(empty), "main:refs/heads/equipo/tk1-barber-mockups") |
| 159 | hostileGit(t, seed, "push", "-q", f.path(other), "main:refs/heads/dev") |
| 160 | os.MkdirAll(filepath.Join(root, "work"), 0700) |
| 161 | c := &Clones{Root: filepath.Join(root, "work"), Git: "git", Forge: f} |
| 162 | ctx := context.Background() |
| 163 | if _, err := c.DefaultBranch(ctx, empty); !errors.Is(err, ErrEmptyRepo) { |
| 164 | t.Fatalf("empty but for a task branch: %v", err) |
| 165 | } |
| 166 | if _, err := c.DefaultBranch(ctx, other); !errors.Is(err, ErrNoCreate) { |
| 167 | t.Fatalf("another branch beside an unborn default: %v", err) |
| 168 | } |
| 169 | x := &Index{Forge: f, Clones: c} |
| 170 | x.Refresh(ctx) |
| 171 | cat := x.Catalog() |
| 172 | if len(cat) != 1 || cat[0].Repo != empty || !cat[0].Empty { |
| 173 | t.Fatalf("catalog %+v", cat) |
| 174 | } |
| 175 | if _, err := x.ReadFile(ctx, empty, "index.html", 1); !errors.Is(err, ErrEmptyRepo) { |
| 176 | t.Fatalf("read: %v", err) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Review reads a change as its diff: an initial commit (no base) is |
| 181 | // read against the empty tree, so Review sees every file it adds. With |
| 182 | // no base and a commit that has a parent there is nothing to say. |
| 183 | func TestChangeDiffOfInitialCommit(t *testing.T) { |
| 184 | root := t.TempDir() |
| 185 | gitDir := filepath.Join(root, "repo.git") |
| 186 | hostileGit(t, root, "init", "-q", "--bare", "-b", "main", gitDir) |
| 187 | seed := filepath.Join(root, "seed") |
| 188 | os.MkdirAll(seed, 0700) |
| 189 | os.WriteFile(filepath.Join(seed, "index.html"), []byte("<h1>Barber</h1>\n"), 0600) |
| 190 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 191 | hostileGit(t, seed, "add", "-A") |
| 192 | hostileGit(t, seed, "commit", "-q", "-m", "mockups") |
| 193 | os.WriteFile(filepath.Join(seed, "book.html"), []byte("<a href=index.html>Back</a>\n"), 0600) |
| 194 | hostileGit(t, seed, "add", "-A") |
| 195 | hostileGit(t, seed, "commit", "-q", "-m", "book") |
| 196 | hostileGit(t, seed, "push", "-q", gitDir, "main") |
| 197 | out, _ := exec.Command("git", "--git-dir="+gitDir, "rev-parse", "main", "main^").Output() |
| 198 | shas := strings.Fields(string(out)) |
| 199 | ctx := context.Background() |
| 200 | diff, err := changeDiff(ctx, "git", gitDir, "", shas[1], wire.MaxDiffBytes) |
| 201 | if err != nil || !strings.Contains(diff, "+<h1>Barber</h1>") || !strings.Contains(diff, "new file mode") { |
| 202 | t.Fatalf("initial commit diff %q %v", diff, err) |
| 203 | } |
| 204 | if diff, err := changeDiff(ctx, "git", gitDir, "", shas[0], wire.MaxDiffBytes); err != nil || diff != "" { |
| 205 | t.Fatalf("no base, not an initial commit: %q %v", diff, err) |
| 206 | } |
| 207 | if diff, err := changeDiff(ctx, "git", gitDir, shas[1], shas[0], wire.MaxDiffBytes); err != nil || !strings.Contains(diff, "+<a href=index.html>Back</a>") || strings.Contains(diff, "Barber") { |
| 208 | t.Fatalf("with a base %q %v", diff, err) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Engineering is told when the repository has no commits: /workspace |
| 213 | // starts empty and its work is the first commit, not "the tip of its |
| 214 | // default branch". |
| 215 | func TestStepPromptEmptyRepository(t *testing.T) { |
| 216 | e := &Engineer{} |
| 217 | r := stepRow{DispatchCommand: wire.DispatchCommand{Person: "owner@example.com", Repo: "amadan.net/paulca/barber", Title: "Mockups", Branch: "equipo/tk1-mockups"}} |
| 218 | empty, full := e.stepPrompt(r, true, claude.LinuxWorkspaceRoot), e.stepPrompt(r, false, claude.LinuxWorkspaceRoot) |
| 219 | if !strings.Contains(empty, stepEmptyRepo) || strings.Contains(empty, "tip of its default branch") { |
| 220 | t.Fatalf("empty: %s", empty) |
| 221 | } |
| 222 | if strings.Contains(full, stepEmptyRepo) || !strings.Contains(full, "at the tip of its default branch. Make the smallest change") { |
| 223 | t.Fatalf("full: %s", full) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Merge inspection in an empty repository: the default branch is unborn, |
| 228 | // so there is no tip and nothing landed; the task branch's head is read |
| 229 | // as usual. A hub whose HEAD a rebuild pointed at the task branch itself |
| 230 | // still has an unborn default: a task branch is never the default, for |
| 231 | // the merge, the index and Engineering alike. |
| 232 | func TestUnbornDefaultForMerge(t *testing.T) { |
| 233 | root := t.TempDir() |
| 234 | f := &pathForge{root: filepath.Join(root, "forge")} |
| 235 | const repo, branch = "amadan.net/paulca/barber", "equipo/tk1-barber-mockups" |
| 236 | f.repos = []string{repo} |
| 237 | os.MkdirAll(f.root, 0700) |
| 238 | seed := filepath.Join(root, "seed") |
| 239 | os.MkdirAll(seed, 0700) |
| 240 | os.WriteFile(filepath.Join(seed, "index.html"), []byte("<h1>Barber</h1>\n"), 0600) |
| 241 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 242 | hostileGit(t, seed, "add", "-A") |
| 243 | hostileGit(t, seed, "commit", "-q", "-m", "mockups") |
| 244 | hostileGit(t, root, "init", "-q", "--bare", "-b", "main", f.path(repo)) |
| 245 | hostileGit(t, seed, "push", "-q", f.path(repo), "main:refs/heads/"+branch) |
| 246 | out, _ := exec.Command("git", "-C", seed, "rev-parse", "HEAD").Output() |
| 247 | commit := strings.TrimSpace(string(out)) |
| 248 | os.MkdirAll(filepath.Join(root, "work"), 0700) |
| 249 | c := &Clones{Root: filepath.Join(root, "work"), Git: "git", Forge: f} |
| 250 | e := &Engineer{Clones: c, Forge: f} |
| 251 | ctx := context.Background() |
| 252 | for _, rebuilt := range []bool{false, true} { |
| 253 | if rebuilt { |
| 254 | hostileGit(t, f.path(repo), "symbolic-ref", "HEAD", "refs/heads/"+branch) |
| 255 | } |
| 256 | def, tip, heads, err := c.Heads(ctx, repo, []string{branch}) |
| 257 | if !errors.Is(err, ErrEmptyRepo) || def != "main" || tip != "" || heads[branch] != commit { |
| 258 | t.Fatalf("rebuilt=%v heads: %q %q %v %v", rebuilt, def, tip, heads, err) |
| 259 | } |
| 260 | st, err := e.inspectMerge(ctx, repo, branch, commit) |
| 261 | if err != nil || !st.Unborn || st.Landed || st.Tip != "" || st.Head != commit || st.Default != "main" { |
| 262 | t.Fatalf("rebuilt=%v inspect %+v %v", rebuilt, st, err) |
| 263 | } |
| 264 | if _, err := c.DefaultBranch(ctx, repo); !errors.Is(err, ErrEmptyRepo) { |
| 265 | t.Fatalf("rebuilt=%v default branch: %v", rebuilt, err) |
| 266 | } |
| 267 | } |
| 268 | // The first merge made main: no longer empty, for the merge and reads. |
| 269 | hostileGit(t, f.path(repo), "update-ref", "refs/heads/main", commit) |
| 270 | hostileGit(t, f.path(repo), "symbolic-ref", "HEAD", "refs/heads/main") |
| 271 | st, err := e.inspectMerge(ctx, repo, branch, commit) |
| 272 | if err != nil || st.Unborn || !st.Landed || st.Default != "main" { |
| 273 | t.Fatalf("after the first merge %+v %v", st, err) |
| 274 | } |
| 275 | if b, err := c.DefaultBranch(ctx, repo); err != nil || b != "main" { |
| 276 | t.Fatalf("default after the first merge: %q %v", b, err) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // amadan-13's threatmin, verbatim: after the first task's push and a hub |
| 281 | // rebuild, ls-remote --symref answers |
| 282 | // |
| 283 | // ref: refs/heads/equipo/tk5fen4uajpn-… HEAD |
| 284 | // 47a897b… HEAD |
| 285 | // 47a897b… refs/heads/equipo/tk5fen4uajpn-… |
| 286 | // |
| 287 | // and no refs/heads/main. Merge inspection reads that in one ls-remote |
| 288 | // (the hub limits an agent's git requests): HEAD names a task branch and |
| 289 | // main is absent, so the default is unborn, named main, with the task |
| 290 | // branch's head as read. HEAD naming nothing (an unborn main) is the |
| 291 | // same. When main exists it is the default, wherever HEAD points. |
| 292 | func TestHeadsReadsAnUnbornDefaultInOneCall(t *testing.T) { |
| 293 | root := t.TempDir() |
| 294 | f := &pathForge{root: filepath.Join(root, "forge")} |
| 295 | const repo, branch = "amadan.net/paulca/threatmin", "equipo/tk5fen4uajpn-create-two-clickable-barber-app-mockups" |
| 296 | f.repos = []string{repo} |
| 297 | os.MkdirAll(f.root, 0700) |
| 298 | seed := filepath.Join(root, "seed") |
| 299 | os.MkdirAll(seed, 0700) |
| 300 | os.WriteFile(filepath.Join(seed, "index.html"), []byte("<h1>Barber</h1>\n"), 0600) |
| 301 | hostileGit(t, seed, "init", "-q", "-b", "main") |
| 302 | hostileGit(t, seed, "add", "-A") |
| 303 | hostileGit(t, seed, "commit", "-q", "-m", "mockups") |
| 304 | out, _ := exec.Command("git", "-C", seed, "rev-parse", "HEAD").Output() |
| 305 | sha := strings.TrimSpace(string(out)) |
| 306 | bare := f.path(repo) |
| 307 | hostileGit(t, root, "init", "-q", "--bare", "-b", "main", bare) |
| 308 | hostileGit(t, seed, "push", "-q", bare, "main:refs/heads/"+branch) |
| 309 | // A git that counts its ls-remote calls. |
| 310 | calls := filepath.Join(root, "calls") |
| 311 | git := filepath.Join(root, "git") |
| 312 | real, _ := exec.LookPath("git") |
| 313 | os.WriteFile(git, []byte("#!/bin/sh\nfor a in \"$@\"; do [ \"$a\" = ls-remote ] && echo x >> "+calls+"; done\nexec "+real+" \"$@\"\n"), 0700) |
| 314 | count := func() int { raw, _ := os.ReadFile(calls); os.Remove(calls); return strings.Count(string(raw), "x") } |
| 315 | os.MkdirAll(filepath.Join(root, "work"), 0700) |
| 316 | c := &Clones{Root: filepath.Join(root, "work"), Git: git, Forge: f} |
| 317 | e := &Engineer{Clones: c, Forge: f} |
| 318 | ctx := context.Background() |
| 319 | for _, rebuilt := range []bool{true, false} { |
| 320 | if rebuilt { |
| 321 | hostileGit(t, bare, "symbolic-ref", "HEAD", "refs/heads/"+branch) |
| 322 | ls, _ := exec.Command("git", "ls-remote", "--symref", bare).Output() |
| 323 | want := "ref: refs/heads/" + branch + "\tHEAD\n" + sha + "\tHEAD\n" + sha + "\trefs/heads/" + branch + "\n" |
| 324 | if string(ls) != want { |
| 325 | t.Fatalf("not amadan-13's answer:\n%s", ls) |
| 326 | } |
| 327 | } else { |
| 328 | hostileGit(t, bare, "symbolic-ref", "HEAD", "refs/heads/main") |
| 329 | } |
| 330 | count() |
| 331 | def, tip, heads, err := c.Heads(ctx, repo, []string{branch}) |
| 332 | if n := count(); !errors.Is(err, ErrEmptyRepo) || def != "main" || tip != "" || heads[branch] != sha || n != 1 { |
| 333 | t.Fatalf("rebuilt=%v heads: %q %q %v %v in %d ls-remote calls", rebuilt, def, tip, heads, err, n) |
| 334 | } |
| 335 | st, err := e.inspectMerge(ctx, repo, branch, sha) |
| 336 | if n := count(); err != nil || !st.Unborn || st.Default != "main" || st.Head != sha || st.Landed || n != 1 { |
| 337 | t.Fatalf("rebuilt=%v inspect %+v %v in %d ls-remote calls", rebuilt, st, err, n) |
| 338 | } |
| 339 | } |
| 340 | // The first merge made main and kept the task branch (branch_closed: |
| 341 | // false), HEAD still at the task branch: main is the default, landed. |
| 342 | hostileGit(t, bare, "update-ref", "refs/heads/main", sha) |
| 343 | hostileGit(t, bare, "symbolic-ref", "HEAD", "refs/heads/"+branch) |
| 344 | def, tip, heads, err := c.Heads(ctx, repo, []string{branch}) |
| 345 | if err != nil || def != "main" || tip != sha || heads[branch] != sha { |
| 346 | t.Fatalf("main beside a kept branch: %q %q %v %v", def, tip, heads, err) |
| 347 | } |
| 348 | e.O.Runner.GitPath = git |
| 349 | if st, err := e.inspectMerge(ctx, repo, branch, sha); err != nil || st.Unborn || !st.Landed || st.Default != "main" || st.MergeSHA != sha { |
| 350 | t.Fatalf("the kept branch reads as merged: %+v %v", st, err) |
| 351 | } |
| 352 | } |
| 353 | |