aver-cert 0.1.2

Independent artifact certificate engine and verifier for Aver WebAssembly
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
//! Byte-binding lint: every producer-declared value must be constrained by the
//! module bytes somewhere inside the acceptance predicate.
//!
//! # Why this exists
//!
//! A certificate manifest carries values DECLARED by the producer, who is
//! untrusted. A declared value is only trustworthy if some conjunct of
//! `AverCert.AcceptedArtifact.accepted` constrains it against the artifact
//! bytes. Six times a declared value has been consumed by a proof while nothing
//! constrained it; the most recent was `hostRoleTable.toIndex`, where the
//! byte-derived binding (`CertDecode.AddSub.toIndexIdx`) existed and its own
//! comment claimed to prevent exactly that attack, but its only consumer had
//! left the acceptance path in an earlier refactor. The spec, the comment and
//! the function name all agreed, and all three were wrong.
//!
//! # What the lint checks, and why it is not "is the field projected"
//!
//! The obvious property — "every field is projected somewhere inside the
//! definitional cone of `accepted`" — does NOT catch that bug. At the pre-fix
//! commit the chain
//!
//! ```text
//! accepted -> StandardFace.checkedFaces -> symFragmentMatches
//!          -> hostTableBound -> decodedRoleIdx -> roles.toIndex
//! ```
//!
//! puts `roles.toIndex` squarely inside the cone. The bug was not "never
//! projected"; it was "projected, and even compared, but only against another
//! producer-declared value" — `hostTableBound` compares the claim's `hostTable`
//! against the subject's declared roles, and nothing in that chain touches
//! `modBytes`. So reachability is necessary (it is what notices a binding that
//! has LEFT the acceptance path, the literal failure mode here) but nowhere near
//! sufficient. The discriminating condition is ANCHORING: the field has to meet
//! something the producer does not control.
//!
//! A field slot has binding evidence when a definition reachable from `accepted`
//! contains a top-level conjunct satisfying one of:
//!
//! * **A — byte co-occurrence.** The conjunct projects the field and applies a
//!   byte anchor. Anchors are auto-derived, never hand-listed: a wall `def` is an
//!   anchor when its binders carry the module byte stream (`(n len : Nat)`,
//!   `(modBytes modLen : Nat)`) or `(artifact : ArtifactData)`. This is why
//!   `CertDecode.AddSub.toIndexIdx` is an anchor and `decodedRoleIdx` is not —
//!   exactly the distinction the bug turned on.
//! * **A1 — one-hop argument.** A producer value passed whole into a reachable
//!   definition from an anchored conjunct, where the callee projects the field.
//!   Depth one only; deeper propagation reinstates the historical false negative.
//! * **B — pinned to a wall term.** The conjunct equates the field to a term
//!   mentioning no producer-supplied value at all (a wall literal, a wall
//!   constant, a byte decode). The producer then has no freedom in that field.
//! * **C — whole-value equality against bytes.** One side of the conjunct is
//!   producer-free AND byte-derived, the other mentions a producer value whole;
//!   every leaf field of that value is then determined. This is how the lowered
//!   plan payloads are constrained (`lowerX plan = <real code bytes>`).
//!
//! Rules A1 and C are deliberately narrow. Blanket versions of both were tried
//! and both silently re-bound `roles.toIndex` in the pre-fix tree, i.e. they
//! reproduced the very blindness the lint exists to prevent. The regression test
//! `lint_still_flags_the_historical_index_helper_gap` is what holds that line.
//!
//! # What this lint cannot see
//!
//! It finds MISSING conjuncts, never WEAK ones. Two limits are worth stating
//! because both were measured, not guessed:
//!
//! * **Co-occurrence is not comparison.** Rule A asks whether a projection and
//!   a byte anchor appear in the same conjunct, not whether they are the two
//!   sides of one equality. Deleting the exact byte equality
//!   `actual == declared.map capabilityBytes` from `importsWithinCapabilities`
//!   does NOT flag `Subject.capabilities`, because a sibling conjunct in the
//!   same (byte-anchored) function still projects the field for the weaker
//!   registry-membership test. Deleting the only conjunct that mentions a field
//!   IS caught — that is the historical class, and mutation runs confirm it for
//!   `Roles.toIndex`, `Roles.box` and `Subject.start`.
//! * **Binding strength is invisible.** `arithRoleCheck` returns `true` for an
//!   unbound (`none`) role — vacuous by design — and the lint still scores
//!   `Roles.add` as bound. Guard-isolation tests (delete one conjunct, watch a
//!   hostile package get accepted) remain the only thing that measures whether a
//!   conjunct is load-bearing.
//!
//! It also sees only the wall: values pinned by the Rust checker or the
//! generated `CheckerWitness.lean` are outside `accepted`'s cone and get
//! flagged, which is what [`Category::PinnedOutsideTheWall`] is for — and that
//! category is itself an unaudited trust surface.
//!
//! # Exceptions
//!
//! There is deliberately no "ignore" or "skip" switch. The only way to make a
//! field green without a byte binding is an [`Allowance`] stating WHY, and every
//! allowance is printed on every run, pass or fail, so the declared-only surface
//! is visible rather than discoverable. Allowances are also checked for rot:
//! an allowance for a field that has since acquired a real binding fails, an
//! allowance naming a field that no longer exists fails, and a
//! [`Category::KnownGap`] allowance must resolve to a `Known gap` marker in the
//! format specification.

use std::collections::{BTreeMap, BTreeSet};

// ---------------------------------------------------------------------------
// Lexical helpers (hand-rolled: this crate ships a deliberately minimal
// dependency tree and the lint must not add to it)
// ---------------------------------------------------------------------------

fn is_ident_start(c: char) -> bool {
    c.is_ascii_alphabetic() || c == '_'
}

fn is_ident_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_' || c == '\'' || ('\u{2080}'..='\u{2089}').contains(&c)
}

/// Identifier characters plus the dot/bang/question marks Lean allows in a
/// qualified name, used when scanning for referenced definitions.
fn is_token_char(c: char) -> bool {
    is_ident_char(c) || c == '.' || c == '!' || c == '?'
}

/// Replace block and line comments with layout-preserving whitespace, so byte
/// offsets and line numbers stay meaningful.
fn strip_comments(text: &str) -> String {
    let chars: Vec<char> = text.chars().collect();
    let mut out = String::with_capacity(text.len());
    let mut i = 0usize;
    let mut depth = 0usize;
    while i < chars.len() {
        let two = |i: usize| -> Option<(char, char)> {
            if i + 1 < chars.len() {
                Some((chars[i], chars[i + 1]))
            } else {
                None
            }
        };
        if two(i) == Some(('/', '-')) {
            depth += 1;
            out.push(' ');
            out.push(' ');
            i += 2;
            continue;
        }
        if two(i) == Some(('-', '/')) && depth > 0 {
            depth -= 1;
            out.push(' ');
            out.push(' ');
            i += 2;
            continue;
        }
        if depth > 0 {
            out.push(if chars[i] == '\n' { '\n' } else { ' ' });
            i += 1;
            continue;
        }
        if two(i) == Some(('-', '-')) {
            while i < chars.len() && chars[i] != '\n' {
                out.push(' ');
                i += 1;
            }
            continue;
        }
        out.push(chars[i]);
        i += 1;
    }
    out
}

