@@ -1462,18 +1462,31 @@ int cbm_remove_antigravity_mcp(const char *config_path) {
14621462
14631463/* ── Claude Code pre-tool hooks ───────────────────────────────── */
14641464
1465- #define CMM_HOOK_MATCHER "Grep|Glob|Read|Search"
1465+ /* Matcher intentionally excludes Read: gating Read breaks Claude Code's
1466+ * read-before-edit invariant (issue #362). The hook is a non-blocking
1467+ * augmenter, never a gate. */
1468+ #define CMM_HOOK_MATCHER "Grep|Glob"
14661469#define CMM_HOOK_COMMAND "~/.claude/hooks/cbm-code-discovery-gate"
1470+ /* Hard backstop in settings.json; the binary also self-bounds with an
1471+ * in-process deadline well under this. */
1472+ #define CMM_HOOK_TIMEOUT_SEC 5
14671473
14681474/* Old matcher values from previous versions — recognized during upgrade so
1469- * upsert_hooks_json can remove them before inserting the current matcher. */
1470- static const char * cmm_old_matchers [] = {
1475+ * upsert/remove can clean them up before inserting the current matcher.
1476+ * Per-agent lists (no shared global): each caller passes its own. */
1477+ static const char * const cmm_claude_old_matchers [] = {
1478+ "Grep|Glob|Read|Search" ,
14711479 "Grep|Glob|Read" ,
14721480 NULL ,
14731481};
1482+ static const char * const cmm_gemini_old_matchers [] = {
1483+ "google_search|read_file|grep_search" ,
1484+ NULL ,
1485+ };
14741486
1475- /* Check if a PreToolUse array entry matches our hook (current or old matcher). */
1476- static bool is_cmm_hook_entry (yyjson_mut_val * entry , const char * matcher_str ) {
1487+ /* Check if a hook array entry is ours (current matcher or a known old one). */
1488+ static bool is_cmm_hook_entry (yyjson_mut_val * entry , const char * matcher_str ,
1489+ const char * const * old_matchers ) {
14771490 yyjson_mut_val * matcher = yyjson_mut_obj_get (entry , "matcher" );
14781491 if (!matcher || !yyjson_mut_is_str (matcher )) {
14791492 return false;
@@ -1486,8 +1499,8 @@ static bool is_cmm_hook_entry(yyjson_mut_val *entry, const char *matcher_str) {
14861499 return true;
14871500 }
14881501 /* Also match old versions for backwards-compatible upgrade */
1489- for (int i = 0 ; cmm_old_matchers [i ]; i ++ ) {
1490- if (strcmp (val , cmm_old_matchers [i ]) == 0 ) {
1502+ for (int i = 0 ; old_matchers && old_matchers [i ]; i ++ ) {
1503+ if (strcmp (val , old_matchers [i ]) == 0 ) {
14911504 return true;
14921505 }
14931506 }
@@ -1501,12 +1514,15 @@ typedef struct {
15011514 const char * hook_event ;
15021515 const char * matcher_str ;
15031516 const char * command_str ;
1517+ const char * const * old_matchers ; /* NULL-terminated; may be NULL */
1518+ int timeout_sec ; /* >0 adds "timeout" to the hook entry */
15041519} hooks_upsert_args_t ;
15051520static int upsert_hooks_json (hooks_upsert_args_t args ) {
15061521 const char * settings_path = args .settings_path ;
15071522 const char * hook_event = args .hook_event ;
15081523 const char * matcher_str = args .matcher_str ;
15091524 const char * command_str = args .command_str ;
1525+ const char * const * old_matchers = args .old_matchers ;
15101526 if (!settings_path ) {
15111527 return CLI_ERR ;
15121528 }
@@ -1549,7 +1565,7 @@ static int upsert_hooks_json(hooks_upsert_args_t args) {
15491565 size_t max ;
15501566 yyjson_mut_val * item ;
15511567 yyjson_mut_arr_foreach (event_arr , idx , max , item ) {
1552- if (is_cmm_hook_entry (item , matcher_str )) {
1568+ if (is_cmm_hook_entry (item , matcher_str , old_matchers )) {
15531569 yyjson_mut_arr_remove (event_arr , idx );
15541570 break ;
15551571 }
@@ -1563,6 +1579,9 @@ static int upsert_hooks_json(hooks_upsert_args_t args) {
15631579 yyjson_mut_val * hook_obj = yyjson_mut_obj (mdoc );
15641580 yyjson_mut_obj_add_str (mdoc , hook_obj , "type" , "command" );
15651581 yyjson_mut_obj_add_str (mdoc , hook_obj , "command" , command_str );
1582+ if (args .timeout_sec > 0 ) {
1583+ yyjson_mut_obj_add_int (mdoc , hook_obj , "timeout" , args .timeout_sec );
1584+ }
15661585 yyjson_mut_arr_append (hooks_arr , hook_obj );
15671586 yyjson_mut_obj_add_val (mdoc , entry , "hooks" , hooks_arr );
15681587
@@ -1579,11 +1598,13 @@ typedef struct {
15791598 const char * settings_path ;
15801599 const char * hook_event ;
15811600 const char * matcher_str ;
1601+ const char * const * old_matchers ; /* NULL-terminated; may be NULL */
15821602} hooks_remove_args_t ;
15831603static int remove_hooks_json (hooks_remove_args_t args ) {
15841604 const char * settings_path = args .settings_path ;
15851605 const char * hook_event = args .hook_event ;
15861606 const char * matcher_str = args .matcher_str ;
1607+ const char * const * old_matchers = args .old_matchers ;
15871608 if (!settings_path ) {
15881609 return CLI_ERR ;
15891610 }
@@ -1618,7 +1639,7 @@ static int remove_hooks_json(hooks_remove_args_t args) {
16181639 size_t max ;
16191640 yyjson_mut_val * item ;
16201641 yyjson_mut_arr_foreach (event_arr , idx , max , item ) {
1621- if (is_cmm_hook_entry (item , matcher_str )) {
1642+ if (is_cmm_hook_entry (item , matcher_str , old_matchers )) {
16221643 yyjson_mut_arr_remove (event_arr , idx );
16231644 break ;
16241645 }
@@ -1630,20 +1651,40 @@ static int remove_hooks_json(hooks_remove_args_t args) {
16301651}
16311652
16321653int cbm_upsert_claude_hooks (const char * settings_path ) {
1633- return upsert_hooks_json (
1634- (hooks_upsert_args_t ){settings_path , "PreToolUse" , CMM_HOOK_MATCHER , CMM_HOOK_COMMAND });
1654+ return upsert_hooks_json ((hooks_upsert_args_t ){
1655+ .settings_path = settings_path ,
1656+ .hook_event = "PreToolUse" ,
1657+ .matcher_str = CMM_HOOK_MATCHER ,
1658+ .command_str = CMM_HOOK_COMMAND ,
1659+ .old_matchers = cmm_claude_old_matchers ,
1660+ .timeout_sec = CMM_HOOK_TIMEOUT_SEC ,
1661+ });
16351662}
16361663
16371664int cbm_remove_claude_hooks (const char * settings_path ) {
1638- return remove_hooks_json ((hooks_remove_args_t ){settings_path , "PreToolUse" , CMM_HOOK_MATCHER });
1639- }
1640-
1641- /* Install the code discovery gate script to ~/.claude/hooks/.
1642- * Blocks the first Grep/Glob/Read/Search call per session (exit 2 + stderr),
1643- * nudging Claude toward codebase-memory-mcp. All subsequent calls in the same
1644- * session pass through (gate file keyed on PPID). */
1645- static void cbm_install_hook_gate_script (const char * home ) {
1646- if (!home ) {
1665+ return remove_hooks_json ((hooks_remove_args_t ){
1666+ .settings_path = settings_path ,
1667+ .hook_event = "PreToolUse" ,
1668+ .matcher_str = CMM_HOOK_MATCHER ,
1669+ .old_matchers = cmm_claude_old_matchers ,
1670+ });
1671+ }
1672+
1673+ /* Install the search-augmenter shim to ~/.claude/hooks/.
1674+ * The shim is a thin wrapper that delegates to `<binary> hook-augment`,
1675+ * which adds graph context to Grep/Glob calls. It NEVER blocks a tool call:
1676+ * a missing/old/hung binary results in a silent exit 0 (issue #362/#288).
1677+ * The legacy filename `cbm-code-discovery-gate` is retained so existing
1678+ * settings.json entries and uninstall keep working with zero migration. */
1679+ static void cbm_install_hook_gate_script (const char * home , const char * binary_path ) {
1680+ if (!home || !binary_path ) {
1681+ return ;
1682+ }
1683+ /* Defensive: refuse to embed a binary path containing a double-quote, which
1684+ * would break the BIN="..." shell quoting in the generated shim. In normal
1685+ * installs this is unreachable (paths come from cbm_detect_self_path), but
1686+ * fail-loud here beats silently emitting a malformed script. */
1687+ if (strchr (binary_path , '"' ) != NULL ) {
16471688 return ;
16481689 }
16491690 char hooks_dir [CLI_BUF_1K ];
@@ -1657,22 +1698,18 @@ static void cbm_install_hook_gate_script(const char *home) {
16571698 if (!f ) {
16581699 return ;
16591700 }
1660- (void )fprintf (f , "#!/bin/bash\n"
1661- "# Gate hook: nudges Claude toward codebase-memory-mcp for code discovery.\n"
1662- "# First Grep/Glob/Read/Search per session -> block. Subsequent -> allow.\n"
1663- "# PPID = Claude Code process PID, unique per session.\n"
1664- "GATE=/tmp/cbm-code-discovery-gate-$PPID\n"
1665- "find /tmp -name 'cbm-code-discovery-gate-*' -mtime +1 -delete 2>/dev/null\n"
1666- "if [ -f \"$GATE\" ]; then\n"
1667- " exit 0\n"
1668- "fi\n"
1669- "touch \"$GATE\"\n"
1670- "echo 'BLOCKED: For code discovery, use codebase-memory-mcp tools first: "
1671- "search_graph(name_pattern) to find functions/classes, trace_path() for "
1672- "call chains, get_code_snippet(qualified_name) to read source. If the graph "
1673- "is not indexed yet, call index_repository first. Fall back to Grep/Glob/Read "
1674- "only for text content search. If you need Grep, retry.' >&2\n"
1675- "exit 2\n" );
1701+ (void )fprintf (
1702+ f ,
1703+ "#!/bin/bash\n"
1704+ "# codebase-memory-mcp search augmenter (Claude Code PreToolUse).\n"
1705+ "# NOTE: the legacy filename is kept for zero-migration upgrades.\n"
1706+ "# Despite the name this NEVER blocks a tool call - it only adds\n"
1707+ "# graph context. Any failure is silent (exit 0, no output).\n"
1708+ "BIN=\"%s\"\n"
1709+ "[ -x \"$BIN\" ] || exit 0\n"
1710+ "\"$BIN\" hook-augment 2>/dev/null\n"
1711+ "exit 0\n" ,
1712+ binary_path );
16761713 /* fchmod before close to avoid TOCTOU race (CodeQL cpp/toctou-race-condition) */
16771714#ifndef _WIN32
16781715 fchmod (fileno (f ), CLI_OCTAL_PERM );
@@ -1710,11 +1747,12 @@ static void cbm_install_session_reminder_script(const char *home) {
17101747 "1. ALWAYS use codebase-memory-mcp tools FIRST for ANY code exploration:\n"
17111748 " - search_graph(name_pattern/label/qn_pattern) to find functions/classes/routes\n"
17121749 " - trace_path(function_name, mode=calls|data_flow|cross_service) for call chains\n"
1713- " - get_code_snippet(qualified_name) to read source (NOT Read/cat )\n"
1750+ " - get_code_snippet(qualified_name) for exact symbol source (precise ranges )\n"
17141751 " - query_graph(query) for complex Cypher patterns\n"
17151752 " - get_architecture(aspects) for project structure\n"
17161753 " - search_code(pattern) for text search (graph-augmented grep)\n"
1717- "2. Fall back to Grep/Glob/Read ONLY for text content, config values, non-code files.\n"
1754+ "2. Use Grep/Glob/Read freely for text, configs, non-code files, and\n"
1755+ " always Read a file before editing it.\n"
17181756 "3. If a project is not indexed yet, run index_repository FIRST.\n"
17191757 "REMINDER\n" );
17201758#ifndef _WIN32
@@ -1730,8 +1768,10 @@ static int cbm_upsert_session_hooks(const char *settings_path) {
17301768 static const char * matchers [] = {"startup" , "resume" , "clear" , "compact" };
17311769 int rc = 0 ;
17321770 for (int i = 0 ; i < NUM_DIRS ; i ++ ) {
1733- if (upsert_hooks_json ((hooks_upsert_args_t ){settings_path , "SessionStart" , matchers [i ],
1734- CMM_SESSION_COMMAND }) != 0 ) {
1771+ if (upsert_hooks_json ((hooks_upsert_args_t ){.settings_path = settings_path ,
1772+ .hook_event = "SessionStart" ,
1773+ .matcher_str = matchers [i ],
1774+ .command_str = CMM_SESSION_COMMAND }) != 0 ) {
17351775 rc = CLI_ERR ;
17361776 }
17371777 }
@@ -1742,27 +1782,39 @@ static int cbm_remove_session_hooks(const char *settings_path) {
17421782 static const char * matchers [] = {"startup" , "resume" , "clear" , "compact" };
17431783 int rc = 0 ;
17441784 for (int i = 0 ; i < NUM_DIRS ; i ++ ) {
1745- if (remove_hooks_json ((hooks_remove_args_t ){settings_path , "SessionStart" , matchers [i ]}) !=
1746- 0 ) {
1785+ if (remove_hooks_json ((hooks_remove_args_t ){.settings_path = settings_path ,
1786+ .hook_event = "SessionStart" ,
1787+ .matcher_str = matchers [i ]}) != 0 ) {
17471788 rc = CLI_ERR ;
17481789 }
17491790 }
17501791 return rc ;
17511792}
17521793
1753- #define GEMINI_HOOK_MATCHER "google_search|read_file|grep_search"
1794+ /* Matcher excludes read_file for consistency with the Claude fix: the hook
1795+ * is an advisory reminder, not a gate over the agent's file reads. */
1796+ #define GEMINI_HOOK_MATCHER "google_search|grep_search"
17541797#define GEMINI_HOOK_COMMAND \
17551798 "echo 'Reminder: prefer codebase-memory-mcp search_graph/trace_path/" \
17561799 "get_code_snippet over grep/file search for code discovery.' >&2"
17571800
17581801int cbm_upsert_gemini_hooks (const char * settings_path ) {
1759- return upsert_hooks_json ((hooks_upsert_args_t ){settings_path , "BeforeTool" , GEMINI_HOOK_MATCHER ,
1760- GEMINI_HOOK_COMMAND });
1802+ return upsert_hooks_json ((hooks_upsert_args_t ){
1803+ .settings_path = settings_path ,
1804+ .hook_event = "BeforeTool" ,
1805+ .matcher_str = GEMINI_HOOK_MATCHER ,
1806+ .command_str = GEMINI_HOOK_COMMAND ,
1807+ .old_matchers = cmm_gemini_old_matchers ,
1808+ });
17611809}
17621810
17631811int cbm_remove_gemini_hooks (const char * settings_path ) {
1764- return remove_hooks_json (
1765- (hooks_remove_args_t ){settings_path , "BeforeTool" , GEMINI_HOOK_MATCHER });
1812+ return remove_hooks_json ((hooks_remove_args_t ){
1813+ .settings_path = settings_path ,
1814+ .hook_event = "BeforeTool" ,
1815+ .matcher_str = GEMINI_HOOK_MATCHER ,
1816+ .old_matchers = cmm_gemini_old_matchers ,
1817+ });
17661818}
17671819
17681820/* ── PATH management ──────────────────────────────────────────── */
@@ -2654,11 +2706,11 @@ static void install_claude_code_config(const char *home, const char *binary_path
26542706 snprintf (settings_path , sizeof (settings_path ), "%s/.claude/settings.json" , home );
26552707 if (!dry_run ) {
26562708 cbm_upsert_claude_hooks (settings_path );
2657- cbm_install_hook_gate_script (home );
2709+ cbm_install_hook_gate_script (home , binary_path );
26582710 cbm_install_session_reminder_script (home );
26592711 cbm_upsert_session_hooks (settings_path );
26602712 }
2661- printf (" hooks: PreToolUse (code discovery gate )\n" );
2713+ printf (" hooks: PreToolUse (Grep/Glob search-graph augmenter, non-blocking )\n" );
26622714 printf (" hooks: SessionStart (MCP usage reminder on startup/resume/clear/compact)\n" );
26632715}
26642716
0 commit comments