relay-knowledge 1.1.17

Graph-database-based knowledge graph project.
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
453
454
455
456
457
458
459
460
use super::{
    CODE_WORKSPACE_PACKAGE_MAPPING_UNIQUE, GRAPH_BM25_GLOBAL_LABEL_LOOKUP_INDEX,
    GRAPH_BM25_GLOBAL_LABEL_LOOKUP_INDEX_COLUMNS, GRAPH_BM25_GLOBAL_LABEL_STATE_INDEX,
    GRAPH_BM25_GLOBAL_LABEL_STATE_INDEX_COLUMNS, GRAPH_BM25_ROUTE_DOCUMENT_COLUMNS,
    GRAPH_GLOBAL_VERSION_INDEX_COLUMNS, GRAPH_SEMANTIC_GLOBAL_INDEX,
    bm25_route_table_is_compatible, index_has_columns, prepare_existing_database_once,
    schema_compatibility_error_is_retryable, schema_compatibility_error_message_is_retryable,
    table_has_exact_columns, table_has_unique_columns,
};
use crate::storage::StorageError;
use rusqlite::Connection;

#[test]
fn migration_preserves_nonempty_incompatible_reference_search_progress_fail_closed() {
    let store = crate::storage::SqliteGraphStore::open_in_memory().expect("store should open");
    let connection = store.connection.lock().expect("connection should lock");
    connection
        .execute_batch(
            "DROP TABLE code_repository_reference_search_progress;
             CREATE TABLE code_repository_reference_search_progress (
                 source_scope TEXT PRIMARY KEY,
                 stage TEXT NOT NULL
             );
             INSERT INTO code_repository_reference_search_progress (source_scope, stage)
             VALUES ('scope', 'cleanup');",
        )
        .expect("incompatible durable progress should seed");

    let error = prepare_existing_database_once(&connection)
        .expect_err("nonempty incompatible durable progress must not be discarded");
    assert!(matches!(error, StorageError::Invariant(_)));
    assert_eq!(
        connection
            .query_row(
                "SELECT COUNT(*) FROM code_repository_reference_search_progress",
                [],
                |row| row.get::<_, usize>(0),
            )
            .expect("durable progress should remain"),
        1
    );
}

#[test]
fn schema_compatibility_retry_is_limited_to_transient_open_errors() {
    assert!(schema_compatibility_error_message_is_retryable(
        "vtable constructor failed: graph_bm25"
    ));
    assert!(schema_compatibility_error_message_is_retryable(
        "database schema is locked"
    ));
    assert!(!schema_compatibility_error_message_is_retryable(
        "no such table: graph_bm25"
    ));
    assert!(!schema_compatibility_error_is_retryable(
        &StorageError::InvalidInput("database is locked".to_owned())
    ));
}

#[test]
fn migration_rebuilds_legacy_workspace_package_mapping_uniqueness() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "
                CREATE TABLE code_workspace_package_mappings (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    set_id TEXT NOT NULL,
                    package_name TEXT NOT NULL,
                    ecosystem TEXT NOT NULL,
                    repository_id TEXT NOT NULL,
                    source_scope TEXT NOT NULL,
                    workspace_format TEXT NOT NULL,
                    created_at_ms INTEGER NOT NULL,
                    UNIQUE (set_id, package_name)
                );
                INSERT INTO code_workspace_package_mappings (
                    set_id, package_name, ecosystem, repository_id, source_scope,
                    workspace_format, created_at_ms
                )
                VALUES ('set-1', 'core', 'npm', 'repo', 'scope', 'pnpm', 1);
                ",
        )
        .expect("legacy mapping table should create");

    prepare_existing_database_once(&connection).expect("migration should run");

    assert!(
        table_has_unique_columns(
            &connection,
            "code_workspace_package_mappings",
            CODE_WORKSPACE_PACKAGE_MAPPING_UNIQUE
        )
        .expect("unique key should inspect")
    );
    let row_count: u32 = connection
        .query_row(
            "SELECT COUNT(*) FROM code_workspace_package_mappings",
            [],
            |row| row.get(0),
        )
        .expect("row count should load");
    assert_eq!(row_count, 1);
}

