Skip to content

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

feat(endpoint): enable auto updater daemon lifecycle

shukieshah39e868220ca7
diff --git a/cli/beacon/cmd/endpoint_test.go b/cli/beacon/cmd/endpoint_test.go
index ef8920ce..5ccc0289 100644
--- a/cli/beacon/cmd/endpoint_test.go
+++ b/cli/beacon/cmd/endpoint_test.go
@@ -1198,6 +1198,11 @@ func TestAutoUpdateModeIgnoresDestinationValidation(t *testing.T) {
if err := setConfigAutoUpdateModeAt(path, "off"); err != nil {
t.Fatalf("setConfigAutoUpdateModeAt: %v", err)
}
+ if info, err := os.Stat(path); err != nil {
+ t.Fatal(err)
+ } else if got := info.Mode().Perm(); got != 0o600 {
+ t.Fatalf("config permissions = %v, want 0600", got)
+ }
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
diff --git a/cli/beacon/cmd/endpoint_update_service.go b/cli/beacon/cmd/endpoint_update_service.go
index 16f41811..5a935b3b 100644
--- a/cli/beacon/cmd/endpoint_update_service.go
+++ b/cli/beacon/cmd/endpoint_update_service.go
@@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
+ "math/rand"
"os"
"path/filepath"
+ "strconv"
"time"
"github.com/spf13/cobra"
@@ -22,9 +24,14 @@ var endpointUpdateServiceOpts struct {
scheduled bool
}
+// jitterEnv overrides the maximum scheduled-run jitter (seconds). Default 1800;
+// 0 disables jitter for tests/manual diagnostics.
+const jitterEnv = "BEACON_UPDATE_JITTER_SECONDS"
+const defaultJitterSeconds = 1800
+
var endpointUpdateEnableCmd = &cobra.Command{
Use: "enable",
- Short: "Enable check-only update monitoring",
+ Short: "Enable endpoint update monitoring",
SilenceUsage: true,
RunE: runUpdateEnable,
}
@@ -43,13 +50,25 @@ var endpointUpdateStatusCmd = &cobra.Command{
RunE: runUpdateStatus,
}
+// endpointUpdateInstallDaemonCmd is invoked by package postinstall. It
+// reconciles the launchd job with the resolved mode without changing stored
+// config, so explicit off/check-only/auto choices survive package upgrades.
+var endpointUpdateInstallDaemonCmd = &cobra.Command{
+ Use: "install-daemon",
+ Short: "Internal: reconcile the updater launchd job with configured mode",
+ Hidden: true,
+ SilenceUsage: true,
+ RunE: runUpdateInstallDaemon,
+}
+
func init() {
endpointUpdateCmd.AddCommand(endpointUpdateEnableCmd)
endpointUpdateCmd.AddCommand(endpointUpdateDisableCmd)
endpointUpdateCmd.AddCommand(endpointUpdateStatusCmd)
+ endpointUpdateCmd.AddCommand(endpointUpdateInstallDaemonCmd)
endpointUpdateEnableCmd.Flags().BoolVar(&endpointUpdateServiceOpts.checkOnly, "check-only", false, "Enable periodic checks without applying updates")
- // The launchd job runs `update --scheduled`; Phase 1 is check-only. Hidden
- // from the user-facing help.
+ // The launchd job runs `update --scheduled`; it resolves the mode and either
+ // checks only or applies. Hidden from user-facing help.
endpointUpdateCmd.Flags().BoolVar(&endpointUpdateServiceOpts.scheduled, "scheduled", false, "Internal: run as the scheduled updater job")
_ = endpointUpdateCmd.Flags().MarkHidden("scheduled")
}
@@ -81,6 +100,10 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
+ perm := os.FileMode(0644)
+ if info, err := os.Stat(path); err == nil {
+ perm = info.Mode().Perm()
+ }
autoUpdate, err := json.Marshal(endpointconfig.AutoUpdate{Mode: mode})
if err != nil {
return err
@@ -90,7 +113,7 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err != nil {
return err
}
- return os.WriteFile(path, out, 0644)
+ return os.WriteFile(path, out, perm)
}
func requireRootForUpdater() error {
@@ -107,10 +130,10 @@ func runUpdateEnable(cmd *cobra.Command, args []string) error {
if err := requireSystemPackageForUpdater(detectUpdateInstall()); err != nil {
return err
}
- if !endpointUpdateServiceOpts.checkOnly {
- return fmt.Errorf("phase 1 supports check-only monitoring only; rerun with --check-only")
+ mode := selfupdate.ModeAuto
+ if endpointUpdateServiceOpts.checkOnly {
+ mode = selfupdate.ModeCheckOnly
}
- mode := selfupdate.ModeCheckOnly
if err := setConfigAutoUpdateMode(string(mode)); err != nil {
return fmt.Errorf("write auto-update config: %w", err)
}
@@ -121,7 +144,7 @@ func runUpdateEnable(cmd *cobra.Command, args []string) error {
if err := mgr.Load(); err != nil {
return fmt.Errorf("load updater job: %w", err)
}
- fmt.Printf("Update checks enabled (mode: %s).\n", mode)
+ fmt.Printf("Endpoint updater enabled (mode: %s).\n", mode)
return nil
}
@@ -161,8 +184,34 @@ func runUpdateStatus(cmd *cobra.Command, args []string) error {
return nil
}
-// runScheduledUpdate is the entrypoint the launchd job calls. Phase 1 only
-// performs check-only monitoring and writes a local system-log event.
+func runUpdateInstallDaemon(cmd *cobra.Command, args []string) error {
+ if err := requireRootForUpdater(); err != nil {
+ return err
+ }
+ if err := requireSystemPackageForUpdater(detectUpdateInstall()); err != nil {
+ return err
+ }
+ mode := selfupdate.ResolveMode(configAutoUpdateMode())
+ mgr := service.UpdaterManager{}
+ if mode == selfupdate.ModeOff {
+ _ = mgr.Unload()
+ _ = os.Remove(mgr.PlistPath())
+ fmt.Println("Auto-update is off; updater daemon not installed.")
+ return nil
+ }
+ if _, err := mgr.WritePlist(selfupdate.SystemBeaconPath()); err != nil {
+ return fmt.Errorf("write updater plist: %w", err)
+ }
+ if err := mgr.Load(); err != nil {
+ return fmt.Errorf("load updater job: %w", err)
+ }
+ fmt.Printf("Background updater installed (mode: %s).\n", mode)
+ return nil
+}
+
+// runScheduledUpdate is the entrypoint the launchd job calls. It resolves the
+// effective mode and either checks only or applies, after a randomized delay to
+// spread fleet-wide release-manifest/package fetches.
func runScheduledUpdate(parent context.Context) error {
mode := selfupdate.ResolveMode(configAutoUpdateMode())
if mode == selfupdate.ModeOff {
@@ -178,23 +227,15 @@ func runScheduledUpdate(parent context.Context) error {
fmt.Println("Update checks are off; nothing to do.")
return nil
}
- if mode != selfupdate.ModeCheckOnly {
- current := version.GetVersion()
- res := selfupdate.CheckResult{Mode: mode}
- if err := selfupdate.EmitCheckEvent(selfupdate.CheckEventOptions{
- Result: res,
- Action: selfupdate.EventUnsupported,
- Reason: "mode_not_supported_in_phase1",
- LogPath: endpointSystemLogPath(),
- AgentVersion: current,
- }); err != nil {
- return fmt.Errorf("write update check event: %w", err)
- }
- fmt.Printf("Phase 1 scheduled updater supports check-only mode only (resolved: %s).\n", mode)
- return nil
- }
+ sleepJitter()
current := version.GetVersion()
+ if mode == selfupdate.ModeAuto {
+ return applyUpdate(parent, current)
+ }
+ if mode != selfupdate.ModeCheckOnly {
+ return fmt.Errorf("unsupported update mode %q", mode)
+ }
ctx, cancel := context.WithTimeout(parent, 30*time.Second)
defer cancel()
res, err := selfupdate.CheckWithMode(ctx, current, configAutoUpdateMode())
@@ -224,6 +265,19 @@ func runScheduledUpdate(parent context.Context) error {
return nil
}
+func sleepJitter() {
+ max := defaultJitterSeconds
+ if v := os.Getenv(jitterEnv); v != "" {
+ if n, err := strconv.Atoi(v); err == nil {
+ max = n
+ }
+ }
+ if max <= 0 {
+ return
+ }
+ time.Sleep(time.Duration(rand.Intn(max)) * time.Second)
+}
+
func endpointSystemLogPath() string {
if endpointUpdateServiceOpts.scheduled {
return selfupdate.SystemLogPath(writer.DefaultPath(false), false)
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index d67111eb..43a0e284 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -246,6 +246,11 @@ func Uninstall(opts UninstallOptions) error {
cfg := loadOrDefault(opts.UserMode, opts.LogPath)
manager := service.Manager{UserMode: cfg.UserMode}
_ = manager.Unload()
+ if !cfg.UserMode {
+ updater := service.UpdaterManager{}
+ _ = updater.Unload()
+ _ = os.Remove(updater.PlistPath())
+ }
manifest, _ := ReadManifest(cfg.UserMode)
if !opts.KeepConfig {
restoreBackups(manifest.Backups)
diff --git a/cli/beacon/internal/endpoint/service/updater.go b/cli/beacon/internal/endpoint/service/updater.go
index e8815e19..17e155d3 100644
--- a/cli/beacon/internal/endpoint/service/updater.go
+++ b/cli/beacon/internal/endpoint/service/updater.go
@@ -8,11 +8,10 @@ import (
"strings"
)
-// UpdaterLabel is the launchd label for the periodic update-check job. Phase 1
-// is check-only and does not apply package updates.
+// UpdaterLabel is the launchd label for the periodic endpoint updater job.
const UpdaterLabel = "com.beacon.endpoint.updater"
-// UpdaterManager manages the update-check launchd job. Unlike the collector
+// UpdaterManager manages the endpoint updater launchd job. Unlike the collector
// service it is system-only and runs on a schedule rather than KeepAlive.
type UpdaterManager struct{}
@@ -78,9 +77,8 @@ func (m UpdaterManager) Status() Status {
return status
}
-// updaterPlist renders the check-only LaunchDaemon. Phase 1 runs every ten
-// minutes so update detection can be dogfooded quickly without install risk.
-// It does not RunAtLoad and is not KeepAlive (one-shot scheduled job).
+// updaterPlist renders the updater LaunchDaemon. It runs daily and does not
+// RunAtLoad or KeepAlive; each invocation resolves the configured mode.
func updaterPlist(label, program string) string {
return fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@@ -93,11 +91,15 @@ func updaterPlist(label, program string) string {
<string>%s</string>
<string>endpoint</string>
<string>update</string>
- <string>--check</string>
<string>--scheduled</string>
</array>
- <key>StartInterval</key>
- <integer>600</integer>
+ <key>StartCalendarInterval</key>
+ <dict>
+ <key>Hour</key>
+ <integer>3</integer>
+ <key>Minute</key>
+ <integer>0</integer>
+ </dict>
<key>RunAtLoad</key>
<false/>
<key>StandardOutPath</key>
diff --git a/cli/beacon/internal/endpoint/service/updater_test.go b/cli/beacon/internal/endpoint/service/updater_test.go
index f3da00b2..fa22708e 100644
--- a/cli/beacon/internal/endpoint/service/updater_test.go
+++ b/cli/beacon/internal/endpoint/service/updater_test.go
@@ -11,10 +11,12 @@ func TestUpdaterPlistContent(t *testing.T) {
for _, want := range []string{
"<string>com.beacon.endpoint.updater</string>",
"<string>/opt/beacon/bin/beacon</string>",
- "<string>--check</string>",
"<string>--scheduled</string>",
- "<key>StartInterval</key>",
- "<integer>600</integer>",
+ "<key>StartCalendarInterval</key>",
+ "<key>Hour</key>",
+ "<integer>3</integer>",
+ "<key>Minute</key>",
+ "<integer>0</integer>",
} {
if !strings.Contains(out, want) {
t.Errorf("plist missing %q", want)
@@ -24,6 +26,9 @@ func TestUpdaterPlistContent(t *testing.T) {
if strings.Contains(out, "<key>KeepAlive</key>") {
t.Errorf("updater plist should not set KeepAlive")
}
+ if strings.Contains(out, "<string>--check</string>") {
+ t.Errorf("updater plist should let scheduled mode resolve check/apply behavior")
+ }
if !strings.Contains(out, "<key>RunAtLoad</key>\n <false/>") {
t.Errorf("updater plist should set RunAtLoad false")
}
diff --git a/packaging/macos/scripts/postinstall b/packaging/macos/scripts/postinstall
index fb9ae5ad..ed875b0d 100755
--- a/packaging/macos/scripts/postinstall
+++ b/packaging/macos/scripts/postinstall
@@ -40,6 +40,7 @@ if command -v launchctl >/dev/null 2>&1; then
}
restart_optional_forwarder "com.beacon.endpoint.falcon-forwarder" "/Library/LaunchDaemons/com.beacon.endpoint.falcon-forwarder.plist"
restart_optional_forwarder "com.beacon.endpoint.s3-forwarder" "/Library/LaunchDaemons/com.beacon.endpoint.s3-forwarder.plist"
+ "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint update install-daemon >/dev/null 2>&1 || true
fi
exit 0
diff --git a/packaging/macos/scripts/preinstall b/packaging/macos/scripts/preinstall
index 8a0e0994..c6af93dd 100755
--- a/packaging/macos/scripts/preinstall
+++ b/packaging/macos/scripts/preinstall
@@ -6,6 +6,7 @@ set -eu
if command -v launchctl >/dev/null 2>&1; then
launchctl bootout system/com.beacon.endpoint.collector >/dev/null 2>&1 || true
launchctl bootout system/com.beacon.endpoint.falcon-forwarder >/dev/null 2>&1 || true
+ launchctl bootout system/com.beacon.endpoint.updater >/dev/null 2>&1 || true
fi
exit 0