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(cypher): enforce variable-length path semantics

King Star8546070dc259
diff --git a/src/store/store.c b/src/store/store.c
index 015e50ca2..4197ccad3 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -4702,19 +4702,16 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
}
snprintf(sql, sizeof(sql),
- /* SHORTEST-PATH semantics: the UNION dedupes (node, hop) PAIRS,
- * so a single self-loop minted every hop level for every node it
- * could reach — walk-padding that fabricated *k..k Cypher matches
- * of arbitrary length and exploded the row set to nodes x depth
- * (#797). MIN(hop) GROUP BY node returns each node once at its
- * minimal distance. */
- "WITH RECURSIVE bfs(node_id, hop) AS ("
- " SELECT %lld, 0"
+ "WITH RECURSIVE bfs(node_id, hop, edge_path) AS ("
+ " SELECT %lld, 0, ''"
" UNION"
- " SELECT %s, bfs.hop + 1"
+ " SELECT %s, bfs.hop + 1,"
+ " CASE WHEN bfs.edge_path = '' THEN CAST(e.id AS TEXT)"
+ " ELSE bfs.edge_path || ',' || e.id END"
" FROM bfs"
" JOIN edges e ON %s"
" WHERE e.type IN (%s) AND bfs.hop < %d"
+ " AND instr(',' || bfs.edge_path || ',', ',' || e.id || ',') = 0"
")"
"SELECT n.id, n.project, n.label, n.name, n.qualified_name, "
"n.file_path, n.start_line, n.end_line, n.properties, MIN(bfs.hop) AS hop "
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index ff5250c48..6a95a1264 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -1798,6 +1798,48 @@ TEST(cypher_exec_var_length_explicit_bound_capped) {
PASS();
}
+TEST(cypher_exec_variable_length_repeated_node_var_unifies) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+
+ int rc = cbm_cypher_execute(s,
+ "MATCH (f:Function)-[:CALLS*1..2]->(f:Function) "
+ "RETURN f.name",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 0);
+
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
+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");
+
+ cbm_node_t n = {.project = "test",
+ .label = "Function",
+ .name = "Recursive",
+ .qualified_name = "test.Recursive",
+ .file_path = "recursive.go"};
+ int64_t id = cbm_store_upsert_node(s, &n);
+ cbm_edge_t e = {.project = "test", .source_id = id, .target_id = id, .type = "CALLS"};
+ cbm_store_insert_edge(s, &e);
+
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s,
+ "MATCH (f:Function {name: \"Recursive\"})-[:CALLS*2..2]"
+ "->(g:Function) RETURN g.name",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 0);
+
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_exec_defines_edge) {
cbm_store_t *s = setup_cypher_store();
cbm_cypher_result_t r = {0};
@@ -4037,6 +4079,8 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_variable_length);
RUN_TEST(cypher_exec_variable_length_repeated_node_var_unifies);
RUN_TEST(cypher_exec_var_length_explicit_bound_capped);
+ RUN_TEST(cypher_exec_variable_length_repeated_node_var_unifies);
+ RUN_TEST(cypher_exec_var_length_no_reuse_self_loop);
RUN_TEST(cypher_exec_defines_edge);
RUN_TEST(cypher_exec_no_results);
RUN_TEST(cypher_exec_where_numeric);