relay-knowledge 1.1.9

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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use super::*;

fn setup_test_schema(connection: &Connection) {
    connection
        .execute_batch(
            "
        CREATE TABLE IF NOT EXISTS evidence (
            id TEXT PRIMARY KEY,
            source_scope TEXT NOT NULL DEFAULT '',
            source_path TEXT,
            span_start_byte INTEGER,
            span_end_byte INTEGER,
            span_start_line INTEGER,
            span_end_line INTEGER,
            created_graph_version INTEGER NOT NULL DEFAULT 1,
            status TEXT NOT NULL
        );
        CREATE TABLE IF NOT EXISTS entities (
            id TEXT PRIMARY KEY,
            label TEXT NOT NULL
        );
        CREATE TABLE IF NOT EXISTS evidence_entities (
            evidence_id TEXT NOT NULL,
            entity_id TEXT NOT NULL,
            PRIMARY KEY (evidence_id, entity_id)
        );
        CREATE TABLE IF NOT EXISTS graph_fact_evidence (
            fact_kind TEXT NOT NULL,
            fact_id TEXT NOT NULL,
            evidence_id TEXT NOT NULL,
            PRIMARY KEY (fact_kind, fact_id, evidence_id)
        );
        CREATE TABLE IF NOT EXISTS graph_relations (
            id TEXT PRIMARY KEY,
            source_entity_id TEXT NOT NULL,
            relation_type TEXT NOT NULL,
            target_entity_id TEXT NOT NULL,
            evidence_ids_json TEXT NOT NULL,
            confidence_basis_points INTEGER NOT NULL,
            status TEXT NOT NULL,
            valid_from_graph_version INTEGER NOT NULL,
            valid_until_graph_version INTEGER,
            created_graph_version INTEGER NOT NULL
        );
        CREATE TABLE IF NOT EXISTS graph_claims (
            id TEXT PRIMARY KEY,
            subject_entity_id TEXT NOT NULL,
            predicate TEXT NOT NULL,
            object TEXT NOT NULL,
            evidence_ids_json TEXT NOT NULL,
            confidence_basis_points INTEGER NOT NULL,
            status TEXT NOT NULL,
            valid_from_graph_version INTEGER NOT NULL,
            valid_until_graph_version INTEGER,
            created_graph_version INTEGER NOT NULL
        );
        CREATE TABLE IF NOT EXISTS graph_events (
            id TEXT PRIMARY KEY,
            event_type TEXT NOT NULL,
            occurred_at TEXT,
            evidence_ids_json TEXT NOT NULL,
            confidence_basis_points INTEGER NOT NULL,
            status TEXT NOT NULL,
            valid_from_graph_version INTEGER NOT NULL,
            valid_until_graph_version INTEGER,
            created_graph_version INTEGER NOT NULL
        );
        CREATE VIRTUAL TABLE IF NOT EXISTS graph_bm25 USING fts5(
            document_id UNINDEXED,
            document_kind UNINDEXED,
            evidence_id UNINDEXED,
            parent_evidence_id UNINDEXED,
            modality UNINDEXED,
            created_graph_version UNINDEXED,
            source_scope,
            source_path,
            entity_labels,
            entity_aliases,
            content
        );
        ",
        )
        .expect("schema should initialize");
    label_trigrams::initialize_schema(connection).expect("label gram schema should initialize");
}

fn insert_test_symbol(connection: &Connection, id: &str, scope: &str, labels: &str, content: &str) {
    connection
        .execute(
            "
        INSERT INTO graph_bm25 (
            document_id, document_kind, evidence_id, parent_evidence_id,
            modality, created_graph_version, source_scope, source_path,
            entity_labels, entity_aliases, content
        ) VALUES (?1, 'code_symbol', ?2, NULL, 'text_span', 1, ?3, NULL, ?4, '', ?5)
        ",
            params![id, id, scope, labels, content],
        )
        .expect("should insert test symbol");
    index_test_labels(connection, id, "code_symbol", scope, labels);
}

fn insert_test_chunk(
    connection: &Connection,
    id: &str,
    scope: &str,
    path: Option<&str>,
    labels: &str,
    content: &str,
) {
    connection
        .execute(
            "
        INSERT INTO graph_bm25 (
            document_id, document_kind, evidence_id, parent_evidence_id,
            modality, created_graph_version, source_scope, source_path,
            entity_labels, entity_aliases, content
        ) VALUES (?1, 'code_chunk', ?2, NULL, 'text_span', 1, ?3, ?4, ?5, '', ?6)
        ",
            params![id, id, scope, path, labels, content],
        )
        .expect("should insert test chunk");
    index_test_labels(connection, id, "code_chunk", scope, labels);
}