/// Blank out string literals so their contents are never mistaken for code.
fn blank_string_literals(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut in_str = false;
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if in_str {
            if c == '\\' {
                chars.next();
                continue;
            }
            if c == '"' {
                in_str = false;
                out.push('"');
            }
            continue;
        }
        if c == '"' {
            in_str = true;
            out.push('"');
            continue;
        }
        out.push(c);
    }
    out
}

/// All identifier-or-qualified-name tokens in `s`.
fn tokens(s: &str) -> Vec<String> {
    let chars: Vec<char> = s.chars().collect();
    let mut out = Vec::new();
    let mut i = 0usize;
    while i < chars.len() {
        if is_ident_start(chars[i]) && (i == 0 || !is_token_char(chars[i - 1])) {
            let start = i;
            while i < chars.len() && is_token_char(chars[i]) {
                i += 1;
            }
            out.push(chars[start..i].iter().collect::<String>());
        } else {
            i += 1;
        }
    }
    out
}

/// Plain identifiers (no dots), used for local-name scanning.
fn idents(s: &str) -> Vec<String> {
    let chars: Vec<char> = s.chars().collect();
    let mut out = Vec::new();
    let mut i = 0usize;
    while i < chars.len() {
        if is_ident_start(chars[i]) && (i == 0 || !is_token_char(chars[i - 1])) {
            let start = i;
            while i < chars.len() && is_ident_char(chars[i]) {
                i += 1;
            }
            if i < chars.len() && chars[i] == '.' {
                while i < chars.len() && is_token_char(chars[i]) {
                    i += 1;
                }
                continue;
            }
            out.push(chars[start..i].iter().collect::<String>());
        } else {
            i += 1;
        }
    }
    out
}

/// A projection chain `base.f1.f2...` found in source text.
#[derive(Debug, Clone)]
struct Chain {
    base: String,
    fields: Vec<String>,
    /// byte offset just past the chain, used to tell `x.f` from `x.f.g`
    end: usize,
    start: usize,
}

/// Scan every `base.f1.f2...` chain. A dot preceded by whitespace or an opening
/// bracket is Lean's anonymous-constructor dot, not a projection, and is skipped
/// because the scan requires an identifier immediately before the dot.
fn chains(s: &str) -> Vec<Chain> {
    let chars: Vec<char> = s.chars().collect();
    let mut out = Vec::new();
    let mut i = 0usize;
    while i < chars.len() {
        if is_ident_start(chars[i]) && (i == 0 || !is_token_char(chars[i - 1])) {
            let start = i;
            while i < chars.len() && is_ident_char(chars[i]) {
                i += 1;
            }
            let base: String = chars[start..i].iter().collect();
            let mut fields = Vec::new();
            while i + 1 < chars.len() && chars[i] == '.' && is_ident_start(chars[i + 1]) {
                i += 1;
                let fs = i;
                while i < chars.len() && is_ident_char(chars[i]) {
                    i += 1;
                }
                fields.push(chars[fs..i].iter().collect::<String>());
            }
            if !fields.is_empty() {
                out.push(Chain {
                    base,
                    fields,
                    end: i,
                    start,
                });
            }
        } else {
            i += 1;
        }
    }
    out
}

/// Split `s` on top-level (bracket depth zero) occurrences of any separator.
fn split_top(s: &str, seps: &[&str]) -> Vec<String> {
    let chars: Vec<char> = s.chars().collect();
    let mut parts = Vec::new();
    let mut cur = String::new();
    let mut depth = 0i32;
    let mut i = 0usize;
    'outer: while i < chars.len() {
        let c = chars[i];
        if c == '(' || c == '[' || c == '{' {
            depth += 1;
        } else if c == ')' || c == ']' || c == '}' {
            depth -= 1;
        }
        if depth <= 0 {
            for sep in seps {
                let sc: Vec<char> = sep.chars().collect();
                if i + sc.len() <= chars.len() && chars[i..i + sc.len()] == sc[..] {
                    parts.push(std::mem::take(&mut cur));
                    i += sc.len();
                    continue 'outer;
                }
            }
        }
        cur.push(c);
        i += 1;
    }
    parts.push(cur);
    parts
}

/// A balanced binder group `( names : type )` / `{ .. }` / `[ .. ]`.
#[derive(Debug, Clone)]
struct Binder {
    names: Vec<String>,
    ty: String,
}

fn binders(sig: &str) -> Vec<Binder> {
    let chars: Vec<char> = sig.chars().collect();
    let mut out = Vec::new();
    let mut i = 0usize;
    while i < chars.len() {
        let open = chars[i];
        let close = match open {
            '(' => ')',
            '{' => '}',
            '[' => ']',
            _ => {
                i += 1;
                continue;
            }
        };
        let mut depth = 0i32;
        let mut j = i;
        while j < chars.len() {
            if chars[j] == open {
                depth += 1;
            } else if chars[j] == close {
                depth -= 1;
                if depth == 0 {
                    break;
                }
            }
            j += 1;
        }
        if j >= chars.len() {
            i += 1;
            continue;
        }
        let inner: String = chars[i + 1..j].iter().collect();
        if let Some(b) = parse_binder(&inner) {
            out.push(b);
        }
        i += 1;
    }
    out
}

fn parse_binder(inner: &str) -> Option<Binder> {
    // first top-level `:` that is not `:=`
    let chars: Vec<char> = inner.chars().collect();
    let mut depth = 0i32;
    let mut cut = None;
    for i in 0..chars.len() {
        let c = chars[i];
        if c == '(' || c == '[' || c == '{' {
            depth += 1;
        } else if c == ')' || c == ']' || c == '}' {
            depth -= 1;
        } else if c == ':' && depth == 0 && chars.get(i + 1) != Some(&'=') {
            cut = Some(i);
            break;
        }
    }
    let cut = cut?;
    let lhs: String = chars[..cut].iter().collect();
    let rhs: String = chars[cut + 1..].iter().collect();
    let names: Vec<String> = lhs.split_whitespace().map(|s| s.to_string()).collect();
    if names.is_empty()
        || !names
            .iter()
            .all(|n| n.chars().next().is_some_and(is_ident_start))
    {
        return None;
    }
    Some(Binder {
        names,
        ty: rhs.trim().to_string(),
    })
}

// ---------------------------------------------------------------------------
// Wall model
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Def,
    Structure,
    Other,
}

#[derive(Debug, Clone)]
struct Decl {
    kind: Kind,
    short: String,
    full: String,
    ns: Vec<String>,
    opens: Vec<String>,
    sig: String,
    body: String,
    file: String,
    line: usize,
    /// structure fields, as (name, type expression)
    fields: Vec<(String, String)>,
}

const DECL_KEYWORDS: [&str; 7] = [
    "def",
    "abbrev",
    "theorem",
    "lemma",
    "instance",
    "structure",
    "inductive",
];

