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(store): cap recursive BFS path rows

King Star0c5ffa50f925
diff --git a/src/store/store.c b/src/store/store.c
index 4197ccad3..1da7cfeb2 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -63,6 +63,8 @@ enum {
ST_METHOD_PROP_LEN = 8,
ST_PATH_PROP_LEN = 6,
ST_HANDLER_PROP_LEN = 9,
+ ST_BFS_CTE_ROW_MULTIPLIER = 8,
+ ST_BFS_MAX_CTE_ROWS = 4096,
};
#define SLEN(s) (sizeof(s) - 1)
@@ -4673,6 +4675,18 @@ static void bfs_build_types_clause(int edge_type_count, char *buf, int buf_sz) {
}
}
+static int bfs_cte_row_limit(int max_results) {
+ int result_budget = max_results > 0 ? max_results : ST_INIT_CAP_16;
+ long row_budget = (long)result_budget * ST_BFS_CTE_ROW_MULTIPLIER + SKIP_ONE;
+ if (row_budget > ST_BFS_MAX_CTE_ROWS) {
+ return ST_BFS_MAX_CTE_ROWS;
+ }
+ if (row_budget < ST_INIT_CAP_16) {
+ return ST_INIT_CAP_16;
+ }
+ 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) {
memset(out, 0, sizeof(*out));
@@ -4701,6 +4715,8 @@ 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, ''"
@@ -4712,6 +4728,7 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
" 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 "
@@ -4723,7 +4740,8 @@ int cbm_store_bfs(cbm_store_t *s, int64_t start_id, const char *direction, const
* 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, max_results);
+ (long long)start_id, next_id, join_cond, types_clause, max_depth, cte_row_limit,
+ max_results);
sqlite3_stmt *stmt = NULL;
rc = sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL);
diff --git a/tests/test_store_search.c b/tests/test_store_search.c
index 2eb189ed5..5df21dc22 100644
--- a/tests/test_store_search.c
+++ b/tests/test_store_search.c
@@ -8,6 +8,7 @@
#include "test_helpers.h"
#include <store/store.h>
#include <stdint.h>
+#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
@@ -992,6 +993,40 @@ TEST(store_bfs_with_risk_labels) {
PASS();
}
+TEST(store_bfs_caps_recursive_cte_rows) {
+ 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"};
+ int64_t root_id = cbm_store_upsert_node(s, &root);
+
+ enum { NODE_COUNT = 4200 };
+ for (int i = 0; i < NODE_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);
+ cbm_edge_t edge = {
+ .project = "test", .source_id = root_id, .target_id = leaf_id, .type = "CALLS"};
+ cbm_store_insert_edge(s, &edge);
+ }
+
+ const char *types[] = {"CALLS"};
+ 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);
+
+ cbm_store_traverse_free(&result);
+ cbm_store_close(s);
+ PASS();
+}
+
/* ── BFS cross-service summary ─────────────────────────────────── */
TEST(store_bfs_cross_service_summary) {
@@ -1509,6 +1544,7 @@ 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_cross_service_summary);
RUN_TEST(store_glob_to_like);
RUN_TEST(store_extract_like_hints);