Skip to content

Commit 6b2650c

Browse files
authored
Merge pull request DeusData#1443 from astandrik/codex/fix-1432-codex-hook-reconcile
fix(cli): reconcile normalized Codex hooks
2 parents bd11088 + 0cf0ccb commit 6b2650c

5 files changed

Lines changed: 728 additions & 86 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 65 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,54 +3448,32 @@ bool cbm_optional_hook_supported_for_testing(const char *agent_name, bool window
34483448
}
34493449
#endif
34503450

3451-
static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
3452-
const char *command_windows) {
3451+
static int cbm_reconcile_codex_hooks_command(const char *config_path, const char *command,
3452+
const char *command_windows,
3453+
cbm_toml_codex_hook_action_t action, bool check_only) {
34533454
if (!config_path || !command || !command_windows) {
34543455
return CLI_ERR;
34553456
}
3456-
char escaped[CLI_BUF_8K];
3457-
char escaped_windows[CLI_BUF_8K];
3458-
if (cbm_toml_escape_basic_string(command, escaped, sizeof(escaped)) != CLI_OK ||
3459-
cbm_toml_escape_basic_string(command_windows, escaped_windows, sizeof(escaped_windows)) !=
3460-
CLI_OK) {
3461-
return CLI_ERR;
3462-
}
3463-
char block[CLI_BUF_8K];
3464-
int written = snprintf(block, sizeof(block),
3465-
"[[hooks.SessionStart]]\n"
3466-
"matcher = \"startup|resume|clear|compact\"\n\n"
3467-
"[[hooks.SessionStart.hooks]]\n"
3468-
"type = \"command\"\n"
3469-
"command = \"%s\"\n"
3470-
"command_windows = \"%s\"\n"
3471-
"timeout = 5\n\n"
3472-
"[[hooks.SubagentStart]]\n"
3473-
"matcher = \"*\"\n\n"
3474-
"[[hooks.SubagentStart.hooks]]\n"
3475-
"type = \"command\"\n"
3476-
"command = \"%s\"\n"
3477-
"command_windows = \"%s\"\n"
3478-
"timeout = 5\n",
3479-
escaped, escaped_windows, escaped, escaped_windows);
3480-
if (written < 0 || (size_t)written >= sizeof(block)) {
3481-
return CLI_ERR;
3482-
}
3483-
return cbm_toml_upsert_managed_block(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, block) == 0
3457+
return cbm_toml_reconcile_codex_hooks(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, command,
3458+
command_windows, action, check_only ? 1 : 0) == 0
34843459
? CLI_OK
34853460
: CLI_ERR;
34863461
}
3487-
3462+
static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
3463+
const char *command_windows) {
3464+
return cbm_reconcile_codex_hooks_command(config_path, command, command_windows,
3465+
CBM_TOML_CODEX_HOOK_UPSERT, false);
3466+
}
34883467
/* Public path used by config-level regression tests and manual callers. */
34893468
int cbm_upsert_codex_hooks(const char *config_path) {
34903469
return cbm_upsert_codex_hooks_command(config_path, "codebase-memory-mcp hook-augment",
34913470
"codebase-memory-mcp hook-augment");
34923471
}
34933472

34943473
int cbm_remove_codex_hooks(const char *config_path) {
3495-
return config_path &&
3496-
cbm_toml_remove_managed_block(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END) == 0
3497-
? CLI_OK
3498-
: CLI_ERR;
3474+
return cbm_reconcile_codex_hooks_command(config_path, "codebase-memory-mcp hook-augment",
3475+
"codebase-memory-mcp hook-augment",
3476+
CBM_TOML_CODEX_HOOK_REMOVE, false);
34993477
}
35003478