const MODIFIERS: [&str; 5] = ["private", "protected", "partial", "noncomputable", "unsafe"];

/// Recognise a declaration header, returning (indent, kind, name).
fn decl_header(line: &str) -> Option<(usize, Kind, String)> {
    let indent = line.len() - line.trim_start().len();
    let mut rest = line.trim_start();
    if rest.starts_with("@[") {
        let close = rest.find(']')?;
        rest = rest[close + 1..].trim_start();
    }
    loop {
        let mut advanced = false;
        for m in MODIFIERS {
            if let Some(r) = rest.strip_prefix(m)
                && r.starts_with(char::is_whitespace)
            {
                rest = r.trim_start();
                advanced = true;
            }
        }
        if !advanced {
            break;
        }
    }
    for kw in DECL_KEYWORDS {
        if let Some(r) = rest.strip_prefix(kw) {
            if !r.starts_with(char::is_whitespace) {
                continue;
            }
            let name: String = r
                .trim_start()
                .chars()
                .take_while(|c| is_token_char(*c))
                .collect();
            if name.is_empty() || !name.chars().next().is_some_and(is_ident_start) {
                continue;
            }
            let kind = match kw {
                "def" | "abbrev" => Kind::Def,
                "structure" => Kind::Structure,
                _ => Kind::Other,
            };
            return Some((indent, kind, name));
        }
    }
    None
}

fn parse_file(name: &str, text: &str) -> Vec<Decl> {
    let stripped = strip_comments(text);
    let lines: Vec<&str> = stripped.split('\n').collect();

    // namespace / open context per line
    let mut stack: Vec<Option<String>> = Vec::new();
    let mut opens: Vec<String> = Vec::new();
    let mut ns_at: Vec<Vec<String>> = Vec::with_capacity(lines.len());
    let mut op_at: Vec<Vec<String>> = Vec::with_capacity(lines.len());
    for line in &lines {
        let t = line.trim_start();
        if let Some(r) = t.strip_prefix("namespace ") {
            stack.push(Some(r.trim().to_string()));
        } else if t.starts_with("section") || t.starts_with("mutual") {
            stack.push(None);
        } else if t == "end" || t.starts_with("end ") {
            stack.pop();
        } else if let Some(r) = t.strip_prefix("open ")
            && !r.contains(" in ")
        {
            for tok in r.split_whitespace() {
                if tok.chars().next().is_some_and(is_ident_start) {
                    opens.push(tok.to_string());
                }
            }
        }
        ns_at.push(stack.iter().flatten().cloned().collect());
        op_at.push(opens.clone());
    }

    let mut starts: Vec<(usize, usize, Kind, String)> = Vec::new();
    for (i, line) in lines.iter().enumerate() {
        if let Some((indent, kind, nm)) = decl_header(line) {
            starts.push((i, indent, kind, nm));
        }
    }

    let mut out = Vec::new();
    for (k, (i, indent, kind, short)) in starts.iter().enumerate() {
        let end = starts.get(k + 1).map(|s| s.0).unwrap_or(lines.len());
        let body = lines[*i..end].join("\n");
        let sig = match body.find(":=") {
            Some(c) => body[..c].to_string(),
            None => body.clone(),
        };
        let short_tail = short.rsplit('.').next().unwrap_or(short).to_string();
        let mut full = ns_at[*i].clone();
        full.push(short.clone());
        let fields = if *kind == Kind::Structure {
            parse_fields(&lines[*i..end], *indent)
        } else {
            Vec::new()
        };
        out.push(Decl {
            kind: *kind,
            short: short_tail,
            full: full.join("."),
            ns: ns_at[*i].clone(),
            opens: op_at[*i].clone(),
            sig,
            body,
            file: name.to_string(),
            line: i + 1,
            fields,
        });
    }
    out
}

fn parse_fields(chunk: &[&str], base_indent: usize) -> Vec<(String, String)> {
    let mut out = Vec::new();
    for line in chunk.iter().skip(1) {
        if line.trim().is_empty() {
            continue;
        }
        let ind = line.len() - line.trim_start().len();
        if ind <= base_indent {
            break;
        }
        let t = line.trim();
        if t.starts_with("deriving") {
            break;
        }
        // `names : type`, rejecting `:=` (default values are still fields, but
        // the header form `name : Ty := default` splits on the first `:`)
        let chars: Vec<char> = line.chars().collect();
        let mut depth = 0i32;
        let mut cut = None;
        for i in 0..chars.len() {
            let c = chars[i];
            if c == '(' || c == '[' || c == '{' {
                depth += 1;
            } else if c == ')' || c == ']' || c == '}' {
                depth -= 1;
            } else if c == ':' && depth == 0 && chars.get(i + 1) != Some(&'=') {
                cut = Some(i);
                break;
            }
        }
        let Some(cut) = cut else { continue };
        let lhs: String = chars[..cut].iter().collect();
        let ty: String = chars[cut + 1..].iter().collect();
        let names: Vec<String> = lhs.split_whitespace().map(|s| s.to_string()).collect();
        if names.is_empty()
            || !names.iter().all(|n| {
                n.chars().all(is_ident_char) && n.chars().next().is_some_and(is_ident_start)
            })
        {
            continue;
        }
        for n in names {
            out.push((n, ty.trim().to_string()));
        }
    }
    out
}

struct Wall {
    decls: Vec<Decl>,
    by_full: BTreeMap<String, usize>,
    structs: BTreeMap<String, usize>,
    struct_by_short: BTreeMap<String, Vec<String>>,
}

const BUILTIN_TYPES: [&str; 12] = [
    "Option", "List", "Prod", "Nat", "String", "Int", "Bool", "Type", "Array", "Char", "UInt64",
    "Prop",
];

impl Wall {
    fn new(decls: Vec<Decl>) -> Self {
        let mut by_full = BTreeMap::new();
        let mut structs = BTreeMap::new();
        let mut struct_by_short: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for (i, d) in decls.iter().enumerate() {
            by_full.entry(d.full.clone()).or_insert(i);
            if d.kind == Kind::Structure {
                structs.entry(d.full.clone()).or_insert(i);
                struct_by_short
                    .entry(d.short.clone())
                    .or_default()
                    .push(d.full.clone());
            }
        }
        Wall {
            decls,
            by_full,
            structs,
            struct_by_short,
        }
    }

    fn get(&self, full: &str) -> Option<&Decl> {
        self.by_full.get(full).map(|i| &self.decls[*i])
    }

    fn structure(&self, full: &str) -> Option<&Decl> {
        self.structs.get(full).map(|i| &self.decls[*i])
    }

