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+
10051010static 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+
14621502static 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+
16731694bool 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}
0 commit comments