Skip to content

Commit cb7dfae

Browse files
authored
Merge pull request #1 from vitalNohj/cloud/cbm-worktree-ignore-rework-6ddc
feat(config): ignore_worktrees rework for #1430
2 parents 80eb92a + b21c8cc commit cb7dfae

14 files changed

Lines changed: 925 additions & 15 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ Watcher registration is controlled separately by `auto_watch` (default `true`).
155155

156156
To turn the watcher off entirely, set `config set watcher_enabled false` (default `true`): the background poll thread never starts and no project is registered, while `auto_index` and manual `index_repository` keep working. Unlike `auto_watch` — which is consulted per session — `watcher_enabled` is read once when the background daemon starts, so run `codebase-memory-mcp daemon stop` after changing it; reconnecting your MCP client alone will not restart the daemon. See [docs/CONFIGURATION.md](docs/CONFIGURATION.md#2-cli-managed-runtime-settings).
157157

158+
Linked git worktrees are indexed as their own projects by default. Set `config set ignore_worktrees true` to skip them on the automatic paths — useful when short-lived `git worktree add` checkouts would otherwise each leave behind a permanent index of the same repository. An explicit `index_repository` call on a worktree is then refused unless you pass `index_worktree=true`. See [docs/CONFIGURATION.md](docs/CONFIGURATION.md#ignore_worktrees).
159+
158160
### Keeping Up to Date
159161

160162
**Updates run from the install script on every platform, not from inside the running binary.** `codebase-memory-mcp update` validates your flags and then prints the exact command to run:
@@ -735,6 +737,7 @@ codebase-memory-mcp config list # show all settings
735737
codebase-memory-mcp config set auto_index true # auto-index on session start
736738
codebase-memory-mcp config set auto_index_limit 50000 # max files for auto-index
737739
codebase-memory-mcp config set auto_watch false # don't register background git watcher (default: true)
740+
codebase-memory-mcp config set ignore_worktrees true # skip linked git worktrees when auto-indexing (default: false)
738741
codebase-memory-mcp config set watcher_enabled false # stop the watcher thread entirely (default: true)
739742
codebase-memory-mcp config set index_max_files 250000 # optional per-index source-file limit
740743
codebase-memory-mcp config set index_max_source_mb 16384 # optional per-index source-size limit

‎docs/CONFIGURATION.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Current keys:
8787
|---|---|---|
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. |
90+
| `ignore_worktrees` | `false` | Skip linked git worktrees (`git worktree add`) when indexing automatically. |
9091
| `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). |
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
| `index_max_files` | `off` | Optional maximum number of accepted source files in one discovery run. |
@@ -126,6 +127,35 @@ and preserves any previously serving database. See
126127
[Index resource limits](INDEX_RESOURCE_LIMITS.md) for counting, validation, and
127128
error-response details.
128129
130+
### `ignore_worktrees`
131+
132+
Every indexed project is registered under its own absolute root path, so each
133+
linked worktree becomes a separate permanent index. On machines that create many
134+
short-lived worktrees, the automatic paths (`auto_index`, and the session hook's
135+
"index this project first" guidance) turn every throwaway checkout into another
136+
stored index of what is largely the same repository.
137+
138+
Enable the key to keep those checkouts out of the index:
139+
140+
```bash
141+
codebase-memory-mcp config set ignore_worktrees true
142+
```
143+
144+
With it enabled:
145+
146+
- automatic indexing skips a session whose root is a linked worktree;
147+
- the `hook-augment` context says the worktree is unindexed on purpose instead
148+
of telling the agent to run `index_repository`;
149+
- an explicit `index_repository` call on a linked worktree is refused, and names
150+
both ways forward — pass `index_worktree=true` for that one call, or turn the
151+
key back off.
152+
153+
The main checkout of the same repository is unaffected, as are ordinary clones
154+
and submodules. Detection is git plumbing only: a linked worktree's `.git` is a
155+
file pointing at a gitdir that contains a `commondir` entry.
156+
157+
The default is `false`, so indexing behavior is unchanged unless you opt in.
158+
129159
## 3. UI Settings
130160
131161
The optional built-in graph UI stores its settings in:

‎src/cli/cli.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7442,6 +7442,7 @@ static const config_key_def_t CONFIG_KEYS[] = {
74427442
{CBM_CONFIG_AUTO_INDEX, "false", "Enable auto-indexing on MCP session start"},
74437443
{CBM_CONFIG_AUTO_INDEX_LIMIT, "50000", "Max files for auto-indexing new projects"},
74447444
{CBM_CONFIG_AUTO_WATCH, "true", "Register background git watcher on session connect"},
7445+
{CBM_CONFIG_IGNORE_WORKTREES, "false", "Skip linked git worktrees when indexing automatically"},
74457446
{CBM_CONFIG_WATCHER_ENABLED, "true",
74467447
"Run the background watcher thread (auto-reindex); false to disable"},
74477448
{CBM_CONFIG_UI_LANG, "auto", "Pin graph UI language: en, zh, or auto"},

‎src/cli/cli.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ bool cbm_hook_augment_invocation_supported_for_testing(const char *dialect,
262262
const char *forced_event);
263263
bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
264264
bool case_insensitive);
265-
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event);
265+
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
266+
bool worktree_ignored);
266267
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);
267268
bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);
268269
void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);
@@ -443,6 +444,7 @@ bool cbm_config_load_index_policy(cbm_config_t *cfg, cbm_index_resource_policy_t
443444
#define CBM_CONFIG_AUTO_INDEX "auto_index"
444445
#define CBM_CONFIG_AUTO_INDEX_LIMIT "auto_index_limit"
445446
#define CBM_CONFIG_AUTO_WATCH "auto_watch"
447+
#define CBM_CONFIG_IGNORE_WORKTREES "ignore_worktrees"
446448
#define CBM_CONFIG_UI_LANG "ui-lang"
447449
#define CBM_CONFIG_WATCHER_ENABLED "watcher_enabled"
448450
/* #1558: the graph UI's loopback listener. Stored in the UI config file rather

‎src/cli/hook_augment.c‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include "foundation/constants.h"
2222
#include "foundation/mem.h"
2323
#include "foundation/platform.h"
24+
#include "git/git_context.h"
2425
#include "mcp/mcp.h"
26+
#include "mcp/mcp_internal.h"
2527
#include "pipeline/pipeline.h"
2628
#include "yyjson/yyjson.h"
2729

@@ -1000,10 +1002,6 @@ static char *ha_resolve_indexed_project_with_root(cbm_mcp_server_t *srv, const c
10001002
return ha_registry_project_for_path(srv, cwd, root_out, root_out_size);
10011003
}
10021004

1003-
static char *ha_resolve_indexed_project(cbm_mcp_server_t *srv, const char *cwd) {
1004-
return ha_resolve_indexed_project_with_root(srv, cwd, NULL, 0U);
1005-
}
1006-
10071005
static const char *ha_hook_event_name(yyjson_val *root) {
10081006
const char *event = ha_obj_str(root, "hook_event_name");
10091007
return event ? event : ha_obj_str(root, "hookEventName");
@@ -1440,7 +1438,14 @@ static const char *ha_active_tier(yyjson_val *root, const char *event) {
14401438
return "Tier 2 verification";
14411439
}
14421440

1443-
static const char *ha_no_project_index_guidance(const char *event) {
1441+
static const char *ha_no_project_index_guidance(const char *event, bool worktree_ignored) {
1442+
/* ignore_worktrees is on and this cwd is a linked worktree: indexing it is
1443+
* deliberately disabled, so telling the agent to run index_repository would
1444+
* only produce a refusal. Point at the setting instead. */
1445+
if (worktree_ignored) {
1446+
return "This is a linked git worktree and ignore_worktrees is enabled, so it is not "
1447+
"indexed on purpose; do not run index_repository here.";
1448+
}
14441449
return event && strcmp(event, "SubagentStart") == 0
14451450
? "Ask the parent agent to run index_repository before structural exploration; "
14461451
"do not attempt graph mutation."
@@ -1466,12 +1471,48 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
14661471

14671472
char cwd_buffer[4096];
14681473
cbm_mcp_server_t *owned_server = NULL;
1474+
cbm_config_t *owned_config = NULL;
14691475
if (!srv) {
14701476
owned_server = cbm_mcp_server_new(NULL);
14711477
srv = owned_server;
14721478
}
1479+
/* cbm_mcp_server_new(NULL) does not attach the runtime store. The production
1480+
* hook uses that constructor, so ignore_worktrees would stay at its default
1481+
* (off) and the "not indexed" note would still tell the agent to run
1482+
* index_repository in a linked worktree the setting refuses. Load the store
1483+
* only when the caller has not already set one, then drop it before return
1484+
* so a caller-owned server is not left pointing at a closed config. */
1485+
if (srv) {
1486+
owned_config = cbm_mcp_server_attach_runtime_config(srv);
1487+
}
14731488
const char *cwd = ha_normalized_cwd_with_server(root, srv, cwd_buffer, sizeof(cwd_buffer));
1474-
char *project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;
1489+
/* A symlink alias has no .git of its own, and walking its lexical parents
1490+
* leaves the linked worktree. index_repository canonicalizes before the
1491+
* same gate, so the hook must too or it tells the agent to index a path
1492+
* the setting will refuse. */
1493+
char canonical_cwd[4096];
1494+
if (cwd && ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {
1495+
cwd = canonical_cwd;
1496+
}
1497+
char project_root[4096];
1498+
project_root[0] = '\0';
1499+
char *project = srv && cwd ? ha_resolve_indexed_project_with_root(srv, cwd, project_root,
1500+
sizeof(project_root))
1501+
: NULL;
1502+
/* ignore_worktrees refuses a linked worktree, including one nested inside an
1503+
* indexed checkout or inside another linked worktree. Keep a match only when
1504+
* its root belongs to this cwd's own linked worktree, not a parent graph. */
1505+
bool ignore_linked =
1506+
srv && cwd && cbm_mcp_ignore_worktrees_enabled(srv) && cbm_git_is_linked_worktree(cwd);
1507+
/* Drop a parent graph without a second free(): this file's raw free count
1508+
* is ratcheted, and the single free(project) below still releases it. */
1509+
bool drop_ancestor =
1510+
ignore_linked && project && !cbm_git_same_linked_worktree(cwd, project_root);
1511+
bool worktree_ignored = ignore_linked && (!project || drop_ancestor);
1512+
if (owned_config) {
1513+
cbm_mcp_server_set_config(srv, NULL);
1514+
cbm_config_close(owned_config);
1515+
}
14751516
cbm_mcp_server_free(owned_server);
14761517

14771518
char context[2048];
@@ -1490,7 +1531,7 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
14901531
scope = "Compaction";
14911532
}
14921533
const char *tier = ha_active_tier(root, event);
1493-
if (project) {
1534+
if (project && !drop_ancestor) {
14941535
char safe_project[HA_METADATA_CAP];
14951536
ha_sanitize_metadata(project, safe_project, sizeof(safe_project));
14961537
snprintf(context, sizeof(context),
@@ -1505,7 +1546,7 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
15051546
"and file reads for literals, configs, non-code files, and verification.",
15061547
scope, safe_project, tier);
15071548
} else {
1508-
const char *index_guidance = ha_no_project_index_guidance(event);
1549+
const char *index_guidance = ha_no_project_index_guidance(event, worktree_ignored);
15091550
snprintf(context, sizeof(context),
15101551
"[codebase-memory] %s context: no indexed graph project matched this working "
15111552
"directory. %s Once indexed, "
@@ -1624,8 +1665,9 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
16241665
return ha_path_contains_mode(root, candidate, case_insensitive);
16251666
}
16261667

1627-
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event) {
1628-
return ha_no_project_index_guidance(event);
1668+
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
1669+
bool worktree_ignored) {
1670+
return ha_no_project_index_guidance(event, worktree_ignored);
16291671
}
16301672

16311673
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz) {

‎src/daemon/application.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "foundation/sha256.h"
1616
#include "foundation/subprocess.h"
1717
#include "foundation/workspace.h"
18+
#include "git/git_context.h"
1819
#include "mcp/index_supervisor.h"
1920
#include "mcp/mcp.h"
2021
#include "mcp/mcp_internal.h"
@@ -1456,6 +1457,18 @@ static void application_auto_index_retry_pending_locked(cbm_daemon_application_t
14561457
application_refresh_watch_locked(session);
14571458
continue;
14581459
}
1460+
/* ignore_worktrees can be turned on after this session was queued for
1461+
* a capacity slot. Initial admission already skips linked worktrees;
1462+
* retry must apply the same gate or the pending checkout is indexed
1463+
* once a slot opens. */
1464+
if (cbm_mcp_ignore_worktrees_enabled(session->mcp) &&
1465+
cbm_git_is_linked_worktree(root_path)) {
1466+
session->auto_index_retry_pending = false;
1467+
cbm_log_info("daemon.autoindex.skipped", "project", project, "reason",
1468+
"linked_worktree");
1469+
application_refresh_watch_locked(session);
1470+
continue;
1471+
}
14591472
char *args = application_auto_index_args(application, root_path);
14601473
if (!args) {
14611474
continue;
@@ -2071,6 +2084,11 @@ static void application_background_initialize_impl(cbm_daemon_application_sessio
20712084
!application_session_workspace_allowed(session, "auto_index_discovery")) {
20722085
auto_index_candidate = false;
20732086
}
2087+
if (auto_index_candidate && cbm_mcp_ignore_worktrees_enabled(session->mcp) &&
2088+
cbm_git_is_linked_worktree(root_path)) {
2089+
cbm_log_info("daemon.autoindex.skipped", "project", project, "reason", "linked_worktree");
2090+
auto_index_candidate = false;
2091+
}
20742092
bool within_auto_index_limit =
20752093
!auto_index_candidate ||
20762094
cbm_mcp_auto_index_within_file_limit(root_path, auto_index_limit, &tracked_files);

0 commit comments

Comments
 (0)