Skip to content

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

fix(endpoint): harden manual update apply rollback paths

shukieshah72af1bbac606
diff --git a/cli/beacon/cmd/endpoint_test.go b/cli/beacon/cmd/endpoint_test.go
index 02eba5bb..e79e8cb7 100644
--- a/cli/beacon/cmd/endpoint_test.go
+++ b/cli/beacon/cmd/endpoint_test.go
@@ -1274,6 +1274,20 @@ func TestApplyUpdateRequiresRoot(t *testing.T) {
}
}
+func TestApplyUpdateInsecureRequiresInstallPrefix(t *testing.T) {
+ oldAllow := endpointUpdateOpts.allowInsecure
+ oldPrefix := endpointUpdateOpts.installPrefix
+ t.Cleanup(func() {
+ endpointUpdateOpts.allowInsecure = oldAllow
+ endpointUpdateOpts.installPrefix = oldPrefix
+ })
+ endpointUpdateOpts.allowInsecure = true
+ endpointUpdateOpts.installPrefix = ""
+ if err := applyUpdate(context.Background(), "0.0.1"); err == nil || !strings.Contains(err.Error(), "requires --install-prefix") {
+ t.Fatalf("applyUpdate error = %v, want install-prefix requirement", err)
+ }
+}
+
func TestCompletionAndDocsCommandsRegistered(t *testing.T) {
for _, name := range []string{"completion", "docs"} {
cmd, _, err := rootCmd.Find([]string{name})
diff --git a/cli/beacon/cmd/endpoint_update.go b/cli/beacon/cmd/endpoint_update.go
index 0eab099f..8ce8321b 100644
--- a/cli/beacon/cmd/endpoint_update.go
+++ b/cli/beacon/cmd/endpoint_update.go
@@ -159,6 +159,20 @@ func runEndpointUpdate(cmd *cobra.Command, args []string) error {
return nil
}
+ if !endpointUpdateOpts.allowInsecure {
+ switch res.Install.Kind {
+ case selfupdate.InstallHomebrew:
+ fmt.Println("Installed via Homebrew. Update with:")
+ fmt.Println(" brew upgrade beacon")
+ return nil
+ case selfupdate.InstallOther:
+ return fmt.Errorf("manual apply is supported only for the system package install (detected: %s)", res.Install.Kind)
+ }
+ if !res.HasArtifact {
+ return fmt.Errorf("no signed endpoint package artifact is available for %s", res.ArchKey)
+ }
+ }
+
return applyUpdate(cmd.Context(), current)
}
@@ -183,6 +197,8 @@ func applyUpdate(parent context.Context, current string) error {
if install := detectUpdateInstall(); !install.SupportsSeamlessUpdate() {
return fmt.Errorf("automatic apply is only supported for the system package install (detected: %s)", install.Kind)
}
+ } else if endpointUpdateOpts.installPrefix == "" {
+ return fmt.Errorf("--allow-insecure-test requires --install-prefix")
}
ctx, cancel := context.WithTimeout(parent, 15*time.Minute)
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply.go b/cli/beacon/internal/endpoint/selfupdate/apply.go
index 0df5f852..88f7a9cb 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply.go
@@ -93,6 +93,9 @@ func (a *Applier) Apply(ctx context.Context) (ApplyResult, error) {
if a.SkipGatekeeper && !a.AllowInsecureTest {
return ApplyResult{}, fmt.Errorf("SkipGatekeeper requires AllowInsecureTest")
}
+ if a.AllowInsecureTest && filepath.Clean(a.prefix()) == string(filepath.Separator) {
+ return ApplyResult{}, fmt.Errorf("AllowInsecureTest requires a non-root install prefix")
+ }
result := ApplyResult{FromVersion: a.CurrentVersion}
current := strings.TrimSpace(a.CurrentVersion)
if current == "dev" {
@@ -176,14 +179,22 @@ func (a *Applier) Apply(ctx context.Context) (ApplyResult, error) {
}
if err := a.install(ctx, pkgPath); err != nil {
- a.rollback(backup, &result)
- a.emit(false, result, err.Error())
+ rollbackErr := a.rollback(backup, &result)
+ message := err.Error()
+ if rollbackErr != nil {
+ message += "; rollback failed: " + rollbackErr.Error()
+ }
+ a.emit(false, result, message)
return result, fmt.Errorf("install update: %w", err)
}
if err := a.healthCheck(ctx, manifest.Version); err != nil {
- a.rollback(backup, &result)
- a.emit(false, result, "post-install health check failed: "+err.Error())
+ rollbackErr := a.rollback(backup, &result)
+ message := "post-install health check failed: " + err.Error()
+ if rollbackErr != nil {
+ message += "; rollback failed: " + rollbackErr.Error()
+ }
+ a.emit(false, result, message)
return result, fmt.Errorf("post-install health check failed: %w", err)
}
@@ -342,20 +353,35 @@ func (a *Applier) snapshotInstall() (string, error) {
// 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) {
+func (a *Applier) rollback(backup string, result *ApplyResult) error {
if backup == "" {
- return
+ return nil
}
- if err := os.RemoveAll(a.installDir()); err != nil {
- return
+ live := a.installDir()
+ restore := filepath.Join(a.stageDir(), "rollback", "restore")
+ failed := filepath.Join(a.stageDir(), "rollback", "failed-install")
+ if err := os.RemoveAll(restore); err != nil {
+ return err
}
- if err := copyTree(backup, a.installDir()); err != nil {
- return
+ if err := copyTree(backup, restore); err != nil {
+ return err
+ }
+ _ = os.RemoveAll(failed)
+ if err := os.Rename(live, failed); err != nil && !os.IsNotExist(err) {
+ return err
+ }
+ if err := os.Rename(restore, live); err != nil {
+ if _, statErr := os.Stat(failed); statErr == nil {
+ _ = os.Rename(failed, live)
+ }
+ return err
}
result.RolledBack = true
+ _ = os.RemoveAll(failed)
if !a.AllowInsecureTest {
_ = a.restartCollector()
}
+ return nil
}
// healthCheck confirms the freshly installed beacon reports the expected
diff --git a/cli/beacon/internal/endpoint/selfupdate/apply_test.go b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
index 96168005..ccdc1a71 100644
--- a/cli/beacon/internal/endpoint/selfupdate/apply_test.go
+++ b/cli/beacon/internal/endpoint/selfupdate/apply_test.go
@@ -225,6 +225,38 @@ func TestSkipGatekeeperRequiresInsecureFlag(t *testing.T) {
}
}
+func TestInsecureApplyRequiresNonRootPrefix(t *testing.T) {
+ a := NewApplier("0.0.1")
+ a.AllowInsecureTest = true
+ if _, err := a.Apply(context.Background()); err == nil || !strings.Contains(err.Error(), "non-root install prefix") {
+ t.Fatalf("expected non-root prefix guard, got %v", err)
+ }
+}
+
+func TestRollbackDoesNotRemoveLiveInstallWhenRestorePreparationFails(t *testing.T) {
+ a := NewApplier("0.0.1")
+ a.AllowInsecureTest = true
+ a.InstallPrefix = t.TempDir()
+ a.StageDir = t.TempDir()
+ liveBin := filepath.Join(a.InstallPrefix, "opt/beacon/bin/beacon")
+ if err := os.MkdirAll(filepath.Dir(liveBin), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(liveBin, []byte("live"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ var result ApplyResult
+ if err := a.rollback(filepath.Join(t.TempDir(), "missing-backup"), &result); err == nil {
+ t.Fatal("expected rollback error for missing backup")
+ }
+ if got := string(mustRead(t, liveBin)); got != "live" {
+ t.Fatalf("live install changed after failed rollback prep: %q", got)
+ }
+ if result.RolledBack {
+ t.Fatal("RolledBack should remain false when restore was not completed")
+ }
+}
+
func TestVersionLineMatches(t *testing.T) {
out := "beacon version 0.0.69 (abc1234) built on 2026-01-01"
if !versionLineMatches(out, "0.0.69") {