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
17 changes: 15 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,26 @@ jobs:

# 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.
# that work there run here for now. `go test -run` passes when a name matches nothing,
# so each listed name must also have run: a renamed test fails the step instead of
# silently dropping out. Every entry is a full test name, matched exactly, not a prefix
# that stands for several tests.
- name: Build, vet and test cmd/agentop
working-directory: cmd/agentop
run: |
set -euo pipefail
go build ./...
go vet ./...
go test -run 'TestRefuseUnsupported|TestProcessAlive|TestLockFile|TestWritable|TestTerminateRefusesOnWindows|TestNoRecordWanted|TestWithRecordOff|TestAddRecordOff' .
tests="TestRefuseUnsupported TestProcessAlive TestLockFile TestWritable TestTerminateRefusesOnWindows
TestNoRecordWanted TestWithRecordOff
TestAddRecordOff_RefusesASettingsOverride TestAddRecordOff_AppendsToTheEnvironment
TestAddRecordOff_RefusesAnUnreadableSettingsFile TestAddRecordOff_ChecksClaudeConfigDir
TestAddRecordOff_RefusesAChildThatIsNotClaudeCode"
go test -v -run "^($(echo ${tests} | tr ' ' '|'))\$" . | tee "${RUNNER_TEMP}/agentop-tests.txt"
for t in ${tests}; do
grep -qE "^--- (PASS|SKIP): ${t} \(" "${RUNNER_TEMP}/agentop-tests.txt" \
|| { echo "::error::${t} did not run"; exit 1; }
done

