codesynapse-core 0.1.1

Core graph extraction and analysis engine for codesynapse — AST parsing, semantic embeddings, BM25 index
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
use regex::Regex;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::sync::OnceLock;

const RATIONALE_MIN_CHARS: usize = 80;
const RATIONALE_MIN_WORDS: usize = 8;

const VALID_SEMANTIC_FILE_TYPES: &[&str] =
    &["code", "document", "paper", "image", "rationale", "concept"];

fn semantic_id_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"^[A-Za-z0-9._:-]+$").unwrap())
}

pub struct SemanticLimits {
    pub max_bytes: usize,
    pub max_nodes: usize,
    pub max_edges: usize,
    pub max_hyperedges: usize,
    pub max_hyperedge_nodes: usize,
    pub max_id_length: usize,
}

impl Default for SemanticLimits {
    fn default() -> Self {
        Self {
            max_bytes: 25 * 1024 * 1024,
            max_nodes: 10_000,
            max_edges: 100_000,
            max_hyperedges: 10_000,
            max_hyperedge_nodes: 256,
            max_id_length: 256,
        }
    }
}

pub fn validate_semantic_fragment(fragment: &Value) -> Vec<String> {
    validate_semantic_fragment_with_limits(fragment, &SemanticLimits::default())
}

pub fn validate_semantic_fragment_with_limits(
    fragment: &Value,
    limits: &SemanticLimits,
) -> Vec<String> {
    let obj = match fragment.as_object() {
        Some(o) => o,
        None => return vec!["fragment must be a JSON object".to_string()],
    };

    let mut errors: Vec<String> = Vec::new();

    let payload = serde_json::to_string(fragment).unwrap_or_default();
    let byte_len = payload.len();
    if byte_len > limits.max_bytes {
        errors.push(format!(
            "payload is {} bytes; max is {}",
            byte_len, limits.max_bytes
        ));
    }

    let empty_arr = vec![];
    let nodes = match obj.get("nodes") {
        Some(v) => match v.as_array() {
            Some(arr) => {
                if arr.len() > limits.max_nodes {
                    errors.push(format!(
                        "nodes has {} entries; max is {}",
                        arr.len(),
                        limits.max_nodes
                    ));
                }
                arr
            }
            None => {
                errors.push("nodes must be a list".to_string());
                &empty_arr
            }
        },
        None => &empty_arr,
    };

    let edges = match obj.get("edges") {
        Some(v) => match v.as_array() {
            Some(arr) => {
                if arr.len() > limits.max_edges {
                    errors.push(format!(
                        "edges has {} entries; max is {}",
                        arr.len(),
                        limits.max_edges
                    ));
                }
                arr
            }
            None => {
                errors.push("edges must be a list".to_string());
                &empty_arr
            }
        },
        None => &empty_arr,
    };

    for (i, node) in nodes.iter().enumerate() {
        match node.as_object() {
            None => {
                errors.push(format!("nodes[{i}] must be an object"));
                continue;
            }
            Some(n) => {
                validate_semantic_id(&mut errors, &format!("nodes[{i}].id"), n.get("id"), limits);
                if let Some(ft) = n.get("file_type").and_then(|v| v.as_str()) {
                    if !VALID_SEMANTIC_FILE_TYPES.contains(&ft) {
                        errors.push(format!("nodes[{i}].file_type {ft:?} is not one of {:?}", {
                            let mut v: Vec<&&str> = VALID_SEMANTIC_FILE_TYPES.iter().collect();
                            v.sort();
                            v
                        }));
                    }
                }
            }
        }
    }

    for (i, edge) in edges.iter().enumerate() {
        match edge.as_object() {
            None => {
                errors.push(format!("edges[{i}] must be an object"));
                continue;
            }
            Some(e) => {
                validate_semantic_id(
                    &mut errors,
                    &format!("edges[{i}].source"),
                    e.get("source"),
                    limits,
                );
                validate_semantic_id(
                    &mut errors,
                    &format!("edges[{i}].target"),
                    e.get("target"),
                    limits,
                );
            }
        }
    }

    let hyperedges = match obj.get("hyperedges") {
        None | Some(Value::Null) => &empty_arr,
        Some(v) => match v.as_array() {
            Some(arr) => {
                if arr.len() > limits.max_hyperedges {
                    errors.push(format!(
                        "hyperedges has {} entries; max is {}",
                        arr.len(),
                        limits.max_hyperedges
                    ));
                }
                arr
            }
            None => {
                errors.push("hyperedges must be a list".to_string());
                &empty_arr
            }
        },
    };

    for (i, he) in hyperedges.iter().enumerate() {
        match he.as_object() {
            None => {
                errors.push(format!("hyperedges[{i}] must be an object"));
                continue;
            }
            Some(h) => {
                validate_semantic_id(
                    &mut errors,
                    &format!("hyperedges[{i}].id"),
                    h.get("id"),
                    limits,
                );
                match h.get("nodes") {
                    None => {
                        errors.push(format!("hyperedges[{i}].nodes must be a list"));
                    }
                    Some(v) => match v.as_array() {
                        None => {
                            errors.push(format!("hyperedges[{i}].nodes must be a list"));
                        }
                        Some(he_nodes) => {
                            if he_nodes.len() > limits.max_hyperedge_nodes {
                                errors.push(format!(
                                    "hyperedges[{i}].nodes has {} entries; max is {}",
                                    he_nodes.len(),
                                    limits.max_hyperedge_nodes
                                ));
                            }
                            for (j, r) in he_nodes.iter().enumerate() {
                                validate_semantic_id(
                                    &mut errors,
                                    &format!("hyperedges[{i}].nodes[{j}]"),
                                    Some(r),
                                    limits,
                                );
                            }
                        }
                    },
                }
            }
        }
    }

    errors
}

