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(hooks): SessionStart reminders for Codex, Gemini CLI, Antigravity (#330)

Martin Vogel6c71244e6375
diff --git a/README.md b/README.md
index 54e571114..4c701151b 100644
--- a/README.md
+++ b/README.md
@@ -335,11 +335,11 @@ Restart your agent. Verify with `/mcp` — you should see `codebase-memory-mcp`
| Agent | MCP Config | Instructions | Hooks |
|-------|-----------|-------------|-------|
| Claude Code | `.claude/.mcp.json` | 4 Skills | PreToolUse (Grep/Glob graph augment, non-blocking) |
-| Codex CLI | `.codex/config.toml` | `.codex/AGENTS.md` | — |
-| Gemini CLI | `.gemini/settings.json` | `.gemini/GEMINI.md` | BeforeTool (grep reminder) |
+| Codex CLI | `.codex/config.toml` | `.codex/AGENTS.md` | SessionStart reminder |
+| Gemini CLI | `.gemini/settings.json` | `.gemini/GEMINI.md` | BeforeTool (grep reminder) + SessionStart reminder |
| Zed | `settings.json` (JSONC) | — | — |
| OpenCode | `opencode.json` | `AGENTS.md` | — |
-| Antigravity | `mcp_config.json` | `AGENTS.md` | — |
+| Antigravity | `mcp_config.json` | `AGENTS.md` | SessionStart reminder |
| Aider | — | `CONVENTIONS.md` | — |
| KiloCode | `mcp_settings.json` | `~/.kilocode/rules/` | — |
| VS Code | `Code/User/mcp.json` | — | — |
@@ -351,7 +351,9 @@ For Claude Code, the `PreToolUse` hook intercepts `Grep`/`Glob` (never `Read`
gating `Read` breaks the read-before-edit invariant) and, when the search
token matches indexed symbols, injects them as `additionalContext` via
`search_graph` so the agent gets structured context alongside its normal
-search results. For Gemini CLI, `BeforeTool` prints a short reminder.
+search results. For Codex, Gemini CLI, and Antigravity, a `SessionStart` hook
+injects a one-line code-discovery reminder as session context (Gemini CLI also
+keeps its `BeforeTool` reminder).
The installed Claude shim file is named `cbm-code-discovery-gate` for
backward compatibility with existing installs; despite the legacy name it
never gates and never blocks.
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 3d892f65a..5bba0bf5b 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -1435,6 +1435,115 @@ int cbm_remove_codex_mcp(const char *config_path) {
return rc;
}
+/* ── SessionStart reminder hook (Codex / Gemini / Antigravity) ──────
+ * Same methodology as the Claude Code SessionStart hook: a non-blocking
+ * lifecycle hook whose stdout is injected as session context, reminding the
+ * agent to use codebase-memory-mcp graph tools first. The command is written
+ * so it is valid both inside a TOML single-quoted literal (Codex config.toml)
+ * and a JSON string (Gemini settings.json) — i.e. it contains NO single quotes
+ * and NO newlines. (issues #330 + Gemini/Antigravity parity) */
+#define CMM_SESSION_REMINDER_CMD \
+ "echo \"Code discovery: prefer codebase-memory-mcp (search_graph, trace_path, " \
+ "get_code_snippet, query_graph, search_code) over grep/file-read; run " \
+ "index_repository first if the project is not indexed.\""
+
+/* Sentinel-delimited block so upsert/remove are robust to the nested TOML
+ * array-of-tables (which both start with '['). */
+#define CODEX_HOOK_BEGIN "# >>> codebase-memory-mcp SessionStart >>>"
+#define CODEX_HOOK_END "# <<< codebase-memory-mcp SessionStart <<<"
+
+/* Splice out an existing [CODEX_HOOK_BEGIN .. CODEX_HOOK_END] block (inclusive,
+ * plus a leading newline). Returns a newly-malloc'd string the caller frees, or
+ * NULL if no block was present (content is left untouched). */
+static char *codex_hook_strip(const char *content) {
+ char *begin = strstr(content, CODEX_HOOK_BEGIN);
+ if (!begin) {
+ return NULL;
+ }
+ char *end = strstr(begin, CODEX_HOOK_END);
+ if (!end) {
+ return NULL;
+ }
+ end += strlen(CODEX_HOOK_END);
+ if (*end == '\n') {
+ end++;
+ }
+ /* Drop one leading newline before the block, if any. */
+ char *cut = begin;
+ if (cut > content && *(cut - CLI_SKIP_ONE) == '\n') {
+ cut--;
+ }
+ size_t prefix_len = (size_t)(cut - content);
+ size_t suffix_len = strlen(end);
+ char *out = malloc(prefix_len + suffix_len + CLI_SKIP_ONE);
+ if (!out) {
+ return NULL;
+ }
+ memcpy(out, content, prefix_len);
+ memcpy(out + prefix_len, end, suffix_len);
+ out[prefix_len + suffix_len] = '\0';
+ return out;
+}
+
+/* Install/update the Codex SessionStart reminder hook in config.toml. */
+int cbm_upsert_codex_hooks(const char *config_path) {
+ if (!config_path) {
+ return CLI_ERR;
+ }
+ char block[CLI_BUF_2K];
+ snprintf(block, sizeof(block),
+ "\n" CODEX_HOOK_BEGIN "\n"
+ "[[hooks.SessionStart]]\n"
+ "matcher = \"startup|resume|clear|compact\"\n\n"
+ "[[hooks.SessionStart.hooks]]\n"
+ "type = \"command\"\n"
+ "command = '%s'\n" CODEX_HOOK_END "\n",
+ CMM_SESSION_REMINDER_CMD);
+
+ size_t len = 0;
+ char *content = read_file_str(config_path, &len);
+ if (!content) {
+ return write_file_str(config_path, block + CLI_SKIP_ONE); /* skip leading newline */
+ }
+ char *stripped = codex_hook_strip(content);
+ const char *base = stripped ? stripped : content;
+ size_t base_len = strlen(base);
+ char *result = malloc(base_len + strlen(block) + CLI_SKIP_ONE);
+ if (!result) {
+ free(content);
+ free(stripped);
+ return CLI_ERR;
+ }
+ memcpy(result, base, base_len);
+ memcpy(result + base_len, block, strlen(block));
+ result[base_len + strlen(block)] = '\0';
+ int rc = write_file_str(config_path, result);
+ free(content);
+ free(stripped);
+ free(result);
+ return rc;
+}
+
+int cbm_remove_codex_hooks(const char *config_path) {
+ if (!config_path) {
+ return CLI_ERR;
+ }
+ size_t len = 0;
+ char *content = read_file_str(config_path, &len);
+ if (!content) {
+ return CLI_TRUE;
+ }
+ char *stripped = codex_hook_strip(content);
+ if (!stripped) {
+ free(content);
+ return CLI_TRUE; /* nothing to remove */
+ }
+ int rc = write_file_str(config_path, stripped);
+ free(content);
+ free(stripped);
+ return rc;
+}
+
/* ── OpenCode MCP config (JSON with "mcp" key) ───────────────── */
int cbm_upsert_opencode_mcp(const char *binary_path, const char *config_path) {
@@ -1713,6 +1822,12 @@ static int remove_hooks_json(hooks_remove_args_t args) {
}
}
+ /* Prune the event key once its array is empty, so removing our hook leaves
+ * no stale "<Event>": [] cruft behind. */
+ if (yyjson_mut_arr_size(event_arr) == 0) {
+ yyjson_mut_obj_remove_key(hooks, hook_event);
+ }
+
int rc = write_json_file(settings_path, mdoc);
yyjson_mut_doc_free(mdoc);
return rc;
@@ -1898,6 +2013,29 @@ int cbm_remove_gemini_hooks(const char *settings_path) {
});
}
+/* Gemini CLI / Antigravity SessionStart reminder. settings.json uses the same
+ * hooks.<Event>[].hooks[] JSON shape as Claude, so it reuses upsert_hooks_json.
+ * The SessionStart matcher is advisory in Gemini (it does not filter lifecycle
+ * sources), so a single "startup" entry fires on startup/resume/clear. The
+ * command's stdout is injected as session context. (Gemini/Antigravity parity
+ * with the Claude/Codex SessionStart reminder.) */
+int cbm_upsert_gemini_session_hooks(const char *settings_path) {
+ return upsert_hooks_json((hooks_upsert_args_t){
+ .settings_path = settings_path,
+ .hook_event = "SessionStart",
+ .matcher_str = "startup",
+ .command_str = CMM_SESSION_REMINDER_CMD,
+ });
+}
+
+int cbm_remove_gemini_session_hooks(const char *settings_path) {
+ return remove_hooks_json((hooks_remove_args_t){
+ .settings_path = settings_path,
+ .hook_event = "SessionStart",
+ .matcher_str = "startup",
+ });
+}
+
/* ── PATH management ──────────────────────────────────────────── */
int cbm_ensure_path(const char *bin_dir, const char *rc_file, bool dry_run) {
@@ -2919,12 +3057,14 @@ static void install_gemini_config(const char *home, const char *binary_path, boo
install_generic_agent_config("Gemini CLI", binary_path, cp, ip, dry_run,
cbm_install_editor_mcp);
if (g_install_plan) {
- return; /* config recorded by install_generic_agent_config above */
+ plan_record("Gemini CLI", "hook", cp); /* BeforeTool + SessionStart in settings.json */
+ return;
}
if (!dry_run) {
cbm_upsert_gemini_hooks(cp);
+ cbm_upsert_gemini_session_hooks(cp);
}
- printf(" hooks: BeforeTool (grep/file search reminder)\n");
+ printf(" hooks: BeforeTool + SessionStart (codebase-memory-mcp reminder)\n");
}
static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const char *home,
@@ -2936,6 +3076,14 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
snprintf(ip, sizeof(ip), "%s/.codex/AGENTS.md", home);
install_generic_agent_config("Codex CLI", binary_path, cp, ip, dry_run,
cbm_upsert_codex_mcp);
+ if (g_install_plan) {
+ plan_record("Codex CLI", "hook", cp);
+ } else {
+ if (!dry_run) {
+ cbm_upsert_codex_hooks(cp);
+ }
+ printf(" hooks: SessionStart (codebase-memory-mcp reminder)\n");
+ }
}
if (agents->gemini) {
install_gemini_config(home, binary_path, dry_run);
@@ -2955,6 +3103,18 @@ static void install_cli_agent_configs(const cbm_detected_agents_t *agents, const
snprintf(ip, sizeof(ip), "%s/.gemini/antigravity/AGENTS.md", home);
install_generic_agent_config("Antigravity", binary_path, cp, ip, dry_run,
cbm_upsert_antigravity_mcp);
+ /* Antigravity shares Gemini's hooks JSON schema; its settings live in
+ * the antigravity config dir. */
+ char sp[CLI_BUF_1K];
+ snprintf(sp, sizeof(sp), "%s/.gemini/antigravity/settings.json", home);
+ if (g_install_plan) {
+ plan_record("Antigravity", "hook", sp);
+ } else {
+ if (!dry_run) {
+ cbm_upsert_gemini_session_hooks(sp);
+ }
+ printf(" hooks: SessionStart (codebase-memory-mcp reminder)\n");
+ }
}
if (agents->aider) {
char ip[CLI_BUF_1K];
@@ -3351,6 +3511,7 @@ static void uninstall_gemini_config(const char *home, bool dry_run) {
if (!dry_run) {
cbm_remove_editor_mcp(cp);
cbm_remove_gemini_hooks(cp);
+ cbm_remove_gemini_session_hooks(cp);
cbm_remove_instructions(ip);
}
printf("Gemini CLI: removed MCP config + hooks + instructions\n");
@@ -3365,6 +3526,9 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
snprintf(ip, sizeof(ip), "%s/.codex/AGENTS.md", home);
uninstall_agent_mcp_instr((mcp_uninstall_args_t){"Codex CLI", cp, ip}, dry_run,
cbm_remove_codex_mcp);
+ if (!dry_run) {
+ cbm_remove_codex_hooks(cp);
+ }
}
if (agents->gemini) {
uninstall_gemini_config(home, dry_run);
@@ -3384,6 +3548,11 @@ static void uninstall_cli_agents(const cbm_detected_agents_t *agents, const char
snprintf(ip, sizeof(ip), "%s/.gemini/antigravity/AGENTS.md", home);
uninstall_agent_mcp_instr((mcp_uninstall_args_t){"Antigravity", cp, ip}, dry_run,
cbm_remove_antigravity_mcp);
+ if (!dry_run) {
+ char sp[CLI_BUF_1K];
+ snprintf(sp, sizeof(sp), "%s/.gemini/antigravity/settings.json", home);
+ cbm_remove_gemini_session_hooks(sp);
+ }
}
if (agents->aider) {
char ip[CLI_BUF_1K];
diff --git a/src/cli/cli.h b/src/cli/cli.h
index 931daf51c..384af45a7 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -189,6 +189,14 @@ int cbm_upsert_gemini_hooks(const char *settings_path);
* Returns 0 on success. */
int cbm_remove_gemini_hooks(const char *settings_path);
+/* Install/remove a SessionStart reminder hook in Codex config.toml (#330) and
+ * Gemini/Antigravity settings.json — same methodology as the Claude Code
+ * SessionStart hook (non-blocking; stdout injected as session context). */
+int cbm_upsert_codex_hooks(const char *config_path);
+int cbm_remove_codex_hooks(const char *config_path);
+int cbm_upsert_gemini_session_hooks(const char *settings_path);
+int cbm_remove_gemini_session_hooks(const char *settings_path);
+
/* ── PATH management ──────────────────────────────────────────── */
/* Append an export PATH line to the given rc file.
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 45cfc682e..6647b7224 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -1538,6 +1538,65 @@ TEST(cli_install_plan_receipt_no_mutation_issue388) {
PASS();
}
+/* issue #330: Codex SessionStart reminder hook in config.toml — installed,
+ * idempotent, preserves other content, and cleanly removed. */
+TEST(cli_codex_session_hook_issue330) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-codexhook-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ SKIP("cbm_mkdtemp failed");
+
+ char cfg[512];
+ snprintf(cfg, sizeof(cfg), "%s/config.toml", tmpdir);
+ write_test_file(cfg, "[mcp_servers.other]\ncommand = \"x\"\n");
+
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ const char *d = read_test_file(cfg);
+ ASSERT_NOT_NULL(d);
+ ASSERT(strstr(d, "[[hooks.SessionStart]]") != NULL);
+ ASSERT(strstr(d, "[[hooks.SessionStart.hooks]]") != NULL);
+ ASSERT(strstr(d, "search_graph") != NULL);
+ ASSERT(strstr(d, "[mcp_servers.other]") != NULL); /* pre-existing content preserved */
+ /* Idempotent: a second upsert leaves exactly ONE hook block. */
+ ASSERT_EQ(cbm_upsert_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ const char *first = strstr(d, "[[hooks.SessionStart]]");
+ ASSERT_NOT_NULL(first);
+ ASSERT_NULL(strstr(first + 1, "[[hooks.SessionStart]]"));
+
+ ASSERT_EQ(cbm_remove_codex_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NULL(strstr(d, "hooks.SessionStart"));
+ ASSERT(strstr(d, "[mcp_servers.other]") != NULL); /* still preserved after removal */
+
+ test_rmdir_r(tmpdir);
+ PASS();
+}
+
+/* Gemini/Antigravity SessionStart reminder parity (settings.json JSON path). */
+TEST(cli_gemini_session_hook_parity) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-gemhook-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ SKIP("cbm_mkdtemp failed");
+
+ char cfg[512];
+ snprintf(cfg, sizeof(cfg), "%s/settings.json", tmpdir);
+
+ ASSERT_EQ(cbm_upsert_gemini_session_hooks(cfg), 0);
+ const char *d = read_test_file(cfg);
+ ASSERT_NOT_NULL(d);
+ ASSERT(strstr(d, "SessionStart") != NULL);
+ ASSERT(strstr(d, "search_graph") != NULL);
+
+ ASSERT_EQ(cbm_remove_gemini_session_hooks(cfg), 0);
+ d = read_test_file(cfg);
+ ASSERT_NULL(strstr(d, "SessionStart"));
+
+ test_rmdir_r(tmpdir);
+ PASS();
+}
+
TEST(cli_detect_agents_finds_gemini) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-detect-XXXXXX");
@@ -2609,6 +2668,8 @@ SUITE(cli) {
RUN_TEST(cli_detect_agents_finds_codex);
RUN_TEST(cli_detect_agents_finds_cursor_issue222);
RUN_TEST(cli_install_plan_receipt_no_mutation_issue388);
+ RUN_TEST(cli_codex_session_hook_issue330);
+ RUN_TEST(cli_gemini_session_hook_parity);
RUN_TEST(cli_detect_agents_finds_gemini);
RUN_TEST(cli_detect_agents_finds_zed);
RUN_TEST(cli_detect_agents_finds_antigravity);