kv-prefix-guard 1.0.0

Prefix-invariant context partitioning, K-bounded cache breakpoint scheduling, and commit-time relational artifact distillation for multi-turn LLM KV-caches (Anthropic Claude, OpenAI, Gemini, vLLM, SGLang).
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
use crate::partitioner::ChatMessage;
use crate::pruners::purge_reasoning_artifacts;
use serde_json::Value;

/// Counts net `{` and `}` brace delta on a single line outside string literals and line comments.
pub fn count_line_braces(line: &str) -> (usize, usize, isize) {
    let mut open = 0usize;
    let mut close = 0usize;
    let mut in_single = false;
    let mut in_double = false;
    let mut in_template = false;
    let bytes = line.as_bytes();

    let mut i = 0usize;
    while i < bytes.len() {
        let ch = bytes[i];
        let prev = if i > 0 { bytes[i - 1] } else { 0 };
        if prev == b'\\' {
            i += 1;
            continue;
        }
        if !in_double && !in_template && ch == b'\'' {
            in_single = !in_single;
        } else if !in_single && !in_double && ch == b'`' {
            in_template = !in_template;
        } else if !in_single && !in_template && ch == b'"' {
            in_double = !in_double;
        } else if !in_single && !in_double && !in_template {
            if ch == b'/' && bytes.get(i + 1) == Some(&b'/') {
                break;
            }
            if ch == b'{' {
                open += 1;
            } else if ch == b'}' {
                close += 1;
            }
        }
        i += 1;
    }
    (open, close, (open as isize) - (close as isize))
}

/// Deterministic O(n) string-aware brace-depth AST code skeletonizer.
/// Preserves imports, interfaces, types, enums, struct/class declarations, and function/method
/// signatures while eliding implementation bodies (80-95% token reduction).
pub fn compact_code_skeleton(raw_code: &str, max_lines: usize) -> String {
    let trimmed = raw_code.trim();
    if trimmed.is_empty() || trimmed.starts_with("// [AST CODE SKELETON") {
        return trimmed.to_string();
    }

    #[derive(PartialEq, Eq)]
    enum State {
        Top,
        Interface,
        Class,
        SkipClassMethod,
        SkipTopBody,
    }

    let mut state = State::Top;
    let mut in_multi_comment = false;
    let mut brace_depth: isize = 0;
    let mut import_count = 0usize;
    let mut out_lines = Vec::new();
    out_lines.push("// [AST CODE SKELETON: Signatures & Contracts Preserved]".to_string());

    for raw_line in trimmed.lines() {
        let line = raw_line.trim();
        if line.starts_with("/*") {
            in_multi_comment = true;
        }
        if in_multi_comment {
            if line.contains("*/") {
                in_multi_comment = false;
            }
            continue;
        }
        if line.is_empty() || line.starts_with("//") || line.starts_with('#') {
            continue;
        }

        let (open, close, net) = count_line_braces(line);

        match state {
            State::SkipTopBody => {
                brace_depth = (brace_depth + net).max(0);
                if brace_depth == 0 {
                    state = State::Top;
                }
                continue;
            }
            State::SkipClassMethod => {
                brace_depth = (brace_depth + net).max(0);
                if brace_depth <= 1 {
                    if brace_depth == 0 {
                        out_lines.push("}".to_string());
                        state = State::Top;
                    } else {
                        state = State::Class;
                    }
                }
                continue;
            }
            State::Interface => {
                out_lines.push(raw_line.to_string());
                brace_depth = (brace_depth + net).max(0);
                if brace_depth == 0 {
                    state = State::Top;
                }
            }
            State::Class => {
                let next_depth = (brace_depth + net).max(0);
                if next_depth == 0 && close > 0 && open == 0 {
                    out_lines.push("}".to_string());
                    brace_depth = 0;
                    state = State::Top;
                    continue;
                }
                let is_control = line.starts_with("if ")
                    || line.starts_with("if(")
                    || line.starts_with("for ")
                    || line.starts_with("while ")
                    || line.starts_with("switch ")
                    || line.starts_with("return ");
                if !is_control && line.contains('(') {
                    let sig = raw_line.trim_end().trim_end_matches('{').trim_end();
                    out_lines.push(format!("{};", sig));
                    if net > 0 {
                        brace_depth = next_depth;
                        state = State::SkipClassMethod;
                    }
                } else if net > 0 {
                    brace_depth = next_depth;
                    state = State::SkipClassMethod;
                } else {
                    brace_depth = next_depth;
                }
            }
            State::Top => {
                if line.starts_with("import ") || line.starts_with("from ") || line.starts_with("use ") {
                    if import_count < 8 {
                        out_lines.push(raw_line.to_string());
                        import_count += 1;
                    }
                    continue;
                }
                let is_type_decl = line.starts_with("export interface ")
                    || line.starts_with("interface ")
                    || line.starts_with("export type ")
                    || line.starts_with("type ")
                    || line.starts_with("export enum ")
                    || line.starts_with("enum ")
                    || line.starts_with("pub struct ")
                    || line.starts_with("struct ");
                if is_type_decl {
                    out_lines.push(raw_line.to_string());
                    if net > 0 {
                        brace_depth += net;
                        state = State::Interface;
                    }
                    continue;
                }

                let is_class_decl = line.starts_with("export class ")
                    || line.starts_with("class ")
                    || line.starts_with("impl ");
                if is_class_decl {
                    out_lines.push(raw_line.to_string());
                    if net > 0 {
                        brace_depth += net;
                        state = State::Class;
                    }
                    continue;
                }

                let is_fn_decl = line.starts_with("export function ")
                    || line.starts_with("export async function ")
                    || line.starts_with("function ")
                    || line.starts_with("async function ")
                    || line.starts_with("pub fn ")
                    || line.starts_with("fn ")
                    || line.starts_with("def ")
                    || line.starts_with("async def ")
                    || ((line.starts_with("export const ") || line.starts_with("const "))
                        && line.contains("=>"));

                if is_fn_decl {
                    let sig = raw_line.trim_end().trim_end_matches('{').trim_end();
                    out_lines.push(format!("{};", sig));
                    if net > 0 {
                        brace_depth += net;
                        state = State::SkipTopBody;
                    }
                } else if net > 0 {
                    brace_depth += net;
                    state = State::SkipTopBody;
                }
            }
        }

        if out_lines.len() >= max_lines {
            out_lines.push("// ... [remaining implementation bodies elided]".to_string());
            break;
        }
    }

    out_lines.join("\n")
}

