kmp-application 0.1.3

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
use std::collections::{BTreeMap, BTreeSet};

use kmp_domain::{BundleNodeDetail, KmpBundle, KmpMode, RelationSemanticClass, ResolutionTier};

use crate::queries::ContextRenderOptions;
use crate::queries::bundle_section_renderer::{render_detail, render_node, render_relationship};

/// A rendered section tagged with its resolution tier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TieredSection {
    pub tier: ResolutionTier,
    pub content: String,
}

/// Classify bundle contents into resolution tiers.
///
/// Dispatches to mode-specific classification:
/// - `ResumeFocused`: only causal spine in L1, no L2 at all
/// - All other modes: default classification with L0/L1/L2
pub(crate) fn classify_into_tiers(
    bundle: &KmpBundle,
    detail_by_node_id: &BTreeMap<&str, &BundleNodeDetail>,
    options: &ContextRenderOptions,
    resolved_mode: KmpMode,
) -> Vec<TieredSection> {
    match resolved_mode {
        KmpMode::ResumeFocused => classify_resume_focused(bundle, detail_by_node_id, options),
        _ => classify_default(bundle, detail_by_node_id, options),
    }
}

/// Default classification: L0 summary, L1 causal spine, L2 evidence pack.
fn classify_default(
    bundle: &KmpBundle,
    detail_by_node_id: &BTreeMap<&str, &BundleNodeDetail>,
    options: &ContextRenderOptions,
) -> Vec<TieredSection> {
    let max_tier = options.max_tier.unwrap_or(ResolutionTier::L2EvidencePack);
    let mut sections = Vec::new();

    // ── L0 Summary ──────────────────────────────────────────────────
    sections.push(TieredSection {
        tier: ResolutionTier::L0Summary,
        content: render_l0_summary(bundle, options),
    });

    if max_tier < ResolutionTier::L1CausalSpine {
        return sections;
    }

    // ── L1 Causal Spine ─────────────────────────────────────────────
    // Root node
    sections.push(TieredSection {
        tier: ResolutionTier::L1CausalSpine,
        content: render_node(bundle.root_node()),
    });

    // Focus node (if different from root)
    let focus_node_id = find_focus_node(bundle, options);

    if let Some(focus_node) = focus_node_id {
        sections.push(TieredSection {
            tier: ResolutionTier::L1CausalSpine,
            content: render_node(focus_node),
        });
    }

    // Explanatory relationships (causal, motivational, evidential, constraint)
    let relationships = salience_sorted_relationships(bundle);
    append_l1_explanatory(&mut sections, &relationships);

    if max_tier < ResolutionTier::L2EvidencePack {
        return sections;
    }

    // ── L2 Evidence Pack ────────────────────────────────────────────
    let focus_id = focus_node_id.map(|n| n.node_id());
    append_l2_evidence(
        &mut sections,
        &relationships,
        bundle,
        detail_by_node_id,
        options,
        focus_id,
    );

    sections
}

fn salience_sorted_relationships(bundle: &KmpBundle) -> Vec<&kmp_domain::BundleRelationship> {
    let mut relationships: Vec<_> = bundle.relationships().iter().collect();
    relationships.sort_by_key(|r| r.explanation().semantic_class().salience_rank());
    relationships
}

fn find_focus_node<'a>(
    bundle: &'a KmpBundle,
    options: &'a ContextRenderOptions,
) -> Option<&'a kmp_domain::BundleNode> {
    options.focus_node_id.as_deref().and_then(|fid| {
        if fid != bundle.root_node().node_id() {
            bundle.neighbor_nodes().iter().find(|n| n.node_id() == fid)
        } else {
            None
        }
    })
}

fn append_l1_explanatory(
    sections: &mut Vec<TieredSection>,
    relationships: &[&kmp_domain::BundleRelationship],
) {
    for rel in relationships {
        if is_explanatory(rel.explanation().semantic_class()) {
            sections.push(TieredSection {
                tier: ResolutionTier::L1CausalSpine,
                content: render_relationship(rel),
            });
        }
    }
}

