Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(cli): give Codex shell searches graph context through a PreToolUse hook

Martin Vogel3e5234f660cf
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 1ebe1687e..8ffd95fef 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -4053,13 +4053,14 @@ static int cbm_reconcile_codex_hooks_command_detailed(const char *config_path, c
const char *command_windows,
cbm_toml_codex_hook_action_t action,
bool check_only,
- cbm_toml_codex_hook_failure_t *failure) {
+ cbm_toml_codex_hook_failure_t *failure,
+ int *pretool_written) {
if (!config_path || !command || !command_windows) {
return CLI_ERR;
}
- return cbm_toml_reconcile_codex_hooks_detailed(config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END,
- command, command_windows, action,
- check_only ? 1 : 0, failure) == 0
+ return cbm_toml_reconcile_codex_hooks_detailed(
+ config_path, CODEX_HOOK_BEGIN, CODEX_HOOK_END, command, command_windows, action,
+ check_only ? 1 : 0, failure, pretool_written) == 0
? CLI_OK
: CLI_ERR;
}
@@ -4067,7 +4068,7 @@ static int cbm_reconcile_codex_hooks_command(const char *config_path, const char
const char *command_windows,
cbm_toml_codex_hook_action_t action, bool check_only) {
return cbm_reconcile_codex_hooks_command_detailed(config_path, command, command_windows, action,
- check_only, NULL);
+ check_only, NULL, NULL);
}
static int cbm_upsert_codex_hooks_command(const char *config_path, const char *command,
const char *command_windows) {
@@ -6509,6 +6510,35 @@ static int cbm_remove_paired_lifecycle_hooks_json(const char *settings_path,
return session_result == CLI_OK && subagent_result == CLI_OK ? CLI_OK : CLI_ERR;
}
+/* Codex names its shell tool Bash and passes {"command": "..."} as tool_input,
+ * the payload hook-augment already augments for Claude Code. Only the entry whose
+ * command is exactly ours is touched; the user's own PreToolUse hooks stay. */
+static int cbm_upsert_codex_pretool_hooks_json(const char *settings_path, const char *command,
+ const char *command_windows) {
+ return upsert_hooks_json((hooks_upsert_args_t){
+ .settings_path = settings_path,
+ .hook_event = "PreToolUse",
+ .matcher_str = "Bash",
+ .command_str = command,
+ .command_windows = command_windows,
+ .timeout_value = CMM_HOOK_TIMEOUT_SEC,
+ .match_command_exact = command,
+ });
+}
+
+static int cbm_remove_codex_pretool_hooks_json(const char *settings_path,
+ const char *canonical_command) {
+ if (!canonical_command) {
+ return CLI_ERR;
+ }
+ return remove_hooks_json((hooks_remove_args_t){
+ .settings_path = settings_path,
+ .hook_event = "PreToolUse",
+ .matcher_str = "Bash",
+ .match_command_exact = canonical_command,
+ });
+}
+
static int cbm_remove_qwen_lifecycle_hooks(const char *settings_path, const char *binary_path,
bool windows) {
char command[CLI_BUF_8K];
@@ -9745,7 +9775,7 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
int preflight_result = commands_ok ? cbm_reconcile_codex_hooks_command_detailed(
cp, command, command_windows, preflight_action,
- true, &preflight_failure)
+ true, &preflight_failure, NULL)
: CLI_ERR;
if (preflight_result != CLI_OK) {
if (!g_install_plan) {
@@ -9788,19 +9818,29 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
plan_record("Codex CLI", "hook", hook_target);
} else {
bool hook_ok = true;
+ int pretool_written = 1;
if (!dry_run && use_hooks_json) {
hook_ok =
cbm_upsert_paired_lifecycle_hooks_json(hooks_json, command, command_windows,
NULL, CMM_HOOK_TIMEOUT_SEC) == CLI_OK &&
+ cbm_upsert_codex_pretool_hooks_json(hooks_json, command, command_windows) ==
+ CLI_OK &&
cbm_reconcile_codex_hooks_command(cp, command, command_windows,
CBM_TOML_CODEX_HOOK_REMOVE, false) == CLI_OK;
} else if (!dry_run) {
- hook_ok = cbm_upsert_codex_hooks_command(cp, command, command_windows) == CLI_OK;
+ hook_ok = cbm_reconcile_codex_hooks_command_detailed(
+ cp, command, command_windows, CBM_TOML_CODEX_HOOK_UPSERT, false, NULL,
+ &pretool_written) == CLI_OK;
}
if (!hook_ok) {
record_agent_config_error(false, "Codex CLI", "hook_install", hook_target);
} else {
- printf(" hooks: SessionStart + SubagentStart (dynamic graph context)\n");
+ printf(pretool_written
+ ? " hooks: SessionStart + SubagentStart + PreToolUse Bash (dynamic "
+ "graph context)\n"
+ : " hooks: SessionStart + SubagentStart (dynamic graph context); "
+ "PreToolUse skipped: config.toml already has an inline "
+ "hooks.PreToolUse\n");
printf(" note: non-managed hooks require /hooks trust; definition changes "
"require re-trust\n");
}
@@ -12226,10 +12266,10 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
installed_binary, hook_command_windows,
sizeof(hook_command_windows)) == CLI_OK;
cbm_toml_codex_hook_failure_t preflight_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
- bool hook_preflight_ok =
- hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
- cp, hook_command, hook_command_windows,
- CBM_TOML_CODEX_HOOK_REMOVE, true, &preflight_failure) == CLI_OK;
+ bool hook_preflight_ok = hook_commands_ok && cbm_reconcile_codex_hooks_command_detailed(
+ cp, hook_command, hook_command_windows,
+ CBM_TOML_CODEX_HOOK_REMOVE, true,
+ &preflight_failure, NULL) == CLI_OK;
if (!hook_preflight_ok) {
printf("Codex CLI:\n");
fflush(stdout);
@@ -12264,7 +12304,8 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
snprintf(hooks_json, sizeof(hooks_json), "%s/hooks.json", config_dir);
if (cbm_file_exists(hooks_json) &&
(!hook_command_ok ||
- cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK)) {
+ cbm_remove_paired_lifecycle_hooks_json(hooks_json, hook_command) != CLI_OK ||
+ cbm_remove_codex_pretool_hooks_json(hooks_json, hook_command) != CLI_OK)) {
record_agent_config_error(true, "Codex CLI", "json_hook_uninstall", hooks_json);
}
}
diff --git a/src/cli/config_toml_edit.c b/src/cli/config_toml_edit.c
index 5ffcbafaa..8f0931279 100644
--- a/src/cli/config_toml_edit.c
+++ b/src/cli/config_toml_edit.c
@@ -3379,7 +3379,8 @@ static int toml_codex_inline_is_owned(const char *data, size_t start, size_t end
toml_string_t type = {0};
toml_string_t command = {0};
toml_string_t windows = {0};
- const char *expected_matcher = event == 0 ? "startup|resume|clear|compact" : "*";
+ const char *expected_matcher =
+ event == 0 ? "startup|resume|clear|compact" : (event == 1 ? "*" : "Bash");
int result = 0;
if (toml_codex_take(data, end, &pos, "[") != TOML_EDIT_OK ||
toml_codex_take(data, end, &pos, "{") != TOML_EDIT_OK)
@@ -3432,11 +3433,15 @@ done:
toml_string_dispose(&windows);
return result;
}
+/* include_pretool renders the current block: the lifecycle pair plus a PreToolUse
+ * Bash hook, so Codex shell searches get graph context in flow. Without it the
+ * function renders the previous block (lifecycle pair only), which installs up to
+ * v0.12 wrote and which upgrades and uninstalls must still recognize as owned. */
static int toml_codex_build_block(const char *command, const char *command_windows,
- toml_buffer_t *block) {
+ int include_pretool, toml_buffer_t *block) {
char escaped[8192];
char escaped_windows[8192];
- char rendered[24576];
+ char rendered[32768];
if (cbm_toml_escape_basic_string(command, escaped, sizeof(escaped)) != TOML_EDIT_OK ||
cbm_toml_escape_basic_string(command_windows, escaped_windows, sizeof(escaped_windows)) !=
TOML_EDIT_OK ||
@@ -3455,6 +3460,16 @@ static int toml_codex_build_block(const char *command, const char *command_windo
"type = \"command\"\ncommand = \"%s\"\n"
"command_windows = \"%s\"\ntimeout = 5\n",
escaped, escaped_windows, escaped, escaped_windows);
+ if (written > 0 && (size_t)written < sizeof(rendered) && include_pretool) {
+ int more = snprintf(rendered + written, sizeof(rendered) - (size_t)written,
+ "\n[[hooks.PreToolUse]]\nmatcher = \"Bash\"\n\n"
+ "[[hooks.PreToolUse.hooks]]\n"
+ "type = \"command\"\ncommand = \"%s\"\n"
+ "command_windows = \"%s\"\ntimeout = 5\n",
+ escaped, escaped_windows);
+ written =
+ more > 0 && (size_t)more < sizeof(rendered) - (size_t)written ? written + more : -1;
+ }
return written > 0 && (size_t)written < sizeof(rendered)
? toml_buffer_append(block, rendered, (size_t)written)
: TOML_EDIT_ERR;
@@ -3485,19 +3500,24 @@ static size_t toml_codex_owned_span(const char *data, size_t len, const char *be
}
toml_assignment_dispose(&assignment);
}
- toml_buffer_t block = {0};
- toml_buffer_t expected = {0};
- if (toml_codex_current_command_is_owned(values[0].data, values[0].len) &&
- toml_codex_current_command_is_owned(values[1].data, values[1].len) &&
- toml_codex_build_block(values[0].data, values[1].data, &block) == TOML_EDIT_OK &&
- (marked ? toml_append_managed(&expected, begin_marker, end_marker, block.data, newline)
- : toml_append_normalized_text(&expected, block.data, block.len, newline)) ==
- TOML_EDIT_OK &&
- expected.len <= len && memcmp(expected.data, data, expected.len) == 0) {
- result = expected.len;
- }
- toml_buffer_dispose(&expected);
- toml_buffer_dispose(&block);
+ /* Either generation is ours: the current block with PreToolUse first, then the
+ * previous lifecycle-only block, so upgrade and uninstall never strand it. */
+ for (int include_pretool = 1; include_pretool >= 0 && result == 0U; --include_pretool) {
+ toml_buffer_t block = {0};
+ toml_buffer_t expected = {0};
+ if (toml_codex_current_command_is_owned(values[0].data, values[0].len) &&
+ toml_codex_current_command_is_owned(values[1].data, values[1].len) &&
+ toml_codex_build_block(values[0].data, values[1].data, include_pretool, &block) ==
+ TOML_EDIT_OK &&
+ (marked ? toml_append_managed(&expected, begin_marker, end_marker, block.data, newline)
+ : toml_append_normalized_text(&expected, block.data, block.len, newline)) ==
+ TOML_EDIT_OK &&
+ expected.len <= len && memcmp(expected.data, data, expected.len) == 0) {
+ result = expected.len;
+ }
+ toml_buffer_dispose(&expected);
+ toml_buffer_dispose(&block);
+ }
done:
toml_string_dispose(&values[0]);
toml_string_dispose(&values[1]);
@@ -3509,14 +3529,21 @@ static int toml_codex_add_edit(toml_codex_edit_t *edits, size_t *count, size_t s
edits[(*count)++] = (toml_codex_edit_t){.start = start, .end = end};
return TOML_EDIT_OK;
}
+/* Inline forms Codex may normalize our hooks to (#1432). SessionStart and
+ * SubagentStart must be ours or the edit is refused. A foreign inline PreToolUse
+ * is the user's own hook: it is kept, and *foreign_pretool tells the caller to
+ * leave PreToolUse out of the block, because TOML cannot hold an inline array and
+ * array-of-tables entries for the same key. */
static int toml_codex_scan_inline(const char *data, size_t len, toml_codex_edit_t *edits,
- size_t *edit_count, int found[2],
+ size_t *edit_count, int found[3], int *foreign_pretool,
cbm_toml_codex_hook_failure_t *failure) {
- toml_key_path_t wanted[2] = {{0}};
+ toml_key_path_t wanted[3] = {{0}};
toml_key_path_t scope = {0};
if (toml_parse_key_path("hooks.SessionStart", 0U, strlen("hooks.SessionStart"), &wanted[0]) !=
TOML_EDIT_OK ||
toml_parse_key_path("hooks.SubagentStart", 0U, strlen("hooks.SubagentStart"), &wanted[1]) !=
+ TOML_EDIT_OK ||
+ toml_parse_key_path("hooks.PreToolUse", 0U, strlen("hooks.PreToolUse"), &wanted[2]) !=
TOML_EDIT_OK) {
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_EDIT_BUILD);
goto error;
@@ -3554,9 +3581,12 @@ static int toml_codex_scan_inline(const char *data, size_t len, toml_codex_edit_
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_EDIT_BUILD);
goto error;
}
- int event = toml_key_path_equal(&full_key, &wanted[0])
- ? 0
- : (toml_key_path_equal(&full_key, &wanted[1]) ? 1 : -1);
+ int event = -1;
+ for (int candidate = 0; candidate < 3 && event < 0; ++candidate) {
+ if (toml_key_path_equal(&full_key, &wanted[candidate])) {
+ event = candidate;
+ }
+ }
toml_key_path_dispose(&full_key);
if (event >= 0) {
size_t tail = assignment.value_end;
@@ -3564,12 +3594,14 @@ static int toml_codex_scan_inline(const char *data, size_t len, toml_codex_edit_
size_t payload_start = toml_codex_payload_start(data, len);
size_t edit_start = line.start == 0U ? payload_start : line.start;
cbm_toml_codex_hook_failure_t event_failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE;
- ++found[event];
- if (found[event] > 1) {
+ int owned = !assignment.multiline_value && tail == line.content_end &&
+ toml_codex_inline_is_owned(data, assignment.value_start,
+ assignment.value_end, event);
+ if (event == 2 && !owned) {
+ *foreign_pretool = 1;
+ } else if (++found[event] > 1) {
event_failure = CBM_TOML_CODEX_HOOK_FAILURE_CONFLICTING_HOOKS;
- } else if (assignment.multiline_value || tail != line.content_end ||
- !toml_codex_inline_is_owned(data, assignment.value_start,
- assignment.value_end, event)) {
+ } else if (!owned) {
event_failure = CBM_TOML_CODEX_HOOK_FAILURE_AMBIGUOUS_OWNERSHIP;
} else if (toml_codex_add_edit(edits, edit_count, edit_start, line.full_end) !=
TOML_EDIT_OK) {
@@ -3600,14 +3632,19 @@ done:
toml_key_path_dispose(&scope);
toml_key_path_dispose(&wanted[0]);
toml_key_path_dispose(&wanted[1]);
+ toml_key_path_dispose(&wanted[2]);
return result;
}
int cbm_toml_reconcile_codex_hooks_detailed(const char *file_path, const char *begin_marker,
const char *end_marker, const char *command,
const char *command_windows,
cbm_toml_codex_hook_action_t action, int check_only,
- cbm_toml_codex_hook_failure_t *failure) {
+ cbm_toml_codex_hook_failure_t *failure,
+ int *pretool_written) {
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_NONE);
+ if (pretool_written) {
+ *pretool_written = 0;
+ }
if (!toml_valid_path(file_path) || !toml_valid_marker(begin_marker) ||
!toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0 || !command ||
!command_windows ||
@@ -3616,7 +3653,7 @@ int cbm_toml_reconcile_codex_hooks_detailed(const char *file_path, const char *b
return TOML_EDIT_ERR;
}
toml_buffer_t block = {0};
- if (toml_codex_build_block(command, command_windows, &block) != TOML_EDIT_OK) {
+ if (toml_codex_build_block(command, command_windows, 1, &block) != TOML_EDIT_OK) {
toml_buffer_dispose(&block);
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_COMMAND_RENDER);
return TOML_EDIT_ERR;
@@ -3687,15 +3724,23 @@ int cbm_toml_reconcile_codex_hooks_detailed(const char *file_path, const char *b
pos += owned_len - 1U;
}
}
- int inline_found[2] = {0};
- if (toml_codex_scan_inline(existing, existing_len, edits, &edit_count, inline_found, failure) !=
- TOML_EDIT_OK) {
+ int inline_found[3] = {0};
+ int foreign_pretool = 0;
+ if (toml_codex_scan_inline(existing, existing_len, edits, &edit_count, inline_found,
+ &foreign_pretool, failure) != TOML_EDIT_OK) {
goto error;
}
- if (markerless_count && (has_pair || inline_found[0] || inline_found[1])) {
+ if (markerless_count && (has_pair || inline_found[0] || inline_found[1] || inline_found[2])) {
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_CONFLICTING_HOOKS);
goto error;
}
+ if (foreign_pretool) {
+ toml_buffer_dispose(&block);
+ if (toml_codex_build_block(command, command_windows, 0, &block) != TOML_EDIT_OK) {
+ toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_COMMAND_RENDER);
+ goto error;
+ }
+ }
for (size_t i = 0U; i < edit_count; ++i) {
for (size_t j = i + 1U; j < edit_count; ++j) {
if (edits[j].start < edits[i].start) {
@@ -3737,6 +3782,8 @@ int cbm_toml_reconcile_codex_hooks_detailed(const char *file_path, const char *b
output.len, &snapshot);
if (result != TOML_EDIT_OK) {
toml_codex_set_failure(failure, CBM_TOML_CODEX_HOOK_FAILURE_CONFIG_WRITE);
+ } else if (pretool_written) {
+ *pretool_written = action == CBM_TOML_CODEX_HOOK_UPSERT && !foreign_pretool;
}
error:
toml_buffer_dispose(&output);
@@ -3749,5 +3796,5 @@ int cbm_toml_reconcile_codex_hooks(const char *file_path, const char *begin_mark
const char *command_windows, cbm_toml_codex_hook_action_t action,
int check_only) {
return cbm_toml_reconcile_codex_hooks_detailed(file_path, begin_marker, end_marker, command,
- command_windows, action, check_only, NULL);
+ command_windows, action, check_only, NULL, NULL);
}
diff --git a/src/cli/config_toml_edit.h b/src/cli/config_toml_edit.h
index 6b944ee19..430401b3b 100644
--- a/src/cli/config_toml_edit.h
+++ b/src/cli/config_toml_edit.h
@@ -98,12 +98,16 @@ typedef enum {
/* Returns a stable, content-free diagnostic name for a failure category. */
const char *cbm_toml_codex_hook_failure_name(cbm_toml_codex_hook_failure_t failure);
/* `failure` may be NULL. When non-NULL, it is reset to NONE on entry and remains
- * NONE on success; failure categories never contain configuration content. */
+ * NONE on success; failure categories never contain configuration content.
+ * `pretool_written` may be NULL. When non-NULL it is set to 1 after a successful
+ * upsert that wrote the PreToolUse Bash hook, and to 0 when a foreign inline
+ * hooks.PreToolUse made the upsert keep only the lifecycle pair. */
int cbm_toml_reconcile_codex_hooks_detailed(const char *file_path, const char *begin_marker,
const char *end_marker, const char *command,
const char *command_windows,
cbm_toml_codex_hook_action_t action, int check_only,
- cbm_toml_codex_hook_failure_t *failure);
+ cbm_toml_codex_hook_failure_t *failure,
+ int *pretool_written);
int cbm_toml_reconcile_codex_hooks(const char *file_path, const char *begin_marker,
const char *end_marker, const char *command,
const char *command_windows, cbm_toml_codex_hook_action_t action,
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 539b0df18..510115d11 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -12197,6 +12197,89 @@ TEST(cli_codex_session_hook_issue330) {
PASS();
}
+/* The block installs up to v0.12 wrote: the lifecycle pair only, inside the
+ * markers. Upgrades and uninstalls must still recognize it as ours. */
+static const char test_codex_previous_hook_block[] =
+ "# >>> codebase-memory-mcp SessionStart >>>\n"
+ "[[hooks.SessionStart]]\n"
+ "matcher = \"startup|resume|clear|compact\"\n\n"
+ "[[hooks.SessionStart.hooks]]\n"
+ "type = \"command\"\ncommand = \"codebase-memory-mcp hook-augment\"\n"
+ "command_windows = \"codebase-memory-mcp hook-augment\"\ntimeout = 5\n\n"
+ "[[hooks.SubagentStart]]\nmatcher = \"*\"\n\n"
+ "[[hooks.SubagentStart.hooks]]\n"
+ "type = \"command\"\ncommand = \"codebase-memory-mcp hook-augment\"\n"
+ "command_windows = \"codebase-memory-mcp hook-augment\"\ntimeout = 5\n"
+ "# <<< codebase-memory-mcp SessionStart <<<\n";
+
+/* Codex shell searches get graph context in flow: the managed block adds a
+ * PreToolUse Bash hook, upgrades the previous block in place, removes either
+ * generation, keeps a user's own inline hooks.PreToolUse (TOML cannot mix an
+ * inline array with array-of-tables entries for one key), and replaces the
+ * inline form Codex may normalize ours to. */
+TEST(cli_codex_pretooluse_hook_lifecycle) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-codexpre-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("cbm_mkdtemp failed");
+ char cfg[512];
+ snprintf(cfg, sizeof(cfg), "%s/config.toml", tmpdir);
+
+ /* fresh install: one PreToolUse Bash hook running hook-augment, idempotent */
+ write_test_file(cfg, "[mcp_servers.other]\ncommand = \"x\"\n");
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ const char *d = read_test_file(cfg);
+ ASSERT_NOT_NULL(d);
+ ASSERT_EQ(test_count_substring(d, "[[hooks.PreToolUse]]\nmatcher = \"Bash\""), 1U);
+ ASSERT_EQ(test_count_substring(d, "[[hooks.PreToolUse.hooks]]"), 1U);
+ ASSERT_EQ(test_count_substring(d, "[[hooks.SessionStart]]"), 1U);
+ ASSERT_EQ(cbm_remove_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NULL(strstr(d, "hooks.PreToolUse"));
+ ASSERT_NOT_NULL(strstr(d, "[mcp_servers.other]"));
+
+ /* upgrade: the previous block is replaced, never refused as ambiguous */
+ char previous[2048];
+ snprintf(previous, sizeof(previous), "[mcp_servers.other]\ncommand = \"x\"\n\n%s",
+ test_codex_previous_hook_block);
+ write_test_file(cfg, previous);
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_EQ(test_count_substring(d, "[[hooks.SessionStart]]"), 1U);
+ ASSERT_EQ(test_count_substring(d, "[[hooks.PreToolUse]]"), 1U);
+
+ /* uninstall of the previous generation */
+ write_test_file(cfg, previous);
+ ASSERT_EQ(cbm_remove_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NULL(strstr(d, "hooks.SessionStart"));
+ ASSERT_NULL(strstr(d, "hook-augment"));
+
+ /* a user's own inline PreToolUse stays, and our block leaves PreToolUse out */
+ const char *foreign = "[hooks]\nPreToolUse = [{ matcher = \"Bash\", hooks = [{ type = "
+ "\"command\", command = \"/usr/local/bin/audit-shell\" }] }]\n";
+ write_test_file(cfg, foreign);
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NOT_NULL(strstr(d, "audit-shell"));
+ ASSERT_NULL(strstr(d, "[[hooks.PreToolUse]]"));
+ ASSERT_EQ(test_count_substring(d, "[[hooks.SessionStart]]"), 1U);
+
+ /* Codex's inline form of our own PreToolUse is replaced, not duplicated */
+ write_test_file(cfg, "[hooks]\nPreToolUse = [{ matcher = \"Bash\", hooks = [{ type = "
+ "\"command\", command = \"codebase-memory-mcp hook-augment\", "
+ "command_windows = \"codebase-memory-mcp hook-augment\", "
+ "timeout = 5 }] }]\n");
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NULL(strstr(d, "PreToolUse = ["));
+ ASSERT_EQ(test_count_substring(d, "[[hooks.PreToolUse]]"), 1U);
+
+ test_rmdir_r(tmpdir);
+ PASS();
+}
+
/* Gemini/Antigravity SessionStart reminder parity (settings.json JSON path). */
TEST(cli_gemini_session_hook_parity) {
char tmpdir[256];
@@ -13221,7 +13304,8 @@ TEST(cli_codex_migrates_to_single_hook_representation) {
bool lifecycle_ok = first_rc == 0 && repeat_rc == 0 && dry_rc == 0 && first && repeated &&
after_dry && strcmp(first, repeated) == 0 && strcmp(first, after_dry) == 0;
bool migrated = migration_rc == 0 && toml && !strstr(toml, "SessionStart") && hooks &&
- strstr(hooks, "SessionStart") && strstr(hooks, "SubagentStart");
+ strstr(hooks, "SessionStart") && strstr(hooks, "SubagentStart") &&
+ strstr(hooks, "PreToolUse");
free(first);
free(repeated);
free(after_dry);
@@ -13580,6 +13664,47 @@ TEST(cli_hook_augment_bash_pretooluse_reaches_augmenter) {
PASS();
}
+/* Codex sends PreToolUse for its shell tool as tool_name "Bash" with a string
+ * tool_input.command, plus its own fields (turn_id, model, permission_mode,
+ * tool_use_id, a null transcript_path). The default dialect must augment it. */
+TEST(cli_hook_augment_codex_pretooluse_payload) {
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *st = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(st);
+ char *project = cbm_project_name_from_path("/tmp/hookproj");
+ ASSERT_NOT_NULL(project);
+ cbm_mcp_server_set_project(srv, project);
+ cbm_store_upsert_project(st, project, "/tmp/hookproj");
+ char qualified_name[256];
+ snprintf(qualified_name, sizeof(qualified_name), "%s.mod.someIndexedSymbol", project);
+ cbm_node_t node = {.project = project,
+ .label = "Function",
+ .name = "someIndexedSymbol",
+ .qualified_name = qualified_name,
+ .file_path = "mod.py",
+ .start_line = 1,
+ .end_line = 4};
+ ASSERT_GT(cbm_store_upsert_node(st, &node), 0);
+
+ const char *input = "{\"session_id\":\"0199a1b2-codex\",\"turn_id\":\"turn-1\","
+ "\"transcript_path\":null,\"cwd\":\"/tmp/hookproj\","
+ "\"hook_event_name\":\"PreToolUse\",\"model\":\"gpt-5.6\","
+ "\"permission_mode\":\"default\",\"tool_name\":\"Bash\","
+ "\"tool_input\":{\"command\":\"rg -n someIndexedSymbol .\"},"
+ "\"tool_use_id\":\"call_1\"}";
+ char *output = cbm_hook_augment_process(srv, input);
+ bool reached = output && strstr(output, "\"hookEventName\":\"PreToolUse\"") &&
+ strstr(output, "additionalContext") && strstr(output, "someIndexedSymbol");
+ free(output);
+ cbm_mcp_server_free(srv);
+ free(project);
+
+ if (!reached)
+ FAIL("a Codex-shaped PreToolUse Bash payload must reach graph search augmentation");
+ PASS();
+}
+
TEST(cli_hook_augment_bash_pattern_extractor) {
char out[256];
@@ -18500,6 +18625,7 @@ SUITE(cli) {
RUN_TEST(cli_hook_metadata_rejects_truncated_utf8_without_oob);
RUN_TEST(cli_aider_config_loads_installed_conventions);
RUN_TEST(cli_codex_session_hook_issue330);
+ RUN_TEST(cli_codex_pretooluse_hook_lifecycle);
RUN_TEST(cli_gemini_session_hook_parity);
RUN_TEST(cli_claude_subagent_hook);
RUN_TEST(cli_claude_hook_mutation_converges_mixed_owned_duplicates);
@@ -18525,6 +18651,7 @@ SUITE(cli) {
RUN_TEST(cli_hook_augment_context_tracks_search_json_shape);
RUN_TEST(cli_hook_augment_context_names_graph_follow_ups);
RUN_TEST(cli_hook_augment_bash_pretooluse_reaches_augmenter);
+ RUN_TEST(cli_hook_augment_codex_pretooluse_payload);
RUN_TEST(cli_hook_augment_bash_pattern_extractor);
RUN_TEST(cli_hook_augment_coverage_requests_machine_json);
RUN_TEST(cli_hook_augment_lifecycle_output_contract);
diff --git a/tests/test_config_toml_edit.c b/tests/test_config_toml_edit.c
index 8f928aa6e..68930617c 100644
--- a/tests/test_config_toml_edit.c
+++ b/tests/test_config_toml_edit.c
@@ -57,7 +57,8 @@ static int cte_codex_edit_commands_detailed(const char *path, const char *comman
cbm_toml_codex_hook_action_t action, int check_only,
cbm_toml_codex_hook_failure_t *failure) {
return cbm_toml_reconcile_codex_hooks_detailed(path, CTE_CODEX_BEGIN, CTE_CODEX_END, command,
- command_windows, action, check_only, failure);
+ command_windows, action, check_only, failure,
+ NULL);
}
static int cte_codex_edit_detailed(const char *path, cbm_toml_codex_hook_action_t action,