fn extract_tag_inner<'a>(html: &'a str, tag: &str) -> Option<&'a str> {
    let open_pat = format!("<{}", tag);
    let close_pat = format!("</{}>", tag);
    let lower = html.to_ascii_lowercase();
    let start = lower.find(&open_pat)?;
    let gt = lower[start..].find('>')? + start + 1;
    let end = lower[gt..].find(&close_pat)? + gt;
    Some(html[gt..end].trim())
}

fn push_unique(vec: &mut Vec<String>, item: String, cap: usize) {
    if !item.is_empty() && !vec.contains(&item) && vec.len() < cap {
        vec.push(item);
    }
}

/// Distills an interactive HTML5 / Canvas / Three.js WebGL application into a compact
/// Relational Event-Graph Blueprint (~120-180 tokens, 88-95% token reduction) preserving
/// 100% of DOM `#id` selectors, state variables, function signatures, and event-loop edges.
pub fn distill_app_graph_blueprint(raw_html: &str, fallback_title: &str) -> String {
    let text = raw_html.trim();
    if text.len() <= 360 || text.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
        return text.to_string();
    }

    let title = extract_tag_inner(text, "title")
        .or_else(|| extract_tag_inner(text, "h1"))
        .or_else(|| extract_tag_inner(text, "h2"))
        .unwrap_or(fallback_title)
        .replace(['\n', '\r', '"'], "");

    // 1. Detect stack
    let mut stack = Vec::new();
    if text.contains("tailwindcss") {
        stack.push("Tailwind CSS");
    }
    if text.contains("THREE.") || text.contains("three.js") {
        stack.push("Three.js 3D WebGL");
    } else if text.contains("<canvas") || text.contains("getContext(") {
        stack.push("HTML5 Canvas 2D");
    }
    if text.contains("<svg") {
        stack.push("Inline SVG");
    }
    if text.contains("localStorage") {
        stack.push("LocalStorage State");
    }
    let stack_str = if stack.is_empty() {
        "Vanilla JavaScript & CSS".to_string()
    } else {
        stack.join(" + ")
    };

    // 2. Extract DOM IDs (`id="..."` and `getElementById("...")`)
    let mut dom_ids = Vec::new();
    for pat in ["id=\"", "id='", "getElementById(\"", "getElementById('", "querySelector(\"#", "querySelector('#"] {
        let mut cur = 0usize;
        while let Some(pos) = text[cur..].find(pat) {
            let start = cur + pos + pat.len();
            let bytes = text.as_bytes();
            let mut end = start;
            while end < bytes.len() {
                let b = bytes[end];
                if b.is_ascii_alphanumeric() || b == b'_' || b == b'-' {
                    end += 1;
                } else {
                    break;
                }
            }
            if end > start {
                push_unique(&mut dom_ids, format!("#{}", &text[start..end]), 12);
            }
            cur = end;
        }
    }
    let dom_str = if dom_ids.is_empty() {
        "#app (root container)".to_string()
    } else {
        dom_ids.join(" | ")
    };

    // 3. Extract JS function signatures and state variables
    let mut fn_sigs = Vec::new();
    let mut fn_names = Vec::new();
    let mut state_vars = Vec::new();

    for stmt in text.split(['\n', ';', '>', '<', '}']) {
        let t = stmt.trim();
        for kw in ["let ", "var ", "const "] {
            if let Some(rest) = t.strip_prefix(kw) {
                if let Some(eq_idx) = rest.find('=') {
                    let var_name = rest[..eq_idx]
                        .split(':')
                        .next()
                        .unwrap_or("")
                        .trim();
                    let rhs = rest[eq_idx + 1..].trim();
                    let valid_ident = !var_name.is_empty()
                        && var_name
                            .bytes()
                            .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'$')
                        && !matches!(var_name, "i" | "j" | "k" | "e" | "evt" | "el" | "ctx" | "canvas");
                    if valid_ident {
                        if rhs.starts_with('(') || rhs.starts_with("async") {
                            let params = rhs
                                .find(')')
                                .and_then(|close| rhs.find('(').map(|open| &rhs[open..=close]))
                                .unwrap_or("()");
                            push_unique(&mut fn_names, var_name.to_string(), 10);
                            push_unique(&mut fn_sigs, format!("{}{}", var_name, params), 10);
                        } else if kw != "const " || rhs.starts_with('{') || rhs.starts_with('[') || rhs.starts_with("new ") {
                            push_unique(&mut state_vars, var_name.to_string(), 10);
                        }
                    }
                }
            }
        }
        for fkw in ["function ", "async function "] {
            if let Some(rest) = t.strip_prefix(fkw) {
                if let Some(open_p) = rest.find('(') {
                    let name = rest[..open_p].trim();
                    if !name.is_empty()
                        && name
                            .bytes()
                            .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'$')
                    {
                        let close_p = rest[open_p..].find(')').map(|c| open_p + c).unwrap_or(open_p);
                        let params = if close_p > open_p {
                            &rest[open_p..=close_p]
                        } else {
                            "()"
                        };
                        push_unique(&mut fn_names, name.to_string(), 10);
                        push_unique(&mut fn_sigs, format!("{}{}", name, params), 10);
                    }
                }
            }
        }
    }

    let state_str = if state_vars.is_empty() {
        "Internal application state".to_string()
    } else {
        state_vars.join(" | ")
    };
    let methods_str = if fn_sigs.is_empty() {
        "Event dispatchers & render cycle".to_string()
    } else {
        fn_sigs.join(" | ")
    };

    // 4. Build Relational Event Edges
    let mut edges = Vec::new();
    if text.contains("addEventListener(\"click\"")
        || text.contains("addEventListener('click'")
        || text.contains("onclick=")
    {
        let btn = dom_ids
            .iter()
            .find(|id| id.contains("btn") || id.contains("start") || id.contains("toggle") || id.contains("reset"))
            .cloned()
            .or_else(|| dom_ids.first().cloned())
            .unwrap_or_else(|| "#control".to_string());
        let handler = fn_names
            .iter()
            .find(|f| f.contains("click") || f.contains("handle") || f.contains("toggle") || f.contains("start"))
            .cloned()
            .or_else(|| fn_names.first().cloned())
            .unwrap_or_else(|| "handleClick".to_string());
        edges.push(format!("({}) --[ON_CLICK]--> ({}()) --[MUTATES]--> (State)", btn, handler));
    }
    if text.contains("requestAnimationFrame") || text.contains("setInterval") {
        let loop_fn = fn_names
            .iter()
            .find(|f| f.contains("render") || f.contains("animate") || f.contains("loop") || f.contains("tick") || f.contains("draw"))
            .cloned()
            .unwrap_or_else(|| "renderLoop".to_string());
        let target = dom_ids
            .iter()
            .find(|id| id.contains("canvas") || id.contains("stage") || id.contains("viewport"))
            .cloned()
            .unwrap_or_else(|| "#canvas".to_string());
        edges.push(format!("(requestAnimationFrame) --[TICK_CADENCE]--> ({}()) --[DRAWS_FRAME]--> ({})", loop_fn, target));
    }
    if edges.is_empty() {
        let first_fn = fn_names.first().cloned().unwrap_or_else(|| "handler".to_string());
        edges.push(format!("(UserInteraction) --[EVENT_DISPATCH]--> ({}()) --[RE_RENDERS]--> (DOM)", first_fn));
    }

    format!(
        "<!-- [RELATIONAL ARTIFACT BLUEPRINT: \"{}\"]\nArchitecture & Stack: {}\nNodes:\n  * DOM Elements: {}\n  * State Stores: {}\n  * Methods: {}\nRelational Edges:\n  * {}\nGlobal Invariants:\n  * Preserve existing DOM element IDs ({}), state variables ({}), and method signatures ({}) while implementing requested updates.\n-->",
        title,
        stack_str,
        dom_str,
        state_str,
        methods_str,
        edges.join("\n  * "),
        dom_str,
        state_str,
        methods_str
    )
}

