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(pipeline): capture committed_nodes before dump frees the index (#334)

sorena0a9c2680944
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 4d4cc231d..2393a3775 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -233,6 +233,13 @@ void cbm_pipeline_get_committed_counts(const cbm_pipeline_t *p, int *nodes, int
}
}
+void cbm_pipeline_set_committed_counts(cbm_pipeline_t *p, int nodes, int edges) {
+ if (p) {
+ p->committed_nodes = nodes;
+ p->committed_edges = edges;
+ }
+}
+
/* Resolve the DB path for this pipeline. Caller must free(). */
static char *resolve_db_path(const cbm_pipeline_t *p) {
char *path = malloc(CBM_SZ_1K);
@@ -847,13 +854,17 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil
*last_slash = '\0';
cbm_mkdir_p(db_dir, CBM_DIR_PERMS);
}
+ /* Capture committed counts BEFORE the dump. cbm_gbuf_dump_to_sqlite calls
+ * release_gbuf_indexes(), which frees node_by_qn (graph_buffer.c), after
+ * which cbm_gbuf_node_count() returns 0. Reading these post-dump left
+ * committed_nodes at 0, so the #334 plausibility gate never fired. */
+ p->committed_nodes = cbm_gbuf_node_count(p->gbuf);
+ p->committed_edges = cbm_gbuf_edge_count(p->gbuf);
int rc = cbm_gbuf_dump_to_sqlite(p->gbuf, db_path);
if (rc != 0) {
cbm_log_error("pipeline.err", "phase", "dump");
return rc;
}
- p->committed_nodes = cbm_gbuf_node_count(p->gbuf);
- p->committed_edges = cbm_gbuf_edge_count(p->gbuf);
cbm_log_info("pass.timing", "pass", "dump", "elapsed_ms", itoa_buf((int)elapsed_ms(*t)));
cbm_store_t *hash_store = cbm_store_open_path(db_path);
if (hash_store) {
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index aa835faf0..a1cc44820 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -845,6 +845,11 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
* reindex can correctly classify those files instead of seeing them
* as never-existed; also exports a fast-mode artifact when one is
* already present alongside the repo). */
+ /* Record committed counts before dump_and_persist (whose dump frees the
+ * gbuf node index, zeroing the count) so the #334 plausibility gate also
+ * covers incremental reindexes, not just full ones. */
+ cbm_pipeline_set_committed_counts(p, cbm_gbuf_node_count(existing),
+ cbm_gbuf_edge_count(existing));
dump_and_persist(existing, db_path, project, files, file_count, mode_skipped,
mode_skipped_count, cbm_pipeline_repo_path(p));
free_mode_skipped(mode_skipped, mode_skipped_count);
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 1eb108421..aff9659c5 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -532,6 +532,9 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
/* Pipeline accessors for incremental use */
const char *cbm_pipeline_repo_path(const cbm_pipeline_t *p);
atomic_int *cbm_pipeline_cancelled_ptr(cbm_pipeline_t *p);
+/* Record committed graph size (#334 gate axis) from the incremental path,
+ * which cannot see the opaque cbm_pipeline struct. Call before the dump. */
+void cbm_pipeline_set_committed_counts(cbm_pipeline_t *p, int nodes, int edges);
/* Parse a gRPC stub call "<service-stub>.<method>" into the canonical proto
* service name + method. Returns true ONLY when a recognized gRPC stub/client
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index d472ba00a..98b4204fd 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -12,6 +12,7 @@
#include "pipeline/pipeline_internal.h"
#include "store/store.h"
#include "git/git_context.h"
+#include "foundation/dump_verify.h"
#include <stdlib.h>
#include <string.h>
@@ -6030,6 +6031,48 @@ TEST(pipeline_complexity_transitive_loop_depth) {
PASS();
}
+/* Regression for #334: the plausibility gate compares committed (extracted)
+ * node count against persisted rows. committed_nodes must be captured BEFORE
+ * cbm_gbuf_dump_to_sqlite frees the gbuf node index — otherwise it reads 0 and
+ * the gate is silently inert. Drives the real pipeline (not a synthetic count)
+ * and asserts committed_nodes is populated and matches what was persisted. */
+TEST(pipeline_committed_counts_match_persisted) {
+ if (setup_test_repo() != 0) {
+ FAIL("failed to create temp dir");
+ }
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/committed_test.db", g_tmpdir);
+
+ cbm_pipeline_t *p = cbm_pipeline_new(g_tmpdir, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(p);
+ int rc = cbm_pipeline_run(p);
+ ASSERT_EQ(rc, 0);
+
+ int committed_nodes = -1;
+ int committed_edges = -1;
+ cbm_pipeline_get_committed_counts(p, &committed_nodes, &committed_edges);
+
+ cbm_store_t *s = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(s);
+ const char *project = cbm_pipeline_project_name(p);
+ int persisted_nodes = cbm_store_count_nodes(s, project);
+ cbm_store_close(s);
+
+ /* The bug captured committed_nodes after the node index was freed → 0. */
+ ASSERT_GT(committed_nodes, 0);
+ /* A faithful full dump persists exactly what it committed. */
+ ASSERT_EQ(committed_nodes, persisted_nodes);
+ /* The gate must NOT flag a healthy full index as degraded. */
+ ASSERT_FALSE(cbm_dump_verify_is_degraded(committed_nodes, persisted_nodes,
+ CBM_DUMP_VERIFY_DEFAULT_RATIO,
+ CBM_DUMP_VERIFY_MIN_FLOOR));
+
+ cbm_pipeline_free(p);
+ teardown_test_repo();
+ PASS();
+}
+
SUITE(pipeline) {
/* Index lock */
RUN_TEST(pipeline_lock_try_acquire);
@@ -6048,6 +6091,7 @@ SUITE(pipeline) {
RUN_TEST(store_bulk_persistence);
/* Integration: structure pass */
RUN_TEST(pipeline_structure_nodes);
+ RUN_TEST(pipeline_committed_counts_match_persisted);
RUN_TEST(pipeline_structure_edges);
RUN_TEST(pipeline_branch_root_structure);
RUN_TEST(pipeline_project_name_derived);