axiom_engine 0.5.0

Axiom-TTT Inference Engine – Rust implementation using candle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
//! skeleton.rs — Claude-READABLE compression of heavy context.
//!
//! The neural fingerprint (vocab indices + Frobenius norms) is meaningful to the
//! Axiom model but opaque to a *different* model like Claude — shipping it
//! upstream wastes tokens and degrades answers. Instead we ship a compact
//! structural *skeleton*: doc summary, imports, and declaration signatures with
//! bodies elided. It is small (≈80% smaller than the source) AND readable, so
//! Claude can answer accurately from real signatures.
//!
//! Axiom's neural capability is untouched: the TTT session already absorbed the
//! heavy context into its fast-weights (adapt_session), and the drift signal
//! (recall_norm + state_hash) rides along as tiny attributes on the digest.

use std::cmp::Ordering;
use std::collections::HashSet;

use tree_sitter::{Node, Parser, Query};

/// Visibility / async prefixes stripped before testing for a declaration.
const VIS_PREFIXES: [&str; 9] = [
    "pub ",
    "public ",
    "private ",
    "protected ",
    "export ",
    "default ",
    "async ",
    "static ",
    "final ",
];

/// Keywords that begin a declaration whose signature we keep. Covers Rust, Go,
/// Python, JS/TS, Java/C#, C/C++.
const DECL_KEYWORDS: [&str; 18] = [
    "fn ",
    "func ",
    "def ",
    "function ",
    "struct ",
    "enum ",
    "trait ",
    "impl ",
    "impl<",
    "interface ",
    "class ",
    "type ",
    "const ",
    "static ",
    "mod ",
    "namespace ",
    "package ",
    "module ",
];

/// Control-flow keywords that can also end a line with `{` — never signatures.
const CONTROL_KEYWORDS: [&str; 13] = [
    "if ", "if(", "for ", "for(", "while ", "while(", "switch ", "switch(", "match ", "else",
    "do ", "try ", "catch",
];

/// Heuristic: a method/function signature with no leading keyword (JS/TS class
/// methods, Java/C# methods). The line opens a block `{`, has a parameter list
/// `(...)`, and is not control flow. Keeps brace-language methods that the
/// keyword list alone would miss.
fn looks_like_signature(t: &str) -> bool {
    let trimmed = t.trim_end();
    if !trimmed.ends_with('{') {
        return false;
    }
    if !t.contains('(') || !t.contains(')') {
        return false;
    }
    if t.starts_with('}') || t.starts_with("//") || t.starts_with('*') || t.starts_with('@') {
        return false;
    }
    !CONTROL_KEYWORDS.iter().any(|c| t.starts_with(c))
}

/// Largest char boundary <= idx (safe string slicing).
fn floor_boundary(s: &str, mut idx: usize) -> usize {
    if idx >= s.len() {
        return s.len();
    }
    while idx > 0 && !s.is_char_boundary(idx) {
        idx -= 1;
    }
    idx
}

/// Smallest char boundary >= idx (safe string slicing).
fn ceil_boundary(s: &str, mut idx: usize) -> usize {
    while idx < s.len() && !s.is_char_boundary(idx) {
        idx += 1;
    }
    idx
}

/// Prose / non-code fallback: a signature skeleton would destroy plain text, so
/// keep a head + tail excerpt instead. Still compresses large prose heavily.
fn prose_excerpt(text: &str, head_budget: usize, tail_budget: usize) -> String {
    if text.len() <= head_budget + tail_budget + 64 {
        return text.trim().to_string();
    }
    let head_end = floor_boundary(text, head_budget);
    let tail_start = ceil_boundary(text, text.len().saturating_sub(tail_budget));
    let elided = tail_start.saturating_sub(head_end);
    format!(
        "{}\n… [{elided} chars of prose elided] …\n{}",
        text[..head_end].trim_end(),
        text[tail_start..].trim_start()
    )
}

fn is_import(t: &str) -> bool {
    ["use ", "import ", "from ", "#include", "require("]
        .iter()
        .any(|p| t.starts_with(p))
}

fn is_doc(t: &str) -> bool {
    t.starts_with("///")
        || t.starts_with("//!")
        || t.starts_with("# ")
        || t.starts_with("\"\"\"")
        || t.starts_with("/**")
}

fn rust_ast_body(heavy: &str, max_doc_lines: usize) -> Option<(String, usize)> {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_rust::LANGUAGE.into())
        .ok()?;
    let tree = parser.parse(heavy, None)?;
    let language = tree_sitter_rust::LANGUAGE.into();
    let query = Query::new(
        &language,
        r#"
        (use_declaration) @import
        (function_item) @decl
        (struct_item) @decl
        (enum_item) @decl
        (trait_item) @decl
        (impl_item) @decl
        (type_item) @decl
        (const_item) @decl
        (static_item) @decl
        (mod_item) @decl
        (macro_definition) @decl
        (line_comment) @doc
        (block_comment) @doc
        "#,
    )
    .ok()?;

    let _capture_names = query.capture_names();
    let mut captured: Vec<(usize, &'static str, Node)> = Vec::new();
    collect_rust_captures(tree.root_node(), &mut captured);
    captured.sort_by_key(|(start, _, _)| *start);

    let mut out: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    let mut doc_budget = max_doc_lines;
    let mut elided = 0usize;
    let mut decl_count = 0usize;

    for (_, capture_name, node) in captured {
        if capture_name == "decl" {
            decl_count += 1;
        }
        let rendered = match capture_name {
            "import" => node_text(node, heavy)
                .map(str::trim_end)
                .map(str::to_string),
            "doc" => {
                let text = node_text(node, heavy)?.trim();
                if !is_doc(text) || doc_budget == 0 {
                    elided += 1;
                    None
                } else {
                    doc_budget -= 1;
                    Some(text.to_string())
                }
            }
            "decl" => render_rust_decl(node, heavy),
            _ => None,
        };
        if let Some(line) = rendered {
            push_unique_structural(&line, &mut out, &mut seen, &mut elided);
        } else {
            elided += 1;
        }
    }

    if out.is_empty() {
        return None;
    }
    if decl_count > 0 || elided > 0 || tree.root_node().has_error() {
        out.push(format!(
            "// … {} implementation lines elided …",
            elided.max(1)
        ));
    }
    Some((out.join("\n"), elided))
}

fn collect_rust_captures<'tree>(
    node: Node<'tree>,
    captured: &mut Vec<(usize, &'static str, Node<'tree>)>,
) {
    let capture = match node.kind() {
        "use_declaration" => Some("import"),
        "line_comment" | "block_comment" => Some("doc"),
        "function_item" | "struct_item" | "enum_item" | "trait_item" | "impl_item"
        | "type_item" | "const_item" | "static_item" | "mod_item" | "macro_definition" => {
            Some("decl")
        }
        _ => None,
    };
    if let Some(name) = capture {
        captured.push((node.start_byte(), name, node));
    }
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i as u32) {
            collect_rust_captures(child, captured);
        }
    }
}

fn node_text<'a>(node: Node, source: &'a str) -> Option<&'a str> {
    node.utf8_text(source.as_bytes()).ok()
}

fn render_rust_decl(node: Node, source: &str) -> Option<String> {
    match node.kind() {
        "function_item" => render_until_child(node, source, &["block"], " { … }"),
        "impl_item" | "trait_item" | "mod_item" => {
            render_until_child(node, source, &["declaration_list", "block"], " { … }")
        }
        "struct_item" | "enum_item" | "macro_definition" => render_until_child(
            node,
            source,
            &[
                "field_declaration_list",
                "ordered_field_declaration_list",
                "enum_variant_list",
                "token_tree",
            ],
            " { … }",
        ),
        "const_item" | "static_item" => render_until_equals(node, source),
        _ => node_text(node, source).map(|s| s.trim_end().to_string()),
    }
}

fn render_until_child(
    node: Node,
    source: &str,
    child_kinds: &[&str],
    suffix: &str,
) -> Option<String> {
    let child = first_named_child_kind(node, child_kinds);
    let end = child
        .map(|n| n.start_byte())
        .unwrap_or_else(|| node.end_byte());
    let start = node.start_byte();
    let prefix = source.get(start..end)?.trim_end();
    Some(format!(
        "{}{}",
        prefix.trim_end_matches('{').trim_end(),
        suffix
    ))
}

fn first_named_child_kind<'tree>(node: Node<'tree>, child_kinds: &[&str]) -> Option<Node<'tree>> {
    for i in 0..node.named_child_count() {
        let child = node.named_child(i as u32)?;
        if child_kinds.iter().any(|kind| child.kind() == *kind) {
            return Some(child);
        }
    }
    None
}

