@@ -3382,54 +3382,32 @@ bool cbm_optional_hook_supported_for_testing(const char *agent_name, bool window
33823382}
33833383#endif
33843384
3385- static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
3386- const char *command_windows) {
3385+ static int cbm_reconcile_codex_hooks_command(const char *config_path, const char *command,
3386+ const char *command_windows,
3387+ cbm_toml_codex_hook_action_t action, bool check_only) {
33873388 if (!config_path || !command || !command_windows) {
33883389 return CLI_ERR;
33893390 }
3390- char escaped[CLI_BUF_8K];
3391- char escaped_windows[CLI_BUF_8K];
3392- if (cbm_toml_escape_basic_string(command, escaped, sizeof(escaped)) != CLI_OK ||
3393- cbm_toml_escape_basic_string(command_windows, escaped_windows, sizeof(escaped_windows)) !=
3394- CLI_OK) {
3395- return CLI_ERR;
3396- }
3397- char block[CLI_BUF_8K];
3398- int written = snprintf(block, sizeof(block),
3399- "[[hooks.SessionStart]]\n"
3400- "matcher = \"startup|resume|clear|compact\"\n\n"
3401- "[[hooks.SessionStart.hooks]]\n"
3402- "type = \"command\"\n"
3403- "command = \"%s\"\n"
3404- "command_windows = \"%s\"\n"
3405- "timeout = 5\n\n"
3406- "[[hooks.SubagentStart]]\n"
3407- "matcher = \"*\"\n\n"
3408- "[[hooks.SubagentStart.hooks]]\n"
3409- "type = \"command\"\n"
3410- "command = \"%s\"\n"
3411- "command_windows = \"%s\"\n"
3412- "timeout = 5\n",
3413- escaped, escaped_windows, escaped, escaped_windows);
3414- if (written < 0 || (size_t)written >= sizeof(block)) {
3415- return CLI_ERR;
3416- }
3417- return cbm_toml_upsert_managed_block(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, block) == 0
3391+ return cbm_toml_reconcile_codex_hooks(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, command,
3392+ command_windows, action, check_only ? 1 : 0) == 0
34183393 ? CLI_OK
34193394 : CLI_ERR;
34203395}
3421-
3396+ static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
3397+ const char *command_windows) {
3398+ return cbm_reconcile_codex_hooks_command(config_path, command, command_windows,
3399+ CBM_TOML_CODEX_HOOK_UPSERT, false);
3400+ }
34223401/* Public path used by config-level regression tests and manual callers. */
34233402int cbm_upsert_codex_hooks(const char *config_path) {
34243403 return cbm_upsert_codex_hooks_command(config_path, "codebase-memory-mcp hook-augment",
34253404 "codebase-memory-mcp hook-augment");
34263405}
34273406
34283407int cbm_remove_codex_hooks(const char *config_path) {
3429- return config_path &&
3430- cbm_toml_remove_managed_block(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END) == 0
3431- ? CLI_OK
3432- : CLI_ERR;
3408+ return cbm_reconcile_codex_hooks_command(config_path, "codebase-memory-mcp hook-augment",
3409+ "codebase-memory-mcp hook-augment",
3410+ CBM_TOML_CODEX_HOOK_REMOVE, false);
34333411}
34343412
34353413/* ── OpenCode MCP config (JSON with "mcp" key) ───────────────── */
@@ -8166,6 +8144,23 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
81668144 snprintf(ip, sizeof(ip), "%s/AGENTS.md", config_dir);
81678145 snprintf(skills_dir, sizeof(skills_dir), "%s/skills", config_dir);
81688146 snprintf(ap, sizeof(ap), "%s/agents/codebase-memory.toml", config_dir);
8147+ char command[CLI_BUF_8K];
8148+ char command_windows[CLI_BUF_8K];
8149+ char hooks_json[CLI_BUF_1K];
8150+ snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
8151+ bool use_hooks_json = cbm_file_exists(hooks_json);
8152+ bool commands_ok =
8153+ cbm_build_augment_command(binary_path, command, sizeof(command)) == CLI_OK &&
8154+ cbm_build_augment_command_windows(binary_path, command_windows,
8155+ sizeof(command_windows)) == CLI_OK;
8156+ cbm_toml_codex_hook_action_t preflight_action =
8157+ use_hooks_json ? CBM_TOML_CODEX_HOOK_REMOVE : CBM_TOML_CODEX_HOOK_UPSERT;
8158+ if (!commands_ok || cbm_reconcile_codex_hooks_command(cp, command, command_windows,
8159+ preflight_action, true) != CLI_OK) {
8160+ record_agent_config_error(false, "Codex CLI",
8161+ commands_ok ? "hook_preflight" : "hook_command_build", cp);
8162+ goto codex_install_done;
8163+ }
81698164 install_generic_agent_config("Codex CLI", binary_path, cp, ip, dry_run,
81708165 cbm_upsert_codex_mcp);
81718166 install_agent_skill("Codex CLI", skills_dir, force, dry_run);
@@ -8183,53 +8178,29 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
81838178 * SessionStart reminder there instead of config.toml. Writing both
81848179 * makes Codex warn about loading hooks from two representations (#570).
81858180 * config.toml remains the mcp_config target above either way. */
8186- char hooks_json[CLI_BUF_1K];
8187- snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
8188- bool use_hooks_json = cbm_file_exists(hooks_json);
81898181 const char *hook_target = use_hooks_json ? hooks_json : cp;
81908182 if (g_install_plan) {
81918183 plan_record("Codex CLI", "hook", hook_target);
81928184 } else {
81938185 bool hook_ok = true;
8194- if (!dry_run) {
8195- char command[CLI_BUF_8K];
8196- char command_windows[CLI_BUF_8K];
8197- if (cbm_build_augment_command(binary_path, command, sizeof(command)) == CLI_OK &&
8198- cbm_build_augment_command_windows(binary_path, command_windows,
8199- sizeof(command_windows)) == CLI_OK) {
8200- if (use_hooks_json) {
8201- if (cbm_upsert_paired_lifecycle_hooks_json(
8202- hooks_json, command, command_windows, NULL, CMM_HOOK_TIMEOUT_SEC) ==
8203- CLI_OK) {
8204- if (cbm_remove_codex_hooks(cp) != CLI_OK) {
8205- hook_ok = false;
8206- record_agent_config_error(false, "Codex CLI", "legacy_hook_cleanup",
8207- cp);
8208- }
8209- } else {
8210- hook_ok = false;
8211- record_agent_config_error(false, "Codex CLI", "hook_install",
8212- hooks_json);
8213- }
8214- } else {
8215- if (cbm_upsert_codex_hooks_command(cp, command, command_windows) !=
8216- CLI_OK) {
8217- hook_ok = false;
8218- record_agent_config_error(false, "Codex CLI", "hook_install", cp);
8219- }
8220- }
8221- } else {
8222- hook_ok = false;
8223- record_agent_config_error(false, "Codex CLI", "hook_command_build",
8224- hook_target);
8225- }
8186+ if (!dry_run && use_hooks_json) {
8187+ hook_ok =
8188+ cbm_upsert_paired_lifecycle_hooks_json(hooks_json, command, command_windows,
8189+ NULL, CMM_HOOK_TIMEOUT_SEC) == CLI_OK &&
8190+ cbm_reconcile_codex_hooks_command(cp, command, command_windows,
8191+ CBM_TOML_CODEX_HOOK_REMOVE, false) == CLI_OK;
8192+ } else if (!dry_run) {
8193+ hook_ok = cbm_upsert_codex_hooks_command(cp, command, command_windows) == CLI_OK;
82268194 }
8227- if (hook_ok) {
8195+ if (!hook_ok) {
8196+ record_agent_config_error(false, "Codex CLI", "hook_install", hook_target);
8197+ } else {
82288198 printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
8199+ printf(" note: non-managed hooks require /hooks trust; definition changes "
8200+ "require re-trust\n");
82298201 }
8230- printf(" note: non-managed hooks require /hooks trust; definition changes require "
8231- "re-trust\n");
82328202 }
8203+ codex_install_done:;
82338204 }
82348205 if (agents->gemini) {
82358206 install_gemini_config(home, binary_path, dry_run);
@@ -10166,8 +10137,28 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1016610137 snprintf(skills_dir, sizeof(skills_dir), "%s/skills", config_dir);
1016710138 snprintf(ap, sizeof(ap), "%s/agents/codebase-memory.toml", config_dir);
1016810139 cbm_agent_installed_binary_path(home, installed_binary, sizeof(installed_binary));
10140+ char hook_command[CLI_BUF_8K];
10141+ char hook_command_windows[CLI_BUF_8K];
10142+ bool hook_command_ok = cbm_build_augment_command(installed_binary, hook_command,
10143+ sizeof(hook_command)) == CLI_OK;
10144+ bool hook_preflight_ok =
10145+ hook_command_ok &&
10146+ cbm_build_augment_command_windows(installed_binary, hook_command_windows,
10147+ sizeof(hook_command_windows)) == CLI_OK &&
10148+ cbm_reconcile_codex_hooks_command(cp, hook_command, hook_command_windows,
10149+ CBM_TOML_CODEX_HOOK_REMOVE, true) == CLI_OK;
10150+ if (!hook_preflight_ok) {
10151+ record_agent_config_error(true, "Codex CLI", "hook_preflight", cp);
10152+ goto codex_toml_done;
10153+ }
1016910154 uninstall_agent_mcp_instr((mcp_uninstall_args_t){"Codex CLI", cp, ip}, dry_run,
1017010155 cbm_remove_codex_mcp_owned);
10156+ if (!dry_run &&
10157+ cbm_reconcile_codex_hooks_command(cp, hook_command, hook_command_windows,
10158+ CBM_TOML_CODEX_HOOK_REMOVE, false) != CLI_OK) {
10159+ record_agent_config_error(true, "Codex CLI", "hook_uninstall", cp);
10160+ }
10161+ codex_toml_done:
1017110162 uninstall_agent_skill("Codex CLI", skills_dir, dry_run);
1017210163 uninstall_tiered_agent_profiles(
1017310164 (cbm_tiered_profile_set_t){
@@ -10179,15 +10170,10 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1017910170 },
1018010171 dry_run);
1018110172 if (!dry_run) {
10182- if (cbm_remove_codex_hooks(cp) != CLI_OK) {
10183- record_agent_config_error(true, "Codex CLI", "hook_uninstall", cp);
10184- }
1018510173 char hooks_json[CLI_BUF_1K];
10186- char hook_command[CLI_BUF_8K];
1018710174 snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
1018810175 if (cbm_file_exists(hooks_json) &&
10189- (cbm_build_augment_command(installed_binary, hook_command, sizeof(hook_command)) !=
10190- CLI_OK ||
10176+ (!hook_command_ok ||
1019110177 cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK)) {
1019210178 record_agent_config_error(true, "Codex CLI", "json_hook_uninstall", hooks_json);
1019310179 }
0 commit comments