fix: clone worktree before container acquire to fix bind-mount race

This commit is contained in:
sam committed 2026-07-22 21:02:25 +10:00
1 parent bdc6d39039
commit 3598498813
3 files changed
+22 -1

No files matched your search

+12
View File
@@ -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.
+2 -1
View File
@@ -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)
+8
View File
@@ -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
}