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.

Merge remote-tracking branch 'origin/main' into fix/1388-hook-conflict-notice

Martin Vogel856ec69f8475
diff --cc tests/test_cli.c
index cc0de91a2,30bb3a337..625ed83a3
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@@ -4670,32 -4706,107 +4706,133 @@@ TEST(cli_agent_reinstall_preserves_fore
PASS();
}
+/* Regression for #1388: a hook client blocked by a daemon BUILD CONFLICT must
+ * emit a stdout systemMessage. stdout is the only channel a hook caller sees,
+ * so the pre-fix stderr-only reporting was indistinguishable from "no matches"
+ * and produced silent skips for the whole session. The absent-daemon notice
+ * must stay distinct: it points at `daemon start`, which cannot heal a build
+ * conflict. Non-Claude dialects take no bare stdout JSON at all. */
+TEST(cli_hook_conflict_emits_stdout_notice_issue1388) {
+ const char *conflict = cbm_hook_admission_notice(CBM_HOOK_ADMISSION_BUILD_CONFLICT, NULL);
+ ASSERT_NOT_NULL(conflict);
+ ASSERT_NOT_NULL(strstr(conflict, "systemMessage"));
+ ASSERT_NOT_NULL(strstr(conflict, "different build"));
+ /* The actionable step: a conflicted daemon must be STOPPED, not started. */
+ ASSERT_NOT_NULL(strstr(conflict, "daemon stop"));
+
+ const char *absent = cbm_hook_admission_notice(CBM_HOOK_ADMISSION_DAEMON_ABSENT, NULL);
+ ASSERT_NOT_NULL(absent);
+ ASSERT_NOT_NULL(strstr(absent, "daemon start"));
+ /* Distinct diagnoses: the conflict notice must never claim no daemon runs. */
+ ASSERT_TRUE(strcmp(conflict, absent) != 0);
+ ASSERT_NULL(strstr(conflict, "no CBM daemon is running"));
+
+ /* Other dialects do not consume a bare stdout JSON object. */
+ ASSERT_NULL(cbm_hook_admission_notice(CBM_HOOK_ADMISSION_BUILD_CONFLICT, "codex"));
+ ASSERT_NULL(cbm_hook_admission_notice(CBM_HOOK_ADMISSION_DAEMON_ABSENT, "codex"));
+ PASS();
+}
+ #ifndef _WIN32
+ /* Regression for #1387: installing over an existing setup whose hook scripts
+ * are not byte-owned (manual install with a custom binary location, or a
+ * user-modified reminder) must NOT remove the existing, working hook entries
+ * from settings.json. The refused script rewrite is reported as an error and
+ * both the scripts and the entries survive byte-identically. */
+ TEST(cli_install_preserves_hook_entries_when_scripts_unowned_issue1387) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hooks-preserve-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("cbm_mkdtemp failed");
+
+ char hooks_dir[512];
+ snprintf(hooks_dir, sizeof(hooks_dir), "%s/.claude/hooks", tmpdir);
+ if (!cbm_mkdir_p(hooks_dir, 0755))
+ FAIL("mkdir hooks_dir failed");
+
+ /* Gate script written by a manual install: BIN outside the managed target,
+ * so its bytes match no current or released installer-owned shape. */
+ static const char unowned_gate[] =
+ "#!/usr/bin/env bash\n"
+ "# codebase-memory-mcp search augmenter (Claude Code PreToolUse).\n"
+ "BIN=\"/opt/tools/cbm/codebase-memory-mcp\"\n"
+ "[ -x \"$BIN\" ] || exit 0\n"
+ "\"$BIN\" hook-augment 2>/dev/null\n"
+ "exit 0\n";
+ char gate_path[768];
+ snprintf(gate_path, sizeof(gate_path), "%s/cbm-code-discovery-gate", hooks_dir);
+ write_test_file(gate_path, unowned_gate);
+
+ /* Session reminder carrying a user-added line. */
+ static const char unowned_session[] =
+ "#!/usr/bin/env bash\n"
+ "# SessionStart hook: remind agent to use codebase-memory-mcp tools.\n"
+ "echo my-extra-team-reminder\n";
+ char session_path[768];
+ snprintf(session_path, sizeof(session_path), "%s/cbm-session-reminder", hooks_dir);
+ write_test_file(session_path, unowned_session);
+
+ static const char settings_before[] =
+ "{\n"
+ " \"hooks\": {\n"
+ " \"PreToolUse\": [\n"
+ " {\"matcher\": \"Grep|Glob\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"~/.claude/hooks/cbm-code-discovery-gate\", \"timeout\": 5}]},\n"
+ " {\"matcher\": \"Bash\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"/usr/local/bin/my-own-guard\"}]}\n"
+ " ],\n"
+ " \"SessionStart\": [\n"
+ " {\"matcher\": \"startup\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"~/.claude/hooks/cbm-session-reminder\"}]},\n"
+ " {\"matcher\": \"resume\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"~/.claude/hooks/cbm-session-reminder\"}]},\n"
+ " {\"matcher\": \"clear\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"~/.claude/hooks/cbm-session-reminder\"}]},\n"
+ " {\"matcher\": \"compact\", \"hooks\": [{\"type\": \"command\", "
+ "\"command\": \"~/.claude/hooks/cbm-session-reminder\"}]}\n"
+ " ]\n"
+ " }\n"
+ "}\n";
+ char settings_path[768];
+ snprintf(settings_path, sizeof(settings_path), "%s/.claude/settings.json", tmpdir);
+ write_test_file(settings_path, settings_before);
+
+ const char *const env_names[] = {"HOME", "PATH", "CLAUDE_CONFIG_DIR"};
+ char *saved[sizeof(env_names) / sizeof(env_names[0])];
+ for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) {
+ saved[i] = save_test_env(env_names[i]);
+ cbm_unsetenv(env_names[i]);
+ }
+ cbm_setenv("HOME", tmpdir, 1);
+ cbm_setenv("PATH", tmpdir, 1);
+
+ int install_rc =
+ cbm_install_agent_configs(tmpdir, "/opt/other/codebase-memory-mcp", false, false);
+
+ char *settings = read_test_file_alloc(settings_path);
+ char *gate = read_test_file_alloc(gate_path);
+ char *session = read_test_file_alloc(session_path);
+ bool preserved =
+ install_rc != 0 /* the refused rewrites are reported, not silent */
+ && settings && strstr(settings, "~/.claude/hooks/cbm-code-discovery-gate") &&
+ strstr(settings, "Grep|Glob") && strstr(settings, "/usr/local/bin/my-own-guard") &&
+ strstr(settings, "\"startup\"") && strstr(settings, "\"resume\"") &&
+ strstr(settings, "\"clear\"") && strstr(settings, "\"compact\"") &&
+ strstr(settings, "~/.claude/hooks/cbm-session-reminder") && gate &&
+ strcmp(gate, unowned_gate) == 0 && session && strcmp(session, unowned_session) == 0;
+ free(settings);
+ free(gate);
+ free(session);
+
+ for (size_t i = 0U; i < sizeof(env_names) / sizeof(env_names[0]); i++) {
+ restore_test_env(env_names[i], saved[i]);
+ }
+ test_rmdir_r(tmpdir);
+ if (!preserved)
+ FAIL("install must preserve existing hook entries and scripts when the on-disk "
+ "scripts are unowned (#1387)");
+ PASS();
+ }
+ #endif
TEST(cli_existing_agents_install_durable_child_context) {
char tmpdir[256];
@@@ -11963,7 -12075,9 +12101,10 @@@ SUITE(cli)
RUN_TEST(cli_new_agent_install_plans_use_documented_paths);
RUN_TEST(cli_new_agent_configs_use_documented_schemas);
RUN_TEST(cli_agent_reinstall_preserves_foreign_policy_entries);
+ RUN_TEST(cli_hook_conflict_emits_stdout_notice_issue1388);
+ #ifndef _WIN32
+ RUN_TEST(cli_install_preserves_hook_entries_when_scripts_unowned_issue1387);
+ #endif
RUN_TEST(cli_existing_agents_install_durable_child_context);
RUN_TEST(cli_durable_profiles_follow_current_vendor_paths);
RUN_TEST(cli_cline_data_dir_only_redirects_data_state);