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(store): report a failed COUNT read instead of returning zero

kavish-1999c3bb4cbe4c
diff --git a/src/store/store.c b/src/store/store.c
index c36f00b44..d2369c1f9 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -3069,7 +3069,11 @@ int cbm_store_count_nodes(cbm_store_t *s, const char *project) {
}
bind_text(stmt, SKIP_ONE, project);
- int count = 0;
+ /* A step that does not yield a row is a failed read (SQLITE_CORRUPT,
+ * SQLITE_BUSY, SQLITE_IOERR), not a count of zero. Report it through the
+ * error channel this function already uses for a failed prepare, so a
+ * caller cannot mistake an unreadable table for an empty project. */
+ int count = CBM_STORE_ERR;
if (sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
@@ -3389,7 +3393,10 @@ int cbm_store_count_edges(cbm_store_t *s, const char *project) {
}
bind_text(stmt, SKIP_ONE, project);
- int count = 0;
+ /* See cbm_store_count_nodes: a non-row step is a failed read, not zero.
+ * index_status reads this alongside the node count and already treats a
+ * negative value as degraded. */
+ int count = CBM_STORE_ERR;
if (sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
diff --git a/tests/test_store_nodes.c b/tests/test_store_nodes.c
index cf7b8bc8f..c08beadad 100644
--- a/tests/test_store_nodes.c
+++ b/tests/test_store_nodes.c
@@ -2030,6 +2030,37 @@ TEST(store_count_nodes_unknown_project) {
PASS();
}
+/* A COUNT(*) that cannot be read must not be reported as a count of zero:
+ * index_status renders that as the positive assertion status "empty", so a
+ * corrupt project looks like one that was never indexed. Dropping the table
+ * after the statement is cached makes the step fail deterministically. */
+TEST(store_count_failed_read_is_not_zero) {
+ cbm_store_t *s = cbm_store_open_memory();
+ cbm_store_upsert_project(s, "test", "/tmp/test");
+
+ cbm_node_t n = {.project = "test",
+ .label = "File",
+ .name = "main.c",
+ .qualified_name = "test.main.c",
+ .file_path = "main.c"};
+ cbm_store_upsert_node(s, &n);
+
+ /* Sanity: a readable table still counts normally. */
+ ASSERT_EQ(cbm_store_count_nodes(s, "test"), 1);
+ ASSERT_TRUE(cbm_store_count_edges(s, "test") >= 0);
+
+ /* Make the read fail. The statements are cached by the calls above, so the
+ * step (not the prepare) is what fails once the tables are gone. */
+ ASSERT_EQ(cbm_store_exec(s, "DROP TABLE nodes;"), 0);
+ ASSERT_EQ(cbm_store_exec(s, "DROP TABLE edges;"), 0);
+
+ ASSERT_TRUE(cbm_store_count_nodes(s, "test") < 0);
+ ASSERT_TRUE(cbm_store_count_edges(s, "test") < 0);
+
+ cbm_store_close(s);
+ PASS();
+}
+
/* ── Index coverage (#963) ──────────────────────────────────────── */
/* Round-trip + deleted-file prune + shadow miss-graph materialization +
@@ -2432,4 +2463,5 @@ SUITE(store_nodes) {
RUN_TEST(store_node_properties_special_chars);
RUN_TEST(store_delete_nodes_nonexistent);
RUN_TEST(store_count_nodes_unknown_project);
+ RUN_TEST(store_count_failed_read_is_not_zero);
}