Skip to content

Commit 619192a

Browse files
authored
Merge pull request DeusData#2719 from DeusData/feat/codex-pretooluse-hook
feat(cli): give Codex shell searches graph context through a PreToolUse hook
2 parents ced508f + 18d7273 commit 619192a

5 files changed

Lines changed: 278 additions & 52 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,21 +4112,22 @@ static int cbm_reconcile_codex_hooks_command_detailed(const char *config_path, c
41124112
const char *command_windows,
41134113
cbm_toml_codex_hook_action_t action,
41144114
bool check_only,
4115-
cbm_toml_codex_hook_failure_t *failure) {
4115+
cbm_toml_codex_hook_failure_t *failure,
4116+
int *pretool_written) {
41164117
if (!config_path || !command || !command_windows) {
41174118
return CLI_ERR;
41184119
}
4119-
return cbm_toml_reconcile_codex_hooks_detailed(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END,
4120-
command, command_windows, action,
4121-
check_only ? 1 : 0, failure) == 0
4120+
return cbm_toml_reconcile_codex_hooks_detailed(
4121+
config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, command, command_windows, action,
4122+
check_only ? 1 : 0, failure, pretool_written) == 0
41224123
? CLI_OK
41234124
: CLI_ERR;
41244125
}
41254126
static int cbm_reconcile_codex_hooks_command(const char *config_path, const char *command,
41264127
const char *command_windows,
41274128
cbm_toml_codex_hook_action_t action, bool check_only) {
41284129
return cbm_reconcile_codex_hooks_command_detailed(config_path, command, command_windows, action,
4129-
check_only, NULL);
4130+
check_only, NULL, NULL);
41304131
}
41314132
static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
41324133
const char *command_windows) {
@@ -6568,6 +6569,35 @@ static int cbm_remove_paired_lifecycle_hooks_json(const char *settings_path,
65686569
return session_result == CLI_OK && subagent_result == CLI_OK ? CLI_OK : CLI_ERR;
65696570
}
65706571

6572+
/* Codex names its shell tool Bash and passes {"command": "..."} as tool_input,
6573+
* the payload hook-augment already augments for Claude Code. Only the entry whose
6574+
* command is exactly ours is touched; the user's own PreToolUse hooks stay. */
6575+
static int cbm_upsert_codex_pretool_hooks_json(const char *settings_path, const char *command,
6576+
const char *command_windows) {
6577+
return upsert_hooks_json((hooks_upsert_args_t){
6578+
.settings_path = settings_path,
6579+
.hook_event = "PreToolUse",
6580+
.matcher_str = "Bash",
6581+
.command_str = command,
6582+
.command_windows = command_windows,
6583+
.timeout_value = CMM_HOOK_TIMEOUT_SEC,
6584+
.match_command_exact = command,
6585+
});
6586+
}
6587+
6588+
static int cbm_remove_codex_pretool_hooks_json(const char *settings_path,
6589+
const char *canonical_command) {
6590+
if (!canonical_command) {
6591+
return CLI_ERR;
6592+
}
6593+
return remove_hooks_json((hooks_remove_args_t){
6594+
.settings_path = settings_path,
6595+
.hook_event = "PreToolUse",
6596+
.matcher_str = "Bash",
6597+
.match_command_exact = canonical_command,
6598+
});
6599+
}
6600+
65716601
static int cbm_remove_qwen_lifecycle_hooks(const char *settings_path, const char *binary_path,
65726602
bool windows) {
65736603
char command[CLI_BUF_8K];
@@ -9804,7 +9834,7 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
98049834
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
98059835
int preflight_result = commands_ok ? cbm_reconcile_codex_hooks_command_detailed(
98069836
cp, command, command_windows, preflight_action,
9807-
true, &preflight_failure)
9837+
true, &preflight_failure, NULL)
98089838
: CLI_ERR;
98099839
if (preflight_result != CLI_OK) {
98109840
if (!g_install_plan) {
@@ -9847,19 +9877,29 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
98479877
plan_record("Codex CLI", "hook", hook_target);
98489878
} else {
98499879
bool hook_ok = true;
9880+
int pretool_written = 1;
98509881
if (!dry_run && use_hooks_json) {
98519882
hook_ok =
98529883
cbm_upsert_paired_lifecycle_hooks_json(hooks_json, command, command_windows,
98539884
NULL, CMM_HOOK_TIMEOUT_SEC) == CLI_OK &&
9885+
cbm_upsert_codex_pretool_hooks_json(hooks_json, command, command_windows) ==
9886+
CLI_OK &&
98549887
cbm_reconcile_codex_hooks_command(cp, command, command_windows,
98559888
CBM_TOML_CODEX_HOOK_REMOVE, false) == CLI_OK;
98569889
} else if (!dry_run) {
9857-
hook_ok = cbm_upsert_codex_hooks_command(cp, command, command_windows) == CLI_OK;
9890+
hook_ok = cbm_reconcile_codex_hooks_command_detailed(
9891+
cp, command, command_windows, CBM_TOML_CODEX_HOOK_UPSERT, false, NULL,
9892+
&pretool_written) == CLI_OK;
98589893
}
98599894
if (!hook_ok) {
98609895
record_agent_config_error(false, "Codex CLI", "hook_install", hook_target);
98619896
} else {
9862-
printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
9897+
printf(pretool_written
9898+
? " hooks: SessionStart + SubagentStart + PreToolUse Bash (dynamic "
9899+
"graph context)\n"
9900+
: " hooks: SessionStart + SubagentStart (dynamic graph context); "
9901+
"PreToolUse skipped: config.toml already has an inline "
9902+
"hooks.PreToolUse\n");
98639903
printf(" note: non-managed hooks require /hooks trust; definition changes "
98649904
"require re-trust\n");
98659905
}
@@ -12285,10 +12325,10 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1228512325
installed_binary, hook_command_windows,
1228612326
sizeof(hook_command_windows)) == CLI_OK;
1228712327
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
12288-
bool hook_preflight_ok =
12289-
hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
12290-
cp, hook_command, hook_command_windows,
12291-
CBM_TOML_CODEX_HOOK_REMOVE, true, &preflight_failure) == CLI_OK;
12328+
bool hook_preflight_ok = hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
12329+
cp, hook_command, hook_command_windows,
12330+
CBM_TOML_CODEX_HOOK_REMOVE, true,
12331+
&preflight_failure, NULL) == CLI_OK;
1229212332
if (!hook_preflight_ok) {
1229312333
printf("Codex CLI:\n");
1229412334
fflush(stdout);
@@ -12323,7 +12363,8 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
1232312363
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
1232412364
if (cbm_file_exists(hooks_json) &&
1232512365
(!hook_command_ok ||
12326-
cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK)) {
12366+
cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK ||
12367+
cbm_remove_codex_pretool_hooks_json(hooks_json, hook_command) != CLI_OK)) {
1232712368
record_agent_config_error(true, "Codex CLI", "json_hook_uninstall", hooks_json);
1232812369
}
1232912370
}

0 commit comments

Comments
 (0)