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(mcp,store): address QA round 1

Shane McCarron2791d0d8af05
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 5d27a5d40..97d9c3347 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -642,9 +642,14 @@ static cbm_store_t *resolve_store(cbm_mcp_server_t *srv, const char *project) {
char path[1024];
project_db_path(project, path, sizeof(path));
srv->store = cbm_store_open_path_query(path);
- srv->owns_store = true;
- free(srv->current_project);
- srv->current_project = heap_strdup(project);
+ if (srv->store) {
+ /* Only update ownership and cached project name on successful open.
+ * When the file is absent, store is NULL and current_project retains
+ * its previous value so the next call correctly retries the open. */
+ srv->owns_store = true;
+ free(srv->current_project);
+ srv->current_project = heap_strdup(project);
+ }
return srv->store;
}
@@ -748,8 +753,10 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {
/* verify_project_indexed — returns a heap-allocated error JSON string when the
* named project has not been indexed yet, or NULL when the project exists.
- * resolve_store uses SQLITE_OPEN_CREATE so store is always non-NULL even for
- * unindexed projects; this check catches that silent-empty case.
+ * resolve_store uses cbm_store_open_path_query (no SQLITE_OPEN_CREATE), so
+ * store is NULL for missing .db files (REQUIRE_STORE fires first). This
+ * function catches the remaining case: a .db file exists but has no indexed
+ * nodes (e.g., an empty or half-initialised project).
* Callers that receive a non-NULL return value must free(project) themselves
* before returning the error string. */
static char *verify_project_indexed(cbm_store_t *store, const char *project) {
diff --git a/tests/smoke_guard.sh b/tests/smoke_guard.sh
index 3d720c93a..37cf8b4df 100755
--- a/tests/smoke_guard.sh
+++ b/tests/smoke_guard.sh
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
# smoke_guard.sh — Smoke test for guard and ghost-file invariants.
#
-# Verifies two properties:
-# 1. Query handlers return "project not indexed" for unknown projects.
+# Verifies two properties across all 5 guarded query handlers:
+# 1. Each handler returns a guard error for unknown/unindexed projects.
# 2. No ghost .db file is created for the unknown project name.
#
# Usage: bash tests/smoke_guard.sh
@@ -15,6 +15,7 @@ BINARY="$PROJECT_ROOT/build/c/codebase-memory-mcp"
FAKE_PROJECT="nonexistent_smoke_test_xyz"
CACHE_DIR="${HOME}/.cache/codebase-memory-mcp"
GHOST_FILE="$CACHE_DIR/${FAKE_PROJECT}.db"
+FAILURES=0
# ── Step 1: Build ─────────────────────────────────────────────────
echo "[smoke_guard] Building project..."
@@ -31,29 +32,47 @@ if [ -f "$GHOST_FILE" ]; then
rm -f "$GHOST_FILE"
fi
-# ── Step 3: Invoke query tool with unknown project ────────────────
-echo "[smoke_guard] Invoking search_graph with project='$FAKE_PROJECT'..."
-RESPONSE="$("$BINARY" cli search_graph "{\"project\":\"$FAKE_PROJECT\",\"name_pattern\":\".*\"}" 2>/dev/null)"
-echo "[smoke_guard] Response: $RESPONSE"
-
-# ── Step 4: Assert error message present ─────────────────────────
-# For a truly absent project (no .db file), cbm_store_open_path_query returns
-# NULL, so REQUIRE_STORE fires with "no project loaded" before
-# verify_project_indexed is reached. Both messages confirm the guard is active.
-if ! echo "$RESPONSE" | grep -qE "no project loaded|not indexed"; then
- echo "[smoke_guard] FAIL: response does not contain guard error ('no project loaded' or 'not indexed')" >&2
- echo "[smoke_guard] Got: $RESPONSE" >&2
- exit 1
-fi
-echo "[smoke_guard] PASS: guard error message present"
+# ── Helper: assert guard error and no ghost file ──────────────────
+check_handler() {
+ local handler="$1"
+ local args="$2"
+ echo "[smoke_guard] Invoking $handler with project='$FAKE_PROJECT'..."
+ local response
+ response="$("$BINARY" cli "$handler" "$args" 2>/dev/null)"
+ echo "[smoke_guard] Response: $response"
-# ── Step 5: Assert no ghost .db file was created ─────────────────
-if [ -f "$GHOST_FILE" ]; then
- echo "[smoke_guard] FAIL: ghost file was created at $GHOST_FILE" >&2
- rm -f "$GHOST_FILE"
+ # For a missing .db file, cbm_store_open_path_query returns NULL so
+ # REQUIRE_STORE fires ("no project loaded"). For an empty .db,
+ # verify_project_indexed fires ("project not indexed"). Both are valid.
+ if ! echo "$response" | grep -qE "no project loaded|not indexed"; then
+ echo "[smoke_guard] FAIL [$handler]: response does not contain guard error" >&2
+ echo "[smoke_guard] Got: $response" >&2
+ FAILURES=$((FAILURES + 1))
+ else
+ echo "[smoke_guard] PASS [$handler]: guard error present"
+ fi
+
+ if [ -f "$GHOST_FILE" ]; then
+ echo "[smoke_guard] FAIL [$handler]: ghost file created at $GHOST_FILE" >&2
+ rm -f "$GHOST_FILE"
+ FAILURES=$((FAILURES + 1))
+ else
+ echo "[smoke_guard] PASS [$handler]: no ghost .db file"
+ fi
+}
+
+# ── Step 3: Test all 5 guarded handlers ───────────────────────────
+check_handler "search_graph" "{\"project\":\"$FAKE_PROJECT\",\"name_pattern\":\".*\"}"
+check_handler "query_graph" "{\"project\":\"$FAKE_PROJECT\",\"cypher\":\"MATCH (n) RETURN n LIMIT 1\"}"
+check_handler "get_graph_schema" "{\"project\":\"$FAKE_PROJECT\"}"
+check_handler "trace_call_path" "{\"project\":\"$FAKE_PROJECT\",\"function_name\":\"main\",\"direction\":\"both\",\"depth\":1}"
+check_handler "get_code_snippet" "{\"project\":\"$FAKE_PROJECT\",\"qualified_name\":\"main\"}"
+
+# ── Step 4: Final result ──────────────────────────────────────────
+if [ "$FAILURES" -gt 0 ]; then
+ echo "[smoke_guard] FAILED: $FAILURES check(s) failed." >&2
exit 1
fi
-echo "[smoke_guard] PASS: no ghost .db file created"
-echo "[smoke_guard] All checks passed."
+echo "[smoke_guard] All checks passed (5 handlers, guard + ghost-file invariants)."
exit 0