Skip to content

Commit 77310d5

Browse files
committed
fix(cli): keep the hook default path untouched while ignore_worktrees is off
The lifecycle hook now reads ignore_worktrees first and does nothing else for it while the key is off; the project lookup is then the one the hook did before the key existed. - cbm_mcp_ignore_worktrees_runtime_enabled() replaces cbm_mcp_server_attach_runtime_config(). It never attaches a store to the server, which may be the daemon session server. A server with a store answers from it. Otherwise the runtime store is opened only to read this key, and only when it already exists, so a hook event never creates _config.db. - cwd canonicalization, the linked-worktree probe and the parent-graph check moved into ha_resolve_project_ignoring_worktrees(), which runs only when the key is on. Test cli_hook_ignore_worktrees_default_off_does_no_worktree_work was red on the previous head (a default-off event created _config.db). It also pins, through a test-API counter, that neither an absent store nor a store without the key reaches the worktree gate, and that the key on reaches it once. mcp_ignore_worktrees_runtime_read_leaves_server_and_store_alone replaces the test of the removed helper. Signed-off-by: Martin Vogel <[email protected]>
1 parent faf25ee commit 77310d5

7 files changed

Lines changed: 220 additions & 72 deletions

File tree

‎src/cli/cli.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ bool cbm_hook_path_contains_for_testing(const char *root, const char *candidate,
264264
bool case_insensitive);
265265
const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
266266
bool worktree_ignored);
267+
/* Counts lifecycle calls that reached the ignore_worktrees resolution (cwd
268+
* canonicalization and the linked-worktree probe). NULL stops counting. */
269+
void cbm_hook_set_worktree_gate_counter_for_testing(int *counter);
267270
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz);
268271
bool cbm_mcp_command_path_probe_safe_for_testing(const char *command, bool windows);
269272
void cbm_set_mcp_command_path_probe_counter_for_testing(int *counter);

‎src/cli/hook_augment.c‎

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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"
@@ -1002,6 +1003,10 @@ static char *ha_resolve_indexed_project_with_root(cbm_mcp_server_t *srv, const c
10021003
return ha_registry_project_for_path(srv, cwd, root_out, root_out_size);
10031004
}
10041005