#[test]
fn schema_version_change_invalidates_compatible_bm25_route_state() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "
            CREATE TABLE relay_storage_schema_state (
                key TEXT PRIMARY KEY,
                version INTEGER NOT NULL,
                updated_at_ms INTEGER NOT NULL
            );
            INSERT INTO relay_storage_schema_state VALUES ('sqlite_graph_store', 3, 0);
            CREATE TABLE graph_bm25_route_state (
                id INTEGER PRIMARY KEY,
                indexed_graph_version INTEGER NOT NULL,
                document_count INTEGER NOT NULL,
                state TEXT NOT NULL,
                algorithm_version TEXT NOT NULL,
                semantic_generation TEXT NOT NULL,
                vector_generation TEXT NOT NULL,
                rebuild_phase TEXT,
                rebuild_cursor TEXT,
                rebuild_semantic INTEGER,
                rebuild_vector INTEGER,
                rebuild_owner TEXT,
                rebuild_lease_expires_at_ms INTEGER
            );
            INSERT INTO graph_bm25_route_state
            VALUES (
                1, 7, 10, 'fresh', 'compatible', 'generation', 'generation',
                NULL, NULL, NULL, NULL, NULL, NULL
            );
            CREATE TABLE graph_bm25_route_documents (
                document_id TEXT PRIMARY KEY,
                fts_rowid INTEGER NOT NULL UNIQUE,
                document_kind TEXT NOT NULL,
                created_graph_version INTEGER NOT NULL,
                source_scope TEXT NOT NULL,
                source_path TEXT,
                label_gram_state TEXT NOT NULL,
                group_token TEXT NOT NULL,
                term_counts_json TEXT NOT NULL
            );
            CREATE TABLE graph_bm25_route_groups (
                source_scope TEXT NOT NULL,
                group_token TEXT NOT NULL,
                document_count INTEGER NOT NULL,
                PRIMARY KEY (source_scope, group_token)
            );
            CREATE TABLE graph_bm25_route_terms (
                term TEXT NOT NULL,
                source_scope TEXT NOT NULL,
                group_token TEXT NOT NULL,
                collection_frequency INTEGER NOT NULL,
                PRIMARY KEY (term, source_scope, group_token)
            );
            CREATE TABLE graph_bm25_route_term_totals (
                term TEXT PRIMARY KEY,
                document_frequency INTEGER NOT NULL
            );
            ",
        )
        .expect("previous marker and compatible route state should create");

    prepare_existing_database_once(&connection).expect("migration should invalidate routing");

    let state = connection
        .query_row(
            "SELECT state FROM graph_bm25_route_state WHERE id = 1",
            [],
            |row| row.get::<_, String>(0),
        )
        .expect("route state should remain readable");
    assert_eq!(state, "stale");
}