# 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
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/cmd_doctor_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestDoctorChecksABackgroundInstallWhoseProxyIsGone(t *testing.T) {
t.Fatal("setup --no-service left no proxy.pid")
}
killAndReap(pid)
if alive(pid) {
if processAlive(pid) {
t.Fatalf("fixture: pid %d is still alive", pid)
}
writeHomeFile(t, pidFile, c.pidfile(pid))
Expand Down
2 changes: 1 addition & 1 deletion cmd/agentop/cmd_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ func stopIfAlive(pid int) error {
if pid <= 0 {
return nil
}
if err := stopPID(pid); err != nil && alive(pid) {
if err := stopPID(pid); err != nil && processAlive(pid) {
return err
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/cmd_uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestUninstallStopsABackgroundCortex(t *testing.T) {
stopProcessOnCleanup(t, &pid)
sc.setUp(t, "--no-service")
pidFile := filepath.Join(sc.home, ".cortex", "proxy.pid")
if pid = readPIDFile(pidFile); !alive(pid) {
if pid = readPIDFile(pidFile); !processAlive(pid) {
t.Fatal("fixture: setup --no-service left no live proxy")
}
// ps names the stub's sleep as cortex, as it names the real background proxy.
Expand All @@ -369,7 +369,7 @@ func TestUninstallStopsABackgroundCortex(t *testing.T) {
if row := consentRow(out, "stop and remove"); !strings.Contains(row, " the background Cortex ") || !strings.HasSuffix(row, " ~/.cortex/proxy.pid") {
t.Errorf("the consent row is %q, want the background Cortex and its pidfile", row)
}
if alive(pid) {
if processAlive(pid) {
t.Errorf("the background Cortex (pid %d) is still running", pid)
}
if _, err := os.Stat(pidFile); !os.IsNotExist(err) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/agentop/setup_prior_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func detachedSleeperAt(t *testing.T, path string) int {

// ranFrom reports whether pid is alive and was started as path, by ps's argv[0].
func ranFrom(pid int, path string) bool {
if !alive(pid) {
if !processAlive(pid) {
return false
}
out, err := exec.Command("ps", "-p", strconv.Itoa(pid), "-o", "args=").Output()
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestSetupStopsAPreRenameBackgroundProxyAndRestartsItOnRollback(t *testing.T
if code != 1 {
t.Errorf("a failed run exited %d", code)
}
if alive(newPID) {
if processAlive(newPID) {
t.Error("the rollback left the new cortex running")
}
if !ranFrom(restarted, filepath.Join(bin, "authbridge-proxy")) || restarted == old {
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestSetupRollbackRestartsTheBackgroundCortexBesideAStoppedService(t *testin
running := filepath.Join(t.TempDir(), "running")
sc := newSetupScene(t, func(w http.ResponseWriter, _ *http.Request) {
_, err := os.Stat(running)
if err != nil && !alive(readPIDFile(filepath.Join(os.Getenv("HOME"), ".cortex", "proxy.pid"))) {
if err != nil && !processAlive(readPIDFile(filepath.Join(os.Getenv("HOME"), ".cortex", "proxy.pid"))) {
w.WriteHeader(http.StatusServiceUnavailable)
}
})
Expand Down
8 changes: 3 additions & 5 deletions cmd/agentop/setup_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ func ourProxy(binDir, pidFile string, pid int, exe string) bool {
return samePath(exe, filepath.Join(binDir, proxyBinary)) || samePath(exe, filepath.Join(binDir, preRenameBinary))
}

func alive(pid int) bool { return processAlive(pid) }

// psComm is ps's comm for pid, and whether ps answered at all.
func psComm(pid int) (string, bool) {
out, err := exec.Command("ps", "-o", "comm=", "-p", strconv.Itoa(pid)).Output()
Expand All @@ -221,7 +219,7 @@ func psComm(pid int) (string, bool) {
// and ps either cannot see it (sandbox-blind) or calls it cortex.
func proxyRunning(pidFile string) (int, bool) {
pid := readPIDFile(pidFile)
if !alive(pid) {
if !processAlive(pid) {
return 0, false
}
comm, seen := psComm(pid)
Expand All @@ -236,7 +234,7 @@ func proxyRunning(pidFile string) (int, bool) {
// background proxy. proxyRunning does not count it: ps does not call it cortex.
func preRenameProxyRunning(binDir, pidFile string) (int, bool) {
pid := readPIDFile(pidFile)
if !alive(pid) {
if !processAlive(pid) {
return 0, false
}
exe := setupPIDExePath(pid)
Expand All @@ -250,7 +248,7 @@ func preRenameProxyRunning(binDir, pidFile string) (int, bool) {
// process ps cannot name, which may still re-exec a pre-rename binary.
func pidfileProcessUnnamed(pidFile string) bool {
pid := readPIDFile(pidFile)
if !alive(pid) {
if !processAlive(pid) {
return false
}
_, seen := psComm(pid)
Expand Down
4 changes: 2 additions & 2 deletions cmd/agentop/setup_step_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (s serviceStep) apply(env *setupEnv, act *checklist.Running) (string, undo,
// once it is gone: a failure before the stop leaves it running, and a second copy
// would take proxy.pid from it. Asked up to a minute after the stop, so a live pid
// that now runs something else is gone too.
case adopt > 0 && (!alive(adopt) || setupPIDExePath(adopt) != adoptSeen):
case adopt > 0 && (!processAlive(adopt) || setupPIDExePath(adopt) != adoptSeen):
env.priorDesc = "the previous background Cortex is running again"
env.priorManual = startByHand(env, adoptExe)
env.restorePrior = func() error {
Expand Down Expand Up @@ -459,7 +459,7 @@ func (serviceStep) applyUnsupervised(env *setupEnv) (string, undo, error) {
err = se
}
u := undo{label: "the background proxy", fn: func() error {
if alive(pid) {
if processAlive(pid) {
if err := stopPID(pid); err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/agentop/setup_step_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestServiceStepUnsupervisedRestoreNamesItsStartByHand(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if alive(old) {
if processAlive(old) {
t.Fatal("fixture: the step did not stop the background proxy")
}
want := "sh -c 'nohup ~/.local/bin/cortex --local --supervise >> ~/.cortex/proxy.log 2>&1 & echo $! > ~/.cortex/proxy.pid'"
Expand All @@ -217,7 +217,7 @@ func TestServiceStepUnsupervisedRestoreNamesItsStartByHand(t *testing.T) {
func stopProcessOnCleanup(t *testing.T, pid *int) {
t.Helper()
t.Cleanup(func() {
if alive(*pid) {
if processAlive(*pid) {
killAndReap(*pid)
}
})
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestServiceStepRunsUnsupervisedInASandbox(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !alive(pid) {
if !processAlive(pid) {
t.Fatal("no background proxy")
}
if u.manual != "kill $(cat ~/.cortex/proxy.pid)" {
Expand All @@ -259,7 +259,7 @@ func TestServiceStepRunsUnsupervisedInASandbox(t *testing.T) {
if err := u.fn(); err != nil {
t.Fatal(err)
}
if alive(pid) {
if processAlive(pid) {
t.Error("undo left the background proxy running")
return
}
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestSupervisedHolderWhenPsCannotNameTheParent(t *testing.T) {
t.Fatal(err)
}
if got := supervisedHolder(pidFile, 31337); got != tc.want {
t.Errorf("proxy.pid %d alive=%v: supervisedHolder = %v, want %v", tc.pf, alive(tc.pf), got, tc.want)
t.Errorf("proxy.pid %d alive=%v: supervisedHolder = %v, want %v", tc.pf, processAlive(tc.pf), got, tc.want)
}
}
}
Expand Down Expand Up @@ -924,8 +924,8 @@ func TestServiceStepBackgroundRestoreRefusesBesideALoadedUnhealthyJob(t *testing
t.Fatalf("plan = %+v %v", p, prob)
}
_, u, err := serviceStep{}.apply(env, nil)
if err == nil || alive(old) || env.restorePrior == nil {
t.Fatalf("fixture: apply = %v, background proxy alive %v, restorePrior set %v", err, alive(old), env.restorePrior != nil)
if err == nil || processAlive(old) || env.restorePrior == nil {
t.Fatalf("fixture: apply = %v, background proxy alive %v, restorePrior set %v", err, processAlive(old), env.restorePrior != nil)
}
failingUnload(t, loaded)
if err := u.fn(); err == nil {
Expand Down Expand Up @@ -969,8 +969,8 @@ func TestStartByHandRunsFromAShell(t *testing.T) {
stopProcessOnCleanup(t, &pid)
waitStarted()
log, _ := os.ReadFile(filepath.Join(env.cortexDir, "proxy.log"))
if !alive(pid) || !strings.Contains(string(log), "started --local --supervise") {
t.Errorf("%s: proxy.pid %d alive=%v, proxy.log %q", sh, pid, alive(pid), log)
if !processAlive(pid) || !strings.Contains(string(log), "started --local --supervise") {
t.Errorf("%s: proxy.pid %d alive=%v, proxy.log %q", sh, pid, processAlive(pid), log)
}
}
}
Expand Down
Loading