1006+
static char *ha_resolve_indexed_project(cbm_mcp_server_t *srv, const char *cwd) {
1007+
return ha_resolve_indexed_project_with_root(srv, cwd, NULL, 0U);
1008+
}
1009+
10051010
static const char *ha_hook_event_name(yyjson_val *root) {
10061011
const char *event = ha_obj_str(root, "hook_event_name");
10071012
return event ? event : ha_obj_str(root, "hookEventName");
@@ -1459,6 +1464,41 @@ static bool ha_invocation_supported(ha_lifecycle_dialect_t dialect, const char *
14591464
return !forced_event || ha_dialect_event_supported(dialect, forced_event);
14601465
}
14611466

1467+
#ifdef CBM_CLI_ENABLE_TEST_API
1468+
static CBM_TLS int *g_ha_worktree_gate_counter = NULL;
1469+
#endif
1470+
1471+
/* Project resolution once ignore_worktrees is on; the caller runs this only
1472+
* after reading the key, so the default path does none of it. A symlink alias
1473+
* has no .git of its own, and walking its lexical parents leaves the linked
1474+
* worktree. index_repository canonicalizes before the same gate, so the hook
1475+
* must too or it tells the agent to index a path the setting will refuse. The
1476+
* setting refuses a linked worktree even when it sits inside an indexed
1477+
* checkout or inside another linked worktree, so a match is kept only when its
1478+
* root belongs to this cwd's own linked worktree, not a parent graph. */
1479+
static char *ha_resolve_project_ignoring_worktrees(cbm_mcp_server_t *srv, const char *cwd,
1480+
bool *drop_ancestor, bool *worktree_ignored) {
1481+
#ifdef CBM_CLI_ENABLE_TEST_API
1482+
if (g_ha_worktree_gate_counter) {
1483+
(*g_ha_worktree_gate_counter)++;
1484+
}
1485+
#endif
1486+
char canonical_cwd[4096];
1487+
if (ha_canonical_path(cwd, canonical_cwd, sizeof(canonical_cwd))) {
1488+
cwd = canonical_cwd;
1489+
}
1490+
char project_root[4096];
1491+
project_root[0] = '\0';
1492+
char *project =
1493+
ha_resolve_indexed_project_with_root(srv, cwd, project_root, sizeof(project_root));
1494+
bool linked = cbm_git_is_linked_worktree(cwd);
1495+
/* Drop a parent graph without a second free(): this file's raw free count
1496+
* is ratcheted, and the caller's single free(project) still releases it. */
1497+
*drop_ancestor = linked && project && !cbm_git_same_linked_worktree(cwd, project_root);
1498+
*worktree_ignored = linked && (!project || *drop_ancestor);
1499+
return project;
1500+
}
1501+
14621502
static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root,
14631503
const char *forced_event, ha_lifecycle_dialect_t dialect) {
14641504
if (!root || !yyjson_is_obj(root)) {
@@ -1471,47 +1511,24 @@ static char *ha_lifecycle_json_from_root(cbm_mcp_server_t *srv, yyjson_val *root
14711511

14721512
char cwd_buffer[4096];
14731513
cbm_mcp_server_t *owned_server = NULL;
1474-
cbm_config_t *owned_config = NULL;
14751514
if (!srv) {
14761515
owned_server = cbm_mcp_server_new(NULL);
14771516
srv = owned_server;
14781517
}
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-
}
14881518
const char *cwd = ha_normalized_cwd_with_server(root, srv, cwd_buffer, sizeof(cwd_buffer));
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);
1519+
/* Read ignore_worktrees first and do nothing else for it while it is off.
1520+
* cbm_mcp_server_new(NULL) has no store, and srv may be the daemon's
1521+
* session server, so the read leaves srv untouched (see
1522+
* cbm_mcp_ignore_worktrees_runtime_enabled). Off, this is the resolution
1523+
* the hook did before the key existed. */
1524+
bool drop_ancestor = false;
1525+
bool worktree_ignored = false;
1526+
char *project = NULL;
1527+
if (srv && cwd && cbm_mcp_ignore_worktrees_runtime_enabled(srv)) {
1528+
project =
1529+
ha_resolve_project_ignoring_worktrees(srv, cwd, &drop_ancestor, &worktree_ignored);
1530+
} else {
1531+
project = srv && cwd ? ha_resolve_indexed_project(srv, cwd) : NULL;
15151532
}
15161533
cbm_mcp_server_free(owned_server);
15171534

@@ -1670,6 +1687,10 @@ const char *cbm_hook_no_project_index_guidance_for_testing(const char *event,
16701687
return ha_no_project_index_guidance(event, worktree_ignored);
16711688
}
16721689

1690+
void cbm_hook_set_worktree_gate_counter_for_testing(int *counter) {
1691+
g_ha_worktree_gate_counter = counter;
1692+
}
1693+
16731694
bool cbm_hook_augment_parse_bash_pattern_for_testing(const char *cmd, char *out, size_t out_sz) {
16741695
return ha_parse_bash_search_pattern(cmd, out, out_sz);
16751696
}

‎src/mcp/mcp.c‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,17 +1849,6 @@ void cbm_mcp_server_set_config(cbm_mcp_server_t *srv, struct cbm_config *cfg) {
18491849
}
18501850
}
18511851

