kmp-application 0.1.11

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
use kmp_domain::{KmpBundle, KmpMode, RelationSemanticClass};

use crate::queries::EndpointHint;

/// Resolves `Auto` mode into a concrete mode based on token pressure,
/// causal density, focus presence, and endpoint type.
///
/// When mode is explicit (not Auto), it passes through unchanged.
/// When Auto, the heuristic considers:
/// 1. Token pressure (budget / nodes) — tight budgets favor ResumeFocused
/// 2. Causal density — high explanatory ratio preserves rationale; very low density prunes even at generous budgets
/// 3. Focus presence — scoped paths tolerate more pruning (threshold 30 → 60)
/// 4. Endpoint type — sessions need richer context (threshold → 15)
pub(crate) fn resolve_mode(
    explicit_mode: KmpMode,
    bundle: &KmpBundle,
    token_budget: Option<u32>,
    focus_node_id: Option<&str>,
    endpoint_hint: EndpointHint,
) -> KmpMode {
    match explicit_mode {
        KmpMode::Auto => auto_detect(bundle, token_budget, focus_node_id, endpoint_hint),
        concrete => concrete,
    }
}

/// Default tokens-per-node threshold (GetContext without focus).
const TOKENS_PER_NODE_THRESHOLD: u32 = 30;

/// When a focus path is set, scoped context benefits from pruning even at
/// moderate budgets. Double the threshold.
const FOCUSED_TOKENS_PER_NODE_THRESHOLD: u32 = 60;

/// Sessions serve multi-role snapshots and need richer context. Lower threshold
/// means we stay in ReasonPreserving longer.
const SESSION_TOKENS_PER_NODE_THRESHOLD: u32 = 15;

/// Causal density above which we keep ReasonPreserving even under token pressure.
const CAUSAL_DENSITY_PRESERVE_THRESHOLD: f64 = 0.5;

/// Causal density below which we switch to ResumeFocused even at generous budgets.
/// Structural-heavy graphs have nothing worth preserving in ReasonPreserving mode.
const STRUCTURAL_OVERRIDE_DENSITY: f64 = 0.2;

fn auto_detect(
    bundle: &KmpBundle,
    token_budget: Option<u32>,
    focus_node_id: Option<&str>,
    endpoint_hint: EndpointHint,
) -> KmpMode {
    let Some(budget) = token_budget else {
        return KmpMode::ReasonPreserving;
    };

    let total_nodes = bundle.stats().selected_nodes();
    if total_nodes == 0 {
        return KmpMode::ReasonPreserving;
    }

    let tokens_per_node = budget / total_nodes;
    let causal_density = bundle_causal_density(bundle);

    let effective_threshold = match endpoint_hint {
        EndpointHint::SessionSnapshot => SESSION_TOKENS_PER_NODE_THRESHOLD,
        _ if focus_node_id.is_some() => FOCUSED_TOKENS_PER_NODE_THRESHOLD,
        _ => TOKENS_PER_NODE_THRESHOLD,
    };

    if tokens_per_node >= effective_threshold {
        // Budget is generous relative to endpoint type.
        // Structural-heavy graphs still benefit from pruning.
        if causal_density < STRUCTURAL_OVERRIDE_DENSITY {
            return KmpMode::ResumeFocused;
        }
        return KmpMode::ReasonPreserving;
    }

    // Budget is tight — preserve rationale only if density justifies it.
    if causal_density >= CAUSAL_DENSITY_PRESERVE_THRESHOLD {
        KmpMode::ReasonPreserving
    } else {
        KmpMode::ResumeFocused
    }
}

