tessera-codegraph 0.8.0

A local, deterministic semantic code graph and MCP server for AI coding agents. 11 languages, personalized PageRank impact, hallucination validator, call-path tracing, graph export, incremental indexing.
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
441
442
443
444
445
446
447
448
449
450
451
452
use std::fs;
use std::path::Path;

use anyhow::{bail, Result};
use rusqlite::{params, Connection, OptionalExtension};

use crate::types::{
    IndexedImport, IndexedReference, IndexedSymbol, Language, ReferenceRecord, SymbolRecord,
};

pub const SCHEMA_VERSION: i64 = 3;

pub fn open(path: &Path) -> Result<Connection> {
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            fs::create_dir_all(parent)?;
        }
    }
    let conn = Connection::open(path)?;
    conn.pragma_update(None, "journal_mode", "WAL")?;
    conn.pragma_update(None, "foreign_keys", "ON")?;
    conn.pragma_update(None, "synchronous", "NORMAL")?;
    migrate(&conn)?;
    Ok(conn)
}

pub fn open_existing(path: &Path) -> Result<Connection> {
    if !path.exists() {
        bail!(
            "Tessera database not found at {}. Run `tessera index . --db {}` first.",
            path.display(),
            path.display()
        );
    }
    open(path)
}

pub fn reset(conn: &Connection) -> Result<()> {
    // The FTS5 virtual table is configured with external content (content='symbols')
    // and is kept in sync via triggers. Deleting `symbols` here causes the
    // AFTER DELETE trigger to fire for every row, which is correct but slow on
    // large tables; for v0.2's repo sizes the cost is irrelevant.
    conn.execute_batch(
        "
        DELETE FROM imports;
        DELETE FROM edges;
        DELETE FROM refs;
        DELETE FROM symbols;
        DELETE FROM files;
        ",
    )?;
    // Ask FTS5 to drop any orphaned tombstones.
    let _ = conn.execute_batch("INSERT INTO symbols_fts(symbols_fts) VALUES('delete-all');");
    Ok(())
}

