fix(agentworker): hand the worktree back on every sandbox exit
A container run only restored ownership of the bind-mounted worktree on the CommitAll/Push success path (archie-core#520). The agent runs as root inside the container, so every other way a run can end -- a gate failure that parks the task, a stage error, an early return -- left the worktree owned by UID 0 on the host. The daemon is the process that later cleans and resets that directory as its own non-root user, and it cannot even unlink inside a root-owned directory, so the next attempt failed before it could start no matter what the worktree refresh did. runTask now defers the same reconcileOwnership the commit and push paths already call, so it runs at every exit. The retry recoveries in worktree package cannot fix this from the daemon side: a non-root process cannot chown, and removing the directory needs write access to it. Observed through a supplementary group -- a non-root process may move a file it owns into a group it is a member of -- so the test proves the chown ran without needing root. Residual, filed as archie-core-zcg8: a container that is SIGKILLed never runs this defer.
This commit is contained in:
1 parent
fe3d508b6c
commit
bcf7622a8c
2 files changed
+101
No files matched your search
@@ -75,6 +75,26 @@ func (h *hybridTrees) reconcileOwnership(dir string) error {
|
||||
return chownTree(dir, h.worktreeUID, h.worktreeGID)
|
||||
}
|
||||
|
||||
// restoreWorktreeOwnership returns the deferred half of the ownership
|
||||
// contract: the agent runs as root inside the container, so a run that ends
|
||||
// without ever reaching CommitAll or Push (a gate failure that parks the task,
|
||||
// a stage error, an early return) has to hand the bind-mounted worktree back to
|
||||
// the daemon's own UID on its way out. The daemon is the process that later
|
||||
// cleans and resets that directory as a non-root host user, and it cannot even
|
||||
// unlink inside a root-owned directory -- so a parked attempt otherwise left a
|
||||
// worktree the next attempt could not start from, no matter what the refresh
|
||||
// did.
|
||||
//
|
||||
// Returns the func rather than deferring internally so the caller's own defer
|
||||
// runs it at the caller's return, not at this function's.
|
||||
func restoreWorktreeOwnership(trees *hybridTrees, dir string, log *slog.Logger) func() {
|
||||
return func() {
|
||||
if err := trees.reconcileOwnership(dir); err != nil {
|
||||
log.Warn("worktree ownership restore failed", "dir", dir, "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *hybridTrees) Diff(ctx context.Context, dir, base string) (string, error) {
|
||||
return h.local.Diff(ctx, dir, base)
|
||||
}
|
||||
@@ -188,6 +208,16 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
|
||||
worktreeUID: worktreeOwnerID(os.Getenv("WORKTREE_UID")),
|
||||
worktreeGID: worktreeOwnerID(os.Getenv("WORKTREE_GID")),
|
||||
}
|
||||
// The agent process runs as root inside the container, so everything this
|
||||
// run writes into the bind-mounted worktree is owned by UID 0 on the host;
|
||||
// CommitAll and Push hand it back on the success path (archie-core#520).
|
||||
// Doing the same here covers every OTHER way a run can end -- a gate
|
||||
// failure that parks the task, a stage error, an early return -- because
|
||||
// the daemon is the one that later cleans and resets this directory, as its
|
||||
// own non-root host user, and it cannot even unlink a root-owned directory.
|
||||
// Without this, a parked attempt left a worktree the next attempt could not
|
||||
// start from.
|
||||
defer restoreWorktreeOwnership(trees, workDir, log)()
|
||||
|
||||
// Start MCP providers and build a local tool registry.
|
||||
mcpSet, mcpErr := startMCPProviders(ctx, req.MCPServers, log)
|
||||
|
||||
@@ -8,8 +8,10 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -421,6 +423,75 @@ func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTaskRestoresWorktreeOwnershipOnEveryExit pins the half of the
|
||||
// worktree-ownership contract that archie-core#520 left open. CommitAll and
|
||||
// Push hand the worktree back to the daemon's own UID on the SUCCESS path;
|
||||
// every other way a container run can end -- a gate failure that parks the
|
||||
// task, a stage error, an early return -- used to leave the worktree owned by
|
||||
// root, because the agent runs as root inside the container. The daemon then
|
||||
// could not clean or reset those paths on the next attempt (a root-owned
|
||||
// directory cannot be unlinked from by its non-root owner), so a retry failed
|
||||
// before it could start no matter what the worktree refresh did.
|
||||
//
|
||||
// The run below exits early, which is the point: the restore has to be
|
||||
// deferred over the whole run rather than attached to the paths that happen to
|
||||
// commit. It is observed through a supplementary group -- a non-root process
|
||||
// may move a file it owns into a group it is a member of, so this proves the
|
||||
// chown ran without needing root.
|
||||
func TestRunTaskRestoresWorktreeOwnershipOnEveryExit(t *testing.T) {
|
||||
probeGroup := supplementaryGroup(t)
|
||||
workDir := t.TempDir()
|
||||
probe := filepath.Join(workDir, "probe.txt")
|
||||
if err := os.WriteFile(probe, []byte("x\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Setenv("WORKTREE_UID", strconv.Itoa(os.Getuid()))
|
||||
t.Setenv("WORKTREE_GID", strconv.Itoa(probeGroup))
|
||||
|
||||
// A runner factory that yields nothing fails the run after the trees (and
|
||||
// the ownership defer) are already in place.
|
||||
_, err := runTask(t.Context(), taskrun.Request{
|
||||
Task: &workflow.Task{ID: 77, Branch: "feat/77-probe", Workflow: "implement"},
|
||||
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
|
||||
}, taskDependencies{}, runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner {
|
||||
return nil
|
||||
}), workDir, slog.New(slog.DiscardHandler))
|
||||
if err == nil {
|
||||
t.Fatal("runTask error = nil, want the missing-runner failure this exit is built around")
|
||||
}
|
||||
|
||||
info, statErr := os.Stat(probe)
|
||||
if statErr != nil {
|
||||
t.Fatal(statErr)
|
||||
}
|
||||
gid, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
t.Fatal("no syscall.Stat_t for the ownership probe")
|
||||
}
|
||||
if int(gid.Gid) != probeGroup {
|
||||
t.Errorf("probe.txt gid = %d, want %d -- a run that never reached CommitAll or Push left the worktree owned by the container's own user", gid.Gid, probeGroup)
|
||||
}
|
||||
}
|
||||
|
||||
// supplementaryGroup returns a group this process is a member of but that is
|
||||
// not its primary gid, so a test can observe a chown a non-root process is
|
||||
// allowed to make.
|
||||
func supplementaryGroup(t *testing.T) int {
|
||||
t.Helper()
|
||||
groups, err := os.Getgroups()
|
||||
if err != nil {
|
||||
t.Skipf("os.Getgroups: %v", err)
|
||||
}
|
||||
for _, gid := range groups {
|
||||
if gid != os.Getgid() {
|
||||
return gid
|
||||
}
|
||||
}
|
||||
t.Skip("process belongs to no supplementary group to chown into")
|
||||
return 0
|
||||
}
|
||||
|
||||
// TestExecuteTaskRequestUsesInfrastructureRPCDependencies preserves the
|
||||
// application-to-infrastructure proof: the application asks Transport for its
|
||||
// forge/store/tree contracts, and those concrete clients cross embedded NATS
|
||||
|
||||
Reference in new issue
Block a user