/// Detect a Python-style module-level constant: zero-indentation
/// `UPPER_SNAKE_CASE = ...` assignment. Returns the constant name.
///
/// Rationale (P0 safety): these carry bug-relevant values (e.g.
/// `RISK_ORDER = {"low": 3, ...}`) but match no declaration keyword, so the
/// generic extractor silently drops them. The digest then hides the defect
/// entirely -- worse than an elided body.
fn is_module_constant(line: &str, trimmed: &str) -> Option<String> {
    // Must be at module level (no leading whitespace).
    if line.len() != trimmed.len() {
        return None;
    }
    // Must be an assignment, not a comparison or annotation.
    let eq = trimmed.find('=')?;
    // Exclude `==`, `!=`, `<=`, `>=`, `=>`, `:=`.
    let bytes = trimmed.as_bytes();
    if eq > 0 {
        match bytes[eq - 1] {
            b'=' | b'!' | b'<' | b'>' | b':' => return None,
            _ => {}
        }
    }
    if bytes.get(eq + 1) == Some(&b'=') {
        return None;
    }
    let name = trimmed[..eq].trim_end();
    // UPPER_SNAKE_CASE: starts uppercase, rest uppercase/digits/underscore,
    // at least 2 chars to avoid single-letter noise.
    if name.len() < 2
        || !name
            .chars()
            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
        || !name.chars().next().unwrap().is_ascii_uppercase()
    {
        return None;
    }
    Some(name.to_string())
}

fn render_until_equals(node: Node, source: &str) -> Option<String> {
    let text = node_text(node, source)?.trim_end();
    if let Some(eq) = text.find('=') {
        return Some(format!("{} = …;", text[..eq].trim_end()));
    }
    Some(text.to_string())
}

/// Strip stacked visibility/async prefixes (e.g. "pub async fn") so keyword
/// tests see the real declaration kind.
fn strip_decl_prefix(mut s: &str) -> &str {
    loop {
        let mut changed = false;
        for p in VIS_PREFIXES {
            if let Some(rest) = s.strip_prefix(p) {
                s = rest.trim_start();
                changed = true;
            }
        }
        // pub(crate) / pub(super) etc.
        if let Some(rest) = s.strip_prefix("pub(") {
            if let Some(i) = rest.find(')') {
                s = rest[i + 1..].trim_start();
                changed = true;
            }
        }
        if !changed {
            break;
        }
    }
    s
}

fn is_decl(t: &str) -> bool {
    let s = strip_decl_prefix(t);
    DECL_KEYWORDS.iter().any(|k| s.starts_with(k))
}

/// Function/method declarations: the units diagnostic elision filters.
/// Classes, structs, enums, etc. stay dumb signatures even in diagnostic mode
/// (their interesting content lives in the methods, which are handled
/// separately — this also avoids duplicating method bodies inside the
/// class/impl rendering).
fn is_function_decl(t: &str) -> bool {
    let s = strip_decl_prefix(t);
    const FN_KEYWORDS: [&str; 4] = ["fn ", "func ", "def ", "function "];
    if FN_KEYWORDS.iter().any(|k| s.starts_with(k)) {
        return true;
    }
    looks_like_signature(t)
}

/// True if `t` starts with keyword `kw` followed by a word boundary
/// (so "except" doesn't match "exceptional").
fn starts_with_kw(t: &str, kw: &str) -> bool {
    if let Some(rest) = t.strip_prefix(kw) {
        rest.is_empty() || !is_ident_byte(rest.as_bytes()[0])
    } else {
        false
    }
}

/// Build the compact readable digest from heavy context text.
///
/// `original_tokens`, `recall_norm`, and `state_hash` ride along as attributes so
/// Axiom keeps its drift / session-continuity signal at negligible token cost.
pub fn build_digest(
    heavy: &str,
    session_id: &str,
    original_tokens: usize,
    recall_norm: f32,
    state_hash: &str,
    max_doc_lines: usize,
) -> String {
    let mut out: Vec<String> = Vec::new();
    let mut seen_structural: HashSet<String> = HashSet::new();
    let mut doc_budget = max_doc_lines as i32;
    let mut elided = 0usize;
    let mut code_lines = 0usize; // imports + declarations + brace-method signatures

    if let Some((body, _)) = rust_ast_body(heavy, max_doc_lines) {
        return format!(
            "<axiom_context_digest session=\"{session_id}\" kind=\"structural-skeleton\" \
original_tokens=\"{original_tokens}\" recall_norm=\"{recall_norm:.3}\" state=\"{state_hash}\">\n\
# Lossy digest of elided heavy context. For code: signatures kept, bodies dropped.\n\
# Ask Axiom to expand a named symbol if you need its body.\n\
state_hash={state_hash}\n\
{body}\n\
</axiom_context_digest>"
        );
    }

    for line in heavy.lines() {
        let t = line.trim_start();
        if t.is_empty() {
            continue;
        }
        if is_import(t) {
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            code_lines += 1;
        } else if is_decl(t) || looks_like_signature(t) {
            let sig = line.split('{').next().unwrap_or(line).trim_end();
            let suffix = if line.contains('{') { " { … }" } else { "" };
            let structural = format!("{sig}{suffix}");
            push_unique_structural(&structural, &mut out, &mut seen_structural, &mut elided);
            code_lines += 1;
        } else if is_doc(t) && doc_budget > 0 {
            let before = seen_structural.len();
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            if seen_structural.len() > before {
                doc_budget -= 1;
            }
        } else {
            elided += 1;
        }
    }

    // If nothing structural was found, this is prose/data — a signature skeleton
    // would erase it. Keep a head+tail excerpt instead so meaning survives.
    let (kind, body) = if code_lines == 0 {
        ("prose-excerpt", prose_excerpt(heavy, 1200, 500))
    } else {
        if elided > 0 {
            out.push(format!("// … {elided} implementation lines elided …"));
        }
        ("structural-skeleton", out.join("\n"))
    };

    format!(
        "<axiom_context_digest session=\"{session_id}\" kind=\"{kind}\" \
original_tokens=\"{original_tokens}\" recall_norm=\"{recall_norm:.3}\" state=\"{state_hash}\">\n\
# Lossy digest of elided heavy context. For code: signatures kept, bodies dropped.\n\
# Ask Axiom to expand a named symbol if you need its body.\n\
state_hash={state_hash}\n\
{body}\n\
</axiom_context_digest>"
    )
}

/// Build just the skeleton body (no XML wrapper) for direct human/agent
/// consumption. Returns the structural skeleton: imports, doc comments, and
/// declaration signatures with bodies elided. For prose, returns a head+tail
/// excerpt. This is what `axiom skeleton` prints per file.
pub fn skeleton_body(heavy: &str, max_doc_lines: usize) -> String {
    if let Some((body, _)) = rust_ast_body(heavy, max_doc_lines) {
        return body;
    }

    let mut out: Vec<String> = Vec::new();
    let mut seen_structural: HashSet<String> = HashSet::new();
    let mut doc_budget = max_doc_lines as i32;
    let mut elided = 0usize;
    let mut code_lines = 0usize;

    for line in heavy.lines() {
        let t = line.trim_start();
        if t.is_empty() {
            continue;
        }
        if is_import(t) {
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            code_lines += 1;
        } else if is_decl(t) || looks_like_signature(t) {
            let sig = line.split('{').next().unwrap_or(line).trim_end();
            let suffix = if line.contains('{') { " { … }" } else { "" };
            let structural = format!("{sig}{suffix}");
            push_unique_structural(&structural, &mut out, &mut seen_structural, &mut elided);
            code_lines += 1;
        } else if is_doc(t) && doc_budget > 0 {
            let before = seen_structural.len();
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            if seen_structural.len() > before {
                doc_budget -= 1;
            }
        } else {
            elided += 1;
        }
    }

    if code_lines == 0 {
        prose_excerpt(heavy, 1200, 500)
    } else {
        if elided > 0 {
            out.push(format!("// … {elided} implementation lines elided …"));
        }
        out.join("\n")
    }
}

fn push_unique_structural(
    line: &str,
    out: &mut Vec<String>,
    seen: &mut HashSet<String>,
    elided: &mut usize,
) {
    let normalized = line.trim();
    if normalized.is_empty() {
        return;
    }
    if seen.insert(normalized.to_string()) {
        out.push(line.to_string());
    } else {
        *elided += 1;
    }
}

/// Leading-whitespace width of a line (tabs count as 4) — for indent matching.
fn indent_of(line: &str) -> usize {
    let mut w = 0;
    for ch in line.chars() {
        match ch {
            ' ' => w += 1,
            '\t' => w += 4,
            _ => break,
        }
    }
    w
}

/// True if `name` appears in `line` as a whole identifier (not a substring of a
/// larger identifier).
fn contains_symbol(line: &str, name: &str) -> bool {
    let bytes = line.as_bytes();
    let nb = name.as_bytes();
    if nb.is_empty() {
        return false;
    }
    let mut i = 0;
    while let Some(pos) = line[i..].find(name) {
        let start = i + pos;
        let end = start + name.len();
        let before_ok = start == 0 || !is_ident_byte(bytes[start - 1]);
        let after_ok = end >= bytes.len() || !is_ident_byte(bytes[end]);
        if before_ok && after_ok {
            return true;
        }
        i = end;
    }
    false
}

fn is_ident_byte(b: u8) -> bool {
    b.is_ascii_alphanumeric() || b == b'_'
}