#[test]
fn bm25_hierarchy_suite_schema_migration_replaces_misordered_global_fallback_indexes() {
    let store = crate::storage::SqliteGraphStore::open_in_memory()
        .expect("complete storage schema should initialize");
    let connection = store.connection.lock().expect("connection should lock");
    super::super::marker::mark_schema_initialization_current(&connection)
        .expect("current schema marker should initialize");
    connection
        .execute_batch(
            "DROP INDEX graph_semantic_documents_version;
             CREATE INDEX graph_semantic_documents_version
             ON graph_semantic_documents(document_id, created_graph_version);
             DROP INDEX graph_bm25_route_documents_global_label_state;
             CREATE INDEX graph_bm25_route_documents_global_label_state
             ON graph_bm25_route_documents(
                 label_gram_state, source_scope, created_graph_version, document_id
             );
             DROP INDEX graph_bm25_label_grams_global_label_lookup;
             CREATE INDEX graph_bm25_label_grams_global_label_lookup
             ON graph_bm25_label_grams(
                 label_lower, source_scope, created_graph_version, document_id
             );",
        )
        .expect("misordered fallback indexes should replace current indexes");

    prepare_existing_database_once(&connection).expect("migration should drop stale indexes");
    crate::storage::sqlite::retrieval::initialize_schema(&connection)
        .expect("retrieval schema should recreate current indexes");

    for (index, columns) in [
        (
            GRAPH_SEMANTIC_GLOBAL_INDEX,
            GRAPH_GLOBAL_VERSION_INDEX_COLUMNS,
        ),
        (
            GRAPH_BM25_GLOBAL_LABEL_STATE_INDEX,
            GRAPH_BM25_GLOBAL_LABEL_STATE_INDEX_COLUMNS,
        ),
        (
            GRAPH_BM25_GLOBAL_LABEL_LOOKUP_INDEX,
            GRAPH_BM25_GLOBAL_LABEL_LOOKUP_INDEX_COLUMNS,
        ),
    ] {
        assert!(
            index_has_columns(&connection, index, columns)
                .expect("recreated global fallback index should inspect"),
            "migration did not recreate exact index {index}"
        );
    }
}

#[test]
fn bm25_hierarchy_suite_schema_migration_preserves_an_active_rebuild_lease() {
    let store = crate::storage::SqliteGraphStore::open_in_memory()
        .expect("complete storage schema should initialize");
    let connection = store.connection.lock().expect("connection should lock");
    create_canonical_bm25_shadow(&connection);
    mark_active_bm25_rebuild(&connection);

    prepare_existing_database_once(&connection).expect("migration should serialize safely");

    let state = connection
        .query_row(
            "SELECT state, rebuild_owner FROM graph_bm25_route_state WHERE id = 1",
            [],
            |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
        )
        .expect("active lease should remain readable");
    assert_eq!(state, ("building".to_owned(), "active-owner".to_owned()));
}

#[test]
fn bm25_hierarchy_suite_schema_migration_stales_unresumable_active_rebuilds() {
    for missing_dependency in ["shadow", "code", "companion"] {
        let store = crate::storage::SqliteGraphStore::open_in_memory()
            .expect("complete storage schema should initialize");
        let connection = store.connection.lock().expect("connection should lock");
        if missing_dependency != "shadow" {
            create_canonical_bm25_shadow(&connection);
        }
        match missing_dependency {
            "shadow" => {}
            "code" => {
                connection
                    .execute("DROP TABLE code_files", [])
                    .expect("code dependency should drop");
            }
            "companion" => {
                connection
                    .execute("DROP TABLE graph_semantic_documents", [])
                    .expect("companion dependency should drop");
            }
            _ => unreachable!("fixture dependency is exhaustive"),
        }
        mark_active_bm25_rebuild(&connection);

        prepare_existing_database_once(&connection)
            .expect("unresumable rebuild should migrate safely");

        let state = connection
            .query_row(
                "SELECT state FROM graph_bm25_route_state WHERE id = 1",
                [],
                |row| row.get::<_, String>(0),
            )
            .expect("route state should remain readable");
        assert_eq!(state, "stale", "missing {missing_dependency} was resumable");
    }
}

fn create_canonical_bm25_shadow(connection: &Connection) {
    connection
        .execute_batch(
            "CREATE VIRTUAL TABLE graph_bm25_rebuild USING fts5(
                 document_id UNINDEXED,
                 document_kind UNINDEXED,
                 evidence_id UNINDEXED,
                 parent_evidence_id UNINDEXED,
                 modality UNINDEXED,
                 created_graph_version UNINDEXED,
                 routing_key,
                 source_scope,
                 source_path,
                 entity_labels,
                 entity_aliases,
                 content
             );",
        )
        .expect("canonical BM25 shadow should initialize");
}

