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
use rusqlite::{Connection, params, params_from_iter, types::Value};

use super::{
    MAX_DOCUMENT_BYTES, MAX_DOCUMENT_FILES, MAX_DOCUMENT_PATH_FILTERS, SqlPathPredicate,
    bounded_document_content_query, bounded_file_metadata_query, read_indexed_markdown,
};
use crate::storage::StorageError;

#[test]
fn reads_only_scoped_markdown_and_reassembles_chunks_in_source_order() {
    let mut connection = document_connection();

    let documents = read_indexed_markdown(&mut connection, "scope-a", &["docs".to_owned()], 2, 64)
        .expect("bounded Markdown documents should load");

    assert_eq!(documents.len(), 2);
    assert_eq!(documents[0].path, "docs/a.md");
    assert_eq!(documents[0].language_id, "markdown");
    assert_eq!(documents[0].content, "alpha\nbeta");
    assert_eq!(documents[1].path, "docs/b.md");
    assert_eq!(documents[1].content, "gamma");
}

#[test]
fn rejects_invalid_and_exhausted_document_budgets() {
    let mut connection = document_connection();

    assert!(matches!(
        read_indexed_markdown(&mut connection, "scope-a", &[], 0, 64),
        Err(StorageError::InvalidInput(message))
            if message == format!(
                "repository document file limit must be between 1 and {MAX_DOCUMENT_FILES}"
            )
    ));
    assert!(matches!(
        read_indexed_markdown(
            &mut connection,
            "scope-a",
            &[],
            1,
            MAX_DOCUMENT_BYTES + 1,
        ),
        Err(StorageError::InvalidInput(message))
            if message == format!(
                "repository document byte limit must be between 1 and {MAX_DOCUMENT_BYTES}"
            )
    ));
    assert!(matches!(
        read_indexed_markdown(&mut connection, "scope-a", &[], 1, 64),
        Err(StorageError::InvalidInput(message))
            if message == "repository document file budget exhausted"
    ));
    assert!(matches!(
        read_indexed_markdown(
            &mut connection,
            "scope-a",
            &["docs/a.md".to_owned()],
            1,
            8,
        ),
        Err(StorageError::InvalidInput(message))
            if message == "repository document byte budget exhausted"
    ));
}

#[test]
fn preflights_file_and_byte_budgets_before_materializing_content() {
    let mut file_limited = document_connection();
    file_limited
        .execute(
            "UPDATE code_repository_chunks SET content = ?1 WHERE path = 'docs/a.md'",
            params![Value::Blob(vec![0xff])],
        )
        .expect("invalid text fixture should update");
    assert!(matches!(
        read_indexed_markdown(&mut file_limited, "scope-a", &[".".to_owned()], 1, 64),
        Err(StorageError::InvalidInput(message))
            if message == "repository document file budget exhausted"
    ));

    let mut byte_limited = document_connection();
    byte_limited
        .execute(
            "UPDATE code_repository_files SET byte_len = 65 WHERE path = 'docs/a.md'",
            [],
        )
        .expect("indexed byte fixture should update");
    byte_limited
        .execute(
            "UPDATE code_repository_chunks SET content = ?1 WHERE path = 'docs/a.md'",
            params![Value::Blob(vec![0xff])],
        )
        .expect("invalid text fixture should update");
    assert!(matches!(
        read_indexed_markdown(
            &mut byte_limited,
            "scope-a",
            &["docs/a.md".to_owned()],
            1,
            64,
        ),
        Err(StorageError::InvalidInput(message))
            if message == "repository document byte budget exhausted"
    ));
}

#[test]
fn path_filter_excludes_large_outside_content_before_it_is_read() {
    let mut connection = document_connection();
    for index in 0..512 {
        insert_document(
            &connection,
            "scope-a",
            &format!("archive/{index:04}.md"),
            1024 * 1024,
            Value::Blob(vec![0xff]),
        );
    }

    let documents =
        read_indexed_markdown(&mut connection, "scope-a", &["docs/a.md".to_owned()], 1, 10)
            .expect("out-of-path blobs must not consume the selected document budget");

    assert_eq!(documents.len(), 1);
    assert_eq!(documents[0].path, "docs/a.md");
    assert_eq!(documents[0].content, "alpha\nbeta");
}