/// Capture a declaration's full block starting at `start`. Brace-delimited blocks
/// are brace-matched; Python-style `:` headers use indentation; otherwise the
/// single declaration line is returned. (Braces inside strings/comments are not
/// specially handled — a known approximation.)
fn capture_block(lines: &[&str], start: usize) -> String {
    let first = lines[start];
    if first.contains('{') {
        let mut depth: i32 = 0;
        let mut out: Vec<&str> = Vec::new();
        for line in &lines[start..] {
            out.push(line);
            for ch in line.chars() {
                if ch == '{' {
                    depth += 1;
                } else if ch == '}' {
                    depth -= 1;
                }
            }
            if depth <= 0 && line.contains('}') {
                break;
            }
        }
        out.join("\n")
    } else if first.trim_end().ends_with(':') {
        let base = indent_of(first);
        let mut out: Vec<&str> = vec![first];
        for line in &lines[start + 1..] {
            if line.trim().is_empty() {
                out.push(line);
                continue;
            }
            if indent_of(line) <= base {
                break;
            }
            out.push(line);
        }
        // Trim trailing blank lines.
        while out.last().map(|l| l.trim().is_empty()).unwrap_or(false) {
            out.pop();
        }
        out.join("\n")
    } else {
        first.to_string()
    }
}

/// Extract the full declaration + body of `name` from `source`. Returns None when
/// no declaration of that symbol is found. This is the retrieval half of the
/// skeleton round-trip: the digest drops bodies, and the proxy can expand any one
/// back on demand from the stored source.
pub fn expand_symbol(source: &str, name: &str) -> Option<String> {
    let lines: Vec<&str> = source.lines().collect();
    for (i, line) in lines.iter().enumerate() {
        let t = line.trim_start();
        if (is_decl(t) || looks_like_signature(t)) && contains_symbol(line, name) {
            return Some(capture_block(&lines, i));
        }
        // Module-level constants (e.g. `RISK_ORDER = {...}`): the digest
        // shows only `RISK_ORDER = …`, so expansion must return the full
        // assignment including a multi-line value.
        if let Some(const_name) = is_module_constant(line, t) {
            if const_name == name {
                return Some(capture_constant_block(&lines, i));
            }
        }
    }
    None
}

/// Capture a module-level constant's full assignment, including multi-line
/// values (dicts, lists). Stops at the first subsequent line at zero
/// indentation that is not a continuation of the value.
fn capture_constant_block(lines: &[&str], start: usize) -> String {
    let mut out = vec![lines[start].to_string()];
    // Track bracket depth to handle multi-line dict/list values.
    let mut depth = 0i32;
    for c in lines[start].chars() {
        match c {
            '{' | '[' | '(' => depth += 1,
            '}' | ']' | ')' => depth -= 1,
            _ => {}
        }
    }
    for line in &lines[start + 1..] {
        // A new zero-indentation line outside any brackets ends the value.
        if depth <= 0 && !line.starts_with(' ') && !line.starts_with('\t') && !line.trim().is_empty() {
            break;
        }
        for c in line.chars() {
            match c {
                '{' | '[' | '(' => depth += 1,
                '}' | ']' | ')' => depth -= 1,
                _ => {}
            }
        }
        out.push(line.to_string());
        if depth <= 0 {
            break;
        }
    }
    out.join("\n")
}

// ---------------------------------------------------------------------------
// PageRank-ranked skeletonization.
//
// `build_digest` keeps every declaration equally: under a token budget the
// output truncates arbitrarily. The ranked variant below builds an intra-file
// reference graph (symbol A -> symbol B when A's body mentions B) and orders
// declarations by PageRank, so a budget keeps the most load-bearing symbols
// first and the skeleton degrades gracefully instead of cutting off mid-file.
// ---------------------------------------------------------------------------

/// One extracted declaration: display signature, full body text (reference
/// scanning only — never rendered), and its PageRank importance score.
struct RankedSymbol {
    name: String,
    signature: String,
    body: String,
    score: f64,
}

/// Approximate token count, matching the Python prototype's chars/4 rule.
fn approx_tokens(s: &str) -> usize {
    (s.len() / 4).max(1)
}

/// PageRank over `adj` (adjacency lists, edge i -> j). Dangling nodes
/// distribute their rank uniformly. Standard power iteration.
fn pagerank(adj: &[Vec<usize>], damping: f64, iterations: usize) -> Vec<f64> {
    let n = adj.len();
    if n == 0 {
        return Vec::new();
    }
    let nf = n as f64;
    let mut rank = vec![1.0 / nf; n];
    let teleport = (1.0 - damping) / nf;
    for _ in 0..iterations {
        let mut next = vec![teleport; n];
        for (i, outs) in adj.iter().enumerate() {
            if outs.is_empty() {
                let share = damping * rank[i] / nf;
                for r in next.iter_mut() {
                    *r += share;
                }
            } else {
                let share = damping * rank[i] / outs.len() as f64;
                for &j in outs {
                    next[j] += share;
                }
            }
        }
        rank = next;
    }
    rank
}

/// Edge i -> j when symbol i's body mentions symbol j's name as a whole
/// identifier (heuristic reference extraction; v1, no type resolution).
fn build_reference_graph(symbols: &[RankedSymbol]) -> Vec<Vec<usize>> {
    let n = symbols.len();
    let mut adj: Vec<Vec<usize>> = vec![Vec::new(); n];
    for i in 0..n {
        for j in 0..n {
            if i == j || symbols[j].name.is_empty() {
                continue;
            }
            if contains_symbol(&symbols[i].body, &symbols[j].name) {
                adj[i].push(j);
            }
        }
    }
    adj
}

/// Best-effort declaration name from a tree-sitter Rust node: the grammar's
/// `name` field when present, else the first identifier-like direct child.
/// For `impl` blocks, prefers the implemented type (`impl Trait for Type`
/// yields `Type`).
fn rust_decl_name(node: Node, source: &str) -> Option<String> {
    if let Some(n) = node.child_by_field_name("name") {
        if let Some(t) = node_text(n, source) {
            let t = t.trim();
            if !t.is_empty() {
                return Some(t.to_string());
            }
        }
    }
    // Direct identifier-like children, in source order, stopping at the body.
    let mut cands: Vec<String> = Vec::new();
    for i in 0..node.named_child_count() {
        let c = node.named_child(i as u32)?;
        match c.kind() {
            "declaration_list" | "block" | "field_declaration_list"
            | "ordered_field_declaration_list" | "enum_variant_list" | "token_tree" => break,
            "identifier" | "type_identifier" => {
                if let Some(t) = node_text(c, source) {
                    let t = t.trim();
                    if !t.is_empty() {
                        cands.push(t.to_string());
                    }
                }
            }
            _ => {}
        }
    }
    if node.kind() == "impl_item" {
        // `impl Trait for Type` — the type is what other code references.
        cands.pop()
    } else {
        cands.into_iter().next()
    }
}

