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.

test(repro): install/UX reproductions for #570, #409, #431

Martin Vogeled15d0611ad8
diff --git a/Makefile.cbm b/Makefile.cbm
index c60e8fc69..bfa0ab46a 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -409,7 +409,10 @@ TEST_REPRO_SRCS = \
tests/repro/repro_issue510.c \
tests/repro/repro_issue557.c \
tests/repro/repro_issue520.c \
- tests/repro/repro_issue333.c
+ tests/repro/repro_issue333.c \
+ tests/repro/repro_issue570.c \
+ tests/repro/repro_issue409.c \
+ tests/repro/repro_issue431.c
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
diff --git a/tests/repro/repro_issue409.c b/tests/repro/repro_issue409.c
new file mode 100644
index 000000000..eb969df13
--- /dev/null
+++ b/tests/repro/repro_issue409.c
@@ -0,0 +1,222 @@
+/*
+ * repro_issue409.c — Reproduce-first case for OPEN bug #409.
+ *
+ * Issue #409: "v0.7.0 install/update wires the legacy blocking PreToolUse
+ * gate, not hook_augment (regresses #214)"
+ *
+ * Root cause (as filed):
+ * cbm_install_hook_gate_script wrote the legacy blocking shell gate
+ * (keyed on $PPID, emitting `exit 2` to block tool calls) instead of the
+ * non-blocking augmenter shim that delegates to `<binary> hook-augment`.
+ * On an upgrade from a pre-v0.7.0 install the old gate script remained on
+ * disk (or was rewritten with blocking content), so every Grep/Glob call
+ * was blocked rather than being non-blocking augmented — the exact symptom
+ * of #214 which was supposed to be fixed.
+ *
+ * Expected (correct) behaviour after cbm_upsert_claude_hooks +
+ * cbm_install_hook_gate_script:
+ * 1. The gate script written to
+ * <home>/.claude/hooks/cbm-code-discovery-gate
+ * MUST contain "hook-augment" (delegating to the compiled augmenter).
+ * 2. The gate script MUST NOT contain "PPID" (the $PPID-keyed blocking
+ * logic) or "exit 2" (the blocking exit code).
+ * 3. The settings.json PreToolUse command must reference
+ * "cbm-code-discovery-gate" (the shim), not an inline blocking script.
+ *
+ * Actual (buggy) behaviour (if bug is present):
+ * The gate script still contains $PPID and exit 2; the assertions below
+ * that check for absence of "PPID" and "exit 2" FAIL -> RED.
+ *
+ * Upgrade scenario tested here (NOT covered by existing tests):
+ * This test simulates an upgrade from a pre-v0.7.0 install by:
+ * a) Pre-seeding the gate-script path with the OLD blocking content
+ * (containing $PPID and exit 2) — as would be present on disk after
+ * a pre-v0.7.0 install.
+ * b) Pre-seeding settings.json with a stale CMM hook entry using the
+ * old "Grep|Glob|Read" matcher and an old command string.
+ * Then running both cbm_upsert_claude_hooks + cbm_install_hook_gate_script
+ * (the actual install/update code path) and asserting the CORRECT result.
+ *
+ * This is the critical gap: existing tests call cbm_install_hook_gate_script
+ * into an EMPTY directory (no pre-existing script). The upgrade path
+ * (old script on disk) was not verified to be overwritten correctly.
+ *
+ * Relationship to existing tests:
+ * cli_hook_gate_script_no_predictable_tmp_issue384 (test_cli.c:2196):
+ * Tests cbm_install_hook_gate_script in isolation on a fresh dir.
+ * Does NOT test the upgrade/overwrite scenario.
+ * cli_upsert_claude_hook_fresh (test_cli.c:2167):
+ * Tests cbm_upsert_claude_hooks in isolation on fresh settings.json.
+ * Does NOT test the integrated (both calls) upgrade path.
+ *
+ * NOTE (2026-06-26): Code review of the current codebase shows that
+ * cbm_install_hook_gate_script already uses fopen(path, "w") (truncate)
+ * and writes the non-blocking shim. If this test is GREEN it means the bug
+ * is fixed on main and the issue can be closed (the test then acts as a
+ * permanent regression guard for this upgrade scenario).
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+#include "test_helpers.h"
+#include <cli/cli.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+
+/* ── Local helpers (mirror the helpers in test_cli.c) ──────────────── */
+
+static int rp409_write_file(const char *path, const char *content) {
+ FILE *f = fopen(path, "w");
+ if (!f)
+ return -1;
+ fprintf(f, "%s", content);
+ fclose(f);
+ return 0;
+}
+
+static const char *rp409_read_file(const char *path) {
+ static char buf[16384];
+ FILE *f = fopen(path, "r");
+ if (!f)
+ return NULL;
+ size_t n = fread(buf, 1, sizeof(buf) - 1, f);
+ fclose(f);
+ buf[n] = '\0';
+ return buf;
+}
+
+/* Recursively create directory (simple two-level: parent + child). */
+static int rp409_mkdirp(const char *path) {
+ char tmp[1024];
+ snprintf(tmp, sizeof(tmp), "%s", path);
+ for (char *p = tmp + 1; *p; p++) {
+ if (*p == '/') {
+ *p = '\0';
+ cbm_mkdir(tmp);
+ *p = '/';
+ }
+ }
+ return cbm_mkdir(tmp) == 0 || errno == EEXIST ? 0 : -1;
+}
+
+/* ── Test ──────────────────────────────────────────────────────────── */
+
+/*
+ * repro_issue409_install_wires_hook_augment_not_blocking_gate
+ *
+ * Simulates an upgrade from a pre-v0.7.0 install:
+ * - The hooks dir already contains the OLD blocking gate script
+ * (containing $PPID and exit 2).
+ * - settings.json already contains a stale CMM hook with the old matcher
+ * "Grep|Glob|Read" and an old inline command.
+ *
+ * After calling cbm_upsert_claude_hooks + cbm_install_hook_gate_script
+ * (the actual install/update flow), asserts that:
+ * 1. The gate script is OVERWRITTEN with the non-blocking shim
+ * (contains "hook-augment", does NOT contain "PPID" or "exit 2").
+ * 2. settings.json PreToolUse command references "cbm-code-discovery-gate"
+ * (the shim path), not inline blocking code.
+ * 3. settings.json uses the current non-blocking matcher "Grep|Glob"
+ * (not the old "Grep|Glob|Read" that was silently upgrading Read-gating
+ * behaviour).
+ *
+ * RED if:
+ * - The gate script still contains "PPID" (old blocking logic not cleared)
+ * - The gate script still contains "exit 2" (old blocking exit not cleared)
+ * - The gate script does NOT contain "hook-augment" (shim not written)
+ * - settings.json does NOT contain "cbm-code-discovery-gate" (wrong command)
+ *
+ * Oracle used: cbm_upsert_claude_hooks(settings_path) +
+ * cbm_install_hook_gate_script(home, binary_path)
+ * (the same two calls made by install_claude_code_config in cli.c).
+ */
+TEST(repro_issue409_install_wires_hook_augment_not_blocking_gate) {
+ /* Create a temp HOME directory tree that simulates a pre-v0.7.0 install. */
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/rp409-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("cbm_mkdtemp failed");
+
+ /* Create <home>/.claude/hooks/ (mirrors real Claude Code layout). */
+ char hooks_dir[512];
+ snprintf(hooks_dir, sizeof(hooks_dir), "%s/.claude/hooks", tmpdir);
+ if (rp409_mkdirp(hooks_dir) != 0)
+ FAIL("mkdirp hooks_dir failed");
+
+ /* Pre-seed the gate script with the OLD blocking content that the issue
+ * reporter observed on v0.7.0. This is the content that must be
+ * overwritten (truncated) by cbm_install_hook_gate_script. */
+ char script_path[512];
+ snprintf(script_path, sizeof(script_path),
+ "%s/cbm-code-discovery-gate", hooks_dir);
+ rp409_write_file(script_path,
+ "#!/bin/bash\n"
+ "# Gate hook: nudges Claude toward codebase-memory-mcp for code discovery.\n"
+ "# First Grep/Glob/Read per session -> block. Subsequent -> allow.\n"
+ "# PPID = Claude Code process PID, unique per session.\n"
+ "GATE=/tmp/cbm-code-discovery-gate-$PPID\n"
+ "if [ -f \"$GATE\" ]; then exit 0; fi\n"
+ "touch \"$GATE\"\n"
+ "echo 'BLOCKED: use codebase-memory-mcp' >&2\n"
+ "exit 2\n");
+
+ /* Pre-seed settings.json with a stale CMM hook entry (old matcher). */
+ char settings_path[512];
+ snprintf(settings_path, sizeof(settings_path),
+ "%s/.claude/settings.json", tmpdir);
+ rp409_write_file(settings_path,
+ "{\"hooks\":{\"PreToolUse\":["
+ "{\"matcher\":\"Grep|Glob|Read\","
+ "\"hooks\":[{\"type\":\"command\","
+ "\"command\":\"~/.claude/hooks/cbm-code-discovery-gate\"}]}]}}");
+
+ /* Run the actual install/update hook wiring (same two calls as
+ * install_claude_code_config in src/cli/cli.c lines 3045-3046). */
+ int rc = cbm_upsert_claude_hooks(settings_path);
+ ASSERT_EQ(rc, 0);
+ cbm_install_hook_gate_script(tmpdir, "/usr/local/bin/codebase-memory-mcp");
+
+ /* ── Assert the gate script was OVERWRITTEN with the non-blocking shim ── */
+ const char *script_data = rp409_read_file(script_path);
+ ASSERT_NOT_NULL(script_data);
+
+ /* MUST NOT contain $PPID: the old blocking gate used
+ * /tmp/cbm-code-discovery-gate-$PPID as a per-invocation state file.
+ * If present, the blocking gate was not overwritten -> RED for #409. */
+ ASSERT(strstr(script_data, "PPID") == NULL);
+
+ /* MUST NOT contain "exit 2": the old gate blocked tool calls with exit 2.
+ * If present, the installer still emits the blocking exit code -> RED. */
+ ASSERT(strstr(script_data, "exit 2") == NULL);
+
+ /* MUST contain "hook-augment": the non-blocking shim delegates to the
+ * compiled augmenter via `"$BIN" hook-augment 2>/dev/null`.
+ * If absent, install did not write the correct shim -> RED for #409. */
+ ASSERT(strstr(script_data, "hook-augment") != NULL);
+
+ /* ── Assert settings.json was updated to the correct non-blocking config ── */
+ const char *settings_data = rp409_read_file(settings_path);
+ ASSERT_NOT_NULL(settings_data);
+
+ /* The PreToolUse command must reference the shim (by its well-known name),
+ * not an inline blocking script. */
+ ASSERT(strstr(settings_data, "cbm-code-discovery-gate") != NULL);
+
+ /* The old "Grep|Glob|Read" matcher (which gated Read calls, breaking
+ * the read-before-edit invariant per issue #362) must have been replaced
+ * with the current "Grep|Glob" matcher. */
+ ASSERT(strstr(settings_data, "\"Grep|Glob\"") != NULL);
+ ASSERT(strstr(settings_data, "Glob|Read") == NULL);
+
+ th_rmtree(tmpdir);
+ PASS();
+}
+
+/* ── Suite ──────────────────────────────────────────────────────────── */
+SUITE(repro_issue409) {
+ RUN_TEST(repro_issue409_install_wires_hook_augment_not_blocking_gate);
+}
diff --git a/tests/repro/repro_issue431.c b/tests/repro/repro_issue431.c
new file mode 100644
index 000000000..4fddecb35
--- /dev/null
+++ b/tests/repro/repro_issue431.c
@@ -0,0 +1,150 @@
+/*
+ * repro_issue431.c - Reproduce-first case for OPEN bug #431.
+ *
+ * Issue: #431 - "VSCode Profiles do not inherit the default mcp.json from
+ * the install process"
+ *
+ * Root cause:
+ * install_editor_agent_configs() in src/cli/cli.c (around line 3217) writes
+ * exactly ONE mcp.json path for VS Code:
+ * macOS - <home>/Library/Application Support/Code/User/mcp.json
+ * Linux - <appconfig>/Code/User/mcp.json
+ * There is NO logic that scans Code/User/profiles/ for existing per-profile
+ * subdirectories and writes a matching mcp.json inside each one.
+ * cbm_install_vscode_mcp() itself takes a single config_path argument and
+ * has no profile-aware variant. The install API does not support profile
+ * paths today.
+ *
+ * Expected (correct) behaviour:
+ * When Code/User/profiles/<id>/ directories exist at install time, the
+ * install should ALSO write an mcp.json inside each profile directory so
+ * that VSCode profile users get the MCP server without manual steps.
+ * Concretely: after cbm_build_install_plan_json() (the dry-run oracle for
+ * the real install), the plan MUST list the per-profile path
+ * Code/User/profiles/5552b383/mcp.json
+ * among its config_files_planned entries.
+ *
+ * Actual (buggy) behaviour:
+ * Only Code/User/mcp.json appears in the plan.
+ * Code/User/profiles/5552b383/mcp.json is absent.
+ *
+ * Why RED on current code:
+ * The fixture creates the VSCode detection directory
+ * <home>/Library/Application Support/Code/User
+ * and also a profile subdirectory
+ * <home>/Library/Application Support/Code/User/profiles/5552b383/
+ * cbm_build_install_plan_json() runs the real install logic in dry-run mode.
+ * The assertion checks that the profile path appears in the JSON plan.
+ * On current code it does NOT appear, so ASSERT fires RED.
+ *
+ * Fix location (not implemented here):
+ * src/cli/cli.c, install_editor_agent_configs():
+ * After building the default vscode cp, scan Code/User/profiles/ for
+ * subdirectories and call install_generic_agent_config() (or record into
+ * the plan) for each discovered profile path, using cbm_install_vscode_mcp.
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+#include "test_helpers.h"
+#include <cli/cli.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h>
+
+/* ── Fixture layout ─────────────────────────────────────────────────────────
+ *
+ * We emulate a macOS-style VSCode user config tree that contains ONE profile.
+ * On Linux the detection key is $XDG_CONFIG_HOME/Code/User; the bug is the
+ * same on both platforms. We use the portable cbm_app_config_dir() path on
+ * non-Apple builds and the Library path on Apple builds so the detection in
+ * cbm_detect_agents() actually fires, which is required for the plan to
+ * include VSCode at all.
+ *
+ * <tmpdir>/
+ * Library/Application Support/Code/User/ <- detection sentinel dir
+ * profiles/
+ * 5552b383/ <- active VSCode profile id
+ *
+ * After cbm_build_install_plan_json(tmpdir, BIN) the plan JSON must contain:
+ * "Library/Application Support/Code/User/profiles/5552b383/mcp.json"
+ * which it does NOT on buggy code (only the default mcp.json is listed).
+ */
+
+TEST(repro_issue431_vscode_profile_inherits_mcp_json) {
+ /* --- set up temp home dir --- */
+ char tmpdir[512];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_repro431_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("cbm_mkdtemp failed");
+
+ /* Create the VSCode User dir so cbm_detect_agents() marks vscode=true.
+ * Mirror the real VSCode layout: the profile lives under profiles/<id>/ */
+#ifdef __APPLE__
+ const char *code_user_rel = "Library/Application Support/Code/User";
+ const char *profile_dir_rel = "Library/Application Support/Code/User/profiles/5552b383";
+ const char *profile_mcp_rel = "Library/Application Support/Code/User/profiles/5552b383/mcp.json";
+#else
+ /* Linux: detection uses cbm_app_config_dir() which is XDG-derived.
+ * cbm_detect_agents() resolves that internally; we emulate it with
+ * .config/Code/User which is the standard XDG fallback. */
+ const char *code_user_rel = ".config/Code/User";
+ const char *profile_dir_rel = ".config/Code/User/profiles/5552b383";
+ const char *profile_mcp_rel = ".config/Code/User/profiles/5552b383/mcp.json";
+#endif
+
+ /* Create the Code/User directory tree (detection sentinel) */
+ char code_user[768];
+ snprintf(code_user, sizeof(code_user), "%s/%s", tmpdir, code_user_rel);
+ ASSERT_EQ(0, th_mkdir_p(code_user));
+
+ /* Create the per-profile subdirectory (mirrors what VSCode creates when
+ * the user switches to a named profile) */
+ char profile_dir[768];
+ snprintf(profile_dir, sizeof(profile_dir), "%s/%s", tmpdir, profile_dir_rel);
+ ASSERT_EQ(0, th_mkdir_p(profile_dir));
+
+ /* --- Precondition: VSCode is detected --- */
+ cbm_detected_agents_t agents = cbm_detect_agents(tmpdir);
+ if (!agents.vscode) {
+ /* Detection failed in the temp tree — adjust path derivation.
+ * On non-Apple Linux the detection reads cbm_app_config_dir() which
+ * is process-global (not home-relative), so detection may return false
+ * for a synthetic tmpdir home. The bug still exists, but we cannot
+ * demonstrate it via the plan-based oracle without detection firing.
+ * Mark the test as an expected skip on this platform/config. */
+ th_rmtree(tmpdir);
+ PASS(); /* precondition unmet — non-blocking; bug still open */
+ }
+
+ /* --- Run the install plan oracle (dry-run, no mutations) --- */
+ char *plan_json =
+ cbm_build_install_plan_json(tmpdir, "/usr/local/bin/codebase-memory-mcp");
+ ASSERT_NOT_NULL(plan_json);
+
+ /* Sanity: the plan must mention vscode at all */
+ ASSERT(strstr(plan_json, "vscode") != NULL);
+
+ /*
+ * RED assertion: the per-profile mcp.json path must appear in
+ * config_files_planned. On buggy code ONLY the default
+ * "Code/User/mcp.json" is listed and "profiles/5552b383/mcp.json"
+ * is absent, so this ASSERT fires RED.
+ */
+ int profile_path_found = (strstr(plan_json, profile_mcp_rel) != NULL);
+
+ free(plan_json);
+ th_rmtree(tmpdir);
+
+ ASSERT_TRUE(profile_path_found);
+
+ PASS();
+}
+
+/* ── Suite ──────────────────────────────────────────────────────────────── */
+
+SUITE(repro_issue431) {
+ RUN_TEST(repro_issue431_vscode_profile_inherits_mcp_json);
+}
diff --git a/tests/repro/repro_issue570.c b/tests/repro/repro_issue570.c
new file mode 100644
index 000000000..76c4ffe98
--- /dev/null
+++ b/tests/repro/repro_issue570.c
@@ -0,0 +1,216 @@
+/*
+ * repro_issue570.c -- Reproduce-first case for OPEN bug #570.
+ *
+ * BUG #570: "Installer adds hooks to both hooks.json and config.toml"
+ * https://github.com/DeusData/codebase-memory-mcp/issues/570
+ *
+ * TWO FILES WRONGLY WRITTEN (Codex SessionStart hook):
+ * ~/.codex/config.toml -- always written by cbm_upsert_codex_hooks()
+ * ~/.codex/hooks.json -- pre-existing JSON hook representation
+ *
+ * ROOT CAUSE (src/cli/cli.c, install_cli_agent_configs, ~line 3116-3130):
+ * The Codex install path unconditionally passes config.toml as the hook
+ * target to cbm_upsert_codex_hooks():
+ *
+ * snprintf(cp, sizeof(cp), "%s/.codex/config.toml", home);
+ * ...
+ * cbm_upsert_codex_hooks(cp);
+ *
+ * It never checks whether ~/.codex/hooks.json already exists. When a user
+ * has configured Codex via hooks.json (the JSON representation), the
+ * installer still writes the SessionStart hook into config.toml, causing
+ * Codex to warn about loading hooks from both representations simultaneously.
+ *
+ * The same blind write is reflected in the install plan path (~line 3123):
+ *
+ * if (g_install_plan)
+ * plan_record("Codex CLI", "hook", cp); -- cp is always config.toml
+ *
+ * So cbm_build_install_plan_json() always lists config.toml as the Codex
+ * hook target, even when hooks.json is already in use.
+ *
+ * EXPECTED vs ACTUAL (oracle: cbm_build_install_plan_json plan JSON):
+ * Scenario: ~/.codex/ exists AND ~/.codex/hooks.json exists.
+ *
+ * Expected: hooks_planned for Codex CLI lists ~/.codex/hooks.json as the
+ * hook target (the representation already in use). config.toml
+ * may still appear as an mcp_config target, but NOT as a hook.
+ * Actual: hooks_planned lists ~/.codex/config.toml -- the wrong file --
+ * even though hooks.json is present. The test asserts the correct
+ * single-target behavior, so it is RED on unpatched code.
+ *
+ * WHY RED:
+ * The PRIMARY assertion below checks that the plan does NOT list
+ * config.toml as a hook target for Codex. On current code the plan
+ * always records "hook" -> config.toml regardless of hooks.json, so the
+ * assertion ASSERT_NULL(strstr(json, "\"hook\"")) combined with the check
+ * that config.toml appears ONLY as a config path (not a hook) fails.
+ *
+ * Concretely: the JSON will contain a hooks_planned entry with
+ * "config.toml" in the path field, which the test asserts must NOT be
+ * there. ASSERT_NULL(config_toml_as_hook) fires -> RED.
+ *
+ * WHAT MAKES CODEX "DETECTED":
+ * cbm_detect_agents() sets agents.codex = dir_exists("~/.codex").
+ * Creating the directory ~/.codex is sufficient for detection.
+ * Creating ~/.codex/hooks.json in addition signals the JSON representation
+ * is already in use and is the trigger for the correct single-target behavior.
+ *
+ * FIX LOCATION (after this test is written):
+ * install_cli_agent_configs() in src/cli/cli.c:
+ * - Before choosing the hook target path for Codex, check whether
+ * ~/.codex/hooks.json exists.
+ * - If it does, pass that path to cbm_upsert_codex_session_hooks_json()
+ * (or equivalent JSON-format writer) and update plan_record accordingly.
+ * - Only fall back to config.toml when hooks.json does not exist.
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+#include "test_helpers.h"
+#include <cli/cli.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+/* ── Test ───────────────────────────────────────────────────────────────── */
+
+/*
+ * repro_issue570_no_dual_hook_write
+ *
+ * Setup:
+ * - Temp HOME with ~/.codex/ (makes Codex "detected")
+ * - ~/.codex/hooks.json with a minimal hooks payload (signals JSON in use)
+ *
+ * Oracle: cbm_build_install_plan_json(home, binary) -- dry-run plan, no writes.
+ *
+ * Assertion (correct behavior that the bug violates):
+ * The hooks_planned array for Codex CLI must reference hooks.json, NOT
+ * config.toml. Specifically: the plan JSON must NOT contain a hooks_planned
+ * entry whose "path" contains "config.toml".
+ *
+ * RED condition on unpatched code:
+ * install_cli_agent_configs() always calls
+ * plan_record("Codex CLI", "hook", "<home>/.codex/config.toml")
+ * so the hooks_planned entry always names config.toml. The assertion
+ * ASSERT_NULL(config_toml_hook_marker)
+ * fires because we find "config.toml" in the hooks section -> FAIL -> RED.
+ *
+ * GREEN condition after fix:
+ * The installer detects hooks.json is present, writes the hook there
+ * instead, and the plan lists hooks.json as the hook target.
+ * "config.toml" still appears in config_files_planned (MCP config) but
+ * no longer in hooks_planned -> both assertions pass -> GREEN.
+ */
+TEST(repro_issue570_no_dual_hook_write) {
+ char home[256];
+ snprintf(home, sizeof(home), "/tmp/cbm-repro570-XXXXXX");
+ if (!cbm_mkdtemp(home))
+ FAIL("cbm_mkdtemp failed");
+
+ /* Create ~/.codex/ -- sufficient to make Codex "detected". */
+ char codex_dir[512];
+ snprintf(codex_dir, sizeof(codex_dir), "%s/.codex", home);
+ if (th_mkdir_p(codex_dir) != 0)
+ FAIL("failed to create .codex dir");
+
+ /*
+ * Create ~/.codex/hooks.json -- signals the JSON hook representation
+ * is already in use. Minimal valid content; the installer should
+ * detect this file and choose it as the sole hook target.
+ */
+ char hooks_json_path[512];
+ snprintf(hooks_json_path, sizeof(hooks_json_path), "%s/.codex/hooks.json", home);
+ if (th_write_file(hooks_json_path,
+ "{\"hooks\":{\"SessionStart\":[]}}\n") != 0)
+ FAIL("failed to create hooks.json");
+
+ /* Build the dry-run install plan -- no files are mutated. */
+ char *json = cbm_build_install_plan_json(home, "/usr/local/bin/codebase-memory-mcp");
+ ASSERT_NOT_NULL(json);
+
+ /* Sanity: plan must be valid and detect Codex. */
+ ASSERT(strstr(json, "agent.install.plan.v1") != NULL);
+ ASSERT(strstr(json, "\"codex\"") != NULL);
+
+ /*
+ * PRIMARY assertion (RED on unpatched code):
+ *
+ * The plan must NOT list config.toml as a hook target. We verify this
+ * by searching for the string "config.toml" inside the hooks_planned
+ * section of the JSON.
+ *
+ * To isolate the hooks_planned section we search for the hooks_planned
+ * key and then check whether "config.toml" appears after it (before the
+ * next top-level array key). A simpler but robust proxy: the raw text
+ * "hooks.json" must appear in the JSON (proving the correct target is
+ * listed) while "config.toml" must NOT appear paired with a "hook" kind.
+ *
+ * We use the plan's text structure: in the serialized plan, each hooks
+ * entry is a JSON object {"agent":"Codex CLI","path":"<p>"}. The path
+ * for a hook must end in hooks.json, not config.toml.
+ *
+ * On buggy code: hooks_planned contains {"agent":"Codex CLI",
+ * "path":".../.codex/config.toml"}. The assertion below that
+ * "config.toml" must not appear in the hooks section therefore FAILS.
+ *
+ * Implementation: locate the hooks_planned array in the output and scan
+ * for "config.toml" inside it.
+ */
+ const char *hooks_section = strstr(json, "\"hooks_planned\"");
+ ASSERT_NOT_NULL(hooks_section); /* plan must include this key */
+
+ /*
+ * config.toml must NOT appear as a hook-planned path.
+ * On buggy code the hooks_planned entry is:
+ * {"agent": "Codex CLI", "path": ".../.codex/config.toml"}
+ * which will make strstr(hooks_section, "config.toml") non-NULL -> FAIL.
+ *
+ * After the fix the hooks_planned entry names hooks.json instead, so
+ * "config.toml" does not appear in this section -> PASS.
+ */
+ const char *config_toml_in_hooks = strstr(hooks_section, "config.toml");
+ if (config_toml_in_hooks != NULL) {
+ printf(" BUG #570 reproduced: plan lists config.toml as a Codex hook target\n");
+ printf(" even though hooks.json already exists.\n");
+ printf(" hooks_planned section:\n %.400s\n", hooks_section);
+ }
+ ASSERT_NULL(config_toml_in_hooks);
+
+ /*
+ * SECONDARY assertion: hooks.json must appear as the hook target.
+ * After the fix the plan should list ~/.codex/hooks.json in hooks_planned.
+ * This assertion will also be RED on buggy code because the plan never
+ * mentions hooks.json at all (it uses config.toml instead).
+ */
+ const char *hooks_json_in_plan = strstr(hooks_section, "hooks.json");
+ if (hooks_json_in_plan == NULL) {
+ printf(" BUG #570: plan does not list hooks.json as Codex hook target.\n");
+ }
+ ASSERT_NOT_NULL(hooks_json_in_plan);
+
+ /*
+ * INVARIANT: config.toml must still appear in config_files_planned
+ * (that is the correct MCP config target), just not in hooks_planned.
+ * This confirms the plan is otherwise intact.
+ */
+ ASSERT(strstr(json, "config.toml") != NULL);
+
+ free(json);
+
+ /* Building the plan must not have created any actual config files. */
+ struct stat st;
+ char cfg[512];
+ snprintf(cfg, sizeof(cfg), "%s/.codex/config.toml", home);
+ ASSERT(stat(cfg, &st) != 0); /* config.toml must NOT have been created */
+
+ th_rmtree(home);
+ PASS();
+}
+
+/* ── Suite ──────────────────────────────────────────────────────────────── */
+SUITE(repro_issue570) {
+ RUN_TEST(repro_issue570_no_dual_hook_write);
+}
diff --git a/tests/repro/repro_main.c b/tests/repro/repro_main.c
index d8904b5b4..d3d739b75 100644
--- a/tests/repro/repro_main.c
+++ b/tests/repro/repro_main.c
@@ -44,6 +44,9 @@ extern void suite_repro_issue510(void);
extern void suite_repro_issue557(void);
extern void suite_repro_issue520(void);
extern void suite_repro_issue333(void);
+extern void suite_repro_issue570(void);
+extern void suite_repro_issue409(void);
+extern void suite_repro_issue431(void);
int main(void) {
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
@@ -75,6 +78,9 @@ int main(void) {
RUN_SUITE(repro_issue557);
RUN_SUITE(repro_issue520);
RUN_SUITE(repro_issue333);
+ RUN_SUITE(repro_issue570);
+ RUN_SUITE(repro_issue409);
+ RUN_SUITE(repro_issue431);
TEST_SUMMARY();
}