MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_install_hook_gate_script

Function cbm_install_hook_gate_script

src/cli/cli.c:2046–2092  ·  view source on GitHub ↗

Install the search-augmenter shim to ~/.claude/hooks/. * The shim is a thin wrapper that delegates to ` hook-augment`, * which adds graph context to Grep/Glob calls. It NEVER blocks a tool call: * a missing/old/hung binary results in a silent exit 0 (issue #362/#288). * The legacy filename `cbm-code-discovery-gate` is retained so existing * settings.json entries and uninstall keep wor

Source from the content-addressed store, hash-verified

2044 * The legacy filename `cbm-code-discovery-gate` is retained so existing
2045 * settings.json entries and uninstall keep working with zero migration. */
2046void cbm_install_hook_gate_script(const char *home, const char *binary_path) {
2047 if (!home || !binary_path) {
2048 return;
2049 }
2050 /* Defensive: refuse to embed a binary path containing a double-quote, which
2051 * would break the BIN="..." shell quoting in the generated shim. In normal
2052 * installs this is unreachable (paths come from cbm_detect_self_path), but
2053 * fail-loud here beats silently emitting a malformed script. */
2054 if (strchr(binary_path, '"') != NULL) {
2055 return;
2056 }
2057 char config_dir[CLI_BUF_1K];
2058 cbm_claude_config_dir(home, config_dir, sizeof(config_dir));
2059 if (!config_dir[0]) {
2060 return;
2061 }
2062 char hooks_dir[CLI_BUF_1K];
2063 snprintf(hooks_dir, sizeof(hooks_dir), "%s/hooks", config_dir);
2064 cbm_mkdir_p(hooks_dir, CLI_OCTAL_PERM);
2065
2066 char script_path[CLI_BUF_1K];
2067 snprintf(script_path, sizeof(script_path), "%s/" CMM_HOOK_GATE_SCRIPT, hooks_dir);
2068
2069 FILE *f = fopen(script_path, "w");
2070 if (!f) {
2071 return;
2072 }
2073 (void)fprintf(f,
2074 "#!/usr/bin/env bash\n"
2075 "# codebase-memory-mcp search augmenter (Claude Code PreToolUse).\n"
2076 "# NOTE: the legacy filename is kept for zero-migration upgrades.\n"
2077 "# Despite the name this NEVER blocks a tool call - it only adds\n"
2078 "# graph context. Any failure is silent (exit 0, no output).\n"
2079 "BIN=\"%s\"\n"
2080 "[ -x \"$BIN\" ] || exit 0\n"
2081 "\"$BIN\" hook-augment 2>/dev/null\n"
2082 "exit 0\n",
2083 binary_path);
2084 /* fchmod before close to avoid TOCTOU race (CodeQL cpp/toctou-race-condition) */
2085#ifndef _WIN32
2086 fchmod(fileno(f), CLI_OCTAL_PERM);
2087#endif
2088 (void)fclose(f);
2089#ifdef _WIN32
2090 chmod(script_path, CLI_OCTAL_PERM);
2091#endif
2092}
2093
2094/* SessionStart hook: remind agent to use MCP tools on every context reset. */
2095#define CMM_SESSION_REMINDER_SCRIPT "cbm-session-reminder"

Callers 3

test_cli.cFile · 0.85
repro_issue409.cFile · 0.85

Calls 2

cbm_claude_config_dirFunction · 0.85
cbm_mkdir_pFunction · 0.85

Tested by

no test coverage detected