1852-
struct cbm_config *cbm_mcp_server_attach_runtime_config(cbm_mcp_server_t *srv) {
1853-
if (!srv || srv->config) {
1854-
return NULL;
1855-
}
1856-
cbm_config_t *config = cbm_config_open(cbm_resolve_cache_dir());
1857-
if (config) {
1858-
cbm_mcp_server_set_config(srv, config);
1859-
}
1860-
return config;
1861-
}
1862-
18631852
#ifdef CBM_ENABLE_TEST_SEAMS
18641853
void cbm_mcp_server_set_auto_index_count_test_hook(cbm_mcp_server_t *srv,
18651854
cbm_mcp_auto_index_count_test_hook_fn hook,
@@ -17928,6 +17917,33 @@ bool cbm_mcp_ignore_worktrees_enabled(const cbm_mcp_server_t *srv) {
1792817917
return cbm_config_get_bool(srv->config, CBM_CONFIG_IGNORE_WORKTREES, false);
1792917918
}
1793017919

17920+
/* The same key for a caller whose server may have no store: the stand-alone
17921+
* lifecycle hook builds one with cbm_mcp_server_new(NULL), and the daemon lends
17922+
* its session server, which this must not modify. A server with a store answers
17923+
* from it. Otherwise the runtime store is opened only to read this key, and only
17924+
* when it exists: a missing store leaves every key at its default, so this read
17925+
* never creates one. */
17926+
bool cbm_mcp_ignore_worktrees_runtime_enabled(const cbm_mcp_server_t *srv) {
17927+
if (srv && srv->config) {
17928+
return cbm_mcp_ignore_worktrees_enabled(srv);
17929+
}
17930+
const char *cache_dir = cbm_resolve_cache_dir();
17931+
if (!cache_dir) {
17932+
return false;
17933+
}
17934+
char store_path[CBM_SZ_4K];
17935+
int written = snprintf(store_path, sizeof(store_path), "%s/" CBM_CONFIG_DB_FILENAME, cache_dir);
17936+
cbm_path_info_t info;
17937+
if (written < 0 || (size_t)written >= sizeof(store_path) ||
17938+
cbm_path_info_utf8(store_path, &info) != CBM_PATH_INFO_OK || !info.is_regular) {
17939+
return false;
17940+
}
17941+
cbm_config_t *config = cbm_config_open(cache_dir);
17942+
bool enabled = config && cbm_config_get_bool(config, CBM_CONFIG_IGNORE_WORKTREES, false);
17943+
cbm_config_close(config);
17944+
return enabled;
17945+
}
17946+
1793117947
/* Register the session project with the background watcher for ongoing
1793217948
* change detection — unless auto_watch is disabled. */
1793317949
static void register_watcher_if_enabled(cbm_mcp_server_t *srv) {

‎src/mcp/mcp.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,6 @@ void cbm_mcp_server_set_watcher(cbm_mcp_server_t *srv, struct cbm_watcher *w);
182182
/* Set external config store reference (for auto_index setting). Not owned. */
183183
void cbm_mcp_server_set_config(cbm_mcp_server_t *srv, struct cbm_config *cfg);
184184

185-
/* Open the runtime config and attach it when srv has none. The returned store
186-
* is owned by the caller, who must clear it with cbm_mcp_server_set_config
187-
* before closing. Returns NULL when a config is already set or the store
188-
* cannot be opened. */
189-
struct cbm_config *cbm_mcp_server_attach_runtime_config(cbm_mcp_server_t *srv);
190-
191185
/* Set an explicit session context for an embedded/daemon-backed server.
192186
* session_root is copied and its project name is derived using the same naming
193187
* rule as indexing. allowed_root is copied when non-NULL; an explicit NULL

‎src/mcp/mcp_internal.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ bool cbm_mcp_auto_index_within_file_limit(const char *root_path, int file_limit,
6868
* automatic index of a linked worktree should be skipped. */
6969
bool cbm_mcp_ignore_worktrees_enabled(const cbm_mcp_server_t *srv);
7070

71+
/* The same key when srv may have no store (the lifecycle hook). Never attaches
72+
* a store to srv and never creates the runtime store; reads it only when it
73+
* already exists. */
74+
bool cbm_mcp_ignore_worktrees_runtime_enabled(const cbm_mcp_server_t *srv);
75+
7176
/* detect_changes seed scoping (#1363): does `node`'s line range overlap any
7277
* recorded hunk for `file`? Exposed for direct unit testing of the overlap
7378
* logic, independent of the git/subprocess/index plumbing around it. */

‎tests/test_cli.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <foundation/platform.h>
2727
#include <foundation/private_file_lock.h>
2828
#include <foundation/sha256.h>
29+
#include <foundation/str_util.h>
2930
#include <mcp/mcp.h>
3031
#include <mcp/index_supervisor.h>
3132
#include <pipeline/pipeline.h>
@@ -10944,6 +10945,96 @@ TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent) {
1094410945
FAIL("the indexed main checkout must still resolve in the same matrix");
1094510946
PASS();
1094610947
}
10948+
10949+
/* With ignore_worktrees unset, the lifecycle hook must do what it did before
10950+
* the key existed. It may read the key, but it must not create a runtime
10951+
* config store just to find the key unset, and it must not canonicalize the
10952+
* cwd or probe for a linked worktree. Turning the key on is the positive
10953+
* control: the same call then reaches the worktree gate exactly once. */
10954+
TEST(cli_hook_ignore_worktrees_default_off_does_no_worktree_work) {
10955+
char tmpdir[256];
10956+
char *raw = th_mktempdir("cbm_hook_wt_off");
10957+
if (!raw)
10958+
FAIL("th_mktempdir returned NULL");
10959+
snprintf(tmpdir, sizeof(tmpdir), "%s", raw);
10960+
char cache[512];
10961+
char maindir[512];
10962+
char wtdir[512];
10963+
char store_path[600];
10964+
snprintf(cache, sizeof(cache), "%s/cache", tmpdir);
10965+
snprintf(maindir, sizeof(maindir), "%s/checkout-main", tmpdir);
10966+
snprintf(wtdir, sizeof(wtdir), "%s/checkout-side", tmpdir);
10967+
snprintf(store_path, sizeof(store_path), "%s/" CBM_CONFIG_DB_FILENAME, cache);
10968+
test_mkdirp(cache);
10969+
test_mkdirp(maindir);
10970+
10971+
char cmd[2048];
10972+
snprintf(cmd, sizeof(cmd),
10973+
"cd \"%s\" && git init -q && git config user.email t@t && git config user.name t && "
10974+
": > f.txt && git add f.txt && git commit -qm init && "
10975+
"git worktree add -q \"%s\" -b wtoff",
10976+
maindir, wtdir);
10977+
if (system(cmd) != 0) {
10978+
th_rmtree(tmpdir);
10979+
SKIP_PLATFORM("git worktree add unavailable");
10980+
}
10981+
10982+
char *saved_cache = save_test_env("CBM_CACHE_DIR");
10983+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
10984+
10985+
char input[1024];
10986+
snprintf(input, sizeof(input), "{\"hook_event_name\":\"SessionStart\",\"cwd\":\"%s\"}", wtdir);
10987+
struct stat st;
10988+
int gate_calls = 0;
10989+
cbm_hook_set_worktree_gate_counter_for_testing(&gate_calls);
10990+
bool store_absent_before = stat(store_path, &st) != 0;
10991+
char *off_out = cbm_hook_augment_lifecycle_json(input);
10992+
bool off_note = off_out && strstr(off_out, "Run index_repository") &&
10993+
!strstr(off_out, "ignore_worktrees is enabled");
10994+
free(off_out);
10995+
bool store_absent_after = stat(store_path, &st) != 0;
10996+
int off_gate_calls = gate_calls;
10997+
10998+
/* A store that leaves the key unset may be read for the key, nothing more. */
10999+
cbm_config_t *cfg = cbm_config_open(cache);
11000+
bool other_key_set = cfg && cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, "true") == 0;
11001+
cbm_config_close(cfg);
11002+
gate_calls = 0;
11003+
char *unset_out = other_key_set ? cbm_hook_augment_lifecycle_json(input) : NULL;
11004+
bool unset_note = unset_out && strstr(unset_out, "Run index_repository") &&
11005+
!strstr(unset_out, "ignore_worktrees is enabled");
11006+
free(unset_out);
11007+
int unset_gate_calls = gate_calls;
11008+
11009+
cfg = cbm_config_open(cache);
11010+
bool enabled = cfg && cbm_config_set(cfg, CBM_CONFIG_IGNORE_WORKTREES, "true") == 0;
11011+
cbm_config_close(cfg);
11012+
gate_calls = 0;
11013+
char *on_out = enabled ? cbm_hook_augment_lifecycle_json(input) : NULL;
11014+
bool on_note = on_out && strstr(on_out, "ignore_worktrees is enabled");
11015+
free(on_out);
11016+
int on_gate_calls = gate_calls;
11017+
cbm_hook_set_worktree_gate_counter_for_testing(NULL);
11018+
11019+
restore_test_env("CBM_CACHE_DIR", saved_cache);
11020+
snprintf(cmd, sizeof(cmd), "cd \"%s\" && git worktree remove --force \"%s\" >/dev/null 2>&1",
11021+
maindir, wtdir);
11022+
(void)system(cmd);
11023+
th_rmtree(tmpdir);
11024+
11025+
ASSERT_TRUE(store_absent_before);
11026+
ASSERT_TRUE(off_note);
11027+
if (!store_absent_after)
11028+
FAIL("a default-off lifecycle event must not create the runtime config store");
11029+
ASSERT_EQ(off_gate_calls, 0);
11030+
ASSERT_TRUE(other_key_set);
11031+
ASSERT_TRUE(unset_note);
11032+
ASSERT_EQ(unset_gate_calls, 0);
11033+
ASSERT_TRUE(enabled);
11034+
ASSERT_TRUE(on_note);
11035+
ASSERT_EQ(on_gate_calls, 1);
11036+
PASS();
11037+
}
1094711038
#endif
1094811039

1094911040
TEST(cli_hook_session_sanitizes_untrusted_project_metadata) {
@@ -16975,6 +17066,7 @@ SUITE(cli) {
1697517066
#ifndef _WIN32
1697617067
RUN_TEST(cli_hook_worktree_cwd_resolves_own_indexed_project);
1697717068
RUN_TEST(cli_hook_unindexed_worktree_reports_no_match_not_silent);
17069+
RUN_TEST(cli_hook_ignore_worktrees_default_off_does_no_worktree_work);
1697817070
#endif
1697917071
RUN_TEST(cli_hook_session_sanitizes_untrusted_project_metadata);
1698017072
RUN_TEST(cli_hook_ownership_requires_exact_command_identity);

0 commit comments

Comments
 (0)