kmp-application 0.1.8

Application services of the KMP kernel: the use cases behind ingest, wake, ask, near, rewind and trace
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
use std::collections::BTreeMap;

use kmp_domain::{
    BundleQualityMetrics, KmpBundle, KmpMode, ResolutionTier, TierBudget, TokenEstimator,
};

use crate::queries::ContextRenderOptions;
use crate::queries::bundle_section_renderer::ordered_sections;
use crate::queries::bundle_truncator::{TruncationMetadata, limit_sections_by_tier_budget};
use crate::queries::cl100k_estimator::Cl100kEstimator;
use crate::queries::mode_heuristic::resolve_mode;
use crate::queries::tier_section_classifier::classify_into_tiers;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderedSection {
    pub content: String,
    pub token_count: u32,
    /// Source node or relationship ID that originated this section.
    pub source_id: String,
}

/// A rendered tier with its sections and token count.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderedTier {
    pub tier: ResolutionTier,
    pub content: String,
    pub token_count: u32,
    pub sections: Vec<RenderedSection>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RenderedContext {
    pub content: String,
    /// SHA-256 hash of `content` — verifies the LLM received exactly what the kernel rendered.
    pub content_hash: String,
    pub token_count: u32,
    pub sections: Vec<RenderedSection>,
    pub truncation: Option<TruncationMetadata>,
    /// Multi-resolution tiers (L0 Summary, L1 Causal Spine, L2 Evidence Pack).
    pub tiers: Vec<RenderedTier>,
    /// The mode that was actually used for tiered rendering.
    pub resolved_mode: KmpMode,
    /// Quality and efficiency metrics for this render.
    pub quality: BundleQualityMetrics,
}

pub fn render_graph_bundle(bundle: &KmpBundle) -> RenderedContext {
    render_graph_bundle_with_estimator(
        bundle,
        &ContextRenderOptions::default(),
        &Cl100kEstimator::new(),
    )
}

pub fn render_graph_bundle_with_options(
    bundle: &KmpBundle,
    options: &ContextRenderOptions,
) -> RenderedContext {
    render_graph_bundle_with_estimator(bundle, options, &Cl100kEstimator::new())
}

pub fn render_graph_bundle_with_estimator(
    bundle: &KmpBundle,
    options: &ContextRenderOptions,
    estimator: &dyn TokenEstimator,
) -> RenderedContext {
    let detail_by_node_id = bundle
        .node_details()
        .iter()
        .map(|detail| (detail.node_id(), detail))
        .collect::<BTreeMap<_, _>>();

    // ── Resolve mode first (needed by both flat and tiered paths) ──
    let resolved_mode = resolve_mode(
        options.rehydration_mode,
        bundle,
        options.token_budget,
        options.focus_node_id.as_deref(),
        options.endpoint_hint,
    );

    // ── Flat rendering (tier-aware truncation) ─────────────────────
    let all_sections = ordered_sections(bundle, &detail_by_node_id, options);

    let (section_pairs, truncation) =
        limit_sections_by_tier_budget(all_sections, options.token_budget, resolved_mode, estimator);

    let content = section_pairs
        .iter()
        .map(|(s, _)| s.as_str())
        .collect::<Vec<_>>()
        .join("\n\n");
    let token_count = estimator.estimate_tokens(&content);

    let sections = section_pairs
        .into_iter()
        .map(|(s, source_id)| {
            let tc = estimator.estimate_tokens(&s);
            RenderedSection {
                content: s,
                token_count: tc,
                source_id,
            }
        })
        .collect();

    // ── Tiered rendering ────────────────────────────────────────────
    let tiered_sections = classify_into_tiers(bundle, &detail_by_node_id, options, resolved_mode);
    let tier_budget = options
        .token_budget
        .map(|total| TierBudget::from_total_with_mode(total, resolved_mode))
        .unwrap_or_else(TierBudget::unlimited);

    let tiers = build_rendered_tiers(tiered_sections, &tier_budget, estimator);

    // ── Quality metrics (domain value object) ────────────────────────
    let quality = BundleQualityMetrics::compute(bundle, token_count, estimator);

    let content_hash = render_content_hash(&content);

    RenderedContext {
        content,
        content_hash,
        token_count,
        sections,
        truncation,
        tiers,
        resolved_mode,
        quality,
    }
}

