Skip to content

Commit 2921415

Browse files
authored
Merge pull request DeusData#1430 from vitalNohj/fm/cbm-worktree-ignore-1
feat(config): add ignore_worktrees to skip linked git worktrees
2 parents 9c19b5d + 141b25a commit 2921415

13 files changed

Lines changed: 1113 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ To turn the watcher off entirely, set `config set watcher_enabled false` (defaul
263263

264264
The watcher follows **git** projects only by default: a project indexed from a directory that is not a git repository is not watched and stays at its last index until you run `index_repository` again. To have such roots polled too, set `config set watch_non_git true` (default `false`, read when the daemon starts): they are then checked on the same cadence with a file-tree scan that uses the indexer's own ignore rules.
265265

266+
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).
267+
266268
### Keeping Up to Date
267269

268270
**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:
@@ -1016,6 +1018,7 @@ codebase-memory-mcp config list # show all settings
10161018
codebase-memory-mcp config set auto_index true # auto-index on session start
10171019
codebase-memory-mcp config set auto_index_limit 50000 # max files for auto-index
10181020
codebase-memory-mcp config set auto_watch false # remove/disable all background watches (default: true)
1021+
codebase-memory-mcp config set ignore_worktrees true # skip linked git worktrees when auto-indexing (default: false)
10191022
codebase-memory-mcp config set doc_candidates false # skip doc<->code candidate scoring on huge repos (default: auto = on for full, off for moderate/fast)
10201023
codebase-memory-mcp config set watcher_enabled false # stop the watcher thread entirely (default: true)
10211024
codebase-memory-mcp config set watch_non_git true # also poll non-git project roots (default: false)

‎docs/CONFIGURATION.md‎