fn insert_test_evidence(
    connection: &Connection,
    id: &str,
    scope: &str,
    labels: &str,
    content: &str,
) {
    connection
        .execute(
            "INSERT INTO evidence (id, status) VALUES (?1, 'accepted')",
            params![id],
        )
        .expect("should insert accepted evidence");
    connection
        .execute(
            "
        INSERT INTO graph_bm25 (
            document_id, document_kind, evidence_id, parent_evidence_id,
            modality, created_graph_version, source_scope, source_path,
            entity_labels, entity_aliases, content
        ) VALUES (?1, 'evidence', ?1, NULL, 'text_span', 1, ?2, NULL, ?3, '', ?4)
        ",
            params![id, scope, labels, content],
        )
        .expect("should insert test evidence");
    index_test_labels(connection, id, "evidence", scope, labels);
}

fn index_test_labels(
    connection: &Connection,
    document_id: &str,
    document_kind: &str,
    source_scope: &str,
    labels: &str,
) {
    let labels = split_labels(labels.to_owned());
    label_trigrams::replace_document(
        connection,
        label_trigrams::LabelGramDocument {
            document_id,
            document_kind,
            source_scope,
            graph_version: 1,
            labels: &labels,
        },
    )
    .expect("label grams should index");
}

fn test_request(query: &str) -> GraphSearchRequest {
    GraphSearchRequest {
        query: query.to_owned(),
        source_scope: None,
        graph_version: crate::domain::GraphVersion::new(1),
        limit: 10,
        disabled_retriever_sources: Vec::new(),
    }
}

#[test]
fn fallback_candidates_returns_empty_for_short_query() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    let result = fallback_candidates(&connection, &test_request("a")).expect("should succeed");
    assert!(result.is_empty());
}

#[test]
fn exact_name_rows_match_by_content() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_symbol(&connection, "doc-1", "docs", "[\"getUser\"]", "getUser");
    let request = GraphSearchRequest {
        query: "getUser".to_owned(),
        source_scope: None,
        graph_version: crate::domain::GraphVersion::new(1),
        limit: 10,
        disabled_retriever_sources: Vec::new(),
    };
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(!result.is_empty());
}

#[test]
fn like_substring_matches_partial_content() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_chunk(
        &connection,
        "doc-2",
        "docs",
        Some("src/sign_in.rs"),
        "[\"signIn\"]",
        "signInWithGoogle requires OAuth2 configuration",
    );
    let request = test_request("signIn");
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(!result.is_empty());
}

#[test]
fn fuzzy_levenshtein_matches_close_names() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_symbol(
        &connection,
        "doc-3",
        "repo",
        "[\"getUser\"]",
        "getUser fn function",
    );
    let request = test_request("getUsr");
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(!result.is_empty());
}

#[test]
fn fuzzy_levenshtein_matches_evidence_labels() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_evidence(
        &connection,
        "ev-fuzzy-label",
        "repo",
        "[\"getUser\"]",
        "profile retrieval behavior",
    );

    let result = fallback_candidates(&connection, &test_request("getUsr")).expect("should succeed");

    assert!(
        result
            .iter()
            .any(|hit| hit.hit.evidence_id == "ev-fuzzy-label"),
        "evidence labels should participate in fuzzy fallback"
    );
}

#[test]
fn fuzzy_levenshtein_matches_name_after_many_nonmatching_labels() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    for index in 0..(FALLBACK_CANDIDATE_LIMIT * 5 + 20) {
        let label = format!("aaaNoiseSymbol{index:04}");
        let labels_json =
            serde_json::to_string(&vec![label.clone()]).expect("labels should encode");
        insert_test_symbol(
            &connection,
            &format!("doc-noise-{index:04}"),
            "repo",
            &labels_json,
            &format!("{label} fn function"),
        );
    }
    insert_test_symbol(
        &connection,
        "doc-fuzzy-tail",
        "repo",
        "[\"zzTailSymbol\"]",
        "zzTailSymbol fn function",
    );

    let request = test_request("zzTailSymbl");
    let result = fallback_candidates(&connection, &request).expect("should succeed");

    assert!(
        result
            .iter()
            .any(|hit| hit.hit.evidence_id == "doc-fuzzy-tail"),
        "fuzzy fallback should rank by edit distance before applying matched-name caps"
    );
}

#[test]
fn fuzzy_levenshtein_orders_closest_match_before_document_id() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_symbol(
        &connection,
        "doc-a-less-close",
        "repo",
        "[\"getUxx\"]",
        "getUxx fn function",
    );
    insert_test_symbol(
        &connection,
        "doc-z-closest",
        "repo",
        "[\"getUser\"]",
        "getUser fn function",
    );

    let result = fallback_candidates(&connection, &test_request("getUsr")).expect("should succeed");

    assert_eq!(result[0].hit.evidence_id, "doc-z-closest");
    assert!(
        result[0].source_score > result[1].source_score,
        "lower edit distance should get a higher fuzzy score"
    );
}

