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.

fix(hook): surface daemon build conflicts to the hook caller (#1388)

Martin Vogel716f1d683c2e
diff --git a/scripts/test.sh b/scripts/test.sh
index 2d34486d8..815f02561 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -282,6 +282,12 @@ CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_worker_watchdog.sh"
echo "=== Step 5c: worker error-response transport regression ==="
bash "$ROOT/tests/test_worker_error_response.sh"
+# Step 5d: a hook client facing a daemon BUILD CONFLICT must surface a stdout
+# systemMessage (stderr is invisible to the hook caller). Needs the TEST_SEAMS
+# binary from step 5's build (CBM_TEST_HOOK_CLIENT_BUILD forces the conflict).
+echo "=== Step 5d: hook daemon-conflict notice regression (#1388) ==="
+bash "$ROOT/tests/test_hook_conflict_notice.sh"
+
# Step 6: security-strings URL allow-list regression. The MSYS2 CLANG64 toolchain
# bakes its package-tracker URL into the static Windows .exe; the binary string
# audit must allow-list it (Windows-only — Linux smoke never saw it).
diff --git a/src/main.c b/src/main.c
index a6645ab07..b12ebc77e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1493,6 +1493,24 @@ static void main_hook_report_absent_daemon(const char *hook_dialect) {
}
}
+/* #1388: a version-conflicted daemon is an actionable broken state, unlike a
+ * merely absent one - and stdout is the only hook channel Claude Code
+ * surfaces, so stderr-only reporting reads as eternal silence in-session.
+ * Emit a throttled systemMessage with restart guidance (the misleading
+ * "no daemon is running" text pointed at `daemon start`, which cannot heal a
+ * build conflict). */
+static void main_hook_report_conflicted_daemon(const char *hook_dialect) {
+ if (hook_dialect || !main_hook_absent_notice_due()) {
+ return;
+ }
+ (void)fputs("{\"systemMessage\":\"codebase-memory-mcp: graph augmentation is skipped: the "
+ "active CBM daemon runs a different build than this binary (usually an update "
+ "was installed while the old daemon kept running). Run `codebase-memory-mcp "
+ "daemon stop`, then retry - the next command starts a matching daemon.\"}",
+ stdout);
+ (void)fflush(stdout);
+}
+
static int main_run_hook_frontend(cbm_daemon_runtime_client_t *client, const char *hook_event,
const char *hook_dialect) {
char *input = cbm_hook_augment_read_stdin();
@@ -2148,6 +2166,18 @@ int main(int argc, char **argv) {
return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
+#ifdef CBM_ENABLE_TEST_SEAMS
+ /* #1388 test seam: let one binary present a foreign build fingerprint as a
+ * hook client, so the daemon-conflict reporting path is testable without
+ * building a second binary. */
+ static char seam_hook_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
+ const char *seam_forced_build = cbm_safe_getenv("CBM_TEST_HOOK_CLIENT_BUILD", seam_hook_build,
+ sizeof(seam_hook_build), NULL);
+ if (role == CBM_DAEMON_PROCESS_HOOK_CLIENT && seam_forced_build && seam_forced_build[0]) {
+ identity.build_fingerprint = seam_hook_build;
+ }
+#endif
+
cbm_version_cohort_manager_t *client_cohort_manager = cbm_version_cohort_manager_new(endpoint);
cbm_version_cohort_lease_t *client_cohort_lease = NULL;
cbm_daemon_conflict_t client_cohort_conflict;
@@ -2169,6 +2199,10 @@ int main(int argc, char **argv) {
}
(void)fprintf(stderr, "codebase-memory-mcp: %s\n",
formatted ? message : "client exact-build admission failed");
+ if (role == CBM_DAEMON_PROCESS_HOOK_CLIENT &&
+ client_cohort_status == CBM_VERSION_COHORT_CONFLICT) {
+ main_hook_report_conflicted_daemon(hook_dialect);
+ }
(void)main_version_cohort_close(&client_cohort_lease, &client_cohort_manager);
cbm_daemon_ipc_endpoint_free(endpoint);
return role == CBM_DAEMON_PROCESS_HOOK_CLIENT ? EXIT_SUCCESS : EXIT_FAILURE;
@@ -2181,7 +2215,16 @@ int main(int argc, char **argv) {
endpoint, &identity, MAIN_HOOK_CONNECT_TIMEOUT_MS, &hook_connect);
cbm_daemon_ipc_endpoint_free(endpoint);
if (!hook_client) {
- main_hook_report_absent_daemon(hook_dialect);
+ if (hook_connect.status == CBM_DAEMON_RUNTIME_CONNECT_CONFLICT) {
+ char conflict_detail[CBM_DAEMON_CONFLICT_MESSAGE_SIZE];
+ if (cbm_daemon_conflict_format(&hook_connect.conflict, conflict_detail,
+ sizeof(conflict_detail))) {
+ (void)fprintf(stderr, "codebase-memory-mcp: %s\n", conflict_detail);
+ }
+ main_hook_report_conflicted_daemon(hook_dialect);
+ } else {
+ main_hook_report_absent_daemon(hook_dialect);
+ }
(void)main_version_cohort_close(&client_cohort_lease, &client_cohort_manager);
return EXIT_SUCCESS;
}
diff --git a/tests/test_hook_conflict_notice.sh b/tests/test_hook_conflict_notice.sh
new file mode 100644
index 000000000..cc678a138
--- /dev/null
+++ b/tests/test_hook_conflict_notice.sh
@@ -0,0 +1,58 @@
+#!/usr/bin/env bash
+# #1388: a hook client that cannot join because the active daemon runs a
+# DIFFERENT build must say so on stdout (systemMessage) - stdout is the only
+# hook channel Claude Code surfaces, so stderr-only reporting reads as eternal
+# silence in-session. Requires a TEST_SEAMS=1 binary (scripts/test.sh step 5
+# builds one): CBM_TEST_HOOK_CLIENT_BUILD forces the client fingerprint.
+set -euo pipefail
+
+ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+BINARY="${ROOT}/build/c/codebase-memory-mcp"
+if [[ ! -x "${BINARY}" && -x "${BINARY}.exe" ]]; then
+ BINARY="${BINARY}.exe"
+fi
+if [[ ! -x "${BINARY}" ]]; then
+ echo "missing binary: ${BINARY}" >&2
+ exit 2
+fi
+
+tmpdir="$(mktemp -d)"
+cleanup() {
+ CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon stop >/dev/null 2>&1 || true
+ rm -rf "${tmpdir}"
+}
+trap cleanup EXIT
+
+CBM_CACHE_DIR="${tmpdir}/cache" "${BINARY}" daemon start >/dev/null 2>&1
+payload='{"session_id":"probe","hook_event_name":"PreToolUse","tool_name":"Grep","tool_input":{"pattern":"x","path":"/tmp"},"cwd":"/tmp"}'
+forced_build="$(printf 'f%.0s' $(seq 1 64))"
+
+set +e
+out="$(printf '%s' "${payload}" | CBM_CACHE_DIR="${tmpdir}/cache" \
+ CBM_TEST_HOOK_CLIENT_BUILD="${forced_build}" \
+ "${BINARY}" hook-augment 2>"${tmpdir}/probe.err")"
+rc=$?
+set -e
+
+if [[ ${rc} -ne 0 ]]; then
+ echo "hook-augment must fail open (exit 0); got ${rc}" >&2
+ cat "${tmpdir}/probe.err" >&2
+ exit 1
+fi
+if ! grep -q "conflicting CBM process" "${tmpdir}/probe.err"; then
+ echo "seam did not produce a build conflict - is this a TEST_SEAMS=1 binary?" >&2
+ cat "${tmpdir}/probe.err" >&2
+ exit 2
+fi
+if ! printf '%s' "${out}" | grep -q "systemMessage"; then
+ echo "conflicted daemon must surface a stdout systemMessage to the hook caller (#1388)" >&2
+ echo "stdout was: ${out}" >&2
+ exit 1
+fi
+if ! printf '%s' "${out}" | grep -q "daemon stop"; then
+ echo "the systemMessage must carry the daemon-restart guidance (#1388)" >&2
+ echo "stdout was: ${out}" >&2
+ exit 1
+fi
+
+echo "ok: daemon build conflicts reach the hook caller as a systemMessage"