khive-runtime 0.10.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
// Keep fixture SQL excluded by the unchanged inline-SQL test-module predicate.
#[cfg(test)]
mod tests {
    use super::super::*;

    #[test]
    fn still_inline_control_requires_production_sql() {
        let root = PathBuf::from("/crates/db");
        let mut sources = BTreeMap::new();
        for path in [
            "src/tests.rs",
            "src/foo_tests.rs",
            "src/tests/fixture.rs",
            "src/benches/fixture.rs",
            "tests/api.rs",
        ] {
            sources.insert(
                root.join(path),
                r#"const Q: &str = "SELECT id FROM notes";"#.into(),
            );
        }
        sources.insert(
            root.join("src/lib.rs"),
            r#"#[cfg(test)] mod tests { const Q: &str = "SELECT id FROM notes"; }"#.into(),
        );
        assert!(!still_inline_seen(&sources, &root));
        sources.insert(
            root.join("src/query.rs"),
            r#"const Q: &str = "SELECT id FROM notes";"#.into(),
        );
        assert!(still_inline_seen(&sources, &root));
        sources.remove(&root.join("src/query.rs"));
        assert!(!still_inline_seen(&sources, &root));
    }

    #[test]
    fn out_of_line_test_module_declaration_keeps_the_code_after_it() {
        let source = r#"#[cfg(test)]
#[path = "lib_tests.rs"]
mod lib_tests;

impl Store {
    fn q() -> &'static str { "SELECT id FROM notes" }
}

#[cfg(test)]
mod inline {
    const Q: &str = "SELECT secret FROM notes";
}
"#;
        assert_eq!(
            sql_literals(&strip_test_modules(source)),
            vec!["SELECT id FROM notes".to_string()]
        );
    }

    #[test]
    fn test_fixture_braces_do_not_hide_production_sql() {
        let root = PathBuf::from("/crates");
        let path = root.join("demo/src/lib.rs");
        for fixture in [
            r#"const BRACE: &str = "{";"#,
            r##"const BRACE: &str = r#"{"#;"##,
            r#"const BRACE: &[u8] = b"{";"#,
            "const BRACE: char = '{';",
            "// {",
            "/* { /* nested } */ { */",
            r#"const BRACE: &str = "}";"#,
        ] {
            let source = format!(
                "#[cfg(test)]\nmod tests {{\n{fixture}\n\
                 const Q: &str = \"SELECT fixture FROM notes\";\n}}\n\
                 const Q: &str = \"SELECT production FROM notes\";\n"
            );
            let sources = BTreeMap::from([(path.clone(), source.clone())]);
            assert_eq!(
                inline_errors(&sources, &root, &["demo"], &[]),
                vec!["inline SQL: demo/src/lib.rs: SELECT production FROM notes"],
                "{source}"
            );
            assert!(still_inline_seen(&sources, &root.join("demo")));
        }
    }

    #[test]
    fn test_module_mask_preserves_prefix_suffix_and_line_boundaries() {
        let root = PathBuf::from("/crates");
        let path = root.join("demo/src/lib.rs");
        for prefix in [
            "",
            "\u{feff}",
            "#!/usr/bin/env rust-script\n",
            "\u{feff}#!/usr/bin/env rust-script\r\n",
            "#![allow(dead_code)]\n",
            "\u{feff}#! /* prelude */ [allow(dead_code)]\r\n",
        ] {
            let before = format!("{prefix}const LABEL: &str = \"雪\"; ");
            let after = " const Q: &str = \"SELECT production FROM notes\";\r\n";
            for attributes in [
                "#[cfg(test)]\r\n#[doc = \"SELECT fixture_doc FROM notes\"]",
                "#[doc = \"SELECT fixture_doc FROM notes\"]\r\n#[cfg(test)]",
            ] {
                let source = format!(
                    "{before}{attributes}\r\nmod tests {{\r\n\
                     const Q: &str = \"SELECT fixture FROM notes\";\r\n}}{after}"
                );
                let masked = strip_test_modules(&source);
                assert_eq!(masked.len(), source.len());
                assert!(masked.starts_with(&before));
                assert!(masked.ends_with(after));
                for (index, byte) in source.bytes().enumerate() {
                    if matches!(byte, b'\r' | b'\n') {
                        assert_eq!(masked.as_bytes()[index], byte);
                    }
                }
                let sources = BTreeMap::from([(path.clone(), source)]);
                assert_eq!(
                    inline_errors(&sources, &root, &["demo"], &[]),
                    vec!["inline SQL: demo/src/lib.rs: SELECT production FROM notes"]
                );
            }
        }
    }

