Skip to content

Commit 3e5234f

Browse files
committed
feat(cli): give Codex shell searches graph context through a PreToolUse hook
Codex runs its hooks with the same contract as Claude Code: its shell tool is named Bash, tool_input.command is a string, and PreToolUse hookSpecificOutput additionalContext reaches the model (openai/codex hooks/src/events/ pre_tool_use.rs, core/src/hook_runtime.rs). Codex only had the SessionStart and SubagentStart reminders (DeusData#330), so its grep and rg calls never got the graph context Claude Code's searches get (DeusData#715). - config.toml: the managed block adds [[hooks.PreToolUse]] matcher = "Bash" running the same hook-augment command. The previous block (lifecycle pair only, as installs up to v0.12 wrote it) stays recognized as owned, so an upgrade replaces it in place and uninstall removes either generation. - Inline forms (DeusData#1432): Codex's inline rewrite of our PreToolUse is replaced. A user's own inline hooks.PreToolUse is kept and our block then leaves PreToolUse out, because TOML cannot hold an inline array and array-of-tables entries for the same key; install says so instead of failing. - hooks.json: the Codex-only PreToolUse Bash entry is added and removed next to the lifecycle pair, touching only the entry whose command is ours. Codex asks users to re-trust changed hook definitions (/hooks). Refs DeusData#715, DeusData#509 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 10ab613 commit 3e5234f

5 files changed

Lines changed: 270 additions & 50 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4053,21 +4053,22 @@ static int cbm_reconcile_codex_hooks_command_detailed(const char *config_path, c
40534053
const char *command_windows,
40544054
cbm_toml_codex_hook_action_t action,
40554055
bool check_only,
4056-
cbm_toml_codex_hook_failure_t *failure) {
4056+
cbm_toml_codex_hook_failure_t *failure,
4057+
int *pretool_written) {
40574058
if (!config_path || !command || !command_windows) {
40584059
return CLI_ERR;
40594060
}
4060-
return cbm_toml_reconcile_codex_hooks_detailed(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END,
4061-
command, command_windows, action,
4062-
check_only ? 1 : 0, failure) == 0
4061+
return cbm_toml_reconcile_codex_hooks_detailed(
4062+
config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, command, command_windows, action,
4063+
check_only ? 1 : 0, failure, pretool_written) == 0
40634064
? CLI_OK
40644065
: CLI_ERR;
40654066
}
40664067
static int cbm_reconcile_codex_hooks_command(const char *config_path, const char *command,
40674068
const char *command_windows,
40684069
cbm_toml_codex_hook_action_t action, bool check_only) {
40694070
return cbm_reconcile_codex_hooks_command_detailed(config_path, command, command_windows, action,
4070-
check_only, NULL);
4071+
check_only, NULL, NULL);
40714072
}
40724073
static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
40734074
const char *command_windows) {
@@ -6509,6 +6510,35 @@ static int cbm_remove_paired_lifecycle_hooks_json(const char *settings_path,
65096510
return session_result == CLI_OK && subagent_result == CLI_OK ? CLI_OK : CLI_ERR;
65106511
}
65116512

6513+
/* Codex names its shell tool Bash and passes {"command": "..."} as tool_input,
6514+
* the payload hook-augment already augments for Claude Code. Only the entry whose
6515+
* command is exactly ours is touched; the user's own PreToolUse hooks stay. */
6516+
static int cbm_upsert_codex_pretool_hooks_json(const char *settings_path, const char *command,
6517+
const char *command_windows) {
6518+
return upsert_hooks_json((hooks_upsert_args_t){
6519+
.settings_path = settings_path,
6520+
.hook_event = "PreToolUse",
6521+
.matcher_str = "Bash",
6522+
.command_str = command,
6523+
.command_windows = command_windows,
6524+
.timeout_value = CMM_HOOK_TIMEOUT_SEC,
6525+
.match_command_exact = command,
6526+
});
6527+
}
6528+
6529+
static int cbm_remove_codex_pretool_hooks_json(const char *settings_path,
6530+
const char *canonical_command) {
6531+
if (!canonical_command) {
6532+
return CLI_ERR;
6533+
}
6534+
return remove_hooks_json((hooks_remove_args_t){
6535+
.settings_path = settings_path,
6536+
.hook_event = "PreToolUse",
6537+
.matcher_str = "Bash",
6538+
.match_command_exact = canonical_command,
6539+
});
6540+
}
6541+
65126542
static int cbm_remove_qwen_lifecycle_hooks(const char *settings_path, const char *binary_path,
65136543
bool windows) {
65146544
char command[CLI_BUF_8K];
@@ -9745,7 +9775,7 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
97459775
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
97469776
int preflight_result = commands_ok ? cbm_reconcile_codex_hooks_command_detailed(
97479777
cp, command, command_windows, preflight_action,
9748-
true, &preflight_failure)
9778+
true, &preflight_failure, NULL)
97499779
: CLI_ERR;
97509780
if (preflight_result != CLI_OK) {
97519781
if (!g_install_plan) {
@@ -9788,19 +9818,29 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
97889818
plan_record("Codex CLI", "hook", hook_target);
97899819
} else {
97909820
bool hook_ok = true;
9821+
int pretool_written = 1;
97919822
if (!dry_run && use_hooks_json) {
97929823
hook_ok =
97939824
cbm_upsert_paired_lifecycle_hooks_json(hooks_json, command, command_windows,
97949825
NULL, CMM_HOOK_TIMEOUT_SEC) == CLI_OK &&
9826+
cbm_upsert_codex_pretool_hooks_json(hooks_json, command, command_windows) ==
9827+
CLI_OK &&
97959828
cbm_reconcile_codex_hooks_command(cp, command, command_windows,
97969829
CBM_TOML_CODEX_HOOK_REMOVE, false) == CLI_OK;
97979830
} else if (!dry_run) {
9798-
hook_ok = cbm_upsert_codex_hooks_command(cp, command, command_windows) == CLI_OK;
9831+
hook_ok = cbm_reconcile_codex_hooks_command_detailed(
9832+
cp, command, command_windows, CBM_TOML_CODEX_HOOK_UPSERT, false, NULL,
9833+
&pretool_written) == CLI_OK;
97999834
}
98009835
if (!hook_ok) {
98019836
record_agent_config_error(false, "Codex CLI", "hook_install", hook_target);
98029837
} else {
9803-
printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
9838+
printf(pretool_written
9839+
? " hooks: SessionStart + SubagentStart + PreToolUse Bash (dynamic "
9840+
"graph context)\n"
9841+
: " hooks: SessionStart + SubagentStart (dynamic graph context); "
9842+
"PreToolUse skipped: config.toml already has an inline "
9843+
"hooks.PreToolUse\n");
98049844
printf(" note: non-managed hooks require /hooks trust; definition changes "
98059845
"require re-trust\n");
98069846
}
@@ -12226,10 +12266,10 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1222612266
installed_binary, hook_command_windows,
1222712267
sizeof(hook_command_windows)) == CLI_OK;
1222812268
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
12229-
bool hook_preflight_ok =
12230-
hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
12231-
cp, hook_command, hook_command_windows,
12232-
CBM_TOML_CODEX_HOOK_REMOVE, true, &preflight_failure) == CLI_OK;
12269+
bool hook_preflight_ok = hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
12270+
cp, hook_command, hook_command_windows,
12271+
CBM_TOML_CODEX_HOOK_REMOVE, true,
12272+
&preflight_failure, NULL) == CLI_OK;
1223312273
if (!hook_preflight_ok) {
1223412274
printf("Codex CLI:\n");
1223512275
fflush(stdout);
@@ -12264,7 +12304,8 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1226412304
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
1226512305
if (cbm_file_exists(hooks_json) &&
1226612306
(!hook_command_ok ||
12267-
cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK)) {
12307+
cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK ||
12308+
cbm_remove_codex_pretool_hooks_json(hooks_json, hook_command) != CLI_OK)) {
1226812309
record_agent_config_error(true, "Codex CLI", "json_hook_uninstall", hooks_json);
1226912310
}
1227012311
}

0 commit comments

Comments
 (0)