Skip to content

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

fix(endpoint): keep scheduled jobs the host cannot run out of the install plan (#1233)

Saurav Singha577c89b2148
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index 87f68bc3..ed47400d 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -922,10 +922,7 @@ func plannedInstallActions(repair bool, kind service.Kind, otlpTargets, hookTarg
if unitPath, err := mgr.UnitPath(); err == nil {
serviceAction.Target = unitPath
}
- inventoryAction := plannedAction{Action: "write_unit", Message: "scheduled inventory heartbeat job"}
- if unitPath, err := (service.InventoryManager{UserMode: cfg.UserMode, Kind: kind}).UnitPath(); err == nil {
- inventoryAction.Target = unitPath
- }
+ inventoryAction, inventoryScheduled := plannedScheduledJob(service.InventoryManager{UserMode: cfg.UserMode, Kind: kind}, "scheduled inventory heartbeat job")
actions = append(actions,
plannedAction{Action: "write_file", Target: cfg.Collector.ConfigPath, Message: "collector configuration"},
serviceAction,
@@ -941,12 +938,11 @@ func plannedInstallActions(repair bool, kind service.Kind, otlpTargets, hookTarg
if on, _ := sessionSyncOption(); on != nil {
sessionSync, sessionSyncChosen = *on, true
}
+ sessionSyncScheduled := false
switch {
case sessionSync:
- sessionSyncAction := plannedAction{Action: "write_unit", Message: "scheduled session sync job"}
- if unitPath, err := (service.SessionSyncManager{UserMode: cfg.UserMode, Kind: kind}).UnitPath(); err == nil {
- sessionSyncAction.Target = unitPath
- }
+ var sessionSyncAction plannedAction
+ sessionSyncAction, sessionSyncScheduled = plannedScheduledJob(service.SessionSyncManager{UserMode: cfg.UserMode, Kind: kind}, "scheduled session sync job")
actions = append(actions, sessionSyncAction)
case sessionSyncChosen:
actions = append(actions, plannedAction{Action: "unload_service", Message: "remove scheduled session sync job if present"})
@@ -958,17 +954,33 @@ func plannedInstallActions(repair bool, kind service.Kind, otlpTargets, hookTarg
actions = append(actions, plannedAction{Action: "configure_harness", Target: h, Message: "install endpoint hook integration"})
}
if !endpointOpts.noStart {
- actions = append(actions,
- plannedAction{Action: "load_service", Message: "start endpoint collector service"},
- plannedAction{Action: "load_service", Message: "start scheduled inventory heartbeat job"},
- )
- if sessionSync {
+ actions = append(actions, plannedAction{Action: "load_service", Message: "start endpoint collector service"})
+ if inventoryScheduled {
+ actions = append(actions, plannedAction{Action: "load_service", Message: "start scheduled inventory heartbeat job"})
+ }
+ if sessionSyncScheduled {
actions = append(actions, plannedAction{Action: "load_service", Message: "start scheduled session sync job"})
}
}
return actions
}
+// plannedScheduledJob is the plan's line for one scheduled job, and whether the install will start
+// it. An install skips a job on a host with no scheduler for it (Windows, or a supervised collector
+// in a container or under WSL) and says why, so the plan says the same. It used to name a launchd
+// plist under ~/Library/LaunchAgents there, and a step that started the job, neither of which the
+// install does.
+func plannedScheduledJob(job service.ScheduledJob, name string) (plannedAction, bool) {
+ if !job.Supported() {
+ return plannedAction{Action: "skip_unit", Message: name + " not installed: " + job.UnsupportedReason()}, false
+ }
+ action := plannedAction{Action: "write_unit", Message: name}
+ if unitPath, err := job.UnitPath(); err == nil {
+ action.Target = unitPath
+ }
+ return action, true
+}
+
func plannedUninstallActions() []plannedAction {
cfg := loadOrDefaultConfig()
actions := []plannedAction{
diff --git a/cli/beacon/cmd/endpoint_test.go b/cli/beacon/cmd/endpoint_test.go
index 08ab1211..5bcd0424 100644
--- a/cli/beacon/cmd/endpoint_test.go
+++ b/cli/beacon/cmd/endpoint_test.go
@@ -182,6 +182,11 @@ func TestCanonicalHookTargetsNormalizesAliasesToSwitchNames(t *testing.T) {
}
}
+// hostSchedulesJobs reports whether this host can schedule Beacon's jobs (launchd on macOS,
+// systemd as PID 1 on Linux; not Windows or a container), so a plan test expects what the install
+// would do here. TestPlannedInstallActionsSkipJobsForASupervisedCollector covers the skip on any host.
+func hostSchedulesJobs() bool { return service.InventoryManager{UserMode: true}.Supported() }
+
func TestPlannedInstallActionsSeparatesHookHarnesses(t *testing.T) {
old := endpointOpts
t.Cleanup(func() { endpointOpts = old })
@@ -213,17 +218,22 @@ func TestPlannedInstallActionsSeparatesHookHarnesses(t *testing.T) {
if counts["devin"] != 0 {
t.Fatalf("legacy devin alias should be deduped, counts=%#v", counts)
}
- inventoryUnits := 0
+ // A host with no scheduler skips the job, and the plan says so once instead.
+ inventoryLine := "write_unit"
+ if !hostSchedulesJobs() {
+ inventoryLine = "skip_unit"
+ }
+ inventoryLines := 0
for _, action := range actions {
- if action.Action == "write_unit" && action.Message == "scheduled inventory heartbeat job" {
- inventoryUnits++
+ if action.Action == inventoryLine && strings.HasPrefix(action.Message, "scheduled inventory heartbeat job") {
+ inventoryLines++
}
if action.Action == "load_service" {
t.Fatalf("--no-start plan must not load anything: %+v", action)
}
}
- if inventoryUnits != 1 {
- t.Fatalf("plan should write the scheduled inventory job once, got %d in %+v", inventoryUnits, actions)
+ if inventoryLines != 1 {
+ t.Fatalf("plan should name the scheduled inventory job once as %s, got %d in %+v", inventoryLine, inventoryLines, actions)
}
}
@@ -236,7 +246,7 @@ func TestPlannedInstallActionsFollowSessionSync(t *testing.T) {
endpointOpts.logPath = filepath.Join(t.TempDir(), "runtime.jsonl")
endpointOpts.noStart = false
- plan := func() (units, loads, removals int) {
+ plan := func() (units, loads, removals, skips int) {
for _, action := range plannedInstallActions(false, service.KindAuto, nil, nil) {
switch {
case action.Action == "write_unit" && action.Message == "scheduled session sync job":
@@ -245,21 +255,67 @@ func TestPlannedInstallActionsFollowSessionSync(t *testing.T) {
loads++
case action.Action == "unload_service" && action.Message == "remove scheduled session sync job if present":
removals++
+ case action.Action == "skip_unit" && strings.HasPrefix(action.Message, "scheduled session sync job"):
+ skips++
}
}
- return units, loads, removals
+ return units, loads, removals, skips
}
endpointOpts.sessionSync, endpointOpts.noSessionSync = false, false
- if u, l, r := plan(); u+l+r != 0 {
- t.Fatalf("default plan names the session sync job: units=%d loads=%d removals=%d", u, l, r)
+ if u, l, r, s := plan(); u+l+r+s != 0 {
+ t.Fatalf("default plan names the session sync job: units=%d loads=%d removals=%d skips=%d", u, l, r, s)
}
+ // --session-sync writes and starts the job, or on a host with no scheduler says it is skipped.
endpointOpts.sessionSync = true
- if u, l, r := plan(); u != 1 || l != 1 || r != 0 {
- t.Fatalf("--session-sync plan: units=%d loads=%d removals=%d", u, l, r)
+ wantScheduled, wantSkips := 1, 0
+ if !hostSchedulesJobs() {
+ wantScheduled, wantSkips = 0, 1
+ }
+ if u, l, r, s := plan(); u != wantScheduled || l != wantScheduled || r != 0 || s != wantSkips {
+ t.Fatalf("--session-sync plan: units=%d loads=%d removals=%d skips=%d", u, l, r, s)
}
endpointOpts.sessionSync, endpointOpts.noSessionSync = false, true
- if u, l, r := plan(); u != 0 || l != 0 || r != 1 {
- t.Fatalf("--no-session-sync plan: units=%d loads=%d removals=%d", u, l, r)
+ if u, l, r, s := plan(); u != 0 || l != 0 || r != 1 || s != 0 {
+ t.Fatalf("--no-session-sync plan: units=%d loads=%d removals=%d skips=%d", u, l, r, s)
+ }
+}
+
+// A supervised collector has no scheduler on any platform, so the install skips the inventory and
+// session sync jobs and says why. The plan has to say the same: it used to name a launchd unit and
+// a start step for each.
+func TestPlannedInstallActionsSkipJobsForASupervisedCollector(t *testing.T) {
+ testenv.SetHome(t, t.TempDir())
+ old := endpointOpts
+ t.Cleanup(func() { endpointOpts = old })
+ endpointOpts.userMode, endpointOpts.systemMode = true, false
+ endpointOpts.logPath = filepath.Join(t.TempDir(), "runtime.jsonl")
+ endpointOpts.noStart = false
+ endpointOpts.sessionSync, endpointOpts.noSessionSync = true, false
+
+ skipped, collectorStarts := 0, 0
+ for _, action := range plannedInstallActions(false, service.KindSupervised, nil, nil) {
+ if action.Action == "load_service" && action.Message == "start endpoint collector service" {
+ collectorStarts++
+ }
+ if strings.Contains(action.Target, "LaunchAgents") || strings.Contains(action.Target, "LaunchDaemons") {
+ t.Errorf("plan names a launchd unit for a supervised collector: %+v", action)
+ }
+ if action.Action == "load_service" && strings.Contains(action.Message, "scheduled") {
+ t.Errorf("plan starts a scheduled job a supervised collector cannot run: %+v", action)
+ }
+ if action.Action == "skip_unit" {
+ skipped++
+ if !strings.Contains(action.Message, "needs launchd or systemd") {
+ t.Errorf("skipped job does not say why: %+v", action)
+ }
+ }
+ }
+ if skipped != 2 {
+ t.Errorf("plan should skip the inventory and session sync jobs, skipped %d", skipped)
+ }
+ // The collector is not a scheduled job: the plan still starts it.
+ if collectorStarts != 1 {
+ t.Errorf("plan should start the collector once, got %d", collectorStarts)
}
}
diff --git a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
index 81d98010..d3c9a1fa 100644
--- a/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
+++ b/cli/beacon/internal/endpoint/lifecycle/inventory_job_test.go
@@ -50,7 +50,7 @@ func installFakeInventoryJobWithLoadError(t *testing.T, supported bool, loadErr
t.Helper()
calls := &[]string{}
old := newInventoryJob
- newInventoryJob = func(userMode bool, kind service.Kind) inventoryJobController {
+ newInventoryJob = func(userMode bool, kind service.Kind) service.ScheduledJob {
return recordingInventoryJob{calls: calls, supported: supported, unitPath: "/fake/" + InventoryUnitName(userMode), loadErr: loadErr}
}
t.Cleanup(func() { newInventoryJob = old })
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index f97b50c0..fbdc0ac8 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -47,7 +47,7 @@ var (
}
// newInventoryJob and removeInventoryJob are the seams tests replace so Install, Uninstall
// and Repair can be exercised without touching launchd or systemd.
- newInventoryJob = func(userMode bool, kind service.Kind) inventoryJobController {
+ newInventoryJob = func(userMode bool, kind service.Kind) service.ScheduledJob {
return service.InventoryManager{UserMode: userMode, Kind: kind, Env: inventoryJobEnv(userMode)}
}
removeInventoryJob = func(userMode bool) {
@@ -79,19 +79,6 @@ var (
sentinelForwarderStatus = sentinel.ReadStatus
)
-// inventoryJobController is what lifecycle needs from service.InventoryManager.
-type inventoryJobController interface {
- Supported() bool
- UnsupportedReason() string
- UnitPath() (string, error)
- UnitPaths() []string
- WriteUnit(program, logPath string) (string, error)
- Load() error
- Unload() error
- RemoveUnits()
- Status() service.Status
-}
-
// InventoryJobOptions drives ReconcileInventoryJob.
type InventoryJobOptions struct {
UserMode bool
@@ -122,7 +109,7 @@ func ReconcileInventoryJob(opts InventoryJobOptions) (InventoryJobResult, error)
return reconcileInventoryJobWith(newInventoryJob(opts.UserMode, opts.Kind), opts)
}
-func reconcileInventoryJobWith(job inventoryJobController, opts InventoryJobOptions) (InventoryJobResult, error) {
+func reconcileInventoryJobWith(job service.ScheduledJob, opts InventoryJobOptions) (InventoryJobResult, error) {
result := InventoryJobResult{Enabled: inventoryEnabledFromConfigFile(endpointconfig.ConfigPath(opts.UserMode))}
if !job.Supported() {
result.Skipped = job.UnsupportedReason()
@@ -516,14 +503,14 @@ type installRollback struct {
// it started, so the scheduler is not left registered against rolled-back config.
// InventoryWasLoaded records a job that was already scheduled before this install began; a
// reinstall that re-enabled it and then failed must bring it back, exactly like the collector.
- InventoryJob inventoryJobController
+ InventoryJob service.ScheduledJob
InventoryLoaded bool
InventoryWasLoaded bool
// SessionSyncJob and the two flags beside it do the same for the scheduled session sync.
// SessionSyncRemoved records that this install unloaded and removed the job (the setting
// turned off): unlike the inventory job, off is an ordinary choice here, so a rollback has to
// put back a job that was scheduled before as well as unload one it started.
- SessionSyncJob inventoryJobController
+ SessionSyncJob service.ScheduledJob
SessionSyncLoaded bool
SessionSyncRemoved bool
SessionSyncWasLoaded bool
diff --git a/cli/beacon/internal/endpoint/lifecycle/session_sync.go b/cli/beacon/internal/endpoint/lifecycle/session_sync.go
index f2569cf1..116fe7a4 100644
--- a/cli/beacon/internal/endpoint/lifecycle/session_sync.go
+++ b/cli/beacon/internal/endpoint/lifecycle/session_sync.go
@@ -14,7 +14,7 @@ import (
var (
// newSessionSyncJob and removeSessionSyncJob are the seams tests replace so Install,
// Uninstall and Repair can be exercised without touching launchd or systemd.
- newSessionSyncJob = func(userMode bool, kind service.Kind) inventoryJobController {
+ newSessionSyncJob = func(userMode bool, kind service.Kind) service.ScheduledJob {
return service.SessionSyncManager{UserMode: userMode, Kind: kind, Env: sessionSyncJobEnv(userMode)}
}
removeSessionSyncJob = func(userMode bool) {
@@ -83,7 +83,7 @@ func ReconcileSessionSyncJob(opts InventoryJobOptions) (SessionSyncJobResult, er
return reconcileSessionSyncJobWith(newSessionSyncJob(opts.UserMode, opts.Kind), opts)
}
-func reconcileSessionSyncJobWith(job inventoryJobController, opts InventoryJobOptions) (SessionSyncJobResult, error) {
+func reconcileSessionSyncJobWith(job service.ScheduledJob, opts InventoryJobOptions) (SessionSyncJobResult, error) {
result := SessionSyncJobResult{Enabled: sessionSyncEnabledFromConfigFile(endpointconfig.ConfigPath(opts.UserMode))}
if !result.Enabled {
// Removing is safe with or without a scheduler, and it is what turning the setting off
diff --git a/cli/beacon/internal/endpoint/lifecycle/session_sync_test.go b/cli/beacon/internal/endpoint/lifecycle/session_sync_test.go
index 478c673a..915e83c5 100644
--- a/cli/beacon/internal/endpoint/lifecycle/session_sync_test.go
+++ b/cli/beacon/internal/endpoint/lifecycle/session_sync_test.go
@@ -171,7 +171,7 @@ func TestSetSessionSyncChangesOnlyItsOwnKey(t *testing.T) {
writeInventoryConfig(t, true, body)
calls := &[]string{}
old := newSessionSyncJob
- newSessionSyncJob = func(bool, service.Kind) inventoryJobController {
+ newSessionSyncJob = func(bool, service.Kind) service.ScheduledJob {
return recordingInventoryJob{calls: calls, supported: true, unitPath: "/fake/s.plist"}
}
t.Cleanup(func() { newSessionSyncJob = old })
diff --git a/cli/beacon/internal/endpoint/service/scheduled_job.go b/cli/beacon/internal/endpoint/service/scheduled_job.go
index 8f547e49..69dd6775 100644
--- a/cli/beacon/internal/endpoint/service/scheduled_job.go
+++ b/cli/beacon/internal/endpoint/service/scheduled_job.go
@@ -48,6 +48,21 @@ func intervalFromEnv(env string, def time.Duration) time.Duration {
return def
}
+// ScheduledJob is what callers ask of a scheduled job's manager, InventoryManager or
+// SessionSyncManager: install and repair reconcile one through it, and their dry-run plan asks it
+// whether the host can schedule the job and where its unit goes.
+type ScheduledJob interface {
+ Supported() bool
+ UnsupportedReason() string
+ UnitPath() (string, error)
+ UnitPaths() []string
+ WriteUnit(program, logPath string) (string, error)
+ Load() error
+ Unload() error
+ RemoveUnits()
+ Status() Status
+}
+
// scheduledJob is one jobSpec installed in one mode. It is a one-shot job on a timer, like the
// updater, but it exists in both user and system modes, like the forwarder.
//
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index 624567de..24381aee 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -13,6 +13,8 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
Changes on `main` since v1.3.58. They ship in the next release.
+- **`install --dry-run` no longer plans scheduled jobs the host cannot run** · On a host with no scheduler for Beacon's jobs (Windows, or a supervised collector in a container or under WSL), `beacon endpoint install --dry-run` and `repair --dry-run` listed a launchd plist under `~/Library/LaunchAgents` for the inventory heartbeat job, and for the session sync job with `--session-sync`, plus a step that started each. The install does none of that there: it skips both jobs and says why. The plan now shows a `skip_unit` line with that same reason instead. On macOS and on Linux with systemd the plan is unchanged.
+
- **An AWS secret key after a labeled key id is redacted** · Where a label's or a flag's value ended in an AWS access key id and its secret access key followed after a comma, a blank or a quote (`token=AKIA…,<secret>`, `--access-key AKIA… <secret>`, `x-access-token:abc@AKIA…,<secret>`), the value rule replaced the key id and the rule for a key id and its secret then found no key id, so the secret was kept. The secret is now replaced with the value, keeping the separators: `token=[REDACTED],[REDACTED]`. A URL password that runs on over a key id written right after a host name (`…@hostAKIA…,<secret>`) takes the secret too.
- **A URL password after `Bearer` is redacted** · The Bearer rule takes the token after `Bearer` up to a `:`, so for `Bearer postgres://user:pw@host` it replaced the scheme, and the URL rule then found no scheme before `://` and kept the password. A password after a replaced scheme is now redacted too: `Bearer [REDACTED]://user:[REDACTED]@host`.