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
Spill oversized index keys to overflow pages; escape remaining edge-props text
Martin Vogel7f78f936ece7
diff --git a/internal/cbm/sqlite_writer.c b/internal/cbm/sqlite_writer.cindex c3148e5cb..e566a04b1 100644--- a/internal/cbm/sqlite_writer.c+++ b/internal/cbm/sqlite_writer.c@@ -1051,6 +1051,58 @@ static bool pb_ensure_leaf_cap(PageBuilder *pb) {// max_local = usable - 35 = 65501// min_local = (usable - 12) * 32 / 255 - 23 = 8199 (C integer arithmetic, same as SQLite)#define TABLE_OVERFLOW_MAX_LOCAL 65501++// SQLite index B-tree local-payload thresholds for PAGE_SIZE=65536, reserved=0:+// X (max local) = ((U-12)*64/255) - 23 = 16422+// M (min local) = ((U-12)*32/255) - 23 = 8199+// An index cell whose payload exceeds X MUST spill to overflow pages; storing+// it fully inline makes SQLite read key bytes as an overflow page number+// (integrity_check: "invalid page number", name lookups silently miss — seen+// on elasticsearch's very long Section names in idx_nodes_name).+#define INDEX_OVERFLOW_MAX_LOCAL 16422+#define INDEX_OVERFLOW_MIN_LOCAL 8199++// Read a SQLite varint (1-9 bytes). Returns bytes consumed.+static int get_varint(const uint8_t *buf, uint64_t *out) {+ uint64_t v = 0;+ for (int i = 0; i < 8; i++) {+ v = (v << 7) | (uint64_t)(buf[i] & 0x7f);+ if ((buf[i] & 0x80) == 0) {+ *out = v;+ return i + 1;+ }+ }+ v = (v << 8) | (uint64_t)buf[8];+ *out = v;+ return 9;+}++// If an index cell's payload exceeds X, rewrite it to spill the tail to+// overflow pages: varint(payload_len) + payload[0..local) + u32(first_ovfl).+// Returns the (possibly new, malloc'd) cell; frees the original when replaced.+static uint8_t *overflowize_index_cell(FILE *fp, uint32_t *next_page, uint8_t *cell,+ int *cell_len) {+ uint64_t plen = 0;+ int vlen = get_varint(cell, &plen);+ if ((int64_t)plen <= INDEX_OVERFLOW_MAX_LOCAL) {+ return cell;+ }+ int64_t per_ovfl = (int64_t)CBM_PAGE_SIZE - BTREE_PTR_SIZE;+ int64_t k = INDEX_OVERFLOW_MIN_LOCAL + (((int64_t)plen - INDEX_OVERFLOW_MIN_LOCAL) % per_ovfl);+ int local = (k <= INDEX_OVERFLOW_MAX_LOCAL) ? (int)k : INDEX_OVERFLOW_MIN_LOCAL;+ uint32_t first_ovfl =+ write_overflow_pages(fp, next_page, cell + vlen + local, (int)plen - local);+ int nlen = vlen + local + BTREE_PTR_SIZE;+ uint8_t *data = (uint8_t *)malloc((size_t)nlen);+ if (!data) {+ return cell; /* fall back to the (broken) inline form on OOM */+ }+ memcpy(data, cell, (size_t)(vlen + local));+ put_u32(data + vlen + local, first_ovfl);+ free(cell);+ *cell_len = nlen;+ return data;+}#define TABLE_OVERFLOW_MIN_LOCAL 8199// Add a table cell to the PageBuilder, flushing leaf pages as needed.@@ -1209,6 +1261,14 @@ static uint32_t write_index_btree(FILE *fp, uint32_t *next_page, uint8_t **cellsreturn write_empty_index_leaf(fp, next_page);}+ /* Spill oversized index payloads to overflow pages BEFORE page building so+ * every cell added below is within the local-payload limit (see+ * INDEX_OVERFLOW_MAX_LOCAL). Overflow pages are allocated from *next_page+ * ahead of the leaf pages, which is fine — page order is arbitrary. */+ for (int i = 0; i < count; i++) {+ cells[i] = overflowize_index_cell(fp, next_page, cells[i], &cell_lens[i]);+ }+PageBuilder pb;pb_init(&pb, fp, *next_page, true);diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.cindex a488fa62b..7384b8fed 100644--- a/src/pipeline/pass_calls.c+++ b/src/pipeline/pass_calls.c@@ -209,7 +209,9 @@ static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *caconst cbm_gbuf_node_t *handler = cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);if (handler != NULL) {char hprops[CBM_SZ_256];- snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", hres.qualified_name);+ char esc_h[CBM_SZ_256];+ cbm_json_escape(esc_h, sizeof(esc_h), hres.qualified_name);+ snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", esc_h);cbm_gbuf_insert_edge(ctx->gbuf, handler->id, route_id, "HANDLES", hprops);}}diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.cindex 6d1bca7f2..8f8b90f4a 100644--- a/src/pipeline/pass_parallel.c+++ b/src/pipeline/pass_parallel.c@@ -507,7 +507,9 @@ static void insert_def_into_gbuf(extract_worker_state_t *ws, const cbm_file_infocbm_gbuf_upsert_node(ws->local_gbuf, "Route", def->route_path, route_qn,def->file_path ? def->file_path : fi->rel_path, 0, 0, rprops);char hprops[CBM_SZ_512];- snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", def->qualified_name);+ char esc_h[CBM_SZ_256];+ cbm_json_escape(esc_h, sizeof(esc_h), def->qualified_name);+ snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", esc_h);cbm_gbuf_insert_edge(ws->local_gbuf, func_id, route_id, "HANDLES", hprops);}}@@ -1288,7 +1290,9 @@ static void emit_route_registration(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *souconst cbm_gbuf_node_t *h = cbm_gbuf_find_by_qn(main_gbuf, hres.qualified_name);if (h) {char hp[CBM_SZ_256];- snprintf(hp, sizeof(hp), "{\"handler\":\"%s\"}", hres.qualified_name);+ char esc_h2[CBM_SZ_256];+ cbm_json_escape(esc_h2, sizeof(esc_h2), hres.qualified_name);+ snprintf(hp, sizeof(hp), "{\"handler\":\"%s\"}", esc_h2);cbm_gbuf_insert_edge(gbuf, h->id, rid, "HANDLES", hp);}}diff --git a/src/pipeline/pass_route_nodes.c b/src/pipeline/pass_route_nodes.cindex 68f469e0f..fdf31131e 100644--- a/src/pipeline/pass_route_nodes.c+++ b/src/pipeline/pass_route_nodes.c@@ -15,6 +15,7 @@* Service B: create_order() → HANDLES → Route("POST /api/orders")*/#include "foundation/constants.h"+#include "foundation/str_util.h" // cbm_json_escapeenum {RN_MAX_SCHEME = 12,@@ -588,17 +589,22 @@ static int try_create_data_flow(cbm_gbuf_t *gb, int64_t caller_id, int64_t handlchar handler_params[CBM_SZ_512];extract_param_names(handler_node, handler_params, sizeof(handler_params));+ /* Route names/QNs are sliced source text (URL strings, decorator args) and+ * can contain quotes — escape them or the edge properties JSON is+ * malformed (aborts json_extract consumers incl. integrity_check). */+ char esc_rname[CBM_SZ_256];+ char esc_rqn[CBM_SZ_512];+ cbm_json_escape(esc_rname, sizeof(esc_rname), route->name ? route->name : "");+ cbm_json_escape(esc_rqn, sizeof(esc_rqn), route->qualified_name ? route->qualified_name : "");char props[CBM_SZ_2K];int n;if (via_infra) {n = snprintf(props, sizeof(props),"{\"via\":\"%s\",\"route\":\"%s\",\"edge_type\":\"%s\",\"via_infra\":true",- route->name ? route->name : "",- route->qualified_name ? route->qualified_name : "", edge_type);+ esc_rname, esc_rqn, edge_type);} else {n = snprintf(props, sizeof(props), "{\"via\":\"%s\",\"route\":\"%s\",\"edge_type\":\"%s\"",- route->name ? route->name : "",- route->qualified_name ? route->qualified_name : "", edge_type);+ esc_rname, esc_rqn, edge_type);}if (n > 0 && (size_t)n < sizeof(props) - RN_PROPS_MARGIN) {diff --git a/tests/test_sqlite_writer.c b/tests/test_sqlite_writer.cindex 453d3459b..db8da3d61 100644--- a/tests/test_sqlite_writer.c+++ b/tests/test_sqlite_writer.c@@ -61,7 +61,8 @@ TEST(sw_minimal_data) {.url_path = ""},};- int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", nodes, 2, edges, 1, NULL, 0, NULL, 0);+ int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", nodes, 2, edges, 1,+ NULL, 0, NULL, 0);ASSERT_EQ(rc, 0);/* Verify via SQLite */@@ -195,8 +196,8 @@ TEST(sw_scale_and_indexes) {edge_count++;}- int rc =- cbm_write_db(path, "proj", "/repo", "2026-03-14T12:00:00Z", nodes, 100, edges, edge_count, NULL, 0, NULL, 0);+ int rc = cbm_write_db(path, "proj", "/repo", "2026-03-14T12:00:00Z", nodes, 100, edges,+ edge_count, NULL, 0, NULL, 0);ASSERT_EQ(rc, 0);sqlite3 *db = NULL;@@ -274,14 +275,90 @@ TEST(sw_scale_and_indexes) {PASS();}-TEST(sw_empty) {+/* Index keys longer than the index-page max-local payload (~16.4 KB at the+ * 64 KB page size) MUST spill to overflow pages; writing them fully inline+ * makes SQLite read key bytes as an overflow page number — PRAGMA+ * integrity_check reports "invalid page number 0x43654C6C ('CeLl')" and name+ * lookups silently return nothing (seen on elasticsearch: very long+ * Section/heading names). */+TEST(sw_long_index_keys_overflow) {char path[256];ASSERT_EQ(make_temp_db(path, sizeof(path)), 0);- int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", NULL, 0, NULL, 0,+ enum { LONGN = 20000 };+ char *longname = malloc(LONGN + 1);+ char *longqn = malloc(LONGN + 16);+ ASSERT_NOT_NULL(longname);+ ASSERT_NOT_NULL(longqn);+ memset(longname, 'N', LONGN);+ longname[LONGN] = '\0';+ snprintf(longqn, LONGN + 16, "test.%s", longname);++ CBMDumpNode nodes[3] = {+ {.id = 1,+ .project = "test",+ .label = "Module",+ .name = "main",+ .qualified_name = "test.main",+ .file_path = "main.md",+ .start_line = 1,+ .end_line = 2,+ .properties = "{}"},+ {.id = 2,+ .project = "test",+ .label = "Section",+ .name = longname,+ .qualified_name = longqn,+ .file_path = "main.md",+ .start_line = 3,+ .end_line = 9,+ .properties = "{}"},+ {.id = 3,+ .project = "test",+ .label = "Function",+ .name = "after",+ .qualified_name = "test.main.after",+ .file_path = "main.md",+ .start_line = 10,+ .end_line = 12,+ .properties = "{}"},+ };++ int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", nodes, 3, NULL, 0,NULL, 0, NULL, 0);ASSERT_EQ(rc, 0);+ sqlite3 *db = NULL;+ ASSERT_EQ(sqlite3_open(path, &db), SQLITE_OK);+ sqlite3_stmt *stmt = NULL;+ sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &stmt, NULL);+ ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);+ const char *integrity = (const char *)sqlite3_column_text(stmt, 0);+ ASSERT_STR_EQ(integrity, "ok");+ sqlite3_finalize(stmt);++ /* The long-named row must be findable via the name index. */+ sqlite3_prepare_v2(db, "SELECT id FROM nodes WHERE name = ?1", -1, &stmt, NULL);+ sqlite3_bind_text(stmt, 1, longname, -1, SQLITE_STATIC);+ ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);+ ASSERT_EQ(sqlite3_column_int64(stmt, 0), 2);+ sqlite3_finalize(stmt);+ sqlite3_close(db);++ free(longname);+ free(longqn);+ unlink(path);+ PASS();+}++TEST(sw_empty) {+ char path[256];+ ASSERT_EQ(make_temp_db(path, sizeof(path)), 0);++ int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", NULL, 0, NULL, 0, NULL,+ 0, NULL, 0);+ ASSERT_EQ(rc, 0);+sqlite3 *db = NULL;rc = sqlite3_open(path, &db);ASSERT_EQ(rc, SQLITE_OK);@@ -326,8 +403,8 @@ TEST(sw_multi_page) {};}- int rc = cbm_write_db(path, "p", "/r", "2026-01-01T00:00:00Z", nodes, N, NULL, 0, NULL, 0,- NULL, 0);+ int rc =+ cbm_write_db(path, "p", "/r", "2026-01-01T00:00:00Z", nodes, N, NULL, 0, NULL, 0, NULL, 0);ASSERT_EQ(rc, 0);sqlite3 *db = NULL;@@ -404,7 +481,7 @@ TEST(sw_oversized_node) {}};int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-28T00:00:00Z", nodes, 1, NULL, 0,- NULL, 0, NULL, 0);+ NULL, 0, NULL, 0);free(big_props);ASSERT_EQ(rc, 0);@@ -443,6 +520,7 @@ TEST(sw_oversized_node) {SUITE(sqlite_writer) {RUN_TEST(sw_minimal_data);RUN_TEST(sw_scale_and_indexes);+ RUN_TEST(sw_long_index_keys_overflow);RUN_TEST(sw_empty);RUN_TEST(sw_multi_page);RUN_TEST(sw_oversized_node);