From 3d49d919d90e94370c90d30db240bec8dcc329d2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 22 Sep 2026 04:00:55 +1000 Subject: [PATCH] fix(daemon): cut worktrees from the mapped base branch Repo.BaseBranch() is where an empty base means "main", and the daemon handed the raw field to worktree.Prepare -- the one call site that did, beside the workflow step that passes the mapped value. A repository stored with no base therefore reached git with "" and its branch was never cut from main. Reachable now because the document editor stopped prefilling base (its per-path skeletons retired in aa650e2c), which had been masking the missing mapping rather than fixing it (archie-core-lwzd). The guard is a wiring invariant in architecture_test.go: it walks the package for the worktree.Prepare call and refuses the raw field, and fails if the call is gone rather than passing unchecked. --- internal/daemon/architecture_test.go | 49 ++++++++++++++++++++++++++++ internal/daemon/daemon.go | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/internal/daemon/architecture_test.go b/internal/daemon/architecture_test.go index ce8adcf51..b0f57e072 100644 --- a/internal/daemon/architecture_test.go +++ b/internal/daemon/architecture_test.go @@ -108,3 +108,52 @@ func TestDaemonCarriesNoLocalWorkflowExecutionState(t *testing.T) { } } } + +// A repository's base branch has exactly one default and Repo.BaseBranch() is +// where it lives: an empty base means "main". Handing the raw field to the +// worktree manager skips that mapping, so a repository stored with no base +// reaches git with "" and the branch is never cut from main +// (archie-core-lwzd). +// +// This is a wiring invariant rather than a behaviour test because the value is +// an argument: the worktree manager's own tests cover what it does with a base, +// and a daemon-level test would need a real clone to reach the call. +func TestDaemonPreparesWorktreesFromTheMappedBaseBranch(t *testing.T) { + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + fileset := token.NewFileSet() + checked := 0 + for _, path := range files { + if strings.HasSuffix(path, "_test.go") { + continue + } + file, parseErr := parser.ParseFile(fileset, path, nil, 0) + if parseErr != nil { + t.Errorf("parse %s: %v", path, parseErr) + continue + } + ast.Inspect(file, func(node ast.Node) bool { + call, ok := node.(*ast.CallExpr) + if !ok || len(call.Args) != 8 { + return true + } + selector, ok := call.Fun.(*ast.SelectorExpr) + if !ok || selector.Sel.Name != "Prepare" { + return true + } + checked++ + base, ok := call.Args[3].(*ast.SelectorExpr) + if ok && base.Sel.Name == "Base" { + t.Errorf("%s: worktree.Prepare receives the raw base field; pass BaseBranch() so an empty base means main", + fileset.Position(call.Pos())) + } + return true + }) + } + // A rename or a move must not leave this passing without having looked. + if checked == 0 { + t.Error("no worktree.Prepare call found, so the base-branch invariant went unchecked") + } +} diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 3b1ca445d..cd71ab1b2 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1175,7 +1175,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) { // go-git has no --dissociate, so a shared cache would stay a live // dependency of each worktree and expiring one would corrupt running // tasks. repo.PersistentStorage still governs the container volume. - _, branch, err = trees.Prepare(ctx, task.Owner, task.Repo, repo.Base, task.IssueNumber, task.Title, task.Body, task.Labels) + _, branch, err = trees.Prepare(ctx, task.Owner, task.Repo, repo.BaseBranch(), task.IssueNumber, task.Title, task.Body, task.Labels) if err != nil { d.Log.Error("worktree prepare failed", "err", err) d.parkRunningTask(ctx, task.ID, "worktree prepare failed: "+err.Error())