/// Extract (imports, declarations) from Rust via tree-sitter.
fn extract_symbols_rust(heavy: &str) -> (Vec<String>, Vec<RankedSymbol>) {
    let mut imports = Vec::new();
    let mut symbols = Vec::new();
    let mut parser = Parser::new();
    let Ok(()) = parser.set_language(&tree_sitter_rust::LANGUAGE.into()) else {
        return (imports, symbols);
    };
    let Some(tree) = parser.parse(heavy, None) else {
        return (imports, symbols);
    };
    let mut captured: Vec<(usize, &'static str, Node)> = Vec::new();
    collect_rust_captures(tree.root_node(), &mut captured);
    captured.sort_by_key(|(start, _, _)| *start);
    // Key dedup on node start byte, not signature text: methods with
    // identical signatures in different impl blocks are distinct symbols.
    let mut seen: HashSet<usize> = HashSet::new();
    for (_, kind, node) in captured {
        if kind == "import" {
            if let Some(t) = node_text(node, heavy) {
                let t = t.trim_end().to_string();
                if !imports.contains(&t) {
                    imports.push(t);
                }
            }
        } else if kind == "decl" {
            let Some(signature) = render_rust_decl(node, heavy) else {
                continue;
            };
            if !seen.insert(node.start_byte()) {
                continue;
            }
            let name = rust_decl_name(node, heavy).unwrap_or_default();
            let body = node_text(node, heavy).unwrap_or("").to_string();
            symbols.push(RankedSymbol {
                name,
                signature,
                body,
                score: 0.0,
            });
        }
    }
    (imports, symbols)
}

/// Heuristic declaration name from a source line, mirroring `is_decl`'s
/// prefix stripping. `fn add(a: i32)` -> `add`; `class Foo:` -> `Foo`.
fn extract_decl_name(line: &str) -> Option<String> {
    let mut s = line.trim_start();
    // Strip stacked visibility/async prefixes (same as is_decl).
    loop {
        let mut changed = false;
        for p in VIS_PREFIXES {
            if let Some(rest) = s.strip_prefix(p) {
                s = rest.trim_start();
                changed = true;
            }
        }
        if let Some(rest) = s.strip_prefix("pub(") {
            if let Some(i) = rest.find(')') {
                s = rest[i + 1..].trim_start();
                changed = true;
            }
        }
        if !changed {
            break;
        }
    }
    // Case 1: parameter list present — name is the identifier before '('.
    if let Some(paren) = s.find('(') {
        let before = s[..paren].trim_end();
        if let Some(tok) = before.split_whitespace().last() {
            let name = tok.trim_matches(|c: char| !(c.is_ascii_alphanumeric() || c == '_'));
            if !name.is_empty() && !is_decl_keyword(name) {
                return Some(name.to_string());
            }
        }
        // Fell through: the pre-paren token was a keyword (e.g. `const f: fn()`).
    }
    // Case 2: no parens, or keyword fallback — first identifier after the
    // declaration keyword. Skip `impl<...>` generics.
    if let Some(rest) = s.strip_prefix("impl<") {
        if let Some(gt) = rest.find('>') {
            s = rest[gt + 1..].trim_start();
        }
    }
    for kw in DECL_KEYWORDS {
        if let Some(rest) = s.strip_prefix(kw) {
            let mut toks = rest.split_whitespace();
            let mut tok = toks.next()?;
            if tok.starts_with('<') {
                while !tok.contains('>') {
                    tok = toks.next()?;
                }
                tok = toks.next()?;
            }
            let name = tok.trim_matches(|c: char| !(c.is_ascii_alphanumeric() || c == '_'));
            if !name.is_empty() {
                return Some(name.to_string());
            }
        }
    }
    None
}

/// True if `name` is itself a declaration keyword (so `const f: fn()` yields
/// `f`, not `fn`).
fn is_decl_keyword(name: &str) -> bool {
    DECL_KEYWORDS
        .iter()
        .any(|k| k.trim_end() == name || k.trim_end_matches('<') == name)
}

/// Extract (imports, declarations) with the language-agnostic line heuristic
/// used by `build_digest`'s non-Rust path. Bodies come from `capture_block`.
fn extract_symbols_generic(heavy: &str) -> (Vec<String>, Vec<RankedSymbol>) {
    let mut imports = Vec::new();
    let mut symbols = Vec::new();
    let lines: Vec<&str> = heavy.lines().collect();
    let mut seen: HashSet<String> = HashSet::new();
    for (i, line) in lines.iter().enumerate() {
        let t = line.trim_start();
        if t.is_empty() {
            continue;
        }
        if is_import(t) {
            let s = line.trim_end().to_string();
            if !imports.contains(&s) {
                imports.push(s);
            }
        } else if is_decl(t) || looks_like_signature(t) {
            let base = line.split('{').next().unwrap_or(line).trim_end();
            let signature = if line.contains('{') {
                format!("{base} {{ … }}")
            } else {
                base.to_string()
            };
            if !seen.insert(signature.clone()) {
                continue;
            }
            let name = extract_decl_name(line).unwrap_or_default();
            let body = capture_block(&lines, i);
            symbols.push(RankedSymbol {
                name,
                signature,
                body,
                score: 0.0,
            });
        } else if let Some(const_name) = is_module_constant(line, t) {
            // Safety (P0): Python-style UPPER_SNAKE_CASE module constants
            // (e.g. `RISK_ORDER = {...}`) carry bug-relevant values but match
            // no declaration keyword. Capture the name with an elided-value
            // stub so it stays visible under a token budget.
            let signature = format!("{const_name} = …");
            if !seen.insert(signature.clone()) {
                continue;
            }
            symbols.push(RankedSymbol {
                name: const_name,
                signature,
                body: String::new(),
                score: 0.0,
            });
        }
    }
    (imports, symbols)
}

/// Ranked skeletonization: declarations ordered by PageRank importance over the
/// intra-file reference graph, so a token budget keeps the most load-bearing
/// symbols first.
///
/// `language` selects the extractor: `"rust"` (or `"rs"`) uses tree-sitter;
/// anything else uses the language-agnostic line heuristic. `token_budget`
/// caps approximate output tokens (chars/4); `None` keeps everything.
/// Imports always lead (deduped, source order); declarations follow sorted by
/// score descending. At least one line is emitted whenever there is content.
///
/// This is additive: `build_digest` and `expand_symbol` are untouched.
pub fn skeletonize_ranked(
    text: &str,
    language: &str,
    token_budget: Option<usize>,
) -> String {
    let (imports, mut symbols) = if language.eq_ignore_ascii_case("rust")
        || language.eq_ignore_ascii_case("rs")
    {
        extract_symbols_rust(text)
    } else {
        extract_symbols_generic(text)
    };

    if !symbols.is_empty() {
        let adj = build_reference_graph(&symbols);
        let scores = pagerank(&adj, 0.85, 20);
        for (sym, sc) in symbols.iter_mut().zip(scores.iter()) {
            sym.score = *sc;
        }
        symbols.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(Ordering::Equal)
        });
    }

    // No code structure found: fall back to a prose excerpt like build_digest.
    if imports.is_empty() && symbols.is_empty() {
        return prose_excerpt(text, 1200, 500);
    }

    let mut out: Vec<String> = Vec::new();
    let mut used = 0usize;
    let mut elided = 0usize;
    // Cap imports at 25% of budget so declarations (the ranked content)
    // always get room. Without a budget, emit all imports.
    let import_budget = token_budget.map(|b| b / 4);
    let mut import_used = 0usize;
    for imp in &imports {
        let t = approx_tokens(imp);
        if let Some(ibudget) = import_budget {
            // Skip oversized imports even if first: don't let one long
            // `use` line consume the budget needed for ranked declarations.
            if import_used + t > ibudget {
                elided += 1;
                continue;
            }
            import_used += t;
        }
        if let Some(budget) = token_budget {
            if used + t > budget && !out.is_empty() {
                elided += 1;
                continue;
            }
            used += t;
        }
        out.push(imp.clone());
    }
    for sym in &symbols {
        let t = approx_tokens(&sym.signature);
        if let Some(budget) = token_budget {
            if used + t > budget && !out.is_empty() {
                // Safety (P0): never let a symbol vanish entirely. Emit a
                // name-only stub so the agent knows it exists and can expand
                // it. An agent that sees `// charge (… elided …)` knows to
                // ask for the body; an agent that never sees `charge` cannot.
                // The stub is ~1-2 tokens vs the full signature.
                if !sym.name.is_empty() {
                    let stub = format!("// {} (… body elided …)", sym.name);
                    let stub_t = approx_tokens(&stub);
                    if used + stub_t <= budget {
                        out.push(stub);
                        used += stub_t;
                        continue;
                    }
                }
                elided += 1;
                continue;
            }
            used += t;
        }
        out.push(sym.signature.clone());
    }
    if elided > 0 {
        out.push(format!(
            "// … {elided} lower-ranked lines elided by token budget …"
        ));
    }
    out.join("\n")
}

// ---------------------------------------------------------------------------
// Diagnostic elision.
//
// Dumb skeletons drop every function body, which erases diagnostic signal:
// an agent fixing a bug inside a body must read the full file anyway. Diagnostic
// mode keeps the lines that carry diagnostic signal and elides the rest:
//
//   1. Error handling paths: try/except/catch/finally, raise/throw,
//      panic!/unwrap()/expect()/unreachable!, Rust's `?` operator, Err.
//   2. Boundary conditions: comparisons (<,>,<=,>=,==,!=) on values.
//   3. Suspicious patterns: TODO/FIXME/XXX/HACK comments, bare `except:`,
//      asserts, todo!/unimplemented!.
//   4. Complex conditionals: if/while with 3+ conditions (and/or/&&/||).
//   5. Return statements (and Rust tail expressions): a function's contract.
//
// A kept line that opens a block keeps its whole block (the except body, the
// if body, the returned dict literal). Target: 40-60% of source — noticeably
// larger than a dumb skeleton (~20%), still far from full source.
// ---------------------------------------------------------------------------

/// True when the trimmed line carries diagnostic signal worth keeping.
fn is_interesting_line(t: &str) -> bool {
    is_error_handling(t)
        || is_suspicious(t)
        || is_boundary_condition(t)
        || is_complex_conditional(t)
        || is_return_stmt(t)
}

fn is_error_handling(t: &str) -> bool {
    // Python
    if starts_with_kw(t, "try") || starts_with_kw(t, "except") || starts_with_kw(t, "finally") {
        return true;
    }
    if starts_with_kw(t, "raise") {
        return true;
    }
    // JS/TS/Java/C#
    if starts_with_kw(t, "catch") || starts_with_kw(t, "throw") {
        return true;
    }
    // Rust
    if t.contains("panic!") || t.contains("unreachable!") {
        return true;
    }
    if t.contains(".unwrap()") || t.contains(".expect(") {
        return true;
    }
    if t.contains("bail!") || t.contains("ensure!") {
        return true;
    }
    if t.starts_with("Err(") || t.contains("=> Err(") || t.contains("return Err(") {
        return true;
    }
    // Rust `?` try operator: a line ending in `?`. (A `?` inside a comment is
    // a harmless false positive; other languages don't end lines with `?`.)
    if t.ends_with('?') || t.ends_with("?,") || t.ends_with("?;") {
        return true;
    }
    false
}

