Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
platform-specific process handling for unix/windows
Dane Schneidere14c8f9855f8
diff --git a/app/cli/cmd/debug.go b/app/cli/cmd/debug.goindex 3914809b..f9da7912 100644--- a/app/cli/cmd/debug.go+++ b/app/cli/cmd/debug.go@@ -86,7 +86,7 @@ func doDebug(cmd *cobra.Command, args []string) {execCmd := exec.Command("sh", "-c", shellCmdStr)execCmd.Dir = cwdexecCmd.Env = os.Environ()- execCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}+ lib.SetPlatformSpecificAttrs(execCmd)pipe, err := execCmd.StdoutPipe()if err != nil {@@ -121,7 +121,7 @@ func doDebug(cmd *cobra.Command, args []string) {interrupted.Store(true)- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGINT); err != nil {+ if err := lib.KillProcessGroup(execCmd, syscall.SIGINT); err != nil {log.Printf("Failed to send SIGINT to process group: %v", err)}@@ -130,7 +130,7 @@ func doDebug(cmd *cobra.Command, args []string) {fmt.Println()color.New(term.ColorHiYellow, color.Bold).Println("π Commands didn't exit after 2 seconds. Sending SIGKILL.")fmt.Println()- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGKILL); err != nil {+ if err := lib.KillProcessGroup(execCmd, syscall.SIGKILL); err != nil {log.Printf("Failed to send SIGKILL to process group: %v", err)}case <-ctx.Done():diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.goindex f0972125..eb2f0a33 100644--- a/app/cli/lib/apply.go+++ b/app/cli/lib/apply.go@@ -388,11 +388,6 @@ func execApplyScript(execCmd.Env = os.Environ()execCmd.Stdin = os.Stdin- // Set up process group isolation- execCmd.SysProcAttr = &syscall.SysProcAttr{- Setpgid: true,- }-// Create a pipe for both stdout and stderrpipe, err := execCmd.StdoutPipe()if err != nil {@@ -401,6 +396,9 @@ func execApplyScript(}execCmd.Stderr = execCmd.Stdout+ // Set platform-specific process attributes+ SetPlatformSpecificAttrs(execCmd)+if err := execCmd.Start(); err != nil {os.Remove(dstPath)onErr("failed to start command: %s", err)@@ -434,8 +432,8 @@ func execApplyScript(fmt.Println()interrupted.Store(true)- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGINT); err != nil {- log.Printf("Failed to send SIGINT to process group: %v", err)+ if err := KillProcessGroup(execCmd, syscall.SIGINT); err != nil {+ log.Printf("Failed to send interrupt signal to process group: %v", err)}select {@@ -443,8 +441,8 @@ func execApplyScript(fmt.Println()color.New(term.ColorHiYellow, color.Bold).Println("π Commands didn't exit after 2 seconds. Sending SIGKILL.")fmt.Println()- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGKILL); err != nil {- log.Printf("Failed to send SIGKILL to process group: %v", err)+ if err := KillProcessGroup(execCmd, syscall.SIGKILL); err != nil {+ log.Printf("Failed to terminate process group: %v", err)}case <-ctx.Done():returndiff --git a/app/cli/lib/apply_unix.go b/app/cli/lib/apply_unix.gonew file mode 100644index 00000000..1b9fb3b4--- /dev/null+++ b/app/cli/lib/apply_unix.go@@ -0,0 +1,18 @@+//go:build !windows++package lib++import (+ "os/exec"+ "syscall"+)++func SetPlatformSpecificAttrs(cmd *exec.Cmd) {+ cmd.SysProcAttr = &syscall.SysProcAttr{+ Setpgid: true,+ }+}++func KillProcessGroup(cmd *exec.Cmd, signal syscall.Signal) error {+ return syscall.Kill(-cmd.Process.Pid, signal)+}diff --git a/app/cli/lib/apply_windows.go b/app/cli/lib/apply_windows.gonew file mode 100644index 00000000..680e5390--- /dev/null+++ b/app/cli/lib/apply_windows.go@@ -0,0 +1,27 @@+//go:build windows++package lib++import (+ "os/exec"+ "strconv"+ "syscall"++ "golang.org/x/sys/windows"+)++func SetPlatformSpecificAttrs(cmd *exec.Cmd) {+ cmd.SysProcAttr = &syscall.SysProcAttr{+ CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,+ }+}++func KillProcessGroup(cmd *exec.Cmd, signal syscall.Signal) error {+ // Windows uses different signals+ if signal == syscall.SIGINT {+ // Ctrl+C event+ return windows.GenerateConsoleCtrlEvent(windows.CTRL_C_EVENT, uint32(cmd.Process.Pid))+ }+ // For SIGKILL equivalent, terminate the process tree+ return exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()+}