#[test]
fn convert_fallback_candidates_handles_empty_evidence() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_chunk(
        &connection,
        "doc-4",
        "repo",
        None,
        "[\"handler\"]",
        "request handler implementation",
    );
    let request = test_request("handler");
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(!result.is_empty());
}

#[test]
fn adaptive_max_distance_returns_one_for_short_queries() {
    assert_eq!(adaptive_max_distance("ab"), 1);
    assert_eq!(adaptive_max_distance("abc"), 1);
    assert_eq!(adaptive_max_distance("abcd"), 1);
}

#[test]
fn adaptive_max_distance_returns_two_for_long_queries() {
    assert_eq!(adaptive_max_distance("abcde"), 2);
    assert_eq!(adaptive_max_distance("getUser"), 2);
    assert_eq!(adaptive_max_distance("signInWithGoogle"), 2);
}

#[test]
fn levenshtein_distance_computes_correct_edit_distance() {
    assert_eq!(levenshtein_distance("", ""), 0);
    assert_eq!(levenshtein_distance("abc", ""), 3);
    assert_eq!(levenshtein_distance("", "abc"), 3);
    assert_eq!(levenshtein_distance("getUser", "getUsr"), 1);
    assert_eq!(levenshtein_distance("getUser", "getUssr"), 1);
    assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
    assert_eq!(levenshtein_distance("abc", "def"), 3);
    assert_eq!(levenshtein_distance("abc", "abc"), 0);
}

#[test]
fn merge_fallback_candidates_deduplicates_by_document_id() {
    let exact = vec![FallbackCandidate {
        document_id: "doc-1".to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: "ev-1".to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "content".to_owned(),
        match_score: 1.0,
    }];
    let like = vec![FallbackCandidate {
        document_id: "doc-1".to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: "ev-1".to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "content".to_owned(),
        match_score: 0.5,
    }];
    let fuzzy: Vec<FallbackCandidate> = vec![];

    let merged = merge_fallback_candidates(exact, like, fuzzy);
    assert_eq!(merged.len(), 1);
    assert_eq!(merged[0].match_score, 1.0);
}

#[test]
fn merge_fallback_candidates_prioritizes_exact_over_like_and_fuzzy() {
    let exact = vec![FallbackCandidate {
        document_id: "doc-exact".to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: "ev-exact".to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "exact match".to_owned(),
        match_score: 1.0,
    }];
    let like = vec![FallbackCandidate {
        document_id: "doc-like".to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: "ev-like".to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "substring match".to_owned(),
        match_score: 0.5,
    }];
    let fuzzy = vec![FallbackCandidate {
        document_id: "doc-fuzzy".to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: "ev-fuzzy".to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "fuzzy match".to_owned(),
        match_score: 0.25,
    }];

    let merged = merge_fallback_candidates(exact, like, fuzzy);
    assert_eq!(merged.len(), 3);
    assert_eq!(merged[0].match_score, 1.0);
    assert_eq!(merged[1].match_score, 0.5);
    assert_eq!(merged[2].match_score, 0.25);
}

#[test]
fn sort_fallback_candidates_orders_only_materialized_rows() {
    let mut candidates = vec![
        fallback_candidate("doc-c"),
        fallback_candidate("doc-a"),
        fallback_candidate("doc-b"),
    ];

    sort_fallback_candidates(&mut candidates);

    assert_eq!(
        candidates
            .into_iter()
            .map(|candidate| candidate.document_id)
            .collect::<Vec<_>>(),
        ["doc-a", "doc-b", "doc-c"]
    );
}

#[test]
fn sort_fuzzy_candidates_prefers_best_distance_score() {
    let mut candidates = vec![
        fallback_candidate_with_score("doc-a", 0.25),
        fallback_candidate_with_score("doc-z", 0.26),
    ];

    sort_fuzzy_candidates(&mut candidates);

    assert_eq!(candidates[0].document_id, "doc-z");
    assert!(candidates[0].match_score > candidates[1].match_score);
}