fn validate_semantic_id(
    errors: &mut Vec<String>,
    field: &str,
    value: Option<&Value>,
    limits: &SemanticLimits,
) {
    match value {
        None | Some(Value::Null) => {
            errors.push(format!("{field} must be a string"));
        }
        Some(v) => match v.as_str() {
            None => {
                errors.push(format!("{field} must be a string"));
            }
            Some(s) => {
                if s.is_empty() {
                    errors.push(format!("{field} must not be empty"));
                    return;
                }
                if s.len() > limits.max_id_length {
                    errors.push(format!(
                        "{field} is {} chars; max is {}",
                        s.len(),
                        limits.max_id_length
                    ));
                }
                if s.contains('/') || s.contains('\\') || s.contains("..") {
                    errors.push(format!("{field} must not contain path separators or '..'"));
                }
                if !semantic_id_re().is_match(s) {
                    errors.push(format!("{field} contains unsupported characters"));
                }
            }
        },
    }
}

pub fn load_validated_semantic_fragment(path: &Path) -> (Option<Value>, Vec<String>) {
    let limits = SemanticLimits::default();
    load_validated_semantic_fragment_with_limits(path, &limits)
}

pub fn load_validated_semantic_fragment_with_limits(
    path: &Path,
    limits: &SemanticLimits,
) -> (Option<Value>, Vec<String>) {
    let size = match std::fs::metadata(path) {
        Err(e) => {
            return (
                None,
                vec![format!("could not stat {}: {}", path.display(), e)],
            )
        }
        Ok(m) => m.len() as usize,
    };
    if size > limits.max_bytes {
        return (
            None,
            vec![format!(
                "payload is {} bytes; max is {}",
                size, limits.max_bytes
            )],
        );
    }
    let text = match std::fs::read_to_string(path) {
        Err(e) => {
            return (
                None,
                vec![format!("could not read {}: {}", path.display(), e)],
            )
        }
        Ok(t) => t,
    };
    let fragment: Value = match serde_json::from_str(&text) {
        Err(e) => return (None, vec![format!("invalid JSON: {}", e)]),
        Ok(v) => v,
    };
    let errors = validate_semantic_fragment_with_limits(&fragment, limits);
    if errors.is_empty() {
        (Some(fragment), vec![])
    } else {
        (None, errors)
    }
}