Lines changed: 39 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` | Global switch for MCP session watch registration. On the next refresh or registration request, `true` allows session roots and successful `index_repository` targets to be watched; `false` removes all watches. The background watcher can still run with no registered projects. |
9192
| `doc_candidates` | `auto` | Score documentation sections against code to store doc↔code candidates (the possible targets of a section, and the sections that may describe a function or file, each with a judged probability). Unset (`auto`), the index mode decides: on for `full` indexes (the default mode), off for `moderate` and `fast`. Set `true` or `false` to choose for every mode; `config reset doc_candidates` returns to `auto`. The scoring grows with sections × functions: under a second up to ~100k functions, ~15 s at ~400k, ~3 minutes on the Linux kernel. Off stores no candidates and leaves the rest of the graph unchanged; on in `fast` mode stores candidates without adding `fast`'s skipped `SIMILAR_TO` / `SEMANTICALLY_RELATED` edges. `index_repository` also takes `doc_candidates` for a single index, which wins over this setting. Applies from the next index. |
9293
| `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. Active MCP sessions can register their root project and successful explicit index targets; under `daemon start` those watches outlive the session until the daemon stops, the project is deleted, its directory disappears, or `auto_watch` is set to `false`. `index_status` reports the current state in its `watch` object. |
@@ -142,6 +143,44 @@ and preserves any previously serving database. See
142143
[Index resource limits](INDEX_RESOURCE_LIMITS.md) for counting, validation, and
143144
error-response details.
144145
146+
### `ignore_worktrees`
147+
148+
Every indexed project is registered under its own absolute root path, so each
149+
linked worktree becomes a separate permanent index. On machines that create many
150+
short-lived worktrees, the automatic paths (`auto_index`, and the session hook's
151+
"index this project first" guidance) turn every throwaway checkout into another
152+
stored index of what is largely the same repository.
153+
154+
Enable the key to keep those checkouts out of the index:
155+
156+
```bash
157+
codebase-memory-mcp config set ignore_worktrees true
158+
```
159+
160+
With it enabled:
161+
162+
- automatic indexing skips a session whose root is a linked worktree;
163+
- the `hook-augment` context says the worktree is unindexed on purpose instead
164+
of telling the agent to run `index_repository`;
165+
- an explicit `index_repository` call on a linked worktree is refused, and names
166+
both ways forward — pass `index_worktree=true` for that one call, or turn the
167+
key back off.
168+
169+
The main checkout of the same repository is unaffected, as are ordinary clones
170+
and submodules. Detection is git plumbing only: a linked worktree's `.git` is a
171+
file pointing at a gitdir that contains a `commondir` entry.
172+
173+
A subdirectory counts as the worktree it sits in. Detection walks up from the
174+
given path to the nearest `.git` entry, so `index_repository` on any
175+
subdirectory of a linked worktree is refused the same way, and a session root or
176+
hook working directory inside one is treated as that worktree. The walk stops at
177+
the first `.git` it finds; a main checkout or a submodule ends it as "not a
178+
worktree". A path outside any git checkout is checked one directory at a time up
179+
to the filesystem root and is never treated as a worktree. None of this runs
180+
while the key is off.
181+
182+
The default is `false`, so indexing behavior is unchanged unless you opt in.
183+
145184
## 3. UI Settings
146185
147186
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
@@ -8088,6 +8088,7 @@ static const config_key_def_t CONFIG_KEYS[] = {
80888088
{CBM_CONFIG_AUTO_INDEX, "false", "Enable auto-indexing on MCP session start"},
80898089
{CBM_CONFIG_AUTO_INDEX_LIMIT, "50000", "Max files for auto-indexing new projects"},
80908090
{CBM_CONFIG_AUTO_WATCH, "true", "Register background git watcher on session connect"},
8091+
{CBM_CONFIG_IGNORE_WORKTREES, "false", "Skip linked git worktrees when indexing automatically"},
80918092
/* Unset: the index mode decides (cbm_pipeline_doc_candidates_enabled). */
80928093
{CBM_CONFIG_DOC_CANDIDATES, "auto",
80938094
"Doc-code candidates: auto = on for full, off for moderate/fast; true/false forces"},

‎src/cli/cli.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ bool cbm_hook_augment_invocation_supported_for_testing(const char *dialect,
336336
const char *forced_event);
337337
bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
338338
bool case_insensitive);
339-
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event);
339+
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
340+
bool worktree_ignored);
341+
/* Counts lifecycle calls that reached the ignore_worktrees resolution (cwd
342+
* canonicalization and the linked-worktree probe). NULL stops counting. */
343+
void cbm_hook_set_worktree_gate_counter_for_testing(int *counter);
340344
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);
341345
/* include_posttool false renders the previous lifecycle-only manifest. Release
342346
* the result with cbm_free(CBM_MEM_CLASS_OTHER, ...). */
@@ -526,6 +530,7 @@ bool cbm_config_load_index_policy(cbm_config_t *cfg, cbm_index_resource_policy_t
526530
#define CBM_CONFIG_AUTO_INDEX "auto_index"
527531
#define CBM_CONFIG_AUTO_INDEX_LIMIT "auto_index_limit"
528532
#define CBM_CONFIG_AUTO_WATCH "auto_watch"
533+
#define CBM_CONFIG_IGNORE_WORKTREES "ignore_worktrees"
529534
#define CBM_CONFIG_DOC_CANDIDATES "doc_candidates"
530535
#define CBM_CONFIG_UI_LANG "ui-lang"
531536
#define CBM_CONFIG_WATCHER_ENABLED "watcher_enabled"

‎src/cli/hook_augment.c‎

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
*/
1818

1919
#include "cli/cli.h"
20+
#include "foundation/compat.h"
2021
#include "foundation/compat_fs.h"
2122
#include "foundation/constants.h"
2223
#include "foundation/mem.h"
2324
#include "foundation/platform.h"
25+
#include "git/git_context.h"
2426
#include "mcp/mcp.h"
27+
#include "mcp/mcp_internal.h"
2528
#include "pipeline/pipeline.h"
2629
#include "yyjson/yyjson.h"
2730

@@ -1654,7 +1657,14 @@ static const char *ha_active_tier(yyjson_val *root, const char *event) {
16541657
return "Tier 2 verification";
16551658
}
16561659

1657-
static const char *ha_no_project_index_guidance(const char *event) {
1660+
static const char *ha_no_project_index_guidance(const char *event, bool worktree_ignored) {
1661+
/* ignore_worktrees is on and this cwd is a linked worktree: indexing it is
1662+
* deliberately disabled, so telling the agent to run index_repository would
1663+
* only produce a refusal. Point at the setting instead. */
1664+
if (worktree_ignored) {
1665+
return "This is a linked git worktree and ignore_worktrees is enabled, so it is not "
1666+
"indexed on purpose; do not run index_repository here.";
1667+
}
16581668
return event && strcmp(event, "SubagentStart") == 0
16591669
? "Ask the parent agent to run index_repository before structural exploration; "
16601670
"do not attempt graph mutation."
@@ -1671,6 +1681,41 @@ static bool ha_invocation_supported(ha_lifecycle_dialect_t dialect, const char *
16711681
return !forced_event || ha_dialect_event_supported(dialect, forced_event);
16721682
}
16731683

1684+
#ifdef CBM_CLI_ENABLE_TEST_API
1685+
static CBM_TLS int *g_ha_worktree_gate_counter = NULL;
1686+
#endif
1687+
1688+
/* Project resolution once ignore_worktrees is on; the caller runs this only
1689+
* after reading the key, so the default path does none of it. A symlink alias
1690+
* has no .git of its own, and walking its lexical parents leaves the linked
1691+
* worktree. index_repository canonicalizes before the same gate, so the hook
1692+
* must too or it tells the agent to index a path the setting will refuse. The
1693+
* setting refuses a linked worktree even when it sits inside an indexed
1694+
* checkout or inside another linked worktree, so a match is kept only when its
1695+
* root belongs to this cwd's own linked worktree, not a parent graph. */
1696+
static char *ha_resolve_project_ignoring_worktrees(cbm_mcp_server_t *srv, const char *cwd,
1697+
bool *drop_ancestor, bool *worktree_ignored) {
1698+
#ifdef CBM_CLI_ENABLE_TEST_API
1699+
if (g_ha_worktree_gate_counter) {
1700+
(*g_ha_worktree_gate_counter)++;
1701+
}
1702+
#endif
1703+
char canonical_cwd[4096];
1704+
if (ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {
1705+
cwd = canonical_cwd;
1706+
}
1707+
char project_root[4096];
1708+
project_root[0] = '\0';
1709+
char *project =
1710+
ha_resolve_indexed_project_with_root(srv, cwd, project_root, sizeof(project_root));
1711+
bool linked = cbm_git_is_linked_worktree(cwd);
1712+
/* Drop a parent graph without a second free(): this file's raw free count
1713+
* is ratcheted, and the caller's single free(project) still releases it. */
1714+
*drop_ancestor = linked && project && !cbm_git_same_linked_worktree(cwd, project_root);
1715+
*worktree_ignored = linked && (!project || *drop_ancestor);
1716+
return project;
1717+
}
1718+
16741719
static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root,
16751720
const char *forced_event, ha_lifecycle_dialect_t dialect) {
16761721
if (!root || !yyjson_is_obj(root)) {
@@ -1688,7 +1733,20 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
16881733
srv = owned_server;
16891734
}
16901735
const char *cwd = ha_normalized_cwd_with_server(root, srv, cwd_buffer, sizeof(cwd_buffer));
1691-
char *project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;
1736+
/* Read ignore_worktrees first and do nothing else for it while it is off.
1737+
* cbm_mcp_server_new(NULL) has no store, and srv may be the daemon's
1738+
* session server, so the read leaves srv untouched (see
1739+
* cbm_mcp_ignore_worktrees_runtime_enabled). Off, this is the resolution
1740+
* the hook did before the key existed. */
1741+
bool drop_ancestor = false;
1742+
bool worktree_ignored = false;
1743+
char *project = NULL;
1744+
if (srv && cwd && cbm_mcp_ignore_worktrees_runtime_enabled(srv)) {
1745+
project =
1746+
ha_resolve_project_ignoring_worktrees(srv, cwd, &drop_ancestor, &worktree_ignored);
1747+
} else {
1748+
project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;
1749+
}
16921750
cbm_mcp_server_free(owned_server);
16931751

16941752
char context[2048];
@@ -1713,7 +1771,7 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
17131771
? " The same tools are also available from the shell: "
17141772
"`codebase-memory-mcp cli <tool> --help` lists each tool's flags."
17151773
: "";
1716-
if (project) {
1774+
if (project && !drop_ancestor) {
17171775
char safe_project[HA_METADATA_CAP];
17181776
ha_sanitize_metadata(project, safe_project, sizeof(safe_project));
17191777
/* The untrusted label covers the graph-derived project name only. Wrapping
@@ -1732,7 +1790,7 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
17321790
"and file reads for literals, configs, non-code files, and verification.%s",
17331791
scope, safe_project, tier, cli_hint);
17341792
} else {
1735-
const char *index_guidance = ha_no_project_index_guidance(event);
1793+
const char *index_guidance = ha_no_project_index_guidance(event, worktree_ignored);
17361794
snprintf(context, sizeof(context),
17371795
"[codebase-memory] %s context: no indexed graph project matched this working "
17381796
"directory. %s Once indexed, "
@@ -1851,8 +1909,13 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
18511909
return ha_path_contains_mode(root, candidate, case_insensitive);
18521910
}
18531911

1854-
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event) {
1855-
return ha_no_project_index_guidance(event);
1912+
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
1913+
bool worktree_ignored) {
1914+
return ha_no_project_index_guidance(event, worktree_ignored);
1915+
}
1916+
1917+
void cbm_hook_set_worktree_gate_counter_for_testing(int *counter) {
1918+
g_ha_worktree_gate_counter = counter;
18561919
}
18571920

18581921
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
@@ -17,6 +17,7 @@
1717
#include "foundation/sha256.h"
1818
#include "foundation/subprocess.h"
1919
#include "foundation/workspace.h"
20+
#include "git/git_context.h"
2021
#include "mcp/index_supervisor.h"
2122
#include "mcp/mcp.h"
2223
#include "mcp/mcp_internal.h"
@@ -1941,6 +1942,18 @@ static void application_auto_index_retry_pending_locked(cbm_daemon_application_t
19411942
application_refresh_watch_locked(session);
19421943
continue;
19431944
}
1945+
/* ignore_worktrees can be turned on after this session was queued for
1946+
* a capacity slot. Initial admission already skips linked worktrees;
1947+
* retry must apply the same gate or the pending checkout is indexed
1948+
* once a slot opens. */
1949+
if (cbm_mcp_ignore_worktrees_enabled(session->mcp) &&
1950+
cbm_git_is_linked_worktree(root_path)) {
1951+
session->auto_index_retry_pending = false;
1952+
cbm_log_info("daemon.autoindex.skipped", "project", project, "reason",
1953+
"linked_worktree");
1954+
application_refresh_watch_locked(session);
1955+
continue;
1956+
}
19441957
char *args = application_auto_index_args(application, root_path);
19451958
if (!args) {
19461959
continue;
@@ -2599,6 +2612,11 @@ static void application_background_initialize_impl(cbm_daemon_application_sessio
25992612
!application_session_workspace_allowed(session, "auto_index_discovery")) {
26002613
auto_index_candidate = false;
26012614
}
2615+
if (auto_index_candidate && cbm_mcp_ignore_worktrees_enabled(session->mcp) &&
2616+
cbm_git_is_linked_worktree(root_path)) {
2617+
cbm_log_info("daemon.autoindex.skipped", "project", project, "reason", "linked_worktree");
2618+
auto_index_candidate = false;
2619+
}
26022620
bool within_auto_index_limit =
26032621
!auto_index_candidate ||
26042622
cbm_mcp_auto_index_within_file_limit(root_path, auto_index_limit, &tracked_files);

0 commit comments

Comments
 (0)