fn is_suspicious(t: &str) -> bool {
    for m in [
        "TODO",
        "FIXME",
        "XXX",
        "HACK",
        "BUG",
        "KLUDGE",
        "WORKAROUND",
    ] {
        if contains_symbol(t, m) {
            return true;
        }
    }
    // Bare `except:` swallows everything — always worth a look.
    if t == "except:" {
        return true;
    }
    if starts_with_kw(t, "assert") || t.starts_with("debug_assert") {
        return true;
    }
    if t.contains("todo!") || t.contains("unimplemented!") {
        return true;
    }
    false
}

/// If `b[i]` is `<` opening a generic type argument list (e.g. `Vec<T>`,
/// `HashMap<String, Vec<u8>>`, `foo::<T>`), return the index of the matching
/// `>`. None otherwise. Only type-like content may appear between the
/// brackets; anything else (a `;`, `{`, …) means this is not a generic.
fn generic_close(b: &[u8], i: usize) -> Option<usize> {
    // A generic `<` follows an identifier, a nested `>`, or `::` (turbofish).
    let prev = if i > 0 { b[i - 1] } else { return None };
    if !(prev.is_ascii_alphanumeric() || prev == b'_' || prev == b'>' || prev == b':') {
        return None;
    }
    let mut depth = 0i32;
    let mut j = i;
    while j < b.len() {
        match b[j] {
            b'<' => depth += 1,
            b'>' => {
                depth -= 1;
                if depth == 0 {
                    return Some(j);
                }
            }
            b if b.is_ascii_alphanumeric()
                || b == b'_'
                || b == b' '
                || b == b'\t'
                || b == b','
                || b == b':'
                || b == b'&'
                || b == b'\''
                || b == b'['
                || b == b']'
                || b == b'('
                || b == b')'
                || b == b'*'
                || b == b'+'
                || b == b'.'
                || b == b'?'
                || b == b'!'
                || b == b'|' => {}
            _ => return None,
        }
        j += 1;
    }
    None
}

/// True if the line contains a `<` or `>` that is a comparison, not part of
/// `->`, `=>`, `>>`, `<<`, `>=`, `<=`, `<-`, or a generic type argument list
/// like `Vec<T>`.
fn has_bare_comparison(t: &str) -> bool {
    let b = t.as_bytes();
    let mut i = 0;
    while i < b.len() {
        let c = b[i];
        if c == b'<' || c == b'>' {
            let prev = if i > 0 { b[i - 1] } else { 0 };
            let next = if i + 1 < b.len() { b[i + 1] } else { 0 };
            let prev_ok = !(prev == b'<' || prev == b'>' || prev == b'-' || prev == b'=');
            let next_ok = !(next == b'<' || next == b'>' || next == b'=');
            if prev_ok && next_ok {
                if c == b'<' {
                    // Skip generic type argument lists entirely.
                    if let Some(close) = generic_close(b, i) {
                        i = close + 1;
                        continue;
                    }
                }
                return true;
            }
        }
        i += 1;
    }
    false
}

fn is_boundary_condition(t: &str) -> bool {
    t.contains("<=")
        || t.contains(">=")
        || t.contains("==")
        || t.contains("!=")
        || has_bare_comparison(t)
}

/// Count boolean connectors with word boundaries for and/or
/// (so "candle" doesn't count as "and").
fn count_bool_connectors(t: &str) -> usize {
    let mut count = t.matches("&&").count() + t.matches("||").count();
    for tok in t.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) {
        if tok == "and" || tok == "or" {
            count += 1;
        }
    }
    count
}

fn is_complex_conditional(t: &str) -> bool {
    let is_cond =
        starts_with_kw(t, "if") || starts_with_kw(t, "elif") || starts_with_kw(t, "while");
    if !is_cond {
        return false;
    }
    // 3+ conditions joined by boolean connectors.
    count_bool_connectors(t) >= 2
}

fn is_return_stmt(t: &str) -> bool {
    if !starts_with_kw(t, "return") {
        return false;
    }
    // Bare `return` is structural noise; `return <expr>` is the contract.
    t.len() > "return".len()
}

/// If the trimmed line at `lines[start]` opens a block, return the index one
/// past the block's last line. Python-style `:` blocks use indentation;
/// brace blocks use brace balance. None for single-line statements.
fn block_end(lines: &[&str], start: usize) -> Option<usize> {
    let t = lines[start].trim();
    if t.is_empty() || t.starts_with('#') || t.starts_with("//") {
        return None;
    }
    let n = lines.len();
    if t.ends_with(':') {
        // Python-style indent block. (A `:` inside brackets, e.g. slices or
        // dict displays, is never line-final — acceptable approximation.)
        let base = indent_of(lines[start]);
        let mut i = start + 1;
        while i < n {
            let l = lines[i];
            if !l.trim().is_empty() && indent_of(l) <= base {
                break;
            }
            i += 1;
        }
        if i > start + 1 {
            Some(i)
        } else {
            None
        }
    } else if t.contains('{') {
        let mut depth = 0i32;
        let mut i = start;
        loop {
            for ch in lines[i].chars() {
                if ch == '{' {
                    depth += 1;
                } else if ch == '}' {
                    depth -= 1;
                }
            }
            i += 1;
            if depth <= 0 || i >= n {
                break;
            }
        }
        if i > start + 1 {
            Some(i)
        } else {
            None
        }
    } else {
        None
    }

}


/// Filter one function/method body: keep the signature header, interesting
/// lines (plus their blocks), and the closing brace; collapse boring runs
/// into elision markers.
fn diagnostic_symbol_body(body: &str) -> String {
    let lines: Vec<&str> = body.lines().collect();
    let n = lines.len();
    if n == 0 {
        return String::new();
    }
    let mut keep = vec![false; n];

    // Header: signature lines up to and including the block opener.
    let mut idx = 0;
    let mut header_end = 0;
    while idx < n {
        keep[idx] = true;
        header_end = idx + 1;
        let te = lines[idx].trim_end();
        if te.ends_with('{') || te.ends_with(':') {
            break;
        }
        idx += 1;
        if idx > 8 {
            break;
        }
    }

    // Mark interesting lines.
    for (i, line) in lines.iter().enumerate() {
        if keep[i] {
            continue;
        }
        if is_interesting_line(line.trim_start()) {
            keep[i] = true;
        }
    }

    // Interesting block-openers keep their whole block (the except body, the
    // if body, the returned dict literal).
    let mut i = header_end;
    while i < n {
        if keep[i] {
            if let Some(end) = block_end(&lines, i) {
                let end = end.min(n);
                for k in keep.iter_mut().take(end).skip(i) {
                    *k = true;
                }
                // The absorbed block's own nested openers are subsumed.
                i = end;
                continue;
            }
        }
        i += 1;
    }

    // Always keep the final closing brace — it balances the header's opener.
    // (Python bodies have no closing brace; this is a no-op for them.)
    if n >= 1 && lines[n - 1].trim_start().starts_with('}') {
        keep[n - 1] = true;
    }

    // Rust tail expression: the last value before the closing brace is the
    // return value. Keep it when it's a bare expression, not a statement.
    // (Python uses explicit `return`, already covered above.)
    if n >= 2 {
        let last = lines[n - 1].trim();
        if last == "}" || last == "};" {
            let prev_trimmed = lines[n - 2].trim();
            if !prev_trimmed.is_empty()
                && !prev_trimmed.ends_with(';')
                && !prev_trimmed.ends_with('{')
                && !prev_trimmed.ends_with('}')
                && !keep[n - 2]
                && !is_decl(lines[n - 2].trim_start())
            {
                keep[n - 2] = true;
            }
        }
    }

    // Render, collapsing boring runs into markers.
    let mut out: Vec<String> = Vec::new();
    let mut boring = 0usize;
    let mut boring_indent = 0usize;
    for (i, line) in lines.iter().enumerate() {
        if keep[i] {
            if boring > 0 {
                let pad = " ".repeat(boring_indent);
                out.push(format!("{pad}// … [{boring} lines elided] …"));
                boring = 0;
            }
            out.push(line.to_string());
        } else if boring == 0 {
            boring_indent = indent_of(line);
            boring = 1;
        } else {
            boring += 1;
        }
    }
    // Trailing boring lines drop silently — the function just ends.
    out.join("\n")
}

