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 a worktree graph only for that same worktree
Nohj51a18c7b2772
diff --git a/src/cli/hook_augment.c b/src/cli/hook_augment.cindex b08d6ebd1..d3baace47 100644--- a/src/cli/hook_augment.c+++ b/src/cli/hook_augment.c@@ -1496,12 +1496,11 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *rootsizeof(project_root)): NULL;/* ignore_worktrees refuses a linked worktree, including one nested inside an- * indexed checkout. The ancestor walk would otherwise report that parent- * graph as this cwd's project. An index whose own root is the linked- * worktree (or inside it) still counts. */+ * 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);- if (ignore_linked && project && !cbm_git_is_linked_worktree(project_root)) {+ if (ignore_linked && project && !cbm_git_same_linked_worktree(cwd, project_root)) {free(project);project = NULL;}diff --git a/src/git/git_context.c b/src/git/git_context.cindex 3fbf5b65d..8e2629cc4 100644--- a/src/git/git_context.c+++ b/src/git/git_context.c@@ -211,7 +211,8 @@ static bool parent_directory(char *path) {return true;}-bool cbm_git_is_linked_worktree(const char *path) {+/* On a linked-worktree hit, optionally copy that anchor directory into root_out. */+static bool walk_linked_worktree_root(const char *path, char *root_out, size_t root_out_size) {if (!path || !path[0]) {return false;}@@ -230,7 +231,16 @@ bool cbm_git_is_linked_worktree(const char *path) {for (int depth = 0; depth < 64; depth++) {int kind = linked_worktree_anchor(current);if (kind >= 0) {- return kind == 1;+ if (kind != 1) {+ return false;+ }+ if (root_out && root_out_size > 0U) {+ int written = snprintf(root_out, root_out_size, "%s", current);+ if (written < 0 || (size_t)written >= root_out_size) {+ return false;+ }+ }+ return true;}char previous[GIT_OUTPUT_MAX];snprintf(previous, sizeof(previous), "%s", current);@@ -241,6 +251,29 @@ bool cbm_git_is_linked_worktree(const char *path) {return false;}+bool cbm_git_is_linked_worktree(const char *path) {+ return walk_linked_worktree_root(path, NULL, 0U);+}++bool cbm_git_same_linked_worktree(const char *a, const char *b) {+ char root_a[GIT_OUTPUT_MAX];+ char root_b[GIT_OUTPUT_MAX];+ if (!walk_linked_worktree_root(a, root_a, sizeof(root_a)) ||+ !walk_linked_worktree_root(b, root_b, sizeof(root_b))) {+ return false;+ }+ /* The gitdir file is unique per linked worktree. Comparing those pointers+ * keeps a nested worktree from matching its parent even when both are+ * linked checkouts. */+ char git_a[GIT_OUTPUT_MAX];+ char git_b[GIT_OUTPUT_MAX];+ if (!read_gitlink_target(root_a, git_a, sizeof(git_a)) ||+ !read_gitlink_target(root_b, git_b, sizeof(git_b))) {+ return false;+ }+ return strcmp(git_a, git_b) == 0;+}+static char *join_root_relative(const char *root, const char *rel) {if (!root || !root[0]) {return git_strdup(rel);diff --git a/src/git/git_context.h b/src/git/git_context.hindex 30f2f545a..1b9d174f1 100644--- a/src/git/git_context.h+++ b/src/git/git_context.h@@ -32,6 +32,12 @@ typedef struct {* cbm_git_context_resolve() shells out to git and is not usable on that path. */bool cbm_git_is_linked_worktree(const char *path);+/* True when both paths sit in the same linked worktree. Identity is the+ * gitdir pointer of each path's linked-worktree anchor, so a worktree nested+ * inside another does not count as that parent. False when either path is not+ * inside a linked worktree. */+bool cbm_git_same_linked_worktree(const char *a, const char *b);+int cbm_git_context_resolve(const char *path, cbm_git_context_t *out);void cbm_git_context_free(cbm_git_context_t *ctx);char *cbm_git_context_branch_qn(const char *project_name, const cbm_git_context_t *ctx);diff --git a/tests/test_cli.c b/tests/test_cli.cindex 6297a64d0..e234d77df 100644--- a/tests/test_cli.c+++ b/tests/test_cli.c@@ -10679,6 +10679,25 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {!strstr(exact_out, "ignore_worktrees is enabled");free(exact_out);+ /* An unindexed worktree nested inside that indexed worktree must not+ * inherit the outer worktree's graph. */+ char child[600];+ snprintf(child, sizeof(child), "%s/child-wt", insider);+ snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree add -q \"%s\" -b agl7child", maindir,+ child);+ bool child_made = inside_stored && system(cmd) == 0;+ char child_sub[700];+ snprintf(child_sub, sizeof(child_sub), "%s/sub", child);+ bool child_sub_made = child_made && test_mkdirp(child_sub) == 0;+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",+ child_sub);+ char *child_out = child_sub_made ? cbm_hook_augment_lifecycle_json(input) : NULL;+ bool child_ignored = child_out && inside_name &&+ strstr(child_out, "ignore_worktrees is enabled") &&+ strstr(child_out, "do not run index_repository") &&+ !strstr(child_out, "is indexed") && !strstr(child_out, inside_name);+ free(child_out);+snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",maindir);char *main_out = cbm_hook_augment_lifecycle_json(input);@@ -10701,6 +10720,8 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {FAIL("a nested linked worktree must not inherit the indexed parent graph");if (!exact_kept)FAIL("an exact index of the nested worktree must still be reported");+ if (!child_ignored)+ FAIL("a worktree nested inside an indexed worktree must not inherit that graph");if (!main_ok)FAIL("the indexed main checkout must still resolve in the same matrix");PASS();diff --git a/tests/test_git_context.c b/tests/test_git_context.cindex 5b3c4b779..10cab0009 100644--- a/tests/test_git_context.c+++ b/tests/test_git_context.c@@ -309,6 +309,19 @@ TEST(is_linked_worktree_true_for_linked_worktree) {snprintf(main_nested, sizeof(main_nested), "%s/src", main_tmp);bool main_nested_ok = th_mkdir_p(main_nested) == 0;bool main_nested_detected = main_nested_ok && cbm_git_is_linked_worktree(main_nested);+ /* A worktree nested inside another must not share the parent's anchor. */+ char inner[1024];+ snprintf(inner, sizeof(inner), "%s/inner-wt", wt_tmp);+ char inner_cmd[1200];+ snprintf(inner_cmd, sizeof(inner_cmd), "worktree add -b inner-branch \"%s\"", inner);+ bool inner_added = git_run(main_tmp, inner_cmd) == 0;+ char inner_sub[1200];+ snprintf(inner_sub, sizeof(inner_sub), "%s/sub", inner);+ bool inner_sub_ok = inner_added && th_mkdir_p(inner_sub) == 0;+ bool same_outer = cbm_git_same_linked_worktree(nested, wt_tmp);+ bool same_inner = inner_sub_ok && cbm_git_same_linked_worktree(inner_sub, inner);+ bool not_parent = inner_sub_ok && !cbm_git_same_linked_worktree(inner_sub, wt_tmp);+ bool not_main = !cbm_git_same_linked_worktree(nested, main_tmp);git_run(main_tmp, "worktree prune");th_rmtree(main_tmp);@@ -320,6 +333,11 @@ TEST(is_linked_worktree_true_for_linked_worktree) {ASSERT(!main_detected);ASSERT(main_nested_ok);ASSERT(!main_nested_detected);+ ASSERT(inner_added);+ ASSERT(same_outer);+ ASSERT(same_inner);+ ASSERT(not_parent);+ ASSERT(not_main);PASS();#endif /* _WIN32 */}