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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(cli): keep the hook default path untouched while ignore_worktrees is off
Martin Vogel77310d5f6f57
diff --git a/src/cli/cli.h b/src/cli/cli.hindex 4ac403387..6e0f85ff3 100644--- a/src/cli/cli.h+++ b/src/cli/cli.h@@ -264,6 +264,9 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,bool case_insensitive);const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,bool worktree_ignored);+/* Counts lifecycle calls that reached the ignore_worktrees resolution (cwd+ * canonicalization and the linked-worktree probe). NULL stops counting. */+void cbm_hook_set_worktree_gate_counter_for_testing(int *counter);bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);diff --git a/src/cli/hook_augment.c b/src/cli/hook_augment.cindex 384ba8467..61bc0100b 100644--- a/src/cli/hook_augment.c+++ b/src/cli/hook_augment.c@@ -17,6 +17,7 @@*/#include "cli/cli.h"+#include "foundation/compat.h"#include "foundation/compat_fs.h"#include "foundation/constants.h"#include "foundation/mem.h"@@ -1002,6 +1003,10 @@ static char *ha_resolve_indexed_project_with_root(cbm_mcp_server_t *srv, const creturn ha_registry_project_for_path(srv, cwd, root_out, root_out_size);}+static char *ha_resolve_indexed_project(cbm_mcp_server_t *srv, const char *cwd) {+ return ha_resolve_indexed_project_with_root(srv, cwd, NULL, 0U);+}+static const char *ha_hook_event_name(yyjson_val *root) {const char *event = ha_obj_str(root, "hook_event_name");return event ? event : ha_obj_str(root, "hookEventName");@@ -1459,6 +1464,41 @@ static bool ha_invocation_supported(ha_lifecycle_dialect_t dialect, const char *return !forced_event || ha_dialect_event_supported(dialect, forced_event);}+#ifdef CBM_CLI_ENABLE_TEST_API+static CBM_TLS int *g_ha_worktree_gate_counter = NULL;+#endif++/* Project resolution once ignore_worktrees is on; the caller runs this only+ * after reading the key, so the default path does none of it. A symlink alias+ * has no .git of its own, and walking its lexical parents leaves the linked+ * worktree. index_repository canonicalizes before the same gate, so the hook+ * must too or it tells the agent to index a path the setting will refuse. The+ * setting refuses a linked worktree even when it sits inside an indexed+ * checkout or inside another linked worktree, so a match is kept only when its+ * root belongs to this cwd's own linked worktree, not a parent graph. */+static char *ha_resolve_project_ignoring_worktrees(cbm_mcp_server_t *srv, const char *cwd,+ bool *drop_ancestor, bool *worktree_ignored) {+#ifdef CBM_CLI_ENABLE_TEST_API+ if (g_ha_worktree_gate_counter) {+ (*g_ha_worktree_gate_counter)++;+ }+#endif+ char canonical_cwd[4096];+ if (ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {+ cwd = canonical_cwd;+ }+ char project_root[4096];+ project_root[0] = '\0';+ char *project =+ ha_resolve_indexed_project_with_root(srv, cwd, project_root, sizeof(project_root));+ bool linked = cbm_git_is_linked_worktree(cwd);+ /* Drop a parent graph without a second free(): this file's raw free count+ * is ratcheted, and the caller's single free(project) still releases it. */+ *drop_ancestor = linked && project && !cbm_git_same_linked_worktree(cwd, project_root);+ *worktree_ignored = linked && (!project || *drop_ancestor);+ return project;+}+static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root,const char *forced_event, ha_lifecycle_dialect_t dialect) {if (!root || !yyjson_is_obj(root)) {@@ -1471,47 +1511,24 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *rootchar cwd_buffer[4096];cbm_mcp_server_t *owned_server = NULL;- cbm_config_t *owned_config = NULL;if (!srv) {owned_server = cbm_mcp_server_new(NULL);srv = owned_server;}- /* cbm_mcp_server_new(NULL) does not attach the runtime store. The production- * hook uses that constructor, so ignore_worktrees would stay at its default- * (off) and the "not indexed" note would still tell the agent to run- * index_repository in a linked worktree the setting refuses. Load the store- * only when the caller has not already set one, then drop it before return- * so a caller-owned server is not left pointing at a closed config. */- if (srv) {- owned_config = cbm_mcp_server_attach_runtime_config(srv);- }const char *cwd = ha_normalized_cwd_with_server(root, srv, cwd_buffer, sizeof(cwd_buffer));- /* A symlink alias has no .git of its own, and walking its lexical parents- * leaves the linked worktree. index_repository canonicalizes before the- * same gate, so the hook must too or it tells the agent to index a path- * the setting will refuse. */- char canonical_cwd[4096];- if (cwd && ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {- cwd = canonical_cwd;- }- char project_root[4096];- project_root[0] = '\0';- char *project = srv && cwd ? ha_resolve_indexed_project_with_root(srv, cwd, project_root,- sizeof(project_root))- : NULL;- /* ignore_worktrees refuses a linked worktree, including one nested inside an- * indexed checkout or inside another linked worktree. Keep a match only when- * its root belongs to this cwd's own linked worktree, not a parent graph. */- bool ignore_linked =- srv && cwd && cbm_mcp_ignore_worktrees_enabled(srv) && cbm_git_is_linked_worktree(cwd);- /* Drop a parent graph without a second free(): this file's raw free count- * is ratcheted, and the single free(project) below still releases it. */- bool drop_ancestor =- ignore_linked && project && !cbm_git_same_linked_worktree(cwd, project_root);- bool worktree_ignored = ignore_linked && (!project || drop_ancestor);- if (owned_config) {- cbm_mcp_server_set_config(srv, NULL);- cbm_config_close(owned_config);+ /* Read ignore_worktrees first and do nothing else for it while it is off.+ * cbm_mcp_server_new(NULL) has no store, and srv may be the daemon's+ * session server, so the read leaves srv untouched (see+ * cbm_mcp_ignore_worktrees_runtime_enabled). Off, this is the resolution+ * the hook did before the key existed. */+ bool drop_ancestor = false;+ bool worktree_ignored = false;+ char *project = NULL;+ if (srv && cwd && cbm_mcp_ignore_worktrees_runtime_enabled(srv)) {+ project =+ ha_resolve_project_ignoring_worktrees(srv, cwd, &drop_ancestor, &worktree_ignored);+ } else {+ project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;}cbm_mcp_server_free(owned_server);@@ -1670,6 +1687,10 @@ const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,return ha_no_project_index_guidance(event, worktree_ignored);}+void cbm_hook_set_worktree_gate_counter_for_testing(int *counter) {+ g_ha_worktree_gate_counter = counter;+}+bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz) {return ha_parse_bash_search_pattern(cmd, out, out_sz);}diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex c45ec6de8..00479171b 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -1849,17 +1849,6 @@ void cbm_mcp_server_set_config(cbm_mcp_server_t *srv, struct cbm_config *cfg) {}}-struct cbm_config *cbm_mcp_server_attach_runtime_config(cbm_mcp_server_t *srv) {- if (!srv || srv->config) {- return NULL;- }- cbm_config_t *config = cbm_config_open(cbm_resolve_cache_dir());- if (config) {- cbm_mcp_server_set_config(srv, config);- }- return config;-}-#ifdef CBM_ENABLE_TEST_SEAMSvoid cbm_mcp_server_set_auto_index_count_test_hook(cbm_mcp_server_t *srv,cbm_mcp_auto_index_count_test_hook_fn hook,@@ -17928,6 +17917,33 @@ bool cbm_mcp_ignore_worktrees_enabled(const cbm_mcp_server_t *srv) {return cbm_config_get_bool(srv->config, CBM_CONFIG_IGNORE_WORKTREES, false);}+/* The same key for a caller whose server may have no store: the stand-alone+ * lifecycle hook builds one with cbm_mcp_server_new(NULL), and the daemon lends+ * its session server, which this must not modify. A server with a store answers+ * from it. Otherwise the runtime store is opened only to read this key, and only+ * when it exists: a missing store leaves every key at its default, so this read+ * never creates one. */+bool cbm_mcp_ignore_worktrees_runtime_enabled(const cbm_mcp_server_t *srv) {+ if (srv && srv->config) {+ return cbm_mcp_ignore_worktrees_enabled(srv);+ }+ const char *cache_dir = cbm_resolve_cache_dir();+ if (!cache_dir) {+ return false;+ }+ char store_path[CBM_SZ_4K];+ int written = snprintf(store_path, sizeof(store_path), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);+ cbm_path_info_t info;+ if (written < 0 || (size_t)written >= sizeof(store_path) ||+ cbm_path_info_utf8(store_path, &info) != CBM_PATH_INFO_OK || !info.is_regular) {+ return false;+ }+ cbm_config_t *config = cbm_config_open(cache_dir);+ bool enabled = config && cbm_config_get_bool(config, CBM_CONFIG_IGNORE_WORKTREES, false);+ cbm_config_close(config);+ return enabled;+}+/* Register the session project with the background watcher for ongoing* change detection — unless auto_watch is disabled. */static void register_watcher_if_enabled(cbm_mcp_server_t *srv) {diff --git a/src/mcp/mcp.h b/src/mcp/mcp.hindex f2fe09b26..31ffd69b0 100644--- a/src/mcp/mcp.h+++ b/src/mcp/mcp.h@@ -182,12 +182,6 @@ void cbm_mcp_server_set_watcher(cbm_mcp_server_t *srv, struct cbm_watcher *w);/* Set external config store reference (for auto_index setting). Not owned. */void cbm_mcp_server_set_config(cbm_mcp_server_t *srv, struct cbm_config *cfg);-/* Open the runtime config and attach it when srv has none. The returned store- * is owned by the caller, who must clear it with cbm_mcp_server_set_config- * before closing. Returns NULL when a config is already set or the store- * cannot be opened. */-struct cbm_config *cbm_mcp_server_attach_runtime_config(cbm_mcp_server_t *srv);-/* Set an explicit session context for an embedded/daemon-backed server.* session_root is copied and its project name is derived using the same naming* rule as indexing. allowed_root is copied when non-NULL; an explicit NULLdiff --git a/src/mcp/mcp_internal.h b/src/mcp/mcp_internal.hindex 8e78f9f40..fc4f37cb4 100644--- a/src/mcp/mcp_internal.h+++ b/src/mcp/mcp_internal.h@@ -68,6 +68,11 @@ bool cbm_mcp_auto_index_within_file_limit(const char *root_path, int file_limit,* automatic index of a linked worktree should be skipped. */bool cbm_mcp_ignore_worktrees_enabled(const cbm_mcp_server_t *srv);+/* The same key when srv may have no store (the lifecycle hook). Never attaches+ * a store to srv and never creates the runtime store; reads it only when it+ * already exists. */+bool cbm_mcp_ignore_worktrees_runtime_enabled(const cbm_mcp_server_t *srv);+/* detect_changes seed scoping (#1363): does `node`'s line range overlap any* recorded hunk for `file`? Exposed for direct unit testing of the overlap* logic, independent of the git/subprocess/index plumbing around it. */diff --git a/tests/test_cli.c b/tests/test_cli.cindex af140c53f..586b78884 100644--- a/tests/test_cli.c+++ b/tests/test_cli.c@@ -26,6 +26,7 @@#include <foundation/platform.h>#include <foundation/private_file_lock.h>#include <foundation/sha256.h>+#include <foundation/str_util.h>#include <mcp/mcp.h>#include <mcp/index_supervisor.h>#include <pipeline/pipeline.h>@@ -10944,6 +10945,96 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {FAIL("the indexed main checkout must still resolve in the same matrix");PASS();}++/* With ignore_worktrees unset, the lifecycle hook must do what it did before+ * the key existed. It may read the key, but it must not create a runtime+ * config store just to find the key unset, and it must not canonicalize the+ * cwd or probe for a linked worktree. Turning the key on is the positive+ * control: the same call then reaches the worktree gate exactly once. */+TEST(cli_hook_ignore_worktrees_default_off_does_no_worktree_work) {+ char tmpdir[256];+ char *raw = th_mktempdir("cbm_hook_wt_off");+ if (!raw)+ FAIL("th_mktempdir returned NULL");+ snprintf(tmpdir, sizeof(tmpdir), "%s", raw);+ char cache[512];+ char maindir[512];+ char wtdir[512];+ char store_path[600];+ snprintf(cache, sizeof(cache), "%s/cache", tmpdir);+ snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);+ snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);+ snprintf(store_path, sizeof(store_path), "%s/" CBM_CONFIG_DB_FILENAME, cache);+ test_mkdirp(cache);+ test_mkdirp(maindir);++ char cmd[2048];+ snprintf(cmd, sizeof(cmd),+ "cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "+ ": > f.txt && git add f.txt && git commit -qm init && "+ "git worktree add -q \"%s\" -b wtoff",+ maindir, wtdir);+ if (system(cmd) != 0) {+ th_rmtree(tmpdir);+ SKIP_PLATFORM("git worktree add unavailable");+ }++ char *saved_cache = save_test_env("CBM_CACHE_DIR");+ cbm_setenv("CBM_CACHE_DIR", cache, 1);++ char input[1024];+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);+ struct stat st;+ int gate_calls = 0;+ cbm_hook_set_worktree_gate_counter_for_testing(&gate_calls);+ bool store_absent_before = stat(store_path, &st) != 0;+ char *off_out = cbm_hook_augment_lifecycle_json(input);+ bool off_note = off_out && strstr(off_out, "Run index_repository") &&+ !strstr(off_out, "ignore_worktrees is enabled");+ free(off_out);+ bool store_absent_after = stat(store_path, &st) != 0;+ int off_gate_calls = gate_calls;++ /* A store that leaves the key unset may be read for the key, nothing more. */+ cbm_config_t *cfg = cbm_config_open(cache);+ bool other_key_set = cfg && cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, "true") == 0;+ cbm_config_close(cfg);+ gate_calls = 0;+ char *unset_out = other_key_set ? cbm_hook_augment_lifecycle_json(input) : NULL;+ bool unset_note = unset_out && strstr(unset_out, "Run index_repository") &&+ !strstr(unset_out, "ignore_worktrees is enabled");+ free(unset_out);+ int unset_gate_calls = gate_calls;++ cfg = cbm_config_open(cache);+ bool enabled = cfg && cbm_config_set(cfg, CBM_CONFIG_IGNORE_WORKTREES, "true") == 0;+ cbm_config_close(cfg);+ gate_calls = 0;+ char *on_out = enabled ? cbm_hook_augment_lifecycle_json(input) : NULL;+ bool on_note = on_out && strstr(on_out, "ignore_worktrees is enabled");+ free(on_out);+ int on_gate_calls = gate_calls;+ cbm_hook_set_worktree_gate_counter_for_testing(NULL);++ restore_test_env("CBM_CACHE_DIR", saved_cache);+ snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",+ maindir, wtdir);+ (void)system(cmd);+ th_rmtree(tmpdir);++ ASSERT_TRUE(store_absent_before);+ ASSERT_TRUE(off_note);+ if (!store_absent_after)+ FAIL("a default-off lifecycle event must not create the runtime config store");+ ASSERT_EQ(off_gate_calls, 0);+ ASSERT_TRUE(other_key_set);+ ASSERT_TRUE(unset_note);+ ASSERT_EQ(unset_gate_calls, 0);+ ASSERT_TRUE(enabled);+ ASSERT_TRUE(on_note);+ ASSERT_EQ(on_gate_calls, 1);+ PASS();+}#endifTEST(cli_hook_session_sanitizes_untrusted_project_metadata) {@@ -16975,6 +17066,7 @@ SUITE(cli) {#ifndef _WIN32RUN_TEST(cli_hook_worktree_cwd_resolves_own_indexed_project);RUN_TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent);+ RUN_TEST(cli_hook_ignore_worktrees_default_off_does_no_worktree_work);#endifRUN_TEST(cli_hook_session_sanitizes_untrusted_project_metadata);RUN_TEST(cli_hook_ownership_requires_exact_command_identity);diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex 8eab275a2..257e799ad 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -10,6 +10,7 @@#include "../src/foundation/log.h"#include "../src/foundation/platform.h" /* cbm_file_size */#include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */+#include "../src/foundation/str_util.h"#include "../src/foundation/subprocess.h"#include "../src/foundation/workspace.h"#include "../src/mcp/compact_out.h"@@ -1202,37 +1203,53 @@ TEST(mcp_initialize_response) {PASS();}-TEST(mcp_attach_runtime_config_respects_caller_ownership) {- char *cache = th_mktempdir("cbm_mcp_runtime_config");- ASSERT_NOT_NULL(cache);+/* The lifecycle hook reads ignore_worktrees through this reader. It must never+ * attach a store to the server (the daemon lends its session server) and never+ * create the runtime store; a server with its own store answers from it. */+TEST(mcp_ignore_worktrees_runtime_read_leaves_server_and_store_alone) {+ char *raw = th_mktempdir("cbm_mcp_runtime_config");+ ASSERT_NOT_NULL(raw);+ char cache[256];+ snprintf(cache, sizeof(cache), "%s", raw);+ char own_dir[320];+ snprintf(own_dir, sizeof(own_dir), "%s/own", cache);+ char store_path[400];+ snprintf(store_path, sizeof(store_path), "%s/" CBM_CONFIG_DB_FILENAME, cache);const char *saved = getenv("CBM_CACHE_DIR");char *saved_copy = saved ? strdup(saved) : NULL;cbm_setenv("CBM_CACHE_DIR", cache, 1);- cbm_config_t *config = cbm_config_open(cache);cbm_mcp_server_t *server = cbm_mcp_server_new(NULL);- bool stored = config && cbm_config_set(config, CBM_CONFIG_IGNORE_WORKTREES, "true") == 0;- bool default_off = server && !cbm_mcp_ignore_worktrees_enabled(server);- cbm_config_t *attached = server ? cbm_mcp_server_attach_runtime_config(server) : NULL;- bool enabled = attached && cbm_mcp_ignore_worktrees_enabled(server);- bool not_replaced = server && cbm_mcp_server_attach_runtime_config(server) == NULL;+ struct stat st;+ bool absent_off = server && !cbm_mcp_ignore_worktrees_runtime_enabled(server);+ bool still_absent = stat(store_path, &st) != 0;++ cbm_config_t *runtime = cbm_config_open(cache);+ bool stored = runtime && cbm_config_set(runtime, CBM_CONFIG_IGNORE_WORKTREES, "true") == 0;+ cbm_config_close(runtime);+ bool runtime_on = server && cbm_mcp_ignore_worktrees_runtime_enabled(server);+ bool server_untouched = server && !cbm_mcp_ignore_worktrees_enabled(server);++ cbm_config_t *own = cbm_config_open(own_dir);+ if (server && own) {+ cbm_mcp_server_set_config(server, own);+ }+ bool own_store_wins = server && own && !cbm_mcp_ignore_worktrees_runtime_enabled(server);if (server) {- cbm_mcp_server_set_config(server, config);+ cbm_mcp_server_set_config(server, NULL);}- cbm_config_close(attached);- bool caller_config_kept = server && cbm_mcp_server_attach_runtime_config(server) == NULL &&- cbm_mcp_ignore_worktrees_enabled(server);cbm_mcp_server_free(server);- cbm_config_close(config);+ cbm_config_close(own);restore_cache_dir(saved_copy);free(saved_copy);int removed = th_rmtree(cache);+ ASSERT(absent_off);+ ASSERT(still_absent);ASSERT(stored);- ASSERT(default_off);- ASSERT(enabled);- ASSERT(not_replaced);- ASSERT(caller_config_kept);+ ASSERT(runtime_on);+ ASSERT(server_untouched);+ ASSERT(own_store_wins);ASSERT_EQ(removed, 0);PASS();}@@ -21182,7 +21199,7 @@ SUITE(mcp) {/* MCP protocol helpers */RUN_TEST(mcp_initialize_response);- RUN_TEST(mcp_attach_runtime_config_respects_caller_ownership);+ RUN_TEST(mcp_ignore_worktrees_runtime_read_leaves_server_and_store_alone);RUN_TEST(mcp_tools_list);RUN_TEST(mcp_tools_help_list_matches_registry);RUN_TEST(mcp_tools_list_latest_metadata);