/// Diagnostic skeleton for Rust via tree-sitter: function bodies keep diagnostic
/// signal; other declarations stay dumb signatures. None when no functions
/// are found (caller falls back to the generic path).
fn rust_diagnostic_body(heavy: &str, max_doc_lines: usize) -> Option<String> {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_rust::LANGUAGE.into())
        .ok()?;
    let tree = parser.parse(heavy, None)?;

    let mut captured: Vec<(usize, &'static str, Node)> = Vec::new();
    collect_rust_captures(tree.root_node(), &mut captured);
    captured.sort_by_key(|(start, _, _)| *start);

    let mut out: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    let mut doc_budget = max_doc_lines;
    let mut elided = 0usize;
    let mut fn_count = 0usize;

    for (_, capture_name, node) in captured {
        let rendered = match capture_name {
            "import" => node_text(node, heavy)
                .map(str::trim_end)
                .map(str::to_string),
            "doc" => {
                let text = node_text(node, heavy)?.trim();
                if !is_doc(text) || doc_budget == 0 {
                    elided += 1;
                    None
                } else {
                    doc_budget -= 1;
                    Some(text.to_string())
                }
            }
            "decl" => {
                if node.kind() == "function_item" {
                    fn_count += 1;
                    node_text(node, heavy).map(diagnostic_symbol_body)
                } else {
                    render_rust_decl(node, heavy)
                }
            }
            _ => None,
        };
        if let Some(line) = rendered {
            push_unique_structural(&line, &mut out, &mut seen, &mut elided);
        } else {
            elided += 1;
        }
    }

    if out.is_empty() || fn_count == 0 {
        return None;
    }
    if elided > 0 {
        out.push(format!("// … {elided} fully-elided lines …"));
    }
    Some(out.join("\n"))
}

/// Diagnostic skeleton via the language-agnostic line heuristic (Python, JS/TS,
/// Go, …): function/method bodies keep diagnostic signal; classes and other
/// declarations stay dumb signatures.
fn generic_diagnostic_body(heavy: &str, max_doc_lines: usize) -> String {
    let mut out: Vec<String> = Vec::new();
    let mut seen_structural: HashSet<String> = HashSet::new();
    let mut doc_budget = max_doc_lines as i32;
    let mut elided = 0usize;
    let mut code_lines = 0usize;

    let lines: Vec<&str> = heavy.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        let t = line.trim_start();
        if t.is_empty() {
            i += 1;
            continue;
        }
        if is_import(t) {
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            code_lines += 1;
            i += 1;
        } else if is_decl(t) || looks_like_signature(t) {
            if is_function_decl(t) {
                // Fold leading decorators into the diagnostic body.
                let mut start = i;
                while start > 0 && lines[start - 1].trim_start().starts_with('@') {
                    start -= 1;
                }
                let block = capture_block(&lines, i);
                let consumed = block.lines().count();
                let body = if start < i {
                    format!("{}\n{block}", lines[start..i].join("\n"))
                } else {
                    block
                };
                let diagnostic = diagnostic_symbol_body(&body);
                push_unique_structural(&diagnostic, &mut out, &mut seen_structural, &mut elided);
                code_lines += 1;
                i += consumed.max(1);
            } else {
                let sig = line.split('{').next().unwrap_or(line).trim_end();
                let suffix = if line.contains('{') { " { … }" } else { "" };
                let structural = format!("{sig}{suffix}");
                push_unique_structural(&structural, &mut out, &mut seen_structural, &mut elided);
                code_lines += 1;
                i += 1;
            }
        } else if is_doc(t) && doc_budget > 0 {
            let before = seen_structural.len();
            push_unique_structural(line.trim_end(), &mut out, &mut seen_structural, &mut elided);
            if seen_structural.len() > before {
                doc_budget -= 1;
            }
            i += 1;
        } else {
            elided += 1;
            i += 1;
        }
    }

    if code_lines == 0 {
        prose_excerpt(heavy, 1200, 500)
    } else {
        if elided > 0 {
            out.push(format!("// … {elided} fully-elided lines …"));
        }
        out.join("\n")
    }
}

/// Diagnostic skeleton body (no XML wrapper) for `axiom skeleton --diagnostic`: like
/// [`skeleton_body`] but function/method bodies keep diagnostic signal
/// (error paths, boundary conditions, suspicious patterns, complex
/// conditionals, return values) instead of being fully elided.
pub fn skeleton_body_diagnostic(heavy: &str, max_doc_lines: usize) -> String {
    if let Some(body) = rust_diagnostic_body(heavy, max_doc_lines) {
        return body;
    }
    generic_diagnostic_body(heavy, max_doc_lines)
}

#[cfg(test)]
mod tests {
    #[test]
    fn ranked_keeps_name_stub_when_signature_exceeds_budget() {
        // P0 safety: a low-ranked symbol must not vanish entirely. With a
        // tiny budget, full signatures don't fit, but names must survive.
        let txt = "def alpha():\n    pass\ndef beta():\n    pass\ndef gamma():\n    pass\n";
        let out = skeletonize_ranked(txt, "python", Some(8));
        // At least the names must appear as stubs, even if signatures don't fit.
        let has_stub = out.contains("(… body elided …)");
        let all_names_visible = ["alpha", "beta", "gamma"]
            .iter()
            .all(|n| out.contains(n));
        assert!(
            has_stub || all_names_visible,
            "budget-elided symbols must keep names: {out}"
        );
    }

    #[test]
    fn ranked_emits_all_names_without_budget() {
        let txt = "def alpha():\n    pass\ndef beta():\n    pass\n";
        let out = skeletonize_ranked(txt, "python", None);
        assert!(out.contains("alpha"), "{out}");
        assert!(out.contains("beta"), "{out}");
    }

    #[test]
    fn module_constant_detected() {
        assert_eq!(
            is_module_constant("RISK_ORDER = {\"low\": 1}", "RISK_ORDER = {\"low\": 1}"),
            Some("RISK_ORDER".to_string())
        );
        assert_eq!(
            is_module_constant("MAX_RETRIES = 3", "MAX_RETRIES = 3"),
            Some("MAX_RETRIES".to_string())
        );
    }

    #[test]
    fn module_constant_rejects_non_constants() {
        // Indented (not module-level).
        assert_eq!(is_module_constant("    X = 1", "X = 1"), None);
        // Lowercase (not UPPER_SNAKE_CASE).
        assert_eq!(is_module_constant("risk_order = 1", "risk_order = 1"), None);
        // Comparison, not assignment.
        assert_eq!(is_module_constant("X == 1", "X == 1"), None);
        // Single char (noise).
        assert_eq!(is_module_constant("X = 1", "X = 1"), None);
        // Walrus / annotated.
        assert_eq!(is_module_constant("X := 1", "X := 1"), None);
    }

    #[test]
    fn ranked_captures_python_module_constant() {
        let txt = "RISK_ORDER = {\"low\": 1, \"high\": 3}\ndef helper():\n    pass\n";
        let out = skeletonize_ranked(txt, "python", None);
        assert!(out.contains("RISK_ORDER"), "{out}");
    }

    #[test]
    fn expand_symbol_returns_full_constant_value() {
        let txt = "RISK_ORDER = {\n    \"low\": 1,\n    \"high\": 3,\n}\ndef helper():\n    pass\n";
        let expanded = expand_symbol(txt, "RISK_ORDER").expect("must expand");
        assert!(expanded.contains("\"low\": 1"), "{expanded}");
        assert!(expanded.contains("\"high\": 3"), "{expanded}");
        assert!(!expanded.contains("def helper"), "{expanded}");
    }
    use super::*;

    const SAMPLE: &str = r#"
//! A small module.
use std::collections::HashMap;
pub fn add(a: i32, b: i32) -> i32 {
    let s = a + b;
    s
}
struct Point {
    x: f64,
    y: f64,
}
impl Point {
    pub fn norm(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
    }
}
"#;

    #[test]
    fn keeps_signatures_drops_bodies() {
        let d = build_digest(SAMPLE, "s1", 100, 1.5, "sha256:abc", 3);
        assert!(d.contains("pub fn add(a: i32, b: i32) -> i32 { … }"));
        assert!(d.contains("struct Point { … }"));
        assert!(d.contains("use std::collections::HashMap;"));
        assert!(d.contains("//! A small module."));
        // bodies gone
        assert!(!d.contains("let s = a + b"));
        assert!(!d.contains("sqrt()"));
        // attributes preserved
        assert!(d.contains("recall_norm=\"1.500\""));
        assert!(d.contains("state=\"sha256:abc\""));
    }

    #[test]
    fn elision_counter_present() {
        let d = build_digest(SAMPLE, "s1", 100, 0.0, "h", 3);
        assert!(d.contains("implementation lines elided"));
    }

    #[test]
    fn strips_stacked_visibility() {
        let txt = "pub async fn handler() -> Result<()> {\n  ok()\n}";
        let d = build_digest(txt, "s", 10, 0.0, "h", 0);
        assert!(d.contains("pub async fn handler() -> Result<()> { … }"));
    }

    #[test]
    fn rust_ast_keeps_static_mut_declarations() {
        let txt = "static mut GLOBAL_COUNTER_XQZ: i64 = 0;\nfn run() { unsafe { GLOBAL_COUNTER_XQZ += 1; } }";
        let d = build_digest(txt, "s", 10, 0.0, "h", 0);
        assert!(d.contains("static mut GLOBAL_COUNTER_XQZ: i64 = …;"), "{d}");
        assert!(d.contains("fn run() { … }"));
        assert!(!d.contains("GLOBAL_COUNTER_XQZ += 1"));
    }

