Skip to content

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

Reload package forwarders after a self-update rollback

shukieshaha1e507c9c293
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply.go b/cli/beacon/internal/endpoint/selfupdate/apply.go
index bdd5d5fa..5a7e0e45 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply.go
@@ -29,6 +29,12 @@ const collectorHealthTimeout = 60 * time.Second
// injectable so tests can avoid real installer/pkgutil/launchctl calls.
type runnerFunc func(ctx context.Context, name string, args ...string) (string, error)
+// restoreScriptForwarders loads the S3, GCS and Falcon forwarders again after a rollback. The
+// package preinstall stopped them and only the postinstall restarts them, so a rolled-back update
+// would otherwise leave customer forwarding off until the next reboot. A variable so tests never
+// reach the machine's launchd.
+var restoreScriptForwarders = service.RestoreScriptForwarders
+
func execRun(ctx context.Context, name string, args ...string) (string, error) {
out, err := exec.CommandContext(ctx, name, args...).CombinedOutput()
return string(out), err
@@ -505,13 +511,14 @@ func (a *Applier) snapshotInstall() (string, error) {
return dst, nil
}
-// rollback restores the pre-update install tree and restarts the collector. It
-// sets result.RolledBack only when a snapshot existed and was restored
-// successfully; with no snapshot (a first update) the failed new version stays
-// in place and RolledBack remains false, so telemetry never over-claims a
-// rollback that did not happen. Endpoint config/plists live outside the tree but
-// reference stable paths and a release-stable schema, so the restored older
-// binaries run against them consistently after the collector restarts.
+// rollback restores the pre-update install tree, restarts the collector and
+// reloads the package-helper forwarders. It sets result.RolledBack only when a
+// snapshot existed and was restored successfully; with no snapshot (a first
+// update) the failed new version stays in place and RolledBack remains false,
+// so telemetry never over-claims a rollback that did not happen. Endpoint
+// config/plists live outside the tree but reference stable paths and a
+// release-stable schema, so the restored older binaries run against them
+// consistently after the collector restarts.
func (a *Applier) rollback(backup string, result *ApplyResult) error {
if backup == "" {
return nil
@@ -541,6 +548,9 @@ func (a *Applier) rollback(backup string, result *ApplyResult) error {
if err := a.restartCollector(); err != nil {
return err
}
+ if err := restoreScriptForwarders(); err != nil {
+ return fmt.Errorf("restore forwarders: %w", err)
+ }
}
return nil
}
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply_test.go b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
index 8fb6e860..c30cb658 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply_test.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
@@ -759,3 +759,51 @@ func TestApplyRecordsVerificationInTelemetry(t *testing.T) {
res.Verification)
}
}
+
+// The package preinstall stops the S3, GCS and Falcon forwarders and only the postinstall starts
+// them again. An update whose install fails never reaches that postinstall, so the rollback has to
+// bring them back itself, or forwarding stays off until the Mac reboots.
+func TestApplyRollbackRestoresScriptForwarders(t *testing.T) {
+ artifact, sha := makeBeaconTarball(t, "9.9.9")
+ srv := nativeManifestServer(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 "installer", "dpkg", "rpm":
+ return "postinstall failed", fmt.Errorf("exit status 1")
+ default:
+ return "", nil
+ }
+ }
+ a.restart = func() error { return nil }
+ restored := 0
+ old := restoreScriptForwarders
+ restoreScriptForwarders = func() error { restored++; return nil }
+ t.Cleanup(func() { restoreScriptForwarders = old })
+
+ res, err := a.Apply(context.Background())
+ if err == nil {
+ t.Fatal("a failed install must fail the update")
+ }
+ if !res.RolledBack {
+ t.Fatalf("the failed install should have been rolled back: %+v", res)
+ }
+ if restored != 1 {
+ t.Fatalf("rollback reloaded the forwarders %d times, want 1", restored)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/selfupdate/main_test.go b/cli/beacon/internal/endpoint/selfupdate/main_test.go
new file mode 100644
index 00000000..85485db6
--- /dev/null
+++ b/cli/beacon/internal/endpoint/selfupdate/main_test.go
@@ -0,0 +1,14 @@
+package selfupdate
+
+import (
+ "os"
+ "testing"
+)
+
+// TestMain keeps every test in this package away from the machine's launchd. A rollback that
+// restarts the collector also reloads the package-helper forwarders, and on a developer Mac with
+// an S3 forwarder installed the real call would bootstrap it.
+func TestMain(m *testing.M) {
+ restoreScriptForwarders = func() error { return nil }
+ os.Exit(m.Run())
+}