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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(routes): make Route extraction independent of worker merge order
Martin Vogel6b48097471e8
diff --git a/src/pipeline/pass_route_nodes.c b/src/pipeline/pass_route_nodes.cindex b1fac1670..878dd7f50 100644--- a/src/pipeline/pass_route_nodes.c+++ b/src/pipeline/pass_route_nodes.c@@ -322,9 +322,34 @@ static bool is_broker_route(const char *qn) {/* Try to match a single infra Route to a handler Route and create HANDLES bridge.* Returns 1 if matched, 0 otherwise. */+/* Total order over candidates, so "which handler" never depends on the order+ * nodes happened to land in the graph buffer. Qualified names are unique per+ * node, so this is a tie-break that always decides; a NULL name sorts last and+ * therefore only wins when nothing else matched. */+static bool infra_candidate_is_better(const cbm_gbuf_node_t *candidate,+ const cbm_gbuf_node_t *best) {+ if (!best) {+ return true;+ }+ if (!candidate->qualified_name) {+ return false;+ }+ if (!best->qualified_name) {+ return true;+ }+ return strcmp(candidate->qualified_name, best->qualified_name) < 0;+}+static int match_one_infra_route(cbm_gbuf_t *gb, const cbm_gbuf_node_t *infra,const char *infra_path, const char *svc_name,const cbm_gbuf_node_t **all_routes, int route_count) {+ /* The FIRST match in array order used to win, and that array is in graph+ * buffer insertion order — parallel worker interleaving. Two runs of the+ * same corpus linked the same infra route to different handlers, so the+ * HANDLES edge set moved between runs (kubernetes, 2026-09-18: 229 vs 242+ * HANDLES with an identical node set). Collect the best candidate under a+ * total order instead, and match on it. */+ const cbm_gbuf_node_t *best = NULL;for (int j = 0; j < route_count; j++) {const cbm_gbuf_node_t *handler_route = all_routes[j];if (is_broker_route(handler_route->qualified_name)) {@@ -352,18 +377,22 @@ static int match_one_infra_route(cbm_gbuf_t *gb, const cbm_gbuf_node_t *infra,strstr(handler_path, infra_path) != NULL));int root_svc_match = (strcmp(handler_path, "/") == 0);if (path_match || root_svc_match) {- const cbm_gbuf_edge_t **fn_handles = NULL;- int fn_hcount = 0;- cbm_gbuf_find_edges_by_target_type(gb, handler_route->id, "HANDLES", &fn_handles,- &fn_hcount);- for (int fh = 0; fh < fn_hcount; fh++) {- cbm_gbuf_insert_edge(gb, fn_handles[fh]->source_id, infra->id, "HANDLES",- "{\"source\":\"infra_match\"}");+ if (infra_candidate_is_better(handler_route, best)) {+ best = handler_route;}- return SKIP_ONE;}}- return 0;+ if (!best) {+ return 0;+ }+ const cbm_gbuf_edge_t **fn_handles = NULL;+ int fn_hcount = 0;+ cbm_gbuf_find_edges_by_target_type(gb, best->id, "HANDLES", &fn_handles, &fn_hcount);+ for (int fh = 0; fh < fn_hcount; fh++) {+ cbm_gbuf_insert_edge(gb, fn_handles[fh]->source_id, infra->id, "HANDLES",+ "{\"source\":\"infra_match\"}");+ }+ return SKIP_ONE;}/* Phase 2: Match infra Route URLs to handler Route nodes by URL path + service name. */@@ -850,20 +879,41 @@ static void create_grpc_routes(cbm_gbuf_t *gb) {return;}- const cbm_gbuf_node_t *services[CBM_SZ_64];- int svc_count = 0;- for (int i = 0; i < class_count && svc_count < CBM_SZ_64; i++) {+ /* EVERY proto service, not a fixed-size prefix of them.+ *+ * This array held 64 and the loop stopped at that many, so WHICH services+ * were considered depended on the order Class nodes happened to land in the+ * graph buffer — i.e. on parallel worker interleaving. Two indexes of the+ * same corpus with the same binary produced different __grpc__ Routes+ * (kubernetes, 2026-09-18: 5 Routes present in one run and absent from the+ * other, 4 the other way, plus their HANDLES edges). It was a silent+ * quality cut as well: every service past the 64th got no Routes at all,+ * and a repository with more than 64 services is not unusual. */+ int svc_total = 0;+ for (int i = 0; i < class_count; i++) {if (classes[i]->file_path && strstr(classes[i]->file_path, ".proto")) {- services[svc_count++] = classes[i];+ svc_total++;}}- if (svc_count == 0) {+ if (svc_total == 0) {return;}+ const cbm_gbuf_node_t **services =+ cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)svc_total * sizeof(*services));+ if (!services) {+ return; /* the borrowed `classes` array belongs to the graph buffer */+ }+ int svc_count = 0;+ for (int i = 0; i < class_count && svc_count < svc_total; i++) {+ if (classes[i]->file_path && strstr(classes[i]->file_path, ".proto")) {+ services[svc_count++] = classes[i];+ }+ }const cbm_gbuf_node_t **funcs = NULL;int func_count = 0;if (cbm_gbuf_find_by_label(gb, "Function", &funcs, &func_count) != 0 || func_count == 0) {+ cbm_free(CBM_MEM_CLASS_OTHER, services);return;}@@ -888,10 +938,13 @@ static void create_grpc_routes(cbm_gbuf_t *gb) {cbm_gbuf_insert_edge(gb, fn->id, route_id, "HANDLES", "{\"via\":\"proto_rpc\"}");grpc_routes++;}+ cbm_free(CBM_MEM_CLASS_OTHER, services);if (grpc_routes > 0) {char buf[CBM_SZ_16];+ char svc_buf[CBM_SZ_16];snprintf(buf, sizeof(buf), "%d", grpc_routes);- cbm_log_info("pass.route_nodes.grpc", "routes", buf);+ snprintf(svc_buf, sizeof(svc_buf), "%d", svc_count);+ cbm_log_info("pass.route_nodes.grpc", "routes", buf, "services", svc_buf);}}diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex 565c72719..86032b99e 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -223,6 +223,137 @@ TEST(store_bulk_persistence) {PASS();}+static void write_temp_file(const char *dir, const char *name, const char *content);++/* EVERY proto service gets its Routes, not the first 64 the graph buffer+ * happened to hold.+ *+ * create_grpc_routes() collected services into a fixed `services[CBM_SZ_64]`+ * and stopped there, in graph-buffer insertion order — which is parallel+ * worker merge order. So on a repo with more than 64 services the extras were+ * silently dropped, and WHICH ones were dropped changed between runs of the+ * same binary: two kubernetes indexes differed by 5 Routes one way and 4 the+ * other, plus their HANDLES edges (2026-09-18). 70 services here, so the old+ * cap fails this test whatever order the workers produce. */+TEST(pipeline_grpc_routes_cover_every_service_past_the_old_cap) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_grpc_cap_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));++ enum { SERVICES = 70 }; /* > the 64 the fixed array used to hold */+ for (int i = 0; i < SERVICES; i++) {+ char name[64];+ char body[512];+ snprintf(name, sizeof(name), "svc%02d.proto", i);+ snprintf(body, sizeof(body),+ "syntax = \"proto3\";\npackage demo%02d;\n\n"+ "message Req%02d { string id = 1; }\n"+ "message Resp%02d { string ok = 1; }\n\n"+ "service Svc%02d {\n"+ " rpc Ping (Req%02d) returns (Resp%02d);\n}\n",+ i, i, i, i, i, i);+ write_temp_file(tmp, name, body);+ }++ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/grpc.db", tmp);+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);+ ASSERT_NOT_NULL(p);+ ASSERT_EQ(cbm_pipeline_run(p), 0);++ cbm_store_t *s = cbm_store_open_path(db_path);+ ASSERT_NOT_NULL(s);+ const char *project = cbm_pipeline_project_name(p);+ cbm_node_t *routes = NULL;+ int route_count = 0;+ ASSERT_EQ(cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count),+ CBM_STORE_OK);+ int grpc_routes = 0;+ for (int i = 0; i < route_count; i++) {+ if (routes[i].qualified_name &&+ strncmp(routes[i].qualified_name, "__grpc__", strlen("__grpc__")) == 0) {+ grpc_routes++;+ }+ }+ cbm_store_free_nodes(routes, route_count);+ cbm_store_close(s);+ cbm_pipeline_free(p);+ th_rmtree(tmp);++ /* One rpc per service: every service must be represented. The old cap+ * produced at most 64 however the workers interleaved. */+ ASSERT_EQ(grpc_routes, SERVICES);+ PASS();+}++/* Spilling must be invisible in the OUTPUT: the same repository indexed with+ * results parked on disk must produce the same graph as one indexed entirely in+ * memory. It did not. The namespace map that `use`/`using`/package imports+ * resolve through was built from the in-memory result cache, where a PARKED+ * result is NULL, so every spilled file contributed no namespace and its+ * imports fell through to the looser fallback. Measured on the php corpus+ * (2026-09-18, reproducible 3/3 each way, and present on main): 57,182 edges in+ * memory against 59,379 while spilling — differing in BOTH directions, because+ * a file missing from that map does not fail to resolve, it resolves+ * differently.+ *+ * Why this needs its own fixture: test_parallel's spill-parity test uses the+ * compact parity harness, which omits the production pass that builds the map,+ * and its repo is Go+Java — neither reaches the namespace path. This one runs+ * the REAL pipeline over >MIN_FILES_FOR_PARALLEL files so the parallel route is+ * taken, with namespaced PHP that imports across files. */+TEST(pipeline_spill_resolves_namespace_imports_like_memory) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_spill_ns_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));++ /* 60 modules: each declares a namespace and the next one imports it, so+ * every file both contributes to the namespace map and depends on it. */+ enum { NS_FILES = 60 };+ for (int i = 0; i < NS_FILES; i++) {+ char name[64];+ char body[512];+ snprintf(name, sizeof(name), "Mod%02d.php", i);+ snprintf(body, sizeof(body),+ "<?php\nnamespace App\\Mod%02d;\n\nuse App\\Mod%02d\\Thing%02d;\n\n"+ "class Thing%02d {\n"+ " public function run() { $t = new Thing%02d(); return $t->run(); }\n}\n",+ i, (i + 1) % NS_FILES, (i + 1) % NS_FILES, i, (i + 1) % NS_FILES);+ write_temp_file(tmp, name, body);+ }++ int counts[2] = {0, 0};+ int imports[2] = {0, 0};+ for (int pass = 0; pass < 2; pass++) {+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/ns%d.db", tmp, pass);+ if (pass == 1) {+ cbm_setenv("CBM_MEM_SPILL", "1", 1); /* park every result on disk */+ }+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);+ ASSERT_NOT_NULL(p);+ int rc = cbm_pipeline_run(p);+ if (pass == 1) {+ cbm_unsetenv("CBM_MEM_SPILL");+ }+ ASSERT_EQ(rc, 0);+ cbm_store_t *s = cbm_store_open_path(db_path);+ ASSERT_NOT_NULL(s);+ const char *project = cbm_pipeline_project_name(p);+ counts[pass] = cbm_store_count_edges(s, project);+ imports[pass] = cbm_store_count_edges_by_type(s, project, "IMPORTS");+ cbm_store_close(s);+ cbm_pipeline_free(p);+ }+ th_rmtree(tmp);++ /* The imports must actually have resolved, or this compares 0 == 0. */+ ASSERT_GT(imports[0], 0);+ ASSERT_EQ(imports[0], imports[1]);+ ASSERT_EQ(counts[0], counts[1]);+ PASS();+}+/* ── Integration: structure pass on temp repo ────────────────────── */TEST(pipeline_structure_nodes) {@@ -3617,6 +3748,52 @@ TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swPASS();}+/* A killed run cleans up nothing, and until 2026-09-18 its staging database was+ * reclaimed only when THAT project was indexed again: a kernel index killed by+ * the OOM killer left ~15 GB parked until someone re-indexed the kernel, and+ * indefinitely if nobody ever did. Indexing ANY project now reclaims the+ * orphans of every project sharing that cache directory -- while the lock probe+ * still keeps a LIVE writer's stage, whichever project owns it. */+TEST(pipeline_sweep_reclaims_orphans_of_other_projects_and_keeps_their_live_stages) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_stage_sweep_other_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 1\n");+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/mine.db", tmp);++ /* Two stages of a DIFFERENT project sharing the cache directory: one whose+ * writer died (nothing holds its lock) and one whose writer is alive. */+ char other_dead[600];+ char other_dead_wal[600];+ char other_live[600];+ snprintf(other_dead, sizeof(other_dead), "%s/other.db.stage.cccccc", tmp);+ snprintf(other_dead_wal, sizeof(other_dead_wal), "%s/other.db.stage.cccccc-wal", tmp);+ snprintf(other_live, sizeof(other_live), "%s/other.db.stage.dddddd", tmp);+ static const char other_live_bytes[] = "other-project-live-bytes";+ ASSERT_EQ(th_write_file(other_dead, "other-project-dead-bytes"), 0);+ ASSERT_EQ(th_write_file(other_dead_wal, "other-project-dead-wal"), 0);+ ASSERT_EQ(th_write_file(other_live, other_live_bytes), 0);+ int other_live_fd = cbm_pipeline_stage_lock_hold(other_live);+ ASSERT_TRUE(other_live_fd >= 0);++ cbm_pipeline_incremental_test_reset_faults();+ cbm_pipeline_t *mine = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);+ ASSERT_NOT_NULL(mine);+ ASSERT_EQ(cbm_pipeline_run(mine), 0);+ cbm_pipeline_free(mine);++ bool dead_gone = !path_exists(other_dead);+ bool dead_wal_gone = !path_exists(other_dead_wal);+ bool live_kept = file_has_content(other_live, other_live_bytes);+ cbm_pipeline_stage_lock_drop(other_live, other_live_fd);++ ASSERT_TRUE(dead_gone); /* reclaimed even though it belongs to "other" */+ ASSERT_TRUE(dead_wal_gone); /* sidecars go with their stage */+ ASSERT_TRUE(live_kept); /* another project's LIVE stage is untouched */+ PASS();+}+/* #1839: the sweep removes exactly the stages nobody owns. A dead writer's* stage (main, -wal, and the unlocked .lock sidecar its death left) goes; a* stage whose writer is LIVE -- here the test, holding its lock -- is kept@@ -14558,6 +14735,8 @@ SUITE(pipeline) {RUN_TEST(store_file_persistence);RUN_TEST(store_bulk_persistence);/* Integration: structure pass */+ RUN_TEST(pipeline_grpc_routes_cover_every_service_past_the_old_cap);+ RUN_TEST(pipeline_spill_resolves_namespace_imports_like_memory);RUN_TEST(pipeline_structure_nodes);RUN_TEST(pipeline_committed_counts_match_persisted);RUN_TEST(pipeline_adr_survives_full_reindex);@@ -14902,6 +15081,7 @@ SUITE(pipeline_semantic_manifest_repro) {RUN_TEST(pipeline_stage_names_never_nest);RUN_TEST(pipeline_minted_stage_is_owned_until_released);RUN_TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swept);+ RUN_TEST(pipeline_sweep_reclaims_orphans_of_other_projects_and_keeps_their_live_stages);RUN_TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage);RUN_TEST(pipeline_concurrent_sweep_must_not_remove_inflight_stage);RUN_TEST(pipeline_fresh_index_never_reports_invalid_existing_db);