    #[test]
    fn go_func_kept() {
        let txt = "package main\nimport \"fmt\"\nfunc Add(a, b int) int {\n  return a + b\n}";
        let d = build_digest(txt, "s", 10, 0.0, "h", 3);
        assert!(d.contains("func Add(a, b int) int { … }"));
        assert!(d.contains("package main"));
        assert!(!d.contains("return a + b"));
    }

    #[test]
    fn python_def_class_kept() {
        let txt = "import os\nclass Foo:\n    def bar(self, x):\n        return x * 2\n";
        let d = build_digest(txt, "s", 10, 0.0, "h", 3);
        assert!(d.contains("class Foo:"));
        assert!(d.contains("def bar(self, x):"));
        assert!(!d.contains("return x * 2"));
    }

    #[test]
    fn skeleton_body_has_no_xml_wrapper() {
        // The CLI-readable body must not include the digest XML envelope.
        let b = skeleton_body(SAMPLE, 3);
        assert!(!b.contains("<axiom_context_digest"));
        assert!(!b.contains("recall_norm="));
        assert!(b.contains("pub fn add(a: i32, b: i32) -> i32 { … }"));
        assert!(b.contains("struct Point { … }"));
        assert!(!b.contains("let s = a + b"));
    }

    #[test]
    fn skeleton_body_python_readable() {
        let txt = "import os\nclass Foo:\n    def bar(self, x):\n        return x * 2\n";
        let b = skeleton_body(txt, 3);
        assert!(b.contains("class Foo:"));
        assert!(b.contains("def bar(self, x):"));
        assert!(!b.contains("return x * 2"));
        assert!(!b.contains("<axiom_context_digest"));
    }

    #[test]
    fn skeleton_body_prose_falls_back_to_excerpt() {
        let txt = "This is just plain prose with no code at all. ".repeat(100);
        let b = skeleton_body(&txt, 3);
        assert!(b.contains("elided"));
        assert!(!b.contains("<axiom_context_digest"));
    }

    #[test]
    fn js_class_method_without_keyword_kept() {
        // Methods with no leading keyword must be caught by looks_like_signature.
        let txt = "class Api {\n  async handle(req, res) {\n    res.send(req.body)\n  }\n}";
        let d = build_digest(txt, "s", 10, 0.0, "h", 3);
        assert!(d.contains("async handle(req, res) { … }"));
        assert!(!d.contains("res.send(req.body)"));
    }

    #[test]
    fn control_flow_not_treated_as_signature() {
        let txt = "fn run() {\n    if (x > 0) {\n        go()\n    }\n    for (i in xs) {\n        step()\n    }\n}";
        let d = build_digest(txt, "s", 10, 0.0, "h", 0);
        assert!(d.contains("fn run() { … }"));
        // control headers must be elided, not kept as signatures
        assert!(!d.contains("if (x > 0) { … }"));
        assert!(!d.contains("for (i in xs) { … }"));
    }

    #[test]
    fn repeated_declarations_are_deduplicated_in_digest() {
        let repeated = "fn calculate_invoice_total(customer_id: &str) -> Result<Money> {\n    lookup_contract_discount(customer_id)?;\n}\n\n".repeat(3);
        let d = build_digest(&repeated, "s", 100, 0.0, "h", 0);
        assert_eq!(d.matches("fn calculate_invoice_total").count(), 1);
        assert!(d.contains("implementation lines elided"));
    }

    #[test]
    fn prose_falls_back_to_excerpt_not_destroyed() {
        // Long plain text with no code structure: must keep readable content,
        // not collapse to "N lines elided".
        let para = "The quarterly report shows revenue grew across all regions. ".repeat(60);
        let d = build_digest(&para, "s", 500, 0.0, "h", 3);
        assert!(d.contains("kind=\"prose-excerpt\""));
        assert!(d.contains("quarterly report shows revenue"));
        assert!(d.contains("chars of prose elided"));
    }

    #[test]
    fn short_prose_kept_whole() {
        let txt = "Just a short note, nothing structural here.";
        let d = build_digest(txt, "s", 10, 0.0, "h", 3);
        assert!(d.contains("Just a short note"));
    }

    #[test]
    fn expand_brace_symbol_returns_full_body() {
        let body = expand_symbol(SAMPLE, "add").unwrap();
        assert!(body.contains("pub fn add(a: i32, b: i32) -> i32 {"));
        assert!(body.contains("let s = a + b;"));
        assert!(body.trim_end().ends_with('}'));
    }

    #[test]
    fn expand_python_uses_indentation() {
        let src = "class Foo:\n    def bar(self, x):\n        y = x * 2\n        return y\n\ndef other():\n    pass\n";
        let body = expand_symbol(src, "bar").unwrap();
        assert!(body.contains("def bar(self, x):"));
        assert!(body.contains("return y"));
        // must stop before the next top-level def
        assert!(!body.contains("def other"));
    }

    #[test]
    fn expand_unknown_symbol_is_none() {
        assert!(expand_symbol(SAMPLE, "does_not_exist").is_none());
    }

    #[test]
    fn expand_matches_whole_identifier_only() {
        // "add" must not match inside "readd" or "address"
        let src = "fn readd() {\n  q()\n}\nfn add() {\n  real()\n}\n";
        let body = expand_symbol(src, "add").unwrap();
        assert!(body.contains("fn add()"));
        assert!(body.contains("real()"));
        assert!(!body.contains("readd"));
    }

    // --- PageRank-ranked skeletonization -----------------------------------

    #[test]
    fn pagerank_orders_by_inbound_references() {
        // 0 -> 1, 0 -> 2, 1 -> 2 : node 2 has the most inbound references.
        let adj = vec![vec![1, 2], vec![2], vec![]];
        let r = pagerank(&adj, 0.85, 20);
        assert!(r[2] > r[1], "{r:?}");
        assert!(r[1] > r[0], "{r:?}");
        let sum: f64 = r.iter().sum();
        assert!((sum - 1.0).abs() < 1e-9, "{sum}");
    }

    #[test]
    fn pagerank_uniform_when_no_edges() {
        let adj = vec![vec![], vec![], vec![]];
        let r = pagerank(&adj, 0.85, 20);
        for v in &r {
            assert!((*v - 1.0 / 3.0).abs() < 1e-9, "{r:?}");
        }
    }

    #[test]
    fn pagerank_empty_graph() {
        let r = pagerank(&[], 0.85, 20);
        assert!(r.is_empty());
    }

    #[test]
    fn ranked_rust_skeleton_orders_callees_first() {
        // util <- helper <- main : the most-depended-upon symbol leads.
        let src = "fn util() -> i32 {\n    1\n}\nfn helper() -> i32 {\n    util() + util()\n}\nfn main() {\n    let x = helper();\n}\n";
        let out = skeletonize_ranked(src, "rust", None);
        let pu = out.find("fn util()").expect("util missing");
        let ph = out.find("fn helper()").expect("helper missing");
        let pm = out.find("fn main()").expect("main missing");
        assert!(pu < ph && ph < pm, "{out}");
    }

    #[test]
    fn ranked_rust_keeps_imports_first() {
        let src = "use std::collections::HashMap;\nfn b() {\n    a();\n}\nfn a() {}\n";
        let out = skeletonize_ranked(src, "rust", None);
        let pi = out.find("use std::collections::HashMap;").unwrap();
        let pa = out.find("fn a()").unwrap();
        assert!(pi < pa, "{out}");
        // a is referenced by b, so a outranks b
        let pb = out.find("fn b()").unwrap();
        assert!(pa < pb, "{out}");
    }

    #[test]
    fn token_budget_keeps_top_symbols() {
        let src = "fn aaa() {\n    bbb();\n}\nfn bbb() {\n    ccc();\n}\nfn ccc() -> i32 {\n    42\n}\n";
        let out = skeletonize_ranked(src, "rust", Some(10));
        assert!(out.contains("fn ccc()"), "{out}");
        assert!(!out.contains("fn aaa()"), "{out}");
        assert!(out.contains("elided by token budget"), "{out}");
    }

    #[test]
    fn token_budget_none_keeps_everything() {
        let src = "fn aaa() {\n    bbb();\n}\nfn bbb() {\n    ccc();\n}\nfn ccc() -> i32 {\n    42\n}\n";
        let out = skeletonize_ranked(src, "rust", None);
        assert!(out.contains("fn aaa()"));
        assert!(out.contains("fn bbb()"));
        assert!(out.contains("fn ccc()"));
        assert!(!out.contains("elided by token budget"));
    }

    #[test]
    fn ranked_generic_python_path() {
        let src = "import os\nclass Foo:\n    def bar(self):\n        return baz()\ndef baz():\n    return 1\n";
        let out = skeletonize_ranked(src, "python", None);
        assert!(out.contains("import os"), "{out}");
        let pb = out.find("def baz():").expect("baz missing");
        let pf = out.find("def bar(self):").expect("bar missing");
        assert!(pb < pf, "{out}");
    }