/// Appends L2 evidence pack sections: non-explanatory relationships,
/// remaining neighbor nodes, and prioritized details.
fn append_l2_evidence(
    sections: &mut Vec<TieredSection>,
    relationships: &[&kmp_domain::BundleRelationship],
    bundle: &KmpBundle,
    detail_by_node_id: &BTreeMap<&str, &BundleNodeDetail>,
    options: &ContextRenderOptions,
    focus_id: Option<&str>,
) {
    for rel in relationships {
        if !is_explanatory(rel.explanation().semantic_class()) {
            sections.push(TieredSection {
                tier: ResolutionTier::L2EvidencePack,
                content: render_relationship(rel),
            });
        }
    }

    for node in bundle.neighbor_nodes() {
        if Some(node.node_id()) != focus_id {
            sections.push(TieredSection {
                tier: ResolutionTier::L2EvidencePack,
                content: render_node(node),
            });
        }
    }

    let details = prioritized_details(bundle, options.focus_node_id.as_deref());
    for detail in details {
        sections.push(TieredSection {
            tier: ResolutionTier::L2EvidencePack,
            content: render_detail(detail, detail_by_node_id),
        });
    }
}

/// Resume-focused classification: only causal spine in L1, no L2.
///
/// Prunes all distractor/noise branches and structural-only relationships.
/// Keeps only nodes that participate in explanatory relationships.
fn classify_resume_focused(
    bundle: &KmpBundle,
    _detail_by_node_id: &BTreeMap<&str, &BundleNodeDetail>,
    options: &ContextRenderOptions,
) -> Vec<TieredSection> {
    let max_tier = options.max_tier.unwrap_or(ResolutionTier::L2EvidencePack);
    let mut sections = Vec::new();

    // L0: compact summary (same as default)
    sections.push(TieredSection {
        tier: ResolutionTier::L0Summary,
        content: render_l0_summary(bundle, options),
    });

    if max_tier < ResolutionTier::L1CausalSpine {
        return sections;
    }

    // L1: ONLY the causal spine

    // Root node
    sections.push(TieredSection {
        tier: ResolutionTier::L1CausalSpine,
        content: render_node(bundle.root_node()),
    });

    // Focus node (if different from root)
    let focus_node = options.focus_node_id.as_deref().and_then(|fid| {
        if fid != bundle.root_node().node_id() {
            bundle.neighbor_nodes().iter().find(|n| n.node_id() == fid)
        } else {
            None
        }
    });
    if let Some(focus_node) = focus_node {
        sections.push(TieredSection {
            tier: ResolutionTier::L1CausalSpine,
            content: render_node(focus_node),
        });
    }

    // Collect causal-spine node IDs from explanatory relationships
    let causal_node_ids: BTreeSet<&str> = bundle
        .relationships()
        .iter()
        .filter(|r| is_explanatory(r.explanation().semantic_class()))
        .flat_map(|r| [r.source_node_id(), r.target_node_id()])
        .collect();

    // Causal-spine neighbor nodes only (not distractors)
    let focus_id = focus_node.map(|n| n.node_id());
    for node in bundle.neighbor_nodes() {
        if Some(node.node_id()) != focus_id && causal_node_ids.contains(node.node_id()) {
            sections.push(TieredSection {
                tier: ResolutionTier::L1CausalSpine,
                content: render_node(node),
            });
        }
    }

    // ALL explanatory relationships (sorted by salience)
    let relationships = salience_sorted_relationships(bundle);
    append_l1_explanatory(&mut sections, &relationships);

    // NO L2. Distractors, structural relationships, and details are dropped entirely.
    // This trades completeness for causal chain preservation under token pressure.

    sections
}

/// Compact L0 summary: objective, status, blocker, next action.
fn render_l0_summary(bundle: &KmpBundle, options: &ContextRenderOptions) -> String {
    let root = bundle.root_node();
    let objective = if root.summary().trim().is_empty() {
        root.title().to_string()
    } else {
        format!("{}{}", root.title(), root.summary().trim())
    };

    let status = root.status();

    // Blocker: look for constraint relationships
    let blocker = bundle
        .relationships()
        .iter()
        .find(|r| r.explanation().semantic_class() == &RelationSemanticClass::Constraint)
        .and_then(|r| r.explanation().rationale())
        .unwrap_or("none identified");

    // Next action: highest-priority causal/motivational relationship
    let next_action = bundle
        .relationships()
        .iter()
        .filter(|r| {
            matches!(
                r.explanation().semantic_class(),
                RelationSemanticClass::Causal | RelationSemanticClass::Motivational
            )
        })
        .min_by_key(|r| r.explanation().semantic_class().salience_rank())
        .map(|r| {
            let target = r.target_node_id();
            let focus_label = if options.focus_node_id.as_deref() == Some(target) {
                " (focus)"
            } else {
                ""
            };
            format!("{}{}{}", r.relationship_type(), target, focus_label)
        })
        .unwrap_or_else(|| "continue".to_string());

    format!("Objective: {objective}\nStatus: {status}\nBlocker: {blocker}\nNext: {next_action}")
}