pub fn sanitize_semantic_fragment(fragment: &mut Value) -> &mut Value {
    let obj = match fragment.as_object_mut() {
        Some(o) => o,
        None => return fragment,
    };

    let invalid_ft: HashSet<&str> = ["rationale", "concept"].iter().copied().collect();

    let nodes: Vec<Value> = obj
        .get("nodes")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    let edges: Vec<Value> = obj
        .get("edges")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    let hyperedges: Vec<Value> = obj
        .get("hyperedges")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();

    let mut node_by_id: HashMap<String, Value> = HashMap::new();
    for n in &nodes {
        if let Some(id) = n.get("id").and_then(|v| v.as_str()) {
            if !id.is_empty() {
                node_by_id.insert(id.to_string(), n.clone());
            }
        }
    }

    let rationale_for_sources: HashSet<String> = edges
        .iter()
        .filter(|e| e.get("relation").and_then(|v| v.as_str()) == Some("rationale_for"))
        .filter_map(|e| {
            e.get("source")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string())
        })
        .collect();

    let mut rationale_candidates: Vec<Value> = Vec::new();
    let mut remove_ids: HashSet<String> = HashSet::new();
    let mut keep_nodes: Vec<Value> = Vec::new();

    for n in nodes {
        let nid = match n.get("id").and_then(|v| v.as_str()) {
            Some(id) if !id.is_empty() => id.to_string(),
            _ => continue,
        };
        let ft = n.get("file_type").and_then(|v| v.as_str()).unwrap_or("");
        let label = n.get("label").and_then(|v| v.as_str()).unwrap_or("");
        if invalid_ft.contains(ft) {
            if is_sentence_like_rationale_label(label) {
                rationale_candidates.push(n.clone());
            }
            remove_ids.insert(nid);
            continue;
        }
        if rationale_for_sources.contains(&nid) && is_sentence_like_rationale_label(label) {
            rationale_candidates.push(n.clone());
            remove_ids.insert(nid);
            continue;
        }
        keep_nodes.push(n);
    }

    let mut rationale_attrs: HashMap<String, Vec<String>> = HashMap::new();
    for rn in &rationale_candidates {
        let rn_id = match rn.get("id").and_then(|v| v.as_str()) {
            Some(id) => id,
            None => continue,
        };
        let text = rn
            .get("label")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .trim()
            .to_string();
        for e in &edges {
            if e.get("relation").and_then(|v| v.as_str()) != Some("rationale_for") {
                continue;
            }
            if e.get("source").and_then(|v| v.as_str()) != Some(rn_id) {
                continue;
            }
            let target_id = match e.get("target").and_then(|v| v.as_str()) {
                Some(t) => t.to_string(),
                None => continue,
            };
            if !node_by_id.contains_key(&target_id) || remove_ids.contains(&target_id) {
                continue;
            }
            rationale_attrs
                .entry(target_id)
                .or_default()
                .push(text.clone());
        }
    }

    for n in &mut keep_nodes {
        let nid = n
            .get("id")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        if let Some(texts) = rationale_attrs.get(&nid) {
            append_rationale_attr(n, texts);
        }
    }

    let keep_edges: Vec<Value> = edges
        .into_iter()
        .filter(|e| {
            let src = e.get("source").and_then(|v| v.as_str()).unwrap_or("");
            let tgt = e.get("target").and_then(|v| v.as_str()).unwrap_or("");
            !remove_ids.contains(src) && !remove_ids.contains(tgt)
        })
        .collect();

    let surviving_ids: HashSet<String> = keep_nodes
        .iter()
        .filter_map(|n| n.get("id").and_then(|v| v.as_str()).map(|s| s.to_string()))
        .filter(|s| !s.is_empty())
        .collect();

    let keep_hyperedges: Vec<Value> = hyperedges
        .into_iter()
        .filter_map(|he| {
            let he_obj = he.as_object()?;
            let he_nodes = he_obj.get("nodes")?.as_array()?;
            let filtered: Vec<Value> = he_nodes
                .iter()
                .filter(|r| {
                    r.as_str()
                        .map(|s| surviving_ids.contains(s))
                        .unwrap_or(false)
                })
                .cloned()
                .collect();
            if filtered.len() < 2 {
                return None;
            }
            let mut new_he = he_obj.clone();
            new_he.insert("nodes".to_string(), Value::Array(filtered));
            Some(Value::Object(new_he))
        })
        .collect();

    let obj = fragment.as_object_mut().unwrap();
    obj.insert("nodes".to_string(), Value::Array(keep_nodes));
    obj.insert("edges".to_string(), Value::Array(keep_edges));
    obj.insert("hyperedges".to_string(), Value::Array(keep_hyperedges));
    fragment
}