/// Distills a Mermaid diagram (> 250 chars) into a compact topology blueprint preserving
/// all subgraph identifiers, node IDs, and directed edge relationships.
pub fn distill_diagram_blueprint(raw_mermaid: &str) -> String {
    let text = raw_mermaid.trim();
    if text.len() <= 250 || text.contains("[RELATIONAL DIAGRAM BLUEPRINT:") {
        return text.to_string();
    }

    let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty());
    let first_line = lines.next().unwrap_or("flowchart TD");
    let mut subgraphs = Vec::new();
    let mut nodes = Vec::new();
    let mut edges = Vec::new();
    let mut total_transitions = 0usize;

    for line in text.lines() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix("subgraph ") {
            let sg_id = rest.split(['[', ' ']).next().unwrap_or("").trim();
            push_unique(&mut subgraphs, sg_id.to_string(), 8);
            continue;
        }
        for arrow in ["-->", "==>", "-.->", "->>", "-->>"] {
            if let Some(pos) = t.find(arrow) {
                total_transitions += 1;
                let left = t[..pos].trim();
                let right = t[pos + arrow.len()..].trim();
                let from_id = left
                    .split(['[', '(', '{', ' '])
                    .next()
                    .unwrap_or("")
                    .trim();
                let after_label = if right.starts_with('|') {
                    right[1..]
                        .find('|')
                        .map(|end_bar| right[end_bar + 2..].trim())
                        .unwrap_or(right)
                } else {
                    right
                };
                let to_id = after_label
                    .split(['[', '(', '{', ':', ' '])
                    .next()
                    .unwrap_or("")
                    .trim();
                push_unique(&mut nodes, from_id.to_string(), 14);
                push_unique(&mut nodes, to_id.to_string(), 14);
                if !from_id.is_empty() && !to_id.is_empty() {
                    push_unique(&mut edges, format!("{} {} {}", from_id, arrow, to_id), 14);
                }
                break;
            }
        }
    }

    format!(
        "%% [RELATIONAL DIAGRAM BLUEPRINT: {}]\nSubgraphs: {}\nEntities: {}\nEdges: {}\nTopology: ~{} connected transitions.\n[REFINEMENT DIRECTIVE]: Preserve existing node identifiers and relationship topology while applying updates. %%",
        first_line,
        if subgraphs.is_empty() { "None".to_string() } else { subgraphs.join(", ") },
        nodes.join(", "),
        edges.join(", "),
        total_transitions
    )
}

