Skip to content

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

fix(windows): give doctor remediation that works on the platform that failed

shukieshah61230e6b1480
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index 42f0f20c..ae033ed2 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -979,8 +979,24 @@ func actionForCheck(check diagnostics.Check, runtimeLog lifecycle.RuntimeLogSour
case "runtime_log":
return "beacon endpoint doctor --fix"
case "runtime_log_permissions":
- if check.Evidence == "runtime_log_missing" || check.Evidence == "missing_optional_file" {
+ // Branch on evidence rather than falling through to chmod. Windows reaches this check too
+ // now, and chmod neither exists there nor would restore an ACL if it did -- so the single
+ // most consequential remediation string on that platform, the one printed for a system-mode
+ // log that hooks cannot write, was advice that could not work.
+ switch check.Evidence {
+ case "runtime_log_missing", "missing_optional_file":
return "beacon endpoint doctor --fix"
+ case "acl_missing_interactive_write":
+ // Printed rather than only offered through --fix: this one needs an elevated shell, and
+ // an operator who cannot elevate right now still deserves to know the exact command.
+ if hint := writer.GrantCommandHint(check.Target); hint != "" {
+ return hint
+ }
+ return "beacon endpoint doctor --fix"
+ case "acl_not_writable_by_user":
+ return "grant your account write access to " + check.Target
+ case "acl_unreadable":
+ return "inspect the access control list on " + check.Target
}
return "chmod 666 " + check.Target
case "runtime_log_source":
@@ -1130,10 +1146,28 @@ func planDoctorFixes(result doctorResult, status lifecycle.Status) doctorFixPlan
}
switch check.Name {
case "runtime_log", "runtime_log_permissions", "last_event":
- if check.Evidence == "missing_optional_file" || check.Evidence == "runtime_log_missing" {
+ switch check.Evidence {
+ case "missing_optional_file", "runtime_log_missing":
addFix(plannedAction{Action: "create_runtime_log", Target: status.LogPath, Message: "create runtime log file and parent directory"})
- } else if check.Evidence == "last_event_missing" {
+ case "last_event_missing":
addSkip(plannedAction{Action: "manual_fix", Target: check.Target, Message: "run beacon endpoint test-event or generate a runtime event"})
+ // The one permission failure that is repairable in place, and the one that matters
+ // most: without this grant a system-mode Windows endpoint runs a healthy collector
+ // that records nothing an agent did, because hooks cannot write to its log.
+ case "acl_missing_interactive_write":
+ addFix(plannedAction{Action: "grant_log_access", Target: check.Target, Message: "grant interactive users write access to the log directory"})
+ default:
+ // Anything else here is a permission problem doctor cannot repair. Reported as a
+ // skip rather than dropped: falling out of this switch is how a broken systemd unit
+ // once produced neither a fix nor a skip, and a check that silently repairs nothing
+ // reads exactly like a check that found nothing.
+ if check.Name == "runtime_log_permissions" {
+ message := "review the log permissions manually"
+ if check.Action != "" {
+ message = "run " + check.Action
+ }
+ addSkip(plannedAction{Action: "manual_fix", Target: check.Target, Message: message})
+ }
}
// systemd_unit belongs here alongside launchd_plist: diagnostics emits whichever the
// resolved backend uses, and omitting it meant a broken systemd unit produced neither a
@@ -1183,6 +1217,12 @@ func applyDoctorFixes(plan doctorFixPlan, status lifecycle.Status) error {
if err := repairCollectorServiceFromStatus(status); err != nil {
errs = append(errs, fmt.Errorf("%s %s: %w", action.Action, action.Target, err))
}
+ case "grant_log_access":
+ // The same call install makes, so a repaired endpoint and a fresh one end up with the
+ // same ACL rather than two grants that drift.
+ if err := writer.EnsureSystemLogWritable(action.Target); err != nil {
+ errs = append(errs, fmt.Errorf("%s %s: %w", action.Action, action.Target, err))
+ }
}
}
return errors.Join(errs...)
diff --git a/cli/beacon/cmd/endpoint_diagnostics_action_test.go b/cli/beacon/cmd/endpoint_diagnostics_action_test.go
new file mode 100644
index 00000000..e1b257d5
--- /dev/null
+++ b/cli/beacon/cmd/endpoint_diagnostics_action_test.go
@@ -0,0 +1,68 @@
+package cmd
+
+import (
+ "runtime"
+ "strings"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/diagnostics"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"
+)
+
+// TestLogPermissionRemediationMatchesThePlatformThatFailed guards advice, which is the part of
+// doctor nobody tests and everybody follows.
+//
+// The check used to end in an unconditional `chmod 666`. That was right while only POSIX reached
+// it. Windows reaches it now, where chmod does not exist and would not restore an ACL if it did --
+// so the most consequential line doctor prints there, the remediation for a system-mode log that
+// hooks cannot write, was a command that cannot work. A wrong fix is worse than none: the operator
+// runs it, nothing changes, and the next thing they doubt is the diagnosis.
+func TestLogPermissionRemediationMatchesThePlatformThatFailed(t *testing.T) {
+ action := func(evidence string) string {
+ return actionForCheck(diagnostics.Check{
+ Name: "runtime_log_permissions",
+ Target: `C:\ProgramData\Beacon\Endpoint\logs`,
+ Evidence: evidence,
+ }, lifecycle.RuntimeLogSource{})
+ }
+
+ // Every ACL evidence is Windows-only by construction, so none of them may produce POSIX advice.
+ for _, evidence := range []string{
+ "acl_missing_interactive_write",
+ "acl_not_writable_by_user",
+ "acl_unreadable",
+ } {
+ got := action(evidence)
+ if got == "" {
+ t.Fatalf("evidence %q produced no remediation at all", evidence)
+ }
+ if strings.Contains(got, "chmod") {
+ t.Fatalf("evidence %q suggests %q; chmod does not exist on Windows and would not "+
+ "restore an ACL if it did", evidence, got)
+ }
+ }
+
+ // The system-mode grant is the one that decides whether an endpoint captures anything, so its
+ // advice has to be the actual command, not a gesture at the problem.
+ grant := action("acl_missing_interactive_write")
+ if runtime.GOOS == "windows" {
+ if !strings.Contains(grant, "icacls") || !strings.Contains(grant, "S-1-5-4") {
+ t.Fatalf("the missing-grant remediation is %q; it should be the icacls command that "+
+ "restores the grant, naming the well-known SID rather than a localized account name", grant)
+ }
+ } else if grant != "beacon endpoint doctor --fix" {
+ // No such grant exists here, so the hint is empty and the advice falls back to the command
+ // that does apply. Printing an icacls line on a Mac would be worse than saying nothing.
+ t.Fatalf("off Windows the missing-grant remediation is %q, want the doctor --fix fallback", grant)
+ }
+
+ if runtime.GOOS != "windows" {
+ if got := action("not_writable"); got != `chmod 666 C:\ProgramData\Beacon\Endpoint\logs` {
+ t.Fatalf("the POSIX mode remediation changed: %q", got)
+ }
+ }
+
+ if got := action("runtime_log_missing"); got != "beacon endpoint doctor --fix" {
+ t.Fatalf("missing-log remediation = %q", got)
+ }
+}
diff --git a/cli/beacon/internal/endpoint/diagnostics/diagnostics.go b/cli/beacon/internal/endpoint/diagnostics/diagnostics.go
index 52d58307..619e87bc 100644
--- a/cli/beacon/internal/endpoint/diagnostics/diagnostics.go
+++ b/cli/beacon/internal/endpoint/diagnostics/diagnostics.go
@@ -159,7 +159,10 @@ func checkLogWritableByThisUser(path string) Check {
return Check{Name: "runtime_log_permissions", Target: path, Status: StatusFail,
Severity: SeverityHigh,
Message: "the runtime log is not writable by this user: " + err.Error(),
- Evidence: "not_writable"}
+ // Distinct from the POSIX not_writable on purpose. Evidence strings are what decide the
+ // remediation doctor prints and what fleet tooling keys on, and the two have nothing in
+ // common but the symptom: one is fixed by chmod, which does not exist here.
+ Evidence: "acl_not_writable_by_user"}
}
_ = f.Close()
return Check{Name: "runtime_log_permissions", Target: path, Status: StatusOK,
diff --git a/cli/beacon/internal/endpoint/writer/acl_other.go b/cli/beacon/internal/endpoint/writer/acl_other.go
index 96aeb260..85edd286 100644
--- a/cli/beacon/internal/endpoint/writer/acl_other.go
+++ b/cli/beacon/internal/endpoint/writer/acl_other.go
@@ -12,3 +12,8 @@ func grantInteractiveUsersWrite(dir string) error { return nil }
// EnsureRuntimeFile already establishes. Reporting false here would make doctor demand a fix that
// does not exist on this platform.
func interactiveUsersCanWrite(dir string) (bool, error) { return true, nil }
+
+// GrantCommandHint is empty because there is no grant to run here. Callers use the empty string to
+// mean "this remediation does not apply on this platform" rather than printing an icacls command to
+// someone on a Mac.
+func GrantCommandHint(dir string) string { return "" }
diff --git a/cli/beacon/internal/endpoint/writer/acl_windows.go b/cli/beacon/internal/endpoint/writer/acl_windows.go
index dad2bed7..14b07c4e 100644
--- a/cli/beacon/internal/endpoint/writer/acl_windows.go
+++ b/cli/beacon/internal/endpoint/writer/acl_windows.go
@@ -32,14 +32,7 @@ import (
// (OI)(CI) makes the grant inherit to files and subdirectories created later, so the log and its
// rotated archives are covered without re-running this after every rotation.
func grantInteractiveUsersWrite(dir string) error {
- // icacls rather than the security APIs directly: it is present on every supported Windows,
- // its argument form is stable, and the equivalent through SetNamedSecurityInfo means building
- // an ACL by hand for a grant an administrator can read and audit in one line.
- //
- // The `*` prefix passes the well-known SID literally, so this does not depend on the machine's
- // display language the way a `INTERACTIVE` or `INTERAKTIV` spelling would.
- cmd := exec.Command("icacls", dir, "/grant", `*S-1-5-4:(OI)(CI)(M)`, "/T")
- out, err := cmd.CombinedOutput()
+ out, err := exec.Command("icacls", grantArgs(dir)...).CombinedOutput()
if err != nil {
return fmt.Errorf("grant interactive users write access to %s: %w: %s",
dir, err, strings.TrimSpace(string(out)))
@@ -47,6 +40,39 @@ func grantInteractiveUsersWrite(dir string) error {
return nil
}
+// grantArgs is the single definition of the grant.
+//
+// icacls rather than the security APIs directly: it is present on every supported Windows, its
+// argument form is stable, and the equivalent through SetNamedSecurityInfo means building an ACL by
+// hand for a grant an administrator can read and audit in one line.
+//
+// The `*` prefix passes the well-known SID literally, so this does not depend on the machine's
+// display language the way an `INTERACTIVE` or `INTERAKTIV` spelling would.
+//
+// One definition because doctor prints this command as the remediation for a missing grant. Advice
+// that has drifted from the implementation is worse than no advice: an operator runs it, doctor
+// still reports the failure, and the next thing they doubt is the diagnosis rather than the hint.
+func grantArgs(dir string) []string {
+ return []string{dir, "/grant", `*S-1-5-4:(OI)(CI)(M)`, "/T"}
+}
+
+// GrantCommandHint renders the grant as a command an operator can paste.
+//
+// Empty on platforms with no such grant, which is how callers tell that this remediation does not
+// apply rather than printing a Windows command on a Mac.
+func GrantCommandHint(dir string) string {
+ args := grantArgs(dir)
+ quoted := make([]string, 0, len(args)+1)
+ quoted = append(quoted, "icacls")
+ for _, arg := range args {
+ if strings.ContainsAny(arg, " \t") {
+ arg = `"` + arg + `"`
+ }
+ quoted = append(quoted, arg)
+ }
+ return strings.Join(quoted, " ")
+}
+
// Rights that answer the question doctor is actually asking: can a hook running as the logged-on
// user create the runtime log if it is missing, and append to it if it is not.
//
diff --git a/cli/beacon/internal/endpoint/writer/acl_windows_test.go b/cli/beacon/internal/endpoint/writer/acl_windows_test.go
index cf049668..8e541334 100644
--- a/cli/beacon/internal/endpoint/writer/acl_windows_test.go
+++ b/cli/beacon/internal/endpoint/writer/acl_windows_test.go
@@ -165,3 +165,25 @@ func TestGrantAndReadBackAgree(t *testing.T) {
"the grant was read without the deny that overrides it")
}
}
+
+// TestGrantCommandHintQuotesThePathItPrints covers the half of the grant an operator runs by hand.
+//
+// doctor prints this when the grant is missing, and the default log directory lives under
+// %ProgramData%. A hint that breaks at the first space would be pasted, would fail, and would send
+// the operator looking for a problem in the diagnosis instead of the advice.
+func TestGrantCommandHintQuotesThePathItPrints(t *testing.T) {
+ hint := GrantCommandHint(`C:\Program Files\Beacon\logs`)
+ if !strings.Contains(hint, `"C:\Program Files\Beacon\logs"`) {
+ t.Fatalf("GrantCommandHint left a path containing a space unquoted: %s", hint)
+ }
+ // The SID rather than a localized name, for the same reason the grant itself uses it.
+ if !strings.Contains(hint, "S-1-5-4") {
+ t.Fatalf("GrantCommandHint does not name the well-known SID: %s", hint)
+ }
+ // Same definition as the applied grant, so the advice cannot drift from the implementation.
+ for _, arg := range grantArgs(`C:\Program Files\Beacon\logs`)[1:] {
+ if !strings.Contains(hint, arg) {
+ t.Fatalf("GrantCommandHint omits %q from the grant it claims to describe: %s", arg, hint)
+ }
+ }
+}