/// Fraction of relationships with explanatory semantic class (causal, motivational, evidential).
fn bundle_causal_density(bundle: &KmpBundle) -> f64 {
    let total = bundle.relationships().len();
    if total == 0 {
        return 0.0;
    }
    let explanatory = bundle
        .relationships()
        .iter()
        .filter(|r| {
            matches!(
                r.explanation().semantic_class(),
                RelationSemanticClass::Causal
                    | RelationSemanticClass::Motivational
                    | RelationSemanticClass::Evidential
            )
        })
        .count();
    explanatory as f64 / total as f64
}

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

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

    use super::resolve_mode;
    use crate::queries::EndpointHint;

    fn bundle_with_nodes(count: usize) -> KmpBundle {
        let root = BundleNode::new(
            "case",
            "case",
            "Root",
            "",
            "ACTIVE",
            vec![],
            BTreeMap::new(),
        );
        let neighbors: Vec<_> = (0..count.saturating_sub(1))
            .map(|i| {
                BundleNode::new(
                    format!("n{i}"),
                    "task",
                    format!("N{i}"),
                    "",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                )
            })
            .collect();
        KmpBundle::new(
            CaseId::new("case").expect("valid"),
            Role::new("dev").expect("valid"),
            root,
            neighbors,
            Vec::new(),
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid")
    }

    fn bundle_with_causal_relations(node_count: usize, causal_count: usize) -> KmpBundle {
        let root = BundleNode::new(
            "case",
            "case",
            "Root",
            "",
            "ACTIVE",
            vec![],
            BTreeMap::new(),
        );
        let neighbors: Vec<_> = (0..node_count.saturating_sub(1))
            .map(|i| {
                BundleNode::new(
                    format!("n{i}"),
                    "task",
                    format!("N{i}"),
                    "",
                    "ACTIVE",
                    vec![],
                    BTreeMap::new(),
                )
            })
            .collect();
        let mut relationships = Vec::new();
        for i in 0..node_count.saturating_sub(1) {
            let class = if i < causal_count {
                RelationSemanticClass::Causal
            } else {
                RelationSemanticClass::Structural
            };
            relationships.push(BundleRelationship::new(
                "case",
                format!("n{i}"),
                "RELATES",
                RelationExplanation::new(class),
            ));
        }
        KmpBundle::new(
            CaseId::new("case").expect("valid"),
            Role::new("dev").expect("valid"),
            root,
            neighbors,
            relationships,
            Vec::new(),
            BundleMetadata::initial("0.1.0"),
        )
        .expect("valid")
    }

    // ── v2 tests (updated with new params, same assertions) ──

    #[test]
    fn auto_selects_resume_focused_when_budget_tight_and_structural() {
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(512),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn auto_keeps_reason_preserving_when_budget_tight_but_high_causal_density() {
        let bundle = bundle_with_causal_relations(49, 40);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(512),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    #[test]
    fn auto_selects_resume_focused_when_budget_tight_and_low_causal_density() {
        let bundle = bundle_with_causal_relations(49, 5);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(512),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn auto_selects_reason_preserving_when_no_budget() {
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            None,
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    #[test]
    fn explicit_mode_passes_through() {
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::ResumeFocused,
            &bundle,
            Some(4096),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn auto_selects_resume_focused_for_single_node_tight_budget() {
        let bundle = bundle_with_nodes(1);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(10),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    // ── Enrichment 1: focus presence ──

    #[test]
    fn focus_biases_toward_resume_focused_at_moderate_budget() {
        // 21 nodes, 1000 tokens → 47 tok/node. Without focus: >= 30 → ReasonPreserving.
        // With focus: < 60 → falls through to density check → 0 density → ResumeFocused.
        let bundle = bundle_with_nodes(21);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(1000),
            Some("n1"),
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn focus_keeps_reason_preserving_at_generous_budget() {
        // 10 nodes, 1000 tokens → 100 tok/node. Even with focus (threshold 60), 100 >= 60.
        // But 0 causal density → structural override → ResumeFocused.
        // Need causal relations to stay ReasonPreserving.
        let bundle = bundle_with_causal_relations(10, 5); // 55% causal
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(1000),
            Some("n1"),
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    #[test]
    fn focus_does_not_affect_no_budget_case() {
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            None,
            Some("n1"),
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    // ── Enrichment 2: endpoint type ──

    #[test]
    fn session_snapshot_keeps_reason_preserving_at_moderate_pressure() {
        // 49 nodes, 1000 tokens → 20 tok/node. Normal threshold (30): tight → density check.
        // Session threshold (15): 20 >= 15 → generous. With causal relations → ReasonPreserving.
        let bundle = bundle_with_causal_relations(49, 30); // 62% causal
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(1000),
            None,
            EndpointHint::SessionSnapshot,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    #[test]
    fn session_snapshot_falls_to_resume_focused_at_extreme_pressure() {
        // 49 nodes, 200 tokens → 4 tok/node. Even session threshold (15): 4 < 15 → tight.
        // Low density → ResumeFocused.
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(200),
            None,
            EndpointHint::SessionSnapshot,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn focused_path_hint_activates_focus_threshold() {
        // 21 nodes, 1000 tokens → 47 tok/node. FocusedPath uses threshold 60.
        // 47 < 60 → tight → 0 density → ResumeFocused.
        let bundle = bundle_with_nodes(21);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(1000),
            None,
            EndpointHint::FocusedPath,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    // ── Enrichment 3: relation distribution ──

    #[test]
    fn generous_budget_structural_graph_switches_to_resume_focused() {
        // 21 nodes, 4096 tokens → 195 tok/node. Very generous.
        // But 0 relations → density 0 < 0.2 → structural override → ResumeFocused.
        let bundle = bundle_with_nodes(21);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(4096),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ResumeFocused);
    }

    #[test]
    fn generous_budget_causal_graph_stays_reason_preserving() {
        // 21 nodes, 4096 tokens → 195 tok/node. 50% causal density → above 0.2 → ReasonPreserving.
        let bundle = bundle_with_causal_relations(21, 10); // 50% causal
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            Some(4096),
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }

    #[test]
    fn no_budget_stays_reason_preserving_even_when_all_structural() {
        let bundle = bundle_with_nodes(49);
        let mode = resolve_mode(
            KmpMode::Auto,
            &bundle,
            None,
            None,
            EndpointHint::Neighborhood,
        );
        assert_eq!(mode, KmpMode::ReasonPreserving);
    }
}