From 1ef576ca7b0b2a2eff32af998e365d117ae8e3ab Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 9 Nov 2026 01:40:10 +1100 Subject: [PATCH] fix(agentexec): run gate commands as the harness user and reap the process group after each invocation --- internal/agentexec/harness_proc_other.go | 3 +++ internal/agentexec/harness_proc_unix.go | 8 ++++++++ internal/agentexec/harness_runner.go | 26 ++++++++++++++++++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/internal/agentexec/harness_proc_other.go b/internal/agentexec/harness_proc_other.go index 870e1e90c..2e0c655cc 100644 --- a/internal/agentexec/harness_proc_other.go +++ b/internal/agentexec/harness_proc_other.go @@ -11,6 +11,9 @@ import ( // process groups are unavailable. func setProcessGroup(*exec.Cmd) {} +// reapGroup has nothing to do where process groups are unavailable. +func reapGroup(*exec.Cmd) {} + // runAsHarnessUser refuses a named harness user where user switching is // unavailable, rather than running the harness as the worker. func runAsHarnessUser(_ *exec.Cmd, name string) error { diff --git a/internal/agentexec/harness_proc_unix.go b/internal/agentexec/harness_proc_unix.go index b2273b6c3..00bfd07d2 100644 --- a/internal/agentexec/harness_proc_unix.go +++ b/internal/agentexec/harness_proc_unix.go @@ -22,6 +22,14 @@ func setProcessGroup(cmd *exec.Cmd) { cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } } +// reapGroup kills whatever the command left running in its process group, so a +// daemonised child cannot outlive the invocation that started it. +func reapGroup(cmd *exec.Cmd) { + if cmd.Process != nil { + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + } +} + // runAsHarnessUser drops the invocation to the Kit's user. A worker running // as root refuses to start a harness with no user, because the harness would // then share the worker's user and could read its credentials. diff --git a/internal/agentexec/harness_runner.go b/internal/agentexec/harness_runner.go index db48ccd72..6b3a1a689 100644 --- a/internal/agentexec/harness_runner.go +++ b/internal/agentexec/harness_runner.go @@ -133,7 +133,7 @@ func (r *HarnessRunner) run(ctx context.Context, workspace string, req agentrun. return res, err } - gateOutput, gateErr := runHarnessGate(runCtx, req.Gate.Commands, workspace) + gateOutput, gateErr := runHarnessGate(runCtx, req.Harness, req.Gate.Commands, workspace) if gateErr == nil { res.Status = agentrun.StatusPassed return res, nil @@ -270,8 +270,16 @@ func invoke(ctx context.Context, workspace string, h *agentrun.HarnessSpec, argv for scanner.Scan() { out.Line(scanner.Bytes(), report) } - _, _ = io.Copy(io.Discard, output) - if err := cmd.Wait(); err != nil { + if err := scanner.Err(); err != nil { + // Stop the CLI rather than leave it writing into a pipe nobody parses. + _ = cmd.Cancel() + _, _ = io.Copy(io.Discard, output) + _ = cmd.Wait() + return fmt.Errorf("read harness output: %w", err) + } + waitErr := cmd.Wait() + reapGroup(cmd) + if err := waitErr; err != nil { if ctx.Err() != nil { return ctx.Err() } @@ -304,14 +312,24 @@ func harnessPrompt(workspace string, req agentrun.Request) string { return b.String() } -func runHarnessGate(ctx context.Context, commands []agentrun.Command, workspace string) (string, error) { +// runHarnessGate runs the gate commands confined like the harness: they +// execute code the harness just wrote, so they get its user, its environment +// and its process group rather than the worker's. +func runHarnessGate(ctx context.Context, h *agentrun.HarnessSpec, commands []agentrun.Command, workspace string) (string, error) { for _, c := range commands { if len(c.Argv) == 0 { continue } cmd := exec.CommandContext(ctx, c.Argv[0], c.Argv[1:]...) cmd.Dir = workspace + cmd.WaitDelay = killGrace + cmd.Env = append([]string{}, h.Env...) + setProcessGroup(cmd) + if err := runAsHarnessUser(cmd, h.User); err != nil { + return "", err + } output, err := cmd.CombinedOutput() + reapGroup(cmd) switch { case c.ExpectFailure && err == nil: return fmt.Sprintf("[%s] expected this command to fail, but it passed:\n%s", c.Name, clipGate(output)),