Files
archie/internal/worktree/worktree_test.go
T
sam 37792c9ec0 fix(worktree): force checkout so retry recovers from a dirty worktree
refresh checked out the task branch without Force before hard-resetting
to base. A prior attempt's leftover uncommitted changes -- a killed
container, or a stage that edits files before ever reaching a commit --
made go-git's default MergeReset checkout refuse to reconcile them, even
with HEAD already on the correct branch and the target commit unmoved.
The fallback then made it worse: any checkout failure was assumed to
mean the branch didn't exist yet, so it retried with Create: true, which
failed with a much more confusing error since the branch did exist --
"a branch named ... already exists" -- masking the real cause and
parking the retry with the same unhelpful message every time.

Reproduced directly against a live deployment's actual broken worktree
(modified tracked file, untracked file, branch already checked out) and
confirmed retry then succeeds and correctly restores to base.

Force is safe unconditionally here: refresh hard-resets to base a few
lines later regardless of what checkout leaves behind.

Closes #523
2026-08-15 11:22:22 +10:00

621 lines
20 KiB
Go

package worktree
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/go-git/go-git/v6"
gitconfig "github.com/go-git/go-git/v6/config"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/object"
"github.com/samcharles93/archie-core/internal/container"
)
const testBase = "main"
func testSignature() *object.Signature {
return &object.Signature{
Name: "seeder",
Email: "seed@example.com",
When: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
}
}
// newLocalRemote creates a bare repo with one commit on main and returns
// the directory that serves as the fake forge host: the bare repo lives
// at <host>/<owner>/<repo>.git so cloneURL resolves it.
//
// Built with go-git rather than by shelling out, so the suite no longer
// requires a git binary on the machine running it.
func newLocalRemote(t *testing.T, owner, repo string) string {
t.Helper()
host := t.TempDir()
bare := filepath.Join(host, owner, repo+".git")
if _, err := git.PlainInit(bare, true, git.WithDefaultBranch(
plumbing.NewBranchReferenceName(testBase),
)); err != nil {
t.Fatalf("init bare remote: %v", err)
}
seed := filepath.Join(t.TempDir(), "seed")
sr, err := git.PlainInit(seed, false, git.WithDefaultBranch(
plumbing.NewBranchReferenceName(testBase),
))
if err != nil {
t.Fatalf("init seed repo: %v", err)
}
writeSeedCommit(t, sr, seed, "README.md", "seed\n", "seed")
if _, err := sr.CreateRemote(&gitconfig.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{bare},
}); err != nil {
t.Fatalf("add remote: %v", err)
}
ref := plumbing.NewBranchReferenceName(testBase)
if err := sr.Push(&git.PushOptions{
RemoteName: git.DefaultRemoteName,
RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(ref + ":" + ref)},
}); err != nil {
t.Fatalf("seed push: %v", err)
}
return host
}
func writeSeedCommit(t *testing.T, r *git.Repository, dir, name, content, message string) plumbing.Hash {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o600); err != nil {
t.Fatalf("write %s: %v", name, err)
}
disableSigning(t, r)
wt, err := r.Worktree()
if err != nil {
t.Fatalf("worktree: %v", err)
}
if _, err := wt.Add(name); err != nil {
t.Fatalf("add %s: %v", name, err)
}
sig := testSignature()
hash, err := wt.Commit(message, &git.CommitOptions{Author: sig, Committer: sig})
if err != nil {
t.Fatalf("commit: %v", err)
}
return hash
}
// disableSigning mirrors what Manager.setIdentity does for real clones,
// so fixtures do not inherit commit.gpgSign from the host's global config.
func disableSigning(t *testing.T, r *git.Repository) {
t.Helper()
cfg, err := r.Config()
if err != nil {
t.Fatalf("read fixture config: %v", err)
}
cfg.Raw.Section("commit").SetOption("gpgsign", "false")
if err := r.SetConfig(cfg); err != nil {
t.Fatalf("write fixture config: %v", err)
}
}
func newManager(t *testing.T, host string) *Manager {
t.Helper()
return &Manager{
WorkDir: t.TempDir(),
BotUser: "archie-bot",
BotEmail: "archie@example.com",
BaseURL: host,
}
}
func TestPrepareCommitPushRoundTrip(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 7, "feat: add widget", "", "feature")
if err != nil {
t.Fatalf("Prepare() error = %v", err)
}
if !strings.HasPrefix(branch, "feat/7-") {
t.Errorf("branch = %q, want prefix feat/7-", branch)
}
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
t.Errorf("prepared sentinel missing: %v", err)
}
// A clean tree must report "nothing committed" rather than erroring.
committed, err := m.CommitAll(ctx, dir, "no-op")
if err != nil {
t.Fatalf("CommitAll() on clean tree error = %v", err)
}
if committed {
t.Error("CommitAll() = true on a clean tree, want false")
}
if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o600); err != nil {
t.Fatal(err)
}
committed, err = m.CommitAll(ctx, dir, "feat: add widget")
if err != nil {
t.Fatalf("CommitAll() error = %v", err)
}
if !committed {
t.Fatal("CommitAll() = false, want true after adding a file")
}
files, err := m.ChangedFiles(ctx, dir, testBase)
if err != nil {
t.Fatalf("ChangedFiles() error = %v", err)
}
if len(files) != 1 || files[0] != "widget.txt" {
t.Errorf("ChangedFiles() = %v, want [widget.txt]", files)
}
lines, err := m.ChangedLines(ctx, dir, testBase)
if err != nil {
t.Fatalf("ChangedLines() error = %v", err)
}
if lines != 2 {
t.Errorf("ChangedLines() = %d, want 2", lines)
}
diff, err := m.Diff(ctx, dir, testBase)
if err != nil {
t.Fatalf("Diff() error = %v", err)
}
if !strings.Contains(diff, "widget.txt") {
t.Errorf("Diff() does not mention the changed file:\n%s", diff)
}
if err := m.Push(ctx, dir, branch); err != nil {
t.Fatalf("Push() error = %v", err)
}
// The branch must actually exist on the remote afterwards.
bare := filepath.Join(host, "acme", "todo.git")
remote, err := git.PlainOpen(bare)
if err != nil {
t.Fatalf("open bare remote: %v", err)
}
if _, err := remote.Reference(plumbing.NewBranchReferenceName(branch), true); err != nil {
t.Errorf("branch %q not present on remote after Push: %v", branch, err)
}
}
func TestPrepareIsIdempotent(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir1, branch1, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug")
if err != nil {
t.Fatalf("first Prepare() error = %v", err)
}
// A marker file proves the second call reused the clone instead of
// wiping and re-cloning it.
marker := filepath.Join(dir1, "reuse-marker")
if err := os.WriteFile(marker, nil, 0o600); err != nil {
t.Fatal(err)
}
dir2, branch2, err := m.Prepare(ctx, "acme", "todo", testBase, 3, "fix: thing", "", "bug")
if err != nil {
t.Fatalf("second Prepare() error = %v", err)
}
if dir1 != dir2 || branch1 != branch2 {
t.Errorf("Prepare() not stable: (%q,%q) then (%q,%q)", dir1, branch1, dir2, branch2)
}
if _, err := os.Stat(marker); err != nil {
t.Error("second Prepare() re-cloned instead of reusing the prepared worktree")
}
}
// TestPrepareRecoversFromADirtyWorktreeOnRetry reproduces a real retry
// failure seen in production: a task that got interrupted mid-stage (killed
// container, or a stage that edits files before ever reaching commit)
// leaves uncommitted local changes -- a modified tracked file plus an
// untracked one -- sitting in the worktree. HEAD is already on the correct
// branch (this is not the "no sentinel" case above). refresh's checkout,
// without Force, resets in git's MergeReset mode, which can fail to
// reconcile a working tree that differs from HEAD even when the target
// commit hasn't moved -- and did fail here with
// `checkout branch <branch>: a branch named "refs/heads/<branch>" already
// exists`, since the fallback that mis-assumes "checkout failed means the
// branch doesn't exist yet" then collides with the branch that, in fact,
// already exists. Retry must recover from this on its own; a worktree that
// refresh is about to hard-reset to base has no reason to fail here at all.
func TestPrepareRecoversFromADirtyWorktreeOnRetry(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 9, "fix: retry robustness", "", "bug")
if err != nil {
t.Fatalf("first Prepare() error = %v", err)
}
// Simulate an interrupted attempt: a tracked file edited but never
// committed, plus a new file the agent created and never added.
if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("seed\nedited, uncommitted\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "scratch.txt"), []byte("uncommitted new file\n"), 0o600); err != nil {
t.Fatal(err)
}
dir2, branch2, err := m.Prepare(ctx, "acme", "todo", testBase, 9, "fix: retry robustness", "", "bug")
if err != nil {
t.Fatalf("retry Prepare() on a dirty worktree error = %v, want recovery", err)
}
if dir2 != dir || branch2 != branch {
t.Errorf("retry Prepare() = (%q, %q), want the same (%q, %q)", dir2, branch2, dir, branch)
}
// refresh hard-resets to base immediately after checkout succeeds, so
// the tracked file's uncommitted edit must be gone -- restored to
// base, not merged in. (An untracked leftover like scratch.txt is real
// git's job for `git clean`, not `reset --hard`, and is harmless here:
// it doesn't block the retry, which is the only thing this test cares
// about.)
content, err := os.ReadFile(filepath.Join(dir, "README.md"))
if err != nil {
t.Fatal(err)
}
if string(content) != "seed\n" {
t.Errorf("README.md = %q, want the base commit's content restored", content)
}
}
// An interrupted clone leaves a directory with no sentinel. Prepare must
// discard it rather than trying to reuse a half-built worktree.
func TestPrepareDiscardsDirectoryWithoutSentinel(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir := m.Dir("acme", "todo", 11)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
junk := filepath.Join(dir, "half-written")
if err := os.WriteFile(junk, []byte("partial"), 0o600); err != nil {
t.Fatal(err)
}
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 11, "chore: retry", "", "chore"); err != nil {
t.Fatalf("Prepare() error = %v", err)
}
if _, err := os.Stat(junk); !os.IsNotExist(err) {
t.Error("Prepare() reused an interrupted clone instead of discarding it")
}
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
t.Errorf("prepared sentinel missing after recovery: %v", err)
}
}
func TestPrepareErrors(t *testing.T) {
host := newLocalRemote(t, "acme", "todo")
tests := []struct {
name string
owner string
repo string
base string
host string
}{
{name: "unknown base branch", owner: "acme", repo: "todo", base: "no-such-branch", host: host},
{name: "unknown repository", owner: "acme", repo: "missing", base: testBase, host: host},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m := newManager(t, tc.host)
_, _, err := m.Prepare(context.Background(), tc.owner, tc.repo, tc.base, 1, "t", "", "")
if err == nil {
t.Fatal("Prepare() error = nil, want a failure")
}
})
}
}
// Every read-only operation must report a clear error on a directory that
// is not a repository, rather than panicking or returning a zero value
// that reads as "no changes".
func TestOperationsOnNonRepository(t *testing.T) {
ctx := context.Background()
m := &Manager{WorkDir: t.TempDir()}
dir := t.TempDir()
t.Run("CommitAll", func(t *testing.T) {
if _, err := m.CommitAll(ctx, dir, "msg"); err == nil {
t.Error("CommitAll() error = nil, want a failure")
}
})
t.Run("ChangedLines", func(t *testing.T) {
if _, err := m.ChangedLines(ctx, dir, testBase); err == nil {
t.Error("ChangedLines() error = nil, want a failure")
}
})
t.Run("ChangedFiles", func(t *testing.T) {
if _, err := m.ChangedFiles(ctx, dir, testBase); err == nil {
t.Error("ChangedFiles() error = nil, want a failure")
}
})
t.Run("Diff", func(t *testing.T) {
if _, err := m.Diff(ctx, dir, testBase); err == nil {
t.Error("Diff() error = nil, want a failure")
}
})
t.Run("Push", func(t *testing.T) {
if err := m.Push(ctx, dir, "any"); err == nil {
t.Error("Push() error = nil, want a failure")
}
})
}
// The diff must be measured from the merge base, not from the remote tip.
// Otherwise commits landed on the base branch after the task started are
// attributed to the task and can trip the diff-size cap.
func TestDiffUsesMergeBaseNotRemoteTip(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 5, "feat: mine", "", "feature")
if err != nil {
t.Fatalf("Prepare() error = %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "mine.txt"), []byte("mine\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := m.CommitAll(ctx, dir, "feat: mine"); err != nil {
t.Fatalf("CommitAll() error = %v", err)
}
// Land an unrelated commit on the remote base and fetch it, so
// origin/main moves ahead of where this branch diverged.
seed := filepath.Join(t.TempDir(), "other")
sr, err := git.PlainClone(seed, &git.CloneOptions{URL: filepath.Join(host, "acme", "todo.git")})
if err != nil {
t.Fatalf("clone for second author: %v", err)
}
writeSeedCommit(t, sr, seed, "theirs.txt", "theirs\n", "chore: theirs")
ref := plumbing.NewBranchReferenceName(testBase)
if err := sr.Push(&git.PushOptions{
RemoteName: git.DefaultRemoteName,
RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(ref + ":" + ref)},
}); err != nil {
t.Fatalf("second author push: %v", err)
}
r, err := git.PlainOpen(dir)
if err != nil {
t.Fatal(err)
}
if err := r.Fetch(&git.FetchOptions{RemoteName: git.DefaultRemoteName}); err != nil {
t.Fatalf("fetch: %v", err)
}
files, err := m.ChangedFiles(ctx, dir, testBase)
if err != nil {
t.Fatalf("ChangedFiles() error = %v", err)
}
for _, f := range files {
if f == "theirs.txt" {
t.Errorf("ChangedFiles() = %v, includes another author's file; diff is not merge-base relative", files)
}
}
if len(files) != 1 || files[0] != "mine.txt" {
t.Errorf("ChangedFiles() = %v, want [mine.txt]", files)
}
}
func TestBranchNaming(t *testing.T) {
tests := []struct {
name string
issue int
title string
labels string
want string
}{
{name: "bug label maps to fix", issue: 4, title: "crash on save", labels: "bug", want: "fix/4-crash-on-save"},
{name: "feature label maps to feat", issue: 9, title: "add export", labels: "feature", want: "feat/9-add-export"},
{name: "no label defaults to feat", issue: 1, title: "tidy", labels: "", want: "feat/1-tidy"},
{name: "conventional prefix is not duplicated", issue: 2, title: "fix: broken link", labels: "bug", want: "fix/2-broken-link"},
{name: "empty title falls back to prefix and issue", issue: 8, title: "", labels: "chore", want: "chore/8"},
{name: "punctuation collapses to single hyphens", issue: 3, title: "a -- b", labels: "docs", want: "docs/3-a-b"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := archieBranch(tc.issue, tc.title, "", tc.labels); got != tc.want {
t.Errorf("archieBranch() = %q, want %q", got, tc.want)
}
})
}
}
func TestCloneURL(t *testing.T) {
tests := []struct {
name string
baseURL string
want string
}{
{
name: "explicit forge host",
baseURL: "https://git.example.test",
want: "https://git.example.test/acme/todo.git",
},
{
// The token must never be embedded in the URL: it travels as
// an in-process credential instead.
name: "default github host carries no credentials",
baseURL: "",
want: "https://github.com/acme/todo.git",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m := &Manager{BaseURL: tc.baseURL, BotUser: "archie-bot", Token: "s3cret"}
got := m.cloneURL("acme", "todo")
if got != tc.want {
t.Errorf("cloneURL() = %q, want %q", got, tc.want)
}
if strings.Contains(got, "s3cret") {
t.Errorf("cloneURL() leaks the token: %q", got)
}
})
}
}
// A missing forge credential over an HTTP(S) remote must fail with a
// message naming the missing token as the cause, not a bare git transport
// error discovered mid-push. Local (non-HTTP) remotes -- the test double
// used throughout this file -- are unaffected: they never need a token.
func TestPushWithoutTokenOverHTTPFailsWithClearMessage(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
seeder := newManager(t, host)
dir, branch, err := seeder.Prepare(ctx, "acme", "todo", testBase, 1, "t", "", "")
if err != nil {
t.Fatalf("Prepare() error = %v", err)
}
// Point the already-cloned worktree's origin at an HTTPS remote, as if
// it had been cloned against a real forge rather than the local test
// double, then push with no token configured.
r, err := git.PlainOpen(dir)
if err != nil {
t.Fatalf("PlainOpen: %v", err)
}
if err := r.DeleteRemote(git.DefaultRemoteName); err != nil {
t.Fatalf("DeleteRemote: %v", err)
}
if _, err := r.CreateRemote(&gitconfig.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{"https://forge.example.invalid/acme/todo.git"},
}); err != nil {
t.Fatalf("CreateRemote: %v", err)
}
m := &Manager{WorkDir: seeder.WorkDir, BotUser: "archie-bot"}
err = m.Push(ctx, dir, branch)
if err == nil {
t.Fatal("Push() error = nil, want a failure: no token is configured")
}
if !strings.Contains(err.Error(), "forge") && !strings.Contains(err.Error(), "token") {
t.Errorf("Push() error = %q, want it to name the missing forge credential as the cause", err.Error())
}
}
func TestAuthOmittedWithoutToken(t *testing.T) {
if opts := (&Manager{BotUser: "archie-bot"}).auth(); opts != nil {
t.Errorf("auth() = %v with no token, want nil for an anonymous request", opts)
}
if opts := (&Manager{BotUser: "archie-bot", Token: "t"}).auth(); len(opts) == 0 {
t.Error("auth() returned no options despite a configured token")
}
}
func TestCleanupRemovesWorktree(t *testing.T) {
m := &Manager{WorkDir: t.TempDir()}
dir := m.Dir("acme", "todo", 42)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := m.Cleanup("acme", "todo", 42); err != nil {
t.Fatalf("Cleanup() error = %v", err)
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Error("Cleanup() left the worktree behind")
}
// Cleanup of an absent worktree is not an error.
if err := m.Cleanup("acme", "todo", 42); err != nil {
t.Errorf("Cleanup() on a missing worktree error = %v, want nil", err)
}
}
// The prepared sentinel must never reach a commit. It previously lived in
// the working tree, hidden by .git/info/exclude -- which git honours and
// go-git does not, so it was committed and pushed onto every task branch.
func TestSentinelIsNeverCommitted(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 21, "feat: thing", "", "feature")
if err != nil {
t.Fatalf("Prepare() error = %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "real.txt"), []byte("x\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := m.CommitAll(ctx, dir, "feat: thing"); err != nil {
t.Fatalf("CommitAll() error = %v", err)
}
files, err := m.ChangedFiles(ctx, dir, testBase)
if err != nil {
t.Fatalf("ChangedFiles() error = %v", err)
}
for _, f := range files {
if strings.Contains(f, "archie-prepared") {
t.Errorf("the prepared sentinel was committed: ChangedFiles() = %v", files)
}
}
// It must still exist on disk, or Prepare stops being idempotent.
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
t.Errorf("sentinel missing after commit: %v", err)
}
}
// The daemon writes the container's boot brief into the task worktree before
// the container starts. CommitAll stages with go-git's All option, which does
// not honour .gitignore or .git/info/exclude, so a brief written to the
// worktree root is swept into the agent's commit and pushed onto the task
// branch -- the same failure the prepared sentinel hit. It has to live
// somewhere that can never be tracked.
func TestTaskBriefIsNeverCommitted(t *testing.T) {
ctx := context.Background()
host := newLocalRemote(t, "acme", "todo")
m := newManager(t, host)
dir, _, err := m.Prepare(ctx, "acme", "todo", testBase, 22, "feat: brief", "", "feature")
if err != nil {
t.Fatalf("Prepare() error = %v", err)
}
if err := container.WriteTaskJSON(dir, container.TaskPayload{
ID: 22, Owner: "acme", Repo: "todo", Number: 22, Title: "feat: brief",
}); err != nil {
t.Fatalf("WriteTaskJSON() error = %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "real.txt"), []byte("x\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := m.CommitAll(ctx, dir, "feat: brief"); err != nil {
t.Fatalf("CommitAll() error = %v", err)
}
files, err := m.ChangedFiles(ctx, dir, testBase)
if err != nil {
t.Fatalf("ChangedFiles() error = %v", err)
}
for _, f := range files {
if strings.Contains(f, "task.json") {
t.Errorf("the task brief was committed: ChangedFiles() = %v", files)
}
}
// The agent still has to be able to read it off its mount.
if _, err := os.Stat(filepath.Join(dir, ".git", "task.json")); err != nil {
t.Errorf("brief missing after commit: %v", err)
}
}