This commit is contained in:
Ian Gulliver
2026-10-20 22:49:11 -07:00
parent 4f7d05021a
commit 99401670ee
7 changed files with 53 additions and 40 deletions
+6 -8
View File
@@ -11,15 +11,14 @@ import (
"io"
"log/slog"
"os"
"os/exec"
"regexp"
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"
"g.fc.run/ai/byre/internal/mcp"
"g.fc.run/ai/byre/internal/procgroup"
)
type Options struct {
@@ -90,7 +89,7 @@ type rpcResult struct {
}
type Session struct {
cmd *exec.Cmd
cmd *procgroup.Cmd
stdin *os.File
events chan Event
done chan struct{}
@@ -156,11 +155,10 @@ func ReadRateLimits(ctx context.Context, cwd string, env []string) (RateLimitSna
}
func spawn(opts Options) (*Session, error) {
cmd := exec.Command("codex", "app-server", "-c", "check_for_update_on_startup=false")
cmd := procgroup.Command("codex", "app-server", "-c", "check_for_update_on_startup=false")
cmd.Dir = opts.Cwd
cmd.Stderr = os.Stderr
cmd.Env = buildEnv(opts)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
stdinR, stdin, err := os.Pipe()
if err != nil {
@@ -538,7 +536,7 @@ func (s *Session) Close() error {
select {
case <-s.done:
case <-time.After(5 * time.Second):
syscall.Kill(-s.cmd.Process.Pid, syscall.SIGKILL)
s.cmd.Kill()
<-s.done
}
@@ -546,7 +544,7 @@ func (s *Session) Close() error {
}
func (s *Session) Kill() {
syscall.Kill(-s.cmd.Process.Pid, syscall.SIGKILL)
s.cmd.Kill()
<-s.done
s.cmd.Wait()
}
@@ -579,7 +577,7 @@ func (s *Session) write(m message, deadline time.Time) error {
n, err := s.stdin.Write(append(b, '\n'))
if n > 0 && errors.Is(err, os.ErrDeadlineExceeded) {
slog.Error("codex stopped reading its stdin mid-line; killing it", "pid", s.cmd.Process.Pid, "written", n, "len", len(b)+1)
syscall.Kill(-s.cmd.Process.Pid, syscall.SIGKILL)
s.cmd.Kill()
}
return err
}