/// Distills a 3D Voxel JSON scene into a compact structural summary preserving palette keys,
/// dimensions, camera coordinates, and primitive definitions while collapsing raw voxel matrices.
pub fn distill_voxel_blueprint(raw_json: &str) -> String {
    let trimmed = raw_json.trim();
    if trimmed.contains("[RELATIONAL VOXEL BLUEPRINT:") {
        return trimmed.to_string();
    }
    let Ok(parsed) = serde_json::from_str::<Value>(trimmed) else {
        return trimmed.to_string();
    };
    let Some(obj) = parsed.as_object() else {
        return trimmed.to_string();
    };
    let voxel_count = obj
        .get("voxels")
        .and_then(|v| v.as_array())
        .map(|a| a.len())
        .unwrap_or(0);
    if voxel_count <= 5 {
        return trimmed.to_string();
    }
    let title = obj
        .get("title")
        .and_then(|v| v.as_str())
        .unwrap_or("3D Voxel Model");
    let dims = obj
        .get("dimensions")
        .map(|d| d.to_string())
        .unwrap_or_else(|| "[16,16,16]".to_string());
    let palette_keys: Vec<String> = obj
        .get("palette")
        .and_then(|p| p.as_object())
        .map(|m| m.keys().cloned().collect())
        .unwrap_or_default();

    format!(
        "<!-- [RELATIONAL VOXEL BLUEPRINT: {}]\nDimensions: {}\nPalette Keys: {}\nVoxel Count: {}\n[REFINEMENT DIRECTIVE]: Preserve existing palette keys and dimensions while applying requested modifications. -->",
        title,
        dims,
        palette_keys.join(", "),
        voxel_count
    )
}

