Skip to content

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

fix(endpoint): restart collector before update health check

shukieshah4bc7a9fa22dc
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply.go b/cli/beacon/internal/endpoint/selfupdate/apply.go
index f9b5a514..13e7b103 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply.go
@@ -59,6 +59,7 @@ type Applier struct {
HTTPClient *http.Client
run runnerFunc
+ restart func() error
now func() time.Time
}
@@ -190,6 +191,18 @@ func (a *Applier) Apply(ctx context.Context) (ApplyResult, error) {
return result, fmt.Errorf("install update: %w", err)
}
+ if !a.AllowInsecureTest {
+ if err := a.restartCollector(); err != nil {
+ rollbackErr := a.rollback(backup, &result)
+ message := "post-install collector restart failed: " + err.Error()
+ if rollbackErr != nil {
+ message += "; rollback failed: " + rollbackErr.Error()
+ }
+ a.emit(false, result, message)
+ return result, fmt.Errorf("post-install collector restart failed: %w", err)
+ }
+ }
+
if err := a.healthCheck(ctx, manifest.Version); err != nil {
rollbackErr := a.rollback(backup, &result)
message := "post-install health check failed: " + err.Error()
@@ -417,6 +430,9 @@ func (a *Applier) healthCheck(ctx context.Context, wantVersion string) error {
}
func (a *Applier) restartCollector() error {
+ if a.restart != nil {
+ return a.restart()
+ }
mgr := service.Manager{UserMode: false}
_ = mgr.Unload()
return mgr.Load()
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply_test.go b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
index 2791a87b..a584d764 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply_test.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
@@ -217,6 +217,50 @@ func TestApplyGatekeeperAbortsBeforeInstall(t *testing.T) {
}
}
+func TestApplyRestartsCollectorBeforeHealthCheck(t *testing.T) {
+ artifact, sha := makeBeaconTarball(t, "9.9.9")
+ srv := manifestServer(t, "9.9.9", sha, artifact)
+ defer srv.Close()
+
+ a := NewApplier("0.0.1")
+ a.ManifestURL = srv.URL + "/manifest.json"
+ a.StageDir = t.TempDir()
+ a.InstallPrefix = t.TempDir()
+ a.LogPath = filepath.Join(t.TempDir(), "system.jsonl")
+ oldBin := filepath.Join(a.InstallPrefix, "opt/beacon/bin/beacon")
+ if err := os.MkdirAll(filepath.Dir(oldBin), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(oldBin, []byte("#!/bin/sh\necho \"beacon version 0.0.1 (old) built on test\"\n"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ a.run = func(ctx context.Context, name string, args ...string) (string, error) {
+ switch filepath.Base(name) {
+ case "pkgutil":
+ return "Developer ID Installer: Example (TEAMID)", nil
+ case "stapler", "spctl", "installer":
+ return "", nil
+ case "beacon":
+ return "beacon version 9.9.9 (new) built on test", nil
+ default:
+ return "", nil
+ }
+ }
+ restartErr := fmt.Errorf("launchctl failed")
+ a.restart = func() error { return restartErr }
+
+ res, err := a.Apply(context.Background())
+ if err == nil || !strings.Contains(err.Error(), "collector restart failed") {
+ t.Fatalf("Apply error = %v, want collector restart failure", err)
+ }
+ if !res.RolledBack {
+ t.Fatalf("filesystem rollback should be recorded after restart failure: %+v", res)
+ }
+ if !strings.Contains(string(mustRead(t, a.LogPath)), "rollback failed") {
+ t.Fatalf("expected rollback restart failure telemetry, got %s", mustRead(t, a.LogPath))
+ }
+}
+
func TestSkipGatekeeperRequiresInsecureFlag(t *testing.T) {
a := NewApplier("0.0.1")
a.SkipGatekeeper = true // without AllowInsecureTest