normalize-semantic 0.3.2

Semantic retrieval layer for normalize: vector embeddings over structurally-derived chunks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Embedding storage: read/write embeddings from/to the structural index SQLite.
//!
//! Operates through direct `libsql` calls on the same connection as `FileIndex`.
//! The embeddings table is created lazily on first write.
//!
//! When sqlite-vec is available (`vec_embeddings` virtual table exists), inserts
//! and deletes are mirrored there so that [`ann_search`] can use the ANN index
//! instead of loading all vectors into memory.

use crate::schema::{
    CREATE_EMBEDDINGS_IDX_MODEL, CREATE_EMBEDDINGS_IDX_SOURCE, CREATE_EMBEDDINGS_TABLE,
    DROP_EMBEDDINGS_IDX_MODEL, DROP_EMBEDDINGS_IDX_SOURCE, DROP_EMBEDDINGS_TABLE,
    DROP_VEC_EMBEDDINGS_TABLE, create_vec_embeddings_ddl,
};
use crate::search::StoredEmbedding;
use crate::vec_ext::VecConnection;
use libsql::{Connection, params};

/// Number of ANN candidates to retrieve before staleness re-ranking.
///
/// Fetching more candidates gives the re-ranker more material to work with;
/// the caller can truncate to a smaller `top_k` afterwards.
pub const ANN_CANDIDATE_COUNT: usize = 50;

/// Ensure the embeddings table and indices exist.
pub async fn ensure_schema(conn: &Connection) -> Result<(), libsql::Error> {
    conn.execute(CREATE_EMBEDDINGS_TABLE, ()).await?;
    conn.execute(CREATE_EMBEDDINGS_IDX_SOURCE, ()).await?;
    conn.execute(CREATE_EMBEDDINGS_IDX_MODEL, ()).await?;
    Ok(())
}

/// Ensure the `vec_embeddings` ANN virtual table exists.
///
/// When a `VecConnection` is provided, the DDL is executed on it (where
/// sqlite-vec is registered).  Otherwise falls back to the main `libsql`
/// connection (which may not have vec, in which case the CREATE silently
/// fails).  Returns `true` if the table is available after this call.
pub async fn ensure_vec_schema(
    conn: &Connection,
    dims: usize,
    vec_conn: Option<&VecConnection>,
) -> bool {
    let ddl = create_vec_embeddings_ddl(dims);
    if let Some(vc) = vec_conn {
        vc.execute(&ddl).is_ok()
    } else {
        conn.execute(&ddl, ()).await.is_ok()
    }
}

/// Returns `true` if the `vec_embeddings` virtual table exists and is queryable.
///
/// When a `VecConnection` is provided, queries through it; otherwise falls
/// back to the `libsql` connection.
pub async fn vec_table_available(conn: &Connection, vec_conn: Option<&VecConnection>) -> bool {
    if let Some(vc) = vec_conn {
        vc.execute("SELECT rowid FROM vec_embeddings LIMIT 1")
            .is_ok()
    } else {
        conn.query("SELECT rowid FROM vec_embeddings LIMIT 1", ())
            .await
            .is_ok()
    }
}

/// Insert or replace one embedding row.
///
/// Uses `INSERT OR REPLACE` keyed on the UNIQUE constraint
/// `(source_type, source_path, source_id)` so re-indexing a symbol replaces the
/// old vector in a single statement — no SELECT-then-DELETE round-trip.
///
/// The corresponding row in `vec_embeddings` is also updated when a
/// `VecConnection` is provided (best-effort; errors are silently ignored).
#[allow(clippy::too_many_arguments)]
pub async fn upsert_embedding(
    conn: &Connection,
    source_type: &str,
    source_path: &str,
    source_id: Option<i64>,
    model: &str,
    last_commit: Option<&str>,
    staleness: f32,
    chunk_text: &str,
    embedding_bytes: &[u8],
    vec_conn: Option<&VecConnection>,
) -> Result<(), libsql::Error> {
    // INSERT OR REPLACE: if a row with the same (source_type, source_path, source_id)
    // already exists, SQLite deletes it and inserts the new one. This gives us a new
    // rowid, which we use for the vec_embeddings mirror.
    conn.execute(
        "INSERT OR REPLACE INTO embeddings (source_type, source_path, source_id, model, last_commit, staleness, chunk_text, embedding)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
        params![
            source_type,
            source_path,
            source_id,
            model,
            last_commit,
            staleness as f64,
            chunk_text,
            embedding_bytes.to_vec()
        ],
    )
    .await?;

    // Mirror into vec_embeddings for ANN queries (best-effort).
    // INSERT OR REPLACE handles the case where the old rowid was already present.
    let new_id: i64 = {
        let mut rows = conn.query("SELECT last_insert_rowid()", ()).await?;
        if let Some(row) = rows.next().await? {
            row.get(0)?
        } else {
            return Ok(());
        }
    };

    if let Some(vc) = vec_conn {
        if let Ok(stmt) =
            vc.prepare("INSERT OR REPLACE INTO vec_embeddings(rowid, embedding) VALUES (?1, ?2)")
        {
            stmt.bind_int64(1, new_id);
            stmt.bind_blob(2, embedding_bytes);
            let _ = stmt.step();
        }
    } else {
        let _ = conn
            .execute(
                "INSERT OR REPLACE INTO vec_embeddings(rowid, embedding) VALUES (?1, ?2)",
                params![new_id, embedding_bytes.to_vec()],
            )
            .await;
    }

    Ok(())
}

