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
feat(store): persist per-file LSP surfaces for closure-repair incremental
Martin Vogel8d3685bfcae7
diff --git a/src/store/store.c b/src/store/store.cindex f64ae98ba..b3eba79e6 100644--- a/src/store/store.c+++ b/src/store/store.c@@ -277,6 +277,22 @@ static int init_schema(cbm_store_t *s) {" created_at TEXT NOT NULL,"" updated_at TEXT NOT NULL"");"+ /* One row per file: its serialized cross-file LSP surface + the+ * closure-repair metadata (surface hash, referenced-name bloom,+ * governing-config context). Written at publication from exactly the+ * def set pass_lsp_cross registered, so an incremental run can+ * rehydrate the cross registries for files it does not re-parse.+ * Deliberately SEPARATE from the graph tables (like index_coverage):+ * this is resolution input, not part of the graph. */+ "CREATE TABLE IF NOT EXISTS lsp_surface ("+ " project TEXT NOT NULL REFERENCES projects(name) ON DELETE CASCADE,"+ " rel_path TEXT NOT NULL,"+ " surface_sha TEXT NOT NULL,"+ " defs_json TEXT NOT NULL,"+ " ref_bloom BLOB,"+ " config_ctx TEXT NOT NULL DEFAULT '',"+ " PRIMARY KEY (project, rel_path)"+ ");"/* Best-effort indexing-coverage signal (#963). One row per file the* indexer could not fully cover: kind "parse_partial" (indexed, but the* parse tree had ERROR/MISSING regions — detail = 1-based line ranges)@@ -3364,6 +3380,170 @@ int cbm_store_upsert_file_hash_batch(cbm_store_t *s, const cbm_file_hash_t *hashreturn cbm_store_commit(s);}+/* ── LSP surface rows (closure-repair incremental) ─────────────── */++static int upsert_lsp_surface_row(cbm_store_t *s, const cbm_lsp_surface_row_t *row) {+ const char *sql =+ "INSERT INTO lsp_surface (project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx)"+ " VALUES (?1, ?2, ?3, ?4, ?5, ?6)"+ " ON CONFLICT(project, rel_path) DO UPDATE SET surface_sha = excluded.surface_sha,"+ " defs_json = excluded.defs_json, ref_bloom = excluded.ref_bloom,"+ " config_ctx = excluded.config_ctx";+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "lsp_surface upsert prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, ST_COL_1, row->project);+ bind_text(stmt, ST_COL_2, row->rel_path);+ bind_text(stmt, ST_COL_3, row->surface_sha ? row->surface_sha : "");+ bind_text(stmt, ST_COL_4, row->defs_json ? row->defs_json : "[]");+ if (row->ref_bloom && row->ref_bloom_len > 0) {+ sqlite3_bind_blob(stmt, ST_COL_5, row->ref_bloom, row->ref_bloom_len, BIND_TRANSIENT);+ } else {+ sqlite3_bind_null(stmt, ST_COL_5);+ }+ bind_text(stmt, ST_COL_6, row->config_ctx ? row->config_ctx : "");+ int rc = sqlite3_step(stmt);+ sqlite3_finalize(stmt);+ if (rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "lsp_surface upsert step");+ return CBM_STORE_ERR;+ }+ return CBM_STORE_OK;+}++int cbm_store_upsert_lsp_surface_batch(cbm_store_t *s, const cbm_lsp_surface_row_t *rows,+ int count) {+ if (count == 0) {+ return CBM_STORE_OK;+ }+ if (!s || !rows || count < 0) {+ return CBM_STORE_ERR;+ }+ int rc = cbm_store_begin(s);+ if (rc != CBM_STORE_OK) {+ return rc;+ }+ for (int i = 0; i < count; i++) {+ rc = upsert_lsp_surface_row(s, &rows[i]);+ if (rc != CBM_STORE_OK) {+ cbm_store_rollback(s);+ return rc;+ }+ }+ return cbm_store_commit(s);+}++int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surface_row_t **out,+ int *count) {+ *out = NULL;+ *count = 0;+ const char *sql = "SELECT project, rel_path, surface_sha, defs_json, ref_bloom, config_ctx"+ " FROM lsp_surface WHERE project = ?1 ORDER BY rel_path";+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "lsp_surface get prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, ST_COL_1, project);++ int cap = ST_INIT_CAP_8;+ int n = 0;+ cbm_lsp_surface_row_t *rows = calloc((size_t)cap, sizeof(*rows));+ if (!rows) {+ sqlite3_finalize(stmt);+ return CBM_STORE_ERR;+ }+ int step_rc;+ bool oom = false;+ while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {+ if (n >= cap) {+ cap *= ST_GROWTH;+ cbm_lsp_surface_row_t *grown = realloc(rows, (size_t)cap * sizeof(*rows));+ if (!grown) {+ oom = true;+ break;+ }+ rows = grown;+ memset(rows + n, 0, (size_t)(cap - n) * sizeof(*rows));+ }+ cbm_lsp_surface_row_t *r = &rows[n];+ memset(r, 0, sizeof(*r));+ const char *proj = (const char *)sqlite3_column_text(stmt, 0);+ const char *rel = (const char *)sqlite3_column_text(stmt, ST_COL_1);+ const char *sha = (const char *)sqlite3_column_text(stmt, ST_COL_2);+ const char *defs = (const char *)sqlite3_column_text(stmt, ST_COL_3);+ const void *bloom = sqlite3_column_blob(stmt, ST_COL_4);+ int bloom_len = sqlite3_column_bytes(stmt, ST_COL_4);+ const char *cfg = (const char *)sqlite3_column_text(stmt, ST_COL_5);+ r->project = strdup(proj ? proj : "");+ r->rel_path = strdup(rel ? rel : "");+ r->surface_sha = strdup(sha ? sha : "");+ r->defs_json = strdup(defs ? defs : "[]");+ r->config_ctx = strdup(cfg ? cfg : "");+ if (bloom && bloom_len > 0) {+ void *copy = malloc((size_t)bloom_len);+ if (copy) {+ memcpy(copy, bloom, (size_t)bloom_len);+ r->ref_bloom = copy;+ r->ref_bloom_len = bloom_len;+ }+ }+ if (!r->project || !r->rel_path || !r->surface_sha || !r->defs_json || !r->config_ctx ||+ (bloom && bloom_len > 0 && !r->ref_bloom)) {+ n++; /* count the partial row so the free below releases it */+ oom = true;+ break;+ }+ n++;+ }+ sqlite3_finalize(stmt);+ if (oom || step_rc != SQLITE_DONE) {+ cbm_store_free_lsp_surfaces(rows, n);+ if (oom) {+ return CBM_STORE_ERR;+ }+ store_set_error_sqlite(s, "lsp_surface get step");+ return CBM_STORE_ERR;+ }+ *out = rows;+ *count = n;+ return CBM_STORE_OK;+}++int cbm_store_delete_lsp_surfaces(cbm_store_t *s, const char *project) {+ const char *sql = "DELETE FROM lsp_surface WHERE project = ?1";+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "lsp_surface delete prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, ST_COL_1, project);+ int rc = sqlite3_step(stmt);+ sqlite3_finalize(stmt);+ if (rc != SQLITE_DONE) {+ store_set_error_sqlite(s, "lsp_surface delete step");+ return CBM_STORE_ERR;+ }+ return CBM_STORE_OK;+}++void cbm_store_free_lsp_surfaces(cbm_lsp_surface_row_t *rows, int count) {+ if (!rows) {+ return;+ }+ for (int i = 0; i < count; i++) {+ free((void *)rows[i].project);+ free((void *)rows[i].rel_path);+ free((void *)rows[i].surface_sha);+ free((void *)rows[i].defs_json);+ free((void *)rows[i].ref_bloom);+ free((void *)rows[i].config_ctx);+ }+ free(rows);+}+/* ── FindEdgesByURLPath ────────────────────────────────────────── */int cbm_store_find_edges_by_url_path(cbm_store_t *s, const char *project, const char *keyword,diff --git a/src/store/store.h b/src/store/store.hindex a3c517b49..859b5a2ba 100644--- a/src/store/store.h+++ b/src/store/store.h@@ -61,6 +61,21 @@ typedef struct {int64_t size;} cbm_file_hash_t;+/* One file's persisted LSP surface: the serialized cross-file definition set+ * (exactly what pass_lsp_cross registration consumes) plus the metadata the+ * closure-repair incremental route needs to decide and bound its work. The+ * store treats defs_json/ref_bloom as opaque; the codec lives with+ * pass_lsp_cross, which is the only writer and reader of their contents. */+typedef struct {+ const char *project;+ const char *rel_path;+ const char *surface_sha; /* sha256 hex of defs_json (the early-cutoff key) */+ const char *defs_json; /* canonical JSON array of the file's LSP defs */+ const void *ref_bloom; /* referenced-identifier bloom blob (may be NULL) */+ int ref_bloom_len;+ const char *config_ctx; /* governing-config context hash ("" = none) */+} cbm_lsp_surface_row_t;+/* Find nodes overlapping a line range in a file (excludes Module/Package). */int cbm_store_find_nodes_by_file_overlap(cbm_store_t *s, const char *project, const char *file_path,int start_line, int end_line, cbm_node_t **out,@@ -93,6 +108,19 @@ int cbm_store_batch_count_degrees(cbm_store_t *s, const int64_t *node_ids, int i/* Upsert file hashes in batch. */int cbm_store_upsert_file_hash_batch(cbm_store_t *s, const cbm_file_hash_t *hashes, int count);+/* ── LSP surface rows (closure-repair incremental) ───────────────+ * Upsert/get/delete are whole-row, keyed (project, rel_path). get returns+ * heap rows released with cbm_store_free_lsp_surfaces. A project with no+ * rows returns OK with *count == 0 — callers treat that as "no surface+ * data" and route to a full rebuild, which is also the upgrade path for+ * databases written before this table existed. */+int cbm_store_upsert_lsp_surface_batch(cbm_store_t *s, const cbm_lsp_surface_row_t *rows,+ int count);+int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surface_row_t **out,+ int *count);+int cbm_store_delete_lsp_surfaces(cbm_store_t *s, const char *project);+void cbm_store_free_lsp_surfaces(cbm_lsp_surface_row_t *rows, int count);+/* Find edges whose properties contain a url_path matching the keyword. */int cbm_store_find_edges_by_url_path(cbm_store_t *s, const char *project, const char *keyword,cbm_edge_t **out, int *count);diff --git a/tests/test_store_nodes.c b/tests/test_store_nodes.cindex cd37a47ce..d80808a37 100644--- a/tests/test_store_nodes.c+++ b/tests/test_store_nodes.c@@ -502,6 +502,81 @@ TEST(store_file_hash_crud) {PASS();}+TEST(store_lsp_surface_round_trip) {+ cbm_store_t *s = cbm_store_open_memory();+ ASSERT_NOT_NULL(s);+ cbm_store_upsert_project(s, "test", "/tmp/test");++ /* Empty project: OK + zero rows — the "no surface data, route to full+ * rebuild" signal, and the upgrade path for pre-table databases. */+ cbm_lsp_surface_row_t *rows = NULL;+ int count = CBM_NOT_FOUND;+ ASSERT_EQ(cbm_store_get_lsp_surfaces(s, "test", &rows, &count), CBM_STORE_OK);+ ASSERT_EQ(count, 0);+ cbm_store_free_lsp_surfaces(rows, count);++ /* Batch with the field shapes the codec will produce: a real defs array,+ * an empty surface, a binary bloom (with embedded NUL), and no bloom. */+ const unsigned char bloom[] = {0x01, 0x00, 0xfe, 0x7f, 0x00, 0xab};+ cbm_lsp_surface_row_t in[] = {+ {.project = "test",+ .rel_path = "pkg/a.go",+ .surface_sha = "sha-a",+ .defs_json = "[{\"qn\":\"pkg.A\",\"sn\":\"A\",\"label\":\"Function\"}]",+ .ref_bloom = bloom,+ .ref_bloom_len = (int)sizeof(bloom),+ .config_ctx = "cfg-1"},+ {.project = "test",+ .rel_path = "pkg/b.go",+ .surface_sha = "sha-b",+ .defs_json = "[]",+ .ref_bloom = NULL,+ .ref_bloom_len = 0,+ .config_ctx = ""},+ };+ ASSERT_EQ(cbm_store_upsert_lsp_surface_batch(s, in, 2), CBM_STORE_OK);++ ASSERT_EQ(cbm_store_get_lsp_surfaces(s, "test", &rows, &count), CBM_STORE_OK);+ ASSERT_EQ(count, 2);+ /* ORDER BY rel_path: a.go before b.go. */+ ASSERT_STR_EQ(rows[0].rel_path, "pkg/a.go");+ ASSERT_STR_EQ(rows[0].surface_sha, "sha-a");+ ASSERT_STR_EQ(rows[0].defs_json, "[{\"qn\":\"pkg.A\",\"sn\":\"A\",\"label\":\"Function\"}]");+ ASSERT_STR_EQ(rows[0].config_ctx, "cfg-1");+ ASSERT_EQ(rows[0].ref_bloom_len, (int)sizeof(bloom));+ ASSERT_TRUE(rows[0].ref_bloom != NULL &&+ memcmp(rows[0].ref_bloom, bloom, sizeof(bloom)) == 0);+ ASSERT_STR_EQ(rows[1].rel_path, "pkg/b.go");+ ASSERT_STR_EQ(rows[1].defs_json, "[]");+ ASSERT_EQ(rows[1].ref_bloom_len, 0);+ ASSERT_TRUE(rows[1].ref_bloom == NULL);+ cbm_store_free_lsp_surfaces(rows, count);++ /* Upsert-on-conflict replaces the whole row, including dropping a bloom. */+ cbm_lsp_surface_row_t update = {.project = "test",+ .rel_path = "pkg/a.go",+ .surface_sha = "sha-a2",+ .defs_json = "[]",+ .ref_bloom = NULL,+ .ref_bloom_len = 0,+ .config_ctx = ""};+ ASSERT_EQ(cbm_store_upsert_lsp_surface_batch(s, &update, 1), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_get_lsp_surfaces(s, "test", &rows, &count), CBM_STORE_OK);+ ASSERT_EQ(count, 2);+ ASSERT_STR_EQ(rows[0].surface_sha, "sha-a2");+ ASSERT_EQ(rows[0].ref_bloom_len, 0);+ cbm_store_free_lsp_surfaces(rows, count);++ /* Project-scoped delete removes everything. */+ ASSERT_EQ(cbm_store_delete_lsp_surfaces(s, "test"), CBM_STORE_OK);+ ASSERT_EQ(cbm_store_get_lsp_surfaces(s, "test", &rows, &count), CBM_STORE_OK);+ ASSERT_EQ(count, 0);+ cbm_store_free_lsp_surfaces(rows, count);++ cbm_store_close(s);+ PASS();+}+TEST(store_file_hash_upsert_rejects_null_required_fields) {/* Pins the API contract that `cbm_store_upsert_file_hash` returns* CBM_STORE_ERR (not silent OK) when a NOT NULL column would receive@@ -1996,6 +2071,7 @@ SUITE(store_nodes) {RUN_TEST(store_node_batch_empty);RUN_TEST(store_cascade_delete);RUN_TEST(store_file_hash_crud);+ RUN_TEST(store_lsp_surface_round_trip);RUN_TEST(store_file_hash_upsert_rejects_null_required_fields);RUN_TEST(store_node_properties_json);RUN_TEST(store_node_null_properties);