    #[test]
    fn only_actual_direct_cfg_test_inline_modules_are_excluded() {
        let source = r##"
const MARKER: &str = r#"#[cfg(test)] mod decoy {"#;
// #[cfg(test)] mod comment_decoy {
mod outer {
    #[cfg(test)]
    mod fixture {
        const Q: &str = "SELECT fixture FROM notes";
        #[cfg(test)]
        mod nested { const Q: &str = "SELECT nested_fixture FROM notes"; }
    }
    const Q: &str = "SELECT nested_production FROM notes";
}
#[cfg(feature = "demo")]
mod conditional { const Q: &str = "SELECT conditional FROM notes"; }
#[cfg(all(test, feature = "demo"))]
mod compound { const Q: &str = "SELECT compound FROM notes"; }
const Q: &str = "SELECT production FROM notes";
"##;
        let root = PathBuf::from("/crates");
        let sources = BTreeMap::from([(root.join("demo/src/lib.rs"), source.into())]);
        assert_eq!(
            inline_errors(&sources, &root, &["demo"], &[]),
            vec![
                "inline SQL: demo/src/lib.rs: SELECT nested_production FROM notes",
                "inline SQL: demo/src/lib.rs: SELECT conditional FROM notes",
                "inline SQL: demo/src/lib.rs: SELECT compound FROM notes",
                "inline SQL: demo/src/lib.rs: SELECT production FROM notes",
            ]
        );
        let masked = strip_test_modules(source);
        assert!(masked.contains(r##"const MARKER: &str = r#"#[cfg(test)] mod decoy {"#;"##));
        assert!(masked.contains("// #[cfg(test)] mod comment_decoy {"));
    }

    #[test]
    #[should_panic(expected = "parse Rust source for inline SQL policy")]
    fn malformed_inline_source_fails_closed() {
        let root = PathBuf::from("/crates");
        let sources = BTreeMap::from([(
            root.join("demo/src/lib.rs"),
            "#[cfg(test)] mod broken {".into(),
        )]);
        inline_errors(&sources, &root, &["demo"], &[]);
    }

    fn fixture(source: &str) -> (BTreeMap<PathBuf, String>, BTreeSet<PathBuf>, PathBuf) {
        let root = PathBuf::from("/crates");
        let sources = BTreeMap::from([
            (root.join("khive-runtime/src/sql_include.rs"), r#"#[macro_export]
                macro_rules! sql { ($name:literal) => { const {
                    include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/sql/", $name, ".sql")).trim_ascii_end()
                } }; }"#.into()),
            (root.join("khive-runtime/src/lib.rs"), "mod sql_include;".into()),
            (root.join("demo/src/lib.rs"), source.into()),
            (root.join("demo/src/sql.rs"), "pub(crate) use khive_runtime::sql;".into()),
        ]);
        (
            sources,
            BTreeSet::from([root.join("demo/sql/query.sql")]),
            root,
        )
    }

    #[test]
    fn loaders_use_exact_owning_assets_and_detect_removal() {
        for source in [
            r#"const Q: &str = khive_runtime::sql!("query");"#,
            r#"mod sql; const Q: &str = crate::sql::sql!("query");"#,
            r#"mod sql; use crate::sql::sql; const Q: &str = sql!(r"query");"#,
            r#"use khive_runtime::sql as statement; const Q: &str = statement!("qu\u{65}ry");"#,
            r#"const MIGRATION: &str = include_str!("../sql/query.sql");"#,
            r#"const Q: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), concat!("/sql/", "query.sql")));"#,
            r#"const DATA: &str = include_str!("../data.json"); const Q: &str = include_str!("../sql/query.sql");"#,
        ] {
            let (mut sources, mut assets, root) = fixture(source);
            assert!(
                reference_errors(&sources, &assets, &root).is_empty(),
                "{source}"
            );
            assets.insert(root.join("demo/sql/unused.sql"));
            assert!(reference_errors(&sources, &assets, &root)
                .iter()
                .any(|e| e.ends_with("unused.sql")));
            assets.remove(&root.join("demo/sql/unused.sql"));
            sources.insert(root.join("demo/src/lib.rs"), String::new());
            assert!(reference_errors(&sources, &assets, &root)
                .iter()
                .any(|e| e.ends_with("query.sql")));
            sources.insert(root.join("demo/src/lib.rs"), source.into());
            assert!(reference_errors(&sources, &assets, &root).is_empty());
        }
    }

    #[test]
    fn runtime_root_macro_is_canonical_only_in_runtime() {
        let (mut sources, _, root) = fixture("");
        let asset = root.join("khive-runtime/sql/query.sql");
        let assets = BTreeSet::from([asset]);
        sources.insert(
            root.join("khive-runtime/src/lib.rs"),
            "mod sql_include; const Q: &str = crate::sql!(\"query\");".into(),
        );
        assert!(reference_errors(&sources, &assets, &root).is_empty());
        let (foreign, foreign_assets, _) = fixture("const Q: &str = crate::sql!(\"query\");");
        assert!(reference_errors(&foreign, &foreign_assets, &root)
            .iter()
            .any(|e| e.starts_with("unused SQL:")));
        sources.insert(
            root.join("khive-runtime/src/sql_include.rs"),
            "#[macro_export] macro_rules! sql { ($name:literal) => { $name }; }".into(),
        );
        assert!(reference_errors(&sources, &assets, &root)
            .iter()
            .any(|e| e.starts_with("unused SQL:")));
    }

    #[test]
    fn strings_identifiers_shadowing_and_foreign_paths_do_not_count() {
        for source in [
            r#"// khive_runtime::sql!("query");
                const Q: &str = "khive_runtime::sql!(\"query\")";"#,
            r#"const Q: &str = "SELECT query FROM query";"#,
            r#"const Q: &str = include_str!("../../other/sql/query.sql");"#,
            r#"macro_rules! sql { ($x:literal) => { $x }; } const Q: &str = sql!("query");"#,
            r#"mod khive_runtime { macro_rules! sql { ($x:literal) => { $x }; } } const Q: &str = khive_runtime::sql!("query");"#,
            r#"mod sql; use crate::sql::sql; fn f() { macro_rules! sql { ($x:literal) => { $x }; } let _ = sql!("query"); }"#,
            r#"use khive_runtime::sql as statement; fn f() { use foreign::statement; let _ = statement!("query"); }"#,
            r#"#[macro_use] mod foreign { macro_rules! include_str { ($x:literal) => { $x }; } } const Q: &str = include_str!("../sql/query.sql");"#,
            r#"macro_rules! include_str { ($x:literal) => { $x }; } const Q: &str = include_str!("../sql/query.sql");"#,
            r#"fn f() { let generated = quote!(khive_runtime::sql!("query")); }"#,
            r#"const Q: &str = concat!("SELECT ", "query FROM table");"#,
        ] {
            let (sources, assets, root) = fixture(source);
            assert!(
                reference_errors(&sources, &assets, &root)
                    .iter()
                    .any(|e| e.starts_with("unused SQL:")),
                "{source}"
            );
        }
    }

    #[test]
    fn malformed_and_missing_loaders_fail_closed() {
        for source in [
            r#"const Q: &str = khive_runtime::sql!("typo");"#,
            r#"const Q: &str = khive_runtime::sql!(dynamic_name);"#,
            r#"const Q: &str = khive_runtime::sql!("query", "extra");"#,
            r#"const Q: &str = include_str!(computed_sql_path());"#,
            r#"const Q: &str = include_str!(concat!(env!("OTHER"), "/query.sql"));"#,
        ] {
            let (sources, assets, root) = fixture(source);
            let errors = reference_errors(&sources, &assets, &root);
            assert!(errors.len() >= 2, "{source}: {errors:?}");
        }
    }

    #[test]
    fn declared_child_globs_import_only_that_modules_names() {
        let (mut sources, assets, root) =
            fixture(r#"mod inner; use inner::*; const Q: &str = khive_runtime::sql!("query");"#);
        let inner = root.join("demo/src/inner.rs");
        sources.insert(
            inner.clone(),
            "use super::*; pub(super) fn helper() {}".into(),
        );
        assert!(reference_errors(&sources, &assets, &root).is_empty());
        for (child, why) in [
            (
                "pub(super) mod khive_runtime {}",
                "the glob brings in a shadowing module",
            ),
            (
                "pub(super) use dependency::*;",
                "a glob re-export imports unknown names",
            ),
            (
                "thread_local! { static X: u8 = 0; }",
                "an item macro may define any name",
            ),
        ] {
            sources.insert(inner.clone(), child.into());
            assert!(
                reference_errors(&sources, &assets, &root)
                    .iter()
                    .any(|e| e.starts_with("unused SQL:")),
                "{why}"
            );
        }
        let (undeclared, assets, root) =
            fixture(r#"use inner::*; const Q: &str = khive_runtime::sql!("query");"#);
        assert!(reference_errors(&undeclared, &assets, &root)
            .iter()
            .any(|e| e.starts_with("unused SQL:")));
    }

    #[test]
    fn sql_included_outside_the_inventory_must_exist() {
        let (sources, _, fixture_root) = fixture(
            r#"const Q: &str = khive_runtime::sql!("query");
                const D: &str = include_str!("../docs/audit.sql");"#,
        );
        let root = std::env::temp_dir().join(format!("khive-sql-policy-{}", std::process::id()));
        let sources = sources
            .into_iter()
            .map(|(path, text)| (root.join(path.strip_prefix(&fixture_root).unwrap()), text))
            .collect::<BTreeMap<_, _>>();
        let assets = BTreeSet::from([root.join("demo/sql/query.sql")]);
        let doc = root.join("demo/docs/audit.sql");
        assert!(reference_errors(&sources, &assets, &root)
            .iter()
            .any(|e| e.starts_with("missing SQL:") && e.ends_with("audit.sql")));
        std::fs::create_dir_all(doc.parent().unwrap()).unwrap();
        std::fs::write(&doc, "SELECT 1;\n").unwrap();
        let errors = reference_errors(&sources, &assets, &root);
        // A file under sql/ that the inventory does not list is refused even though it exists.
        let nested = root.join("demo/sql/nested/query.sql");
        std::fs::create_dir_all(nested.parent().unwrap()).unwrap();
        std::fs::write(&nested, "SELECT 1;\n").unwrap();
        let mut nested_sources = sources.clone();
        nested_sources.insert(
            root.join("demo/src/lib.rs"),
            r#"const Q: &str = khive_runtime::sql!("query");
                const N: &str = include_str!("../sql/nested/query.sql");"#
                .into(),
        );
        let nested_errors = reference_errors(&nested_sources, &assets, &root);
        std::fs::remove_dir_all(&root).unwrap();
        assert!(errors.is_empty(), "{errors:?}");
        assert!(
            nested_errors.iter().any(|e| e.starts_with("missing SQL:")
                && e.ends_with("query.sql")
                && e.contains("nested")),
            "{nested_errors:?}"
        );
    }

    #[test]
    fn module_ancestry_preserves_imports_and_shadowing() {
        let (mut sources, assets, root) = fixture("mod sql; use crate::sql::sql; mod child;");
        sources.insert(
            root.join("demo/src/child.rs"),
            "use super::*; const Q: &str = sql!(\"query\");".into(),
        );
        assert!(reference_errors(&sources, &assets, &root).is_empty());
        sources.insert(
            root.join("demo/src/lib.rs"),
            "use dependency::*; mod child;".into(),
        );
        sources.insert(
            root.join("demo/src/child.rs"),
            r#"const Q: &str = khive_runtime::sql!("query");"#.into(),
        );
        assert!(reference_errors(&sources, &assets, &root).is_empty());
        for (parent, child) in [
            (
                r#"#[macro_use] mod foreign { macro_rules! format { ($($t:tt)*) => { 0 }; } } mod child;"#,
                r#"const Q: usize = format!(khive_runtime::sql!("query"));"#,
            ),
            (
                r#"mod sql; macro_rules! sql { ($x:literal) => { $x }; } mod child;"#,
                r#"use super::*; const Q: &str = sql!("query");"#,
            ),
            (
                r#"macro_rules! format { ($($t:tt)*) => { 0 }; } mod child;"#,
                r#"const Q: usize = format!(khive_runtime::sql!("query"));"#,
            ),
            (
                r#"macro_rules! statement { ($($t:tt)*) => { 0 }; } mod child;"#,
                r#"use khive_runtime::sql as statement; const Q: usize = statement!("query");"#,
            ),
        ] {
            sources.insert(root.join("demo/src/lib.rs"), parent.into());
            sources.insert(root.join("demo/src/child.rs"), child.into());
            assert!(reference_errors(&sources, &assets, &root)
                .iter()
                .any(|e| e.starts_with("unused SQL:")));
        }
    }

    #[test]
    fn named_keeps_are_exact_live_single_statement_exceptions() {
        let root = PathBuf::from("/crates");
        let path = root.join("demo/src/lib.rs");
        let sql = "SELECT id FROM {table}";
        let source = r#"fn query() { let _ = format!("SELECT id FROM {table}"); }"#;
        let mut sources = BTreeMap::from([(path.clone(), source.into())]);
        let keep = || Keep {
            name: "dynamic_table",
            path: "demo/src/lib.rs",
            sql,
            reason: "Runtime table identifier.",
        };
        assert!(inline_errors(&sources, &root, &["demo"], &[keep()]).is_empty());
        assert!(!inline_errors(&sources, &root, &["demo"], &[]).is_empty());
        assert!(!inline_errors(
            &sources,
            &root,
            &["demo"],
            &[Keep {
                reason: "",
                ..keep()
            }]
        )
        .is_empty());
        assert!(!inline_errors(&sources, &root, &["demo"], &[keep(), keep()]).is_empty());
        for changed in [
            "",
            r#"fn f() { let _ = "SELECT other FROM {table}"; }"#,
            r#"fn f() { let _ = "SELECT id FROM {table}"; let _ = "SELECT id FROM {table}"; }"#,
            r#"fn f() { let _ = "SELECT id FROM {table}"; let _ = "SELECT secret FROM notes"; }"#,
        ] {
            sources.insert(path.clone(), changed.into());
            assert!(
                !inline_errors(&sources, &root, &["demo"], &[keep()]).is_empty(),
                "{changed}"
            );
        }
    }

    #[test]
    fn inline_module_paths_preserve_textual_macro_scope() {
        for (source, child) in [
            (
                r#"mod outer { macro_rules! format { ($($t:tt)*) => {0}; } #[path="child.rs"] mod child; }"#,
                "src/outer/child.rs",
            ),
            (
                r#"#[path="special"] mod outer { macro_rules! format { ($($t:tt)*) => {0}; } #[path="child.rs"] mod child; }"#,
                "src/special/child.rs",
            ),
        ] {
            let (mut sources, assets, root) = fixture(source);
            sources.insert(
                root.join("demo").join(child),
                r#"const Q: usize = format!(khive_runtime::sql!("query"));"#.into(),
            );
            assert!(reference_errors(&sources, &assets, &root)
                .iter()
                .any(|e| e.starts_with("unused SQL:")));
            // The same path resolution must also credit an actual unshadowed load.
            sources.insert(
                root.join("demo").join(child),
                r#"const Q: &str = khive_runtime::sql!("query");"#.into(),
            );
            assert!(reference_errors(&sources, &assets, &root).is_empty());
        }
        let (mut sources, assets, root) = fixture("mod holder;");
        sources.insert(root.join("demo/src/holder.rs"),
            r#"mod outer { macro_rules! format { ($($t:tt)*) => {0}; } #[path="child.rs"] mod child; }"#.into());
        sources.insert(
            root.join("demo/src/holder/outer/child.rs"),
            r#"const Q: usize = format!(khive_runtime::sql!("query"));"#.into(),
        );
        assert!(reference_errors(&sources, &assets, &root)
            .iter()
            .any(|e| e.starts_with("unused SQL:")));
    }
}