diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index 6f98fa98d..41cc0165b 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -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) diff --git a/internal/app/agentworker/task_execution_test.go b/internal/app/agentworker/task_execution_test.go index e4d83340f..81d57b947 100644 --- a/internal/app/agentworker/task_execution_test.go +++ b/internal/app/agentworker/task_execution_test.go @@ -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