@@ -2121,6 +2121,74 @@ static int cbm_remove_session_hooks(const char *settings_path) {
21212121 return rc ;
21222122}
21232123
2124+ /* SubagentStart hook: subagents spawned via the Agent tool do NOT fire
2125+ * SessionStart, so the SessionStart reminder above never reaches them. This
2126+ * hook is their equivalent. Unlike SessionStart (where plain stdout is injected
2127+ * as context), SubagentStart injects context only via a JSON object on stdout:
2128+ * {"hookSpecificOutput":{"hookEventName":"SubagentStart","additionalContext":"…"}}
2129+ * The text is a leaner variant of the SessionStart protocol: it omits the
2130+ * "run index_repository first" step, since the parent session has already
2131+ * indexed the project. Matcher "*" fires for every agent type. */
2132+ #define CMM_SUBAGENT_REMINDER_SCRIPT "cbm-subagent-reminder"
2133+
2134+ static void cbm_install_subagent_reminder_script (const char * home ) {
2135+ if (!home ) {
2136+ return ;
2137+ }
2138+ char config_dir [CLI_BUF_1K ];
2139+ cbm_claude_config_dir (home , config_dir , sizeof (config_dir ));
2140+ if (!config_dir [0 ]) {
2141+ return ;
2142+ }
2143+ char hooks_dir [CLI_BUF_1K ];
2144+ snprintf (hooks_dir , sizeof (hooks_dir ), "%s/hooks" , config_dir );
2145+ cbm_mkdir_p (hooks_dir , CLI_OCTAL_PERM );
2146+
2147+ char script_path [CLI_BUF_1K ];
2148+ snprintf (script_path , sizeof (script_path ), "%s/" CMM_SUBAGENT_REMINDER_SCRIPT , hooks_dir );
2149+
2150+ FILE * f = fopen (script_path , "w" );
2151+ if (!f ) {
2152+ return ;
2153+ }
2154+ /* The additionalContext value is a single line with no embedded quotes,
2155+ * backslashes, or newlines, so the JSON below is valid as written — no
2156+ * runtime escaping (and no python3/jq dependency) is required. */
2157+ (void )fprintf (f ,
2158+ "#!/bin/bash\n"
2159+ "# SubagentStart hook: tell subagents to use codebase-memory-mcp tools.\n"
2160+ "# Installed by codebase-memory-mcp. Fires when any subagent is spawned.\n"
2161+ "# SubagentStart injects context via JSON additionalContext, not plain stdout.\n"
2162+ "cat << 'REMINDER'\n"
2163+ "{\"hookSpecificOutput\":{\"hookEventName\":\"SubagentStart\","
2164+ "\"additionalContext\":\"Code discovery: prefer codebase-memory-mcp tools "
2165+ "(search_graph, trace_path, get_code_snippet, query_graph, get_architecture, "
2166+ "search_code) over grep/file-read for navigating code. Use Grep/Glob/Read for "
2167+ "text, configs, and non-code files.\"}}\n"
2168+ "REMINDER\n" );
2169+ #ifndef _WIN32
2170+ fchmod (fileno (f ), CLI_OCTAL_PERM );
2171+ #endif
2172+ (void )fclose (f );
2173+ #ifdef _WIN32
2174+ chmod (script_path , CLI_OCTAL_PERM );
2175+ #endif
2176+ }
2177+
2178+ int cbm_upsert_claude_subagent_hooks (const char * settings_path ) {
2179+ char command [CLI_BUF_1K ];
2180+ cbm_resolve_hook_command (CMM_SUBAGENT_REMINDER_SCRIPT , command , sizeof (command ));
2181+ return upsert_hooks_json ((hooks_upsert_args_t ){.settings_path = settings_path ,
2182+ .hook_event = "SubagentStart" ,
2183+ .matcher_str = "*" ,
2184+ .command_str = command });
2185+ }
2186+
2187+ int cbm_remove_claude_subagent_hooks (const char * settings_path ) {
2188+ return remove_hooks_json ((hooks_remove_args_t ){
2189+ .settings_path = settings_path , .hook_event = "SubagentStart" , .matcher_str = "*" });
2190+ }
2191+
21242192/* Matcher excludes read_file for consistency with the Claude fix: the hook
21252193 * is an advisory reminder, not a gate over the agent's file reads. */
21262194#define GEMINI_HOOK_MATCHER "google_search|grep_search"
@@ -3116,6 +3184,8 @@ static void install_claude_code_config(const char *home, const char *binary_path
31163184 plan_record ("Claude Code" , "hook" , p );
31173185 snprintf (p , sizeof (p ), "%s/hooks/%s" , config_dir , CMM_SESSION_REMINDER_SCRIPT );
31183186 plan_record ("Claude Code" , "hook" , p );
3187+ snprintf (p , sizeof (p ), "%s/hooks/%s" , config_dir , CMM_SUBAGENT_REMINDER_SCRIPT );
3188+ plan_record ("Claude Code" , "hook" , p );
31193189 return ;
31203190 }
31213191
@@ -3149,9 +3219,12 @@ static void install_claude_code_config(const char *home, const char *binary_path
31493219 cbm_install_hook_gate_script (home , binary_path );
31503220 cbm_install_session_reminder_script (home );
31513221 cbm_upsert_session_hooks (settings_path );
3222+ cbm_install_subagent_reminder_script (home );
3223+ cbm_upsert_claude_subagent_hooks (settings_path );
31523224 }
31533225 printf (" hooks: PreToolUse (Grep/Glob search-graph augmenter, non-blocking)\n" );
31543226 printf (" hooks: SessionStart (MCP usage reminder on startup/resume/clear/compact)\n" );
3227+ printf (" hooks: SubagentStart (MCP usage reminder for subagents)\n" );
31553228
31563229 /* Migration nudge: when CLAUDE_CONFIG_DIR is set and a legacy ~/.claude tree
31573230 * still exists, mention it so users can clean up stale artifacts. */
@@ -3774,8 +3847,9 @@ static void uninstall_claude_code(const char *home, bool dry_run) {
37743847 if (!dry_run ) {
37753848 cbm_remove_claude_hooks (settings_path );
37763849 cbm_remove_session_hooks (settings_path );
3850+ cbm_remove_claude_subagent_hooks (settings_path );
37773851 }
3778- printf (" removed PreToolUse + SessionStart hooks\n" );
3852+ printf (" removed PreToolUse + SessionStart + SubagentStart hooks\n" );
37793853}
37803854
37813855/* Remove MCP + instructions for a generic agent. */
0 commit comments