fn mark_active_bm25_rebuild(connection: &Connection) {
    super::super::marker::mark_schema_initialization_current(connection)
        .expect("current schema marker should initialize");
    let marker = connection
        .query_row(
            "SELECT version FROM relay_storage_schema_state
             WHERE key = 'sqlite_graph_store'",
            [],
            |row| row.get::<_, i64>(0),
        )
        .expect("schema marker should load");
    assert_eq!(
        marker,
        super::super::marker::SCHEMA_MARKER_VERSION,
        "rebuild fixture must start at the current marker"
    );
    connection
        .execute(
            "UPDATE graph_bm25_route_state
             SET state = 'building', rebuild_owner = 'active-owner',
                 rebuild_lease_expires_at_ms =
                     CAST(strftime('%s', 'now') AS INTEGER) * 1000 + 60000
             WHERE id = 1",
            [],
        )
        .expect("active rebuild lease should initialize");
}

#[test]
fn obsolete_group_document_frequency_is_not_a_compatible_route_term_schema() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "CREATE TABLE graph_bm25_route_terms (
                 term TEXT NOT NULL,
                 source_scope TEXT NOT NULL,
                 group_token TEXT NOT NULL,
                 document_frequency INTEGER NOT NULL,
                 collection_frequency INTEGER NOT NULL
             );",
        )
        .expect("obsolete route term table should create");

    assert!(
        !table_has_exact_columns(
            &connection,
            "graph_bm25_route_terms",
            &[
                "term",
                "source_scope",
                "group_token",
                "collection_frequency"
            ]
        )
        .expect("route term columns should inspect")
    );
}

#[test]
fn route_aggregate_columns_without_the_primary_key_are_not_compatible() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "CREATE TABLE graph_bm25_route_groups (
                 source_scope TEXT NOT NULL,
                 group_token TEXT NOT NULL,
                 document_count INTEGER NOT NULL
             );",
        )
        .expect("constraint-free route group table should create");

    assert!(
        !bm25_route_table_is_compatible(
            &connection,
            "graph_bm25_route_groups",
            &["source_scope", "group_token", "document_count"]
        )
        .expect("route group constraints should inspect")
    );
}

#[test]
fn route_documents_with_extra_dead_columns_are_not_compatible() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "CREATE TABLE graph_bm25_route_documents (
                 document_id TEXT PRIMARY KEY,
                 fts_rowid INTEGER NOT NULL UNIQUE,
                 document_kind TEXT NOT NULL,
                 created_graph_version INTEGER NOT NULL,
                 source_scope TEXT NOT NULL,
                 source_path TEXT,
                 label_gram_state TEXT NOT NULL,
                 group_token TEXT NOT NULL,
                 term_counts_json TEXT NOT NULL,
                 legacy_generation INTEGER NOT NULL
             );",
        )
        .expect("legacy route documents should create");

    assert!(
        !bm25_route_table_is_compatible(
            &connection,
            "graph_bm25_route_documents",
            &[
                "document_id",
                "fts_rowid",
                "document_kind",
                "created_graph_version",
                "source_scope",
                "source_path",
                "label_gram_state",
                "group_token",
                "term_counts_json"
            ]
        )
        .expect("legacy route columns should inspect")
    );
}

#[test]
fn route_documents_without_created_graph_version_are_not_compatible() {
    let connection = Connection::open_in_memory().expect("connection should open");
    connection
        .execute_batch(
            "CREATE TABLE graph_bm25_route_documents (
                 document_id TEXT PRIMARY KEY,
                 fts_rowid INTEGER NOT NULL UNIQUE,
                 document_kind TEXT NOT NULL,
                 source_scope TEXT NOT NULL,
                 source_path TEXT,
                 label_gram_state TEXT NOT NULL,
                 group_token TEXT NOT NULL,
                 term_counts_json TEXT NOT NULL
             );",
        )
        .expect("unversioned route documents should create");

    assert!(
        !bm25_route_table_is_compatible(
            &connection,
            "graph_bm25_route_documents",
            GRAPH_BM25_ROUTE_DOCUMENT_COLUMNS,
        )
        .expect("unversioned route columns should inspect")
    );
}