#[test]
fn root_and_multiple_literal_roots_match_rust_path_semantics() {
    let mut connection = document_connection();
    let root_documents =
        read_indexed_markdown(&mut connection, "scope-a", &[".".to_owned()], 2, 15)
            .expect("dot should select the complete repository root");
    assert_eq!(
        root_documents
            .iter()
            .map(|document| document.path.as_str())
            .collect::<Vec<_>>(),
        ["docs/a.md", "docs/b.md"]
    );

    insert_document(
        &connection,
        "scope-like",
        r"docs/a%_\root/one.md",
        7,
        Value::Text("literal".to_owned()),
    );
    insert_document(
        &connection,
        "scope-like",
        "docs/axxxroot/decoy.md",
        5,
        Value::Text("decoy".to_owned()),
    );
    insert_document(
        &connection,
        "scope-like",
        r"DOCS/a%_\root/case.md",
        4,
        Value::Blob(vec![0xff]),
    );
    insert_document(
        &connection,
        "scope-like",
        "guides/two.md",
        5,
        Value::Text("guide".to_owned()),
    );

    let documents = read_indexed_markdown(
        &mut connection,
        "scope-like",
        &[r"./docs/a%_\root/".to_owned(), "guides".to_owned()],
        2,
        12,
    )
    .expect("multiple roots must preserve literal SQL metacharacters and case");

    assert_eq!(
        documents
            .iter()
            .map(|document| document.path.as_str())
            .collect::<Vec<_>>(),
        [r"docs/a%_\root/one.md", "guides/two.md"]
    );
}

#[test]
fn materialized_bytes_and_path_filter_count_remain_bounded() {
    let mut connection = document_connection();
    connection
        .execute(
            "UPDATE code_repository_files SET byte_len = 1 WHERE path = 'docs/a.md'",
            [],
        )
        .expect("indexed byte fixture should update");
    assert!(matches!(
        read_indexed_markdown(
            &mut connection,
            "scope-a",
            &["docs/a.md".to_owned()],
            1,
            9,
        ),
        Err(StorageError::InvalidInput(message))
            if message == "repository document byte budget exhausted"
    ));

    let excessive_filters = (0..=MAX_DOCUMENT_PATH_FILTERS)
        .map(|index| format!("docs/{index}"))
        .collect::<Vec<_>>();
    assert!(matches!(
        read_indexed_markdown(&mut connection, "scope-a", &excessive_filters, 1, 64),
        Err(StorageError::InvalidInput(message))
            if message == format!(
                "repository document path filter limit must not exceed {MAX_DOCUMENT_PATH_FILTERS}"
            )
    ));

    let mut empty_chunks = document_connection();
    insert_document(
        &empty_chunks,
        "scope-empty",
        "docs/empty.md",
        0,
        Value::Text(String::new()),
    );
    let okf_content = "---\ntype: research\nname: Focus\n---\n";
    insert_document(
        &empty_chunks,
        "scope-empty",
        "docs/focus.md",
        okf_content.len(),
        Value::Text(okf_content.to_owned()),
    );
    let mixed_documents = read_indexed_markdown(
        &mut empty_chunks,
        "scope-empty",
        &["docs".to_owned()],
        2,
        okf_content.len(),
    )
    .expect("one empty Markdown file must not block a valid OKF document");
    assert_eq!(mixed_documents.len(), 2);
    assert_eq!(mixed_documents[0].path, "docs/empty.md");
    assert!(mixed_documents[0].content.is_empty());
    assert_eq!(mixed_documents[1].path, "docs/focus.md");
    assert_eq!(mixed_documents[1].content, okf_content);

    empty_chunks
        .execute(
            "INSERT INTO code_repository_chunks VALUES
                ('scope-empty', 'empty-2', 'docs/empty.md', '', 0, 0),
                ('scope-empty', 'empty-3', 'docs/empty.md', '', 0, 0)",
            [],
        )
        .expect("empty chunk fixtures should insert");
    assert!(matches!(
        read_indexed_markdown(
            &mut empty_chunks,
            "scope-empty",
            &["docs/empty.md".to_owned()],
            1,
            1,
        ),
        Err(StorageError::InvalidInput(message))
            if message == "repository document 'docs/empty.md' is not lossless in the indexed snapshot; re-index the repository"
    ));
}

