From 35984988139f64cb3ff6221b212c994d4b378479 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 22 Jul 2026 21:02:25 +1000 Subject: [PATCH] fix: clone worktree before container acquire to fix bind-mount race --- internal/daemon/daemon.go | 12 ++++++++++++ internal/workflow/steps.go | 3 ++- internal/worktree/worktree.go | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 036e13430..a8849af82 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -515,6 +515,18 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) { wf := workflow.Route(task, registry) d.Log.Info("processing task", "repo", repo.FullName(), "issue", task.IssueNumber, "workflow", wf.Name, "attempt", task.Attempt) + // Clone the worktree before Docker mount setup. The container + // binds /data/worktree to workDir on the host — the directory + // must exist before Acquire is called. + _, branch, err := d.Trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber) + if err != nil { + d.Log.Error("worktree prepare failed", "err", err) + _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "worktree prepare failed: "+err.Error()) + return + } + task.Branch = branch + _ = d.Store.Update(ctx, task) + // Acquire a container for the task when Docker sandboxing is enabled. if d.ContainerPool != nil { // Write task.json — the container's boot-time brief. diff --git a/internal/workflow/steps.go b/internal/workflow/steps.go index 6560277c9..9a9740a1b 100644 --- a/internal/workflow/steps.go +++ b/internal/workflow/steps.go @@ -17,7 +17,8 @@ import ( // stages live next to their workflow definition. // StagePrepareWorktree clones the repo fresh and checks out the task -// branch. +// branch. Skips if the daemon already prepared the worktree (Docker +// containers require the tree before acquire). func StagePrepareWorktree() Stage { return Stage{Name: "prepare", Run: func(ctx context.Context, tc *TaskContext) error { dir, branch, err := tc.Trees.Prepare(ctx, tc.Task.Owner, tc.Task.Repo, tc.Repo.BaseBranch(), tc.Task.IssueNumber) diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index 879a111fd..308505aa4 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -67,9 +67,17 @@ func (m *Manager) git(ctx context.Context, dir string, args ...string) (string, // Prepare creates a fresh clone for the task and checks out its branch. // Any leftover worktree from a prior attempt is removed first. +// If the worktree is already prepared (the daemon may have done this +// before container acquisition), it returns the existing directory. func (m *Manager) Prepare(ctx context.Context, owner, repo, base string, issue int) (dir, branch string, err error) { dir = m.Dir(owner, repo, issue) branch = fmt.Sprintf("archie/issue-%d", issue) + + // Already prepared (daemon cloned before container acquire). + if st, statErr := os.Stat(filepath.Join(dir, ".git")); statErr == nil && st.IsDir() { + return dir, branch, nil + } + if err := os.RemoveAll(dir); err != nil { return "", "", err }