    #[test]
    fn ranked_prose_falls_back_to_excerpt() {
        let para = "The quarterly report shows revenue grew. ".repeat(80);
        let out = skeletonize_ranked(&para, "english", None);
        assert!(out.contains("quarterly report"), "{out}");
    }

    #[test]
    fn extract_decl_name_cases() {
        assert_eq!(
            extract_decl_name("pub fn add(a: i32) -> i32 {"),
            Some("add".to_string())
        );
        assert_eq!(
            extract_decl_name("class Foo:"),
            Some("Foo".to_string())
        );
        assert_eq!(
            extract_decl_name("    async handle(req, res) {"),
            Some("handle".to_string())
        );
        assert_eq!(
            extract_decl_name("const MAX: usize = 5;"),
            Some("MAX".to_string())
        );
        assert_eq!(
            extract_decl_name("impl Point {"),
            Some("Point".to_string())
        );
        // `fn` after a colon is a type, not the name.
        assert_eq!(
            extract_decl_name("const f: fn() = g;"),
            Some("f".to_string())
        );
    }

    // --- Diagnostic elision --------------------------------------------------------

    const DIAGNOSTIC_PY: &str = r#"import os

class Loader:
    def load(self, path):
        # TODO: support remote paths
        count = 0
        total = 0
        try:
            data = read(path)
        except OSError as e:
            log(e)
            raise
        if count > total and total != 0:
            count += 1
        name = "loader"
        return data
"#;

    #[test]
    fn diagnostic_keeps_error_paths() {
        let b = skeleton_body_diagnostic(DIAGNOSTIC_PY, 3);
        assert!(b.contains("try:"), "{b}");
        assert!(b.contains("except OSError as e:"), "{b}");
        assert!(b.contains("log(e)"), "{b}"); // except-block extension
        assert!(b.contains("raise"), "{b}");
    }

    #[test]
    fn diagnostic_keeps_todo_and_boundary_condition() {
        let b = skeleton_body_diagnostic(DIAGNOSTIC_PY, 3);
        assert!(b.contains("TODO"), "{b}");
        assert!(b.contains("if count > total and total != 0:"), "{b}");
        assert!(b.contains("count += 1"), "{b}"); // if-block extension
    }

    #[test]
    fn diagnostic_elides_boring_assignments() {
        let b = skeleton_body_diagnostic(DIAGNOSTIC_PY, 3);
        assert!(!b.contains("total = 0"), "{b}");
        assert!(!b.contains("name = \"loader\""), "{b}");
        assert!(b.contains("lines elided"), "{b}");
    }

    #[test]
    fn diagnostic_keeps_return_value() {
        let b = skeleton_body_diagnostic(DIAGNOSTIC_PY, 3);
        assert!(b.contains("return data"), "{b}");
    }

    #[test]
    fn diagnostic_keeps_returned_dict_literal() {
        // The SSRF case: a missing key hides inside a returned dict literal.
        let src = "class P:\n    def to_dict(self):\n        tmp = 1\n        return {\n            \"allowed\": self.allowed,\n            \"blocked\": self.blocked,\n        }\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains("\"allowed\": self.allowed"), "{b}");
        assert!(b.contains("\"blocked\": self.blocked"), "{b}");
        assert!(!b.contains("tmp = 1"), "{b}");
    }

    #[test]
    fn diagnostic_keeps_complex_conditional() {
        let src = "def f(a, b, c, d):\n    x = 1\n    if a and b or c and d:\n        return 1\n    return 0\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains("if a and b or c and d:"), "{b}");
        assert!(!b.contains("x = 1"), "{b}");
    }

    #[test]
    fn diagnostic_and_or_need_word_boundaries() {
        // "candle" contains "and" but is not a boolean connector.
        assert_eq!(count_bool_connectors("if candle and wax:"), 1);
        assert_eq!(count_bool_connectors("if a && b || c:"), 2);
    }

    #[test]
    fn diagnostic_keeps_assert_and_bare_except() {
        let src = "def f(x):\n    assert x > 0\n    try:\n        g()\n    except:\n        pass\n    return x\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains("assert x > 0"), "{b}");
        assert!(b.contains("except:"), "{b}");
    }

    #[test]
    fn diagnostic_rust_keeps_unwrap_and_try_operator() {
        let src = "use std::collections::HashMap;\nfn get(m: &HashMap<String, i32>, k: &str) -> i32 {\n    let v = m.get(k).unwrap();\n    let w = v + 1;\n    w\n}\nfn load(path: &str) -> Result<String, std::io::Error> {\n    let s = std::fs::read_to_string(path)?;\n    Ok(s)\n}\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains(".unwrap()"), "{b}");
        assert!(b.contains("?;"), "{b}");
        assert!(!b.contains("let w = v + 1;"), "{b}");
    }

    #[test]
    fn diagnostic_rust_keeps_tail_expression() {
        // Rust's tail expression is the return value.
        let src = "fn inc(x: i32) -> i32 {\n    let y = x + 1;\n    y\n}\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains("\n    y\n"), "{b}");
        assert!(!b.contains("let y = x + 1;"), "{b}");
    }

    #[test]
    fn diagnostic_rust_keeps_err_and_panic() {
        let src = "fn div(a: i32, b: i32) -> Result<i32, String> {\n    let q = a / b;\n    if b == 0 {\n        return Err(\"zero\".to_string());\n    }\n    if q < 0 {\n        panic!(\"negative\");\n    }\n    Ok(q)\n}\n";
        let b = skeleton_body_diagnostic(src, 3);
        assert!(b.contains("return Err("), "{b}");
        assert!(b.contains("panic!"), "{b}");
        assert!(b.contains("if b == 0 {"), "{b}");
        assert!(!b.contains("let q = a / b;"), "{b}");
    }

    #[test]
    fn diagnostic_bare_comparison_ignores_arrows() {
        assert!(!has_bare_comparison("let f = |x| x -> i32;"));
        assert!(!has_bare_comparison("Some(x) => foo(),"));
        assert!(has_bare_comparison("if x < limit {"));
        assert!(has_bare_comparison("while n > 0:"));
    }

    #[test]
    fn diagnostic_generics_are_not_comparisons() {
        assert!(!has_bare_comparison("let x: Vec<u8> = Vec::new();"));
        assert!(!has_bare_comparison("let r: Result<T, E> = foo();"));
        assert!(!has_bare_comparison(
            "let m: HashMap<String, Vec<u8>> = HashMap::new();"
        ));
        assert!(!has_bare_comparison("let y = foo::<T>(a);"));
        // Real comparisons still fire, even unspaced.
        assert!(has_bare_comparison("if x<y {"));
        assert!(has_bare_comparison("while n>0 {"));
        assert!(has_bare_comparison("if x < y {"));
    }

    #[test]
    fn diagnostic_non_function_decls_stay_signatures() {
        let b = skeleton_body_diagnostic(DIAGNOSTIC_PY, 3);
        assert!(b.contains("class Loader:"), "{b}");
        assert!(b.contains("import os"), "{b}");
        // No method bodies leak into the class rendering.
        assert_eq!(b.matches("def load").count(), 1);
    }

    // Boring-heavy sample: realistic code where most lines are assignments
    // and straightforward logic, with a few diagnostic hot spots.
    const DIAGNOSTIC_SIZE_PY: &str = r#"import os
import sys

CONFIG_PATH = "/etc/app.conf"
DEFAULT_TIMEOUT = 30

class Processor:
    def __init__(self, name):
        self.name = name
        self.count = 0
        self.items = []
        self.cache = {}
        self.enabled = True
        self.retries = 3

    def process(self, items, limit):
        results = []
        batch = []
        seen = set()
        mapping = {}
        for item in items:
            batch.append(item)
            seen.add(item.id)
            transformed = item.value * 2
            mapping[item.id] = transformed
        if len(results) > limit:
            raise ValueError("over limit")
        total = sum(batch)
        average = total / max(1, len(batch))
        summary = f"{average:.2f}"
        return results
"#;

    #[test]
    fn diagnostic_size_between_dumb_and_full() {
        let dumb = skeleton_body(DIAGNOSTIC_SIZE_PY, 3);
        let diagnostic = skeleton_body_diagnostic(DIAGNOSTIC_SIZE_PY, 3);
        assert!(
            dumb.len() < diagnostic.len(),
            "dumb={} diagnostic={}",
            dumb.len(),
            diagnostic.len()
        );
        assert!(
            diagnostic.len() < DIAGNOSTIC_SIZE_PY.len(),
            "diagnostic={} full={}",
            diagnostic.len(),
            DIAGNOSTIC_SIZE_PY.len()
        );
        let ratio = diagnostic.len() as f64 / DIAGNOSTIC_SIZE_PY.len() as f64;
        assert!(ratio < 0.8, "diagnostic/full ratio={ratio:.2}");
    }

    #[test]
    fn diagnostic_prose_falls_back_to_excerpt() {
        let txt = "Just plain prose, no code here. ".repeat(100);
        let b = skeleton_body_diagnostic(&txt, 3);
        assert!(b.contains("elided"), "{b}");
    }
}