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
test(hooks): pin the Cursor and Codex hook files Beacon writes (#1096)
Shukan Shahb5cdbeec350b
diff --git a/cli/beacon/internal/endpoint/hooks/claude_extension_test.go b/cli/beacon/internal/endpoint/hooks/claude_extension_test.goindex f0d0f19b..3bfcf32e 100644--- a/cli/beacon/internal/endpoint/hooks/claude_extension_test.go+++ b/cli/beacon/internal/endpoint/hooks/claude_extension_test.go@@ -11,6 +11,7 @@ import (// The extension points are nil in this build, and with them nil the settings Beacon writes must// not change by a byte. The golden file was captured from the installer before they existed.func TestInstallClaudeSettingsUnchangedWithoutExtensions(t *testing.T) {+ withoutHookExtensions(t)path := filepath.Join(t.TempDir(), "settings.json")existing := `{"env":{"EXISTING":"1"},"hooks":{"SessionStart":[{"hooks":[{"type":"command","command":"echo keep"}]}],"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"http","url":"https://guard.example/check","headers":{"X":"1"}}]}]}}`if err := os.WriteFile(path, []byte(existing), 0600); err != nil {@@ -40,6 +41,7 @@ func TestClaudeExtraHookGroupsFollowBeaconHooks(t *testing.T) {installed bool}var changes []change+ withoutHookExtensions(t)claudeExtraHookGroups = func(prefix string) map[string][]settingsHookGroup {return map[string][]settingsHookGroup{"PreToolUse": {{Matcher: "mcp__.*", Hooks: []settingsHookRef{{Type: "command", Command: prefix + " extra-tool", Timeout: 15}}}},@@ -50,10 +52,6 @@ func TestClaudeExtraHookGroupsFollowBeaconHooks(t *testing.T) {changes = append(changes, change{path, installed})return nil}- t.Cleanup(func() {- claudeExtraHookGroups = nil- claudeSettingsChanged = nil- })path := filepath.Join(t.TempDir(), "settings.json")existing := `{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"echo keep"}]}]}}`diff --git a/cli/beacon/internal/endpoint/hooks/hook_goldens_test.go b/cli/beacon/internal/endpoint/hooks/hook_goldens_test.gonew file mode 100644index 00000000..ba740af7--- /dev/null+++ b/cli/beacon/internal/endpoint/hooks/hook_goldens_test.go@@ -0,0 +1,147 @@+package hooks++import (+ "bytes"+ "os"+ "path/filepath"+ "testing"+)++// withoutHookExtensions clears the hook extension points (see version.Edition) for the rest of the+// test and restores whatever they held afterwards. The golden tests pin what this repository's+// installer writes on its own; a build that fills the extension points still runs them, and they+// must still hold there.+func withoutHookExtensions(t *testing.T) {+ t.Helper()+ claudeGroups, claudeChanged := claudeExtraHookGroups, claudeSettingsChanged+ claudeExtraHookGroups, claudeSettingsChanged = nil, nil+ t.Cleanup(func() {+ claudeExtraHookGroups, claudeSettingsChanged = claudeGroups, claudeChanged+ })+}++// requireGolden compares got with testdata/<name> byte for byte. The golden files were captured+// from the installer as it is and are never regenerated by a test run.+func requireGolden(t *testing.T, got []byte, name string) {+ t.Helper()+ want, err := os.ReadFile(filepath.Join("testdata", name))+ if err != nil {+ t.Fatal(err)+ }+ // A Windows checkout turns the golden file's line endings into CRLF; Beacon writes LF.+ want = bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n"))+ if !bytes.Equal(got, want) {+ t.Fatalf("%s changed:\ngot:\n%s\nwant:\n%s", name, got, want)+ }+}++// A Cursor hooks.json holding the user's own entries (a fail-closed command, a prompt hook, an+// extra top-level key) beside Beacon entries an older version wrote: one in an event Beacon still+// installs, which the install replaces, and one in an event it no longer installs, which only an+// uninstall removes.+const cursorGoldenExisting = `{+ "version": 1,+ "note": {"owner": "user"},+ "hooks": {+ "beforeSubmitPrompt": [+ {"command": "/usr/local/bin/prompt-guard", "timeout": 5, "failClosed": true},+ {"command": "BEACON_ENDPOINT_MODE=1 BEACON_ENDPOINT_LOG=/tmp/old.jsonl /old/beacon-hooks --platform cursor prompt-submit", "timeout": 30}+ ],+ "beforeShellExecution": [+ {"type": "prompt", "prompt": "Is this command safe to run?", "model": "fast"},+ {"command": "BEACON_ENDPOINT_MODE=1 BEACON_ENDPOINT_LOG=/tmp/old.jsonl /old/beacon-hooks --platform cursor pre-tool"}+ ],+ "afterFileEdit": [+ {"command": "echo edited"}+ ]+ }+}`++func TestCursorHooksGolden(t *testing.T) {+ withoutHookExtensions(t)+ path := filepath.Join(t.TempDir(), "hooks.json")+ if err := os.WriteFile(path, []byte(cursorGoldenExisting), 0644); err != nil {+ t.Fatal(err)+ }+ if err := installCursorHooksJSON(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {+ t.Fatal(err)+ }+ installed, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, installed, "cursor_hooks_golden.json")++ if err := installCursorHooksJSON(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {+ t.Fatal(err)+ }+ again, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, again, "cursor_hooks_golden.json")++ if _, err := removeEndpointHooks(path); err != nil {+ t.Fatal(err)+ }+ removed, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, removed, "cursor_hooks_removed_golden.json")+}++// A Codex hooks.json holding the user's own groups (one with a field Beacon does not model) and an+// extra top-level key, beside groups an older version of Beacon wrote, including one for an event+// Beacon no longer hooks.+const codexGoldenExisting = `{+ "note": "user-owned",+ "hooks": {+ "PreToolUse": [+ {"matcher": "Bash", "hooks": [{"type": "command", "command": "/usr/local/bin/codex-guard", "timeout": 5, "statusMessage": "Checking the command"}]}+ ],+ "SessionStart": [+ {"hooks": [{"type": "command", "command": "echo hello"}, {"type": "command", "command": "'/old/beacon-hooks' --platform codex --log '/tmp/old.jsonl' codex-session-context", "timeout": 10}]}+ ],+ "UserPromptSubmit": [+ {"hooks": [{"type": "command", "command": "BEACON_ENDPOINT_MODE=1 /old/beacon-hooks --platform codex inventory-heartbeat"}]}+ ],+ "Stop": [+ {"hooks": [{"type": "command", "command": "echo done"}]}+ ]+ }+}`++func TestCodexHooksGolden(t *testing.T) {+ withoutHookExtensions(t)+ path := filepath.Join(t.TempDir(), "hooks.json")+ if err := os.WriteFile(path, []byte(codexGoldenExisting), 0600); err != nil {+ t.Fatal(err)+ }+ if err := installCodexHooks(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {+ t.Fatal(err)+ }+ installed, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, installed, "codex_hooks_golden.json")++ if err := installCodexHooks(path, "/opt/beacon hooks/beacon-hooks", "/var/log/beacon-agent/runtime.jsonl", "/Library/Application Support/Beacon/Endpoint/config.json"); err != nil {+ t.Fatal(err)+ }+ again, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, again, "codex_hooks_golden.json")++ if _, err := removeCodexEndpointHooks(path); err != nil {+ t.Fatal(err)+ }+ removed, err := os.ReadFile(path)+ if err != nil {+ t.Fatal(err)+ }+ requireGolden(t, removed, "codex_hooks_removed_golden.json")+}diff --git a/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_golden.json b/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_golden.jsonnew file mode 100644index 00000000..52c78ce9--- /dev/null+++ b/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_golden.json@@ -0,0 +1,56 @@+{+ "hooks": {+ "PreToolUse": [+ {+ "matcher": "Bash",+ "hooks": [+ {+ "type": "command",+ "command": "/usr/local/bin/codex-guard",+ "timeout": 5,+ "statusMessage": "Checking the command"+ }+ ]+ }+ ],+ "SessionStart": [+ {+ "hooks": [+ {+ "type": "command",+ "command": "echo hello"+ }+ ]+ },+ {+ "hooks": [+ {+ "type": "command",+ "command": "'/opt/beacon hooks/beacon-hooks' --platform codex --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' codex-session-context",+ "timeout": 10+ }+ ]+ }+ ],+ "Stop": [+ {+ "hooks": [+ {+ "type": "command",+ "command": "echo done"+ }+ ]+ },+ {+ "hooks": [+ {+ "type": "command",+ "command": "'/opt/beacon hooks/beacon-hooks' --platform codex --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' codex-assistant-response",+ "timeout": 10+ }+ ]+ }+ ]+ },+ "note": "user-owned"+}\ No newline at end of filediff --git a/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_removed_golden.json b/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_removed_golden.jsonnew file mode 100644index 00000000..d6fb8e53--- /dev/null+++ b/cli/beacon/internal/endpoint/hooks/testdata/codex_hooks_removed_golden.json@@ -0,0 +1,38 @@+{+ "hooks": {+ "PreToolUse": [+ {+ "matcher": "Bash",+ "hooks": [+ {+ "type": "command",+ "command": "/usr/local/bin/codex-guard",+ "timeout": 5,+ "statusMessage": "Checking the command"+ }+ ]+ }+ ],+ "SessionStart": [+ {+ "hooks": [+ {+ "type": "command",+ "command": "echo hello"+ }+ ]+ }+ ],+ "Stop": [+ {+ "hooks": [+ {+ "type": "command",+ "command": "echo done"+ }+ ]+ }+ ]+ },+ "note": "user-owned"+}\ No newline at end of filediff --git a/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_golden.json b/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_golden.jsonnew file mode 100644index 00000000..44245a12--- /dev/null+++ b/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_golden.json@@ -0,0 +1,78 @@+{+ "hooks": {+ "afterAgentResponse": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' agent-response"+ }+ ],+ "afterAgentThought": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' agent-thought"+ }+ ],+ "afterFileEdit": [+ {+ "command": "echo edited"+ },+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' post-tool"+ }+ ],+ "beforeShellExecution": [+ {+ "type": "prompt",+ "prompt": "Is this command safe to run?",+ "model": "fast"+ },+ {+ "command": "BEACON_ENDPOINT_MODE=1 BEACON_ENDPOINT_LOG=/tmp/old.jsonl /old/beacon-hooks --platform cursor pre-tool"+ }+ ],+ "beforeSubmitPrompt": [+ {+ "command": "/usr/local/bin/prompt-guard",+ "timeout": 5,+ "failClosed": true+ },+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' prompt-submit",+ "timeout": 30+ }+ ],+ "postToolUse": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' post-tool"+ }+ ],+ "postToolUseFailure": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' post-tool"+ }+ ],+ "preToolUse": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' pre-tool",+ "matcher": "Write|Edit|MultiEdit|Shell|MCP"+ }+ ],+ "sessionEnd": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' session-end"+ }+ ],+ "sessionStart": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' session-start"+ }+ ],+ "stop": [+ {+ "command": "'/opt/beacon hooks/beacon-hooks' --platform cursor --log '/var/log/beacon-agent/runtime.jsonl' --config '/Library/Application Support/Beacon/Endpoint/config.json' stop"+ }+ ]+ },+ "note": {+ "owner": "user"+ },+ "version": 1+}\ No newline at end of filediff --git a/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_removed_golden.json b/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_removed_golden.jsonnew file mode 100644index 00000000..2196e589--- /dev/null+++ b/cli/beacon/internal/endpoint/hooks/testdata/cursor_hooks_removed_golden.json@@ -0,0 +1,27 @@+{+ "hooks": {+ "afterFileEdit": [+ {+ "command": "echo edited"+ }+ ],+ "beforeShellExecution": [+ {+ "type": "prompt",+ "prompt": "Is this command safe to run?",+ "model": "fast"+ }+ ],+ "beforeSubmitPrompt": [+ {+ "command": "/usr/local/bin/prompt-guard",+ "timeout": 5,+ "failClosed": true+ }+ ]+ },+ "note": {+ "owner": "user"+ },+ "version": 1+}\ No newline at end of file