fn migrate(conn: &Connection) -> Result<()> {
    conn.execute_batch(
        "
        CREATE TABLE IF NOT EXISTS files (
            id INTEGER PRIMARY KEY,
            path TEXT NOT NULL UNIQUE,
            language TEXT NOT NULL,
            sha256 TEXT NOT NULL,
            loc INTEGER NOT NULL,
            indexed_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
        );

        CREATE TABLE IF NOT EXISTS meta (
            key TEXT PRIMARY KEY,
            value TEXT NOT NULL
        );

        CREATE TABLE IF NOT EXISTS meta_blob (
            key TEXT PRIMARY KEY,
            value BLOB NOT NULL
        );

        CREATE TABLE IF NOT EXISTS symbols (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            qualified_name TEXT NOT NULL,
            kind TEXT NOT NULL,
            file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,
            start_line INTEGER NOT NULL,
            end_line INTEGER NOT NULL,
            signature TEXT NOT NULL,
            exported INTEGER NOT NULL DEFAULT 0
        );

        CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name);
        CREATE INDEX IF NOT EXISTS idx_symbols_qualified ON symbols(qualified_name);
        CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(file_id);

        CREATE TABLE IF NOT EXISTS refs (
            id INTEGER PRIMARY KEY,
            symbol_name TEXT NOT NULL,
            from_symbol_id INTEGER REFERENCES symbols(id) ON DELETE SET NULL,
            file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,
            line INTEGER NOT NULL,
            column INTEGER NOT NULL,
            context TEXT NOT NULL,
            kind TEXT NOT NULL
        );

        CREATE INDEX IF NOT EXISTS idx_refs_symbol ON refs(symbol_name);
        CREATE INDEX IF NOT EXISTS idx_refs_from ON refs(from_symbol_id);
        CREATE INDEX IF NOT EXISTS idx_refs_file ON refs(file_id);

        CREATE TABLE IF NOT EXISTS edges (
            id INTEGER PRIMARY KEY,
            from_symbol_id INTEGER NOT NULL REFERENCES symbols(id) ON DELETE CASCADE,
            to_symbol_name TEXT NOT NULL,
            kind TEXT NOT NULL,
            weight REAL NOT NULL DEFAULT 1.0
        );

        CREATE INDEX IF NOT EXISTS idx_edges_to ON edges(to_symbol_name);
        CREATE INDEX IF NOT EXISTS idx_edges_from ON edges(from_symbol_id);

        CREATE TABLE IF NOT EXISTS imports (
            id INTEGER PRIMARY KEY,
            file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,
            source TEXT NOT NULL,
            line INTEGER NOT NULL,
            kind TEXT NOT NULL
        );

        CREATE INDEX IF NOT EXISTS idx_imports_file ON imports(file_id);
        CREATE INDEX IF NOT EXISTS idx_imports_source ON imports(source);
        ",
    )?;

    // FTS5 trigram virtual table. rusqlite's bundled SQLite ships FTS5 and the
    // trigram tokenizer. The triggers below keep it in sync with `symbols`.
    let fts_setup = "
        CREATE VIRTUAL TABLE IF NOT EXISTS symbols_fts USING fts5(
            name, qualified_name,
            content='symbols', content_rowid='id',
            tokenize='trigram'
        );

        CREATE TRIGGER IF NOT EXISTS symbols_ai AFTER INSERT ON symbols BEGIN
            INSERT INTO symbols_fts(rowid, name, qualified_name)
            VALUES (new.id, new.name, new.qualified_name);
        END;

        CREATE TRIGGER IF NOT EXISTS symbols_ad AFTER DELETE ON symbols BEGIN
            INSERT INTO symbols_fts(symbols_fts, rowid, name, qualified_name)
            VALUES('delete', old.id, old.name, old.qualified_name);
        END;
        ";
    // FTS5 may not be available in unusual builds; if it fails, fall back silently.
    let _ = conn.execute_batch(fts_setup);

    set_meta(conn, "schema_version", &SCHEMA_VERSION.to_string())?;
    Ok(())
}

pub fn set_meta(conn: &Connection, key: &str, value: &str) -> Result<()> {
    conn.execute(
        "
        INSERT INTO meta(key, value) VALUES (?1, ?2)
        ON CONFLICT(key) DO UPDATE SET value = excluded.value
        ",
        params![key, value],
    )?;
    Ok(())
}

pub fn get_meta(conn: &Connection, key: &str) -> Result<Option<String>> {
    conn.query_row(
        "SELECT value FROM meta WHERE key = ?1",
        params![key],
        |row| row.get(0),
    )
    .optional()
    .map_err(Into::into)
}

pub fn set_meta_blob(conn: &Connection, key: &str, value: &[u8]) -> Result<()> {
    conn.execute(
        "
        INSERT INTO meta_blob(key, value) VALUES (?1, ?2)
        ON CONFLICT(key) DO UPDATE SET value = excluded.value
        ",
        params![key, value],
    )?;
    Ok(())
}

pub fn get_meta_blob(conn: &Connection, key: &str) -> Result<Option<Vec<u8>>> {
    conn.query_row(
        "SELECT value FROM meta_blob WHERE key = ?1",
        params![key],
        |row| row.get::<_, Vec<u8>>(0),
    )
    .optional()
    .map_err(Into::into)
}

pub fn file_sha(conn: &Connection, path: &str) -> Result<Option<(i64, String)>> {
    conn.query_row(
        "SELECT id, sha256 FROM files WHERE path = ?1",
        params![path],
        |row| Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?)),
    )
    .optional()
    .map_err(Into::into)
}

pub fn delete_file_cascade(conn: &Connection, file_id: i64) -> Result<()> {
    conn.execute("DELETE FROM files WHERE id = ?1", params![file_id])?;
    Ok(())
}

