@@ -1795,9 +1795,15 @@ static const char *const cmm_gemini_old_matchers[] = {
17951795 NULL ,
17961796};
17971797
1798- /* Check if a hook array entry is ours (current matcher or a known old one). */
1798+ /* Check if a hook array entry is ours (current matcher or a known old one).
1799+ * When require_command_substr is non-NULL, the matcher match is not sufficient:
1800+ * the entry must ALSO carry a hooks[].command containing that substring. This
1801+ * disambiguates our entry from a user's own hook that happens to share the same
1802+ * matcher (notably "*", which a user is likely to pick for a catch-all hook), so
1803+ * upsert/remove never clobber a foreign entry. NULL preserves matcher-only
1804+ * matching for callers whose matcher is already CMM-specific (e.g. "startup"). */
17991805static bool is_cmm_hook_entry (yyjson_mut_val * entry , const char * matcher_str ,
1800- const char * const * old_matchers ) {
1806+ const char * const * old_matchers , const char * require_command_substr ) {
18011807 yyjson_mut_val * matcher = yyjson_mut_obj_get (entry , "matcher" );
18021808 if (!matcher || !yyjson_mut_is_str (matcher )) {
18031809 return false;
@@ -1806,16 +1812,36 @@ static bool is_cmm_hook_entry(yyjson_mut_val *entry, const char *matcher_str,
18061812 if (!val ) {
18071813 return false;
18081814 }
1809- if (strcmp (val , matcher_str ) == 0 ) {
1810- return true;
1811- }
1815+ bool matcher_ok = strcmp (val , matcher_str ) == 0 ;
18121816 /* Also match old versions for backwards-compatible upgrade */
1813- for (int i = 0 ; old_matchers && old_matchers [i ]; i ++ ) {
1817+ for (int i = 0 ; ! matcher_ok && old_matchers && old_matchers [i ]; i ++ ) {
18141818 if (strcmp (val , old_matchers [i ]) == 0 ) {
1815- return true;
1819+ matcher_ok = true;
18161820 }
18171821 }
1818- return false;
1822+ if (!matcher_ok ) {
1823+ return false;
1824+ }
1825+ if (require_command_substr ) {
1826+ yyjson_mut_val * hooks = yyjson_mut_obj_get (entry , "hooks" );
1827+ if (!hooks || !yyjson_mut_is_arr (hooks )) {
1828+ return false;
1829+ }
1830+ size_t idx ;
1831+ size_t max ;
1832+ yyjson_mut_val * h ;
1833+ yyjson_mut_arr_foreach (hooks , idx , max , h ) {
1834+ yyjson_mut_val * cmd = yyjson_mut_obj_get (h , "command" );
1835+ if (cmd && yyjson_mut_is_str (cmd )) {
1836+ const char * cs = yyjson_mut_get_str (cmd );
1837+ if (cs && strstr (cs , require_command_substr )) {
1838+ return true;
1839+ }
1840+ }
1841+ }
1842+ return false;
1843+ }
1844+ return true;
18191845}
18201846
18211847/* Generic hook upsert for both Claude Code and Gemini CLI */
@@ -1825,8 +1851,10 @@ typedef struct {
18251851 const char * hook_event ;
18261852 const char * matcher_str ;
18271853 const char * command_str ;
1828- const char * const * old_matchers ; /* NULL-terminated; may be NULL */
1829- int timeout_sec ; /* >0 adds "timeout" to the hook entry */
1854+ const char * const * old_matchers ; /* NULL-terminated; may be NULL */
1855+ int timeout_sec ; /* >0 adds "timeout" to the hook entry */
1856+ const char * match_command_substr ; /* non-NULL: also require this in the
1857+ * entry command to claim ownership */
18301858} hooks_upsert_args_t ;
18311859static int upsert_hooks_json (hooks_upsert_args_t args ) {
18321860 const char * settings_path = args .settings_path ;
@@ -1876,7 +1904,7 @@ static int upsert_hooks_json(hooks_upsert_args_t args) {
18761904 size_t max ;
18771905 yyjson_mut_val * item ;
18781906 yyjson_mut_arr_foreach (event_arr , idx , max , item ) {
1879- if (is_cmm_hook_entry (item , matcher_str , old_matchers )) {
1907+ if (is_cmm_hook_entry (item , matcher_str , old_matchers , args . match_command_substr )) {
18801908 yyjson_mut_arr_remove (event_arr , idx );
18811909 break ;
18821910 }
@@ -1909,7 +1937,9 @@ typedef struct {
19091937 const char * settings_path ;
19101938 const char * hook_event ;
19111939 const char * matcher_str ;
1912- const char * const * old_matchers ; /* NULL-terminated; may be NULL */
1940+ const char * const * old_matchers ; /* NULL-terminated; may be NULL */
1941+ const char * match_command_substr ; /* non-NULL: also require this in the
1942+ * entry command to claim ownership */
19131943} hooks_remove_args_t ;
19141944static int remove_hooks_json (hooks_remove_args_t args ) {
19151945 const char * settings_path = args .settings_path ;
@@ -1950,7 +1980,7 @@ static int remove_hooks_json(hooks_remove_args_t args) {
19501980 size_t max ;
19511981 yyjson_mut_val * item ;
19521982 yyjson_mut_arr_foreach (event_arr , idx , max , item ) {
1953- if (is_cmm_hook_entry (item , matcher_str , old_matchers )) {
1983+ if (is_cmm_hook_entry (item , matcher_str , old_matchers , args . match_command_substr )) {
19541984 yyjson_mut_arr_remove (event_arr , idx );
19551985 break ;
19561986 }
@@ -2121,6 +2151,82 @@ static int cbm_remove_session_hooks(const char *settings_path) {
21212151 return rc ;
21222152}
21232153
2154+ /* SubagentStart hook: subagents spawned via the Agent tool do NOT fire
2155+ * SessionStart, so the SessionStart reminder above never reaches them. This
2156+ * hook is their equivalent. Unlike SessionStart (where plain stdout is injected
2157+ * as context), SubagentStart injects context only via a JSON object on stdout:
2158+ * {"hookSpecificOutput":{"hookEventName":"SubagentStart","additionalContext":"…"}}
2159+ * The text is a leaner variant of the SessionStart protocol: it omits the
2160+ * "run index_repository first" step, since the parent session has already
2161+ * indexed the project. Matcher "*" fires for every agent type. */
2162+ #define CMM_SUBAGENT_REMINDER_SCRIPT "cbm-subagent-reminder"
2163+
2164+ static void cbm_install_subagent_reminder_script (const char * home ) {
2165+ if (!home ) {
2166+ return ;
2167+ }
2168+ char config_dir [CLI_BUF_1K ];
2169+ cbm_claude_config_dir (home , config_dir , sizeof (config_dir ));
2170+ if (!config_dir [0 ]) {
2171+ return ;
2172+ }
2173+ char hooks_dir [CLI_BUF_1K ];
2174+ snprintf (hooks_dir , sizeof (hooks_dir ), "%s/hooks" , config_dir );
2175+ cbm_mkdir_p (hooks_dir , CLI_OCTAL_PERM );
2176+
2177+ char script_path [CLI_BUF_1K ];
2178+ snprintf (script_path , sizeof (script_path ), "%s/" CMM_SUBAGENT_REMINDER_SCRIPT , hooks_dir );
2179+
2180+ FILE * f = fopen (script_path , "w" );
2181+ if (!f ) {
2182+ return ;
2183+ }
2184+ /* The additionalContext value is a single line with no embedded quotes,
2185+ * backslashes, or newlines, so the JSON below is valid as written — no
2186+ * runtime escaping (and no python3/jq dependency) is required. */
2187+ (void )fprintf (f ,
2188+ "#!/bin/bash\n"
2189+ "# SubagentStart hook: tell subagents to use codebase-memory-mcp tools.\n"
2190+ "# Installed by codebase-memory-mcp. Fires when any subagent is spawned.\n"
2191+ "# SubagentStart injects context via JSON additionalContext, not plain stdout.\n"
2192+ "cat << 'REMINDER'\n"
2193+ "{\"hookSpecificOutput\":{\"hookEventName\":\"SubagentStart\","
2194+ "\"additionalContext\":\"Code discovery: prefer codebase-memory-mcp tools "
2195+ "(search_graph, trace_path, get_code_snippet, query_graph, get_architecture, "
2196+ "search_code) over grep/file-read for navigating code. Use Grep/Glob/Read for "
2197+ "text, configs, and non-code files.\"}}\n"
2198+ "REMINDER\n" );
2199+ #ifndef _WIN32
2200+ fchmod (fileno (f ), CLI_OCTAL_PERM );
2201+ #endif
2202+ (void )fclose (f );
2203+ #ifdef _WIN32
2204+ chmod (script_path , CLI_OCTAL_PERM );
2205+ #endif
2206+ }
2207+
2208+ int cbm_upsert_claude_subagent_hooks (const char * settings_path ) {
2209+ char command [CLI_BUF_1K ];
2210+ cbm_resolve_hook_command (CMM_SUBAGENT_REMINDER_SCRIPT , command , sizeof (command ));
2211+ /* matcher "*" is the natural choice a user would also pick for their own
2212+ * catch-all SubagentStart hook, so claim ownership by command too — never
2213+ * clobber or remove a foreign "*" entry. */
2214+ return upsert_hooks_json (
2215+ (hooks_upsert_args_t ){.settings_path = settings_path ,
2216+ .hook_event = "SubagentStart" ,
2217+ .matcher_str = "*" ,
2218+ .command_str = command ,
2219+ .match_command_substr = CMM_SUBAGENT_REMINDER_SCRIPT });
2220+ }
2221+
2222+ int cbm_remove_claude_subagent_hooks (const char * settings_path ) {
2223+ return remove_hooks_json (
2224+ (hooks_remove_args_t ){.settings_path = settings_path ,
2225+ .hook_event = "SubagentStart" ,
2226+ .matcher_str = "*" ,
2227+ .match_command_substr = CMM_SUBAGENT_REMINDER_SCRIPT });
2228+ }
2229+
21242230/* Matcher excludes read_file for consistency with the Claude fix: the hook
21252231 * is an advisory reminder, not a gate over the agent's file reads. */
21262232#define GEMINI_HOOK_MATCHER "google_search|grep_search"
@@ -3116,6 +3222,8 @@ static void install_claude_code_config(const char *home, const char *binary_path
31163222 plan_record ("Claude Code" , "hook" , p );
31173223 snprintf (p , sizeof (p ), "%s/hooks/%s" , config_dir , CMM_SESSION_REMINDER_SCRIPT );
31183224 plan_record ("Claude Code" , "hook" , p );
3225+ snprintf (p , sizeof (p ), "%s/hooks/%s" , config_dir , CMM_SUBAGENT_REMINDER_SCRIPT );
3226+ plan_record ("Claude Code" , "hook" , p );
31193227 return ;
31203228 }
31213229
@@ -3149,9 +3257,12 @@ static void install_claude_code_config(const char *home, const char *binary_path
31493257 cbm_install_hook_gate_script (home , binary_path );
31503258 cbm_install_session_reminder_script (home );
31513259 cbm_upsert_session_hooks (settings_path );
3260+ cbm_install_subagent_reminder_script (home );
3261+ cbm_upsert_claude_subagent_hooks (settings_path );
31523262 }
31533263 printf (" hooks: PreToolUse (Grep/Glob search-graph augmenter, non-blocking)\n" );
31543264 printf (" hooks: SessionStart (MCP usage reminder on startup/resume/clear/compact)\n" );
3265+ printf (" hooks: SubagentStart (MCP usage reminder for subagents)\n" );
31553266
31563267 /* Migration nudge: when CLAUDE_CONFIG_DIR is set and a legacy ~/.claude tree
31573268 * still exists, mention it so users can clean up stale artifacts. */
@@ -3774,8 +3885,9 @@ static void uninstall_claude_code(const char *home, bool dry_run) {
37743885 if (!dry_run ) {
37753886 cbm_remove_claude_hooks (settings_path );
37763887 cbm_remove_session_hooks (settings_path );
3888+ cbm_remove_claude_subagent_hooks (settings_path );
37773889 }
3778- printf (" removed PreToolUse + SessionStart hooks\n" );
3890+ printf (" removed PreToolUse + SessionStart + SubagentStart hooks\n" );
37793891}
37803892
37813893/* Remove MCP + instructions for a generic agent. */
0 commit comments