Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@
TAGS=$(go -C ../../scripts/profile-tags run . local)
go build -tags "${TAGS}" ./...

# agentop builds on Windows (#1520). Most of its tests drive a Unix shell, a service
# manager or signals, so only the tests of the Windows-specific code and of commands
# that work there run here for now.
- name: Build, vet and test cmd/agentop
working-directory: cmd/agentop
run: |
go build ./...
go vet ./...
go test -run 'TestRefuseUnsupported|TestProcessAlive|TestLockFile|TestWritable|TestTerminateRefusesOnWindows|TestNoRecordWanted|TestWithRecordOff|TestAddRecordOff' .

Check warning on line 189 in .github/workflows/ci.yaml

View workflow job for this annotation

GitHub Actions / YAML Lint

189:151 [line-length] line too long (175 > 150 characters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: go test -run with a name that matches no test still exits 0. If someone renames one of these tests, Windows CI stops running it and nobody sees it. You can add -v and check that each name has a --- PASS line, or you can accept the risk.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed in #2050: the step runs with -v, and fails if any listed name has no --- PASS/--- SKIP line, so a renamed test can't drop out silently.

Assisted-By: Claude (Anthropic AI) noreply@anthropic.com


# The cpex plugin's FFI-backed code sits behind the `cpex` build tag and needs
# cgo plus the pinned libcpex_ffi.a, so no other job compiles or tests it. It
# runs in Alpine to link the same musl asset the authbridge-cpex image does,
Expand Down
3 changes: 1 addition & 2 deletions cmd/agentop/cmd_bob_launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -217,7 +216,7 @@ var bobProbeLaunch = func(shell string) (string, error) {
env = append(env, kv)
}
cmd.Env = append(env, "VSCODE_RESOLVING_ENVIRONMENT=1", "__CFBundleIdentifier="+bobBundleID)
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.SysProcAttr = detached()
out, err := cmd.Output()
if ctx.Err() != nil {
return "", fmt.Errorf("%s did not finish within %s", shell, bobProbeTimeout)
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/cmd_claudecode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ listener:
health_addr: 127.0.0.1:47604
tls_bridge:
mode: enabled
ca_dir: "CADIR"
ca_dir: 'CADIR'
generate_ca: true
pipeline:
outbound:
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/cmd_config_migrate_pricing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ stats:
address: 127.0.0.1:47602
tls_bridge:
mode: enabled
ca_dir: "CADIR"
ca_dir: 'CADIR'
generate_ca: true
pipeline:
outbound:
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_config_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stats:
address: 127.0.0.1:47602
tls_bridge:
mode: enabled
ca_dir: "CADIR"
ca_dir: 'CADIR'
generate_ca: true
pipeline:
outbound:
Expand All @@ -42,7 +42,7 @@ const indentedConfig = ` mode: proxy-sidecar
address: 127.0.0.1:47602
tls_bridge:
mode: enabled
ca_dir: "CADIR"
ca_dir: 'CADIR'
generate_ca: true
pipeline:
outbound:
Expand Down
6 changes: 3 additions & 3 deletions cmd/agentop/cmd_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func TestRunExec_RelaysSIGTERM(t *testing.T) {

// Signal THIS process, the way `timeout` signals agentop. runExec's relay must
// pass it to the child.
if err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); err != nil {
if err := killPID(syscall.Getpid(), syscall.SIGTERM); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -1073,10 +1073,10 @@ func TestRunExec_SurvivesSIGINT(t *testing.T) {

// Signal THIS process, as a tty would. If runExec did not catch SIGINT, the
// whole test binary would die here rather than one test failing.
if err := syscall.Kill(syscall.Getpid(), syscall.SIGINT); err != nil {
if err := killPID(syscall.Getpid(), syscall.SIGINT); err != nil {
t.Fatal(err)
}
if err := syscall.Kill(child, syscall.SIGTERM); err != nil {
if err := killPID(child, syscall.SIGTERM); err != nil {
t.Fatal(err)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/agentop/cmd_observe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"

"github.com/rossoctl/cortex/cmd/agentop/tui"
Expand Down Expand Up @@ -526,7 +525,7 @@ func runAgentopChild(t *testing.T, args ...string) (string, error) {
// Not every argv exits on its own: one that reaches tui.Run opens the terminal. In
// a session of its own the child has no controlling terminal, so that open fails
// and the child exits.
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.SysProcAttr = sessionAttr()
out, err := cmd.CombinedOutput()
return string(out), err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_service_launchd_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestServiceLifecycle_RealLaunchd(t *testing.T) {
// pid is the supervisor's, and it stays the same while a new proxy takes over.
// syscall.Kill gets a pid from childPID, which never returns one below 2: kill(2)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This comment still says syscall.Kill, but the call below is now killPID. It's still accurate on Unix, since killPID is syscall.Kill there, so feel free to leave it.

// reads 0 as this process's own group, which holds `go test` too.
if err := syscall.Kill(proxy, syscall.SIGKILL); err != nil {
if err := killPID(proxy, syscall.SIGKILL); err != nil {
t.Fatalf("kill -9 proxy %d: %v", proxy, err)
}
// The wait follows whatever supervisor launchd reports now, so that a broken
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestServiceLifecycle_RealLaunchd(t *testing.T) {
t.Error("after stop the label is not disabled, so the next login would start Cortex again")
}
for _, pid := range []int{sup, newProxy} {
if !waitUntil(10*time.Second, func() bool { return syscall.Kill(pid, 0) != nil }) {
if !waitUntil(10*time.Second, func() bool { return killPID(pid, 0) != nil }) {
t.Errorf("process %d was still running 10s after stop", pid)
}
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/agentop/cmd_service_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"runtime"
"strconv"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -578,11 +577,11 @@ func resolvePath(p string) (string, error) {
// before giving up rather than reporting success on a process that still holds the
// ports. A current one is gone in well under a second.
func stopPID(pid int) error {
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
if err := terminate(pid); err != nil {
return err
}
for i := 0; i < 90; i++ {
if err := syscall.Kill(pid, 0); err != nil {
if !processAlive(pid) {
return nil
}
time.Sleep(200 * time.Millisecond)
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_service_systemd_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestSupervisorRestartsAfterCrash_RealSystemd(t *testing.T) {
// on a slim image missing /bin/kill, that would surface as a failed test with
// AGENTOP_SYSTEMD_TESTS=required set, rather than the environment-problem skip it
// actually is.
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
if err := killPID(pid, syscall.SIGKILL); err != nil {
t.Fatalf("kill -9 %d: %v", pid, err)
}

Expand Down Expand Up @@ -506,7 +506,7 @@ func TestServiceLifecycle_RealSystemd(t *testing.T) {
if !ok {
t.Fatalf("no MainPID to kill; systemd says %+v", unit())
}
if err := syscall.Kill(pid, syscall.SIGKILL); err != nil {
if err := killPID(pid, syscall.SIGKILL); err != nil {
t.Fatalf("kill -9 %d: %v", pid, err)
}
if !waitUntil(3*time.Second, func() bool { return unit().sub == "auto-restart" }) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestSetupSignalsCatchesInterruptAndTerminate(t *testing.T) {
defer signal.Stop(guard)
sigs, stop := setupSignals()
for _, s := range []syscall.Signal{syscall.SIGINT, syscall.SIGTERM} {
if err := syscall.Kill(os.Getpid(), s); err != nil {
if err := killPID(os.Getpid(), s); err != nil {
t.Fatal(err)
}
<-guard
Expand All @@ -167,7 +167,7 @@ func TestSetupSignalsCatchesInterruptAndTerminate(t *testing.T) {
// os/signal hands each signal to every subscriber before it takes the next, so
// once guard has the second, any send of the first to sigs has happened.
for _, s := range []syscall.Signal{syscall.SIGTERM, syscall.SIGINT} {
if err := syscall.Kill(os.Getpid(), s); err != nil {
if err := killPID(os.Getpid(), s); err != nil {
t.Fatal(err)
}
<-guard
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/cmd_uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,7 @@ func TestUninstallARealSigintStillShowsTheCursor(t *testing.T) {
run: func(*checklist.Running) (string, []string, error) { fn(); return "done", nil, nil }}
}
return []removal{step("first", func() {}), step("second", func() {
_ = syscall.Kill(os.Getpid(), syscall.SIGINT)
_ = killPID(os.Getpid(), syscall.SIGINT)
<-arrived
}), step("third", func() {})}
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/agentop/cmd_update_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/rossoctl/cortex/cmd/agentop/updatestatus"
Expand Down Expand Up @@ -52,12 +51,9 @@ func lockUpdate(cortexDir string) (unlock func(), held bool, err error) {
if err != nil {
return nil, false, err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
if held, err := lockFile(f); held || err != nil {
_ = f.Close()
if errors.Is(err, syscall.EWOULDBLOCK) {
return nil, true, nil
}
return nil, false, err
return nil, held, err
}
return func() { _ = f.Close() }, false, nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func TestUpdateScriptExitsWithTheInstallersStatusOnInterrupt(t *testing.T) {
installStub(t, "sh", "#!"+dash+"\nexec "+dash+" \"$@\"\n")
installStub(t, "curl", "#!/bin/sh\nprintf '%s\\n' \"trap 'exit 3' INT\" 'echo ready' 'sleep 5 >/dev/null & wait'\n")
cmd := exec.Command("sh", "-c", updateScript, installerURL)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.SysProcAttr = groupAttr()
out, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
Expand All @@ -498,7 +498,7 @@ func TestUpdateScriptExitsWithTheInstallersStatusOnInterrupt(t *testing.T) {
if line, _ := bufio.NewReader(out).ReadString('\n'); line != "ready\n" {
t.Fatalf("installer said %q", line)
}
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT)
_ = killPID(-cmd.Process.Pid, syscall.SIGINT)
_ = cmd.Wait()
if code := cmd.ProcessState.ExitCode(); code != 3 {
t.Errorf("exit %d, want the installer's 3", code)
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/codex_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var codexStopUpdater = func(codexHome string) error {
if codexIsRecorded(u) != codexRecordMatches {
return nil
}
if err := syscall.Kill(-u.PID, syscall.SIGTERM); err != nil && !errors.Is(err, syscall.ESRCH) {
if err := terminateGroup(u.PID); err != nil {
return fmt.Errorf("stopping Codex's updater (pid %d): %w", u.PID, err)
}
deadline := time.Now().Add(codexUpdaterStopTimeout)
Expand Down
6 changes: 3 additions & 3 deletions cmd/agentop/codex_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func TestCodexRun(t *testing.T) {
b, _ := os.ReadFile(pids) //nolint:gosec // the test's own file
for _, f := range strings.Fields(string(b)) {
if pid, err := strconv.Atoi(f); err == nil {
_ = syscall.Kill(pid, syscall.SIGKILL)
_ = killPID(pid, syscall.SIGKILL)
}
}
})
Expand All @@ -703,13 +703,13 @@ func TestCodexRun(t *testing.T) {
func startGroupLeader(t *testing.T) (*exec.Cmd, <-chan struct{}) {
t.Helper()
cmd := exec.Command("/bin/sh", "-c", "sleep 30")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.SysProcAttr = groupAttr()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
exited := make(chan struct{})
go func() { _ = cmd.Wait(); close(exited) }()
t.Cleanup(func() { _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); <-exited })
t.Cleanup(func() { _ = killPID(-cmd.Process.Pid, syscall.SIGKILL); <-exited })
prev := codexDaemonAlive
t.Cleanup(func() { codexDaemonAlive = prev })
codexDaemonAlive = func(pid int) bool {
Expand Down
26 changes: 26 additions & 0 deletions cmd/agentop/killpid_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !windows

package main

import (
"syscall"

"golang.org/x/sys/unix"
)

// Process helpers for tests, for Unix. killpid_windows_test.go has the Windows half (#1520).

// killPID sends sig to pid (to the group led by -pid when pid is negative).
func killPID(pid int, sig syscall.Signal) error { return syscall.Kill(pid, sig) }

// groupAttr starts a child as the leader of a process group of its own.
func groupAttr() *syscall.SysProcAttr { return &syscall.SysProcAttr{Setpgid: true} }

// sessionAttr starts a child in a session of its own.
func sessionAttr() *syscall.SysProcAttr { return &syscall.SysProcAttr{Setsid: true} }

// mkfifo makes a named pipe at path.
func mkfifo(path string, mode uint32) error { return syscall.Mkfifo(path, mode) }

// getsid is pid's session id.
func getsid(pid int) (int, error) { return unix.Getsid(pid) }
24 changes: 24 additions & 0 deletions cmd/agentop/killpid_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build windows

package main

import (
"errors"
"syscall"
)

// The Windows half of killpid_unix_test.go. Windows has no signals to send to another process,
// no process groups of the Unix kind and no sessions. The tests that use these need a Unix
// service or shell and do not run on Windows; these keep them compiling there (#1520).

var errNoUnixProcess = errors.New("not on Windows")

func killPID(int, syscall.Signal) error { return errNoUnixProcess }

func groupAttr() *syscall.SysProcAttr { return nil }

func sessionAttr() *syscall.SysProcAttr { return nil }

func getsid(int) (int, error) { return 0, errNoUnixProcess }

func mkfifo(string, uint32) error { return errNoUnixProcess }
4 changes: 4 additions & 0 deletions cmd/agentop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"

Expand Down Expand Up @@ -129,6 +130,9 @@ func main() {
// argument selects a subcommand, and anything else falls through to the
// terminal UI, preserving the original flags-only invocation.
if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
if refuseUnsupported(runtime.GOOS, os.Args[1], os.Stderr) {
os.Exit(1)
}
switch os.Args[1] {
case "tools":
os.Exit(runTools(os.Args[2:], os.Stdout, os.Stderr))
Expand Down
28 changes: 28 additions & 0 deletions cmd/agentop/platform_guard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"io"
)

// unsupportedOnWindows are the subcommands that install, run or replace Cortex as a service.
// On Windows they need a service backend, a way to stop the proxy without killing it, and a
// way to replace a running .exe, none of which exists yet (#1520). Everything else in agentop
// talks to a running proxy over HTTP, or edits a settings file, and works there.
var unsupportedOnWindows = map[string]bool{
"service": true,
"setup": true,
"uninstall": true,
"update": true,
}

// refuseUnsupported reports whether subcommand cmd cannot run on goos, and says so on stderr.
// goos is a parameter so the rule is testable on every OS.
func refuseUnsupported(goos, cmd string, stderr io.Writer) bool {
if goos != "windows" || !unsupportedOnWindows[cmd] {
return false
}
fmt.Fprintf(stderr, "agentop: `agentop %s` is not supported on Windows yet (#1520).\n"+
" Run the proxy in a terminal instead: cortex --local\n", cmd)
return true
}
Loading
Loading