35013479
/* ── OpenCode MCP config (JSON with "mcp" key) ───────────────── */
@@ -8351,6 +8329,23 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
83518329
snprintf(ip, sizeof(ip), "%s/AGENTS.md", config_dir);
83528330
snprintf(skills_dir, sizeof(skills_dir), "%s/skills", config_dir);
83538331
snprintf(ap, sizeof(ap), "%s/agents/codebase-memory.toml", config_dir);
8332+
char command[CLI_BUF_8K];
8333+
char command_windows[CLI_BUF_8K];
8334+
char hooks_json[CLI_BUF_1K];
8335+
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
8336+
bool use_hooks_json = cbm_file_exists(hooks_json);
8337+
bool commands_ok =
8338+
cbm_build_augment_command(binary_path, command, sizeof(command)) == CLI_OK &&
8339+
cbm_build_augment_command_windows(binary_path, command_windows,
8340+
sizeof(command_windows)) == CLI_OK;
8341+
cbm_toml_codex_hook_action_t preflight_action =
8342+
use_hooks_json ? CBM_TOML_CODEX_HOOK_REMOVE : CBM_TOML_CODEX_HOOK_UPSERT;
8343+
if (!commands_ok || cbm_reconcile_codex_hooks_command(cp, command, command_windows,
8344+
preflight_action, true) != CLI_OK) {
8345+
record_agent_config_error(false, "Codex CLI",
8346+
commands_ok ? "hook_preflight" : "hook_command_build", cp);
8347+
goto codex_install_done;
8348+
}
83548349
install_generic_agent_config("Codex CLI", binary_path, cp, ip, dry_run,
83558350
cbm_upsert_codex_mcp);
83568351
install_agent_skill("Codex CLI", skills_dir, force, dry_run);
@@ -8368,53 +8363,29 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
83688363
* SessionStart reminder there instead of config.toml. Writing both
83698364
* makes Codex warn about loading hooks from two representations (#570).
83708365
* config.toml remains the mcp_config target above either way. */
8371-
char hooks_json[CLI_BUF_1K];
8372-
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
8373-
bool use_hooks_json = cbm_file_exists(hooks_json);
83748366
const char *hook_target = use_hooks_json ? hooks_json : cp;
83758367
if (g_install_plan) {
83768368
plan_record("Codex CLI", "hook", hook_target);
83778369
} else {
83788370
bool hook_ok = true;
8379-
if (!dry_run) {
8380-
char command[CLI_BUF_8K];
8381-
char command_windows[CLI_BUF_8K];
8382-
if (cbm_build_augment_command(binary_path, command, sizeof(command)) == CLI_OK &&
8383-
cbm_build_augment_command_windows(binary_path, command_windows,
8384-
sizeof(command_windows)) == CLI_OK) {
8385-
if (use_hooks_json) {
8386-
if (cbm_upsert_paired_lifecycle_hooks_json(
8387-
hooks_json, command, command_windows, NULL, CMM_HOOK_TIMEOUT_SEC) ==
8388-
CLI_OK) {
8389-
if (cbm_remove_codex_hooks(cp) != CLI_OK) {
8390-
hook_ok = false;
8391-
record_agent_config_error(false, "Codex CLI", "legacy_hook_cleanup",
8392-
cp);
8393-
}
8394-
} else {
8395-
hook_ok = false;
8396-
record_agent_config_error(false, "Codex CLI", "hook_install",
8397-
hooks_json);
8398-
}
8399-
} else {
8400-
if (cbm_upsert_codex_hooks_command(cp, command, command_windows) !=
8401-
CLI_OK) {
8402-
hook_ok = false;
8403-
record_agent_config_error(false, "Codex CLI", "hook_install", cp);
8404-
}
8405-
}
8406-
} else {
8407-
hook_ok = false;
8408-
record_agent_config_error(false, "Codex CLI", "hook_command_build",
8409-
hook_target);
8410-
}
8371+
if (!dry_run && use_hooks_json) {
8372+
hook_ok =
8373+
cbm_upsert_paired_lifecycle_hooks_json(hooks_json, command, command_windows,
8374+
NULL, CMM_HOOK_TIMEOUT_SEC) == CLI_OK &&
8375+
cbm_reconcile_codex_hooks_command(cp, command, command_windows,
8376+
CBM_TOML_CODEX_HOOK_REMOVE, false) == CLI_OK;
8377+
} else if (!dry_run) {
8378+
hook_ok = cbm_upsert_codex_hooks_command(cp, command, command_windows) == CLI_OK;
84118379
}
8412-
if (hook_ok) {
8380+
if (!hook_ok) {
8381+
record_agent_config_error(false, "Codex CLI", "hook_install", hook_target);
8382+
} else {
84138383
printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
8384+
printf(" note: non-managed hooks require /hooks trust; definition changes "
8385+
"require re-trust\n");
84148386
}
8415-
printf(" note: non-managed hooks require /hooks trust; definition changes require "
8416-
"re-trust\n");
84178387
}
8388+
codex_install_done:;
84188389
}
84198390
if (agents->gemini) {
84208391
install_gemini_config(home, binary_path, dry_run);
@@ -10470,8 +10441,28 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1047010441
snprintf(skills_dir, sizeof(skills_dir), "%s/skills", config_dir);
1047110442
snprintf(ap, sizeof(ap), "%s/agents/codebase-memory.toml", config_dir);
1047210443
cbm_agent_installed_binary_path(home, installed_binary, sizeof(installed_binary));
10444+
char hook_command[CLI_BUF_8K];
10445+
char hook_command_windows[CLI_BUF_8K];
10446+
bool hook_command_ok = cbm_build_augment_command(installed_binary, hook_command,
10447+
sizeof(hook_command)) == CLI_OK;
10448+
bool hook_preflight_ok =
10449+
hook_command_ok &&
10450+
cbm_build_augment_command_windows(installed_binary, hook_command_windows,
10451+
sizeof(hook_command_windows)) == CLI_OK &&
10452+
cbm_reconcile_codex_hooks_command(cp, hook_command, hook_command_windows,
10453+
CBM_TOML_CODEX_HOOK_REMOVE, true) == CLI_OK;
10454+
if (!hook_preflight_ok) {
10455+
record_agent_config_error(true, "Codex CLI", "hook_preflight", cp);
10456+
goto codex_toml_done;
10457+
}
1047310458
uninstall_agent_mcp_instr((mcp_uninstall_args_t){"Codex CLI", cp, ip}, dry_run,
1047410459
cbm_remove_codex_mcp_owned);
10460+
if (!dry_run &&
10461+
cbm_reconcile_codex_hooks_command(cp, hook_command, hook_command_windows,
10462+
CBM_TOML_CODEX_HOOK_REMOVE, false) != CLI_OK) {
10463+
record_agent_config_error(true, "Codex CLI", "hook_uninstall", cp);
10464+
}
10465+
codex_toml_done:
1047510466
uninstall_agent_skill("Codex CLI", skills_dir, dry_run);
1047610467
uninstall_tiered_agent_profiles(
1047710468
(cbm_tiered_profile_set_t){
@@ -10483,15 +10474,10 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1048310474
},
1048410475
dry_run);
1048510476
if (!dry_run) {
10486-
if (cbm_remove_codex_hooks(cp) != CLI_OK) {
10487-
record_agent_config_error(true, "Codex CLI", "hook_uninstall", cp);
10488-
}
1048910477
char hooks_json[CLI_BUF_1K];
10490-
char hook_command[CLI_BUF_8K];
1049110478
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
1049210479
if (cbm_file_exists(hooks_json) &&
10493-
(cbm_build_augment_command(installed_binary, hook_command, sizeof(hook_command)) !=
10494-
CLI_OK ||
10480+
(!hook_command_ok ||
1049510481
cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK)) {
1049610482
record_agent_config_error(true, "Codex CLI", "json_hook_uninstall", hooks_json);
1049710483
}

0 commit comments

Comments
 (0)