docs(agent): correct markSafe's doc comment to its actual existence check

The comment claimed a mount was checked before touching the
operator's global gitconfig; the code only ever checked os.Stat
(existence + directory-ness). A directory that merely exists at the
mount path -- a stale leftover, never actually bind-mounted this run
-- already satisfied the guard and got added to safe.directory,
exactly what the comment said it prevented. A real mount check would
need platform-specific mountinfo parsing for an inert failure mode
(the entry does nothing unless a git repo later exists at that exact
path), so this corrects the contract to match the code and pins it
with a test instead of changing behavior.
This commit is contained in:
sam committed 2026-08-15 01:48:39 +10:00
1 parent 023d0a0165
commit 15556a8a1c
2 files changed
+37 -4

No files matched your search

+11 -4
View File
@@ -34,13 +34,20 @@ func MarkSafe(ctx context.Context, mountDir string, log *slog.Logger) bool {
// working a repo with no gate commands never needs it. A failure is logged and
// the agent continues. Returns true only when git accepted the configuration.
//
// The mount is checked first so that running archie-agent directly on a host
// (shared queue-group mode, no container) does not write to the operator's own
// ~/.gitconfig for a path that was never mounted.
// The guard is existence, not mount-ness: os.Stat confirms mountDir exists and
// is a directory, which is what stops archie-agent running directly on a host
// (shared queue-group mode, no container) from writing to the operator's own
// ~/.gitconfig for a path that was never created at all. It does not detect a
// stale directory that happens to exist at the same path without actually
// being a bind mount -- a real mount check would need platform-specific
// parsing (e.g. /proc/self/mountinfo on Linux) that this deliberately does
// without. The failure mode is inert: git only rejects operations in a
// directory that both exists at this path and is a real repository, and
// safe.directory entries for a stale non-repo path do nothing.
func markSafe(ctx context.Context, mountDir string, run gitRunner, log *slog.Logger) bool {
info, err := os.Stat(mountDir)
if err != nil || !info.IsDir() {
log.Debug("git safe.directory skipped: no worktree mounted", "dir", mountDir)
log.Debug("git safe.directory skipped: mountDir does not exist", "dir", mountDir)
return false
}
@@ -79,6 +79,32 @@ func TestMarkWorktreeSafe(t *testing.T) {
}
}
// The guard markSafe applies is existence, not mount-ness: a directory that
// merely exists at mountDir -- a stale leftover from a prior run, never
// actually bind-mounted this time -- still satisfies os.Stat and gets
// configured. This is the documented, accepted behavior (see markSafe's
// doc comment): a real mount check would need platform-specific mountinfo
// parsing, and the resulting stale safe.directory entry is inert unless a
// git repository later exists at that exact path. This test pins the
// current contract so a future change to it is a deliberate decision, not
// a silent regression either way.
func TestMarkWorktreeSafeAcceptsAnExistingButUnmountedDirectory(t *testing.T) {
dir := t.TempDir() // exists on disk; nothing bind-mounted it here
var runs [][]string
run := func(_ context.Context, args ...string) ([]byte, error) {
runs = append(runs, args)
return []byte("output"), nil
}
if got := markSafe(context.Background(), dir, run, slog.New(slog.DiscardHandler)); !got {
t.Fatal("markSafe() = false for an existing (if unmounted) directory, want true per the documented existence-based guard")
}
if len(runs) != 1 {
t.Fatalf("git invoked %d times, want 1", len(runs))
}
}
// A mount path that is a file, not a directory, is not a worktree: git must
// not be reconfigured for it.
func TestMarkWorktreeSafeIgnoresNonDirectory(t *testing.T) {