Skip to content

Commit 64af0b8

Browse files
committed
feat(install): inject CMM code-discovery reminder into Claude subagents
Subagents spawned via the Agent tool do not fire SessionStart, so the existing SessionStart reminder never reaches them — they start without the codebase-memory-mcp code-discovery guidance and fall back to grep/file-read. Register a Claude Code SubagentStart hook (matcher "*") that injects a leaner variant of the protocol via JSON additionalContext, omitting the index_repository step since the parent session has already indexed the project. SubagentStart injects context only via a JSON object on stdout, not plain text, so the generated script emits a static JSON literal — no runtime escaping and no python3/jq dependency. Advisory only; installed and removed alongside the SessionStart hook in install_claude_code_config. Signed-off-by: Shane McCarron <shane.mccarron@corvexconnect.com>
1 parent b1a646e commit 64af0b8

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎src/cli/cli.c‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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. */

‎src/cli/cli.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ int cbm_remove_codex_hooks(const char *config_path);
213213
int cbm_upsert_gemini_session_hooks(const char *settings_path);
214214
int cbm_remove_gemini_session_hooks(const char *settings_path);
215215

216+
/* Install/remove a Claude Code SubagentStart reminder hook in settings.json.
217+
* Subagents spawned via the Agent tool do not fire SessionStart, so this is the
218+
* channel that gives them the same code-discovery guidance. Non-blocking; the
219+
* hook injects context via JSON additionalContext. Returns 0 on success. */
220+
int cbm_upsert_claude_subagent_hooks(const char *settings_path);
221+
int cbm_remove_claude_subagent_hooks(const char *settings_path);
222+
216223
/* ── PATH management ──────────────────────────────────────────── */
217224

218225
/* Append an export PATH line to the given rc file.

0 commit comments

Comments
 (0)