fn build_rendered_tiers(
    tiered_sections: Vec<crate::queries::tier_section_classifier::TieredSection>,
    budget: &TierBudget,
    estimator: &dyn TokenEstimator,
) -> Vec<RenderedTier> {
    let mut tiers = Vec::new();

    for &tier in ResolutionTier::all() {
        let tier_budget = match tier {
            ResolutionTier::L0Summary => budget.l0,
            ResolutionTier::L1CausalSpine => budget.l1,
            ResolutionTier::L2EvidencePack => budget.l2,
        };

        let mut tier_sections = Vec::new();
        let mut tier_tokens = 0u32;

        for ts in &tiered_sections {
            if ts.tier != tier {
                continue;
            }
            let section_tokens = estimator.estimate_tokens(&ts.content);
            if tier_budget < u32::MAX
                && !tier_sections.is_empty()
                && tier_tokens + section_tokens > tier_budget
            {
                break;
            }
            tier_tokens += section_tokens;
            tier_sections.push(RenderedSection {
                content: ts.content.clone(),
                token_count: section_tokens,
                source_id: format!("tier:{}", ts.tier.as_str()),
            });
        }

        if !tier_sections.is_empty() {
            let tier_content = tier_sections
                .iter()
                .map(|s| s.content.as_str())
                .collect::<Vec<_>>()
                .join("\n\n");
            let actual_tokens = estimator.estimate_tokens(&tier_content);

            tiers.push(RenderedTier {
                tier,
                content: tier_content,
                token_count: actual_tokens,
                sections: tier_sections,
            });
        }
    }

    tiers
}