#[test]
fn rejects_trimmed_legacy_chunks_instead_of_projecting_changed_markdown() {
    let mut connection = document_connection();
    connection
        .execute(
            "UPDATE code_repository_chunks
             SET content = 'alpha', byte_end = 6
             WHERE source_scope = 'scope-a' AND chunk_id = 'a-first'",
            [],
        )
        .expect("legacy trimmed chunk fixture should update");

    assert!(matches!(
        read_indexed_markdown(
            &mut connection,
            "scope-a",
            &["docs/a.md".to_owned()],
            1,
            64,
        ),
        Err(StorageError::InvalidInput(message))
            if message == "repository document 'docs/a.md' is not lossless in the indexed snapshot; re-index the repository"
    ));
}

#[test]
fn filtered_query_plans_seek_files_and_chunks_by_path() {
    let connection = document_connection();
    let predicate = SqlPathPredicate::new(&["docs".to_owned()]).expect("path predicate");
    let (file_sql, file_values) = bounded_file_metadata_query("scope-a", &predicate, 3);
    let file_plan = explain_query_plan(&connection, &file_sql, &file_values);
    assert!(
        file_plan
            .iter()
            .any(|detail| detail.contains("source_scope=? AND path>? AND path<?")),
        "file preflight must use the BINARY path range: {file_plan:?}"
    );

    let (content_sql, content_values) = bounded_document_content_query("scope-a", &predicate);
    let content_plan = explain_query_plan(&connection, &content_sql, &content_values);
    assert!(
        content_plan.iter().any(|detail| {
            detail.contains("code_repository_chunks_lookup (source_scope=? AND path=?)")
        }),
        "content reads must seek chunks for each filtered file: {content_plan:?}"
    );
}

fn document_connection() -> Connection {
    let connection = Connection::open_in_memory().expect("in-memory database should open");
    connection
        .execute_batch(
            "
            CREATE TABLE code_repository_files (
                source_scope TEXT NOT NULL,
                path TEXT NOT NULL,
                language_id TEXT NOT NULL,
                byte_len INTEGER NOT NULL,
                PRIMARY KEY (source_scope, path)
            );
            CREATE TABLE code_repository_chunks (
                source_scope TEXT NOT NULL,
                chunk_id TEXT NOT NULL,
                path TEXT NOT NULL,
                content TEXT NOT NULL,
                byte_start INTEGER NOT NULL,
                byte_end INTEGER NOT NULL,
                PRIMARY KEY (source_scope, chunk_id)
            );
            CREATE INDEX code_repository_chunks_lookup
                ON code_repository_chunks(source_scope, path);
            INSERT INTO code_repository_files VALUES
                ('scope-a', 'docs/a.md', 'markdown', 10),
                ('scope-a', 'docs/b.md', 'markdown', 5),
                ('scope-a', 'src/main.rs', 'rust', 12),
                ('scope-b', 'docs/other.md', 'markdown', 5);
            INSERT INTO code_repository_chunks VALUES
                ('scope-a', 'a-second', 'docs/a.md', 'beta', 6, 10),
                ('scope-a', 'a-first', 'docs/a.md', 'alpha\n', 0, 6),
                ('scope-a', 'b-first', 'docs/b.md', 'gamma', 0, 5),
                ('scope-a', 'rust-first', 'src/main.rs', 'fn main() {}', 0, 12),
                ('scope-b', 'other-first', 'docs/other.md', 'other', 0, 5);
            ",
        )
        .expect("document fixture schema should initialize");
    connection
}

fn insert_document(
    connection: &Connection,
    source_scope: &str,
    path: &str,
    byte_len: usize,
    content: Value,
) {
    let content_len = match &content {
        Value::Text(content) => content.len(),
        Value::Blob(content) => content.len(),
        _ => panic!("document fixtures only support text or blob content"),
    };
    connection
        .execute(
            "INSERT INTO code_repository_files VALUES (?1, ?2, 'markdown', ?3)",
            params![source_scope, path, byte_len],
        )
        .expect("document file fixture should insert");
    connection
        .execute(
            "INSERT INTO code_repository_chunks VALUES (?1, ?2, ?3, ?4, 0, ?5)",
            params![
                source_scope,
                format!("{path}#chunk"),
                path,
                content,
                content_len
            ],
        )
        .expect("document chunk fixture should insert");
}

fn explain_query_plan(connection: &Connection, sql: &str, values: &[Value]) -> Vec<String> {
    let mut statement = connection
        .prepare(&format!("EXPLAIN QUERY PLAN {sql}"))
        .expect("query plan should prepare");
    statement
        .query_map(params_from_iter(values.iter()), |row| {
            row.get::<_, String>(3)
        })
        .expect("query plan should execute")
        .map(|row| row.expect("query-plan detail should decode"))
        .collect()
}