/// Distills structured multi-section documents (Legal Contracts, Resumes, and Technical
/// Specifications > 800 chars) into a Relational Document Knowledge Graph capturing
/// Entity Nodes, Relational Edges, and Global Invariants.
pub fn distill_document_graph_blueprint(raw_doc: &str) -> String {
    let text = raw_doc.trim();
    if text.len() <= 800 || text.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
        return text.to_string();
    }

    let mut title = "Structured Document".to_string();
    let mut sections = Vec::new();
    for line in text.lines() {
        let t = line.trim();
        if let Some(h1) = t.strip_prefix("# ") {
            if title == "Structured Document" {
                title = h1.trim().replace('"', "");
            }
        } else if let Some(h2) = t.strip_prefix("## ") {
            push_unique(&mut sections, h2.trim().to_string(), 12);
        } else if let Some(h3) = t.strip_prefix("### ") {
            push_unique(&mut sections, h3.trim().to_string(), 12);
        }
    }

    let lower = text.to_ascii_lowercase();
    let is_contract = lower.contains("agreement")
        || lower.contains("indemn")
        || lower.contains("liability")
        || lower.contains("confidentiality")
        || lower.contains("statement of work");
    let is_resume = lower.contains("curriculum vitae")
        || lower.contains("resume")
        || (lower.contains("experience") && lower.contains("skills") && lower.contains("education"));

    let mut nodes = Vec::new();
    let mut edges = Vec::new();
    let invariant: &str;

    if is_contract {
        nodes.push(format!("Agreement(\"{}\")", title));
        for s in &sections {
            nodes.push(format!("Clause(\"{}\")", s));
        }
        if sections.iter().any(|s| s.to_ascii_lowercase().contains("indemn"))
            && sections.iter().any(|s| s.to_ascii_lowercase().contains("liabil"))
        {
            edges.push("(Clause:Indemnity) --[BOUND_BY_CAP]--> (Clause:Liability)".to_string());
        }
        if sections.iter().any(|s| s.to_ascii_lowercase().contains("terminat")) {
            edges.push("(Clause:Breach) --[TRIGGERS_NOTICE]--> (Clause:Termination)".to_string());
        }
        if edges.is_empty() {
            edges.push(format!(
                "(Parties) --[EXECUTE_TERMS]--> ({})",
                sections.join(", ")
            ));
        }
        invariant = "Cross-clause legal coherence, party definitions, and liability boundaries must remain synchronized.";
    } else if is_resume {
        nodes.push(format!("Candidate(\"{}\")", title));
        for s in &sections {
            nodes.push(format!("Section(\"{}\")", s));
        }
        edges.push("(Candidate) --[PROVEN_EXPERIENCE]--> (Experience) --[DEMONSTRATES_STACK]--> (Skills)".to_string());
        invariant = "Chronological accuracy, role titles, and technical skill alignment must be preserved across all sections.";
    } else {
        nodes.push(format!("Document(\"{}\")", title));
        for s in &sections {
            nodes.push(format!("Section(\"{}\")", s));
        }
        for pair in sections.windows(2).take(4) {
            edges.push(format!(
                "(Section:{}) --[FLOWS_INTO]--> (Section:{})",
                pair[0], pair[1]
            ));
        }
        if edges.is_empty() {
            edges.push("(Document) --[DECOMPOSES_INTO]--> (Core Sections)".to_string());
        }
        invariant = "Narrative flow, entity definitions, and section structure must be maintained without disjointed transitions.";
    }

    format!(
        "<!-- [RELATIONAL ARTIFACT BLUEPRINT: \"{}\"]\nNodes:\n  * {}\nRelationships:\n  * {}\nGlobal Invariants:\n  * {}\n-->",
        title,
        nodes.join(" | "),
        edges.join("\n  * "),
        invariant
    )
}

