dkp-core 0.5.0

Core DKP bundle parsing, types, validation, and search library
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
use std::collections::HashSet;

use crate::{
    pack::loader::Pack,
    procedures,
    validate::gates::{CheckResult, GateResult, GateStatus},
};

const REQUIRED_MACHINE_FILES: &[&str] = &[
    "system_prompt.md",
    "glossary.json",
    "ontology.json",
    "rules.json",
    "constraints.json",
    "retrieval_chunks.jsonl",
];

/// Gate 4: Machine Usability — required files present, schemas valid, refs resolve.
pub fn run(pack: &Pack) -> GateResult {
    let mut checks = Vec::new();

    // Required file presence
    for file in REQUIRED_MACHINE_FILES {
        if pack.machine_file(file).exists() {
            checks.push(CheckResult::pass(format!("machine/{file} present")));
        } else {
            checks.push(CheckResult::fail(
                format!("machine/{file} present"),
                format!("machine/{file} is required but not found"),
            ));
        }
    }

    // manifest.json present (already opened, so always true if we got here)
    checks.push(CheckResult::pass("manifest.json present"));

    // Parse each required JSON/JSONL asset — deserialization failure = schema violation
    for (name, result) in [
        ("glossary.json", pack.load_glossary().map(|_| ())),
        ("ontology.json", pack.load_ontology().map(|_| ())),
        ("rules.json", pack.load_rules().map(|_| ())),
        ("constraints.json", pack.load_constraints().map(|_| ())),
        ("retrieval_chunks.jsonl", pack.load_chunks().map(|_| ())),
    ] {
        match result {
            Ok(_) => checks.push(CheckResult::pass(format!("machine/{name} parses"))),
            Err(e) => checks.push(CheckResult::fail(
                format!("machine/{name} parses"),
                e.to_string(),
            )),
        }
    }

    // source_ref resolution against evidence/sources.csv
    let sources_csv = pack.evidence_file("sources.csv");
    if sources_csv.exists() {
        let known_ids: HashSet<String> = std::fs::read_to_string(&sources_csv)
            .unwrap_or_default()
            .lines()
            .skip(1) // header row
            .filter_map(|line| {
                line.split(',')
                    .next()
                    .map(|s| s.trim().trim_matches('"').to_string())
            })
            .filter(|s| !s.is_empty())
            .collect();

        let mut unresolved: Vec<String> = Vec::new();

        if let Ok(Some(gf)) = pack.load_glossary() {
            for t in &gf.terms {
                if let Some(ref sr) = t.source_ref
                    && sr != "generated"
                    && !known_ids.contains(sr.as_str())
                {
                    unresolved.push(format!("term/{}: {sr}", t.id));
                }
            }
        }
        if let Ok(Some(rf)) = pack.load_rules() {
            for r in &rf.rules {
                if let Some(ref sr) = r.source_ref
                    && sr != "generated"
                    && !known_ids.contains(sr.as_str())
                {
                    unresolved.push(format!("rule/{}: {sr}", r.id));
                }
            }
        }
        if let Ok(Some(cf)) = pack.load_constraints() {
            for c in cf.all_constraints() {
                if let Some(ref sr) = c.source_ref
                    && sr != "generated"
                    && !known_ids.contains(sr.as_str())
                {
                    unresolved.push(format!("constraint/{}: {sr}", c.id));
                }
            }
        }

        if unresolved.is_empty() {
            checks.push(CheckResult::pass("source_ref resolution"));
        } else {
            checks.push(CheckResult::fail(
                "source_ref resolution",
                format!("unresolved refs: {}", unresolved.join(", ")),
            ));
        }
    }

    // knowledge_graph edge resolution
    if pack.has_knowledge_graph() {
        match pack.load_graph() {
            Ok(Some(graph)) => {
                let node_ids: HashSet<&str> = graph.nodes.iter().map(|n| n.id.as_str()).collect();
                let broken: Vec<String> = graph
                    .edges
                    .iter()
                    .filter_map(|e| {
                        let src_ok = node_ids.contains(e.source.as_str());
                        let tgt_ok = node_ids.contains(e.target.as_str());
                        if !src_ok || !tgt_ok {
                            Some(format!("{}->{}", e.source, e.target))
                        } else {
                            None
                        }
                    })
                    .collect();
                if broken.is_empty() {
                    checks.push(CheckResult::pass("knowledge_graph edge resolution"));
                } else {
                    checks.push(CheckResult::fail(
                        "knowledge_graph edge resolution",
                        format!("broken edges: {}", broken.join(", ")),
                    ));
                }
            }
            Ok(None) => checks.push(CheckResult::skip("knowledge_graph.json (not present)")),
            Err(e) => checks.push(CheckResult::fail(
                "knowledge_graph.json parses",
                e.to_string(),
            )),
        }
    }

    // Procedure completeness when machine/procedures/ is non-empty
    if pack.has_procedures() {
        match procedures::validate_all(pack) {
            Ok(errors) if errors.is_empty() => {
                checks.push(CheckResult::pass("machine/procedures/ completeness"));
            }
            Ok(errors) => {
                for e in &errors {
                    checks.push(CheckResult::fail(
                        "machine/procedures/ completeness",
                        e.clone(),
                    ));
                }
            }
            Err(e) => {
                checks.push(CheckResult::fail(
                    "machine/procedures/ completeness",
                    e.to_string(),
                ));
            }
        }
    }

    // MCP manifest when mcp block is present
    if pack.manifest.mcp.is_some() {
        if pack.machine_file("mcp_manifest.json").exists() {
            checks.push(CheckResult::pass(
                "machine/mcp_manifest.json present (mcp configured)",
            ));
        } else {
            checks.push(CheckResult::fail(
                "machine/mcp_manifest.json present (mcp configured)",
                "manifest.mcp is set but machine/mcp_manifest.json is missing",
            ));
        }
    }

    let failed = checks.iter().any(|c| c.status == GateStatus::Fail);
    GateResult {
        gate: 4,
        status: if failed {
            GateStatus::Fail
        } else {
            GateStatus::Pass
        },
        checks,
        message: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn minimal_manifest_json() -> &'static str {
        r#"{
            "spec": "1.0.0",
            "name": "test-pack",
            "version": "1.0.0",
            "domain": "testing",
            "audience": "internal",
            "intended_use": "unit tests",
            "known_limitations": "none",
            "update_date": "2026-01-01"
        }"#
    }

    /// Builds a pack directory with all gate 4 required machine files present
    /// and valid, returning the opened `Pack`.
    fn complete_pack(tmp: &TempDir) -> Pack {
        std::fs::write(tmp.path().join("manifest.json"), minimal_manifest_json()).unwrap();
        let machine = tmp.path().join("machine");
        std::fs::create_dir_all(&machine).unwrap();
        std::fs::write(machine.join("system_prompt.md"), "Be helpful.").unwrap();
        std::fs::write(machine.join("glossary.json"), r#"{"terms": []}"#).unwrap();
        std::fs::write(machine.join("ontology.json"), r#"{"entity_types": []}"#).unwrap();
        std::fs::write(machine.join("rules.json"), r#"{"rules": []}"#).unwrap();
        std::fs::write(
            machine.join("constraints.json"),
            r#"{"edge_cases": [], "anti_patterns": [], "hard_limits": []}"#,
        )
        .unwrap();
        std::fs::write(machine.join("retrieval_chunks.jsonl"), "").unwrap();
        Pack::open(tmp.path()).unwrap()
    }

    #[test]
    fn all_required_files_present_and_valid_passes() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
        assert_eq!(result.gate, 4);
    }

    #[test]
    fn missing_required_file_fails() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::remove_file(pack.machine_file("glossary.json")).unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Fail);
        assert!(
            result
                .checks
                .iter()
                .any(|c| c.description.contains("glossary.json") && c.status == GateStatus::Fail)
        );
    }

    #[test]
    fn invalid_json_in_machine_file_fails() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(pack.machine_file("rules.json"), "{ not valid").unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Fail);
        assert!(
            result
                .checks
                .iter()
                .any(|c| c.description.contains("rules.json") && c.status == GateStatus::Fail)
        );
    }

    #[test]
    fn sources_csv_absent_skips_source_ref_check() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        let result = run(&pack);
        assert!(
            !result
                .checks
                .iter()
                .any(|c| c.description == "source_ref resolution")
        );
    }

    #[test]
    fn unresolved_source_ref_fails() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(
            pack.machine_file("glossary.json"),
            r#"{"terms": [{"id": "t1", "term": "Term", "definition": "def", "source_ref": "src-1"}]}"#,
        )
        .unwrap();
        std::fs::create_dir_all(pack.evidence_dir()).unwrap();
        std::fs::write(pack.evidence_file("sources.csv"), "id,title\n").unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Fail);
        assert!(
            result
                .checks
                .iter()
                .any(|c| c.description == "source_ref resolution" && c.status == GateStatus::Fail)
        );
    }

    #[test]
    fn source_ref_generated_is_always_allowed() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(
            pack.machine_file("glossary.json"),
            r#"{"terms": [{"id": "t1", "term": "Term", "definition": "def", "source_ref": "generated"}]}"#,
        )
        .unwrap();
        std::fs::create_dir_all(pack.evidence_dir()).unwrap();
        std::fs::write(pack.evidence_file("sources.csv"), "id,title\n").unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
    }

    #[test]
    fn resolved_source_ref_passes() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(
            pack.machine_file("glossary.json"),
            r#"{"terms": [{"id": "t1", "term": "Term", "definition": "def", "source_ref": "src-1"}]}"#,
        )
        .unwrap();
        std::fs::create_dir_all(pack.evidence_dir()).unwrap();
        std::fs::write(
            pack.evidence_file("sources.csv"),
            "id,title\nsrc-1,Some Source\n",
        )
        .unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
    }

    #[test]
    fn knowledge_graph_broken_edge_fails() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(
            pack.machine_file("knowledge_graph.json"),
            r#"{"nodes": [{"id": "n1", "node_type": "concept", "label": "N1"}], "edges": [{"source": "n1", "relation": "see-also", "target": "missing"}]}"#,
        )
        .unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Fail);
        assert!(result.checks.iter().any(|c| {
            c.description.contains("knowledge_graph edge resolution")
                && c.status == GateStatus::Fail
        }));
    }

    #[test]
    fn knowledge_graph_resolved_edges_pass() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        std::fs::write(
            pack.machine_file("knowledge_graph.json"),
            r#"{"nodes": [{"id": "n1", "node_type": "concept", "label": "N1"}, {"id": "n2", "node_type": "concept", "label": "N2"}], "edges": [{"source": "n1", "relation": "see-also", "target": "n2"}]}"#,
        )
        .unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
    }

    #[test]
    fn wasm_backed_procedure_passes() {
        let tmp = TempDir::new().unwrap();
        let pack = complete_pack(&tmp);
        let procedures = pack.procedures_dir();
        std::fs::create_dir_all(&procedures).unwrap();
        std::fs::write(
            procedures.join("calc.schema.json"),
            r#"{"id": "calc", "title": "Calc", "description": "d", "input": {}, "output": {}}"#,
        )
        .unwrap();
        std::fs::write(procedures.join("calc.wasm"), b"\0asm").unwrap();
        std::fs::write(procedures.join("calc.md"), "docs").unwrap();
        std::fs::write(
            tmp.path().join("manifest.json"),
            r#"{
                "spec": "1.0.0",
                "name": "test-pack",
                "version": "1.0.0",
                "domain": "testing",
                "audience": "internal",
                "intended_use": "unit tests",
                "known_limitations": "none",
                "update_date": "2026-01-01",
                "procedure_capabilities": {"sandbox": "wasm"}
            }"#,
        )
        .unwrap();
        let pack = Pack::open(tmp.path()).unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
    }

    #[test]
    fn entry_point_procedure_without_wasm_passes() {
        let tmp = TempDir::new().unwrap();
        let _pack = complete_pack(&tmp);
        let procedures = tmp.path().join("machine").join("procedures");
        std::fs::create_dir_all(&procedures).unwrap();
        std::fs::write(
            procedures.join("calc.schema.json"),
            r#"{
                "id": "calc", "title": "Calc", "description": "d",
                "input": {}, "output": {},
                "entry_point": {"filename": "calc.py", "command": "python3 calc.py"}
            }"#,
        )
        .unwrap();
        std::fs::write(procedures.join("calc.py"), "print('hi')").unwrap();
        std::fs::write(procedures.join("calc.md"), "docs").unwrap();
        std::fs::write(
            tmp.path().join("manifest.json"),
            r#"{
                "spec": "1.0.0",
                "name": "test-pack",
                "version": "1.0.0",
                "domain": "testing",
                "audience": "internal",
                "intended_use": "unit tests",
                "known_limitations": "none",
                "update_date": "2026-01-01",
                "procedure_capabilities": {"sandbox": "none"}
            }"#,
        )
        .unwrap();
        let pack = Pack::open(tmp.path()).unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Pass);
    }

    #[test]
    fn procedure_without_wasm_or_entry_point_fails() {
        let tmp = TempDir::new().unwrap();
        let _pack = complete_pack(&tmp);
        let procedures = tmp.path().join("machine").join("procedures");
        std::fs::create_dir_all(&procedures).unwrap();
        std::fs::write(
            procedures.join("calc.schema.json"),
            r#"{"id": "calc", "title": "Calc", "description": "d", "input": {}, "output": {}}"#,
        )
        .unwrap();
        std::fs::write(procedures.join("calc.md"), "docs").unwrap();
        std::fs::write(
            tmp.path().join("manifest.json"),
            r#"{
                "spec": "1.0.0",
                "name": "test-pack",
                "version": "1.0.0",
                "domain": "testing",
                "audience": "internal",
                "intended_use": "unit tests",
                "known_limitations": "none",
                "update_date": "2026-01-01",
                "procedure_capabilities": {"sandbox": "none"}
            }"#,
        )
        .unwrap();
        let pack = Pack::open(tmp.path()).unwrap();

        let result = run(&pack);
        assert_eq!(result.status, GateStatus::Fail);
        assert!(result.checks.iter().any(|c| {
            c.description.contains("machine/procedures/ completeness")
                && c.status == GateStatus::Fail
        }));
    }
}