/// ANN search using the `vec_embeddings` virtual table.
///
/// Returns up to `k` candidate rows from `embeddings` ordered by vector
/// distance (closest first), ready for staleness re-ranking.
///
/// When a `VecConnection` is provided, uses it for the vec query (required for
/// sqlite-vec support).  Returns `None` if `vec_embeddings` is not available.
/// The caller should fall back to [`load_all_embeddings`] +
/// [`crate::search::rerank`] in that case.
pub async fn ann_search(
    conn: &Connection,
    model: &str,
    query_bytes: &[u8],
    k: usize,
    vec_conn: Option<&VecConnection>,
) -> Option<Vec<StoredEmbedding>> {
    // First verify the virtual table exists.
    if !vec_table_available(conn, vec_conn).await {
        return None;
    }

    // sqlite-vec ANN query: returns rowid + distance for the k nearest vectors.
    // We JOIN back to `embeddings` to get all metadata in one round-trip.
    //
    // Note: `v.k` is a hidden column consumed by the vec0 module as the
    // result-limit parameter; `v.embedding MATCH ?1` supplies the query vector.
    let sql = "
        SELECT e.id, e.source_type, e.source_path, e.source_id,
               e.staleness, e.chunk_text, e.last_commit, e.embedding
        FROM vec_embeddings v
        JOIN embeddings e ON e.id = v.rowid
        WHERE v.embedding MATCH ?1
          AND v.k = ?2
          AND e.model = ?3
        ORDER BY v.distance
    ";

    if let Some(vc) = vec_conn {
        // Use the VecConnection for the ANN query (it has sqlite-vec registered).
        let stmt = vc.prepare(sql).ok()?;
        stmt.bind_blob(1, query_bytes);
        stmt.bind_int64(2, k as i64);
        stmt.bind_text(3, model);

        let mut result = Vec::new();
        while stmt.step().ok()? {
            let id = stmt.column_int64(0);
            let source_type = stmt.column_text(1).unwrap_or_default();
            let source_path = stmt.column_text(2).unwrap_or_default();
            let source_id_val = stmt.column_int64(3);
            let source_id = if source_id_val != 0 {
                Some(source_id_val)
            } else {
                None
            };
            let staleness = stmt.column_double(4) as f32;
            let chunk_text = stmt.column_text(5).unwrap_or_default();
            let last_commit = stmt.column_text(6);
            let blob = stmt.column_blob(7);
            let vector = crate::search::parse_blob(blob);

            result.push(StoredEmbedding {
                id,
                source_type,
                source_path,
                source_id,
                staleness,
                chunk_text,
                last_commit,
                vector,
            });
        }

        Some(result)
    } else {
        // Fallback: try through libsql connection (may not have vec loaded).
        let mut rows = conn
            .query(sql, params![query_bytes.to_vec(), k as i64, model])
            .await
            .ok()?;

        let mut result = Vec::new();
        while let Some(row) = rows.next().await.ok()? {
            let id: i64 = row.get(0).ok()?;
            let source_type: String = row.get(1).ok()?;
            let source_path: String = row.get(2).ok()?;
            let source_id: Option<i64> = row.get(3).ok()?;
            let staleness: f64 = row.get(4).ok()?;
            let chunk_text: String = row.get(5).ok()?;
            let last_commit: Option<String> = row.get(6).ok()?;
            let blob: Vec<u8> = row.get(7).ok()?;

            let vector = crate::search::parse_blob(blob);

            result.push(StoredEmbedding {
                id,
                source_type,
                source_path,
                source_id,
                staleness: staleness as f32,
                chunk_text,
                last_commit,
                vector,
            });
        }

        Some(result)
    }
}

/// Load all stored embeddings for a given model name.
///
/// Returns all rows so the caller can do brute-force cosine search in memory.
/// Prefer [`ann_search`] when the sqlite-vec virtual table is available.
pub async fn load_all_embeddings(
    conn: &Connection,
    model: &str,
) -> Result<Vec<StoredEmbedding>, libsql::Error> {
    let mut rows = conn
        .query(
            "SELECT id, source_type, source_path, source_id, staleness, chunk_text, last_commit, embedding
             FROM embeddings WHERE model = ?1",
            params![model],
        )
        .await?;

    let mut result = Vec::new();
    while let Some(row) = rows.next().await? {
        let id: i64 = row.get(0)?;
        let source_type: String = row.get(1)?;
        let source_path: String = row.get(2)?;
        let source_id: Option<i64> = row.get(3)?;
        let staleness: f64 = row.get(4)?;
        let chunk_text: String = row.get(5)?;
        let last_commit: Option<String> = row.get(6)?;
        let blob: Vec<u8> = row.get(7)?;

        let vector = crate::search::parse_blob(blob);

        result.push(StoredEmbedding {
            id,
            source_type,
            source_path,
            source_id,
            staleness: staleness as f32,
            chunk_text,
            last_commit,
            vector,
        });
    }

    Ok(result)
}

