Skip to content

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

Extend shared runtime log mode to collector exporter, doctor --fix, and Jamf repair

Claudebc40a8de5241
diff --git a/cli/beacon/cmd/endpoint_elastic.go b/cli/beacon/cmd/endpoint_elastic.go
index 57f17361..9ea381d8 100644
--- a/cli/beacon/cmd/endpoint_elastic.go
+++ b/cli/beacon/cmd/endpoint_elastic.go
@@ -10,6 +10,7 @@ import (
"strings"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/elastic"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"
)
func runEndpointElasticUp(ctx context.Context) error {
@@ -85,14 +86,7 @@ func ensureLogFile(path string) error {
if path == "" {
return nil
}
- if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
- return err
- }
- file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND, 0644)
- if err != nil {
- return err
- }
- return file.Close()
+ return writer.EnsureRuntimeFile(path)
}
func runDockerCompose(ctx context.Context, dir string, env []string, args ...string) error {
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index 8d1ed4bb..01d53385 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -82,6 +82,20 @@ func AppendEvent(event schema.Event, opts Options) (string, error) {
return opts.Path, nil
}
+// EnsureRuntimeFile creates the shared runtime log with the shared writable
+// mode without appending an event, for doctor --fix and setup flows that
+// pre-create the log so user-run hooks can append to it later.
+func EnsureRuntimeFile(path string) error {
+ if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
+ return err
+ }
+ f, err := openRuntimeFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY)
+ if err != nil {
+ return err
+ }
+ return f.Close()
+}
+
func LastLine(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
diff --git a/cli/beacon/internal/endpoint/writer/writer_test.go b/cli/beacon/internal/endpoint/writer/writer_test.go
index 5b1eb94b..94694570 100644
--- a/cli/beacon/internal/endpoint/writer/writer_test.go
+++ b/cli/beacon/internal/endpoint/writer/writer_test.go
@@ -71,6 +71,26 @@ func TestAppendEventCreatesSharedRuntimeFilesDespiteUmask(t *testing.T) {
}
}
+func TestEnsureRuntimeFileCreatesSharedRuntimeLogDespiteUmask(t *testing.T) {
+ oldUmask := syscall.Umask(0022)
+ t.Cleanup(func() {
+ syscall.Umask(oldUmask)
+ })
+
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ if err := EnsureRuntimeFile(path); err != nil {
+ t.Fatalf("EnsureRuntimeFile returned error: %v", err)
+ }
+
+ info, err := os.Stat(path)
+ if err != nil {
+ t.Fatalf("stat %s: %v", path, err)
+ }
+ if got := info.Mode().Perm(); got != runtimeFileMode {
+ t.Fatalf("%s mode = %o, want %o", path, got, runtimeFileMode)
+ }
+}
+
func TestAppendEventRedactsSecrets(t *testing.T) {
path := filepath.Join(t.TempDir(), "runtime.jsonl")
event := schema.NewEvent(schema.NewEventOptions{
diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.go
index 96a1cc76..de015e14 100644
--- a/collector-builder/exporter/beaconjsonexporter/writer.go
+++ b/collector-builder/exporter/beaconjsonexporter/writer.go
@@ -123,12 +123,17 @@ func sanitizeTyped[T any](input *T, sanitize func(map[string]interface{}) map[st
return &out
}
+// The collector usually runs as root, while agent hooks append to the same
+// runtime log as the console user, so shared runtime files must stay
+// writable by both regardless of which process creates them first.
+const runtimeFileMode = 0666
+
// Keep this rotation contract mirrored with the endpoint CLI and hook writer.
func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
- lock, err := os.OpenFile(path+".lock", os.O_CREATE|os.O_RDWR, 0644)
+ lock, err := openRuntimeFile(path+".lock", os.O_CREATE|os.O_RDWR)
if err != nil {
return err
}
@@ -144,7 +149,7 @@ func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int
if err := rotateIfNeeded(path, rotateBytes, rotateArchives, int64(len(line))); err != nil {
return err
}
- f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ f, err := openRuntimeFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY)
if err != nil {
return err
}
@@ -153,6 +158,25 @@ func appendJSONL(path string, line []byte, rotateBytes int64, rotateArchives int
return err
}
+func openRuntimeFile(path string, flag int) (*os.File, error) {
+ existed := true
+ if _, err := os.Stat(path); err != nil {
+ if !os.IsNotExist(err) {
+ return nil, err
+ }
+ existed = false
+ }
+ f, err := os.OpenFile(path, flag, runtimeFileMode)
+ if err != nil {
+ return nil, err
+ }
+ if err := os.Chmod(path, runtimeFileMode); err != nil && !existed {
+ _ = f.Close()
+ return nil, err
+ }
+ return f, nil
+}
+
func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int64) error {
if maxSize <= 0 {
return nil
diff --git a/collector-builder/exporter/beaconjsonexporter/writer_test.go b/collector-builder/exporter/beaconjsonexporter/writer_test.go
new file mode 100644
index 00000000..01967d5c
--- /dev/null
+++ b/collector-builder/exporter/beaconjsonexporter/writer_test.go
@@ -0,0 +1,30 @@
+package beaconjsonexporter
+
+import (
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
+)
+
+func TestAppendJSONLCreatesSharedRuntimeFilesDespiteUmask(t *testing.T) {
+ oldUmask := syscall.Umask(0022)
+ t.Cleanup(func() {
+ syscall.Umask(oldUmask)
+ })
+
+ path := filepath.Join(t.TempDir(), "runtime.jsonl")
+ if err := appendJSONL(path, []byte(`{"message":"test"}`+"\n"), defaultRotateBytes, defaultRotateArchives); err != nil {
+ t.Fatalf("appendJSONL returned error: %v", err)
+ }
+
+ for _, target := range []string{path, path + ".lock"} {
+ info, err := os.Stat(target)
+ if err != nil {
+ t.Fatalf("stat %s: %v", target, err)
+ }
+ if got := info.Mode().Perm(); got != runtimeFileMode {
+ t.Fatalf("%s mode = %o, want %o", target, got, runtimeFileMode)
+ }
+ }
+}
diff --git a/packaging/macos/jamf/claude/common/repair-hooks.sh b/packaging/macos/jamf/claude/common/repair-hooks.sh
index 4eccee16..67c85554 100755
--- a/packaging/macos/jamf/claude/common/repair-hooks.sh
+++ b/packaging/macos/jamf/claude/common/repair-hooks.sh
@@ -43,7 +43,11 @@ mkdir -p "$RUNTIME_DIR"
touch "$RUNTIME_LOG" "$RUNTIME_LOCK" "$INVENTORY_LOG" "$INVENTORY_LOCK" "$INVENTORY_STATE" "$INVENTORY_STATE_LOCK"
chown root:wheel "$RUNTIME_DIR" "$RUNTIME_LOG" "$RUNTIME_LOCK" "$INVENTORY_LOG" "$INVENTORY_LOCK" "$INVENTORY_STATE" "$INVENTORY_STATE_LOCK"
chmod 755 "$RUNTIME_DIR"
-chmod 644 "$RUNTIME_LOG" "$RUNTIME_LOCK" "$INVENTORY_LOG" "$INVENTORY_LOCK" "$INVENTORY_STATE" "$INVENTORY_STATE_LOCK"
+# Runtime log and lock stay 666 so user-run hooks can append even when a
+# root-owned rotation or collector recreates them; Beacon's writers use the
+# same shared mode.
+chmod 666 "$RUNTIME_LOG" "$RUNTIME_LOCK"
+chmod 644 "$INVENTORY_LOG" "$INVENTORY_LOCK" "$INVENTORY_STATE" "$INVENTORY_STATE_LOCK"
CONSOLE_USER="$(stat -f %Su /dev/console 2>/dev/null || true)"