Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cli): remove owned Claude scripts with no-hooks

Andy11-cpub0713cc24dcd
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index 5fd4c84f5..aa192261e 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -1694,6 +1694,7 @@ echo "OK 8e: shim installed, non-blocking, delegates to hook-augment"
HOME="$FAKE_HOME" XDG_CONFIG_HOME="$FAKE_HOME/.config" APPDATA="$FAKE_HOME/AppData/Roaming" LOCALAPPDATA="$FAKE_HOME/AppData/Local" PATH="$FAKE_HOME/.local/bin:$PATH" \
"$BINARY" install --no-hooks --skip-binary --clients=claude -y >/dev/null
if grep -qE 'cbm-(code-discovery-gate|session-reminder|subagent-reminder)' "$FAKE_HOME/.claude/settings.json" 2>/dev/null ||
+ [ -e "$FAKE_HOME/.claude/hooks/cbm-code-discovery-gate" ] ||
! grep -q 'codebase-memory-mcp' "$FAKE_HOME/.claude.json" 2>/dev/null; then
echo "FAIL 8e-i: --no-hooks did not remove owned Claude hooks while preserving MCP"
exit 1
diff --git a/src/cli/cli.c b/src/cli/cli.c
index cd1837260..0fd9aca4e 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -7663,6 +7663,93 @@ static void uninstall_tiered_profile_prompts(const char *label, const char *veri
cbm_graph_profile_dialect_t dialect,
const char *legacy_verify_content, bool dry_run);
+static int remove_claude_owned_hook_scripts(const char *config_dir, const char *binary_path,
+ bool uninstall) {
+ char current_gate[CLI_BUF_8K];
+ char current_session[CLI_BUF_8K];
+ char current_subagent[CLI_BUF_8K];
+ char released_gate[CLI_BUF_8K];
+ const char *const gate_legacy[] = {released_gate};
+ const char *const session_legacy[] = {cmm_released_session_script};
+ const char *const subagent_legacy[] = {cmm_released_subagent_script};
+ size_t gate_legacy_count =
+ cbm_build_released_gate_script(binary_path, released_gate, sizeof(released_gate)) == CLI_OK
+ ? 1U
+ : 0U;
+ static const struct {
+ const char *name;
+ const char *legacy_name;
+ const char *prefix;
+ } hook_types[] = {
+ {CMM_HOOK_GATE_SCRIPT, CMM_HOOK_GATE_SCRIPT_LEGACY, cmm_gate_script_prefix},
+ {CMM_SESSION_REMINDER_SCRIPT, CMM_SESSION_REMINDER_SCRIPT_LEGACY,
+ cmm_session_script_prefix},
+ {CMM_SUBAGENT_REMINDER_SCRIPT, CMM_SUBAGENT_REMINDER_SCRIPT_LEGACY,
+ cmm_subagent_script_prefix},
+ };
+ struct {
+ const char *name;
+ const char *legacy_name;
+ const char *current;
+ const char *const *legacy;
+ size_t legacy_count;
+ bool current_valid;
+ } owned_scripts[] = {
+ {hook_types[0].name, hook_types[0].legacy_name, current_gate, gate_legacy,
+ gate_legacy_count,
+ cbm_build_current_hook_script(hook_types[0].prefix, binary_path, current_gate,
+ sizeof(current_gate)) == CLI_OK},
+ {hook_types[1].name, hook_types[1].legacy_name, current_session, session_legacy, 1U,
+ cbm_build_current_hook_script(hook_types[1].prefix, binary_path, current_session,
+ sizeof(current_session)) == CLI_OK},
+ {hook_types[2].name, hook_types[2].legacy_name, current_subagent, subagent_legacy, 1U,
+ cbm_build_current_hook_script(hook_types[2].prefix, binary_path, current_subagent,
+ sizeof(current_subagent)) == CLI_OK},
+ };
+ char hooks_dir[CLI_BUF_1K];
+ int hooks_written = snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir);
+ bool hooks_dir_valid = hooks_written > 0 && (size_t)hooks_written < sizeof(hooks_dir);
+ int rc = CLI_OK;
+ const char *op = uninstall ? "hook_script_uninstall" : "hook_script_disable";
+ for (size_t i = 0; i < sizeof(owned_scripts) / sizeof(owned_scripts[0]); i++) {
+ char script_path[CLI_BUF_1K];
+ int script_written = hooks_dir_valid ? snprintf(script_path, sizeof(script_path), "%s/%s",
+ hooks_dir, owned_scripts[i].name)
+ : CLI_ERR;
+ bool script_path_valid = script_written > 0 && (size_t)script_written < sizeof(script_path);
+ if (!owned_scripts[i].current_valid) {
+ record_agent_config_error(uninstall, "Claude Code", op, owned_scripts[i].name);
+ rc = CLI_ERR;
+ continue;
+ }
+ if (!script_path_valid ||
+ cbm_remove_owned_hook_script(script_path, owned_scripts[i].current,
+ owned_scripts[i].legacy,
+ owned_scripts[i].legacy_count) < CLI_OK) {
+ record_agent_config_error(uninstall, "Claude Code", op,
+ script_path_valid ? script_path : owned_scripts[i].name);
+ rc = CLI_ERR;
+ }
+#ifdef _WIN32
+ if (!hooks_dir_valid ||
+ cbm_remove_owned_legacy_hook_script(hooks_dir, owned_scripts[i].legacy_name,
+ owned_scripts[i].current, owned_scripts[i].legacy,
+ owned_scripts[i].legacy_count) != CLI_OK) {
+ char legacy_path[CLI_BUF_1K];
+ int written = hooks_dir_valid ? snprintf(legacy_path, sizeof(legacy_path), "%s/%s",
+ hooks_dir, owned_scripts[i].legacy_name)
+ : CLI_ERR;
+ record_agent_config_error(uninstall, "Claude Code", "legacy_hook_script_uninstall",
+ written > 0 && (size_t)written < sizeof(legacy_path)
+ ? legacy_path
+ : owned_scripts[i].legacy_name);
+ rc = CLI_ERR;
+ }
+#endif
+ }
+ return rc;
+}
+
static void install_claude_code_config(const char *home, const char *binary_path, bool force,
bool dry_run) {
char config_dir[CLI_BUF_1K];
@@ -7752,6 +7839,7 @@ static void install_claude_code_config(const char *home, const char *binary_path
if (cbm_remove_claude_subagent_hooks(settings_path) != CLI_OK) {
record_agent_config_error(false, "Claude Code", "hook_disable", settings_path);
}
+ remove_claude_owned_hook_scripts(config_dir, binary_path, false);
}
print_hooks_skipped();
goto claude_hooks_done;
@@ -10787,86 +10875,7 @@ static void uninstall_claude_code(const char *home, bool dry_run) {
record_agent_config_error(true, "Claude Code", "subagent_hook_uninstall",
settings_path);
}
- char current_gate[CLI_BUF_8K];
- char current_session[CLI_BUF_8K];
- char current_subagent[CLI_BUF_8K];
- char released_gate[CLI_BUF_8K];
- const char *const gate_legacy[] = {released_gate};
- const char *const session_legacy[] = {cmm_released_session_script};
- const char *const subagent_legacy[] = {cmm_released_subagent_script};
- size_t gate_legacy_count = cbm_build_released_gate_script(installed_binary, released_gate,
- sizeof(released_gate)) == CLI_OK
- ? 1U
- : 0U;
- static const struct {
- const char *name;
- const char *legacy_name;
- const char *prefix;
- } hook_types[] = {
- {CMM_HOOK_GATE_SCRIPT, CMM_HOOK_GATE_SCRIPT_LEGACY, cmm_gate_script_prefix},
- {CMM_SESSION_REMINDER_SCRIPT, CMM_SESSION_REMINDER_SCRIPT_LEGACY,
- cmm_session_script_prefix},
- {CMM_SUBAGENT_REMINDER_SCRIPT, CMM_SUBAGENT_REMINDER_SCRIPT_LEGACY,
- cmm_subagent_script_prefix},
- };
- struct {
- const char *name;
- const char *legacy_name;
- const char *current;
- const char *const *legacy;
- size_t legacy_count;
- bool current_valid;
- } owned_scripts[] = {
- {hook_types[0].name, hook_types[0].legacy_name, current_gate, gate_legacy,
- gate_legacy_count,
- cbm_build_current_hook_script(hook_types[0].prefix, installed_binary, current_gate,
- sizeof(current_gate)) == CLI_OK},
- {hook_types[1].name, hook_types[1].legacy_name, current_session, session_legacy, 1U,
- cbm_build_current_hook_script(hook_types[1].prefix, installed_binary, current_session,
- sizeof(current_session)) == CLI_OK},
- {hook_types[2].name, hook_types[2].legacy_name, current_subagent, subagent_legacy, 1U,
- cbm_build_current_hook_script(hook_types[2].prefix, installed_binary, current_subagent,
- sizeof(current_subagent)) == CLI_OK},
- };
- char hooks_dir[CLI_BUF_1K];
- int hooks_written = snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir);
- bool hooks_dir_valid = hooks_written > 0 && (size_t)hooks_written < sizeof(hooks_dir);
- for (size_t i = 0; i < sizeof(owned_scripts) / sizeof(owned_scripts[0]); i++) {
- char script_path[CLI_BUF_1K];
- int script_written = hooks_dir_valid
- ? snprintf(script_path, sizeof(script_path), "%s/%s",
- hooks_dir, owned_scripts[i].name)
- : CLI_ERR;
- bool script_path_valid =
- script_written > 0 && (size_t)script_written < sizeof(script_path);
- if (!owned_scripts[i].current_valid) {
- record_agent_config_error(true, "Claude Code", "hook_script_uninstall",
- owned_scripts[i].name);
- continue;
- }
- if (!script_path_valid ||
- cbm_remove_owned_hook_script(script_path, owned_scripts[i].current,
- owned_scripts[i].legacy,
- owned_scripts[i].legacy_count) < CLI_OK) {
- record_agent_config_error(true, "Claude Code", "hook_script_uninstall",
- script_path_valid ? script_path : owned_scripts[i].name);
- }
-#ifdef _WIN32
- if (!hooks_dir_valid ||
- cbm_remove_owned_legacy_hook_script(
- hooks_dir, owned_scripts[i].legacy_name, owned_scripts[i].current,
- owned_scripts[i].legacy, owned_scripts[i].legacy_count) != CLI_OK) {
- char legacy_path[CLI_BUF_1K];
- int written = hooks_dir_valid ? snprintf(legacy_path, sizeof(legacy_path), "%s/%s",
- hooks_dir, owned_scripts[i].legacy_name)
- : CLI_ERR;
- record_agent_config_error(true, "Claude Code", "legacy_hook_script_uninstall",
- written > 0 && (size_t)written < sizeof(legacy_path)
- ? legacy_path
- : owned_scripts[i].legacy_name);
- }
-#endif
- }
+ remove_claude_owned_hook_scripts(config_dir, installed_binary, true);
}
printf(" removed PreToolUse + SessionStart + SubagentStart hooks\n");
}
diff --git a/tests/test_cli.c b/tests/test_cli.c
index e5c113abb..b9790fdce 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -1176,11 +1176,23 @@ TEST(cli_install_hooks_preference_persists_and_migrates_owned_state) {
char settings_path[768];
char mcp_path[768];
char db_path[768];
+ char gate_path[768];
+ char session_path[768];
+ char subagent_path[768];
snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
snprintf(claude_dir, sizeof(claude_dir), "%s/.claude", tmpdir);
snprintf(settings_path, sizeof(settings_path), "%s/settings.json", claude_dir);
snprintf(mcp_path, sizeof(mcp_path), "%s/.claude.json", tmpdir);
snprintf(db_path, sizeof(db_path), "%s/_config.db", cache_dir);
+#ifdef _WIN32
+ snprintf(gate_path, sizeof(gate_path), "%s/hooks/cbm-code-discovery-gate.cmd", claude_dir);
+ snprintf(session_path, sizeof(session_path), "%s/hooks/cbm-session-reminder.cmd", claude_dir);
+ snprintf(subagent_path, sizeof(subagent_path), "%s/hooks/cbm-subagent-reminder.cmd", claude_dir);
+#else
+ snprintf(gate_path, sizeof(gate_path), "%s/hooks/cbm-code-discovery-gate", claude_dir);
+ snprintf(session_path, sizeof(session_path), "%s/hooks/cbm-session-reminder", claude_dir);
+ snprintf(subagent_path, sizeof(subagent_path), "%s/hooks/cbm-subagent-reminder", claude_dir);
+#endif
test_mkdirp(claude_dir);
write_test_file(settings_path,
"{\"hooks\":{\"PreToolUse\":[{\"matcher\":\"Bash\",\"hooks\":[{\"type\":"
@@ -1203,6 +1215,8 @@ TEST(cli_install_hooks_preference_persists_and_migrates_owned_state) {
char *default_state = read_test_file_alloc(settings_path);
bool default_hook = default_state && strstr(default_state, "cbm-code-discovery-gate") &&
strstr(default_state, "/usr/bin/foreign-hook");
+ bool default_scripts = cbm_file_exists(gate_path) && cbm_file_exists(session_path) &&
+ cbm_file_exists(subagent_path);
free(default_state);
char *no_hooks_plan_args[] = {"--plan", "--no-hooks"};
@@ -1238,6 +1252,8 @@ TEST(cli_install_hooks_preference_persists_and_migrates_owned_state) {
bool disabled_ok = disabled_state && strstr(disabled_state, "/usr/bin/foreign-hook") &&
!strstr(disabled_state, "cbm-code-discovery-gate") && disabled_mcp &&
strstr(disabled_mcp, "codebase-memory-mcp");
+ bool no_hooks_scripts_gone = !cbm_file_exists(gate_path) && !cbm_file_exists(session_path) &&
+ !cbm_file_exists(subagent_path);
free(disabled_state);
free(disabled_mcp);
@@ -1246,6 +1262,9 @@ TEST(cli_install_hooks_preference_persists_and_migrates_owned_state) {
disabled_state = read_test_file_alloc(settings_path);
bool bare_disabled = disabled_state && strstr(disabled_state, "/usr/bin/foreign-hook") &&
!strstr(disabled_state, "cbm-code-discovery-gate");
+ bool bare_scripts_gone =
+ !cbm_file_exists(gate_path) && !cbm_file_exists(session_path) &&
+ !cbm_file_exists(subagent_path);
free(disabled_state);
char *hooks_plan_args[] = {"--plan", "--hooks"};
@@ -1264,14 +1283,36 @@ TEST(cli_install_hooks_preference_persists_and_migrates_owned_state) {
char *enabled_state = read_test_file_alloc(settings_path);
bool enabled_ok = enabled_state && strstr(enabled_state, "cbm-code-discovery-gate") &&
strstr(enabled_state, "/usr/bin/foreign-hook");
+ bool hooks_scripts_back = cbm_file_exists(gate_path) && cbm_file_exists(session_path) &&
+ cbm_file_exists(subagent_path);
free(enabled_state);
int bare_enabled_rc = cli_test_cmd_install(3, bare_args);
+ bool bare_scripts_enabled = cbm_file_exists(gate_path) && cbm_file_exists(session_path) &&
+ cbm_file_exists(subagent_path);
+
+ const char *foreign_session_script =
+#ifdef _WIN32
+ "@echo off\r\nC:\\tmp\\user-owned-session-hook.cmd\r\n";
+#else
+ "#!/bin/sh\n/usr/bin/user-owned-session-hook\n";
+#endif
+ write_test_file(session_path, foreign_session_script);
+ int foreign_no_hooks_rc = cli_test_cmd_install(4, no_hooks_args);
+ char *preserved_session = read_test_file_alloc(session_path);
+ bool foreign_session_preserved =
+ foreign_no_hooks_rc == 0 && preserved_session &&
+ strcmp(preserved_session, foreign_session_script) == 0 && !cbm_file_exists(gate_path) &&
+ !cbm_file_exists(subagent_path);
+ free(preserved_session);
+
bool results_ok =
conflict_a_rc == 1 && conflict_b_rc == 1 && default_rc == 0 && default_hook &&
- no_hooks_plan_rc == 0 && disabled_plan_ok && no_hooks_dry_rc == 0 && previews_read_only &&
- no_hooks_rc == 0 && stored_false && disabled_ok && bare_disabled_rc == 0 && bare_disabled &&
+ default_scripts && no_hooks_plan_rc == 0 && disabled_plan_ok && no_hooks_dry_rc == 0 &&
+ previews_read_only && no_hooks_rc == 0 && stored_false && disabled_ok &&
+ no_hooks_scripts_gone && bare_disabled_rc == 0 && bare_disabled && bare_scripts_gone &&
hooks_plan_rc == 0 && hooks_dry_rc == 0 && previews_retained_false && hooks_rc == 0 &&
- stored_true && enabled_ok && bare_enabled_rc == 0 && fake.mutation_reserve_count > 0;
+ stored_true && enabled_ok && hooks_scripts_back && bare_enabled_rc == 0 &&
+ bare_scripts_enabled && foreign_session_preserved && fake.mutation_reserve_count > 0;
cbm_cli_set_activation_ops_for_test(NULL);
cbm_set_auto_answer_for_test(0);