fn is_sentence_like_rationale_label(label: &str) -> bool {
    if label.is_empty() {
        return false;
    }
    let label = label.trim();
    if label.len() < RATIONALE_MIN_CHARS {
        let word_count = label.split_whitespace().count();
        if word_count < RATIONALE_MIN_WORDS {
            return false;
        }
    }
    label.contains('.') || label.contains('!') || label.contains('?') || label.contains(':')
}

fn append_rationale_attr(node: &mut Value, texts: &[String]) {
    let new_text = texts.join("\n\n").trim().to_string();
    if let Some(obj) = node.as_object_mut() {
        let existing = obj
            .get("rationale")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let combined = if existing.is_empty() {
            new_text
        } else {
            format!("{}\n\n{}", existing, new_text)
        };
        obj.insert("rationale".to_string(), json!(combined));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn valid_fragment() -> Value {
        json!({
            "nodes": [{"id": "module_func", "label": "func", "file_type": "code"}],
            "edges": [{"source": "module_func", "target": "other_node"}],
            "hyperedges": [],
        })
    }

    #[test]
    fn test_validate_semantic_fragment_accepts_valid() {
        assert_eq!(
            validate_semantic_fragment(&valid_fragment()),
            Vec::<String>::new()
        );
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_non_object() {
        let errors = validate_semantic_fragment(&json!(["not", "an", "object"]));
        assert!(errors.iter().any(|e| e.to_lowercase().contains("object")));
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_oversize_payload() {
        let limits = SemanticLimits {
            max_bytes: 64,
            ..SemanticLimits::default()
        };
        let mut fragment = valid_fragment();
        fragment["nodes"][0]["label"] = json!("x".repeat(128));
        let errors = validate_semantic_fragment_with_limits(&fragment, &limits);
        assert!(errors.iter().any(|e| e.to_lowercase().contains("payload")));
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_too_many_nodes() {
        let limits = SemanticLimits {
            max_nodes: 1,
            ..SemanticLimits::default()
        };
        let mut fragment = valid_fragment();
        fragment["nodes"]
            .as_array_mut()
            .unwrap()
            .push(json!({"id": "extra", "label": "extra", "file_type": "code"}));
        let errors = validate_semantic_fragment_with_limits(&fragment, &limits);
        assert!(errors.iter().any(|e| e.to_lowercase().contains("nodes")));
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_too_many_edges() {
        let limits = SemanticLimits {
            max_edges: 0,
            ..SemanticLimits::default()
        };
        let errors = validate_semantic_fragment_with_limits(&valid_fragment(), &limits);
        assert!(errors.iter().any(|e| e.to_lowercase().contains("edges")));
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_path_separator_in_id() {
        let mut fragment = valid_fragment();
        fragment["nodes"][0]["id"] = json!("../etc/passwd");
        let errors = validate_semantic_fragment(&fragment);
        assert!(errors.iter().any(|e| e.contains("nodes[0].id")));
    }

    #[test]
    fn test_validate_semantic_fragment_rejects_invalid_file_type() {
        let mut fragment = valid_fragment();
        fragment["nodes"][0]["file_type"] = json!("executable");
        let errors = validate_semantic_fragment(&fragment);
        assert!(errors.iter().any(|e| e.contains("file_type")));
    }

    #[test]
    fn test_validate_semantic_fragment_accepts_rationale_file_type() {
        let mut fragment = valid_fragment();
        fragment["nodes"][0]["file_type"] = json!("rationale");
        let errors = validate_semantic_fragment(&fragment);
        assert!(
            !errors.iter().any(|e| e.contains("file_type")),
            "'rationale' must be accepted; errors: {errors:?}"
        );
    }

    #[test]
    fn test_validate_semantic_fragment_accepts_concept_file_type() {
        let mut fragment = valid_fragment();
        fragment["nodes"][0]["file_type"] = json!("concept");
        let errors = validate_semantic_fragment(&fragment);
        assert!(
            !errors.iter().any(|e| e.contains("file_type")),
            "'concept' must be accepted; errors: {errors:?}"
        );
    }

    #[test]
    fn test_load_validated_semantic_fragment_accepts_valid() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("chunk.json");
        std::fs::write(&path, serde_json::to_string(&valid_fragment()).unwrap()).unwrap();
        let (fragment, errors) = load_validated_semantic_fragment(&path);
        assert_eq!(errors, Vec::<String>::new());
        assert!(fragment.is_some());
    }

    #[test]
    fn test_load_validated_semantic_fragment_rejects_oversize_before_parse() {
        let limits = SemanticLimits {
            max_bytes: 64,
            ..SemanticLimits::default()
        };
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("chunk.json");
        let content: String = std::iter::repeat_n('"', 128).collect::<String>();
        std::fs::write(&path, format!("[{}]", content)).unwrap();
        let (fragment, errors) = load_validated_semantic_fragment_with_limits(&path, &limits);
        assert!(fragment.is_none());
        assert!(errors.iter().any(|e| e.to_lowercase().contains("payload")));
    }

    #[test]
    fn test_load_validated_semantic_fragment_rejects_invalid_json() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.json");
        std::fs::write(&path, "{not valid json").unwrap();
        let (fragment, errors) = load_validated_semantic_fragment(&path);
        assert!(fragment.is_none());
        assert!(errors
            .iter()
            .any(|e| e.to_lowercase().contains("invalid json")));
    }

    #[test]
    fn test_validate_hyperedge_rejects_bad_id() {
        let mut fragment = valid_fragment();
        fragment["hyperedges"] = json!([
            {"id": "../escape", "label": "x", "nodes": ["module_func", "module_func"]}
        ]);
        let errors = validate_semantic_fragment(&fragment);
        assert!(errors.iter().any(|e| e.contains("hyperedges[0].id")));
    }

    #[test]
    fn test_validate_hyperedge_rejects_bad_node_ref() {
        let mut fragment = valid_fragment();
        fragment["hyperedges"] = json!([
            {"id": "valid_he", "label": "x", "nodes": ["module_func", "../bad_ref"]}
        ]);
        let errors = validate_semantic_fragment(&fragment);
        assert!(errors.iter().any(|e| e.contains("hyperedges[0].nodes[1]")));
    }

    #[test]
    fn test_validate_hyperedge_requires_list() {
        let mut fragment = valid_fragment();
        fragment["hyperedges"] = json!([{"id": "valid_he", "label": "x", "nodes": "not a list"}]);
        let errors = validate_semantic_fragment(&fragment);
        assert!(errors.iter().any(|e| e.contains("hyperedges[0].nodes")));
    }

    #[test]
    fn test_validate_hyperedge_caps_count() {
        let limits = SemanticLimits {
            max_hyperedges: 1,
            ..SemanticLimits::default()
        };
        let mut fragment = valid_fragment();
        fragment["hyperedges"] = json!([
            {"id": "he_0", "label": "x", "nodes": ["module_func", "module_func"]},
            {"id": "he_1", "label": "x", "nodes": ["module_func", "module_func"]},
            {"id": "he_2", "label": "x", "nodes": ["module_func", "module_func"]},
        ]);
        let errors = validate_semantic_fragment_with_limits(&fragment, &limits);
        assert!(errors.iter().any(|e| e.contains("hyperedges has 3")));
    }

    #[test]
    fn test_sanitize_drops_rationale_filetype_node() {
        let mut fragment = json!({
            "nodes": [
                {"id": "real_node", "label": "Real", "file_type": "code"},
                {"id": "garbage", "label": "junk", "file_type": "rationale"},
            ],
            "edges": [],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let ids: HashSet<&str> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert!(ids.contains("real_node"));
        assert!(!ids.contains("garbage"));
    }

    #[test]
    fn test_sanitize_converts_sentence_rationale_node_to_attribute() {
        let mut fragment = json!({
            "nodes": [
                {"id": "real_node", "label": "Real", "file_type": "code"},
                {
                    "id": "why_node",
                    "label": "We chose tree-sitter because the deterministic parser is faster than regex-based extraction.",
                    "file_type": "rationale",
                },
            ],
            "edges": [{"source": "why_node", "target": "real_node", "relation": "rationale_for"}],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let ids: HashSet<&str> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert!(!ids.contains("why_node"));
        let target = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .find(|n| n["id"].as_str() == Some("real_node"))
            .unwrap();
        assert!(target["rationale"]
            .as_str()
            .unwrap_or("")
            .contains("tree-sitter"));
    }

    #[test]
    fn test_sanitize_converts_allowed_filetype_sentence_via_rationale_for_edge() {
        let long_label = "Decision: this node has sentence-like rationale text but uses an \
            allowed file_type, so it should not survive as a standalone graph node.";
        let mut fragment = json!({
            "nodes": [
                {"id": "real_node", "label": "Real", "file_type": "code"},
                {"id": "sentence_node", "label": long_label, "file_type": "document"},
            ],
            "edges": [{"source": "sentence_node", "target": "real_node", "relation": "rationale_for"}],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let ids: HashSet<&str> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert!(!ids.contains("sentence_node"));
        let target = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .find(|n| n["id"].as_str() == Some("real_node"))
            .unwrap();
        assert!(target["rationale"]
            .as_str()
            .unwrap_or("")
            .contains("Decision"));
    }

    #[test]
    fn test_sanitize_keeps_short_concept_named_node_with_punctuation() {
        let mut fragment = json!({
            "nodes": [
                {"id": "a_b", "label": "a.b.c", "file_type": "document"},
                {"id": "anchor", "label": "Anchor", "file_type": "code"},
            ],
            "edges": [{"source": "a_b", "target": "anchor", "relation": "rationale_for"}],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let ids: HashSet<&str> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert!(ids.contains("a_b"));
        assert!(ids.contains("anchor"));
    }

    #[test]
    fn test_sanitize_filters_hyperedges_after_node_removal() {
        let mut fragment = json!({
            "nodes": [
                {"id": "real_node", "label": "Real", "file_type": "code"},
                {"id": "other", "label": "Other", "file_type": "code"},
                {"id": "garbage", "label": "junk", "file_type": "rationale"},
            ],
            "edges": [],
            "hyperedges": [
                {"id": "group_a", "label": "Group A", "nodes": ["garbage", "real_node", "other"], "relation": "participate_in"},
                {"id": "group_b", "label": "Group B (only one survivor)", "nodes": ["garbage", "real_node"], "relation": "participate_in"},
            ],
        });
        sanitize_semantic_fragment(&mut fragment);
        let he_ids: HashSet<&str> = fragment["hyperedges"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|he| he["id"].as_str())
            .collect();
        assert!(he_ids.contains("group_a"));
        assert!(!he_ids.contains("group_b"));
        let group_a = fragment["hyperedges"]
            .as_array()
            .unwrap()
            .iter()
            .find(|he| he["id"].as_str() == Some("group_a"))
            .unwrap();
        let nodes: HashSet<&str> = group_a["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|v| v.as_str())
            .collect();
        assert!(!nodes.contains("garbage"));
        assert_eq!(nodes, ["real_node", "other"].iter().copied().collect());
    }

    #[test]
    fn test_sanitize_drops_hyperedge_with_only_unknown_refs() {
        let mut fragment = json!({
            "nodes": [{"id": "real_node", "label": "Real", "file_type": "code"}],
            "edges": [],
            "hyperedges": [{"id": "phantom", "label": "Phantom", "nodes": ["ghost1", "ghost2"]}],
        });
        sanitize_semantic_fragment(&mut fragment);
        assert_eq!(
            fragment["hyperedges"].as_array().unwrap(),
            &Vec::<Value>::new()
        );
    }

    #[test]
    fn test_sanitize_boundary_sentence_threshold() {
        // 8 words + colon → sentence-like, gets removed and becomes rationale
        let long_label = "Note: alpha beta gamma delta epsilon zeta eta";
        let mut fragment = json!({
            "nodes": [
                {"id": "anchor", "label": "Anchor", "file_type": "code"},
                {"id": "n1", "label": long_label, "file_type": "rationale"},
            ],
            "edges": [{"source": "n1", "target": "anchor", "relation": "rationale_for"}],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let ids: HashSet<&str> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert_eq!(ids, ["anchor"].iter().copied().collect());
        let anchor = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .find(|n| n["id"].as_str() == Some("anchor"))
            .unwrap();
        assert!(anchor["rationale"].as_str().unwrap_or("").contains("alpha"));

        // 7 words, no terminal punctuation → not sentence-like, but still removed (rationale ft) without attribute
        let short_label = "alpha beta gamma delta epsilon zeta eta";
        let mut fragment2 = json!({
            "nodes": [
                {"id": "anchor", "label": "Anchor", "file_type": "code"},
                {"id": "n2", "label": short_label, "file_type": "rationale"},
            ],
            "edges": [],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment2);
        let ids2: HashSet<&str> = fragment2["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str())
            .collect();
        assert_eq!(ids2, ["anchor"].iter().copied().collect());
        let anchor2 = fragment2["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .find(|n| n["id"].as_str() == Some("anchor"))
            .unwrap();
        assert!(anchor2.get("rationale").is_none());
    }

    #[test]
    fn test_sanitize_rationale_only_propagates_through_rationale_for_edges() {
        let mut fragment = json!({
            "nodes": [
                {"id": "rationale_target", "label": "Rationale Target", "file_type": "code"},
                {"id": "unrelated_target", "label": "Unrelated Target", "file_type": "code"},
                {
                    "id": "why_node",
                    "label": "Decision: we chose tree-sitter because the deterministic parser is faster than regex-based extraction.",
                    "file_type": "rationale",
                },
            ],
            "edges": [
                {"source": "why_node", "target": "rationale_target", "relation": "rationale_for"},
                {"source": "why_node", "target": "unrelated_target", "relation": "references"},
            ],
            "hyperedges": [],
        });
        sanitize_semantic_fragment(&mut fragment);
        let id_map: HashMap<&str, &Value> = fragment["nodes"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|n| n["id"].as_str().map(|id| (id, n)))
            .collect();
        assert!(!id_map.contains_key("why_node"));
        assert!(id_map["rationale_target"]["rationale"]
            .as_str()
            .unwrap_or("")
            .contains("tree-sitter"));
        assert!(id_map["unrelated_target"].get("rationale").is_none());
    }
}