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
This commit is contained in:
sam committed 2026-08-14 02:35:49 +10:00
1 parent a1a2a943e1
commit fd8f9f925f
2 files changed
+35 -15

No files matched your search

+35
View File
@@ -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)
-15
View File
@@ -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()