#[test]
fn fuzzy_label_candidates_rank_by_overlap_before_candidate_cap() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    for index in 0..(FUZZY_LABEL_CANDIDATE_LIMIT + 20) {
        let label = format!("alpha{}gamma", four_character_noise(index));
        let labels_json =
            serde_json::to_string(&vec![label.clone()]).expect("labels should encode");
        insert_test_symbol(
            &connection,
            &format!("doc-noise-overlap-{index:04}"),
            "repo",
            &labels_json,
            &format!("{label} fn function"),
        );
    }
    insert_test_symbol(
        &connection,
        "doc-ranked-target",
        "repo",
        "[\"alphaBetaGamma\"]",
        "alphaBetaGamma fn function",
    );

    let result =
        fallback_candidates(&connection, &test_request("alphaBetoGamma")).expect("should succeed");

    assert!(
        result
            .iter()
            .any(|hit| hit.hit.evidence_id == "doc-ranked-target"),
        "trigram overlap should keep the closest label before applying the candidate cap"
    );
}

fn four_character_noise(index: usize) -> String {
    const ALPHABET: &[u8] = b"cdefghijklmnopqrstuvwxyz0123456789";
    (0..4)
        .map(|offset| {
            let divisor = ALPHABET.len().pow(offset as u32);
            char::from(ALPHABET[(index / divisor) % ALPHABET.len()])
        })
        .collect()
}

fn fallback_candidate(document_id: &str) -> FallbackCandidate {
    FallbackCandidate {
        document_id: document_id.to_owned(),
        document_kind: "evidence".to_owned(),
        evidence_id: document_id.to_owned(),
        parent_evidence_id: None,
        modality: "text_span".to_owned(),
        source_scope: "docs".to_owned(),
        source_path: None,
        entity_labels: vec![],
        content: "content".to_owned(),
        match_score: 1.0,
    }
}

fn fallback_candidate_with_score(document_id: &str, match_score: f64) -> FallbackCandidate {
    let mut candidate = fallback_candidate(document_id);
    candidate.match_score = match_score;
    candidate
}

#[test]
fn like_substring_query_escapes_special_characters() {
    assert_eq!(contains_like_pattern(r"path\name%_"), r"%path\\name\%\_%");
}

#[test]
fn exact_name_rows_matches_via_multi_label_like() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_symbol(
        &connection,
        "doc-multi",
        "repo",
        "[\"get\",\"getUser\",\"fetch\"]",
        "getUser function returns user data",
    );
    let request = test_request("getUser");
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(
        !result.is_empty(),
        "exact match should find multi-label entity"
    );
}

#[test]
fn exact_name_rows_matches_json_escaped_labels() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    let label = r#"get\user"name"#;
    let labels_json = serde_json::to_string(&vec![label.to_owned()]).expect("labels should encode");
    insert_test_symbol(
        &connection,
        "doc-json-escaped",
        "repo",
        &labels_json,
        "symbol metadata without literal label",
    );
    let request = test_request(label);
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(
        result
            .iter()
            .any(|hit| hit.hit.evidence_id == "doc-json-escaped"),
        "exact match should use JSON-safe LIKE pattern"
    );
}

#[test]
fn exact_name_rows_scope_filter_blocks_wrong_scope() {
    let connection = Connection::open_in_memory().expect("db should open");
    setup_test_schema(&connection);
    insert_test_symbol(
        &connection,
        "doc-scope",
        "repo-a",
        "[\"getUser\"]",
        "getUser function in scope a",
    );
    let mut request = test_request("getUser");
    request.source_scope = Some("repo-b".to_owned());
    let result = fallback_candidates(&connection, &request).expect("should succeed");
    assert!(
        result.is_empty() || !result.iter().any(|r| r.hit.evidence_id == "doc-scope"),
        "cross-scope query should not return results from wrong scope"
    );
}

#[test]
fn matching_fuzzy_names_orders_by_distance_and_caps_sql_terms() {
    let mut names = (0..(FUZZY_MATCHED_NAME_LIMIT + 20))
        .map(near_query_name)
        .collect::<Vec<_>>();
    names.push("aaaaa".to_owned());

    let matches = matching_fuzzy_names(names, "aaaaa", FUZZY_LONG_QUERY_MAX_DISTANCE);

    assert_eq!(matches.len(), FUZZY_MATCHED_NAME_LIMIT);
    assert_eq!(matches[0].name, "aaaaa");
    assert_eq!(matches[0].distance, 0);
    assert!(matches.windows(2).all(|window| {
        let left = &window[0];
        let right = &window[1];
        (left.distance, left.name.as_str()) <= (right.distance, right.name.as_str())
    }));
}

fn near_query_name(index: usize) -> String {
    const ALPHABET: &[u8] = b"bcdefghijklmnopqrstuvwxyz0123456789";
    let first = char::from(ALPHABET[index % ALPHABET.len()]);
    let second = char::from(ALPHABET[(index / ALPHABET.len()) % ALPHABET.len()]);
    format!("{first}{second}aaa")
}