Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix: make the Linux test suite pass where systemd is PID 1

shukieshahd2684e1a3143
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 490aeb01..5d703945 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -189,6 +189,9 @@ jobs:
run: |
go install go.opentelemetry.io/collector/cmd/[email protected]
cd collector-builder
+ # builder mkdirs its output path without -p, so the parent has to exist. A fresh checkout
+ # has no dist/ at all, which is why this only failed in CI.
+ mkdir -p dist
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 "$(go env GOPATH)/bin/builder" --config builder.yaml
mkdir -p dist/beacon-otelcol/linux_amd64
cp dist/beacon-otelcol/beacon-otelcol dist/beacon-otelcol/linux_amd64/beacon-otelcol
@@ -211,6 +214,8 @@ jobs:
run: |
go install go.opentelemetry.io/collector/cmd/[email protected]
cd collector-builder
+ # builder mkdirs its output path without -p, so the parent has to exist.
+ mkdir -p dist
for arch in amd64 arm64; do
GOOS=linux GOARCH=$arch CGO_ENABLED=0 "$(go env GOPATH)/bin/builder" --config builder.yaml
mkdir -p dist/beacon-otelcol/linux_$arch
diff --git a/beacon-sandbox/runner/runner.go b/beacon-sandbox/runner/runner.go
index b073e9c2..bfe629fd 100644
--- a/beacon-sandbox/runner/runner.go
+++ b/beacon-sandbox/runner/runner.go
@@ -340,13 +340,24 @@ func Run(ctx context.Context, p sandbox.Provider, sc scenario.Scenario, opts Opt
// argvSamplerScript for why this cannot be a post-session scan.
if r, err := p.Exec(ctx, inst, "cat "+shq(argvVerdictPath)+" 2>/dev/null || true",
sandbox.ExecOpts{User: "root", Timeout: time.Minute}); err == nil {
+ sawAgent := strings.Contains(r.Stdout, "saw_agent=1")
+ art.Meta["argv_saw_agent"] = fmt.Sprintf("%v", sawAgent)
switch {
case strings.Contains(r.Stdout, "ARGV_LEAK"):
+ // A leak is a leak regardless: the sampler found the key on a command line, which is
+ // positive evidence and needs no corroboration.
art.SecretInArgv, art.ArgvCheckRan = true, true
art.Meta["argv_leak_detail"] = oneLine(r.Stdout)
logf("argv leak detail: %s", art.Meta["argv_leak_detail"])
- case strings.Contains(r.Stdout, "ARGV_CLEAN"):
+ case strings.Contains(r.Stdout, "ARGV_CLEAN") && sawAgent:
art.SecretInArgv, art.ArgvCheckRan = false, true
+ case strings.Contains(r.Stdout, "ARGV_CLEAN"):
+ // Clean, but the sampler never had the agent in view -- so it searched the wrong
+ // processes and proves nothing. Reported unverified rather than clean, which is the
+ // whole point of tracking whether a check could run separately from what it found.
+ art.ArgvCheckRan = false
+ logf("argv scan found nothing, but never observed an agent process: " +
+ "the result is unverified, not clean")
default:
// The sampler never wrote a verdict, so nothing was observed and the result
// proves nothing. Reported as unverified rather than clean.
@@ -574,21 +585,30 @@ scan() {
END{exit !found}'
}
n=0
+saw_agent=0
# Bounded so a leaked sandbox cannot spin forever; the instance is destroyed long before this.
while [ "$n" -lt 900 ]; do
n=$((n+1))
- if ps -eo args= 2>/dev/null | scan; then
- echo "ARGV_LEAK samples=$n via=ps" > "$VERDICT"
+ procs="$(ps -eo args= 2>/dev/null || true)"
+ # Whether the agent was ever in view at all. A clean verdict from a sampler that never saw the
+ # process holding the key is not evidence of anything -- and in the nested lane, where the agent
+ # runs in a child PID namespace, "the parent namespace sees its children" is an assumption worth
+ # confirming rather than trusting.
+ case "$procs" in *claude*) saw_agent=1 ;; esac
+ if printf '%%s\n' "$procs" | scan; then
+ echo "ARGV_LEAK samples=$n saw_agent=$saw_agent via=ps" > "$VERDICT"
exit 0
fi
for f in /proc/[0-9]*/cmdline; do
[ -r "$f" ] || continue
- if tr '\0' '\n' < "$f" 2>/dev/null | scan; then
- echo "ARGV_LEAK samples=$n via=proc" > "$VERDICT"
+ cmd="$(tr '\0' '\n' < "$f" 2>/dev/null || true)"
+ case "$cmd" in *claude*) saw_agent=1 ;; esac
+ if printf '%%s\n' "$cmd" | scan; then
+ echo "ARGV_LEAK samples=$n saw_agent=$saw_agent via=proc" > "$VERDICT"
exit 0
fi
done
- echo "ARGV_CLEAN samples=$n" > "$VERDICT"
+ echo "ARGV_CLEAN samples=$n saw_agent=$saw_agent" > "$VERDICT"
sleep 1
done
SAMPLER
diff --git a/beacon-sandbox/runner/runner_test.go b/beacon-sandbox/runner/runner_test.go
index 5cb96083..b284d179 100644
--- a/beacon-sandbox/runner/runner_test.go
+++ b/beacon-sandbox/runner/runner_test.go
@@ -2,6 +2,9 @@ package runner
import (
"fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
"strings"
"testing"
@@ -190,3 +193,47 @@ func TestSessionOutcomeNeverInventsAFailure(t *testing.T) {
}
}
}
+
+// The sampler is a shell program assembled in Go, so a syntax error in it is invisible to the
+// compiler and would surface as a silent "the check did not run" -- which the verdict reports as
+// unverified rather than failing loudly. Checked here with the host's own shell.
+func TestArgvSamplerScriptIsValidShell(t *testing.T) {
+ sh, err := exec.LookPath("sh")
+ if err != nil {
+ t.Skip("no sh on PATH")
+ }
+ f := filepath.Join(t.TempDir(), "sampler.sh")
+ if err := os.WriteFile(f, []byte(argvSamplerScript()), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ out, err := exec.Command(sh, "-n", f).CombinedOutput()
+ if err != nil {
+ t.Fatalf("generated sampler is not valid shell: %v\n%s", err, out)
+ }
+ // The inner sampler body is a heredoc, which `sh -n` does not parse. Extract and check it too,
+ // since that is where the polling loop lives.
+ body := argvSamplerScript()
+ start := strings.Index(body, "#!/bin/sh")
+ end := strings.Index(body, "\nSAMPLER\n")
+ if start < 0 || end < 0 || end < start {
+ t.Fatal("could not locate the inner sampler body; this test is no longer checking it")
+ }
+ inner := filepath.Join(t.TempDir(), "inner.sh")
+ if err := os.WriteFile(inner, []byte(body[start:end]), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if out, err := exec.Command(sh, "-n", inner).CombinedOutput(); err != nil {
+ t.Fatalf("inner sampler body is not valid shell: %v\n%s", err, out)
+ }
+}
+
+// A clean verdict from a sampler that never saw an agent process must read as unverified. This is
+// the invariant the whole tool rests on: silence means verified-clean, never "we did not look."
+func TestArgvSamplerEmitsWhetherItSawTheAgent(t *testing.T) {
+ script := argvSamplerScript()
+ for _, want := range []string{"saw_agent=0", "saw_agent=$saw_agent", "*claude*"} {
+ if !strings.Contains(script, want) {
+ t.Errorf("sampler no longer records whether the agent was in view (missing %q)", want)
+ }
+ }
+}
diff --git a/cli/beacon/internal/endpoint/diagnostics/diagnostics_test.go b/cli/beacon/internal/endpoint/diagnostics/diagnostics_test.go
index e93692d2..d31d8467 100644
--- a/cli/beacon/internal/endpoint/diagnostics/diagnostics_test.go
+++ b/cli/beacon/internal/endpoint/diagnostics/diagnostics_test.go
@@ -3,7 +3,6 @@ package diagnostics
import (
"os"
"path/filepath"
- "runtime"
"testing"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
@@ -89,13 +88,17 @@ func TestRunAndHasFailures(t *testing.T) {
if err := os.WriteFile(cfg.LogPath, []byte("{}\n"), 0644); err != nil {
t.Fatalf("write log: %v", err)
}
- if runtime.GOOS == "darwin" {
- plistPath := servicePlistPathForTest(true)
- if err := os.MkdirAll(filepath.Dir(plistPath), 0755); err != nil {
- t.Fatalf("mkdir plist dir: %v", err)
+ // Run names the service-definition check after whichever manager the host actually uses, so
+ // the fixture has to satisfy that manager rather than a hardcoded one. Keying off GOOS was
+ // enough while launchd was the only backend; it made this test pass on any host without
+ // systemd and fail on a GitHub runner, where systemd genuinely is PID 1 and a unit check runs
+ // that nothing had created a unit for.
+ if path := serviceUnitPathForTest(true); path != "" {
+ if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
+ t.Fatalf("mkdir service dir: %v", err)
}
- if err := os.WriteFile(plistPath, []byte("<plist/>"), 0644); err != nil {
- t.Fatalf("write plist: %v", err)
+ if err := os.WriteFile(path, []byte("# fixture\n"), 0644); err != nil {
+ t.Fatalf("write service definition: %v", err)
}
}
@@ -127,6 +130,21 @@ func TestLaunchPlistPathMatchesServiceManager(t *testing.T) {
// servicePlistPathForTest mirrors what the service package now owns, so these tests keep
// exercising the launchd check without diagnostics duplicating the path logic.
+// serviceUnitPathForTest is the definition file the host's own service manager expects, or empty
+// when it has none -- the supervised backend has no unit file, so there is nothing to create.
+func serviceUnitPathForTest(userMode bool) string {
+ mgr := service.Manager{UserMode: userMode}
+ switch mgr.ResolvedKind() {
+ case service.KindLaunchd, service.KindSystemd:
+ path, err := mgr.UnitPath()
+ if err != nil {
+ return ""
+ }
+ return path
+ }
+ return ""
+}
+
func servicePlistPathForTest(userMode bool) string {
path, err := (service.Manager{UserMode: userMode, Kind: service.KindLaunchd}).UnitPath()
if err != nil {
diff --git a/cli/beacon/internal/endpoint/harness/vscode_test.go b/cli/beacon/internal/endpoint/harness/vscode_test.go
index a0dc1838..e7541713 100644
--- a/cli/beacon/internal/endpoint/harness/vscode_test.go
+++ b/cli/beacon/internal/endpoint/harness/vscode_test.go
@@ -10,7 +10,7 @@ import (
func TestVSCodeUserSettingsPathByOS(t *testing.T) {
home := t.TempDir()
- t.Setenv("HOME", home)
+ isolateUserConfig(t, home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg"))
@@ -37,7 +37,7 @@ func TestVSCodeUserSettingsPathByOS(t *testing.T) {
func TestConfigureVSCodePreservesSettingsAndDisablesContentCaptureByDefault(t *testing.T) {
home := t.TempDir()
- t.Setenv("HOME", home)
+ isolateUserConfig(t, home)
// VS Code stores user settings per platform; assert the platform-correct location rather
// than the macOS one, which silently passed only because tests never ran on Linux.
path := vscodeSettingsPathForTest(home)
@@ -102,6 +102,18 @@ func TestVSCodeOTelStatus(t *testing.T) {
// vscodeSettingsPathForTest mirrors vscodeUserDataDirsForOS so these tests exercise the real
// per-platform layout.
+// isolateUserConfig points every user-config lookup at a temporary home.
+//
+// HOME alone is not enough on Linux: VS Code settings live under XDG_CONFIG_HOME when it is set, and
+// GitHub runners set it. A test that only overrode HOME therefore read and wrote the *real* user's
+// settings path, which passed locally and failed in CI -- and would have been a test quietly editing
+// a developer's own VS Code configuration.
+func isolateUserConfig(t *testing.T, home string) {
+ t.Helper()
+ t.Setenv("HOME", home)
+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
+}
+
func vscodeSettingsPathForTest(home string) string {
switch runtime.GOOS {
case "darwin":
diff --git a/cli/beacon/internal/endpoint/service/service.go b/cli/beacon/internal/endpoint/service/service.go
index d24830e6..01951dff 100644
--- a/cli/beacon/internal/endpoint/service/service.go
+++ b/cli/beacon/internal/endpoint/service/service.go
@@ -67,9 +67,11 @@ func ParseKind(s string) (Kind, error) {
// EnableLingerIfNeeded makes a systemd --user unit survive logout, where that applies.
//
-// Returns ("", ) for every other case -- system mode, and every backend but systemd -- so the
-// caller records nothing rather than reporting a step that was never relevant. launchd needs no
-// equivalent: its gui/<uid> domain persists for the login session by itself.
+// The empty detail means exactly one thing: linger does not apply here -- system mode, or any
+// backend but systemd. Every applicable case reports a detail, success included, so a caller can
+// use an empty detail to mean "we did not try" without that also swallowing "we tried and it
+// worked". launchd needs no equivalent: its gui/<uid> domain persists for the login session by
+// itself.
func (m Manager) EnableLingerIfNeeded() (bool, string) {
if !m.UserMode || m.resolvedKind() != KindSystemd {
return false, ""
diff --git a/cli/beacon/internal/endpoint/service/service_test.go b/cli/beacon/internal/endpoint/service/service_test.go
index a61a438e..4f9ad62a 100644
--- a/cli/beacon/internal/endpoint/service/service_test.go
+++ b/cli/beacon/internal/endpoint/service/service_test.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
+ "os/user"
"path/filepath"
"runtime"
"strings"
@@ -596,3 +597,37 @@ func TestServiceNounNamesEachBackend(t *testing.T) {
t.Error("an unrecognised kind must still yield a description")
}
}
+
+// The success path is the one that used to report nothing. EnableLinger returned an empty detail on
+// success, and the install path reads an empty detail as "linger does not apply" -- so linger
+// actually being enabled was the single outcome the manifest dropped.
+func TestEnableLingerReportsSuccessAndFailureDistinctly(t *testing.T) {
+ if !systemdIsInit() {
+ t.Skip("needs systemd as PID 1; linger does not apply otherwise")
+ }
+ u, err := user.Current()
+ if err != nil || u.Username == "" {
+ t.Skip("needs a resolvable current user")
+ }
+ ok, detail := EnableLinger(u.Username)
+ if detail == "" {
+ t.Fatalf("EnableLinger(%q) = %v with no detail; an empty detail means "+
+ "\"does not apply\" to the caller, so every applicable outcome must say something",
+ u.Username, ok)
+ }
+}
+
+// The contract the install path depends on: an empty detail means "not applicable", never
+// "succeeded" and never "failed quietly".
+func TestEnableLingerNeverReportsAnEmptyDetailWhenItApplies(t *testing.T) {
+ // The non-applicable cases, which are the only ones allowed to be silent.
+ if _, detail := EnableLinger(""); detail == "" {
+ t.Error("an empty username is a reportable problem, not a silent skip")
+ }
+ if systemdIsInit() {
+ return
+ }
+ if _, detail := EnableLinger("anyone"); detail == "" {
+ t.Error("without systemd as PID 1, EnableLinger should still say why it did nothing")
+ }
+}
diff --git a/cli/beacon/internal/endpoint/service/systemd.go b/cli/beacon/internal/endpoint/service/systemd.go
index a688740b..c8fc3dc9 100644
--- a/cli/beacon/internal/endpoint/service/systemd.go
+++ b/cli/beacon/internal/endpoint/service/systemd.go
@@ -208,10 +208,20 @@ func EnableLinger(username string) (bool, string) {
if username == "" {
return false, "no username provided"
}
- if out, err := runLoginctlCommand("enable-linger", username); err != nil {
- return false, strings.TrimSpace(out)
+ out, err := runLoginctlCommand("enable-linger", username)
+ if err != nil {
+ // loginctl does not always print anything on failure, and an empty detail here is
+ // indistinguishable from "linger does not apply" -- which is what the caller uses an empty
+ // detail to mean. The error is folded in so the outcome is always attributable.
+ if detail := strings.TrimSpace(out); detail != "" {
+ return false, detail
+ }
+ return false, "loginctl enable-linger " + username + " failed: " + err.Error()
}
- return true, ""
+ // The success path must say so. It previously returned an empty detail, which the install path
+ // reads as "not applicable" -- so the one outcome worth recording, linger actually being
+ // enabled, was the one the manifest silently dropped.
+ return true, "linger enabled for " + username
}
// LingerEnabled reports whether linger is on for a user, so doctor can flag the gap.