Skip to content

Commit 7648f9d

Browse files
committed
feat(config): opt-in worktree_shared_index — one index per repository across worktrees
For workflows where the main checkout is only the repository's origin and is never kept current (every branch lives in its own linked worktree), a family view of the main checkout is stale by construction. Add an opt-in switch, `worktree_shared_index` (default false, also CBM_WORKTREE_SHARED_INDEX=1 so supervised index workers inherit it): cbm_project_name_from_path() then names a linked worktree after its main checkout, so every worktree of a repository resolves to ONE index. Index once in any worktree; sessions in sibling worktrees derive the same project and are "already indexed"; index_repository from another worktree relinks root_path, so the index follows the last worktree that indexed it and the watcher keeps it in sync from the active session's worktree. Off by default: nothing changes for existing users. Refs DeusData#573 Signed-off-by: sinterqlaas2 <[email protected]>
1 parent 01b19ac commit 7648f9d

8 files changed

Lines changed: 247 additions & 0 deletions

File tree

‎docs/CONFIGURATION.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Current keys:
8888
| `auto_index` | `false` | Automatically index new projects when an MCP session starts. |
8989
| `auto_index_limit` | `50000` | Maximum file count allowed for automatic indexing of a new project. |
9090
| `auto_watch` | `true` | Register the session's project with the background git watcher on connect. Set `false` to keep a session from registering its project (the watcher still runs for other projects). |
91+
| `worktree_shared_index` | `false` | Name linked git worktrees after their main checkout, so every worktree of a repository resolves to **one** index: index once in any worktree, and sessions in sibling worktrees find it. The index follows whichever worktree last indexed it (the watcher keeps it in sync from the active session's worktree), so results reflect that worktree's branch. Off by default: each worktree keeps its own path-derived project. When off, an un-indexed worktree still resolves to its main checkout's index if that one is indexed. Also honoured as the `CBM_WORKTREE_SHARED_INDEX=1` environment variable. |
9192
| `watcher_enabled` | `true` | Master switch for the background watcher subsystem. Set `false` to stop the watcher from starting at all — no poll thread and no project registration. Reindex manually with `index_repository` when disabled. |
9293

9394
> **`watcher_enabled` vs `auto_watch`.** `watcher_enabled` controls whether the

‎src/cli/cli.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,6 +6841,8 @@ static const config_key_def_t CONFIG_KEYS[] = {
68416841
{CBM_CONFIG_AUTO_WATCH, "true", "Register background git watcher on session connect"},
68426842
{CBM_CONFIG_WATCHER_ENABLED, "true",
68436843
"Run the background watcher thread (auto-reindex); false to disable"},
6844+
{CBM_CONFIG_WORKTREE_SHARED_INDEX, "false",
6845+
"Name linked git worktrees after their main checkout so all worktrees share one index"},
68446846
{CBM_CONFIG_UI_LANG, "auto", "Pin graph UI language: en, zh, or auto"},
68456847
{CBM_CONFIG_UI_ENABLED, "false", "Serve the graph UI on a loopback HTTP port"},
68466848
{CBM_CONFIG_UI_PORT, "9749", "Port for the graph UI listener when enabled"},

‎src/cli/cli.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ int cbm_config_delete(cbm_config_t *cfg, const char *key);
425425
#define CBM_CONFIG_AUTO_WATCH "auto_watch"
426426
#define CBM_CONFIG_UI_LANG "ui-lang"
427427
#define CBM_CONFIG_WATCHER_ENABLED "watcher_enabled"
428+
/* #573: name linked git worktrees after their main checkout so every worktree
429+
* of a repository resolves to ONE index (opt-in; default keeps per-path names). */
430+
#define CBM_CONFIG_WORKTREE_SHARED_INDEX "worktree_shared_index"
428431
/* #1558: the graph UI's loopback listener. Stored in the UI config file rather
429432
* than the key-value store, but surfaced through `config` so it is findable. */
430433
#define CBM_CONFIG_UI_ENABLED "ui_enabled"

‎src/mcp/mcp.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,11 @@ void cbm_mcp_server_set_config(cbm_mcp_server_t *srv, struct cbm_config *cfg) {
17251725
if (srv) {
17261726
srv->config = cfg;
17271727
}
1728+
/* Project naming is process-wide, so the opt-in worktree sharing switch
1729+
* (#573) is applied here, where every daemon session receives its config. */
1730+
if (cfg && cbm_config_get_bool(cfg, CBM_CONFIG_WORKTREE_SHARED_INDEX, false)) {
1731+
cbm_project_name_share_worktrees(true);
1732+
}
17281733
}
17291734

17301735
#ifdef CBM_ENABLE_TEST_SEAMS

‎src/pipeline/fqn.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* Handles Python __init__.py, JS/TS index.{js,ts}, path separators.
66
*/
77
#include "pipeline/pipeline.h"
8+
#include "foundation/compat.h"
89
#include "foundation/compat_fs.h"
910
#include "foundation/constants.h"
1011
#include "foundation/platform.h"
12+
#include "git/git_context.h"
1113

1214
#include <stdbool.h>
1315
#include <stddef.h> // NULL
@@ -413,6 +415,42 @@ static bool path_is_root_syntax(const char *path) {
413415
return true;
414416
}
415417

418+
typedef enum {
419+
SHARE_WORKTREES_UNSET, /* no explicit switch: environment decides */
420+
SHARE_WORKTREES_OFF,
421+
SHARE_WORKTREES_ON,
422+
} share_worktrees_t;
423+
424+
static share_worktrees_t g_share_worktrees = SHARE_WORKTREES_UNSET;
425+
426+
void cbm_project_name_share_worktrees(bool enabled) {
427+
g_share_worktrees = enabled ? SHARE_WORKTREES_ON : SHARE_WORKTREES_OFF;
428+
if (enabled) {
429+
const int overwrite = 1;
430+
cbm_setenv("CBM_WORKTREE_SHARED_INDEX", "1", overwrite);
431+
}
432+
}
433+
434+
bool cbm_project_name_shares_worktrees(void) {
435+
if (g_share_worktrees == SHARE_WORKTREES_ON) {
436+
return true;
437+
}
438+
const char *env = getenv("CBM_WORKTREE_SHARED_INDEX");
439+
return env && (strcmp(env, "1") == 0 || strcmp(env, "true") == 0);
440+
}
441+
442+
/* Linked-worktree sharing (#573, opt-in): every worktree of a repository is
443+
* named after its main checkout, so they all resolve to ONE index. Returns the
444+
* path to derive the name from — `buf` when redirected, else `name_path`. */
445+
static const char *shared_worktree_name_path(const char *name_path, char *buf, size_t buf_sz) {
446+
if (cbm_project_name_shares_worktrees() &&
447+
cbm_git_linked_worktree_canonical_root(name_path, buf, buf_sz)) {
448+
cbm_normalize_path_sep(buf);
449+
return buf;
450+
}
451+
return name_path;
452+
}
453+
416454
char *cbm_project_name_from_path(const char *abs_path) {
417455
if (!abs_path || !abs_path[0]) {
418456
return strdup("root");
@@ -429,6 +467,8 @@ char *cbm_project_name_from_path(const char *abs_path) {
429467
cbm_normalize_path_sep(real);
430468
name_path = real;
431469
}
470+
char shared[CBM_SZ_4K];
471+
name_path = shared_worktree_name_path(name_path, shared, sizeof(shared));
432472

433473
/* Work on mutable copy */
434474
char *path = strdup(name_path);

‎src/pipeline/pipeline.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ char *cbm_pipeline_resolve_relative_import(const char *source_rel, const char *m
192192
* Caller must free() the returned string. */
193193
char *cbm_project_name_from_path(const char *abs_path);
194194

195+
/* #573 opt-in worktree sharing: when enabled, cbm_project_name_from_path()
196+
* names a LINKED git worktree after its main checkout, so every worktree of a
197+
* repository resolves to one index (the index follows whichever worktree last
198+
* indexed it). Process-wide; also exported to CBM_WORKTREE_SHARED_INDEX so
199+
* supervised index workers inherit it. Off by default. */
200+
void cbm_project_name_share_worktrees(bool enabled);
201+
bool cbm_project_name_shares_worktrees(void);
202+
195203
/* ── Function Registry ──────────────────────────────────────────── */
196204

197205
typedef struct cbm_registry cbm_registry_t;

‎tests/test_fqn.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* cbm_pipeline_fqn_folder, cbm_project_name_from_path.
66
*/
77
#include "test_framework.h"
8+
#include "test_helpers.h"
9+
#include "../src/foundation/compat.h"
810
#include "../src/pipeline/pipeline.h"
911
#include "../src/foundation/str_util.h"
1012

@@ -655,7 +657,75 @@ TEST(project_name_length_capped_issue624) {
655657
* Suite
656658
* ================================================================ */
657659

660+
/* #573 opt-in worktree sharing: with the switch on, a linked worktree derives
661+
* its MAIN checkout's project name, so every worktree of a repository resolves
662+
* to one index. With the switch off (the default) the worktree keeps its own
663+
* path-derived name, and the main checkout is unaffected either way. */
664+
TEST(project_name_shares_linked_worktree_when_enabled) {
665+
#ifdef _WIN32
666+
SKIP_PLATFORM("git worktree fixture not implemented for Windows");
667+
#else
668+
char main_dir[256];
669+
char wt_dir[256];
670+
char *raw = th_mktempdir("cbm_fqn_famain");
671+
if (!raw)
672+
FAIL("th_mktempdir returned NULL");
673+
snprintf(main_dir, sizeof(main_dir), "%s", raw);
674+
raw = th_mktempdir("cbm_fqn_fawt");
675+
if (!raw)
676+
FAIL("th_mktempdir returned NULL");
677+
snprintf(wt_dir, sizeof(wt_dir), "%s", raw);
678+
th_rmtree(wt_dir); /* git worktree add creates it */
679+
char keep[600];
680+
snprintf(keep, sizeof(keep), "%s/.keep", main_dir);
681+
ASSERT_EQ(th_write_file(keep, ""), 0);
682+
char wt_step[700];
683+
snprintf(wt_step, sizeof(wt_step), "worktree add -q \"%s\" -b fam-branch", wt_dir);
684+
const char *steps[] = {"init -q", "add -A", "commit -q -m init", wt_step};
685+
for (size_t s = 0; s < sizeof(steps) / sizeof(steps[0]); s++) {
686+
char cmd[1600];
687+
snprintf(cmd, sizeof(cmd),
688+
"git -C \"%s\" -c user.name=t -c [email protected] -c commit.gpgsign=false %s "
689+
">/dev/null 2>&1",
690+
main_dir, steps[s]);
691+
if (system(cmd) != 0) {
692+
th_rmtree(wt_dir);
693+
th_rmtree(main_dir);
694+
SKIP_PLATFORM("git not available for a worktree fixture");
695+
}
696+
}
697+
698+
char *main_name = cbm_project_name_from_path(main_dir);
699+
char *wt_name_off = cbm_project_name_from_path(wt_dir);
700+
cbm_project_name_share_worktrees(true);
701+
char *wt_name_on = cbm_project_name_from_path(wt_dir);
702+
char *main_name_on = cbm_project_name_from_path(main_dir);
703+
cbm_project_name_share_worktrees(false);
704+
cbm_unsetenv("CBM_WORKTREE_SHARED_INDEX");
705+
706+
ASSERT_NOT_NULL(main_name);
707+
ASSERT_NOT_NULL(wt_name_off);
708+
ASSERT_NOT_NULL(wt_name_on);
709+
ASSERT_NOT_NULL(main_name_on);
710+
ASSERT(strcmp(wt_name_off, main_name) != 0); /* default: own path-derived name */
711+
ASSERT_STR_EQ(wt_name_on, main_name); /* shared: the main checkout's name */
712+
ASSERT_STR_EQ(main_name_on, main_name); /* main checkout unaffected */
713+
714+
free(main_name);
715+
free(wt_name_off);
716+
free(wt_name_on);
717+
free(main_name_on);
718+
char prune[700];
719+
snprintf(prune, sizeof(prune), "git -C \"%s\" worktree prune >/dev/null 2>&1", main_dir);
720+
(void)system(prune);
721+
th_rmtree(wt_dir);
722+
th_rmtree(main_dir);
723+
PASS();
724+
#endif /* _WIN32 */
725+
}
726+
658727
SUITE(fqn) {
728+
RUN_TEST(project_name_shares_linked_worktree_when_enabled);
659729
/* fqn_compute: basic extensions */
660730
RUN_TEST(fqn_compute_basic_go);
661731
RUN_TEST(fqn_compute_basic_py);

‎tests/test_mcp.c‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4221,6 +4221,123 @@ TEST(session_worktree_family_resolves_to_indexed_canonical) {
42214221
#endif /* _WIN32 */
42224222
}
42234223

4224+
/* #573 opt-in worktree sharing, end to end: index_repository from worktree A
4225+
* lands under the main checkout's project name; a session opened in sibling
4226+
* worktree B derives that same name (so it is "already indexed", not a miss,
4227+
* and no family fallback is needed); and index_repository from B relinks the
4228+
* project's root_path to B — the index follows the last worktree that indexed. */
4229+
TEST(worktree_shared_index_spans_sibling_worktrees) {
4230+
#ifdef _WIN32
4231+
SKIP_PLATFORM("git worktree fixture not implemented for Windows");
4232+
#else
4233+
char main_dir[256];
4234+
char wt_a[256];
4235+
char wt_b[256];
4236+
char *raw = th_mktempdir("cbm_mcp_shmain");
4237+
if (!raw)
4238+
FAIL("th_mktempdir returned NULL");
4239+
snprintf(main_dir, sizeof(main_dir), "%s", raw);
4240+
raw = th_mktempdir("cbm_mcp_shwta");
4241+
if (!raw)
4242+
FAIL("th_mktempdir returned NULL");
4243+
snprintf(wt_a, sizeof(wt_a), "%s", raw);
4244+
raw = th_mktempdir("cbm_mcp_shwtb");
4245+
if (!raw)
4246+
FAIL("th_mktempdir returned NULL");
4247+
snprintf(wt_b, sizeof(wt_b), "%s", raw);
4248+
th_rmtree(wt_a);
4249+
th_rmtree(wt_b);
4250+
char src[600];
4251+
snprintf(src, sizeof(src), "%s/mod.py", main_dir);
4252+
ASSERT_EQ(th_write_file(src, "def foo():\n return 1\n"), 0);
4253+
char step_a[700];
4254+
char step_b[700];
4255+
snprintf(step_a, sizeof(step_a), "worktree add -q \"%s\" -b branch-a", wt_a);
4256+
snprintf(step_b, sizeof(step_b), "worktree add -q \"%s\" -b branch-b", wt_b);
4257+
const char *steps[] = {"init -q", "add -A", "commit -q -m init", step_a, step_b};
4258+
for (size_t s = 0; s < sizeof(steps) / sizeof(steps[0]); s++) {
4259+
char cmd[1600];
4260+
snprintf(cmd, sizeof(cmd),
4261+
"git -C \"%s\" -c user.name=t -c [email protected] -c commit.gpgsign=false %s "
4262+
">/dev/null 2>&1",
4263+
main_dir, steps[s]);
4264+
if (system(cmd) != 0) {
4265+
th_rmtree(wt_a);
4266+
th_rmtree(wt_b);
4267+
th_rmtree(main_dir);
4268+
SKIP_PLATFORM("git not available for a worktree fixture");
4269+
}
4270+
}
4271+
4272+
char cache[256];
4273+
snprintf(cache, sizeof(cache), "%s/cbm-mcp-shared-XXXXXX", cbm_tmpdir());
4274+
ASSERT_NOT_NULL(cbm_mkdtemp(cache));
4275+
const char *saved_cache = getenv("CBM_CACHE_DIR");
4276+
char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
4277+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
4278+
cbm_project_name_share_worktrees(true);
4279+
4280+
char main_real[CBM_SZ_4K];
4281+
ASSERT_NOT_NULL(realpath(main_dir, main_real));
4282+
char *shared_name = cbm_project_name_from_path(main_real);
4283+
ASSERT_NOT_NULL(shared_name);
4284+
4285+
/* Index from worktree A: lands under the main checkout's name. */
4286+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4287+
ASSERT_NOT_NULL(srv);
4288+
char args[CBM_SZ_2K];
4289+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"mode\":\"fast\"}", wt_a);
4290+
char *resp = cbm_mcp_handle_tool(srv, "index_repository", args);
4291+
ASSERT_NOT_NULL(resp);
4292+
ASSERT_NULL(strstr(resp, "\"isError\":true"));
4293+
free(resp);
4294+
snprintf(args, sizeof(args), "{\"project\":\"%s\"}", shared_name);
4295+
resp = cbm_mcp_handle_tool(srv, "index_status", args);
4296+
ASSERT_NOT_NULL(resp);
4297+
ASSERT_NOT_NULL(strstr(resp, "ready"));
4298+
free(resp);
4299+
cbm_mcp_server_free(srv);
4300+
4301+
/* A session in sibling worktree B derives the same name: already indexed,
4302+
* no family fallback involved. */
4303+
srv = cbm_mcp_server_new(NULL);
4304+
ASSERT_NOT_NULL(srv);
4305+
ASSERT_TRUE(cbm_mcp_server_set_session_context(srv, wt_b, NULL));
4306+
ASSERT_STR_EQ(cbm_mcp_server_session_project(srv), shared_name);
4307+
ASSERT_STR_EQ(cbm_mcp_server_session_family_project(srv), "");
4308+
4309+
/* Re-index from B: same project, root_path now follows B. */
4310+
char wt_b_real[CBM_SZ_4K];
4311+
ASSERT_NOT_NULL(realpath(wt_b, wt_b_real));
4312+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"mode\":\"fast\"}", wt_b);
4313+
resp = cbm_mcp_handle_tool(srv, "index_repository", args);
4314+
ASSERT_NOT_NULL(resp);
4315+
ASSERT_NULL(strstr(resp, "\"isError\":true"));
4316+
free(resp);
4317+
snprintf(args, sizeof(args), "{\"project\":\"%s\"}", shared_name);
4318+
resp = cbm_mcp_handle_tool(srv, "index_status", args);
4319+
ASSERT_NOT_NULL(resp);
4320+
ASSERT_NOT_NULL(strstr(resp, "ready"));
4321+
ASSERT_NOT_NULL(strstr(resp, wt_b_real));
4322+
free(resp);
4323+
cbm_mcp_server_free(srv);
4324+
4325+
cbm_project_name_share_worktrees(false);
4326+
cbm_unsetenv("CBM_WORKTREE_SHARED_INDEX");
4327+
free(shared_name);
4328+
char prune[700];
4329+
snprintf(prune, sizeof(prune), "git -C \"%s\" worktree prune >/dev/null 2>&1", main_dir);
4330+
(void)system(prune);
4331+
th_rmtree(wt_a);
4332+
th_rmtree(wt_b);
4333+
th_rmtree(main_dir);
4334+
th_rmtree(cache);
4335+
restore_cache_dir(saved_cache_copy);
4336+
free(saved_cache_copy);
4337+
PASS();
4338+
#endif /* _WIN32 */
4339+
}
4340+
42244341
TEST(tool_index_repository_mutation_guard_blocks_before_local_worker) {
42254342
char root[CBM_SZ_1K];
42264343
(void)snprintf(root, sizeof(root), "%s/cbm-index-guard-XXXXXX", cbm_tmpdir());
@@ -14135,6 +14252,7 @@ SUITE(mcp) {
1413514252
RUN_TEST(mcp_auto_watch_false_skips_watcher_on_connect);
1413614253
RUN_TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853);
1413714254
RUN_TEST(session_worktree_family_resolves_to_indexed_canonical);
14255+
RUN_TEST(worktree_shared_index_spans_sibling_worktrees);
1413814256
}
1413914257

1414014258
/* Kept separate so daemon-coordination regressions can be iterated without

0 commit comments

Comments
 (0)