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): scope trail traversal to path expansion

King Star0d97729c394e
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 7936eff39..51edbc667 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -3202,7 +3202,8 @@ static void expand_var_length(cbm_store_t *store, cbm_rel_pattern_t *rel,
}
cbm_traverse_result_t tr = {0};
const char *dir = rel->direction ? rel->direction : "outbound";
- cbm_store_bfs(store, src->id, dir, rel->types, rel->type_count, max_depth, CBM_PERCENT, &tr);
+ cbm_store_bfs_trail(store, src->id, dir, rel->types, rel->type_count, max_depth, CBM_PERCENT,
+ &tr);
cbm_node_t *bound_to = binding_get(b, to_var);
int64_t bound_to_id = bound_to ? bound_to->id : 0;
/* Same contract as process_edges: the budget caps materialisation, never
diff --git a/src/store/store.c b/src/store/store.c
index 1da7cfeb2..e1be7a5e6 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -25,6 +25,7 @@ enum {
ST_COL_7 = 7,
ST_COL_8 = 8,
ST_COL_9 = 9,
+ ST_COL_10 = 10,
ST_FOUND = -1,
ST_BUF_16 = 16,
ST_BUF_64 = 64,
@@ -4687,8 +4688,9 @@ static int bfs_cte_row_limit(int max_results) {
return (int)row_budget;
}
-int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const char **edge_types,
- int edge_type_count, int max_depth, int max_results, cbm_traverse_result_t *out) {
+static int store_bfs(cbm_store_t *s, int64_t start_id, const char *direction,
+ const char **edge_types, int edge_type_count, int max_depth, int max_results,
+ bool trail, cbm_traverse_result_t *out) {
memset(out, 0, sizeof(*out));
cbm_node_t root = {0};
@@ -4715,33 +4717,53 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
next_id = "e.target_id";
}
- int cte_row_limit = bfs_cte_row_limit(max_results);
-
- snprintf(sql, sizeof(sql),
- "WITH RECURSIVE bfs(node_id, hop, edge_path) AS ("
- " SELECT %lld, 0, ''"
- " UNION"
- " 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"
- " LIMIT %d"
- ")"
- "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 "
- "FROM bfs "
- "JOIN nodes n ON n.id = bfs.node_id "
- "WHERE bfs.hop > 0 " /* exclude root at hop 0 (self via a loop still appears) */
- "GROUP BY n.id "
- /* (hop, id) is a unique total order — deterministic pagination
- * watermarks and reproducible trace output depend on it. */
- "ORDER BY hop, n.id "
- "LIMIT %d;",
- (long long)start_id, next_id, join_cond, types_clause, max_depth, cte_row_limit,
- max_results);
+ int cte_row_limit = 0;
+ if (trail) {
+ cte_row_limit = bfs_cte_row_limit(max_results);
+ snprintf(sql, sizeof(sql),
+ "WITH RECURSIVE bfs(node_id, hop, edge_path) AS ("
+ " SELECT %lld, 0, ''"
+ " UNION"
+ " 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"
+ " LIMIT %d"
+ ")"
+ "SELECT DISTINCT n.id, n.project, n.label, n.name, n.qualified_name, "
+ "n.file_path, n.start_line, n.end_line, n.properties, bfs.hop, "
+ "(SELECT count(*) FROM bfs) "
+ "FROM bfs JOIN nodes n ON n.id = bfs.node_id "
+ "WHERE bfs.hop > 0 ORDER BY bfs.hop LIMIT %d;",
+ (long long)start_id, next_id, join_cond, types_clause, max_depth,
+ cte_row_limit + SKIP_ONE, max_results);
+ } else {
+ snprintf(sql, sizeof(sql),
+ /* SHORTEST-PATH semantics: the UNION dedupes (node, hop) pairs.
+ * MIN(hop) GROUP BY node returns each node once at its minimal
+ * distance, while relationship-trail state remains scoped to
+ * Cypher variable-length expansion. */
+ "WITH RECURSIVE bfs(node_id, hop) AS ("
+ " SELECT %lld, 0"
+ " UNION"
+ " SELECT %s, bfs.hop + 1"
+ " FROM bfs"
+ " JOIN edges e ON %s"
+ " WHERE e.type IN (%s) AND bfs.hop < %d"
+ ")"
+ "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 "
+ "FROM bfs "
+ "JOIN nodes n ON n.id = bfs.node_id "
+ "WHERE bfs.hop > 0 "
+ "GROUP BY n.id "
+ "ORDER BY hop, n.id "
+ "LIMIT %d;",
+ (long long)start_id, next_id, join_cond, types_clause, max_depth, max_results);
+ }
sqlite3_stmt *stmt = NULL;
rc = sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL);
@@ -4761,6 +4783,7 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
int cap = ST_INIT_CAP_16;
int n = 0;
+ int cte_rows = 0;
cbm_node_hop_t *visited = malloc(cap * sizeof(cbm_node_hop_t));
int scan_rc15;
@@ -4771,6 +4794,9 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
}
scan_node(stmt, &visited[n].node);
visited[n].hop = sqlite3_column_int(stmt, ST_COL_9);
+ if (trail) {
+ cte_rows = sqlite3_column_int(stmt, ST_COL_10);
+ }
n++;
}
if (scan_rc15 != SQLITE_DONE) { /* SCANCHK:15:stmt */
@@ -4783,6 +4809,12 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
sqlite3_finalize(stmt);
+ if (trail && cte_rows > cte_row_limit) {
+ char limit_buf[16];
+ snprintf(limit_buf, sizeof(limit_buf), "%d", cte_row_limit);
+ cbm_log_warn("cypher.trail_truncated", "cte_rows", limit_buf, "result", "partial");
+ }
+
out->visited = visited;
out->visited_count = n;
@@ -4798,6 +4830,19 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
return CBM_STORE_OK;
}
+int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const char **edge_types,
+ int edge_type_count, int max_depth, int max_results, cbm_traverse_result_t *out) {
+ return store_bfs(s, start_id, direction, edge_types, edge_type_count, max_depth, max_results,
+ false, out);
+}
+
+int cbm_store_bfs_trail(cbm_store_t *s, int64_t start_id, const char *direction,
+ const char **edge_types, int edge_type_count, int max_depth,
+ int max_results, cbm_traverse_result_t *out) {
+ return store_bfs(s, start_id, direction, edge_types, edge_type_count, max_depth, max_results,
+ true, out);
+}
+
/* Multi-source BFS: one recursive CTE anchored on ALL seeds (via a temp
* table — never a per-seed loop, which re-walks overlapping hub subgraphs
* seed_count times). Semantics for impact analysis:
diff --git a/src/store/store.h b/src/store/store.h
index 8c2f76dd8..61fbf83dc 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -603,6 +603,11 @@ void cbm_store_search_free(cbm_search_output_t *out);
int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const char **edge_types,
int edge_type_count, int max_depth, int max_results, cbm_traverse_result_t *out);
+/* Variable-length Cypher traversal with relationship-trail semantics. */
+int cbm_store_bfs_trail(cbm_store_t *s, int64_t start_id, const char *direction,
+ const char **edge_types, int edge_type_count, int max_depth,
+ int max_results, cbm_traverse_result_t *out);
+
/* Multi-source BFS from ALL seed ids at once (one CTE, temp-table anchored).
* Seeds are EXCLUDED from the result (impact semantics); MIN(hop) across the
* seed set; canonical (hop,id) order; *truncated set when the max_results
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index 6a95a1264..8ed6b6511 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -1171,15 +1171,24 @@ TEST(cypher_exec_varlength_path_semantics_issue797) {
ASSERT_EQ(r1.row_count, 1);
cbm_cypher_result_free(&r1);
- /* Bug 2: *2..2 from loopy — only the REAL 2-chain (leaf); the self-loop
- * must not be reused to pad paths (relationship uniqueness). */
+ /* Bug 2: *2..2 from loopy has two relationship-unique trails: the
+ * self-loop followed by e1 reaches mid, and e1 followed by e2 reaches leaf.
+ * Reusing the self-loop within one trail remains forbidden. */
cbm_cypher_result_t r2 = {0};
ASSERT_EQ(cbm_cypher_execute(s,
"MATCH (a {name: \"loopy\"})-[:CALLS*2..2]->(b) "
"RETURN DISTINCT b.name",
"test", 0, &r2),
0);
- ASSERT_EQ(r2.row_count, 1); /* leaf only */
+ ASSERT_EQ(r2.row_count, 2);
+ bool saw_mid = false;
+ bool saw_leaf = false;
+ for (int i = 0; i < r2.row_count; i++) {
+ saw_mid |= strcmp(r2.rows[i][0], "mid") == 0;
+ saw_leaf |= strcmp(r2.rows[i][0], "leaf") == 0;
+ }
+ ASSERT_TRUE(saw_mid);
+ ASSERT_TRUE(saw_leaf);
cbm_cypher_result_free(&r2);
/* Bug 2 amplifier: no directed path of length 5 exists at all. */
diff --git a/tests/test_store_search.c b/tests/test_store_search.c
index 5df21dc22..c5658349a 100644
--- a/tests/test_store_search.c
+++ b/tests/test_store_search.c
@@ -4,6 +4,7 @@
* Ported from internal/store/store_test.go (TestSearch, TestBFS, etc.)
*/
#include "../src/foundation/compat.h"
+#include "../src/foundation/log.h"
#include "test_framework.h"
#include "test_helpers.h"
#include <store/store.h>
@@ -13,6 +14,15 @@
#include <stdlib.h>
#include <unistd.h>
+static char trail_log[1024];
+
+static void capture_trail_log(const char *line) {
+ size_t used = strlen(trail_log);
+ if (used < sizeof(trail_log) - 1) {
+ snprintf(trail_log + used, sizeof(trail_log) - used, "%s\n", line);
+ }
+}
+
/* Helper: create a typical graph for search/traversal tests.
*
* Nodes: SubmitOrder (Function), ProcessOrder (Function), OrderService (Class)
@@ -993,7 +1003,7 @@ TEST(store_bfs_with_risk_labels) {
PASS();
}
-TEST(store_bfs_caps_recursive_cte_rows) {
+TEST(store_bfs_reachability_is_not_trail_capped) {
cbm_store_t *s = cbm_store_open_memory();
cbm_store_upsert_project(s, "test", "/tmp/test");
@@ -1019,8 +1029,48 @@ TEST(store_bfs_caps_recursive_cte_rows) {
cbm_traverse_result_t result = {0};
int rc = cbm_store_bfs(s, root_id, "outbound", types, 1, 1, 5000, &result);
ASSERT_EQ(rc, CBM_STORE_OK);
- ASSERT_TRUE(result.visited_count > 0);
- ASSERT_TRUE(result.visited_count < NODE_COUNT);
+ ASSERT_EQ(result.visited_count, NODE_COUNT);
+
+ cbm_store_traverse_free(&result);
+ cbm_store_close(s);
+ PASS();
+}
+
+TEST(store_bfs_trail_warns_when_path_rows_are_truncated) {
+ cbm_store_t *s = cbm_store_open_memory();
+ cbm_store_upsert_project(s, "test", "/tmp/test");
+
+ int64_t ids[14];
+ for (int i = 0; i < 14; i++) {
+ char name[16];
+ snprintf(name, sizeof(name), "node-%d", i);
+ cbm_node_t node = {.project = "test",
+ .label = "Function",
+ .name = name,
+ .qualified_name = name,
+ .file_path = "graph.c"};
+ ids[i] = cbm_store_upsert_node(s, &node);
+ }
+ for (int source = 0; source < 13; source++) {
+ for (int target = source + 1; target < 14; target++) {
+ cbm_edge_t edge = {.project = "test",
+ .source_id = ids[source],
+ .target_id = ids[target],
+ .type = "CALLS"};
+ cbm_store_insert_edge(s, &edge);
+ }
+ }
+
+ trail_log[0] = '\0';
+ cbm_log_set_sink(capture_trail_log);
+ const char *types[] = {"CALLS"};
+ cbm_traverse_result_t result = {0};
+ int rc = cbm_store_bfs_trail(s, ids[0], "outbound", types, 1, 10, 5000, &result);
+ cbm_log_set_sink(NULL);
+
+ ASSERT_EQ(rc, CBM_STORE_OK);
+ ASSERT_TRUE(strstr(trail_log, "cypher.trail_truncated") != NULL);
+ ASSERT_TRUE(strstr(trail_log, "result=partial") != NULL);
cbm_store_traverse_free(&result);
cbm_store_close(s);
@@ -1544,7 +1594,8 @@ SUITE(store_search) {
RUN_TEST(store_cross_service_detection);
RUN_TEST(store_deduplicate_hops);
RUN_TEST(store_bfs_with_risk_labels);
- RUN_TEST(store_bfs_caps_recursive_cte_rows);
+ RUN_TEST(store_bfs_reachability_is_not_trail_capped);
+ RUN_TEST(store_bfs_trail_warns_when_path_rows_are_truncated);
RUN_TEST(store_bfs_cross_service_summary);
RUN_TEST(store_glob_to_like);
RUN_TEST(store_extract_like_hints);