    /// Namespace-aware resolution of a referenced name. Short-name keying alone
    /// is not enough: it once pulled the DEAD `CertDecode.AddSub.roleTable` into
    /// the reachable set on the live `StringHost.roleTable`'s name.
    fn resolve(&self, tok: &str, ns: &[String], opens: &[String]) -> Option<String> {
        for k in (0..=ns.len()).rev() {
            let mut cand = ns[..k].to_vec();
            cand.push(tok.to_string());
            let c = cand.join(".");
            if self.by_full.contains_key(&c) {
                return Some(c);
            }
        }
        for o in opens {
            let c = format!("{o}.{tok}");
            if self.by_full.contains_key(&c) {
                return Some(c);
            }
        }
        if self.by_full.contains_key(tok) {
            return Some(tok.to_string());
        }
        None
    }

    /// Type expression -> producer structure, peeling `Option`/`List`/`Prod`.
    fn resolve_struct(&self, ty: &str, ns: &[String], opens: &[String]) -> Option<String> {
        for t in tokens(ty) {
            if BUILTIN_TYPES.contains(&t.as_str()) {
                continue;
            }
            if let Some(r) = self.resolve(&t, ns, opens)
                && self.structs.contains_key(&r)
            {
                return Some(r);
            }
            if self.structs.contains_key(&t) {
                return Some(t);
            }
            let short = t.rsplit('.').next().unwrap_or(&t);
            if let Some(v) = self.struct_by_short.get(short)
                && v.len() == 1
            {
                return Some(v[0].clone());
            }
        }
        None
    }
}

// ---------------------------------------------------------------------------
// Producer surface and reachability
// ---------------------------------------------------------------------------

/// A producer-declared value slot: one leaf field of a producer structure.
pub type FieldRef = (String, String);

fn is_arrow(ty: &str) -> bool {
    ty.contains('→') || ty.contains("->")
}

/// Structures reachable from `ArtifactData` through plain-data field types.
/// Arrow types are not followed: `Obligation.domRepr : CarrierSpec c -> ...`
/// must not drag `CarrierSpec` in, because a `CarrierSpec` is universally
/// quantified inside `Obligation.holds` and is wall-side, not producer-supplied.
fn producer_closure(w: &Wall) -> BTreeSet<String> {
    let mut start = None;
    for d in &w.decls {
        if d.kind == Kind::Structure && d.short == "ArtifactData" {
            start = Some(d.full.clone());
        }
    }
    let mut seen = BTreeSet::new();
    let mut stack: Vec<String> = start.into_iter().collect();
    while let Some(s) = stack.pop() {
        if !seen.insert(s.clone()) {
            continue;
        }
        let Some(d) = w.structure(&s) else { continue };
        for (_f, ty) in &d.fields {
            if is_arrow(ty) {
                continue;
            }
            if let Some(t) = w.resolve_struct(ty, &d.ns, &d.opens)
                && !seen.contains(&t)
            {
                stack.push(t);
            }
        }
    }
    seen
}

fn reachable_from(w: &Wall, root: &str) -> BTreeSet<String> {
    let mut seen = BTreeSet::new();
    let mut stack = vec![root.to_string()];
    while let Some(s) = stack.pop() {
        if !seen.insert(s.clone()) {
            continue;
        }
        let Some(d) = w.get(&s) else { continue };
        for tok in tokens(&d.body) {
            if let Some(r) = w.resolve(&tok, &d.ns, &d.opens)
                && !seen.contains(&r)
            {
                stack.push(r);
            }
            if tok.contains('.') {
                let parts: Vec<&str> = tok.split('.').collect();
                for j in (1..parts.len()).rev() {
                    let cand = parts[..j].join(".");
                    if let Some(r) = w.resolve(&cand, &d.ns, &d.opens)
                        && !seen.contains(&r)
                    {
                        stack.push(r);
                    }
                }
            }
        }
    }
    seen
}

/// A definition is a byte anchor when its binders carry the module byte stream
/// or the whole artifact. Auto-derived on purpose: a hand-maintained anchor list
/// is exactly the sort of table that rots into a lie.
fn is_byte_anchor(d: &Decl) -> bool {
    for b in binders(&d.sig) {
        let ty = b.ty.trim();
        if ty == "Nat" {
            for pair in [("n", "len"), ("modBytes", "modLen")] {
                for i in 0..b.names.len().saturating_sub(1) {
                    if b.names[i] == pair.0 && b.names[i + 1] == pair.1 {
                        return true;
                    }
                }
            }
        }
        if ty == "ArtifactData" && b.names.len() == 1 && b.names[0] == "artifact" {
            return true;
        }
    }
    false
}

fn binds_raw_bytes(d: &Decl) -> bool {
    for b in binders(&d.sig) {
        if b.ty.trim() == "Nat" {
            for pair in [("n", "len"), ("modBytes", "modLen")] {
                for i in 0..b.names.len().saturating_sub(1) {
                    if b.names[i] == pair.0 && b.names[i + 1] == pair.1 {
                        return true;
                    }
                }
            }
        }
    }
    false
}

// ---------------------------------------------------------------------------
// Local typing
// ---------------------------------------------------------------------------

type Env = BTreeMap<String, String>;

fn let_bindings(body: &str) -> Vec<(String, String)> {
    let mut out = Vec::new();
    for line in body.split('\n') {
        let mut rest = line;
        while let Some(p) = rest.find("let ") {
            let after = &rest[p + 4..];
            let name: String = after.chars().take_while(|c| is_ident_char(*c)).collect();
            let tail = &after[name.len()..];
            if !name.is_empty()
                && let Some(t) = tail.trim_start().strip_prefix(":=")
            {
                out.push((name, t.trim().to_string()));
            }
            rest = &rest[p + 4..];
        }
    }
    out
}

/// `match E1, E2 with` scrutinee texts.
fn match_scrutinees(body: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut rest = body;
    while let Some(p) = rest.find("match ") {
        let after = &rest[p + 6..];
        if let Some(w) = after.find(" with") {
            out.push(after[..w].to_string());
        }
        rest = &rest[p + 6..];
    }
    out
}

/// Identifiers bound by a `∃ a b c,` prefix.
fn exists_names(body: &str) -> BTreeSet<String> {
    let mut out = BTreeSet::new();
    let mut rest = body;
    while let Some(p) = rest.find('∃') {
        let after = &rest[p + '∃'.len_utf8()..];
        if let Some(c) = after.find(',') {
            for id in idents(&after[..c]) {
                if id != "_" {
                    out.insert(id);
                }
            }
        }
        rest = after;
    }
    out
}

fn pattern_binds(pat: &str) -> Vec<String> {
    let p = pat.trim();
    for head in ["some ", ".some "] {
        if let Some(r) = p.strip_prefix(head) {
            return idents(r).into_iter().filter(|x| x != "_").collect();
        }
    }
    if let Some(i) = p.find("::") {
        return idents(&p[..i]).into_iter().filter(|x| x != "_").collect();
    }
    Vec::new()
}