pub fn delete_files_not_in(conn: &Connection, retained_ids: &[i64]) -> Result<usize> {
    if retained_ids.is_empty() {
        let removed = conn.execute("DELETE FROM files", [])?;
        return Ok(removed);
    }
    let placeholders = retained_ids
        .iter()
        .map(|_| "?")
        .collect::<Vec<_>>()
        .join(",");
    let sql = format!("DELETE FROM files WHERE id NOT IN ({placeholders})");
    let params_iter: Vec<&dyn rusqlite::ToSql> = retained_ids
        .iter()
        .map(|id| id as &dyn rusqlite::ToSql)
        .collect();
    let removed = conn.execute(&sql, params_iter.as_slice())?;
    Ok(removed)
}

pub fn insert_file(
    conn: &Connection,
    path: &str,
    language: Language,
    sha256: &str,
    loc: usize,
) -> Result<i64> {
    conn.execute(
        "INSERT INTO files(path, language, sha256, loc) VALUES (?1, ?2, ?3, ?4)",
        params![path, language.to_string(), sha256, loc as i64],
    )?;
    Ok(conn.last_insert_rowid())
}

pub fn insert_symbols(
    conn: &Connection,
    file_id: i64,
    symbols: &[IndexedSymbol],
) -> Result<Vec<i64>> {
    let mut ids = Vec::with_capacity(symbols.len());
    for symbol in symbols {
        conn.execute(
            "
            INSERT INTO symbols(
                name, qualified_name, kind, file_id, start_line, end_line, signature, exported
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
            ",
            params![
                symbol.name,
                symbol.qualified_name,
                symbol.kind,
                file_id,
                symbol.start_line as i64,
                symbol.end_line as i64,
                symbol.signature,
                i64::from(symbol.exported)
            ],
        )?;
        ids.push(conn.last_insert_rowid());
    }
    Ok(ids)
}

pub fn find_symbol_id(
    conn: &Connection,
    file_id: i64,
    qualified_name: &str,
) -> Result<Option<i64>> {
    conn.query_row(
        "SELECT id FROM symbols WHERE file_id = ?1 AND qualified_name = ?2",
        params![file_id, qualified_name],
        |row| row.get(0),
    )
    .optional()
    .map_err(Into::into)
}

pub fn insert_imports(conn: &Connection, file_id: i64, imports: &[IndexedImport]) -> Result<usize> {
    for imp in imports {
        conn.execute(
            "INSERT INTO imports(file_id, source, line, kind) VALUES (?1, ?2, ?3, ?4)",
            params![file_id, imp.source, imp.line as i64, imp.kind],
        )?;
    }
    Ok(imports.len())
}

pub fn insert_references(
    conn: &Connection,
    file_id: i64,
    refs: &[IndexedReference],
) -> Result<usize> {
    let mut count = 0;
    for reference in refs {
        let from_symbol_id = reference
            .from_qualified_name
            .as_deref()
            .map(|name| find_symbol_id(conn, file_id, name))
            .transpose()?
            .flatten();
        conn.execute(
            "
            INSERT INTO refs(
                symbol_name, from_symbol_id, file_id, line, column, context, kind
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
            ",
            params![
                reference.symbol_name,
                from_symbol_id,
                file_id,
                reference.line as i64,
                reference.column as i64,
                reference.context,
                reference.kind
            ],
        )?;
        if let Some(from_symbol_id) = from_symbol_id {
            conn.execute(
                "
                INSERT INTO edges(from_symbol_id, to_symbol_name, kind, weight)
                VALUES (?1, ?2, ?3, 1.0)
                ",
                params![from_symbol_id, reference.symbol_name, reference.kind],
            )?;
        }
        count += 1;
    }
    Ok(count)
}

pub fn resolve_symbol(conn: &Connection, symbol: &str) -> Result<Option<SymbolRecord>> {
    conn.query_row(
        "
        SELECT s.id, s.name, s.qualified_name, s.kind, s.file_id, f.path, f.language,
               s.start_line, s.end_line, s.signature, s.exported
        FROM symbols s
        JOIN files f ON f.id = s.file_id
        WHERE s.qualified_name = ?1 OR s.name = ?1 OR s.qualified_name LIKE ?2
        ORDER BY
            CASE
                WHEN s.qualified_name = ?1 THEN 0
                WHEN s.name = ?1 THEN 1
                ELSE 2
            END,
            length(s.qualified_name)
        LIMIT 1
        ",
        params![symbol, format!("%.{}", symbol)],
        map_symbol,
    )
    .optional()
    .map_err(Into::into)
}

pub fn symbol_fanout(conn: &Connection, symbol_id: i64) -> Result<usize> {
    conn.query_row(
        "SELECT COUNT(DISTINCT to_symbol_name) FROM edges WHERE from_symbol_id = ?1",
        params![symbol_id],
        |row| row.get::<_, i64>(0),
    )
    .map(|count| count as usize)
    .map_err(Into::into)
}

pub fn symbol_callers_count(conn: &Connection, symbol_name: &str) -> Result<usize> {
    conn.query_row(
        "SELECT COUNT(DISTINCT from_symbol_id) FROM edges WHERE to_symbol_name = ?1",
        params![symbol_name],
        |row| row.get::<_, i64>(0),
    )
    .map(|count| count as usize)
    .map_err(Into::into)
}

pub fn count_imports(conn: &Connection) -> Result<usize> {
    conn.query_row("SELECT COUNT(*) FROM imports", [], |row| {
        row.get::<_, i64>(0)
    })
    .map(|c| c as usize)
    .map_err(Into::into)
}

pub fn count_files(conn: &Connection) -> Result<usize> {
    conn.query_row("SELECT COUNT(*) FROM files", [], |row| row.get::<_, i64>(0))
        .map(|c| c as usize)
        .map_err(Into::into)
}

pub fn count_symbols(conn: &Connection) -> Result<usize> {
    conn.query_row("SELECT COUNT(*) FROM symbols", [], |row| {
        row.get::<_, i64>(0)
    })
    .map(|c| c as usize)
    .map_err(Into::into)
}

pub fn count_refs(conn: &Connection) -> Result<usize> {
    conn.query_row("SELECT COUNT(*) FROM refs", [], |row| row.get::<_, i64>(0))
        .map(|c| c as usize)
        .map_err(Into::into)
}

pub fn count_edges(conn: &Connection) -> Result<usize> {
    conn.query_row("SELECT COUNT(*) FROM edges", [], |row| row.get::<_, i64>(0))
        .map(|c| c as usize)
        .map_err(Into::into)
}

pub fn map_symbol(row: &rusqlite::Row<'_>) -> rusqlite::Result<SymbolRecord> {
    Ok(SymbolRecord {
        id: row.get(0)?,
        name: row.get(1)?,
        qualified_name: row.get(2)?,
        kind: row.get(3)?,
        file_id: row.get(4)?,
        path: row.get(5)?,
        language: row.get(6)?,
        start_line: row.get::<_, i64>(7)? as usize,
        end_line: row.get::<_, i64>(8)? as usize,
        signature: row.get(9)?,
        exported: row.get::<_, i64>(10)? != 0,
    })
}

pub fn map_reference(row: &rusqlite::Row<'_>) -> rusqlite::Result<ReferenceRecord> {
    Ok(ReferenceRecord {
        id: row.get(0)?,
        symbol_name: row.get(1)?,
        from_symbol_id: row.get(2)?,
        from_symbol: row.get(3)?,
        path: row.get(4)?,
        line: row.get::<_, i64>(5)? as usize,
        column: row.get::<_, i64>(6)? as usize,
        context: row.get(7)?,
        kind: row.get(8)?,
    })
}