Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(agent): check Pi cleanup capability before launch (#9996)
Jiahao56d6c55c64a9
diff --git a/CLI_AND_DAEMON.md b/CLI_AND_DAEMON.mdindex f560765d40..e54fca4306 100644--- a/CLI_AND_DAEMON.md+++ b/CLI_AND_DAEMON.md@@ -301,6 +301,14 @@ The daemon auto-detects these AI CLIs on your PATH:You need at least one installed. The daemon registers each detected CLI as an available runtime.+On macOS, Pi and Oh-My-Pi require kernel support for process identity signalling+to clean up detached tools safely. Each run checks this operation before launching+the runtime. An unsupported or denied operation fails startup with a compatibility+error instead of allowing work to start without the required cleanup capability.+This is a capability check, not a macOS version check. Existing runs blocked by+unconfirmed cleanup still require inspection and a manual rerun; the startup check+does not clear those blocks or establish that earlier background processes exited.+### How It Works1. On start, the daemon detects installed agent CLIs and registers a runtime for each agent in each watched workspacediff --git a/server/pkg/agent/pi.go b/server/pkg/agent/pi.goindex 9a9f5a86fd..da94bf809d 100644--- a/server/pkg/agent/pi.go+++ b/server/pkg/agent/pi.go@@ -39,6 +39,8 @@ type piBackend struct {// changing concurrent executions through package-global state.turnErrorGrace time.DurationterminateGrace time.Duration+ // Tests inject unsupported kernels per backend without changing global state.+ cleanupPreflight func() error}var (@@ -372,6 +374,13 @@ func (b *piBackend) Execute(ctx context.Context, prompt string, opts ExecOptionsif err != nil {return nil, fmt.Errorf("%s executable not found at %q: %w", label, execName, err)}+ checkCleanup := b.cleanupPreflight+ if checkCleanup == nil {+ checkCleanup = checkPiCleanupSupport+ }+ if err := checkCleanup(); err != nil {+ return nil, fmt.Errorf("%s process cleanup unavailable before start: %w", label, err)+ }rpcMode := label == "pi" && opts.EnableTaskSupplementtimeout := opts.Timeoutdiff --git a/server/pkg/agent/pi_cleanup_darwin.go b/server/pkg/agent/pi_cleanup_darwin.goindex d049f97d95..da7e1726c5 100644--- a/server/pkg/agent/pi_cleanup_darwin.go+++ b/server/pkg/agent/pi_cleanup_darwin.go@@ -13,6 +13,13 @@ type piDarwinProcessIdentity struct {unique uint64}+func checkPiCleanupSupport() error {+ if err := probePiIdentitySignalling(signalCursorDarwinIdentity); err != nil {+ return fmt.Errorf("this macOS kernel cannot safely clean up Pi/omp processes; use a macOS kernel with process identity signalling support: %w", err)+ }+ return nil+}+func piProcessMissing(pid int, err error) bool {if errors.Is(err, syscall.ESRCH) {return true@@ -38,8 +45,8 @@ func capturePiProcessIdentity(info cursorUnixProcessInfo) (piProcessIdentity, erreturn nil, fmt.Errorf("process identity changed during capture: %d", info.pid)}// Probe the kernel-checked signalling operation; no numeric-PID fallback.- if err := signalCursorDarwinIdentity(1<<31-1, 0, syscall.SIGKILL); err != syscall.ESRCH {- return nil, fmt.Errorf("kernel process identity signalling is unavailable: %v", err)+ if err := probePiIdentitySignalling(signalCursorDarwinIdentity); err != nil {+ return nil, err}return &piDarwinProcessIdentity{pid: info.pid, unique: identity.unique}, nil}diff --git a/server/pkg/agent/pi_cleanup_preflight.go b/server/pkg/agent/pi_cleanup_preflight.gonew file mode 100644index 0000000000..cc8d2d7f94--- /dev/null+++ b/server/pkg/agent/pi_cleanup_preflight.go@@ -0,0 +1,19 @@+package agent++import (+ "errors"+ "fmt"+ "syscall"+)++func probePiIdentitySignalling(signal func(int, uint32, syscall.Signal) error) error {+ // An impossible PID tests the kernel operation without signalling a process.+ err := signal(1<<31-1, 0, syscall.SIGKILL)+ if errors.Is(err, syscall.ESRCH) {+ return nil+ }+ if err == nil {+ return errors.New("kernel process identity signalling is unavailable: probe unexpectedly succeeded")+ }+ return fmt.Errorf("kernel process identity signalling is unavailable: %w", err)+}diff --git a/server/pkg/agent/pi_cleanup_preflight_other.go b/server/pkg/agent/pi_cleanup_preflight_other.gonew file mode 100644index 0000000000..ccfb37e016--- /dev/null+++ b/server/pkg/agent/pi_cleanup_preflight_other.go@@ -0,0 +1,5 @@+//go:build !darwin++package agent++func checkPiCleanupSupport() error { return nil }diff --git a/server/pkg/agent/pi_cleanup_preflight_test.go b/server/pkg/agent/pi_cleanup_preflight_test.gonew file mode 100644index 0000000000..c3bd683ec9--- /dev/null+++ b/server/pkg/agent/pi_cleanup_preflight_test.go@@ -0,0 +1,29 @@+package agent++import (+ "errors"+ "fmt"+ "syscall"+ "testing"+)++func TestProbePiIdentitySignalling(t *testing.T) {+ for _, result := range []error{syscall.ESRCH, syscall.EINVAL, syscall.ENOSYS, syscall.EPERM, nil} {+ t.Run(fmt.Sprint(result), func(t *testing.T) {+ calls := 0+ err := probePiIdentitySignalling(func(pid int, version uint32, sig syscall.Signal) error {+ calls+++ if pid != 1<<31-1 || version != 0 || sig != syscall.SIGKILL {+ t.Fatalf("unsafe probe arguments: pid=%d version=%d signal=%v", pid, version, sig)+ }+ return result+ })+ if calls != 1 || (err == nil) != errors.Is(result, syscall.ESRCH) {+ t.Fatalf("result=%v: calls=%d error=%v", result, calls, err)+ }+ if result != nil && !errors.Is(result, syscall.ESRCH) && !errors.Is(err, result) {+ t.Fatalf("lost kernel error: %v", err)+ }+ })+ }+}diff --git a/server/pkg/agent/pi_cleanup_preflight_unix_test.go b/server/pkg/agent/pi_cleanup_preflight_unix_test.gonew file mode 100644index 0000000000..3af15cde44--- /dev/null+++ b/server/pkg/agent/pi_cleanup_preflight_unix_test.go@@ -0,0 +1,84 @@+//go:build linux || darwin++package agent++import (+ "context"+ "errors"+ "log/slog"+ "os"+ "path/filepath"+ "strings"+ "syscall"+ "testing"+ "time"+)++// Inject the reported kernel error; this does not reproduce macOS 12 itself.+func TestPiCleanupPreflightBeforeStart(t *testing.T) {+ for _, provider := range []string{"pi", "omp"} {+ for _, supported := range []bool{false, true} {+ name := provider + "/unsupported"+ if supported {+ name = provider + "/supported"+ }+ t.Run(name, func(t *testing.T) {+ dir := t.TempDir()+ marker := filepath.Join(dir, "started")+ fake := filepath.Join(dir, "fake-runtime")+ writeTestExecutable(t, fake, []byte("#!/bin/sh\nprintf started > \"$PREFLIGHT_MARKER\"\n"+strings.TrimPrefix(piEventStreamScript([]string{`{"type":"agent_end","messages":[]}`}), "#!/bin/sh\n")))+ backend, err := ResolveBackend(provider, Config{ExecutablePath: fake, Env: map[string]string{"PREFLIGHT_MARKER": marker}, Logger: slog.Default()})+ if err != nil {+ t.Fatal(err)+ }+ calls := 0+ backend.(*piBackend).cleanupPreflight = func() error {+ calls+++ return probePiIdentitySignalling(func(int, uint32, syscall.Signal) error {+ if supported {+ return syscall.ESRCH+ }+ return syscall.EINVAL+ })+ }+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)+ defer cancel()+ sessionPath := filepath.Join(dir, "session.jsonl")+ if provider == "omp" && supported {+ if err := os.WriteFile(sessionPath, nil, 0o600); err != nil {+ t.Fatal(err)+ }+ }+ session, err := backend.Execute(ctx, "test", ExecOptions{Cwd: dir, ResumeSessionID: sessionPath})+ if supported {+ if err != nil {+ t.Fatal(err)+ }+ go func() {+ for range session.Messages {+ }+ }()+ result := waitPiResult(t, session, 5*time.Second)+ if result.Status != "completed" {+ t.Fatalf("result=%+v", result)+ }+ if _, err := os.Stat(marker); err != nil {+ t.Fatalf("fixture did not start: %v", err)+ }+ } else {+ if session != nil || !errors.Is(err, syscall.EINVAL) || !strings.Contains(err.Error(), provider+" process cleanup unavailable before start") {+ t.Fatalf("expected actionable startup error, got session=%v error=%v", session != nil, err)+ }+ for _, path := range []string{marker, sessionPath} {+ if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {+ t.Fatalf("startup had side effects at %s: %v", path, err)+ }+ }+ }+ if calls != 1 {+ t.Fatalf("preflight calls=%d, want 1", calls)+ }+ })+ }+ }+}