|
| 1 | +/* |
| 2 | + * repro_issue409.c — Reproduce-first case for OPEN bug #409. |
| 3 | + * |
| 4 | + * Issue #409: "v0.7.0 install/update wires the legacy blocking PreToolUse |
| 5 | + * gate, not hook_augment (regresses #214)" |
| 6 | + * |
| 7 | + * Root cause (as filed): |
| 8 | + * cbm_install_hook_gate_script wrote the legacy blocking shell gate |
| 9 | + * (keyed on $PPID, emitting `exit 2` to block tool calls) instead of the |
| 10 | + * non-blocking augmenter shim that delegates to `<binary> hook-augment`. |
| 11 | + * On an upgrade from a pre-v0.7.0 install the old gate script remained on |
| 12 | + * disk (or was rewritten with blocking content), so every Grep/Glob call |
| 13 | + * was blocked rather than being non-blocking augmented — the exact symptom |
| 14 | + * of #214 which was supposed to be fixed. |
| 15 | + * |
| 16 | + * Expected (correct) behaviour after cbm_upsert_claude_hooks + |
| 17 | + * cbm_install_hook_gate_script: |
| 18 | + * 1. The gate script written to |
| 19 | + * <home>/.claude/hooks/cbm-code-discovery-gate |
| 20 | + * MUST contain "hook-augment" (delegating to the compiled augmenter). |
| 21 | + * 2. The gate script MUST NOT contain "PPID" (the $PPID-keyed blocking |
| 22 | + * logic) or "exit 2" (the blocking exit code). |
| 23 | + * 3. The settings.json PreToolUse command must reference |
| 24 | + * "cbm-code-discovery-gate" (the shim), not an inline blocking script. |
| 25 | + * |
| 26 | + * Actual (buggy) behaviour (if bug is present): |
| 27 | + * The gate script still contains $PPID and exit 2; the assertions below |
| 28 | + * that check for absence of "PPID" and "exit 2" FAIL -> RED. |
| 29 | + * |
| 30 | + * Upgrade scenario tested here (NOT covered by existing tests): |
| 31 | + * This test simulates an upgrade from a pre-v0.7.0 install by: |
| 32 | + * a) Pre-seeding the gate-script path with the OLD blocking content |
| 33 | + * (containing $PPID and exit 2) — as would be present on disk after |
| 34 | + * a pre-v0.7.0 install. |
| 35 | + * b) Pre-seeding settings.json with a stale CMM hook entry using the |
| 36 | + * old "Grep|Glob|Read" matcher and an old command string. |
| 37 | + * Then running both cbm_upsert_claude_hooks + cbm_install_hook_gate_script |
| 38 | + * (the actual install/update code path) and asserting the CORRECT result. |
| 39 | + * |
| 40 | + * This is the critical gap: existing tests call cbm_install_hook_gate_script |
| 41 | + * into an EMPTY directory (no pre-existing script). The upgrade path |
| 42 | + * (old script on disk) was not verified to be overwritten correctly. |
| 43 | + * |
| 44 | + * Relationship to existing tests: |
| 45 | + * cli_hook_gate_script_no_predictable_tmp_issue384 (test_cli.c:2196): |
| 46 | + * Tests cbm_install_hook_gate_script in isolation on a fresh dir. |
| 47 | + * Does NOT test the upgrade/overwrite scenario. |
| 48 | + * cli_upsert_claude_hook_fresh (test_cli.c:2167): |
| 49 | + * Tests cbm_upsert_claude_hooks in isolation on fresh settings.json. |
| 50 | + * Does NOT test the integrated (both calls) upgrade path. |
| 51 | + * |
| 52 | + * NOTE (2026-06-26): Code review of the current codebase shows that |
| 53 | + * cbm_install_hook_gate_script already uses fopen(path, "w") (truncate) |
| 54 | + * and writes the non-blocking shim. If this test is GREEN it means the bug |
| 55 | + * is fixed on main and the issue can be closed (the test then acts as a |
| 56 | + * permanent regression guard for this upgrade scenario). |
| 57 | + */ |
| 58 | + |
| 59 | +#include <foundation/compat.h> |
| 60 | +#include "test_framework.h" |
| 61 | +#include "test_helpers.h" |
| 62 | +#include <cli/cli.h> |
| 63 | +#include <string.h> |
| 64 | +#include <stdlib.h> |
| 65 | +#include <stdio.h> |
| 66 | +#include <sys/stat.h> |
| 67 | +#include <unistd.h> |
| 68 | +#include <errno.h> |
| 69 | + |
| 70 | +/* ── Local helpers (mirror the helpers in test_cli.c) ──────────────── */ |
| 71 | + |
| 72 | +static int rp409_write_file(const char *path, const char *content) { |
| 73 | + FILE *f = fopen(path, "w"); |
| 74 | + if (!f) |
| 75 | + return -1; |
| 76 | + fprintf(f, "%s", content); |
| 77 | + fclose(f); |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +static const char *rp409_read_file(const char *path) { |
| 82 | + static char buf[16384]; |
| 83 | + FILE *f = fopen(path, "r"); |
| 84 | + if (!f) |
| 85 | + return NULL; |
| 86 | + size_t n = fread(buf, 1, sizeof(buf) - 1, f); |
| 87 | + fclose(f); |
| 88 | + buf[n] = '\0'; |
| 89 | + return buf; |
| 90 | +} |
| 91 | + |
| 92 | +/* Recursively create directory (simple two-level: parent + child). */ |
| 93 | +static int rp409_mkdirp(const char *path) { |
| 94 | + char tmp[1024]; |
| 95 | + snprintf(tmp, sizeof(tmp), "%s", path); |
| 96 | + for (char *p = tmp + 1; *p; p++) { |
| 97 | + if (*p == '/') { |
| 98 | + *p = '\0'; |
| 99 | + cbm_mkdir(tmp); |
| 100 | + *p = '/'; |
| 101 | + } |
| 102 | + } |
| 103 | + return cbm_mkdir(tmp) == 0 || errno == EEXIST ? 0 : -1; |
| 104 | +} |
| 105 | + |
| 106 | +/* ── Test ──────────────────────────────────────────────────────────── */ |
| 107 | + |
| 108 | +/* |
| 109 | + * repro_issue409_install_wires_hook_augment_not_blocking_gate |
| 110 | + * |
| 111 | + * Simulates an upgrade from a pre-v0.7.0 install: |
| 112 | + * - The hooks dir already contains the OLD blocking gate script |
| 113 | + * (containing $PPID and exit 2). |
| 114 | + * - settings.json already contains a stale CMM hook with the old matcher |
| 115 | + * "Grep|Glob|Read" and an old inline command. |
| 116 | + * |
| 117 | + * After calling cbm_upsert_claude_hooks + cbm_install_hook_gate_script |
| 118 | + * (the actual install/update flow), asserts that: |
| 119 | + * 1. The gate script is OVERWRITTEN with the non-blocking shim |
| 120 | + * (contains "hook-augment", does NOT contain "PPID" or "exit 2"). |
| 121 | + * 2. settings.json PreToolUse command references "cbm-code-discovery-gate" |
| 122 | + * (the shim path), not inline blocking code. |
| 123 | + * 3. settings.json uses the current non-blocking matcher "Grep|Glob" |
| 124 | + * (not the old "Grep|Glob|Read" that was silently upgrading Read-gating |
| 125 | + * behaviour). |
| 126 | + * |
| 127 | + * RED if: |
| 128 | + * - The gate script still contains "PPID" (old blocking logic not cleared) |
| 129 | + * - The gate script still contains "exit 2" (old blocking exit not cleared) |
| 130 | + * - The gate script does NOT contain "hook-augment" (shim not written) |
| 131 | + * - settings.json does NOT contain "cbm-code-discovery-gate" (wrong command) |
| 132 | + * |
| 133 | + * Oracle used: cbm_upsert_claude_hooks(settings_path) + |
| 134 | + * cbm_install_hook_gate_script(home, binary_path) |
| 135 | + * (the same two calls made by install_claude_code_config in cli.c). |
| 136 | + */ |
| 137 | +TEST(repro_issue409_install_wires_hook_augment_not_blocking_gate) { |
| 138 | + /* Create a temp HOME directory tree that simulates a pre-v0.7.0 install. */ |
| 139 | + char tmpdir[256]; |
| 140 | + snprintf(tmpdir, sizeof(tmpdir), "/tmp/rp409-XXXXXX"); |
| 141 | + if (!cbm_mkdtemp(tmpdir)) |
| 142 | + FAIL("cbm_mkdtemp failed"); |
| 143 | + |
| 144 | + /* Create <home>/.claude/hooks/ (mirrors real Claude Code layout). */ |
| 145 | + char hooks_dir[512]; |
| 146 | + snprintf(hooks_dir, sizeof(hooks_dir), "%s/.claude/hooks", tmpdir); |
| 147 | + if (rp409_mkdirp(hooks_dir) != 0) |
| 148 | + FAIL("mkdirp hooks_dir failed"); |
| 149 | + |
| 150 | + /* Pre-seed the gate script with the OLD blocking content that the issue |
| 151 | + * reporter observed on v0.7.0. This is the content that must be |
| 152 | + * overwritten (truncated) by cbm_install_hook_gate_script. */ |
| 153 | + char script_path[512]; |
| 154 | + snprintf(script_path, sizeof(script_path), |
| 155 | + "%s/cbm-code-discovery-gate", hooks_dir); |
| 156 | + rp409_write_file(script_path, |
| 157 | + "#!/bin/bash\n" |
| 158 | + "# Gate hook: nudges Claude toward codebase-memory-mcp for code discovery.\n" |
| 159 | + "# First Grep/Glob/Read per session -> block. Subsequent -> allow.\n" |
| 160 | + "# PPID = Claude Code process PID, unique per session.\n" |
| 161 | + "GATE=/tmp/cbm-code-discovery-gate-$PPID\n" |
| 162 | + "if [ -f \"$GATE\" ]; then exit 0; fi\n" |
| 163 | + "touch \"$GATE\"\n" |
| 164 | + "echo 'BLOCKED: use codebase-memory-mcp' >&2\n" |
| 165 | + "exit 2\n"); |
| 166 | + |
| 167 | + /* Pre-seed settings.json with a stale CMM hook entry (old matcher). */ |
| 168 | + char settings_path[512]; |
| 169 | + snprintf(settings_path, sizeof(settings_path), |
| 170 | + "%s/.claude/settings.json", tmpdir); |
| 171 | + rp409_write_file(settings_path, |
| 172 | + "{\"hooks\":{\"PreToolUse\":[" |
| 173 | + "{\"matcher\":\"Grep|Glob|Read\"," |
| 174 | + "\"hooks\":[{\"type\":\"command\"," |
| 175 | + "\"command\":\"~/.claude/hooks/cbm-code-discovery-gate\"}]}]}}"); |
| 176 | + |
| 177 | + /* Run the actual install/update hook wiring (same two calls as |
| 178 | + * install_claude_code_config in src/cli/cli.c lines 3045-3046). */ |
| 179 | + int rc = cbm_upsert_claude_hooks(settings_path); |
| 180 | + ASSERT_EQ(rc, 0); |
| 181 | + cbm_install_hook_gate_script(tmpdir, "/usr/local/bin/codebase-memory-mcp"); |
| 182 | + |
| 183 | + /* ── Assert the gate script was OVERWRITTEN with the non-blocking shim ── */ |
| 184 | + const char *script_data = rp409_read_file(script_path); |
| 185 | + ASSERT_NOT_NULL(script_data); |
| 186 | + |
| 187 | + /* MUST NOT contain $PPID: the old blocking gate used |
| 188 | + * /tmp/cbm-code-discovery-gate-$PPID as a per-invocation state file. |
| 189 | + * If present, the blocking gate was not overwritten -> RED for #409. */ |
| 190 | + ASSERT(strstr(script_data, "PPID") == NULL); |
| 191 | + |
| 192 | + /* MUST NOT contain "exit 2": the old gate blocked tool calls with exit 2. |
| 193 | + * If present, the installer still emits the blocking exit code -> RED. */ |
| 194 | + ASSERT(strstr(script_data, "exit 2") == NULL); |
| 195 | + |
| 196 | + /* MUST contain "hook-augment": the non-blocking shim delegates to the |
| 197 | + * compiled augmenter via `"$BIN" hook-augment 2>/dev/null`. |
| 198 | + * If absent, install did not write the correct shim -> RED for #409. */ |
| 199 | + ASSERT(strstr(script_data, "hook-augment") != NULL); |
| 200 | + |
| 201 | + /* ── Assert settings.json was updated to the correct non-blocking config ── */ |
| 202 | + const char *settings_data = rp409_read_file(settings_path); |
| 203 | + ASSERT_NOT_NULL(settings_data); |
| 204 | + |
| 205 | + /* The PreToolUse command must reference the shim (by its well-known name), |
| 206 | + * not an inline blocking script. */ |
| 207 | + ASSERT(strstr(settings_data, "cbm-code-discovery-gate") != NULL); |
| 208 | + |
| 209 | + /* The old "Grep|Glob|Read" matcher (which gated Read calls, breaking |
| 210 | + * the read-before-edit invariant per issue #362) must have been replaced |
| 211 | + * with the current "Grep|Glob" matcher. */ |
| 212 | + ASSERT(strstr(settings_data, "\"Grep|Glob\"") != NULL); |
| 213 | + ASSERT(strstr(settings_data, "Glob|Read") == NULL); |
| 214 | + |
| 215 | + th_rmtree(tmpdir); |
| 216 | + PASS(); |
| 217 | +} |
| 218 | + |
| 219 | +/* ── Suite ──────────────────────────────────────────────────────────── */ |
| 220 | +SUITE(repro_issue409) { |
| 221 | + RUN_TEST(repro_issue409_install_wires_hook_augment_not_blocking_gate); |
| 222 | +} |
0 commit comments