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): do not treat a parent graph as a linked worktree

Nohj47c7a07a745c
diff --git a/src/cli/hook_augment.c b/src/cli/hook_augment.c
index a389e487f..b08d6ebd1 100644
--- a/src/cli/hook_augment.c
+++ b/src/cli/hook_augment.c
@@ -1490,9 +1490,22 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
owned_config = cbm_mcp_server_attach_runtime_config(srv);
}
const char *cwd = ha_normalized_cwd_with_server(root, srv, cwd_buffer, sizeof(cwd_buffer));
- char *project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;
- bool worktree_ignored = !project && srv && cwd && cbm_mcp_ignore_worktrees_enabled(srv) &&
- cbm_git_is_linked_worktree(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. 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. */
+ 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)) {
+ free(project);
+ project = NULL;
+ }
+ bool worktree_ignored = ignore_linked && !project;
if (owned_config) {
cbm_mcp_server_set_config(srv, NULL);
cbm_config_close(owned_config);
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 5d7441ec4..6297a64d0 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -10644,6 +10644,41 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
!strstr(ignored_out, "is indexed");
free(ignored_out);
+ /* A linked worktree nested inside the indexed checkout must not inherit
+ * that parent graph once ignore_worktrees is on. */
+ char insider[512];
+ snprintf(insider, sizeof(insider), "%s/inside-wt", maindir);
+ snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree add -q \"%s\" -b agl7inside", maindir,
+ insider);
+ bool inside_made = configured && system(cmd) == 0;
+ char inside_sub[600];
+ snprintf(inside_sub, sizeof(inside_sub), "%s/nested", insider);
+ bool inside_sub_made = inside_made && test_mkdirp(inside_sub) == 0;
+ snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
+ inside_sub);
+ char *inside_out = inside_sub_made ? cbm_hook_augment_lifecycle_json(input) : NULL;
+ bool inside_ignored = inside_out && strstr(inside_out, "ignore_worktrees is enabled") &&
+ strstr(inside_out, "do not run index_repository") &&
+ !strstr(inside_out, "is indexed") && !strstr(inside_out, main_name);
+ free(inside_out);
+
+ /* Indexing that nested worktree itself must still win over the parent. */
+ char *inside_name = inside_made ? cbm_project_name_from_path(insider) : NULL;
+ bool inside_stored = false;
+ if (inside_name) {
+ char db_inside[900];
+ snprintf(db_inside, sizeof(db_inside), "%s/%s.db", cache, inside_name);
+ cbm_store_t *si = cbm_store_open_path(db_inside);
+ inside_stored = si && cbm_store_upsert_project(si, inside_name, insider) == CBM_STORE_OK;
+ if (si)
+ cbm_store_close(si);
+ }
+ char *exact_out = inside_stored ? cbm_hook_augment_lifecycle_json(input) : NULL;
+ bool exact_kept = exact_out && inside_name && strstr(exact_out, inside_name) &&
+ strstr(exact_out, "is indexed") &&
+ !strstr(exact_out, "ignore_worktrees is enabled");
+ free(exact_out);
+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}",
maindir);
char *main_out = cbm_hook_augment_lifecycle_json(input);
@@ -10652,6 +10687,7 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
restore_test_env("CBM_CACHE_DIR", saved_cache);
free(main_name);
+ free(inside_name);
snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
maindir, wtdir);
(void)system(cmd);
@@ -10661,6 +10697,10 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
FAIL("unindexed worktree must get a deliberate no-match notice, never a silent 0 bytes");
if (!honored)
FAIL("ignore_worktrees in the runtime config must change the unindexed-worktree note");
+ if (!inside_ignored)
+ 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 (!main_ok)
FAIL("the indexed main checkout must still resolve in the same matrix");
PASS();