/// Distills a completed assistant message turn once at turn-commit time:
/// 1. Strips inline XML scratchpads (`<scratchpad>`, `<thought>`, `<plan>`, unsigned `<thinking>`)
///    and operational headers while preserving any provider-signed thinking blocks.
/// 2. Converts large fenced or raw HTML/Canvas/WebGL apps, Mermaid diagrams, 3D Voxel scenes,
///    source-code blocks, and structured multi-section documents into compact Relational
///    Event-Graph or AST skeletons.
/// This operator is strictly idempotent: `distill_turn_for_commit(distill_turn_for_commit(x)) == distill_turn_for_commit(x)`.
pub fn distill_turn_for_commit(content: &str) -> String {
    let cleaned = purge_reasoning_artifacts(content);
    if cleaned.len() <= 320 || cleaned.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
        return cleaned;
    }

    if !cleaned.contains("```") {
        if cleaned.contains("<!DOCTYPE") || cleaned.contains("<html") {
            return distill_app_graph_blueprint(&cleaned, "Interactive Application");
        }
        let heading_count = cleaned
            .lines()
            .filter(|l| {
                let t = l.trim();
                t.starts_with("## ") || t.starts_with("### ")
            })
            .count();
        if cleaned.len() > 800 && heading_count >= 3 {
            return distill_document_graph_blueprint(&cleaned);
        }
        return cleaned;
    }

    let mut out = String::with_capacity(cleaned.len() / 2);
    let mut lines = cleaned.lines().peekable();
    let mut in_fence = false;
    let mut fence_marker = String::new();
    let mut fence_lang = String::new();
    let mut fence_body = Vec::new();

    while let Some(line) = lines.next() {
        let trimmed = line.trim();
        if !in_fence && trimmed.starts_with("```") {
            let ticks: String = trimmed.chars().take_while(|&c| c == '`').collect();
            let lang = trimmed[ticks.len()..].trim().to_ascii_lowercase();
            in_fence = true;
            fence_marker = ticks;
            fence_lang = lang;
            fence_body.clear();
            continue;
        }

        if in_fence && trimmed.starts_with(&fence_marker) && trimmed.chars().all(|c| c == '`') {
            let body_str = fence_body.join("\n");
            let compacted_body = distill_fenced_block(&fence_lang, &body_str);
            out.push_str(&format!("```{}\n{}\n```\n", fence_lang, compacted_body));
            in_fence = false;
            fence_body.clear();
            continue;
        }

        if in_fence {
            fence_body.push(line);
        } else {
            out.push_str(line);
            out.push('\n');
        }
    }

    if in_fence && !fence_body.is_empty() {
        let body_str = fence_body.join("\n");
        let compacted_body = distill_fenced_block(&fence_lang, &body_str);
        out.push_str(&format!("```{}\n{}\n```\n", fence_lang, compacted_body));
    }

    out.trim_end().to_string()
}

