Skip to content

Commit 5255939

Browse files
committed
fix(install): address QA round 1
QA round 1 (PR DeusData#632) flagged that the SubagentStart hook uses matcher "*", the same matcher a user is most likely to pick for their own catch-all SubagentStart hook. Because is_cmm_hook_entry keyed ownership on the matcher string alone, install would remove the user's "*" entry and replace it with ours, and uninstall could remove the wrong one. Add an optional `match_command_substr` to the upsert/remove args, threaded into is_cmm_hook_entry: when set, an entry must ALSO carry a hooks[].command containing that substring to be claimed as ours. The Claude SubagentStart hook passes "cbm-subagent-reminder"; all existing callers leave it NULL, preserving their matcher-only behavior unchanged. Install/uninstall now only ever touch CMM's own entry and never clobber a foreign "*" hook. Add cli_claude_subagent_hook_preserves_user_entry covering the case: a pre-existing user "*" SubagentStart hook survives both install and uninstall. Signed-off-by: Shane McCarron <shane.mccarron@corvexconnect.com>
1 parent 3466ba0 commit 5255939

2 files changed

Lines changed: 91 additions & 19 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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"). */
17991805
static 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;
18311859
static 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;
19141944
static 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
}
@@ -2178,15 +2208,23 @@ static void cbm_install_subagent_reminder_script(const char *home) {
21782208
int cbm_upsert_claude_subagent_hooks(const char *settings_path) {
21792209
char command[CLI_BUF_1K];
21802210
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});
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});
21852220
}
21862221

21872222
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 = "*"});
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});
21902228
}
21912229

21922230
/* Matcher excludes read_file for consistency with the Claude fix: the hook

‎tests/test_cli.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,39 @@ TEST(cli_claude_subagent_hook) {
17861786
PASS();
17871787
}
17881788

1789+
/* A user's own catch-all ("*") SubagentStart hook must survive CMM install and
1790+
* uninstall: ownership is keyed on the command, not just the "*" matcher. */
1791+
TEST(cli_claude_subagent_hook_preserves_user_entry) {
1792+
char tmpdir[256];
1793+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-subuser-XXXXXX");
1794+
if (!cbm_mkdtemp(tmpdir))
1795+
FAIL("cbm_mkdtemp failed");
1796+
1797+
char cfg[512];
1798+
snprintf(cfg, sizeof(cfg), "%s/settings.json", tmpdir);
1799+
/* Pre-existing user SubagentStart hook, also matcher "*", different command. */
1800+
write_test_file(
1801+
cfg, "{\"hooks\":{\"SubagentStart\":[{\"matcher\":\"*\","
1802+
"\"hooks\":[{\"type\":\"command\",\"command\":\"echo user-subagent-hook\"}]}]}}");
1803+
1804+
/* Install CMM's hook: the user's "*" entry must remain, ours added alongside. */
1805+
ASSERT_EQ(cbm_upsert_claude_subagent_hooks(cfg), 0);
1806+
const char *d = read_test_file(cfg);
1807+
ASSERT_NOT_NULL(d);
1808+
ASSERT(strstr(d, "echo user-subagent-hook") != NULL); /* user's hook untouched */
1809+
ASSERT(strstr(d, "cbm-subagent-reminder") != NULL); /* ours added */
1810+
1811+
/* Remove CMM's hook: the user's entry must still be intact, ours gone. */
1812+
ASSERT_EQ(cbm_remove_claude_subagent_hooks(cfg), 0);
1813+
d = read_test_file(cfg);
1814+
ASSERT_NOT_NULL(d);
1815+
ASSERT(strstr(d, "echo user-subagent-hook") != NULL); /* user's hook preserved */
1816+
ASSERT_NULL(strstr(d, "cbm-subagent-reminder")); /* only ours removed */
1817+
1818+
test_rmdir_r(tmpdir);
1819+
PASS();
1820+
}
1821+
17891822
TEST(cli_detect_agents_finds_gemini) {
17901823
char tmpdir[256];
17911824
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-detect-XXXXXX");
@@ -2868,6 +2901,7 @@ SUITE(cli) {
28682901
RUN_TEST(cli_codex_session_hook_issue330);
28692902
RUN_TEST(cli_gemini_session_hook_parity);
28702903
RUN_TEST(cli_claude_subagent_hook);
2904+
RUN_TEST(cli_claude_subagent_hook_preserves_user_entry);
28712905
RUN_TEST(cli_detect_agents_finds_gemini);
28722906
RUN_TEST(cli_detect_agents_finds_zed);
28732907
RUN_TEST(cli_detect_agents_finds_antigravity);

0 commit comments

Comments
 (0)