package thin import ( "context" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider/claude" "amadan.net/oficina/equipo/internal/wire" ) // cliTranscript is where the pinned CLI (2.1.312) writes a session's // transcript, modelled here from task 1's static read (S6) and not from // claude.ProjectDir: under $CLAUDE_CONFIG_DIR/projects, in the directory // CLAUDE_CODE_PROJECT_DIR_NAME names when CLAUDE_CONFIG_DIR is set and // the name is valid, otherwise the cwd's realpath with every character // but [A-Za-z0-9] replaced by "-" (short ASCII paths only here). func cliTranscript(home, cwd, session string, env map[string]string) string { name := env["CLAUDE_CODE_PROJECT_DIR_NAME"] if env["CLAUDE_CONFIG_DIR"] == "" || name == "" || len(name) > 64 || strings.Trim(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-") != "" { if real, err := filepath.EvalSymlinks(cwd); err == nil { cwd = real } name = strings.Map(func(r rune) rune { if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' { return r } return '-' }, cwd) } return filepath.Join(home, "projects", name, session+".jsonl") } // asLaunch plays this runner as a Mac (darwin) or Linux launch. func asLaunch(t *testing.T, darwin bool) { t.Helper() was := darwinLaunch darwinLaunch = darwin t.Cleanup(func() { darwinLaunch = was }) } // TestTranscriptPathForRealCwd: the runner looks for a transcript where // the CLI puts it. The CLI names the project directory from its cwd's // realpath (a /var alias names /private/var), unless the launch pins the // name; a Mac launch pins "-workspace", so its transcripts sit where a // Linux launch's do, whatever the worktree. func TestTranscriptPathForRealCwd(t *testing.T) { env := map[string]string{"CLAUDE_CONFIG_DIR": "/home/runner"} long := "/Users/paulequipo/" + strings.Repeat("a", 250) for cwd, want := range map[string]string{ "/workspace": "-workspace", "/Users/paulequipo/x/wt": "-Users-paulequipo-x-wt", "/private/var/folders/ab/T/wt": "-private-var-folders-ab-T-wt", "/Users/José/wt": "-Users-Jos--wt", "/Users/a/\U0001F600/wt": "-Users-a----wt", // 200 characters stay whole; past that, the first 200 and the // base-36 Java hash of the path (values from an independent // implementation of the CLI's rule). "/Users/paulequipo/" + strings.Repeat("c", 182): "-Users-paulequipo-" + strings.Repeat("c", 182), long: "-Users-paulequipo-" + strings.Repeat("a", 182) + "-2yzpv6", "/Users/paulequipo/" + strings.Repeat("b", 183): "-Users-paulequipo-" + strings.Repeat("b", 182) + "-m8crao", } { if got := claude.ProjectDir(cwd, env); got != want { t.Errorf("ProjectDir(%.40q) = %q, want %q", cwd, got, want) } } // The pinned name, only with CLAUDE_CONFIG_DIR and only when valid. pinned := map[string]string{"CLAUDE_CONFIG_DIR": "/home/runner"} claude.PinProjectDir(pinned) if claude.ProjectDirEnv != "CLAUDE_CODE_PROJECT_DIR_NAME" || claude.ProjectDirName != "-workspace" || pinned[claude.ProjectDirEnv] != claude.ProjectDirName { t.Fatalf("pin %v", pinned) } if got := claude.ProjectDir("/Users/paulequipo/x/wt", pinned); got != "-workspace" { t.Fatalf("pinned: %q", got) } if got := claude.ProjectDir("/Users/paulequipo/x/wt", map[string]string{claude.ProjectDirEnv: "-workspace"}); got != "-Users-paulequipo-x-wt" { t.Fatalf("pin without CLAUDE_CONFIG_DIR honoured: %q", got) } for _, bad := range []string{"", "a/b", "a.b", "con", "COM1", "lpt9", strings.Repeat("x", 65)} { if got := claude.ProjectDir("/w", map[string]string{"CLAUDE_CONFIG_DIR": "/h", claude.ProjectDirEnv: bad}); got != "-w" { t.Errorf("invalid pin %q honoured: %q", bad, got) } } // The cwd the CLI names is its realpath. base, _ := filepath.EvalSymlinks(t.TempDir()) real := filepath.Join(base, "private", "var", "wt") if err := os.MkdirAll(real, 0700); err != nil { t.Fatal(err) } if err := os.Symlink(filepath.Join(base, "private", "var"), filepath.Join(base, "var")); err != nil { t.Fatal(err) } alias := filepath.Join(base, "var", "wt") if got := claude.RealCwd(alias); got != real { t.Fatalf("RealCwd(%s) = %s", alias, got) } if got := claude.RealCwd("/no/such/dir"); got != "/no/such/dir" { t.Fatalf("RealCwd of a missing dir: %s", got) } if claude.ProjectDir(claude.RealCwd(alias), env) != claude.ProjectDir(real, env) || claude.ProjectDir(alias, env) == claude.ProjectDir(real, env) { t.Fatal("alias and real path name the same project directory") } home := filepath.Join(base, "home") // Linux: the CLI's cwd is /workspace. asLaunch(t, false) if got := transcriptPath(home, claude.LinuxWorkspaceRoot, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") || got != cliTranscript(home, "/workspace", "s", cliProjectEnv()) { t.Fatalf("Linux transcript %s", got) } // A Mac: the real worktree is the cwd, and the name is pinned. asLaunch(t, true) for _, cwd := range []string{real, alias} { if got := transcriptPath(home, cwd, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") || got != cliTranscript(home, cwd, "s", cliProjectEnv()) { t.Fatalf("Mac transcript for %s: %s", cwd, got) } } // Equipo's own store keeps one layout on both. if got := keptTranscript(home, "s"); got != filepath.Join(home, "projects", "-workspace", "s.jsonl") { t.Fatalf("kept %s", got) } } // TestResumeAcrossWorktrees: a session kept from worktree A resumes in // worktree B (a fix round's new attempt), and again in worktree C after // the runner restarts, each time where the CLI looks for it. On Linux // every worktree is /workspace inside the namespace. func TestResumeAcrossWorktrees(t *testing.T) { for _, darwin := range []bool{true, false} { t.Run(map[bool]string{true: "darwin", false: "linux"}[darwin], func(t *testing.T) { asLaunch(t, darwin) base, _ := filepath.EvalSymlinks(t.TempDir()) cwd := map[string]string{} for _, name := range []string{"A", "B", "C"} { cwd[name] = claude.LinuxWorkspaceRoot if darwin { cwd[name] = filepath.Join(base, "steps", "tk1-"+name, "wt") if err := os.MkdirAll(cwd[name], 0700); err != nil { t.Fatal(err) } } } env := cliProjectEnv() ctx := context.Background() if err := os.MkdirAll(filepath.Join(base, "state"), 0700); err != nil { t.Fatal(err) } state, err := OpenState(ctx, filepath.Join(base, "state")) if err != nil { t.Fatal(err) } sessions := filepath.Join(base, "sessions") keep := filepath.Join(sessions, "steps", "s0") // Round 0 runs in A: the CLI writes its transcript, and the // runner keeps it once the CLI has exited. h1 := filepath.Join(base, "run-1") cli := cliTranscript(h1, cwd["A"], "sess-0", env) if cli != filepath.Join(h1, "projects", "-workspace", "sess-0.jsonl") { t.Fatalf("the CLI keeps A's session at %s, not projects/-workspace", cli) } writeLine(t, cli, `{"type":"user","n":1}`) n, err := keepTranscript(h1, cwd["A"], "sess-0", keep) if err != nil || n == 0 { t.Fatalf("keep from A: %d %v", n, err) } round0 := stepRow{DispatchCommand: wire.DispatchCommand{Person: connPerson, TaskID: "tk1", StepID: "s0", Attempt: 1, Specialist: wire.SpecialistEngineering}} if err := state.keepSession(ctx, round0, wire.SpecialistEngineering, "claude-1", "", "", "sess-0", keep, fixBase, n); err != nil { t.Fatal(err) } // Round 1 resumes it in B, in a fresh runtime home. h2 := filepath.Join(base, "run-2") if err := placeTranscript(keep, h2, cwd["B"], "sess-0"); err != nil { t.Fatal(err) } if got := readText(t, cliTranscript(h2, cwd["B"], "sess-0", env)); got != `{"type":"user","n":1}`+"\n" { t.Fatalf("B's CLI finds %q", got) } writeLine(t, cliTranscript(h2, cwd["B"], "sess-0", env), `{"type":"user","n":2}`) if _, err := keepTranscript(h2, cwd["B"], "sess-0", keep); err != nil { t.Fatal(err) } // The runner restarts: a new state handle and engine find the // kept session from the record alone, and C resumes it whole. state.Close() state, err = OpenState(ctx, filepath.Join(base, "state")) if err != nil { t.Fatal(err) } defer state.Close() e := &Engineer{O: ClaudeOptions{State: state, SessionRoot: sessions}} k, ok := e.fixSession(ctx, fixRound(), fixBase) if !ok || k.SessionID != "sess-0" || k.Dir != keep { t.Fatalf("after a restart: %+v %v", k, ok) } h3 := filepath.Join(base, "run-3") if err := placeTranscript(k.Dir, h3, cwd["C"], k.SessionID); err != nil { t.Fatal(err) } at := cliTranscript(h3, cwd["C"], "sess-0", env) if at != filepath.Join(h3, "projects", "-workspace", "sess-0.jsonl") || readText(t, at) != `{"type":"user","n":1}`+"\n"+`{"type":"user","n":2}`+"\n" { t.Fatalf("C's CLI finds %s: %q", at, readText(t, at)) } }) } } func writeLine(t *testing.T, path, line string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { t.Fatal(err) } defer f.Close() if _, err := f.WriteString(line + "\n"); err != nil { t.Fatal(err) } } func readText(t *testing.T, path string) string { t.Helper() raw, err := os.ReadFile(path) if err != nil { return "missing: " + err.Error() } return string(raw) }