fn distill_fenced_block(lang: &str, body: &str) -> String {
    let trimmed = body.trim();
    if trimmed.len() <= 250 {
        return trimmed.to_string();
    }
    if lang == "mermaid"
        || lang == "diagram"
        || lang == "flowchart"
        || trimmed.starts_with("flowchart ")
        || trimmed.starts_with("sequenceDiagram")
    {
        return distill_diagram_blueprint(trimmed);
    }
    if lang == "voxel"
        || lang == "voxel_world"
        || (trimmed.starts_with('{') && trimmed.contains("\"voxels\""))
    {
        return distill_voxel_blueprint(trimmed);
    }
    if lang == "html"
        || lang == "visual"
        || lang == "interactive"
        || lang == "sandbox"
        || lang.starts_with("canvas")
        || trimmed.contains("<!DOCTYPE")
        || trimmed.contains("<html")
        || trimmed.contains("<script")
    {
        return distill_app_graph_blueprint(trimmed, "Interactive Application");
    }
    if lang == "markdown" || lang == "md" || lang == "document" {
        return distill_document_graph_blueprint(trimmed);
    }
    compact_code_skeleton(trimmed, 30)
}

/// Cold-start session hydration compactor: compacts all historical turns and collapses
/// superseded intermediate visual blueprints and older `[CURRENT ARTIFACT CODE]` user envelopes
/// so resuming a multi-turn session next day avoids 10,000+ token cold-start bloat.
pub fn compact_cold_start_session(messages: &[ChatMessage]) -> Vec<ChatMessage> {
    if messages.is_empty() {
        return Vec::new();
    }
    let last_idx = messages.len() - 1;
    let mut compacted: Vec<ChatMessage> = messages
        .iter()
        .enumerate()
        .map(|(idx, m)| {
            if m.role == "system" || (idx == last_idx && m.role == "user") {
                m.clone()
            } else if m.role == "user" && m.content.contains("[CURRENT ARTIFACT CODE]") {
                let mut collapsed = m.content.clone();
                if let Some(pos) = collapsed.find("[CURRENT ARTIFACT CODE]") {
                    let prefix = &collapsed[..pos];
                    collapsed = format!(
                        "{}[PREVIOUS ARTIFACT CODE: Compacted in conversation history]",
                        prefix
                    );
                }
                ChatMessage {
                    role: m.role.clone(),
                    content: collapsed,
                    cache_control: m.cache_control.clone(),
                }
            } else {
                ChatMessage {
                    role: m.role.clone(),
                    content: distill_turn_for_commit(&m.content),
                    cache_control: m.cache_control.clone(),
                }
            }
        })
        .collect();

    let blueprint_indices: Vec<usize> = compacted
        .iter()
        .enumerate()
        .filter_map(|(idx, m)| {
            if m.role == "assistant"
                && (m.content.contains("[RELATIONAL ARTIFACT BLUEPRINT:")
                    || m.content.contains("[RELATIONAL DIAGRAM BLUEPRINT:")
                    || m.content.contains("[RELATIONAL VOXEL BLUEPRINT:"))
            {
                Some(idx)
            } else {
                None
            }
        })
        .collect();

    if blueprint_indices.len() > 1 {
        for &old_idx in &blueprint_indices[..blueprint_indices.len() - 1] {
            compacted[old_idx].content =
                "<!-- [HISTORICAL ARTIFACT VERSION: Superseded by newer blueprint in active working memory.] -->"
                    .to_string();
        }
    }

    compacted
}