From fd8f9f925fb7f5838221dd5e41df9e0ceb925053 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 14 Aug 2026 02:35:49 +1000 Subject: [PATCH] fix(agent): route git safe.directory setup through markWorktreeSafe The dubious-ownership fix landed twice: markWorktreeSafe in gitsafe.go and an inline exec.Command block in main.go, both configuring the same safe.directory on every start. Two setup paths for one invariant means a later change to either one silently disagrees with the other. Keep the tested helper and drop the inline block. markWorktreeSafe also skips the call when /data/worktree is not a mounted directory, so running archie-agent directly on a host no longer writes to the operator's own ~/.gitconfig for a path that was never mounted. The new test parses main.go and fails on more than one setup path, so a future re-addition of an inline block is caught rather than merged. Refs #468 --- cmd/archie-agent/gitsafe_test.go | 35 ++++++++++++++++++++++++++++++++ cmd/archie-agent/main.go | 15 -------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/cmd/archie-agent/gitsafe_test.go b/cmd/archie-agent/gitsafe_test.go index 28f9e69df..8451f45e9 100644 --- a/cmd/archie-agent/gitsafe_test.go +++ b/cmd/archie-agent/gitsafe_test.go @@ -3,6 +3,9 @@ package main import ( "context" "errors" + "go/ast" + "go/parser" + "go/token" "log/slog" "os" "path/filepath" @@ -10,6 +13,38 @@ import ( "testing" ) +func TestMainConfiguresWorktreeSafetyOnce(t *testing.T) { + file, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0) + if err != nil { + t.Fatal(err) + } + + var markCalls, inlineCalls int + ast.Inspect(file, func(node ast.Node) bool { + call, ok := node.(*ast.CallExpr) + if !ok { + return true + } + if ident, ok := call.Fun.(*ast.Ident); ok && ident.Name == "markWorktreeSafe" { + markCalls++ + } + for _, arg := range call.Args { + literal, ok := arg.(*ast.BasicLit) + if ok && literal.Kind == token.STRING && literal.Value == `"safe.directory"` { + inlineCalls++ + } + } + return true + }) + + if markCalls != 1 { + t.Fatalf("main.go calls markWorktreeSafe %d times, want 1", markCalls) + } + if got := markCalls + inlineCalls; got != 1 { + t.Fatalf("main.go configures safe.directory %d times, want one authoritative setup path", got) + } +} + func TestMarkWorktreeSafe(t *testing.T) { log := slog.New(slog.DiscardHandler) diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index dfd1833f1..6fe96d681 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -21,7 +21,6 @@ import ( "fmt" "log/slog" "os" - "os/exec" "os/signal" "path/filepath" "strings" @@ -78,20 +77,6 @@ func run() int { log := slog.New(slog.NewJSONHandler(os.Stderr, nil)) - // Worktrees are bind-mounted from the host into the container at - // /data/worktree. The host-side UID that owns the worktree differs - // from the container user's UID. Git refuses to operate when the - // containing directory is owned by another user ("fatal: detected - // dubious ownership in repository"). The Go toolchain calls git for - // VCS status during go build/test/vet, so every gate fails before - // any real work begins. Marking the mount directory as safe resolves - // this for the container's lifetime. - gitSafeDir := exec.Command("git", "config", "--global", "--add", "safe.directory", storage.WorktreeMountDir) - if out, gitErr := gitSafeDir.CombinedOutput(); gitErr != nil { - log.Warn("git safe.directory config failed (gate commands that shell out to git may encounter dubious-ownership errors)", - "dir", storage.WorktreeMountDir, "err", gitErr, "output", string(out)) - } - ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop()