khive-pack-code 0.5.0

Code ontology pack - typed edge rules and audit findings vocabulary
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! End-to-end tests for the code pack against an in-memory runtime.
//!
//! FILE SIZE JUSTIFICATION: mirrors `khive-pack-gtd/tests/integration.rs` — a
//! single shared `Fixture` wires `KgPack` + `CodePack` against an in-memory
//! runtime; splitting would duplicate that setup across files.
//!
//! Covers the ADR-085 integration matrix: vocabulary registration, edge
//! endpoint acceptance and rejection, finding-hook defaulting and validation,
//! ingest idempotency, and malformed-input rejection.

use chrono::{DateTime, Utc};
use khive_pack_code::{ingest_findings_json, CodeIngestError, CodeIngestOptions, CodePack};
use khive_pack_kg::KgPack;
use khive_runtime::pack::PackRuntime;
use khive_runtime::{KhiveRuntime, RuntimeError, VerbRegistry, VerbRegistryBuilder};
use khive_types::{EdgeRelation, EndpointKind};
use serde_json::{json, Value};

#[allow(dead_code)]
fn rt() -> KhiveRuntime {
    KhiveRuntime::memory().expect("memory runtime")
}

#[allow(dead_code)]
fn registry(rt: KhiveRuntime) -> VerbRegistry {
    let mut builder = VerbRegistryBuilder::new();
    builder.register(KgPack::new(rt.clone()));
    builder.register(CodePack::new(rt.clone()));
    let registry = builder.build().expect("registry builds");
    rt.install_edge_rules(registry.all_edge_rules());
    registry
}

#[allow(dead_code)]
fn kg_only_registry(rt: KhiveRuntime) -> VerbRegistry {
    let mut builder = VerbRegistryBuilder::new();
    builder.register(KgPack::new(rt.clone()));
    let registry = builder.build().expect("registry builds");
    rt.install_edge_rules(registry.all_edge_rules());
    registry
}

#[allow(dead_code)]
async fn dispatch(registry: &VerbRegistry, verb: &str, args: Value) -> Result<Value, RuntimeError> {
    registry.dispatch(verb, args).await
}

#[allow(dead_code)]
fn sample_findings_json() -> Value {
    json!({
        "audit": {
            "date": "2026-07-07",
            "scope": "khive-pack-code",
            "repo": "khive",
            "branch": "feature/code-pack",
            "commit": "abc123",
            "standards_file": "audit-guidelines.md",
        },
        "findings": [
            {
                "id": "khive-pack-code-001",
                "title": "  Missing   bounds check  ",
                "severity": "high",
                "confidence": "high",
                "categories": ["security"],
                "status": "open",
                "standard": "audit-guidelines.md#security",
                "evidence": [{"path": "src/ingest.rs", "line": 42, "description": "unchecked index"}],
                "impact": "panics on malformed input",
                "recommendation": "validate before indexing",
                "verification": "add regression test",
                "failure_scenario": "attacker-controlled index panics the process",
                "priority": "P1",
            }
        ]
    })
}

#[allow(dead_code)]
fn ingest_options(source_run: Option<&str>) -> CodeIngestOptions<'_> {
    CodeIngestOptions {
        namespace: "local",
        observed_at: Utc::now(),
        source_run,
    }
}

#[tokio::test]
async fn code_pack_declares_adr085_metadata() {
    let registry = registry(rt());
    assert!(registry.all_note_kinds().contains(&"finding"));
}

/// End-to-end (not merely unit-level) proof that the four code concept
/// subtypes and their aliases registered in `khive-pack-kg`'s `BUILTIN_DEFS`
/// are reachable through the full `create` verb dispatch path, canonicalizing
/// aliases to their token before persistence. The registry-level resolution
/// itself is covered by `khive-pack-kg`'s own
/// `entity_type_registry_accepts_code_tokens_and_aliases` unit test; this
/// proves the wiring holds through dispatch, not just the raw registry call.
#[tokio::test]
async fn entity_type_registry_accepts_code_tokens_and_aliases() {
    let reg = registry(rt());
    for (alias, canonical) in [
        ("mod", "module"),
        ("namespace", "module"),
        ("fn", "function"),
        ("func", "function"),
        ("method", "function"),
        ("enum", "datatype"),
        ("record", "datatype"),
        ("type_alias", "datatype"),
        ("trait", "interface"),
        ("protocol", "interface"),
    ] {
        let created = dispatch(
            &reg,
            "create",
            json!({"kind": "entity", "name": format!("Node-{alias}"), "entity_kind": "concept", "entity_type": alias}),
        )
        .await
        .unwrap_or_else(|e| panic!("alias {alias:?} must be creatable: {e}"));
        assert_eq!(
            created["entity_type"], canonical,
            "alias {alias:?} must canonicalize to {canonical:?}"
        );
    }
}

/// `struct`/`class` must remain owned by formal's `structure` token even when
/// the code pack is registered — the code pack must not shadow them with
/// `datatype`. See also `khive-pack-kg`'s unit-level
/// `entity_type_registry_does_not_claim_struct_or_class_for_code`.
#[tokio::test]
async fn entity_type_registry_does_not_claim_struct_or_class_for_code() {
    let reg = registry(rt());
    for alias in ["struct", "class"] {
        let created = dispatch(
            &reg,
            "create",
            json!({"kind": "entity", "name": format!("Node-{alias}"), "entity_kind": "concept", "entity_type": alias}),
        )
        .await
        .unwrap_or_else(|e| panic!("alias {alias:?} must be creatable: {e}"));
        assert_eq!(
            created["entity_type"], "structure",
            "alias {alias:?} must remain owned by formal's structure, not datatype"
        );
    }
}

#[test]
fn code_edge_rules_match_adr085_triples() {
    let pack = CodePack::new(rt());
    let rules = pack.edge_rules();
    assert_eq!(
        rules.len(),
        22,
        "ADR-085 declares exactly 22 additive edge rules"
    );

    fn concept(entity_type: &'static str) -> EndpointKind {
        EndpointKind::EntityOfType {
            kind: "concept",
            entity_type,
        }
    }

    let expected: Vec<(EdgeRelation, EndpointKind, EndpointKind)> = vec![
        (
            EdgeRelation::DependsOn,
            concept("function"),
            concept("function"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("function"),
            concept("datatype"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("function"),
            concept("interface"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("datatype"),
            concept("datatype"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("datatype"),
            concept("interface"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("interface"),
            concept("interface"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("interface"),
            concept("datatype"),
        ),
        (
            EdgeRelation::DependsOn,
            concept("module"),
            concept("module"),
        ),
        (
            EdgeRelation::Contains,
            EndpointKind::EntityOfKind("project"),
            concept("module"),
        ),
        (
            EdgeRelation::Contains,
            EndpointKind::EntityOfKind("project"),
            concept("function"),
        ),
        (
            EdgeRelation::Contains,
            EndpointKind::EntityOfKind("project"),
            concept("datatype"),
        ),
        (
            EdgeRelation::Contains,
            EndpointKind::EntityOfKind("project"),
            concept("interface"),
        ),
        (
            EdgeRelation::Implements,
            concept("datatype"),
            concept("interface"),
        ),
        (
            EdgeRelation::Implements,
            concept("function"),
            EndpointKind::EntityOfKind("concept"),
        ),
        (
            EdgeRelation::Implements,
            concept("datatype"),
            EndpointKind::EntityOfKind("concept"),
        ),
        (
            EdgeRelation::Implements,
            concept("module"),
            EndpointKind::EntityOfKind("concept"),
        ),
        (EdgeRelation::Contains, concept("module"), concept("module")),
        (
            EdgeRelation::Contains,
            concept("module"),
            concept("function"),
        ),
        (
            EdgeRelation::Contains,
            concept("module"),
            concept("datatype"),
        ),
        (
            EdgeRelation::Contains,
            concept("module"),
            concept("interface"),
        ),
        (
            EdgeRelation::Extends,
            concept("interface"),
            concept("interface"),
        ),
        (
            EdgeRelation::Extends,
            concept("datatype"),
            concept("datatype"),
        ),
    ];
    assert_eq!(
        expected.len(),
        22,
        "expected fixture itself must list all 22 ADR-085 triples"
    );

    for (relation, source, target) in &expected {
        assert!(
            rules
                .iter()
                .any(|r| r.relation == *relation && r.source == *source && r.target == *target),
            "missing ADR-085 rule {relation:?} {source:?} -> {target:?}"
        );
    }
}

async fn create_concept(registry: &VerbRegistry, name: &str, entity_type: &str) -> String {
    let created = dispatch(
        registry,
        "create",
        json!({"kind": "entity", "name": name, "entity_kind": "concept", "entity_type": entity_type}),
    )
    .await
    .unwrap_or_else(|e| panic!("create {entity_type} entity must succeed: {e}"));
    created["id"]
        .as_str()
        .expect("created entity has id")
        .to_string()
}

async fn create_project(registry: &VerbRegistry, name: &str) -> String {
    let created = dispatch(
        registry,
        "create",
        json!({"kind": "entity", "name": name, "entity_kind": "project"}),
    )
    .await
    .unwrap_or_else(|e| panic!("create project entity must succeed: {e}"));
    created["id"]
        .as_str()
        .expect("created entity has id")
        .to_string()
}

#[tokio::test]
async fn link_accepts_function_depends_on_datatype_with_code_pack() {
    let reg = registry(rt());
    let src = create_concept(&reg, "DoTheThing", "function").await;
    let tgt = create_concept(&reg, "ThingRecord", "datatype").await;
    let result = dispatch(
        &reg,
        "link",
        json!({"source_id": src, "target_id": tgt, "relation": "depends_on", "weight": 1.0}),
    )
    .await;
    assert!(
        result.is_ok(),
        "function depends_on datatype must be accepted with kg,code registered: {result:?}"
    );
}

#[tokio::test]
async fn link_accepts_project_contains_module_with_code_pack() {
    let reg = registry(rt());
    let src = create_project(&reg, "khive-pack-code").await;
    let tgt = create_concept(&reg, "ingest", "module").await;
    let result = dispatch(
        &reg,
        "link",
        json!({"source_id": src, "target_id": tgt, "relation": "contains", "weight": 1.0}),
    )
    .await;
    assert!(
        result.is_ok(),
        "project contains module must be accepted with kg,code registered: {result:?}"
    );
}

#[tokio::test]
async fn link_rejects_function_depends_on_datatype_without_code_pack() {
    let reg = kg_only_registry(rt());
    let src = create_concept(&reg, "DoTheThing", "function").await;
    let tgt = create_concept(&reg, "ThingRecord", "datatype").await;
    let err = dispatch(
        &reg,
        "link",
        json!({"source_id": src, "target_id": tgt, "relation": "depends_on", "weight": 1.0}),
    )
    .await
    .expect_err("function depends_on datatype must be rejected without the code pack");
    assert!(
        matches!(err, RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

#[tokio::test]
async fn link_rejects_function_depends_on_project_with_code_pack() {
    let reg = registry(rt());
    let src = create_concept(&reg, "DoTheThing", "function").await;
    let tgt = create_project(&reg, "khive-pack-code").await;
    let err = dispatch(
        &reg,
        "link",
        json!({"source_id": src, "target_id": tgt, "relation": "depends_on", "weight": 1.0}),
    )
    .await
    .expect_err("function depends_on project has no ADR-085 rule and must remain rejected");
    assert!(
        matches!(err, RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

#[tokio::test]
async fn create_finding_defaults_kind_status_open() {
    let reg = registry(rt());
    let resp = dispatch(
        &reg,
        "create",
        json!({
            "kind": "finding",
            "title": "Missing bounds check",
            "properties": {"severity": "high", "confidence": "high"},
        }),
    )
    .await
    .expect("create(kind=finding) with valid severity/confidence must succeed");
    assert_eq!(resp["properties"]["kind_status"], "open");
}

#[tokio::test]
async fn create_finding_rejects_invalid_severity_with_valid_values() {
    let reg = registry(rt());
    let err = dispatch(
        &reg,
        "create",
        json!({
            "kind": "finding",
            "title": "Missing bounds check",
            "properties": {"severity": "catastrophic", "confidence": "high"},
        }),
    )
    .await
    .expect_err("invalid severity must be rejected");
    let msg = err.to_string();
    assert!(
        msg.contains("critical, high, medium, low, info"),
        "error must name valid severities, got: {msg}"
    );
}

#[tokio::test]
async fn create_finding_rejects_invalid_confidence_with_valid_values() {
    let reg = registry(rt());
    let err = dispatch(
        &reg,
        "create",
        json!({
            "kind": "finding",
            "title": "Missing bounds check",
            "properties": {"severity": "high", "confidence": "sorta"},
        }),
    )
    .await
    .expect_err("invalid confidence must be rejected");
    let msg = err.to_string();
    assert!(
        msg.contains("high, medium, low"),
        "error must name valid confidences, got: {msg}"
    );
}

#[tokio::test]
async fn create_finding_rejects_non_object_properties() {
    let reg = registry(rt());
    let err = dispatch(
        &reg,
        "create",
        json!({
            "kind": "finding",
            "title": "Missing bounds check",
            "properties": "high",
        }),
    )
    .await
    .expect_err("a present non-object properties must be rejected, not coerced");
    let msg = err.to_string();
    assert!(
        msg.contains("properties must be an object"),
        "error must name the properties-shape requirement, got: {msg}"
    );
}

fn valid_findings_bytes() -> Vec<u8> {
    serde_json::to_vec(&sample_findings_json()).expect("sample findings.json serializes")
}

#[test]
fn ingest_findings_json_same_input_same_ids() {
    let bytes = valid_findings_bytes();
    let opts_a = ingest_options(Some("fixed-run"));
    let opts_b = ingest_options(Some("fixed-run"));

    let batch_a = ingest_findings_json(&bytes, opts_a).expect("first ingest must succeed");
    let batch_b = ingest_findings_json(&bytes, opts_b).expect("second ingest must succeed");

    assert_eq!(batch_a.entities.len(), 1);
    assert_eq!(batch_b.entities.len(), 1);
    assert_eq!(batch_a.entities[0].id, batch_b.entities[0].id);

    assert_eq!(batch_a.notes.len(), batch_b.notes.len());
    for (a, b) in batch_a.notes.iter().zip(batch_b.notes.iter()) {
        assert_eq!(
            a.id, b.id,
            "re-ingesting the same findings.json must reproduce the same note id"
        );
    }

    assert_eq!(batch_a.edges.len(), batch_b.edges.len());
    for (a, b) in batch_a.edges.iter().zip(batch_b.edges.iter()) {
        assert_eq!(
            a.id, b.id,
            "re-ingesting the same findings.json must reproduce the same edge id"
        );
    }
}

#[test]
fn ingest_findings_json_same_ids_across_different_observed_at() {
    let bytes = valid_findings_bytes();
    let early: DateTime<Utc> = "2020-01-01T00:00:00Z".parse().expect("valid rfc3339");
    let late: DateTime<Utc> = "2030-06-15T12:30:00Z".parse().expect("valid rfc3339");

    let batch_early = ingest_findings_json(
        &bytes,
        CodeIngestOptions {
            namespace: "local",
            observed_at: early,
            source_run: Some("fixed-run"),
        },
    )
    .expect("ingest at early timestamp must succeed");
    let batch_late = ingest_findings_json(
        &bytes,
        CodeIngestOptions {
            namespace: "local",
            observed_at: late,
            source_run: Some("fixed-run"),
        },
    )
    .expect("ingest at late timestamp must succeed");

    assert_eq!(
        batch_early.entities[0].id, batch_late.entities[0].id,
        "observed_at must be excluded from identity"
    );
    assert_eq!(batch_early.notes[0].id, batch_late.notes[0].id);
    assert_eq!(batch_early.edges[0].id, batch_late.edges[0].id);
}

#[test]
fn ingest_different_content_produces_different_finding_id() {
    // Same external id/title/evidence path as `sample_findings_json`, but a
    // changed content field (severity). The finding id must diverge — a
    // content change must never collide with the prior record's id.
    let bytes_a = valid_findings_bytes();
    let mut doc_b = sample_findings_json();
    doc_b["findings"][0]["severity"] = json!("critical");
    let bytes_b = serde_json::to_vec(&doc_b).expect("serializes");

    let batch_a = ingest_findings_json(&bytes_a, ingest_options(Some("fixed-run")))
        .expect("first ingest must succeed");
    let batch_b = ingest_findings_json(&bytes_b, ingest_options(Some("fixed-run")))
        .expect("second ingest must succeed");

    assert_ne!(
        batch_a.notes[0].id, batch_b.notes[0].id,
        "changing finding content (severity) must produce a different finding id"
    );
    assert_ne!(
        batch_a.edges[0].id, batch_b.edges[0].id,
        "the finding's annotate-edge id derives from the finding id and must also diverge"
    );
    // The unrelated project entity is scoped by repo/scope only, so it is
    // unaffected by a finding-level content change — the old record's
    // entity is left untouched, as the amendment requires.
    assert_eq!(batch_a.entities[0].id, batch_b.entities[0].id);
}

#[test]
fn ingest_maps_producer_status_fixed_to_kind_status_resolved() {
    // ADR-085 Amendment 1 A2: the pack-owned mapper resolves the governed
    // producer status vocabulary, not the raw passthrough this replaces.
    let mut doc = sample_findings_json();
    doc["findings"][0]["status"] = json!("fixed");
    let bytes = serde_json::to_vec(&doc).expect("serializes");

    let batch =
        ingest_findings_json(&bytes, ingest_options(Some("run"))).expect("ingest must succeed");
    let props = batch.notes[0]
        .properties
        .as_ref()
        .expect("finding note must carry properties");
    assert_eq!(
        props["kind_status"],
        json!("resolved"),
        "producer status 'fixed' must normalize to kind_status 'resolved'"
    );
    assert_eq!(
        props["audit_status"],
        json!("fixed"),
        "the raw producer value must be preserved verbatim under audit_status"
    );
}

#[test]
fn ingest_maps_producer_status_false_positive_to_kind_status_invalid() {
    let mut doc = sample_findings_json();
    doc["findings"][0]["status"] = json!("false_positive");
    let bytes = serde_json::to_vec(&doc).expect("serializes");

    let batch =
        ingest_findings_json(&bytes, ingest_options(Some("run"))).expect("ingest must succeed");
    let props = batch.notes[0]
        .properties
        .as_ref()
        .expect("finding note must carry properties");
    assert_eq!(
        props["kind_status"],
        json!("invalid"),
        "producer status 'false_positive' must normalize to kind_status 'invalid'"
    );
    assert_eq!(props["audit_status"], json!("false_positive"));
}

#[test]
fn ingest_leaves_kind_status_open_for_ungoverned_producer_status() {
    // "open" (and any other value outside the governed set, e.g. "wontfix"
    // as a producer term) has no mapping rule and must fall back to the
    // finding lifecycle's initial state, per Amendment 1 A2.
    let batch = ingest_findings_json(&valid_findings_bytes(), ingest_options(Some("run")))
        .expect("ingest must succeed");
    let props = batch.notes[0]
        .properties
        .as_ref()
        .expect("finding note must carry properties");
    assert_eq!(props["kind_status"], json!("open"));
    assert_eq!(props["audit_status"], json!("open"));
}

#[test]
fn ingest_rejects_missing_findings_array() {
    let malformed = json!({
        "audit": {
            "date": "2026-07-07",
            "scope": "khive-pack-code",
            "repo": "khive",
            "branch": "feature/code-pack",
            "commit": "abc123",
            "standards_file": "audit-guidelines.md",
        },
    });
    let bytes = serde_json::to_vec(&malformed).expect("serializes");
    let err = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect_err("missing findings array must be rejected");
    assert!(
        matches!(err, CodeIngestError::InvalidRoot),
        "expected InvalidRoot, got: {err:?}"
    );
}

#[test]
fn ingest_rejects_invalid_confidence_before_records() {
    let mut doc = sample_findings_json();
    doc["findings"][0]["confidence"] = json!("sorta");
    let bytes = serde_json::to_vec(&doc).expect("serializes");
    let err = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect_err("invalid confidence must be rejected before any record is built");
    match err {
        CodeIngestError::InvalidValue { field, valid, .. } => {
            assert_eq!(field, "confidence");
            assert_eq!(valid, "high | medium | low");
        }
        other => panic!("expected InvalidValue{{field: confidence}}, got: {other:?}"),
    }
}

#[test]
fn ingest_rejects_invalid_severity_before_records() {
    let mut doc = sample_findings_json();
    doc["findings"][0]["severity"] = json!("catastrophic");
    let bytes = serde_json::to_vec(&doc).expect("serializes");
    let err = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect_err("invalid severity must be rejected before any record is built");
    match err {
        CodeIngestError::InvalidValue { field, valid, .. } => {
            assert_eq!(field, "severity");
            assert_eq!(valid, "critical | high | medium | low | info");
        }
        other => panic!("expected InvalidValue{{field: severity}}, got: {other:?}"),
    }
}

#[test]
fn ingest_tolerates_out_of_vocab_priority() {
    // ADR-085 Amendment 1 A1: `priority` is ungoverned — ingest neither
    // rejects nor coerces it, it preserves whatever value was provided.
    let mut doc = sample_findings_json();
    doc["findings"][0]["priority"] = json!("P9");
    let bytes = serde_json::to_vec(&doc).expect("serializes");
    let batch = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect("out-of-vocab priority must be tolerated, not rejected");
    assert_eq!(
        batch.notes[0].properties.as_ref().expect("has properties")["priority"],
        json!("P9"),
        "tolerated priority value must be preserved as-is"
    );
}

#[test]
fn ingest_rejects_invalid_evidence_shape() {
    let mut doc = sample_findings_json();
    doc["findings"][0]["evidence"] = json!([42]);
    let bytes = serde_json::to_vec(&doc).expect("serializes");
    let err = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect_err("evidence entries that are neither string nor object must be rejected");
    assert!(
        matches!(err, CodeIngestError::InvalidEvidence { .. }),
        "expected InvalidEvidence, got: {err:?}"
    );
}

#[test]
fn ingest_requires_failure_scenario_for_medium_or_higher() {
    let mut doc = sample_findings_json();
    doc["findings"][0]["severity"] = json!("medium");
    doc["findings"][0]
        .as_object_mut()
        .expect("finding is an object")
        .remove("failure_scenario");
    let bytes = serde_json::to_vec(&doc).expect("serializes");
    let err = ingest_findings_json(&bytes, ingest_options(Some("run")))
        .expect_err("medium severity without failure_scenario must be rejected");
    match err {
        CodeIngestError::MissingFailureScenario { severity, .. } => {
            assert_eq!(severity, "medium");
        }
        other => panic!("expected MissingFailureScenario, got: {other:?}"),
    }
}