The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
feat(hooks): accept the endpoint settings as flags, not only as environment
shukieshah6487c030b00f
diff --git a/cli/beacon-hooks/cmd/root.go b/cli/beacon-hooks/cmd/root.goindex 8403f19a..31cdea0a 100644--- a/cli/beacon-hooks/cmd/root.go+++ b/cli/beacon-hooks/cmd/root.go@@ -3,12 +3,30 @@ package cmdimport ("fmt""os"+ "strings""github.com/spf13/cobra")var platformFlag string+// The endpoint settings a hook needs, as flags.+//+// They already existed as environment variables, and still do -- an already-installed hook carries+// them as an inline `VAR=value cmd` prefix and must keep working. Flags exist because that prefix is+// a POSIX shell construct: it is not valid in cmd.exe or PowerShell, so on Windows there is no way+// to put the values in the environment from inside a hook command string at all.+//+// Passing them as arguments removes the shell from the equation entirely. One argv, no quoting rules+// beyond the path itself, and the same command works whichever shell a runtime happens to invoke it+// with -- which is also a simplification on POSIX, where the only reason a shell was involved was to+// carry these three values.+var (+ logFlag string+ configFlag string+ cliFlag string+)+var rootCmd = &cobra.Command{Use: "beacon-hooks",Short: "Beacon hooks for Claude Code, Codex, GitHub Copilot, Cursor, VS Code, Devin, Factory, Grok, Hermes, Antigravity, and opencode",@@ -22,6 +40,48 @@ Use --platform to specify the calling platform (default: claude).`,func init() {rootCmd.PersistentFlags().StringVar(&platformFlag, "platform", "claude", "Platform context: claude, codex, antigravity, copilot, cursor, vscode, devin, devin-cli, devin-desktop, factory, grok, hermes, or opencode")+ rootCmd.PersistentFlags().StringVar(&logFlag, "log", "", "Endpoint runtime log to append to (same value as BEACON_ENDPOINT_LOG)")+ rootCmd.PersistentFlags().StringVar(&configFlag, "config", "", "Endpoint config to read (same value as BEACON_ENDPOINT_CONFIG)")+ rootCmd.PersistentFlags().StringVar(&cliFlag, "cli", "", "Path to the beacon CLI for inventory heartbeats (same value as BEACON_ENDPOINT_CLI)")+ rootCmd.PersistentPreRun = func(*cobra.Command, []string) { applyEndpointFlagsToEnv() }+}++// applyEndpointFlagsToEnv puts flag values where the rest of the binary already looks for them.+//+// Deliberately a translation into the environment rather than a config struct threaded through every+// reader. These values are read in eight places across three packages -- logging, cloudshuttle, the+// event commands -- and every one of them reads the environment today. Rewriting all of them to+// accept a parameter would be a large mechanical change whose only purpose is plumbing, and it would+// leave two ways to answer the same question during the transition. One source of truth is worth+// more here than avoiding a Setenv in a process that exists for a few milliseconds and does one job.+//+// It also means a child process inherits them, which is what the inventory heartbeat subprocess+// needs anyway.+//+// A flag beats an inherited variable. Both can be present -- an already-installed POSIX hook carries+// the env prefix, and a repair rewrites the command to use flags -- and when they disagree the flag+// is the more specific statement of intent, because something wrote it into this exact command.+func applyEndpointFlagsToEnv() {+ for _, mapping := range []struct{ value, key string }{+ {logFlag, "BEACON_ENDPOINT_LOG"},+ {configFlag, "BEACON_ENDPOINT_CONFIG"},+ {cliFlag, "BEACON_ENDPOINT_CLI"},+ } {+ if value := strings.TrimSpace(mapping.value); value != "" {+ _ = os.Setenv(mapping.key, value)+ }+ }++ // Any of them implies endpoint mode, which is the condition several readers gate on before+ // falling back to a default log path. Beacon only ever passes these when it installed the hook+ // for an endpoint, so the presence of one is the same signal BEACON_ENDPOINT_MODE=1 carried --+ // and inferring it here means the emitted command does not have to spend an argument saying so.+ if strings.TrimSpace(logFlag) == "" && strings.TrimSpace(configFlag) == "" && strings.TrimSpace(cliFlag) == "" {+ return+ }+ if os.Getenv("BEACON_ENDPOINT_MODE") == "" {+ _ = os.Setenv("BEACON_ENDPOINT_MODE", "1")+ }}// Execute runs the root commanddiff --git a/cli/beacon-hooks/cmd/root_endpoint_flags_test.go b/cli/beacon-hooks/cmd/root_endpoint_flags_test.gonew file mode 100644index 00000000..6cba18b8--- /dev/null+++ b/cli/beacon-hooks/cmd/root_endpoint_flags_test.go@@ -0,0 +1,99 @@+package cmd++import (+ "os"+ "testing"+)++// withFlags sets the package-level flag values for one test and restores them after.+//+// Cobra binds them to globals, so a test that forgot to restore would leak into the next one and the+// failure would land somewhere unrelated.+func withFlags(t *testing.T, logValue, configValue, cliValue string) {+ t.Helper()+ prevLog, prevConfig, prevCLI := logFlag, configFlag, cliFlag+ t.Cleanup(func() { logFlag, configFlag, cliFlag = prevLog, prevConfig, prevCLI })+ logFlag, configFlag, cliFlag = logValue, configValue, cliValue+}++func TestEndpointFlagsReachTheReadersThatOnlyLookAtTheEnvironment(t *testing.T) {+ t.Setenv("BEACON_ENDPOINT_LOG", "")+ t.Setenv("BEACON_ENDPOINT_CONFIG", "")+ t.Setenv("BEACON_ENDPOINT_CLI", "")+ t.Setenv("BEACON_ENDPOINT_MODE", "")+ withFlags(t, `C:\ProgramData\Beacon\Endpoint\logs\runtime.jsonl`, `C:\ProgramData\Beacon\Endpoint\config.json`, `C:\Program Files\Beacon\bin\beacon.exe`)++ applyEndpointFlagsToEnv()++ for key, want := range map[string]string{+ "BEACON_ENDPOINT_LOG": `C:\ProgramData\Beacon\Endpoint\logs\runtime.jsonl`,+ "BEACON_ENDPOINT_CONFIG": `C:\ProgramData\Beacon\Endpoint\config.json`,+ "BEACON_ENDPOINT_CLI": `C:\Program Files\Beacon\bin\beacon.exe`,+ // Inferred, not passed. Several readers gate on it before falling back to a default log+ // path, so without it a hook installed by flags alone would write to the wrong file.+ "BEACON_ENDPOINT_MODE": "1",+ } {+ if got := os.Getenv(key); got != want {+ t.Fatalf("%s = %q, want %q", key, got, want)+ }+ }+}++// TestAFlagBeatsAnInheritedVariable covers the state a repair passes through.+//+// An already-installed POSIX hook carries these values as an inline env prefix. A repair rewrites the+// command to use flags, and until every runtime config has been rewritten a single invocation can+// carry both. The flag is the more specific statement of intent: something wrote it into this exact+// command, while the variable could have come from anywhere in the parent environment.+func TestAFlagBeatsAnInheritedVariable(t *testing.T) {+ t.Setenv("BEACON_ENDPOINT_LOG", "/var/log/beacon-agent/runtime.jsonl")+ t.Setenv("BEACON_ENDPOINT_MODE", "1")+ withFlags(t, "/home/dev/.beacon/endpoint/logs/runtime.jsonl", "", "")++ applyEndpointFlagsToEnv()++ if got := os.Getenv("BEACON_ENDPOINT_LOG"); got != "/home/dev/.beacon/endpoint/logs/runtime.jsonl" {+ t.Fatalf("BEACON_ENDPOINT_LOG = %q, want the flag value to win", got)+ }+}++// TestNoFlagsChangesNothing is what keeps every already-installed hook working.+//+// Those hooks pass no flags at all. If this function invented an endpoint mode or a log path for+// them, it would redirect a working POSIX install to a different file on the next hook call.+func TestNoFlagsChangesNothing(t *testing.T) {+ t.Setenv("BEACON_ENDPOINT_LOG", "/var/log/beacon-agent/runtime.jsonl")+ t.Setenv("BEACON_ENDPOINT_CONFIG", "")+ t.Setenv("BEACON_ENDPOINT_CLI", "")+ t.Setenv("BEACON_ENDPOINT_MODE", "")+ withFlags(t, "", "", "")++ applyEndpointFlagsToEnv()++ if got := os.Getenv("BEACON_ENDPOINT_LOG"); got != "/var/log/beacon-agent/runtime.jsonl" {+ t.Fatalf("BEACON_ENDPOINT_LOG = %q, want it untouched", got)+ }+ if got := os.Getenv("BEACON_ENDPOINT_MODE"); got != "" {+ t.Fatalf("BEACON_ENDPOINT_MODE = %q; a hook that passed no flags is not necessarily an "++ "endpoint hook, and claiming otherwise gives it a default log path it should not have", got)+ }+}++// TestWhitespaceOnlyFlagsAreNotValues guards the shape a shell leaves behind.+//+// A command string that lost its value to a quoting mistake produces an empty or blank flag rather+// than an absent one. Treating that as a path would point a hook at "" and lose its events silently.+func TestWhitespaceOnlyFlagsAreNotValues(t *testing.T) {+ t.Setenv("BEACON_ENDPOINT_LOG", "/var/log/beacon-agent/runtime.jsonl")+ t.Setenv("BEACON_ENDPOINT_MODE", "")+ withFlags(t, " ", "", "")++ applyEndpointFlagsToEnv()++ if got := os.Getenv("BEACON_ENDPOINT_LOG"); got != "/var/log/beacon-agent/runtime.jsonl" {+ t.Fatalf("BEACON_ENDPOINT_LOG = %q, want a blank flag ignored rather than applied", got)+ }+ if got := os.Getenv("BEACON_ENDPOINT_MODE"); got != "" {+ t.Fatalf("BEACON_ENDPOINT_MODE = %q, want a blank flag not to imply endpoint mode", got)+ }+}