Skip to content

Commit 0dccc26

Browse files
committed
Port upstream DeusData#1430 ignore_worktrees to the CLI: hook gate and explicit index refusal
hook_augment: when ignore_worktrees is on in an existing _config.db (never created by the hook), canonicalize cwd and resolve the project through ha_resolve_project_ignoring_worktrees, dropping a parent graph that does not belong to the cwd's own linked worktree and switching to the 'not indexed on purpose' guidance. Key off (default) takes the previous path and the test gate counter does not move. index operation: an explicit index of a linked git worktree is refused when ignore_worktrees is on, with an error naming --index-worktree (per-call index_worktree arg, added to the tool schema) and 'config set ignore_worktrees false'. No silent skip. docs/CONFIGURATION.md: settings row, config example, CLI wording for the ignore_worktrees section, --index-worktree mention. tests: refusal/override/off/main-checkout integration test with a real git worktree, --index-worktree arg parsing test, and hook assertions updated for the CLI guidance text (codebase-memory-cli index).
1 parent 0aad606 commit 0dccc26

5 files changed

Lines changed: 189 additions & 20 deletions

File tree

‎docs/CONFIGURATION.md‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ codebase-memory-cli config get auto_index
7878
codebase-memory-cli config set auto_index true
7979
codebase-memory-cli config set auto_index_limit 50000
8080
codebase-memory-cli config set watcher_enabled false
81+
codebase-memory-cli config set ignore_worktrees true
8182
codebase-memory-cli config set doc_candidates false
8283
codebase-memory-cli config reset doc_candidates
8384
```
8485

8586
`doc_candidates` is `auto` by default: unset, the index mode decides. A single
8687
`codebase-memory-cli index --doc-candidates true|false` also overrides it for
87-
that index only.
88+
that index only. With `ignore_worktrees` on, `codebase-memory-cli index
89+
--index-worktree` indexes a linked git worktree for that one call.
8890

8991
Current keys:
9092

@@ -94,6 +96,7 @@ Current keys:
9496
| `auto_index_limit` | `50000` | Maximum file count allowed for automatic indexing of a new project. |
9597
| `auto_watch` | `true` | Register an active project with the retained background watcher when a warm runtime/hook integration supplies project context. A daemon-started watcher can retain registered projects after the initiating session ends until daemon shutdown, project deletion/root disappearance, or this setting is disabled. The ordinary one-shot CLI does not require this. |
9698
| `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 x 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. `codebase-memory-cli index --doc-candidates true|false` sets it for a single index and wins over this setting. Applies from the next index. |
99+
| `ignore_worktrees` | `false` | Skip linked git worktrees when indexing automatically, and refuse an explicit `codebase-memory-cli index` of one unless `--index-worktree` is passed. See [`ignore_worktrees`](#ignore_worktrees). |
97100
| `watch_non_git` | `false` | Poll non-Git roots with the indexer discovery rules and an adaptive file-tree scan. Skipped paths, including `.codebase-memory/`, do not trigger indexing. The first poll after daemon startup reindexes once; later changes trigger one reindex. This walks the whole tree each poll and is read when the daemon starts. |
98101
| `watcher_enabled` | `true` | Master switch for the retained background watcher subsystem. Set `false` to stop its poll thread/project registration. Reindex manually with `codebase-memory-cli index` when disabled. |
99102
| `index_max_files` | `off` | Optional maximum number of accepted source files in one discovery run. |
@@ -128,24 +131,24 @@ stored index of what is largely the same repository.
128131
Enable the key to keep those checkouts out of the index:
129132

130133
```bash
131-
codebase-memory-mcp config set ignore_worktrees true
134+
codebase-memory-cli config set ignore_worktrees true
132135
```
133136

134137
With it enabled:
135138

136139
- automatic indexing skips a session whose root is a linked worktree;
137140
- the `hook-augment` context says the worktree is unindexed on purpose instead
138-
of telling the agent to run `index_repository`;
139-
- an explicit `index_repository` call on a linked worktree is refused, and names
140-
both ways forward — pass `index_worktree=true` for that one call, or turn the
141-
key back off.
141+
of telling the agent to run `codebase-memory-cli index`;
142+
- an explicit `codebase-memory-cli index` of a linked worktree is refused with an
143+
error that names both ways forward: pass `--index-worktree` for that one call,
144+
or run `codebase-memory-cli config set ignore_worktrees false`.
142145

143146
The main checkout of the same repository is unaffected, as are ordinary clones
144147
and submodules. Detection is git plumbing only: a linked worktree's `.git` is a
145148
file pointing at a gitdir that contains a `commondir` entry.
146149

147150
A subdirectory counts as the worktree it sits in. Detection walks up from the
148-
given path to the nearest `.git` entry, so `index_repository` on any
151+
given path to the nearest `.git` entry, so `codebase-memory-cli index` on any
149152
subdirectory of a linked worktree is refused the same way, and a session root or
150153
hook working directory inside one is treated as that worktree. The walk stops at
151154
the first `.git` it finds; a main checkout or a submodule ends it as "not a

‎src/cli/hook_augment.c‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "foundation/constants.h"
2424
#include "foundation/mem.h"
2525
#include "foundation/platform.h"
26+
#include "foundation/str_util.h"
27+
#include "git/git_context.h"
2628
#include "pipeline/pipeline.h"
2729
#include "yyjson/yyjson.h"
2830

@@ -1715,6 +1717,53 @@ static bool ha_invocation_supported(ha_lifecycle_dialect_t dialect, const char *
17151717
static CBM_TLS int *g_ha_worktree_gate_counter = NULL;
17161718
#endif
17171719

1720+
/* Reads ignore_worktrees from the runtime config store only when the store
1721+
* already exists: the hook must not create _config.db as a side effect, and
1722+
* with the key off (the default) it does nothing else for worktrees. */
1723+
static bool ha_ignore_worktrees_enabled(void) {
1724+
const char *cache_dir = cbm_resolve_cache_dir();
1725+
if (!cache_dir || !cache_dir[0]) {
1726+
return false;
1727+
}
1728+
char db_path[4096];
1729+
int written = snprintf(db_path, sizeof(db_path), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);
1730+
if (written <= 0 || (size_t)written >= sizeof(db_path) || !cbm_file_exists(db_path)) {
1731+
return false;
1732+
}
1733+
cbm_config_t *cfg = cbm_config_open(cache_dir);
1734+
bool enabled = cbm_config_get_bool(cfg, CBM_CONFIG_IGNORE_WORKTREES, false);
1735+
cbm_config_close(cfg);
1736+
return enabled;
1737+
}
1738+
1739+
/* Project resolution once ignore_worktrees is on; the caller runs this only
1740+
* after reading the key. A symlink alias has no .git of its own, so cwd is
1741+
* canonicalized as the index operation does before the same gate. A linked
1742+
* worktree is refused even inside an indexed checkout or another linked
1743+
* worktree, so a match is kept only when its root belongs to this cwd's own
1744+
* linked worktree, not a parent graph. */
1745+
static char *ha_resolve_project_ignoring_worktrees(const char *cwd, bool *drop_ancestor,
1746+
bool *worktree_ignored) {
1747+
#ifdef CBM_CLI_ENABLE_TEST_API
1748+
if (g_ha_worktree_gate_counter) {
1749+
(*g_ha_worktree_gate_counter)++;
1750+
}
1751+
#endif
1752+
char canonical_cwd[4096];
1753+
if (ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {
1754+
cwd = canonical_cwd;
1755+
}
1756+
char project_root[4096];
1757+
project_root[0] = '\0';
1758+
char *project = ha_resolve_indexed_project_with_root(cwd, project_root, sizeof(project_root));
1759+
bool linked = cbm_git_is_linked_worktree(cwd);
1760+
/* Drop a parent graph without a second free(): this file's raw free count
1761+
* is ratcheted, and the caller's single free(project) still releases it. */
1762+
*drop_ancestor = linked && project && !cbm_git_same_linked_worktree(cwd, project_root);
1763+
*worktree_ignored = linked && (!project || *drop_ancestor);
1764+
return project;
1765+
}
1766+
17181767
static char *ha_lifecycle_json_from_root(const char *session_root, yyjson_val *root,
17191768
const char *forced_event, ha_lifecycle_dialect_t dialect) {
17201769
if (!root || !yyjson_is_obj(root)) {
@@ -1728,10 +1777,14 @@ static char *ha_lifecycle_json_from_root(const char *session_root, yyjson_val *r
17281777
char cwd_buffer[4096];
17291778
const char *cwd =
17301779
ha_normalized_cwd_with_root(root, session_root, cwd_buffer, sizeof(cwd_buffer));
1731-
char *project = cwd ? ha_resolve_indexed_project(cwd) : NULL;
1732-
/* Upstream #1430 worktree gate; set by the ignore_worktrees resolution. */
17331780
bool drop_ancestor = false;
17341781
bool worktree_ignored = false;
1782+
char *project = NULL;
1783+
if (cwd && ha_ignore_worktrees_enabled()) {
1784+
project = ha_resolve_project_ignoring_worktrees(cwd, &drop_ancestor, &worktree_ignored);
1785+
} else if (cwd) {
1786+
project = ha_resolve_indexed_project(cwd);
1787+
}
17351788

17361789
char context[2048];
17371790
const char *scope = "Session";

‎src/operations/index.c‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "foundation/platform.h"
1919
#include "foundation/str_util.h"
2020
#include "foundation/workspace.h"
21+
#include "git/git_context.h"
2122
#include "pipeline/artifact.h"
2223
#include "pipeline/pipeline.h"
2324
#include "store/store.h"
@@ -1217,6 +1218,27 @@ static bool index_call_mode_arg(const char *json, const char *key, bool *out) {
12171218
return valid;
12181219
}
12191220

1221+
/* ignore_worktrees: an explicit index of a linked worktree is refused, not
1222+
* silently skipped; index_worktree overrides it for this call. Fills error and
1223+
* returns true on refusal. Reads no config unless repo_path is a linked
1224+
* worktree. */
1225+
static bool index_worktree_refused(const char *json, const char *repo_path, char *error,
1226+
size_t error_size) {
1227+
if (index_bool_arg(json, "index_worktree") || !cbm_git_is_linked_worktree(repo_path)) {
1228+
return false;
1229+
}
1230+
cbm_config_t *config = cbm_config_open(cbm_resolve_cache_dir());
1231+
bool ignore_worktrees = cbm_config_get_bool(config, CBM_CONFIG_IGNORE_WORKTREES, false);
1232+
cbm_config_close(config);
1233+
if (ignore_worktrees) {
1234+
snprintf(error, error_size,
1235+
"repo_path is a linked git worktree and ignore_worktrees is enabled. Pass "
1236+
"--index-worktree to index it anyway, or run: codebase-memory-cli config set "
1237+
"ignore_worktrees false");
1238+
}
1239+
return ignore_worktrees;
1240+
}
1241+
12201242
cbm_operation_result_t cbm_index_operation_execute(const char *args_json,
12211243
const cbm_operation_runtime_t *runtime) {
12221244
const char *json = args_json ? args_json : "{}";
@@ -1258,7 +1280,8 @@ cbm_operation_result_t cbm_index_operation_execute(const char *args_json,
12581280
: getenv("CBM_ALLOWED_ROOT");
12591281
char boundary[CBM_SZ_1K];
12601282
if (!cbm_workspace_root_allowed(repo_path, cbm_workspace_home_dir(), cbm_workspace_cache_dir(),
1261-
allowed_root, boundary, sizeof(boundary))) {
1283+
allowed_root, boundary, sizeof(boundary)) ||
1284+
(!status_mode && index_worktree_refused(json, repo_path, boundary, sizeof(boundary)))) {
12621285
free(repo_path);
12631286
return index_text_error(boundary);
12641287
}

‎src/operations/tool_catalog.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static const tool_def_t TOOLS[] = {
4444
"\"doc_candidates\":{\"type\":\"boolean\",\"description\":"
4545
"\"Doc-code candidates; default: the doc_candidates setting, else on for full, off for "
4646
"moderate/fast.\"},"
47+
"\"index_worktree\":{\"type\":\"boolean\",\"default\":false,\"description\":"
48+
"\"Index repo_path even when it is a linked git worktree and the ignore_worktrees setting "
49+
"is on. No effect otherwise.\"},"
4750
"\"async\":{\"type\":\"boolean\",\"default\":false,\"description\":"
4851
"\"Start or join a background index. Requires a durable daemon (run daemon start). Poll with "
4952
"--status.\"},"

0 commit comments

Comments
 (0)