fn local_env(w: &Wall, d: &Decl) -> Env {
    let mut env: Env = BTreeMap::new();
    for b in binders(&d.sig) {
        if let Some(t) = w.resolve_struct(&b.ty, &d.ns, &d.opens) {
            for n in b.names {
                env.entry(n).or_insert_with(|| t.clone());
            }
        }
    }
    for (name, rhs) in let_bindings(&d.body) {
        if let Some(t) = expr_struct(w, &env, &rhs) {
            env.entry(name).or_insert(t);
        }
    }
    // match-scrutinee propagation, to a fixpoint bounded at three rounds
    for _ in 0..3 {
        let mut grew = false;
        for scrut in match_scrutinees(&d.body) {
            let parts = split_top(&scrut, &[","]);
            let types: Vec<Option<String>> = parts
                .iter()
                .map(|p| expr_struct(w, &env, p.trim()))
                .collect();
            let Some(pos) = d.body.find(&scrut) else {
                continue;
            };
            let tail = &d.body[pos..];
            let window = &tail[..tail.len().min(6000)];
            for arm in window.split('|').skip(1) {
                let Some(ar) = arm.find("=>") else { continue };
                let pats = split_top(&arm[..ar], &[","]);
                if pats.len() != types.len() {
                    continue;
                }
                for (pat, ty) in pats.iter().zip(types.iter()) {
                    let Some(ty) = ty else { continue };
                    for nm in pattern_binds(pat) {
                        if let std::collections::btree_map::Entry::Vacant(e) = env.entry(nm) {
                            e.insert(ty.clone());
                            grew = true;
                        }
                    }
                }
            }
        }
        if !grew {
            break;
        }
    }
    env
}

/// Type of an expression, when it is exactly an identifier or projection chain.
fn expr_struct(w: &Wall, env: &Env, expr: &str) -> Option<String> {
    let e = expr.trim();
    if e.chars().all(is_ident_char) && !e.is_empty() {
        return env.get(e).cloned();
    }
    let cs = chains(e);
    if cs.len() != 1 || cs[0].start != 0 || cs[0].end != e.chars().count() {
        return None;
    }
    let c = &cs[0];
    let mut cur = env.get(&c.base)?.clone();
    for f in &c.fields {
        let sd = w.structure(&cur)?;
        let (_n, ty) = sd.fields.iter().find(|(n, _)| n == f)?;
        if is_arrow(ty) {
            return None;
        }
        cur = w.resolve_struct(ty, &sd.ns, &sd.opens)?;
    }
    Some(cur)
}

/// Resolve the longest prefix of `base.f1.f2...` that ends at a real field.
/// The prefix walk matters: `manifest.subject.declaredUncertified.map` must
/// attribute to `Subject.declaredUncertified`, not fail on the trailing `.map`.
fn chain_field(w: &Wall, env: &Env, c: &Chain) -> Option<FieldRef> {
    for k in (1..=c.fields.len()).rev() {
        if let Some(r) = chain_field_exact(w, env, &c.base, &c.fields[..k]) {
            return Some(r);
        }
    }
    None
}

fn chain_field_exact(w: &Wall, env: &Env, base: &str, fields: &[String]) -> Option<FieldRef> {
    let mut cur = env.get(base)?.clone();
    for (i, f) in fields.iter().enumerate() {
        let sd = w.structure(&cur)?;
        let (_n, ty) = sd.fields.iter().find(|(n, _)| n == f)?;
        if i + 1 == fields.len() {
            return Some((cur, f.clone()));
        }
        if is_arrow(ty) {
            return None;
        }
        cur = w.resolve_struct(ty, &sd.ns, &sd.opens)?;
    }
    None
}

// ---------------------------------------------------------------------------
// Conjunct analysis
// ---------------------------------------------------------------------------

fn conjuncts(body: &str) -> Vec<String> {
    let tail = match body.find(":=") {
        Some(c) => &body[c + 2..],
        None => body,
    };
    let mut out = Vec::new();
    for arm in split_top(tail, &["\n  | ", "\n      | "]) {
        out.extend(split_top(&arm, &["&&", "∧"]));
    }
    out
}

fn expand_lets(c: &str, lets: &BTreeMap<String, String>) -> String {
    let mut cur = c.to_string();
    for _ in 0..3 {
        let mut out = String::with_capacity(cur.len());
        let chars: Vec<char> = cur.chars().collect();
        let mut i = 0usize;
        let mut changed = false;
        while i < chars.len() {
            if is_ident_start(chars[i]) && (i == 0 || !is_token_char(chars[i - 1])) {
                let start = i;
                while i < chars.len() && is_ident_char(chars[i]) {
                    i += 1;
                }
                let word: String = chars[start..i].iter().collect();
                match lets.get(&word) {
                    Some(rhs) => {
                        out.push('(');
                        out.push_str(rhs);
                        out.push(')');
                        changed = true;
                    }
                    None => out.push_str(&word),
                }
            } else {
                out.push(chars[i]);
                i += 1;
            }
        }
        cur = out;
        if !changed {
            break;
        }
    }
    cur
}

/// Split a conjunct at its top-level equality, if it has one.
fn eq_sides(c: &str) -> Option<(String, String)> {
    let chars: Vec<char> = c.chars().collect();
    let mut depth = 0i32;
    let mut i = 0usize;
    while i < chars.len() {
        let ch = chars[i];
        if ch == '(' || ch == '[' || ch == '{' {
            depth += 1;
        } else if ch == ')' || ch == ']' || ch == '}' {
            depth -= 1;
        }
        if depth <= 0 {
            if chars.get(i) == Some(&'=') && chars.get(i + 1) == Some(&'=') {
                return Some((chars[..i].iter().collect(), chars[i + 2..].iter().collect()));
            }
            if (chars.get(i) == Some(&':') || chars.get(i) == Some(&'!'))
                && chars.get(i + 1) == Some(&'=')
            {
                i += 2;
                continue;
            }
            if ch == '=' && chars.get(i + 1) != Some(&'>') {
                return Some((chars[..i].iter().collect(), chars[i + 1..].iter().collect()));
            }
        }
        i += 1;
    }
    None
}

fn strip_wrappers(side: &str) -> String {
    let mut s = side.trim();
    for w in ["some ", ".some ", "Option.some "] {
        if let Some(r) = s.strip_prefix(w) {
            s = r.trim();
        }
    }
    s.to_string()
}

/// A side that mentions no producer-supplied value at all.
fn producer_free(w: &Wall, env: &Env, side: &str) -> bool {
    let s = blank_string_literals(side);
    if s.trim().is_empty() {
        return false;
    }
    for c in chains(&s) {
        if env.contains_key(&c.base) {
            return false;
        }
        if chain_field(w, env, &c).is_some() {
            return false;
        }
    }
    for t in idents(&s) {
        if env.contains_key(&t) {
            return false;
        }
    }
    true
}

/// Every producer value appearing WHOLE (not projected) on a side.
fn whole_producer_values(w: &Wall, env: &Env, side: &str) -> Vec<String> {
    let mut out = Vec::new();
    let cs = chains(side);
    for id in idents(side) {
        if let Some(t) = env.get(&id) {
            out.push(t.clone());
        }
    }
    for c in cs {
        let text: String = side.chars().skip(c.start).take(c.end - c.start).collect();
        if side.chars().nth(c.end) == Some('.') {
            continue;
        }
        if let Some(t) = expr_struct(w, env, &text) {
            out.push(t);
        }
    }
    out
}

