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.

Add semantic_query to search_graph: vector search via cbm_cosine_i8 SQL function

Martin Vogel7a9b7db830c0
diff --git a/internal/cbm/sqlite_writer.c b/internal/cbm/sqlite_writer.c
index da7c7b239..b29fca50e 100644
--- a/internal/cbm/sqlite_writer.c
+++ b/internal/cbm/sqlite_writer.c
@@ -107,6 +107,9 @@ enum {
// SQLite text serial type offset: serial_type = len*2 + TEXT_SERIAL_BASE.
#define TEXT_SERIAL_BASE 13
+// SQLite blob serial type offset: serial_type = len*2 + BLOB_SERIAL_BASE.
+#define BLOB_SERIAL_BASE 12
+
// SQLite integer storage range limits.
#define INT8_MAX_VAL 127
#define INT16_MAX_VAL 32767
@@ -360,6 +363,16 @@ static void rec_add_text(RecordBuilder *r, const char *s) {
}
}
+static void rec_add_blob(RecordBuilder *r, const uint8_t *data, int len) {
+ int64_t st = len > 0 ? (int64_t)len * 2 + BLOB_SERIAL_BASE : 0;
+ uint8_t vbuf[VARINT_MAX_BYTES];
+ int vlen = put_varint(vbuf, st);
+ dynbuf_append(&r->header, vbuf, vlen);
+ if (len > 0 && data) {
+ dynbuf_append(&r->body, data, len);
+ }
+}
+
// Finalize: returns the complete record bytes (header_len + header + body).
// Caller must free the returned buffer.
static uint8_t *rec_finalize(RecordBuilder *r, int *out_len) {
@@ -718,6 +731,21 @@ static uint8_t *build_edge_record(const CBMDumpEdge *e, int *out_len) {
return data;
}
+// Build a node_vectors table record: (node_id, project, vector)
+// Includes node_id in the record body (same pattern as build_node_record).
+static uint8_t *build_vector_record(const CBMDumpVector *v, int *out_len) {
+ RecordBuilder r;
+ rec_init(&r);
+
+ rec_add_int(&r, v->node_id);
+ rec_add_text(&r, v->project);
+ rec_add_blob(&r, v->vector, v->vector_len);
+
+ uint8_t *data = rec_finalize(&r, out_len);
+ rec_free(&r);
+ return data;
+}
+
// Build a projects table record: (name, indexed_at, root_path)
static uint8_t *build_project_record(const char *name, const char *indexed_at,
const char *root_path, int *out_len) {
@@ -1462,10 +1490,13 @@ typedef struct {
int node_count;
CBMDumpEdge *edges;
int edge_count;
+ CBMDumpVector *vectors;
+ int vector_count;
} write_db_ctx_t;
-/* Phase 1: Write node + edge data tables (streaming). */
-static int write_data_tables(write_db_ctx_t *w, uint32_t *nodes_root, uint32_t *edges_root) {
+/* Phase 1: Write node + edge + vector data tables (streaming). */
+static int write_data_tables(write_db_ctx_t *w, uint32_t *nodes_root, uint32_t *edges_root,
+ uint32_t *vectors_root) {
if (w->node_count > 0) {
PageBuilder pb;
pb_init(&pb, w->fp, w->next_page, false);
@@ -1501,6 +1532,26 @@ static int write_data_tables(write_db_ctx_t *w, uint32_t *nodes_root, uint32_t *
} else {
*edges_root = write_table_btree(w->fp, &w->next_page, NULL, NULL, NULL, 0, false);
}
+
+ /* node_vectors table — uses node_id as rowid (not AUTOINCREMENT) */
+ if (w->vector_count > 0 && w->vectors) {
+ PageBuilder pb;
+ pb_init(&pb, w->fp, w->next_page, false);
+ for (int i = 0; i < w->vector_count; i++) {
+ int rec_len;
+ uint8_t *rec = build_vector_record(&w->vectors[i], &rec_len);
+ if (!rec) {
+ return ERR_WRITE_FAILED;
+ }
+ pb_add_table_cell_with_flush(&pb, w->vectors[i].node_id, rec, rec_len,
+ i > 0 ? w->vectors[i - SKIP_ONE].node_id : 0);
+ free(rec);
+ }
+ *vectors_root =
+ pb_finalize_table(&pb, &w->next_page, w->vectors[w->vector_count - SKIP_ONE].node_id);
+ } else {
+ *vectors_root = write_table_btree(w->fp, &w->next_page, NULL, NULL, NULL, 0, false);
+ }
return 0;
}
@@ -1548,7 +1599,7 @@ static void write_metadata_tables(write_db_ctx_t *w, uint32_t *projects_root,
int cbm_write_db(const char *path, const char *project, const char *root_path,
const char *indexed_at, CBMDumpNode *nodes, int node_count, CBMDumpEdge *edges,
- int edge_count) {
+ int edge_count, CBMDumpVector *vectors, int vector_count) {
FILE *fp = fopen(path, "wb");
if (!fp) {
return CBM_NOT_FOUND;
@@ -1562,12 +1613,15 @@ int cbm_write_db(const char *path, const char *project, const char *root_path,
.nodes = nodes,
.node_count = node_count,
.edges = edges,
- .edge_count = edge_count};
+ .edge_count = edge_count,
+ .vectors = vectors,
+ .vector_count = vector_count};
- // Phase 1: Data tables (streaming node + edge records)
+ // Phase 1: Data tables (streaming node + edge + vector records)
uint32_t nodes_root;
uint32_t edges_root;
- int rc = write_data_tables(&w, &nodes_root, &edges_root);
+ uint32_t vectors_root;
+ int rc = write_data_tables(&w, &nodes_root, &edges_root, &vectors_root);
if (rc != 0) {
(void)fclose(fp);
return rc;
@@ -1753,6 +1807,9 @@ int cbm_write_db(const char *path, const char *project, const char *root_path,
"NULL,\n\t\t\tupdated_at TEXT NOT NULL\n\t\t)"},
{"index", "sqlite_autoindex_project_summaries_1", "project_summaries",
autoindex_summaries_root, NULL},
+ {"table", "node_vectors", "node_vectors", vectors_root,
+ "CREATE TABLE node_vectors (\n\t\tnode_id INTEGER PRIMARY KEY,\n\t\tproject TEXT NOT "
+ "NULL,\n\t\tvector BLOB NOT NULL\n\t)"},
{"table", "sqlite_sequence", "sqlite_sequence", sqlite_seq_root,
"CREATE TABLE sqlite_sequence(name,seq)"},
};
diff --git a/internal/cbm/sqlite_writer.h b/internal/cbm/sqlite_writer.h
index bf28dbdd0..92cbf096d 100644
--- a/internal/cbm/sqlite_writer.h
+++ b/internal/cbm/sqlite_writer.h
@@ -27,13 +27,21 @@ typedef struct {
const char *url_path; // extracted from properties by Go (for idx_edges_url_path)
} CBMDumpEdge;
+typedef struct {
+ int64_t node_id; // final sequential ID (matches nodes.id)
+ const char *project;
+ const uint8_t *vector; // int8-quantized vector blob
+ int vector_len; // length in bytes (e.g. 256 for d=256)
+} CBMDumpVector;
+
// --- Public API ---
// Write a complete SQLite .db file from sorted in-memory data.
// Constructs B-tree pages directly — no SQL parser, no INSERTs.
// Returns 0 on success, non-zero on error.
+// vectors/vector_count may be NULL/0 if no vectors are available.
int cbm_write_db(const char *path, const char *project, const char *root_path,
const char *indexed_at, CBMDumpNode *nodes, int node_count, CBMDumpEdge *edges,
- int edge_count);
+ int edge_count, CBMDumpVector *vectors, int vector_count);
#endif // CBM_SQLITE_WRITER_H
diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.c
index 4eb6fa8c0..791a396d6 100644
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@ -82,6 +82,12 @@ struct cbm_gbuf {
CBMHashTable *edges_by_source_type; /* "srcID:type" → edge_ptr_array_t* */
CBMHashTable *edges_by_target_type; /* "tgtID:type" → edge_ptr_array_t* */
CBMHashTable *edges_by_type; /* "type" → edge_ptr_array_t* */
+
+ /* Vector storage for semantic embeddings (filled by pass_semantic_edges,
+ * consumed by cbm_write_db during dump). */
+ CBMDumpVector *dump_vectors;
+ int dump_vector_count;
+ int dump_vector_cap;
};
/* ── Helpers ─────────────────────────────────────────────────────── */
@@ -419,11 +425,50 @@ void cbm_gbuf_free(cbm_gbuf_t *gb) {
cbm_ht_free(gb->edges_by_type);
}
+ /* Free vector storage */
+ for (int i = 0; i < gb->dump_vector_count; i++) {
+ free((void *)gb->dump_vectors[i].vector);
+ }
+ free(gb->dump_vectors);
+
free(gb->project);
free(gb->root_path);
free(gb);
}
+/* ── Vector storage ──────────────────────────────────────────────── */
+
+int cbm_gbuf_store_vector(cbm_gbuf_t *gb, int64_t node_id, const uint8_t *vector, int vector_len) {
+ if (!gb || !vector || vector_len <= 0) {
+ return -1;
+ }
+ enum { VEC_INIT_CAP = 1024, VEC_GROW = 2 };
+ if (gb->dump_vector_count >= gb->dump_vector_cap) {
+ int new_cap =
+ gb->dump_vector_cap < VEC_INIT_CAP ? VEC_INIT_CAP : gb->dump_vector_cap * VEC_GROW;
+ CBMDumpVector *grown = realloc(gb->dump_vectors, (size_t)new_cap * sizeof(CBMDumpVector));
+ if (!grown) {
+ return -1;
+ }
+ gb->dump_vectors = grown;
+ gb->dump_vector_cap = new_cap;
+ }
+ /* Copy vector data */
+ uint8_t *vec_copy = malloc((size_t)vector_len);
+ if (!vec_copy) {
+ return -1;
+ }
+ memcpy(vec_copy, vector, (size_t)vector_len);
+
+ gb->dump_vectors[gb->dump_vector_count++] = (CBMDumpVector){
+ .node_id = node_id,
+ .project = gb->project, /* borrowed — valid until gbuf_free */
+ .vector = vec_copy,
+ .vector_len = vector_len,
+ };
+ return 0;
+}
+
/* ── ID accessors ────────────────────────────────────────────────── */
int64_t cbm_gbuf_next_id(const cbm_gbuf_t *gb) {
@@ -1197,7 +1242,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
* Callers must delete the old .db before calling this (reindex)
* or ensure no file exists (first index). */
int rc = cbm_write_db(path, gb->project, gb->root_path, indexed_at, dump_nodes, node_idx,
- dump_edges, edge_idx);
+ dump_edges, edge_idx, gb->dump_vectors, gb->dump_vector_count);
{
char b1[CBM_SZ_16];
diff --git a/src/graph_buffer/graph_buffer.h b/src/graph_buffer/graph_buffer.h
index 5daeb56a4..e35d500c0 100644
--- a/src/graph_buffer/graph_buffer.h
+++ b/src/graph_buffer/graph_buffer.h
@@ -148,6 +148,13 @@ int cbm_gbuf_edge_count_by_type(const cbm_gbuf_t *gb, const char *type);
/* Delete all edges of a type. */
int cbm_gbuf_delete_edges_by_type(cbm_gbuf_t *gb, const char *type);
+/* ── Vector storage (for semantic embeddings) ───────────────────── */
+
+/* Store an int8-quantized vector for a node. The vector data is copied.
+ * Called by pass_semantic_edges after computing RI vectors.
+ * Vectors are carried through to cbm_write_db during the dump phase. */
+int cbm_gbuf_store_vector(cbm_gbuf_t *gb, int64_t node_id, const uint8_t *vector, int vector_len);
+
/* ── Dump to SQLite ──────────────────────────────────────────────── */
/* Dump the entire buffer to a SQLite file using the direct page writer.
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 9fb80962a..ba2affde8 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -269,7 +269,9 @@ static const tool_def_t TOOLS[] = {
"\"string\"},\"name_pattern\":{\"type\":\"string\"},\"qn_pattern\":{\"type\":\"string\"},"
"\"file_pattern\":{\"type\":\"string\"},\"relationship\":{\"type\":\"string\"},\"min_degree\":"
"{\"type\":\"integer\"},\"max_degree\":{\"type\":\"integer\"},\"exclude_entry_points\":{"
- "\"type\":\"boolean\"},\"include_connected\":{\"type\":\"boolean\"},\"limit\":{\"type\":"
+ "\"type\":\"boolean\"},\"include_connected\":{\"type\":\"boolean\"},\"semantic_query\":{"
+ "\"type\":\"array\",\"items\":{\"type\":\"string\"},\"description\":\"Keywords for semantic "
+ "vector search (requires moderate/full index mode)\"},\"limit\":{\"type\":"
"\"integer\",\"description\":\"Max results. Default: "
"unlimited\"},\"offset\":{\"type\":\"integer\",\"default\":0}},\"required\":[\"project\"]}"},
@@ -1099,6 +1101,56 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
yyjson_mut_obj_add_val(doc, root, "results", results);
yyjson_mut_obj_add_bool(doc, root, "has_more", out.total > offset + out.count);
+ /* Semantic vector search: parse semantic_query array and run vector search */
+ {
+ yyjson_doc *args_doc = yyjson_read(args, strlen(args), 0);
+ yyjson_val *args_root = args_doc ? yyjson_doc_get_root(args_doc) : NULL;
+ yyjson_val *sq_arr = args_root ? yyjson_obj_get(args_root, "semantic_query") : NULL;
+ if (sq_arr && yyjson_is_arr(sq_arr)) {
+ int kw_count = (int)yyjson_arr_size(sq_arr);
+ if (kw_count > 0) {
+ enum { MAX_KW = 32 };
+ const char *keywords[MAX_KW];
+ if (kw_count > MAX_KW) {
+ kw_count = MAX_KW;
+ }
+ size_t kw_idx = 0;
+ size_t kw_max = 0;
+ yyjson_val *kw_val;
+ int ki = 0;
+ yyjson_arr_foreach(sq_arr, kw_idx, kw_max, kw_val) {
+ if (ki < kw_count && yyjson_is_str(kw_val)) {
+ keywords[ki++] = yyjson_get_str(kw_val);
+ }
+ }
+
+ cbm_vector_result_t *vresults = NULL;
+ int vcount = 0;
+ int sem_limit = limit > 0 ? limit : CBM_SZ_16;
+ if (cbm_store_vector_search(store, project, keywords, ki, sem_limit, &vresults,
+ &vcount) == CBM_STORE_OK &&
+ vcount > 0) {
+ yyjson_mut_val *sem_results = yyjson_mut_arr(doc);
+ for (int v = 0; v < vcount; v++) {
+ yyjson_mut_val *vitem = yyjson_mut_obj(doc);
+ yyjson_mut_obj_add_strcpy(doc, vitem, "name", vresults[v].name);
+ yyjson_mut_obj_add_strcpy(doc, vitem, "qualified_name",
+ vresults[v].qualified_name);
+ yyjson_mut_obj_add_strcpy(doc, vitem, "label", vresults[v].label);
+ yyjson_mut_obj_add_strcpy(doc, vitem, "file_path", vresults[v].file_path);
+ yyjson_mut_obj_add_real(doc, vitem, "score", vresults[v].score);
+ yyjson_mut_arr_add_val(sem_results, vitem);
+ }
+ yyjson_mut_obj_add_val(doc, root, "semantic_results", sem_results);
+ cbm_store_free_vector_results(vresults, vcount);
+ }
+ }
+ }
+ if (args_doc) {
+ yyjson_doc_free(args_doc);
+ }
+ }
+
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
cbm_store_search_free(&out);
diff --git a/src/pipeline/pass_semantic_edges.c b/src/pipeline/pass_semantic_edges.c
index 0d81ca91f..88f0e5cab 100644
--- a/src/pipeline/pass_semantic_edges.c
+++ b/src/pipeline/pass_semantic_edges.c
@@ -383,8 +383,24 @@ int cbm_pipeline_pass_semantic_edges(cbm_pipeline_ctx_t *ctx) {
}
}
cbm_sem_normalize(&funcs[f].ri_vec);
+
+ /* Int8-quantize and store in graph buffer for vector search at query time */
+ uint8_t qvec[CBM_SEM_DIM];
+ for (int d = 0; d < CBM_SEM_DIM; d++) {
+ float clamped = funcs[f].ri_vec.v[d];
+ if (clamped > 1.0f) {
+ clamped = 1.0f;
+ }
+ if (clamped < -1.0f) {
+ clamped = -1.0f;
+ }
+ qvec[d] = (uint8_t)(int8_t)(clamped * 127.0f);
+ }
+ cbm_gbuf_store_vector(gbuf, funcs[f].node_id, qvec, CBM_SEM_DIM);
}
+ cbm_log_info("pass.semantic.vectors_stored", "count", itoa_log(func_count));
+
/* Phase 5: Build cosine-LSH index using random hyperplanes on RI vectors.
* This gives O(n) candidate generation instead of O(n²) brute-force.
*
diff --git a/src/store/store.c b/src/store/store.c
index 4920732e8..51b744bbc 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -11,6 +11,8 @@
#include <stdint.h>
#include "foundation/constants.h"
+#include <math.h>
+
enum {
ST_COL_1 = 1,
ST_COL_2 = 2,
@@ -59,6 +61,9 @@ enum {
#include "foundation/compat.h"
#include "foundation/compat_regex.h"
+#define XXH_INLINE_ALL
+#include "xxhash/xxhash.h"
+
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
@@ -340,6 +345,34 @@ static void sqlite_iregexp(sqlite3_context *ctx, int argc, sqlite3_value **argv)
sqlite3_result_int(ctx, rc == 0 ? SKIP_ONE : 0);
}
+/* Cosine similarity between two int8 BLOB vectors.
+ * Returns float in [-1, 1]. Used for vector search at query time. */
+static void sqlite_cosine_i8(sqlite3_context *ctx, int argc, sqlite3_value **argv) {
+ (void)argc;
+ if (sqlite3_value_type(argv[0]) != SQLITE_BLOB || sqlite3_value_type(argv[SKIP_ONE]) != SQLITE_BLOB) {
+ sqlite3_result_double(ctx, 0.0);
+ return;
+ }
+ int len_a = sqlite3_value_bytes(argv[0]);
+ int len_b = sqlite3_value_bytes(argv[SKIP_ONE]);
+ if (len_a != len_b || len_a == 0) {
+ sqlite3_result_double(ctx, 0.0);
+ return;
+ }
+ const int8_t *a = (const int8_t *)sqlite3_value_blob(argv[0]);
+ const int8_t *b = (const int8_t *)sqlite3_value_blob(argv[SKIP_ONE]);
+ int32_t dot = 0;
+ int32_t mag_a = 0;
+ int32_t mag_b = 0;
+ for (int i = 0; i < len_a; i++) {
+ dot += (int32_t)a[i] * (int32_t)b[i];
+ mag_a += (int32_t)a[i] * (int32_t)a[i];
+ mag_b += (int32_t)b[i] * (int32_t)b[i];
+ }
+ double denom = sqrt((double)mag_a) * sqrt((double)mag_b);
+ sqlite3_result_double(ctx, denom > 1e-10 ? (double)dot / denom : 0.0);
+}
+
/* ── Lifecycle ──────────────────────────────────────────────────── */
/* SQLite authorizer: deny dangerous operations that could be exploited via
@@ -391,6 +424,9 @@ static cbm_store_t *store_open_internal(const char *path, bool in_memory) {
/* Case-insensitive variant for search with case_sensitive=false */
sqlite3_create_function(s->db, "iregexp", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
sqlite_iregexp, NULL, NULL);
+ /* Int8 cosine similarity for vector search */
+ sqlite3_create_function(s->db, "cbm_cosine_i8", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
+ NULL, sqlite_cosine_i8, NULL, NULL);
if (configure_pragmas(s, in_memory) != CBM_STORE_OK || init_schema(s) != CBM_STORE_OK ||
create_user_indexes(s) != CBM_STORE_OK) {
@@ -443,6 +479,8 @@ cbm_store_t *cbm_store_open_path_query(const char *db_path) {
sqlite_regexp, NULL, NULL);
sqlite3_create_function(s->db, "iregexp", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
sqlite_iregexp, NULL, NULL);
+ sqlite3_create_function(s->db, "cbm_cosine_i8", ST_COL_2, SQLITE_UTF8 | SQLITE_DETERMINISTIC,
+ NULL, sqlite_cosine_i8, NULL, NULL);
if (configure_pragmas(s, false) != CBM_STORE_OK) {
sqlite3_close(s->db);
@@ -4525,3 +4563,146 @@ void cbm_store_free_file_hashes(cbm_file_hash_t *hashes, int count) {
}
free(hashes);
}
+
+/* ── Vector search ────────────────────────��──────────────────────── */
+
+int cbm_store_count_vectors(cbm_store_t *s, const char *project) {
+ if (!s || !project) {
+ return 0;
+ }
+ sqlite3_stmt *stmt = NULL;
+ const char *sql = "SELECT count(*) FROM node_vectors WHERE project = ?1";
+ if (sqlite3_prepare_v2(s->db, sql, -1, &stmt, NULL) != SQLITE_OK) {
+ return 0;
+ }
+ sqlite3_bind_text(stmt, SKIP_ONE, project, -1, SQLITE_STATIC);
+ int count = 0;
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
+ count = sqlite3_column_int(stmt, 0);
+ }
+ sqlite3_finalize(stmt);
+ return count;
+}
+
+void cbm_store_free_vector_results(cbm_vector_result_t *results, int count) {
+ if (!results) {
+ return;
+ }
+ for (int i = 0; i < count; i++) {
+ free(results[i].name);
+ free(results[i].qualified_name);
+ free(results[i].file_path);
+ free(results[i].label);
+ }
+ free(results);
+}
+
+int cbm_store_vector_search(cbm_store_t *s, const char *project, const char **keywords,
+ int keyword_count, int limit, cbm_vector_result_t **out,
+ int *out_count) {
+ *out = NULL;
+ *out_count = 0;
+ if (!s || !project || !keywords || keyword_count <= 0) {
+ return CBM_STORE_ERR;
+ }
+
+ /* Build merged query vector: sum of base RI vectors for each keyword.
+ * Base RI = sparse random vector from xxHash(keyword) — same construction
+ * as in semantic.c, so it projects into the same space as stored vectors. */
+ enum { VEC_DIM = 256, SPARSE_NNZE = 8, RI_SEED = 0x52494E44 };
+ float query_f[VEC_DIM];
+ memset(query_f, 0, sizeof(query_f));
+
+ for (int k = 0; k < keyword_count; k++) {
+ if (!keywords[k] || !keywords[k][0]) {
+ continue;
+ }
+ /* Generate base sparse random vector (same as cbm_sem_random_index) */
+ uint64_t seed = XXH3_64bits(keywords[k], strlen(keywords[k]));
+ for (int i = 0; i < SPARSE_NNZE; i++) {
+ uint64_t h = XXH3_64bits_withSeed(&i, sizeof(i), seed + RI_SEED);
+ int pos = (int)(h % VEC_DIM);
+ float sign = (h & SKIP_ONE) ? 1.0f : -1.0f;
+ query_f[pos] += sign;
+ }
+ }
+
+ /* Normalize */
+ float mag = 0.0f;
+ for (int i = 0; i < VEC_DIM; i++) {
+ mag += query_f[i] * query_f[i];
+ }
+ mag = sqrtf(mag);
+ if (mag < 1e-10f) {
+ return CBM_STORE_OK; /* zero vector — no results */
+ }
+ float inv = 1.0f / mag;
+
+ /* Int8 quantize */
+ int8_t query_i8[VEC_DIM];
+ for (int i = 0; i < VEC_DIM; i++) {
+ float v = query_f[i] * inv * 127.0f;
+ if (v > 127.0f) {
+ v = 127.0f;
+ }
+ if (v < -127.0f) {
+ v = -127.0f;
+ }
+ query_i8[i] = (int8_t)v;
+ }
+
+ /* Query: cosine similarity JOIN with nodes for metadata.
+ * We use a subquery to compute the score first, then join for metadata
+ * — this avoids SQLite computing the cosine for rows we won't return. */
+ const char *sql =
+ "SELECT n.id, n.name, n.qualified_name, n.file_path, n.label,"
+ " cbm_cosine_i8(v.vector, ?1) as score"
+ " FROM node_vectors v"
+ " INNER JOIN nodes n ON n.id = v.node_id"
+ " WHERE v.project = ?2"
+ " ORDER BY score DESC"
+ " LIMIT ?3";
+
+ sqlite3_stmt *stmt = NULL;
+ int prep_rc = sqlite3_prepare_v2(s->db, sql, -1, &stmt, NULL);
+ if (prep_rc != SQLITE_OK) {
+ (void)fprintf(stderr, "vector_search: %s\n", sqlite3_errmsg(s->db));
+ return CBM_STORE_ERR;
+ }
+
+ sqlite3_bind_blob(stmt, SKIP_ONE, query_i8, VEC_DIM, SQLITE_STATIC);
+ sqlite3_bind_text(stmt, ST_COL_2, project, -1, SQLITE_STATIC);
+ sqlite3_bind_int(stmt, ST_COL_3, limit > 0 ? limit : CBM_SZ_16);
+
+ cbm_vector_result_t *results = NULL;
+ int count = 0;
+ int cap = 0;
+
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ if (count >= cap) {
+ int nc = cap < CBM_SZ_16 ? CBM_SZ_16 : cap * ST_COL_2;
+ cbm_vector_result_t *grown = realloc(results, (size_t)nc * sizeof(cbm_vector_result_t));
+ if (!grown) {
+ break;
+ }
+ results = grown;
+ cap = nc;
+ }
+ results[count].node_id = sqlite3_column_int64(stmt, 0);
+ const char *name = (const char *)sqlite3_column_text(stmt, SKIP_ONE);
+ const char *qn = (const char *)sqlite3_column_text(stmt, ST_COL_2);
+ const char *fp = (const char *)sqlite3_column_text(stmt, ST_COL_3);
+ const char *label = (const char *)sqlite3_column_text(stmt, ST_COL_4);
+ results[count].name = name ? strdup(name) : strdup("");
+ results[count].qualified_name = qn ? strdup(qn) : strdup("");
+ results[count].file_path = fp ? strdup(fp) : strdup("");
+ results[count].label = label ? strdup(label) : strdup("");
+ results[count].score = sqlite3_column_double(stmt, ST_COL_5);
+ count++;
+ }
+
+ sqlite3_finalize(stmt);
+ *out = results;
+ *out_count = count;
+ return CBM_STORE_OK;
+}
diff --git a/src/store/store.h b/src/store/store.h
index 1715f4226..890869ced 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -608,4 +608,29 @@ void cbm_store_free_projects(cbm_project_t *projects, int count);
/* Free an array of file hashes. */
void cbm_store_free_file_hashes(cbm_file_hash_t *hashes, int count);
+/* ── Vector search ───────────────────────────────────────────────── */
+
+/* Result from vector similarity search. */
+typedef struct {
+ int64_t node_id;
+ char *name;
+ char *qualified_name;
+ char *file_path;
+ char *label;
+ double score;
+} cbm_vector_result_t;
+
+/* Search for nodes similar to the given query keywords using stored RI vectors.
+ * Builds a merged query vector from the keywords, then does cosine scan via
+ * the cbm_cosine_i8 SQL function joined with the nodes table.
+ * Returns results sorted by score DESC. Caller must free with cbm_store_free_vector_results. */
+int cbm_store_vector_search(cbm_store_t *s, const char *project, const char **keywords,
+ int keyword_count, int limit, cbm_vector_result_t **out, int *out_count);
+
+/* Free vector search results. */
+void cbm_store_free_vector_results(cbm_vector_result_t *results, int count);
+
+/* Count vectors for a project. */
+int cbm_store_count_vectors(cbm_store_t *s, const char *project);
+
#endif /* CBM_STORE_H */
diff --git a/tests/test_sqlite_writer.c b/tests/test_sqlite_writer.c
index f45a06689..7a1f5fda9 100644
--- a/tests/test_sqlite_writer.c
+++ b/tests/test_sqlite_writer.c
@@ -61,7 +61,7 @@ TEST(sw_minimal_data) {
.url_path = ""},
};
- int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", nodes, 2, edges, 1);
+ int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", nodes, 2, edges, 1, NULL, 0);
ASSERT_EQ(rc, 0);
/* Verify via SQLite */
@@ -196,7 +196,7 @@ TEST(sw_scale_and_indexes) {
}
int rc =
- cbm_write_db(path, "proj", "/repo", "2026-03-14T12:00:00Z", nodes, 100, edges, edge_count);
+ cbm_write_db(path, "proj", "/repo", "2026-03-14T12:00:00Z", nodes, 100, edges, edge_count, NULL, 0);
ASSERT_EQ(rc, 0);
sqlite3 *db = NULL;
@@ -278,7 +278,8 @@ 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);
+ int rc = cbm_write_db(path, "test", "/tmp/test", "2026-03-14T00:00:00Z", NULL, 0, NULL, 0,
+ NULL, 0);
ASSERT_EQ(rc, 0);
sqlite3 *db = NULL;
@@ -325,7 +326,7 @@ TEST(sw_multi_page) {
};
}
- int rc = cbm_write_db(path, "p", "/r", "2026-01-01T00:00:00Z", nodes, N, NULL, 0);
+ int rc = cbm_write_db(path, "p", "/r", "2026-01-01T00:00:00Z", nodes, N, NULL, 0, NULL, 0);
ASSERT_EQ(rc, 0);
sqlite3 *db = NULL;