/// Deterministic SHA-256 hash of rendered content for audit trail.
/// Stable across process restarts, machines, and Rust versions.
fn render_content_hash(content: &str) -> String {
    use sha2::{Digest, Sha256};
    let hash = Sha256::digest(content.as_bytes());
    format!("render:{:064x}", hash)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use kmp_domain::{
        BundleMetadata, BundleNode, BundleNodeDetail, BundleQualityMetrics, BundleRelationship,
        CaseId, KmpBundle, Provenance, RelationExplanation, RelationSemanticClass, ResolutionTier,
        Role, SourceKind,
    };

    /// Budget so tight it forces truncation on any multi-section bundle.
    const TINY_BUDGET: u32 = 10;
    /// Generous budget that fits the sample bundle without truncation.
    const GENEROUS_BUDGET: u32 = 1000;

    use crate::queries::ContextRenderOptions;

    use super::{render_graph_bundle, render_graph_bundle_with_options};

    #[test]
    fn render_graph_bundle_orders_root_relationships_neighbors_and_details() {
        let bundle = KmpBundle::new(
            CaseId::new("case-123").expect("case id is valid"),
            Role::new("developer").expect("role is valid"),
            BundleNode::new(
                "case-123",
                "case",
                "Root",
                "Root summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![BundleNode::new(
                "node-1",
                "decision",
                "Neighbor",
                "Neighbor summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            )],
            vec![BundleRelationship::new(
                "case-123",
                "node-1",
                "RELATES_TO",
                RelationExplanation::new(RelationSemanticClass::Structural),
            )],
            vec![BundleNodeDetail::new(
                "case-123",
                "Expanded detail",
                "hash-1",
                2,
            )],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("bundle should be valid");

        let rendered = render_graph_bundle(&bundle);

        assert_eq!(rendered.sections.len(), 4);
        assert!(rendered.sections[0].content.starts_with("Node Root"));
        assert!(rendered.sections[1].content.starts_with("Relationship"));
        assert!(rendered.sections[2].content.starts_with("Node Neighbor"));
        assert!(rendered.sections[3].content.starts_with("Detail case-123"));
    }

    #[test]
    fn render_graph_bundle_prioritizes_focused_node_sections() {
        let bundle = sample_bundle();

        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                focus_node_id: Some("node-2".to_string()),
                token_budget: None,
                ..Default::default()
            },
        );

        assert!(rendered.sections[0].content.starts_with("Node Root"));
        assert!(rendered.sections[1].content.starts_with("Node Focused"));
        assert!(rendered.sections[2].content.contains("node-2"));
    }

    #[test]
    fn render_graph_bundle_respects_token_budget_after_reordering() {
        let bundle = sample_bundle();

        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                focus_node_id: Some("node-2".to_string()),
                token_budget: Some(TINY_BUDGET),
                ..Default::default()
            },
        );

        assert!(
            rendered.sections.len() < 7,
            "budget should truncate sections"
        );
        assert!(rendered.content.starts_with("Node Root"));
        let truncation = rendered
            .truncation
            .as_ref()
            .expect("should have truncation metadata");
        assert_eq!(truncation.budget_requested, TINY_BUDGET);
        assert!(truncation.sections_dropped > 0);
        assert_eq!(truncation.token_estimator, "cl100k_base");
    }

    #[test]
    fn render_graph_bundle_uses_cl100k_base_estimator() {
        let bundle = KmpBundle::new(
            CaseId::new("case-1").expect("case id is valid"),
            Role::new("dev").expect("role is valid"),
            BundleNode::new(
                "case-1",
                "case",
                "Root",
                "",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("bundle should be valid");

        let rendered = render_graph_bundle(&bundle);
        assert!(rendered.token_count > 0);
        assert!(rendered.token_count < 20);
    }

    fn sample_bundle() -> KmpBundle {
        KmpBundle::new(
            CaseId::new("case-123").expect("case id is valid"),
            Role::new("developer").expect("role is valid"),
            BundleNode::new(
                "case-123",
                "case",
                "Root",
                "Root summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new(
                    "node-1",
                    "decision",
                    "Neighbor",
                    "Neighbor summary",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "node-2",
                    "task",
                    "Focused",
                    "Focused summary",
                    "READY",
                    vec![],
                    BTreeMap::new(),
                ),
            ],
            vec![
                BundleRelationship::new(
                    "case-123",
                    "node-1",
                    "RELATES_TO",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
                BundleRelationship::new(
                    "case-123",
                    "node-2",
                    "HAS_TASK",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
            ],
            vec![
                BundleNodeDetail::new("case-123", "Expanded detail", "hash-1", 2),
                BundleNodeDetail::new("node-2", "Focused detail", "hash-2", 3),
            ],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("bundle should be valid")
    }

    #[test]
    fn render_graph_bundle_includes_explanatory_relation_metadata() {
        let bundle = KmpBundle::new(
            CaseId::new("case-123").expect("case id is valid"),
            Role::new("developer").expect("role is valid"),
            BundleNode::new(
                "case-123",
                "case",
                "Root",
                "Root summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![BundleNode::new(
                "node-1",
                "task",
                "Neighbor",
                "Neighbor summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            )],
            vec![BundleRelationship::new(
                "case-123",
                "node-1",
                "AUTHORIZES",
                RelationExplanation::new(RelationSemanticClass::Motivational)
                    .with_rationale("reserve power must be diverted before repair")
                    .with_decision_id("decision-1")
                    .with_sequence(1),
            )],
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("bundle should be valid");

        let rendered = render_graph_bundle(&bundle);

        assert!(rendered.content.contains("[motivational]"));
        assert!(
            rendered
                .content
                .contains("because reserve power must be diverted before repair")
        );
        assert!(rendered.content.contains("decision=decision-1"));
        assert!(rendered.content.contains("step=1"));
    }

    #[test]
    fn render_without_budget_has_no_truncation_metadata() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle(&bundle);
        assert!(rendered.truncation.is_none());
    }

    #[test]
    fn render_with_budget_reports_truncation_metadata() {
        // Use a causal bundle so the planner stays ReasonPreserving at generous budget
        // (structural bundles now trigger ResumeFocused which drops L2).
        let bundle = quality_bundle();
        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                focus_node_id: None,
                token_budget: Some(GENEROUS_BUDGET),
                ..Default::default()
            },
        );
        let truncation = rendered
            .truncation
            .expect("budget should produce truncation");
        assert_eq!(truncation.budget_requested, GENEROUS_BUDGET);
        assert_eq!(truncation.sections_dropped, 0);
        assert_eq!(truncation.token_estimator, "cl100k_base");
        assert!(truncation.budget_used <= GENEROUS_BUDGET);
    }

    #[test]
    fn causal_relationships_render_before_structural() {
        let bundle = KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "case",
                "Root",
                "",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new("a", "task", "A", "", "ACTIVE", vec![], BTreeMap::new()),
                BundleNode::new("b", "task", "B", "", "ACTIVE", vec![], BTreeMap::new()),
            ],
            vec![
                BundleRelationship::new(
                    "root",
                    "a",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
                BundleRelationship::new(
                    "root",
                    "b",
                    "CAUSED",
                    RelationExplanation::new(RelationSemanticClass::Causal)
                        .with_rationale("failure triggered reroute"),
                ),
            ],
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let rendered = render_graph_bundle(&bundle);

        // Causal relationship must appear before structural in rendered output
        let causal_pos = rendered
            .content
            .find("[causal]")
            .expect("causal should be present");
        let structural_pos = rendered
            .content
            .find("[structural]")
            .expect("structural should be present");
        assert!(
            causal_pos < structural_pos,
            "causal ({causal_pos}) must render before structural ({structural_pos})"
        );
    }

    #[test]
    fn section_token_counts_use_cl100k_base_not_whitespace() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle(&bundle);

        for section in &rendered.sections {
            // Whitespace count and cl100k_base count differ for most text.
            // The important invariant: token_count is computed by cl100k_base,
            // NOT by split_whitespace. For structured text like "Node Root (case):
            // Root summary", cl100k_base produces fewer tokens than words.
            let whitespace_count = section.content.split_whitespace().count() as u32;
            // cl100k_base should differ from whitespace count for most sections
            // (they're not equal for structured text). At minimum, token_count > 0.
            assert!(
                section.token_count > 0,
                "section token_count should be positive"
            );
            // The key test: the section token_count should NOT equal whitespace count
            // for sections with punctuation (which is most of ours)
            if section.content.contains('(') || section.content.contains('[') {
                assert_ne!(
                    section.token_count,
                    whitespace_count,
                    "section '{}' token_count {} should differ from whitespace count {} \
                     (proves cl100k_base, not split_whitespace)",
                    &section.content[..section.content.len().min(40)],
                    section.token_count,
                    whitespace_count
                );
            }
        }
    }

    #[test]
    fn render_produces_three_tiers() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle(&bundle);

        assert!(
            rendered.tiers.len() >= 2,
            "should have at least L0 and L1 tiers, got {}",
            rendered.tiers.len()
        );
        assert_eq!(rendered.tiers[0].tier, ResolutionTier::L0Summary);
        assert!(rendered.tiers[0].content.contains("Objective:"));
    }

    #[test]
    fn tiers_and_flat_content_are_both_populated() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle(&bundle);

        assert!(!rendered.content.is_empty());
        assert!(!rendered.tiers.is_empty());
        assert!(rendered.token_count > 0);
        for tier in &rendered.tiers {
            assert!(tier.token_count > 0);
            assert!(!tier.sections.is_empty());
        }
    }

    #[test]
    fn max_tier_l0_only_produces_single_tier() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                max_tier: Some(ResolutionTier::L0Summary),
                ..Default::default()
            },
        );

        let tier_types: Vec<_> = rendered.tiers.iter().map(|t| t.tier).collect();
        assert_eq!(tier_types, vec![ResolutionTier::L0Summary]);
    }

    #[test]
    fn max_tier_l1_excludes_evidence_pack() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                max_tier: Some(ResolutionTier::L1CausalSpine),
                ..Default::default()
            },
        );

        assert!(
            rendered
                .tiers
                .iter()
                .all(|t| t.tier != ResolutionTier::L2EvidencePack)
        );
        assert!(
            rendered
                .tiers
                .iter()
                .any(|t| t.tier == ResolutionTier::L1CausalSpine)
        );
    }

    #[test]
    fn tier_budget_constrains_l1_token_count() {
        let bundle = sample_bundle();
        let rendered = render_graph_bundle_with_options(
            &bundle,
            &ContextRenderOptions {
                token_budget: Some(200),
                ..Default::default()
            },
        );

        // With budget=200, L0 gets ~100, L1 gets ~100 — L1 should be truncated
        if let Some(l1) = rendered
            .tiers
            .iter()
            .find(|t| t.tier == ResolutionTier::L1CausalSpine)
        {
            assert!(
                l1.token_count <= 120,
                "L1 should be constrained by tier budget, got {} tokens",
                l1.token_count
            );
        }
    }

    #[test]
    fn render_includes_provenance_when_present() {
        let bundle = KmpBundle::new(
            CaseId::new("case-1").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "case-1",
                "incident",
                "Root",
                "Outage",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            )
            .with_provenance(
                Provenance::new(SourceKind::Agent)
                    .with_source_agent("diagnostics-agent")
                    .with_observed_at("2026-03-25T14:00:00Z"),
            ),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let rendered = render_graph_bundle(&bundle);

        assert!(
            rendered.content.contains("[source:agent"),
            "rendered should include provenance source kind"
        );
        assert!(
            rendered.content.contains("agent=diagnostics-agent"),
            "rendered should include source agent"
        );
        assert!(
            rendered.content.contains("observed=2026-03-25T14:00:00Z"),
            "rendered should include observed_at"
        );
    }

    #[test]
    fn render_omits_provenance_when_absent() {
        let bundle = KmpBundle::new(
            CaseId::new("case-1").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "case-1",
                "incident",
                "Root",
                "Outage",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let rendered = render_graph_bundle(&bundle);

        assert!(
            !rendered.content.contains("[source:"),
            "rendered should NOT include provenance bracket when absent"
        );
    }

    // ── Quality metrics tests ───────────────────────────────────────────

    fn quality_bundle() -> KmpBundle {
        KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "incident",
                "Root",
                "Root summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new(
                    "node-a",
                    "decision",
                    "Decision A",
                    "Decision summary",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "noise-1",
                    "task",
                    "Noise node",
                    "Distractor summary",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
            ],
            vec![
                BundleRelationship::new(
                    "root",
                    "node-a",
                    "CAUSED",
                    RelationExplanation::new(RelationSemanticClass::Causal)
                        .with_rationale("failure triggered reroute")
                        .with_caused_by_node_id("root"),
                ),
                BundleRelationship::new(
                    "root",
                    "noise-1",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
            ],
            vec![BundleNodeDetail::new(
                "root",
                "Extended root detail",
                "hash-r",
                1,
            )],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid")
    }

    #[test]
    fn quality_raw_equivalent_tokens_is_positive() {
        let rendered = render_graph_bundle(&quality_bundle());
        assert!(
            rendered.quality.raw_equivalent_tokens() > 0,
            "raw_equivalent_tokens should be positive"
        );
    }

    #[test]
    fn quality_compression_ratio_reflects_raw_vs_rendered() {
        let rendered = render_graph_bundle(&quality_bundle());
        let expected =
            rendered.quality.raw_equivalent_tokens() as f64 / rendered.token_count as f64;
        let diff = (rendered.quality.compression_ratio() - expected).abs();
        assert!(
            diff < 0.001,
            "compression_ratio {:.4} should equal raw/rendered {:.4}",
            rendered.quality.compression_ratio(),
            expected
        );
    }

    #[test]
    fn quality_causal_density_counts_explanatory_relations() {
        let rendered = render_graph_bundle(&quality_bundle());
        // 1 Causal out of 2 total → 0.5
        let diff = (rendered.quality.causal_density() - 0.5).abs();
        assert!(
            diff < 0.001,
            "causal_density should be 0.5, got {:.4}",
            rendered.quality.causal_density()
        );
    }

    #[test]
    fn quality_noise_ratio_detects_noise_nodes() {
        let rendered = render_graph_bundle(&quality_bundle());
        // 1 noise node out of 3 total (root + 2 neighbors) → 1/3
        let expected = 1.0 / 3.0;
        let diff = (rendered.quality.noise_ratio() - expected).abs();
        assert!(
            diff < 0.001,
            "noise_ratio should be {:.4}, got {:.4}",
            expected,
            rendered.quality.noise_ratio()
        );
    }

    #[test]
    fn quality_detail_coverage_tracks_detail_presence() {
        let rendered = render_graph_bundle(&quality_bundle());
        // 1 detail (root) out of 3 nodes → 1/3
        let expected = 1.0 / 3.0;
        let diff = (rendered.quality.detail_coverage() - expected).abs();
        assert!(
            diff < 0.001,
            "detail_coverage should be {:.4}, got {:.4}",
            expected,
            rendered.quality.detail_coverage()
        );
    }

    #[test]
    fn quality_raw_text_includes_caused_by_node_id() {
        use crate::queries::cl100k_estimator::Cl100kEstimator;

        let bundle = quality_bundle();
        let estimator = Cl100kEstimator::new();
        let metrics = BundleQualityMetrics::compute(&bundle, 100, &estimator);

        let bundle_no_caused_by = KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "incident",
                "Root",
                "Root summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![BundleNode::new(
                "node-a",
                "decision",
                "Decision A",
                "Decision summary",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            )],
            vec![BundleRelationship::new(
                "root",
                "node-a",
                "CAUSED",
                RelationExplanation::new(RelationSemanticClass::Causal)
                    .with_rationale("failure triggered reroute"),
            )],
            vec![BundleNodeDetail::new(
                "root",
                "Extended root detail",
                "hash-r",
                1,
            )],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let metrics2 = BundleQualityMetrics::compute(&bundle_no_caused_by, 100, &estimator);

        assert!(
            metrics.raw_equivalent_tokens() > metrics2.raw_equivalent_tokens(),
            "caused_by_node_id should increase raw tokens: with={} without={}",
            metrics.raw_equivalent_tokens(),
            metrics2.raw_equivalent_tokens()
        );
    }

    #[test]
    fn quality_all_causal_density_is_one() {
        let bundle = KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "case",
                "Root",
                "",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new("a", "task", "A", "", "ACTIVE", vec![], BTreeMap::new()),
                BundleNode::new("b", "task", "B", "", "ACTIVE", vec![], BTreeMap::new()),
            ],
            vec![
                BundleRelationship::new(
                    "root",
                    "a",
                    "CAUSED",
                    RelationExplanation::new(RelationSemanticClass::Causal),
                ),
                BundleRelationship::new(
                    "root",
                    "b",
                    "JUSTIFIED",
                    RelationExplanation::new(RelationSemanticClass::Evidential),
                ),
            ],
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let rendered = render_graph_bundle(&bundle);
        let diff = (rendered.quality.causal_density() - 1.0).abs();
        assert!(diff < 0.001, "all-causal density should be 1.0");
    }

    #[test]
    fn quality_no_relationships_has_zero_causal_density() {
        let bundle = KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "case",
                "Root",
                "",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            Vec::new(),
            Vec::new(),
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let rendered = render_graph_bundle(&bundle);
        assert!(
            rendered.quality.causal_density().abs() < 0.001,
            "no-relationship bundle should have 0 causal density"
        );
    }
}