fn is_explanatory(class: &RelationSemanticClass) -> bool {
    matches!(
        class,
        RelationSemanticClass::Causal
            | RelationSemanticClass::Motivational
            | RelationSemanticClass::Evidential
            | RelationSemanticClass::Constraint
    )
}

fn prioritized_details<'a>(
    bundle: &'a KmpBundle,
    focus_node_id: Option<&str>,
) -> Vec<&'a BundleNodeDetail> {
    let Some(focus_node_id) = focus_node_id else {
        return bundle.node_details().iter().collect();
    };
    let (focused, remaining): (Vec<_>, Vec<_>) = bundle
        .node_details()
        .iter()
        .partition(|d| d.node_id() == focus_node_id);
    focused.into_iter().chain(remaining).collect()
}

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

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

    use crate::queries::ContextRenderOptions;

    use super::classify_into_tiers;

    fn sample_bundle() -> KmpBundle {
        KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "incident",
                "Incident Alpha",
                "System outage",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new(
                    "n1",
                    "decision",
                    "Decision",
                    "Recovery decision",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "n2",
                    "task",
                    "Task",
                    "Execute repair",
                    "READY",
                    vec![],
                    BTreeMap::new(),
                ),
            ],
            vec![
                BundleRelationship::new(
                    "root",
                    "n1",
                    "TRIGGERS",
                    RelationExplanation::new(RelationSemanticClass::Causal)
                        .with_rationale("failure triggered recovery"),
                ),
                BundleRelationship::new(
                    "root",
                    "n2",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
            ],
            vec![BundleNodeDetail::new("root", "Extended detail", "h1", 1)],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid")
    }

    #[test]
    fn l0_summary_is_always_first() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ReasonPreserving,
        );

        assert_eq!(sections[0].tier, ResolutionTier::L0Summary);
        assert!(sections[0].content.contains("Objective:"));
        assert!(sections[0].content.contains("Status:"));
    }

    #[test]
    fn causal_relations_go_to_l1() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ReasonPreserving,
        );

        let l1_sections: Vec<_> = sections
            .iter()
            .filter(|s| s.tier == ResolutionTier::L1CausalSpine)
            .collect();
        assert!(l1_sections.iter().any(|s| s.content.contains("[causal]")));
        assert!(
            !l1_sections
                .iter()
                .any(|s| s.content.contains("[structural]"))
        );
    }

    #[test]
    fn structural_relations_go_to_l2() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ReasonPreserving,
        );

        let l2_sections: Vec<_> = sections
            .iter()
            .filter(|s| s.tier == ResolutionTier::L2EvidencePack)
            .collect();
        assert!(
            l2_sections
                .iter()
                .any(|s| s.content.contains("[structural]"))
        );
    }

    #[test]
    fn details_go_to_l2() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ReasonPreserving,
        );

        let l2_sections: Vec<_> = sections
            .iter()
            .filter(|s| s.tier == ResolutionTier::L2EvidencePack)
            .collect();
        assert!(l2_sections.iter().any(|s| s.content.contains("Detail")));
    }

    #[test]
    fn max_tier_l0_only_returns_summary() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions {
                max_tier: Some(ResolutionTier::L0Summary),
                ..Default::default()
            },
            KmpMode::ReasonPreserving,
        );

        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].tier, ResolutionTier::L0Summary);
    }

    #[test]
    fn max_tier_l1_excludes_l2() {
        let bundle = sample_bundle();
        let detail_map = bundle
            .node_details()
            .iter()
            .map(|d| (d.node_id(), d))
            .collect();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions {
                max_tier: Some(ResolutionTier::L1CausalSpine),
                ..Default::default()
            },
            KmpMode::ReasonPreserving,
        );

        assert!(
            sections
                .iter()
                .all(|s| s.tier != ResolutionTier::L2EvidencePack)
        );
        assert!(
            sections
                .iter()
                .any(|s| s.tier == ResolutionTier::L1CausalSpine)
        );
    }

    #[test]
    fn l0_summary_identifies_blocker_and_next_action() {
        let bundle = KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "incident",
                "Root",
                "Outage",
                "BLOCKED",
                vec![],
                BTreeMap::new(),
            ),
            vec![BundleNode::new(
                "n1",
                "task",
                "Fix",
                "",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            )],
            vec![
                BundleRelationship::new(
                    "root",
                    "n1",
                    "TRIGGERS",
                    RelationExplanation::new(RelationSemanticClass::Causal)
                        .with_rationale("must fix first"),
                ),
                BundleRelationship::new(
                    "root",
                    "n1",
                    "BLOCKED_BY",
                    RelationExplanation::new(RelationSemanticClass::Constraint)
                        .with_rationale("waiting for approval"),
                ),
            ],
            vec![],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid");

        let detail_map = BTreeMap::new();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ReasonPreserving,
        );
        let l0 = &sections[0].content;

        assert!(l0.contains("Blocker: waiting for approval"));
        assert!(l0.contains("Next: TRIGGERS"));
    }

    #[test]
    fn resume_focused_excludes_distractor_nodes() {
        let bundle = bundle_with_distractors();
        let detail_map = BTreeMap::new();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ResumeFocused,
        );

        // No L2 sections at all
        assert!(
            sections
                .iter()
                .all(|s| s.tier != ResolutionTier::L2EvidencePack)
        );
        // No distractor content
        let all_content: String = sections.iter().map(|s| s.content.as_str()).collect();
        assert!(!all_content.contains("distractor"));
    }

    #[test]
    fn resume_focused_keeps_causal_relationships() {
        let bundle = bundle_with_distractors();
        let detail_map = BTreeMap::new();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ResumeFocused,
        );

        let l1: Vec<_> = sections
            .iter()
            .filter(|s| s.tier == ResolutionTier::L1CausalSpine)
            .collect();
        assert!(l1.iter().any(|s| s.content.contains("[causal]")));
        assert!(!l1.iter().any(|s| s.content.contains("[structural]")));
    }

    #[test]
    fn resume_focused_includes_causal_spine_nodes() {
        let bundle = bundle_with_distractors();
        let detail_map = BTreeMap::new();
        let sections = classify_into_tiers(
            &bundle,
            &detail_map,
            &ContextRenderOptions::default(),
            KmpMode::ResumeFocused,
        );

        let l1_content: String = sections
            .iter()
            .filter(|s| s.tier == ResolutionTier::L1CausalSpine)
            .map(|s| s.content.as_str())
            .collect::<Vec<_>>()
            .join("\n");

        // Causal chain node should be in L1
        assert!(
            l1_content.contains("Chain Node"),
            "causal chain node should be in L1"
        );
        // Root should be in L1
        assert!(l1_content.contains("Root"), "root should be in L1");
    }

    fn bundle_with_distractors() -> KmpBundle {
        KmpBundle::new(
            CaseId::new("root").expect("valid"),
            Role::new("dev").expect("valid"),
            BundleNode::new(
                "root",
                "incident",
                "Root",
                "Outage",
                "ACTIVE",
                vec![],
                BTreeMap::new(),
            ),
            vec![
                BundleNode::new(
                    "chain-0",
                    "decision",
                    "Chain Node",
                    "Recovery",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "dist-0",
                    "distractor",
                    "distractor 0",
                    "",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "dist-1",
                    "distractor",
                    "distractor 1",
                    "",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
                BundleNode::new(
                    "dist-2",
                    "distractor",
                    "distractor 2",
                    "",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                ),
            ],
            vec![
                BundleRelationship::new(
                    "root",
                    "chain-0",
                    "TRIGGERS",
                    RelationExplanation::new(RelationSemanticClass::Causal)
                        .with_rationale("failure triggered recovery"),
                ),
                BundleRelationship::new(
                    "root",
                    "dist-0",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
                BundleRelationship::new(
                    "root",
                    "dist-1",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
                BundleRelationship::new(
                    "root",
                    "dist-2",
                    "CONTAINS",
                    RelationExplanation::new(RelationSemanticClass::Structural),
                ),
            ],
            vec![],
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid")
    }
}