/// Load stored embeddings filtered by a specific source type.
///
/// Like [`load_all_embeddings`] but scoped to one `source_type` (e.g. `"context"`).
/// Used for the brute-force fallback path when ANN search is not available and
/// the caller wants to restrict results to a single source type.
pub async fn load_embeddings_for_type(
    conn: &Connection,
    model: &str,
    source_type: &str,
) -> Result<Vec<StoredEmbedding>, libsql::Error> {
    let mut rows = conn
        .query(
            "SELECT id, source_type, source_path, source_id, staleness, chunk_text, last_commit, embedding
             FROM embeddings WHERE model = ?1 AND source_type = ?2",
            params![model, source_type],
        )
        .await?;

    let mut result = Vec::new();
    while let Some(row) = rows.next().await? {
        let id: i64 = row.get(0)?;
        let source_type_val: String = row.get(1)?;
        let source_path: String = row.get(2)?;
        let source_id: Option<i64> = row.get(3)?;
        let staleness: f64 = row.get(4)?;
        let chunk_text: String = row.get(5)?;
        let last_commit: Option<String> = row.get(6)?;
        let blob: Vec<u8> = row.get(7)?;

        let vector = crate::search::parse_blob(blob);

        result.push(StoredEmbedding {
            id,
            source_type: source_type_val,
            source_path,
            source_id,
            staleness: staleness as f32,
            chunk_text,
            last_commit,
            vector,
        });
    }

    Ok(result)
}

/// Count embeddings stored for a given model.
pub async fn count_embeddings(conn: &Connection, model: &str) -> Result<i64, libsql::Error> {
    let mut rows = conn
        .query(
            "SELECT COUNT(*) FROM embeddings WHERE model = ?1",
            params![model],
        )
        .await?;
    if let Some(row) = rows.next().await? {
        Ok(row.get(0)?)
    } else {
        Ok(0)
    }
}

/// Delete embeddings for a specific (source_type, source_path) pair, all models.
/// Used during incremental rebuild when a file changes.
///
/// Also removes the corresponding rows from `vec_embeddings` (best-effort)
/// when a `VecConnection` is provided.
pub async fn delete_embeddings_for_path(
    conn: &Connection,
    source_path: &str,
    vec_conn: Option<&VecConnection>,
) -> Result<u64, libsql::Error> {
    // Collect IDs before deletion so we can clean up vec_embeddings.
    let mut rows = conn
        .query(
            "SELECT id FROM embeddings WHERE source_path = ?1",
            params![source_path],
        )
        .await?;
    let mut ids: Vec<i64> = Vec::new();
    while let Some(row) = rows.next().await? {
        ids.push(row.get(0)?);
    }

    let affected = conn
        .execute(
            "DELETE FROM embeddings WHERE source_path = ?1",
            params![source_path],
        )
        .await?;

    for id in ids {
        if let Some(vc) = vec_conn {
            if let Ok(stmt) = vc.prepare("DELETE FROM vec_embeddings WHERE rowid = ?1") {
                stmt.bind_int64(1, id);
                let _ = stmt.step();
            }
        } else {
            let _ = conn
                .execute("DELETE FROM vec_embeddings WHERE rowid = ?1", params![id])
                .await;
        }
    }

    Ok(affected)
}

/// Drop embedding tables entirely for a full rebuild.
///
/// This is much faster than `DELETE FROM` for large tables — it avoids
/// generating tombstone pages that bloat the database file.  The caller
/// must call [`ensure_schema`] + [`ensure_vec_schema`] afterwards to
/// recreate the tables.
pub async fn drop_embedding_tables(
    conn: &Connection,
    vec_conn: Option<&VecConnection>,
) -> Result<(), libsql::Error> {
    if let Some(vc) = vec_conn {
        let _ = vc.execute(DROP_VEC_EMBEDDINGS_TABLE);
    } else {
        let _ = conn.execute(DROP_VEC_EMBEDDINGS_TABLE, ()).await;
    }
    conn.execute(DROP_EMBEDDINGS_IDX_SOURCE, ()).await?;
    conn.execute(DROP_EMBEDDINGS_IDX_MODEL, ()).await?;
    conn.execute(DROP_EMBEDDINGS_TABLE, ()).await?;
    Ok(())
}

/// Run `VACUUM` to reclaim space after a full rebuild.
pub async fn vacuum(conn: &Connection) {
    let _ = conn.execute("VACUUM", ()).await;
}

/// Return the set of file paths that have at least one embedding for the given model.
pub async fn embedded_paths(
    conn: &Connection,
    model: &str,
) -> Result<std::collections::HashSet<String>, libsql::Error> {
    let mut rows = conn
        .query(
            "SELECT DISTINCT source_path FROM embeddings WHERE model = ?1",
            params![model],
        )
        .await?;
    let mut set = std::collections::HashSet::new();
    while let Some(row) = rows.next().await? {
        set.insert(row.get::<String>(0)?);
    }
    Ok(set)
}