/// Leaf slots of a structure, transitively through nested producer structures.
fn leaves_of(
    w: &Wall,
    root: &str,
    slots: &BTreeSet<FieldRef>,
    seen: &mut BTreeSet<String>,
    out: &mut Vec<FieldRef>,
) {
    if !seen.insert(root.to_string()) {
        return;
    }
    let Some(sd) = w.structure(root) else { return };
    for (f, ty) in &sd.fields {
        let key = (root.to_string(), f.clone());
        if slots.contains(&key) {
            out.push(key);
        }
        if !is_arrow(ty)
            && let Some(sub) = w.resolve_struct(ty, &sd.ns, &sd.opens)
        {
            leaves_of(w, &sub, slots, seen, out);
        }
    }
}

/// Parameter names of a definition, in order.
fn sig_params(d: &Decl) -> Vec<String> {
    let head = match d.sig.find(":=") {
        Some(c) => &d.sig[..c],
        None => &d.sig[..],
    };
    binders(head).into_iter().flat_map(|b| b.names).collect()
}

/// Applications `f a1 a2 ...` in a conjunct, with textual arguments.
fn call_sites(c: &str) -> Vec<(String, Vec<String>)> {
    let mut out = Vec::new();
    let chars: Vec<char> = c.chars().collect();
    let mut i = 0usize;
    while i < chars.len() {
        if is_ident_start(chars[i]) && (i == 0 || !is_token_char(chars[i - 1])) {
            let start = i;
            while i < chars.len() && is_token_char(chars[i]) {
                i += 1;
            }
            let name: String = chars[start..i].iter().collect();
            let mut args = Vec::new();
            loop {
                let mut j = i;
                while j < chars.len() && (chars[j] == ' ' || chars[j] == '\n') {
                    j += 1;
                }
                if j >= chars.len() || j == i {
                    break;
                }
                if chars[j] == '(' {
                    let mut depth = 0i32;
                    let mut k = j;
                    while k < chars.len() {
                        if chars[k] == '(' {
                            depth += 1;
                        } else if chars[k] == ')' {
                            depth -= 1;
                            if depth == 0 {
                                break;
                            }
                        }
                        k += 1;
                    }
                    if k >= chars.len() {
                        break;
                    }
                    args.push(chars[j + 1..k].iter().collect::<String>());
                    i = k + 1;
                } else if chars[j] == ')' || chars[j] == ']' || chars[j] == '}' || chars[j] == ',' {
                    break;
                } else {
                    let mut k = j;
                    while k < chars.len()
                        && !chars[k].is_whitespace()
                        && chars[k] != '('
                        && chars[k] != ')'
                    {
                        k += 1;
                    }
                    args.push(chars[j..k].iter().collect::<String>());
                    i = k;
                }
            }
            if !args.is_empty() {
                out.push((name, args));
            }
        } else {
            i += 1;
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Report
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct Evidence {
    pub rule: &'static str,
    pub decl: String,
    pub file: String,
    pub line: usize,
    pub conjunct: String,
}

#[derive(Debug)]
pub struct Report {
    /// every producer-declared leaf value slot
    pub slots: BTreeSet<FieldRef>,
    /// slots with binding evidence, and where it was found
    pub bound: BTreeMap<FieldRef, Evidence>,
    /// slots with no binding evidence at all
    pub flagged: Vec<FieldRef>,
    pub reachable: usize,
    pub anchors: usize,
}

impl Report {
    pub fn is_bound(&self, structure: &str, field: &str) -> bool {
        self.bound
            .keys()
            .any(|(s, f)| s.ends_with(structure) && f == field)
    }

    pub fn is_flagged(&self, structure: &str, field: &str) -> bool {
        self.flagged
            .iter()
            .any(|(s, f)| s.ends_with(structure) && f == field)
    }
}

/// Run the byte-binding analysis over a set of Lean wall sources.
pub fn analyse(sources: &[(&str, &str)]) -> Report {
    let mut decls = Vec::new();
    let mut names: Vec<&(&str, &str)> = sources.iter().collect();
    names.sort_by_key(|(n, _)| *n);
    for (name, text) in names {
        decls.extend(parse_file(name, text));
    }
    let w = Wall::new(decls);

    let root = w
        .decls
        .iter()
        .find(|d| d.short == "accepted" && d.full.ends_with("AcceptedArtifact.accepted"))
        .map(|d| d.full.clone())
        .unwrap_or_default();
    let reach = reachable_from(&w, &root);

    let mut anchors: BTreeSet<String> = BTreeSet::new();
    let mut anchor_short: BTreeSet<String> = BTreeSet::new();
    for d in &w.decls {
        if d.kind == Kind::Def && is_byte_anchor(d) {
            anchors.insert(d.full.clone());
            anchor_short.insert(d.short.clone());
        }
    }

    let prod = producer_closure(&w);
    let mut slots: BTreeSet<FieldRef> = BTreeSet::new();
    for s in &prod {
        let Some(sd) = w.structure(s) else { continue };
        for (f, ty) in &sd.fields {
            let sub = if is_arrow(ty) {
                None
            } else {
                w.resolve_struct(ty, &sd.ns, &sd.opens)
            };
            // A field whose type is itself a producer structure is scaffolding,
            // not a value: its leaves carry the producer's freedom.
            match sub {
                Some(t) if prod.contains(&t) => {}
                _ => {
                    slots.insert((s.clone(), f.clone()));
                }
            }
        }
    }

    // unique-name fallback for chains whose base type cannot be resolved
    let mut field_owner: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for (s, f) in &slots {
        field_owner.entry(f.clone()).or_default().insert(s.clone());
    }
    let unique_owner = |f: &str| -> Option<FieldRef> {
        field_owner.get(f).and_then(|o| {
            if o.len() == 1 {
                Some((o.iter().next().unwrap().clone(), f.to_string()))
            } else {
                None
            }
        })
    };

    // per-declaration caches for the one-hop rule
    let mut env_cache: BTreeMap<String, Env> = BTreeMap::new();
    let mut bound: BTreeMap<FieldRef, Evidence> = BTreeMap::new();

    let mark = |bound: &mut BTreeMap<FieldRef, Evidence>,
                key: FieldRef,
                rule: &'static str,
                d: &Decl,
                c: &str| {
        bound.entry(key).or_insert_with(|| Evidence {
            rule,
            decl: d.full.clone(),
            file: d.file.clone(),
            line: d.line,
            conjunct: c.split_whitespace().collect::<Vec<_>>().join(" "),
        });
    };

    for d in &w.decls {
        if d.kind != Kind::Def || !reach.contains(&d.full) {
            continue;
        }
        let env = local_env(&w, d);
        let raw_bytes = binds_raw_bytes(d);
        let lets: BTreeMap<String, String> = let_bindings(&d.body).into_iter().collect();

        // A byte-derived `match` scrutinee guards every arm of the definition.
        let mut scrut_toks: BTreeSet<String> = BTreeSet::new();
        for s in match_scrutinees(&d.body) {
            for t in tokens(&expand_lets(&s, &lets)) {
                scrut_toks.insert(t.rsplit('.').next().unwrap_or(&t).to_string());
                scrut_toks.insert(t);
            }
        }

        let cjs = conjuncts(&d.body);

        // ONE-STEP existential chaining. A `∃`-bound name that itself appears in
        // a conjunct naming the raw byte stream is byte-determined, which is how
        // `lowerX plan = some codeEntry` reaches the module bytes through the
        // neighbouring `exactFuncBindingForExport modBytes modLen ... codeEntry`.
        // Strictly one step, no fixpoint: a transitive version was tried and it
        // re-bound `roles.toIndex` in the pre-fix tree, destroying the lint.
        let ex = exists_names(&d.body);
        let mut byte_locals: BTreeSet<String> = BTreeSet::new();
        for c in &cjs {
            let tk: BTreeSet<String> = tokens(c).into_iter().collect();
            let has_bytes = tk.contains("modBytes")
                || tk.contains("modLen")
                || (raw_bytes && tk.contains("n") && tk.contains("len"));
            if has_bytes {
                for id in idents(c) {
                    if ex.contains(&id) {
                        byte_locals.insert(id);
                    }
                }
            }
        }

        for c in &cjs {
            let cx = expand_lets(c, &lets);
            let mut toks: BTreeSet<String> = scrut_toks.clone();
            for t in tokens(&cx) {
                if let Some(r) = w.resolve(&t, &d.ns, &d.opens) {
                    toks.insert(r);
                }
                toks.insert(t.rsplit('.').next().unwrap_or(&t).to_string());
                toks.insert(t);
            }
            let anchored = toks.contains("modBytes")
                || toks.contains("modLen")
                || (raw_bytes && toks.contains("n") && toks.contains("len"))
                || toks.iter().any(|t| anchors.contains(t))
                || toks.iter().any(|t| anchor_short.contains(t));

            if anchored {
                // Rule A: projection and byte anchor in the same conjunct.
                for ch in chains(c) {
                    let r = chain_field(&w, &env, &ch)
                        .or_else(|| unique_owner(ch.fields.last().unwrap()));
                    if let Some(r) = r
                        && slots.contains(&r)
                    {
                        mark(&mut bound, r, "A/byte-cooccurrence", d, c);
                    }
                }
                // Rule A1: one hop into a callee that projects the field.
                for (name, args) in call_sites(&cx) {
                    let Some(full) = w.resolve(&name, &d.ns, &d.opens) else {
                        continue;
                    };
                    let Some(g) = w.get(&full) else { continue };
                    if g.kind != Kind::Def {
                        continue;
                    }
                    let genv = env_cache
                        .entry(full.clone())
                        .or_insert_with(|| local_env(&w, g))
                        .clone();
                    let params = sig_params(g);
                    for (i, a) in args.iter().enumerate() {
                        let Some(pname) = params.get(i) else { break };
                        let Some(st) = expr_struct(&w, &env, a) else {
                            continue;
                        };
                        let mut genv2 = genv.clone();
                        genv2.insert(pname.clone(), st);
                        for gc in chains(&g.body) {
                            if &gc.base != pname {
                                continue;
                            }
                            if let Some(r) = chain_field(&w, &genv2, &gc)
                                && slots.contains(&r)
                            {
                                mark(&mut bound, r, "A1/one-hop-argument", d, c);
                            }
                        }
                    }
                }
            }

            // Rules B and C: equality with a producer-free counterpart.
            if let Some((l, r)) = eq_sides(c) {
                for (a, b) in [(l.clone(), r.clone()), (r, l)] {
                    if !producer_free(&w, &env, &b) {
                        continue;
                    }
                    let stripped = strip_wrappers(&a);
                    let cs = chains(&stripped);
                    if cs.len() == 1 && cs[0].start == 0 && cs[0].end == stripped.chars().count() {
                        let hit = chain_field(&w, &env, &cs[0])
                            .or_else(|| unique_owner(cs[0].fields.last().unwrap()));
                        if let Some(hit) = hit
                            && slots.contains(&hit)
                        {
                            mark(&mut bound, hit, "B/pinned-to-wall-term", d, c);
                        }
                    }
                    // C needs the counterpart to be byte-derived, not merely
                    // producer-free: `arithTableCheck ... = true` must NOT bind
                    // the roles it is handed, or the historical bug walks free.
                    let btoks: BTreeSet<String> = tokens(&b)
                        .into_iter()
                        .flat_map(|t| [t.rsplit('.').next().unwrap_or(&t).to_string(), t.clone()])
                        .collect();
                    let b_bytes = btoks.contains("modBytes")
                        || btoks.contains("modLen")
                        || (raw_bytes && btoks.contains("n") && btoks.contains("len"))
                        || btoks.iter().any(|t| anchors.contains(t))
                        || btoks.iter().any(|t| anchor_short.contains(t))
                        || idents(&b).iter().any(|t| byte_locals.contains(t));
                    if !b_bytes {
                        continue;
                    }
                    for st in whole_producer_values(&w, &env, &a) {
                        let mut seen = BTreeSet::new();
                        let mut leaves = Vec::new();
                        leaves_of(&w, &st, &slots, &mut seen, &mut leaves);
                        for leaf in leaves {
                            mark(&mut bound, leaf, "C/whole-value-vs-bytes", d, c);
                        }
                    }
                }
            }
        }
    }

    let flagged: Vec<FieldRef> = slots
        .iter()
        .filter(|k| !bound.contains_key(*k))
        .cloned()
        .collect();

    Report {
        slots,
        bound,
        flagged,
        reachable: reach.len(),
        anchors: anchors.len(),
    }
}

// ---------------------------------------------------------------------------
// Exceptions
// ---------------------------------------------------------------------------

/// Why a producer-declared value is allowed to carry no byte binding. The set is
/// closed on purpose: a free-text category would become a synonym for "ignore".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Category {
    /// Transported for display, deliberately outside the verified claim. No
    /// current member: the documented example (`certified[].dom` / `cod`) lives
    /// in the JSON manifest, not in a Lean producer structure, so it is not a
    /// slot this lint ranges over. The category stays because the policy has
    /// four kinds and a reviewer needs the vocabulary to classify the next one.
    #[allow(dead_code)]
    DeclaredOnlyByDesign,
    /// The value SHOULD be constrained and is not. Must be documented as such.
    KnownGap,
    /// Enforced by the Rust checker or the generated witness, not by `accepted`.
    PinnedOutsideTheWall,
    /// Pinned to a value the wall itself computes, so the producer cannot
    /// influence it, rather than to the artifact bytes.
    WallOwnedConstraint,
}

impl Category {
    fn label(self) -> &'static str {
        match self {
            Category::DeclaredOnlyByDesign => "declared-only by design",
            Category::KnownGap => "KNOWN GAP",
            Category::PinnedOutsideTheWall => "pinned outside the wall",
            Category::WallOwnedConstraint => "wall-owned constraint",
        }
    }
}

/// One reviewable exception. There is no wildcard and no per-structure form:
/// granularity is one field, one reason.
#[derive(Debug, Clone, Copy)]
pub struct Allowance {
    /// Structure short name, e.g. `"Subject"`.
    pub structure: &'static str,
    /// Field name, e.g. `"profile"`.
    pub field: &'static str,
    pub category: Category,
    /// Why this value is not bound to the bytes. Must not be empty.
    pub reason: &'static str,
    /// Exact text that must appear in `docs/certificate-format.md`, so the
    /// specification and the lint cannot drift apart silently. This is the rule
    /// aimed at the actual failure mode: the format spec went on describing a
    /// pin that had been deleted, and nothing went red.
    pub doc_anchor: &'static str,
}

/// The exceptions in force for the embedded wall.
///
/// Every entry is printed on every run. Read them as a list of things the
/// certificate does NOT prove about values it transports.
pub const ALLOWED: &[Allowance] = &[
    Allowance {
        structure: "Subject",
        field: "profile",
        category: Category::KnownGap,
        reason: "The witness pins the JSON string to `subject.profile` by `rfl`, but no \
                 acceptance conjunct compares it against any fixed identifier: any string \
                 passing the candidate gate is accepted. The `PROFILE_ID` constant lives \
                 only on the producer side (engine/mod.rs).",
        doc_anchor: "the identifier constant exists only on the producer side (`PROFILE_ID` in `engine/mod.rs`)",
    },
    Allowance {
        structure: "Subject",
        field: "abi",
        category: Category::KnownGap,
        reason: "Same status as `profile`: pinned to `subject.abi` by `rfl`, not validated \
                 against any fixed identifier. The `RUNTIME_ABI` constant lives only on the \
                 producer side (engine/mod.rs).",
        doc_anchor: "the constant exists only on the producer side (`RUNTIME_ABI` in `engine/mod.rs`)",
    },
    Allowance {
        structure: "Subject",
        field: "exports",
        category: Category::PinnedOutsideTheWall,
        reason: "The wall never reads this field — its only occurrence in the whole wall is \
                 its own declaration in SchemaCore.lean, and `exportsAccounted` does the \
                 byte-level export accounting from `manifest.obligations` and \
                 `subject.declaredUncertified` instead. It is constrained by the \
                 checker-authored witness rather than by `accepted`: verifier.rs emits \
                 `subject.exports = {names} := rfl` against the SAME `{names}` list it pins \
                 `obligations.map (fun o => o.export_)` to, so the two cannot disagree \
                 without the witness failing to elaborate, and the obligation export names \
                 are byte-bound through `exportsAccounted`. Transported duplicate, not an \
                 independent value. Note this is the one category the lint cannot audit: it \
                 sees only the wall, so `PinnedOutsideTheWall` is trusted by assertion and \
                 defended only by review of verifier.rs.",
        doc_anchor: "",
    },
    Allowance {
        structure: "SymBlock",
        field: "result",
        category: Category::WallOwnedConstraint,
        reason: "Pinned by a wall-computed equality rather than by bytes directly: \
                 `PlanCheck.checkSymBlockFuel` requires `block.result + 1 = block.nodes.length` \
                 (PlanCheck.lean:323) and that the node at that index exists with a matching \
                 id, so once `nodes` is byte-bound through the plan lowering the result index \
                 carries no independent producer freedom. Reached from acceptance via \
                 `PlanCheck.checkSymRawPlan`.",
        doc_anchor: "",
    },
];

fn short_of(qualified: &str) -> &str {
    qualified.rsplit('.').next().unwrap_or(qualified)
}

/// Run the lint and enforce the exception policy. `format_doc` is the text of
/// `docs/certificate-format.md`, used to keep `KnownGap` entries honest.
///
/// Returns the human-readable report on success, or the failure text.
pub fn check(sources: &[(&str, &str)], format_doc: &str) -> Result<String, String> {
    let report = analyse(sources);
    let mut out = String::new();

    out.push_str(&format!(
        "byte-binding lint: {} producer-declared value slots, {} bound, {} flagged \
         ({} definitions reachable from `accepted`, {} auto-derived byte anchors)\n",
        report.slots.len(),
        report.bound.len(),
        report.flagged.len(),
        report.reachable,
        report.anchors,
    ));

    out.push_str("\ndeclared values NOT bound to the module bytes (standing exceptions):\n");
    for a in ALLOWED {
        out.push_str(&format!(
            "  [{}] {}.{}\n      {}\n",
            a.category.label(),
            a.structure,
            a.field,
            a.reason,
        ));
    }

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

    // 1. Every flagged field must have an allowance.
    for (s, f) in &report.flagged {
        let covered = ALLOWED
            .iter()
            .any(|a| a.structure == short_of(s) && a.field == f);
        if !covered {
            errors.push(format!(
                "UNBOUND producer-declared value `{}.{}`: no conjunct reachable from \
                 `accepted` constrains it against the module bytes.\n    Bind it, or add an \
                 Allowance stating why it is safe to leave declared-only.",
                short_of(s),
                f
            ));
        }
    }

    // 2. No stale allowances: an exception must not outlive the gap it describes.
    for a in ALLOWED {
        if report.is_bound(a.structure, a.field) {
            let ev = report
                .bound
                .iter()
                .find(|((s, f), _)| short_of(s) == a.structure && f == a.field)
                .map(|(_, e)| {
                    format!(
                        "{}:{} in `{}` [{}]\n      {}",
                        e.file, e.line, e.decl, e.rule, e.conjunct
                    )
                })
                .unwrap_or_default();
            errors.push(format!(
                "STALE Allowance for `{}.{}`: the field now has binding evidence at {ev}. \
                 Delete the exception — an exception must not outlive the gap it describes.",
                a.structure, a.field
            ));
        }
    }

    // 3. No phantom fields: a renamed field must not carry its exemption along.
    for a in ALLOWED {
        let exists = report
            .slots
            .iter()
            .any(|(s, f)| short_of(s) == a.structure && f == a.field);
        if !exists {
            errors.push(format!(
                "PHANTOM Allowance for `{}.{}`: no such producer-declared field exists. \
                 It was renamed or removed; update the exception.",
                a.structure, a.field
            ));
        }
        if a.reason.trim().is_empty() {
            errors.push(format!(
                "EMPTY reason on the Allowance for `{}.{}`.",
                a.structure, a.field
            ));
        }
    }

    // 4. Known gaps must be documented as such in the format specification.
    for a in ALLOWED {
        if a.category != Category::KnownGap {
            continue;
        }
        if a.doc_anchor.trim().is_empty() || !format_doc.contains(a.doc_anchor) {
            errors.push(format!(
                "UNDOCUMENTED KnownGap for `{}.{}`: docs/certificate-format.md does not \
                 contain the declared anchor text\n      {:?}\n    A known gap that is not \
                 in the specification is how the last one hid.",
                a.structure, a.field, a.doc_anchor
            ));
        }
    }

    if errors.is_empty() {
        Ok(out)
    } else {
        Err(format!("{out}\n{}\n", errors.join("\n\n")))
    }
}