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(cypher): surface bounded trail traversal truncation
King Starcd22487ca605
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.cindex 51edbc667..5724f3ac6 100644--- a/src/cypher/cypher.c+++ b/src/cypher/cypher.c@@ -3180,6 +3180,7 @@ static void process_edges(cbm_store_t *store, cbm_edge_t *edges, int edge_count,/* C11 _Thread_local directly: cypher.c stays windows.h-free (compat.h pulls* in windows.h, whose legacy `far` macro breaks this file's identifiers). */static _Thread_local int g_cypher_depth_clamped = 0;+static _Thread_local int g_cypher_trail_truncated = 0;static void expand_var_length(cbm_store_t *store, cbm_rel_pattern_t *rel,cbm_node_pattern_t *target_node, binding_t *b, cbm_node_t *src,@@ -3204,6 +3205,9 @@ static void expand_var_length(cbm_store_t *store, cbm_rel_pattern_t *rel,const char *dir = rel->direction ? rel->direction : "outbound";cbm_store_bfs_trail(store, src->id, dir, rel->types, rel->type_count, max_depth, CBM_PERCENT,&tr);+ if (tr.truncated) {+ g_cypher_trail_truncated = 1;+ }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@@ -4894,6 +4898,7 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projeccbm_cypher_result_t *out) {memset(out, 0, sizeof(*out));g_cypher_depth_clamped = 0;+ g_cypher_trail_truncated = 0;cypher_deadline_arm(); /* #601: start the wall-clock budget for this query */if (max_rows <= 0) {max_rows = CYPHER_RESULT_CEILING;@@ -4964,12 +4969,22 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projecout->col_count = rb.col_count;out->rows = rb.rows;out->row_count = rb.row_count;- if (g_cypher_depth_clamped > 0) {+ if (g_cypher_depth_clamped > 0 || g_cypher_trail_truncated) {char wbuf[CBM_SZ_256];- snprintf(wbuf, sizeof(wbuf),- "variable-length hop range clamped to the engine ceiling (%d) — an empty "- "result may mean \"clamped\", not \"no such path\"",- g_cypher_depth_clamped);+ if (g_cypher_depth_clamped > 0 && g_cypher_trail_truncated) {+ snprintf(wbuf, sizeof(wbuf),+ "variable-length hop range clamped to the engine ceiling (%d) and "+ "traversal budget was exhausted — results may be partial",+ g_cypher_depth_clamped);+ } else if (g_cypher_depth_clamped > 0) {+ snprintf(wbuf, sizeof(wbuf),+ "variable-length hop range clamped to the engine ceiling (%d) — an empty "+ "result may mean \"clamped\", not \"no such path\"",+ g_cypher_depth_clamped);+ } else {+ snprintf(wbuf, sizeof(wbuf),+ "variable-length traversal budget was exhausted — results may be partial");+ }out->warning = heap_strdup(wbuf);}diff --git a/src/store/store.c b/src/store/store.cindex e1be7a5e6..061649a1d 100644--- a/src/store/store.c+++ b/src/store/store.c@@ -65,7 +65,7 @@ enum {ST_PATH_PROP_LEN = 6,ST_HANDLER_PROP_LEN = 9,ST_BFS_CTE_ROW_MULTIPLIER = 8,- ST_BFS_MAX_CTE_ROWS = 4096,+ ST_BFS_MAX_CTE_ROWS = 65536,};#define SLEN(s) (sizeof(s) - 1)@@ -4688,6 +4688,20 @@ static int bfs_cte_row_limit(int max_results) {return (int)row_budget;}+static int bfs_cte_row_limit_for_depth(int max_results, int max_depth) {+ int base = bfs_cte_row_limit(max_results);+ /* Reserve rows across the requested depth range. A result-sized cap can+ * be exhausted by a wide first hop before any deeper match is generated;+ * scaling by depth keeps the bounded traversal useful for *N..N queries+ * while the hard ceiling still protects the SQLite worker. */+ long depth = max_depth > 0 ? max_depth : ST_INIT_CAP_16;+ long scaled = (long)base * depth;+ if (scaled > ST_BFS_MAX_CTE_ROWS) {+ return ST_BFS_MAX_CTE_ROWS;+ }+ return (int)scaled;+}+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) {@@ -4719,7 +4733,7 @@ static int store_bfs(cbm_store_t *s, int64_t start_id, const char *direction,int cte_row_limit = 0;if (trail) {- cte_row_limit = bfs_cte_row_limit(max_results);+ cte_row_limit = bfs_cte_row_limit_for_depth(max_results, max_depth);snprintf(sql, sizeof(sql),"WITH RECURSIVE bfs(node_id, hop, edge_path) AS ("" SELECT %lld, 0, ''"@@ -4731,13 +4745,19 @@ static int store_bfs(cbm_store_t *s, int64_t start_id, const char *direction," JOIN edges e ON %s"" WHERE e.type IN (%s) AND bfs.hop < %d"" AND instr(',' || bfs.edge_path || ',', ',' || e.id || ',') = 0"+ /* SQLite's recursive queue is breadth-first by default. A+ * bounded trail budget must reach the requested depth before+ * 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"" 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;",+ "WHERE bfs.hop > 0 ORDER BY bfs.hop, n.id LIMIT %d;",(long long)start_id, next_id, join_cond, types_clause, max_depth,cte_row_limit + SKIP_ONE, max_results);} else {@@ -4810,6 +4830,7 @@ static int store_bfs(cbm_store_t *s, int64_t start_id, const char *direction,sqlite3_finalize(stmt);if (trail && cte_rows > cte_row_limit) {+ out->truncated = true;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");diff --git a/src/store/store.h b/src/store/store.hindex 61fbf83dc..85d535e05 100644--- a/src/store/store.h+++ b/src/store/store.h@@ -211,6 +211,8 @@ typedef struct {int visited_count;cbm_edge_info_t *edges;int edge_count;+ /* True when trail expansion hit its recursive-row safety budget. */+ bool truncated;} cbm_traverse_result_t;/* ── Schema introspection ───────────────────────────────────────── */diff --git a/tests/test_cypher.c b/tests/test_cypher.cindex 8ed6b6511..aaaa5f3dc 100644--- a/tests/test_cypher.c+++ b/tests/test_cypher.c@@ -1849,6 +1849,46 @@ TEST(cypher_exec_var_length_no_reuse_self_loop) {PASS();}+TEST(cypher_exec_var_length_truncation_surfaces_warning) {+ cbm_store_t *s = cbm_store_open_memory();+ cbm_store_upsert_project(s, "test", "/tmp/test");++ int64_t ids[18];+ for (int i = 0; i < 18; 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 < 17; source++) {+ for (int target = source + 1; target < 18; target++) {+ cbm_edge_t edge = {.project = "test",+ .source_id = ids[source],+ .target_id = ids[target],+ .type = "CALLS"};+ cbm_store_insert_edge(s, &edge);+ }+ }++ cbm_cypher_result_t result = {0};+ int rc = cbm_cypher_execute(s,+ "MATCH (a:Function {name: \"node-0\"})-[:CALLS*10..10]->"+ "(b:Function) RETURN b.name",+ "test", 0, &result);+ ASSERT_EQ(rc, 0);+ ASSERT_NOT_NULL(result.warning);+ ASSERT_TRUE(strstr(result.warning, "traversal budget") != NULL);+ ASSERT_TRUE(result.row_count > 0);++ cbm_cypher_result_free(&result);+ cbm_store_close(s);+ PASS();+}+TEST(cypher_exec_defines_edge) {cbm_store_t *s = setup_cypher_store();cbm_cypher_result_t r = {0};@@ -4090,6 +4130,7 @@ SUITE(cypher) {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_var_length_truncation_surfaces_warning);RUN_TEST(cypher_exec_defines_edge);RUN_TEST(cypher_exec_no_results);RUN_TEST(cypher_exec_where_numeric);diff --git a/tests/test_store_search.c b/tests/test_store_search.cindex c5658349a..6a960c223 100644--- a/tests/test_store_search.c+++ b/tests/test_store_search.c@@ -1040,8 +1040,8 @@ 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++) {+ int64_t ids[18];+ for (int i = 0; i < 18; i++) {char name[16];snprintf(name, sizeof(name), "node-%d", i);cbm_node_t node = {.project = "test",@@ -1051,8 +1051,8 @@ TEST(store_bfs_trail_warns_when_path_rows_are_truncated) {.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++) {+ for (int source = 0; source < 17; source++) {+ for (int target = source + 1; target < 18; target++) {cbm_edge_t edge = {.project = "test",.source_id = ids[source],.target_id = ids[target],@@ -1069,6 +1069,7 @@ TEST(store_bfs_trail_warns_when_path_rows_are_truncated) {cbm_log_set_sink(NULL);ASSERT_EQ(rc, CBM_STORE_OK);+ ASSERT_TRUE(result.truncated);ASSERT_TRUE(strstr(trail_log, "cypher.trail_truncated") != NULL);ASSERT_TRUE(strstr(trail_log, "result=partial") != NULL);