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.

test(cypher): make bounded trails deterministic

King Star7583376fd03f
diff --git a/src/store/store.c b/src/store/store.c
index c8bf27631..1a6f23522 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -4750,7 +4750,7 @@ static int store_bfs(cbm_store_t *s, int64_t start_id, const char *direction,
* a wide shallow layer consumes every row, so prioritize the
* recursive hop (depth-first) while retaining deterministic
* ordering in the outer result query. */
- " ORDER BY 2 DESC"
+ " ORDER BY 2 DESC, 1 ASC, 3 ASC"
" LIMIT %d"
")"
"SELECT DISTINCT n.id, n.project, n.label, n.name, n.qualified_name, "
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index ff611f687..c37b2b7d0 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -1807,6 +1807,8 @@ TEST(cypher_exec_var_length_explicit_bound_capped) {
PASS();
}
+/* Pin the relationship-trail contract: a self-loop edge cannot be reused
+ * within one variable-length trail, so *2..2 yields no fabricated match. */
TEST(cypher_exec_var_length_no_reuse_self_loop) {
cbm_store_t *s = cbm_store_open_memory();
cbm_store_upsert_project(s, "test", "/tmp/test");
diff --git a/tests/test_store_search.c b/tests/test_store_search.c
index 6a960c223..a82b15240 100644
--- a/tests/test_store_search.c
+++ b/tests/test_store_search.c
@@ -1078,6 +1078,69 @@ TEST(store_bfs_trail_warns_when_path_rows_are_truncated) {
PASS();
}
+TEST(store_bfs_trail_preserves_deeper_match_under_hub_budget) {
+ cbm_store_t *s = cbm_store_open_memory();
+ cbm_store_upsert_project(s, "test", "/tmp/test");
+
+ cbm_node_t root = {.project = "test",
+ .label = "Function",
+ .name = "root",
+ .qualified_name = "test.root"};
+ cbm_node_t branch = {.project = "test",
+ .label = "Function",
+ .name = "branch",
+ .qualified_name = "test.branch"};
+ cbm_node_t target = {.project = "test",
+ .label = "Function",
+ .name = "target",
+ .qualified_name = "test.target"};
+ int64_t root_id = cbm_store_upsert_node(s, &root);
+ int64_t branch_id = cbm_store_upsert_node(s, &branch);
+ int64_t target_id = cbm_store_upsert_node(s, &target);
+
+ cbm_edge_t edge = {
+ .project = "test", .source_id = root_id, .target_id = branch_id, .type = "CALLS"};
+ cbm_store_insert_edge(s, &edge);
+ edge.source_id = branch_id;
+ edge.target_id = target_id;
+ cbm_store_insert_edge(s, &edge);
+
+ /* A high-fanout hub must not consume the bounded CTE before a deeper path
+ * is considered; the depth-first queue should still surface `target`. */
+ enum { LEAF_COUNT = 4100 };
+ for (int i = 0; i < LEAF_COUNT; i++) {
+ char name[32];
+ snprintf(name, sizeof(name), "leaf-%d", i);
+ cbm_node_t leaf = {.project = "test",
+ .label = "Function",
+ .name = name,
+ .qualified_name = name};
+ int64_t leaf_id = cbm_store_upsert_node(s, &leaf);
+ edge.source_id = root_id;
+ edge.target_id = leaf_id;
+ cbm_store_insert_edge(s, &edge);
+ }
+
+ const char *types[] = {"CALLS"};
+ cbm_traverse_result_t result = {0};
+ int rc = cbm_store_bfs_trail(s, root_id, "outbound", types, 1, 2, 5000, &result);
+ ASSERT_EQ(rc, CBM_STORE_OK);
+ ASSERT_TRUE(result.truncated);
+
+ bool saw_target = false;
+ for (int i = 0; i < result.visited_count; i++) {
+ if (result.visited[i].node.id == target_id) {
+ saw_target = true;
+ break;
+ }
+ }
+ ASSERT_TRUE(saw_target);
+
+ cbm_store_traverse_free(&result);
+ cbm_store_close(s);
+ PASS();
+}
+
/* ── BFS cross-service summary ─────────────────────────────────── */
TEST(store_bfs_cross_service_summary) {
@@ -1597,6 +1660,7 @@ SUITE(store_search) {
RUN_TEST(store_bfs_with_risk_labels);
RUN_TEST(store_bfs_reachability_is_not_trail_capped);
RUN_TEST(store_bfs_trail_warns_when_path_rows_are_truncated);
+ RUN_TEST(store_bfs_trail_preserves_deeper_match_under_hub_budget);
RUN_TEST(store_bfs_cross_service_summary);
RUN_TEST(store_glob_to_like);
RUN_TEST(store_extract_like_hints);