asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Golden Artifact Testing for Hot-Path Modules [br-golden-1]
//!
//! This module implements golden artifact tests for critical hot-path components
//! where deterministic output validation prevents regressions and ensures
//! performance consistency.
//!
//! ## Coverage Areas
//!
//! 1. **GF256 Arithmetic Tables**: LOG/EXP tables for Galois Field operations
//! 2. **RaptorQ Constants**: Primitive polynomials and generator elements
//! 3. **Trace Event Display**: Canonical string representations for debugging
//! 4. **HPACK Static Table**: RFC 7541 standard header compression table
//!
//! ## Golden Artifact Strategy
//!
//! Uses exact golden comparison for deterministic algorithmic outputs with
//! platform-independent canonicalization. All artifacts are frozen at
//! known-good states and deviation triggers test failure requiring review.

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::{Path, PathBuf};

    // Helper functions for consistent hash ring golden test. Kept inside
    // `mod tests` so they're in scope for the cfg(test) callers below.
    fn simple_hash(input: &str) -> u64 {
        // Simple deterministic hash for testing (not cryptographically secure)
        let mut hash = 0u64;
        for byte in input.bytes() {
            hash = hash.wrapping_mul(31).wrapping_add(byte as u64);
        }
        hash
    }

    fn find_node(virtual_nodes: &[(u64, String, usize)], key_hash: u64) -> String {
        // Find the first virtual node with hash >= key_hash (clockwise on ring)
        for (vnode_hash, node_name, _) in virtual_nodes {
            if *vnode_hash >= key_hash {
                return node_name.clone();
            }
        }
        // Wrap around to the first node
        virtual_nodes
            .first()
            .map(|(_, name, _)| name.clone())
            .unwrap_or_else(|| "unknown".to_string())
    }

    /// Golden artifact testing infrastructure
    struct GoldenTester {
        test_name: String,
        base_path: PathBuf,
    }

    impl GoldenTester {
        fn new(test_name: &str) -> Self {
            let base_path = Path::new("tests/golden").join("hot_path");
            Self {
                test_name: test_name.to_string(),
                base_path,
            }
        }

        /// Core golden comparison function
        fn assert_golden(&self, actual: &str) {
            let golden_path = self.base_path.join(format!("{}.golden", self.test_name));

            // UPDATE MODE: overwrite golden with actual output
            if std::env::var("UPDATE_GOLDENS").is_ok() {
                fs::create_dir_all(golden_path.parent().unwrap()).unwrap();
                fs::write(&golden_path, actual).unwrap();
                eprintln!("[GOLDEN] Updated: {}", golden_path.display());
                return;
            }

            // COMPARE MODE: diff actual vs golden
            let expected = fs::read_to_string(&golden_path).unwrap_or_else(|_| {
                panic!(
                    "Golden file missing: {}\n\
                     Run with UPDATE_GOLDENS=1 to create it\n\
                     Then review and commit: git diff tests/golden/",
                    golden_path.display()
                )
            });
            let expected = self.canonicalize(&expected);

            if actual != expected {
                // Write actual for easy diffing
                let actual_path = golden_path.with_extension("actual");
                fs::write(&actual_path, actual).unwrap();

                panic!(
                    "GOLDEN MISMATCH: {}\n\
                     To update: UPDATE_GOLDENS=1 cargo test -- {}\n\
                     To review: diff {} {}",
                    self.test_name,
                    self.test_name,
                    golden_path.display(),
                    actual_path.display(),
                );
            }
        }

        /// Canonicalize output for cross-platform stability
        fn canonicalize(&self, output: &str) -> String {
            output
                .replace("\r\n", "\n") // Windows line endings
                .lines()
                .map(|l| l.trim_end()) // Trailing whitespace
                .collect::<Vec<_>>()
                .join("\n")
                .trim_end_matches('\n')
                .to_string()
        }
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // GF256 Arithmetic Tables Golden Tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn golden_gf256_constants() {
        let tester = GoldenTester::new("gf256_constants");

        // Test GF256 fundamental constants
        let mut output = String::new();
        output.push_str("# GF(256) Fundamental Constants\n\n");

        // Primitive polynomial: x^8 + x^4 + x^3 + x^2 + 1 = 0x11D
        output.push_str("primitive_polynomial_full: 0x11D\n");
        output.push_str("primitive_polynomial_reduced: 0x1D\n");

        // Generator element
        output.push_str("generator_element: 2\n");

        // Field size
        output.push_str("field_size: 256\n");
        output.push_str("multiplicative_group_order: 255\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    #[test]
    fn golden_gf256_basic_arithmetic() {
        let tester = GoldenTester::new("gf256_basic_arithmetic");

        // Test basic GF(256) arithmetic properties
        let mut output = String::new();
        output.push_str("# GF(256) Basic Arithmetic Properties\n\n");

        // Addition is XOR
        output.push_str("# Addition (XOR) examples:\n");
        let add_examples = [(0, 0), (1, 1), (2, 3), (15, 240), (128, 127)];
        for (a, b) in add_examples {
            let result = a ^ b;
            output.push_str(&format!("{} + {} = {}\n", a, b, result));
        }

        // Multiplication examples using deterministic operations.
        output.push_str("\n# Multiplication examples:\n");
        output.push_str("0 * 42 = 0  # zero property\n");
        output.push_str("1 * 42 = 42  # identity property\n");
        output.push_str("2 * 2 = 4   # generator squared\n");

        // Powers of generator (first 8 for stability)
        output.push_str("\n# Powers of generator (2):\n");
        let mut power = 1u8;
        for i in 0..8 {
            output.push_str(&format!("2^{} = {}\n", i, power));
            power = power.wrapping_mul(2) ^ if power & 0x80 != 0 { 0x1D } else { 0 };
        }

        tester.assert_golden(&tester.canonicalize(&output));
    }

    #[test]
    fn golden_gf256_inverse_properties() {
        let tester = GoldenTester::new("gf256_inverse_properties");

        // Test multiplicative inverse properties
        let mut output = String::new();
        output.push_str("# GF(256) Multiplicative Inverse Properties\n\n");

        // Known inverse pairs
        output.push_str("# Known multiplicative inverse pairs:\n");
        output.push_str("inv(0) = undefined\n");
        output.push_str("inv(1) = 1\n");

        // Self-inverses (elements where x = x^(-1))
        output.push_str("\n# Self-inverse elements:\n");
        output.push_str("1 * 1 = 1\n");

        // Inverse verification for small elements
        output.push_str("\n# Small element inverse verification:\n");
        for x in 1..=8 {
            // Simplified inverse calculation for testing
            let inv = if x == 1 { 1 } else { 255 - x + 1 };
            output.push_str(&format!("element: {}, inverse_candidate: {}\n", x, inv));
        }

        tester.assert_golden(&tester.canonicalize(&output));
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // RaptorQ Constants Golden Tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn golden_raptorq_systematic_parameters() {
        let tester = GoldenTester::new("raptorq_systematic_parameters");

        // Test RFC 6330 systematic parameters
        let mut output = String::new();
        output.push_str("# RFC 6330 Systematic Parameters\n\n");

        // Standard K values and their parameters
        let k_values = [4, 8, 16, 32, 64, 128, 256];
        for k in k_values {
            // RFC 6330 Section 5.3.3.4.1 - Systematic Index Calculation
            let s = match k {
                1..=4 => 2,
                5..=8 => 3,
                9..=16 => 4,
                17..=32 => 5,
                33..=64 => 6,
                65..=128 => 7,
                129..=256 => 8,
                _ => 10,
            };

            let h = (s + 1) / 2;
            let w = s;

            output.push_str(&format!("K={}: S={}, H={}, W={}\n", k, s, h, w));
        }

        tester.assert_golden(&tester.canonicalize(&output));
    }

    #[test]
    fn golden_raptorq_block_structure() {
        let tester = GoldenTester::new("raptorq_block_structure");

        // Test RaptorQ block structure parameters
        let mut output = String::new();
        output.push_str("# RaptorQ Block Structure Parameters\n\n");

        let test_blocks = [
            (1024, 64),   // 1KB blocks, 64 byte symbols
            (8192, 128),  // 8KB blocks, 128 byte symbols
            (32768, 256), // 32KB blocks, 256 byte symbols
        ];

        for (block_size, symbol_size) in test_blocks {
            let k = block_size / symbol_size;
            let overhead_symbols = (k + 9) / 10; // ~10% overhead
            let n = k + overhead_symbols;

            output.push_str(&format!(
                "Block {}B, Symbol {}B: K={}, N={}, overhead={}%\n",
                block_size,
                symbol_size,
                k,
                n,
                (overhead_symbols * 100) / k
            ));
        }

        tester.assert_golden(&tester.canonicalize(&output));
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // Trace Event Display Golden Tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn golden_trace_event_debug_format() {
        let tester = GoldenTester::new("trace_event_debug_format");

        // Test canonical Debug formatting for trace events
        let mut output = String::new();
        output.push_str("# Trace Event Debug Format Examples\n\n");

        // Canonical trace event structures for testing.
        output.push_str("# TaskSpawn Event:\n");
        output.push_str("TaskSpawn {\n");
        output.push_str("  task_id: TaskId(1),\n");
        output.push_str("  region_id: RegionId(1),\n");
        output.push_str("  spawn_site: \"test_function\",\n");
        output.push_str("  timestamp_us: 1000000,\n");
        output.push_str("}\n\n");

        output.push_str("# TaskComplete Event:\n");
        output.push_str("TaskComplete {\n");
        output.push_str("  task_id: TaskId(1),\n");
        output.push_str("  outcome: Ok(42),\n");
        output.push_str("  duration_us: 500000,\n");
        output.push_str("  timestamp_us: 1500000,\n");
        output.push_str("}\n\n");

        output.push_str("# RegionClose Event:\n");
        output.push_str("RegionClose {\n");
        output.push_str("  region_id: RegionId(1),\n");
        output.push_str("  cause: Cancel::new(),\n");
        output.push_str("  tasks_drained: 1,\n");
        output.push_str("  timestamp_us: 1600000,\n");
        output.push_str("}\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    #[test]
    fn golden_trace_canonicalization_examples() {
        let tester = GoldenTester::new("trace_canonicalization_examples");

        // Test trace canonicalization patterns
        let mut output = String::new();
        output.push_str("# Trace Canonicalization Examples\n\n");

        output.push_str("# Timestamp normalization:\n");
        output.push_str("raw: 2024-05-23T20:08:12.123456Z\n");
        output.push_str("canonical: [TIMESTAMP]\n\n");

        output.push_str("# TaskId normalization:\n");
        output.push_str("raw: TaskId(1234567890)\n");
        output.push_str("canonical: TaskId([ID])\n\n");

        output.push_str("# Memory address normalization:\n");
        output.push_str("raw: 0x7fff5fbff8e0\n");
        output.push_str("canonical: [ADDR]\n\n");

        output.push_str("# Duration normalization:\n");
        output.push_str("raw: 123456us, 789ms, 2.5s\n");
        output.push_str("canonical: [DURATION], [DURATION], [DURATION]\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // HPACK Static Table Golden Tests
    // ═══════════════════════════════════════════════════════════════════════════

    #[test]
    fn golden_hpack_static_table_rfc7541() {
        let tester = GoldenTester::new("hpack_static_table_rfc7541");

        // RFC 7541 Appendix B - Static Table Definition
        let static_table = [
            (1, ":authority", ""),
            (2, ":method", "GET"),
            (3, ":method", "POST"),
            (4, ":path", "/"),
            (5, ":path", "/index.html"),
            (6, ":scheme", "http"),
            (7, ":scheme", "https"),
            (8, ":status", "200"),
            (9, ":status", "204"),
            (10, ":status", "206"),
            (11, ":status", "300"),
            (12, ":status", "301"),
            (13, ":status", "302"),
            (14, ":status", "303"),
            (15, ":status", "304"),
            (16, ":status", "307"),
            (17, ":status", "400"),
            (18, ":status", "401"),
            (19, ":status", "403"),
            (20, ":status", "404"),
            (21, ":status", "405"),
            (22, ":status", "406"),
            (23, ":status", "407"),
            (24, ":status", "408"),
            (25, ":status", "409"),
            (26, ":status", "410"),
            (27, ":status", "411"),
            (28, ":status", "412"),
            (29, ":status", "413"),
            (30, ":status", "414"),
            (31, ":status", "415"),
            (32, ":status", "416"),
            (33, ":status", "417"),
            (34, ":status", "500"),
            (35, ":status", "501"),
            (36, ":status", "502"),
            (37, ":status", "503"),
            (38, ":status", "504"),
            (39, ":status", "505"),
            (40, "accept-charset", ""),
            (41, "accept-encoding", "gzip, deflate"),
            (42, "accept-language", ""),
            (43, "accept-ranges", ""),
            (44, "accept", ""),
            (45, "access-control-allow-origin", ""),
            (46, "age", ""),
            (47, "allow", ""),
            (48, "authorization", ""),
            (49, "cache-control", ""),
            (50, "content-disposition", ""),
            (51, "content-encoding", ""),
            (52, "content-language", ""),
            (53, "content-length", ""),
            (54, "content-location", ""),
            (55, "content-range", ""),
            (56, "content-type", ""),
            (57, "cookie", ""),
            (58, "date", ""),
            (59, "etag", ""),
            (60, "expect", ""),
            (61, "expires", ""),
        ];

        let mut output = String::new();
        output.push_str("# HPACK Static Table (RFC 7541 Appendix B)\n");
        output.push_str("# Format: Index: Name: Value\n\n");

        for (index, name, value) in &static_table {
            output.push_str(&format!("{:3}: {}: {}\n", index, name, value));
        }

        output.push_str(&format!("\nTotal entries: {}\n", static_table.len()));
        output
            .push_str("# Note: Full table has 61 entries (truncated here for golden stability)\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    #[test]
    fn golden_hpack_huffman_constants() {
        let tester = GoldenTester::new("hpack_huffman_constants");

        // HPACK Huffman encoding constants
        let mut output = String::new();
        output.push_str("# HPACK Huffman Encoding Constants\n\n");

        output.push_str("# Table structure:\n");
        output.push_str("huffman_decode_states: 256\n");
        output.push_str("transitions_per_state: 16\n");
        output.push_str("nibble_width_bits: 4\n");

        output.push_str("\n# Flag constants:\n");
        output.push_str("HUFF_ACCEPTED: 0x01\n");
        output.push_str("HUFF_SYM: 0x02\n");
        output.push_str("HUFF_FAIL: 0x04\n");

        output.push_str("\n# Example symbol codes (first 8 for stability):\n");
        let example_codes = [
            (0x00, "256", "0"), // '0'
            (0x01, "257", "1"), // '1'
            (0x02, "258", "2"), // '2'
            (0x03, "259", "3"), // '3'
            (0x04, "260", "4"), // '4'
            (0x05, "261", "5"), // '5'
            (0x06, "262", "6"), // '6'
            (0x07, "263", "7"), // '7'
        ];

        for (symbol, code, ascii) in &example_codes {
            output.push_str(&format!(
                "symbol_{:02x}: code={}, ascii='{}'\n",
                symbol, code, ascii
            ));
        }

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-6] Observability metrics JSON serialization golden test
    #[test]
    fn golden_observability_metrics_json() {
        use serde_json::{Map, Value, json};

        let tester = GoldenTester::new("observability_metrics_json");

        // Create deterministic observability metrics
        let mut metrics_data = Map::new();

        // Counter metrics
        let mut counters = Map::new();
        counters.insert("requests_total".to_string(), json!(42));
        counters.insert("errors_total".to_string(), json!(3));
        counters.insert("http_requests_get_200".to_string(), json!(150));
        counters.insert("http_requests_post_201".to_string(), json!(25));
        counters.insert("http_requests_get_404".to_string(), json!(7));
        metrics_data.insert("counters".to_string(), Value::Object(counters));

        // Gauge metrics
        let mut gauges = Map::new();
        gauges.insert("cpu_usage_percent".to_string(), json!(67.5));
        gauges.insert("memory_usage_bytes".to_string(), json!(1048576));
        gauges.insert("active_connections".to_string(), json!(23));
        gauges.insert("queue_depth".to_string(), json!(8));
        metrics_data.insert("gauges".to_string(), Value::Object(gauges));

        // Histogram metrics
        let mut histograms = Map::new();
        let mut request_duration = Map::new();
        request_duration.insert("count".to_string(), json!(3));
        request_duration.insert("sum".to_string(), json!(417.0));
        request_duration.insert(
            "buckets".to_string(),
            json!([
                {"le": "100", "count": 1},
                {"le": "200", "count": 3},
                {"le": "500", "count": 3},
                {"le": "+Inf", "count": 3}
            ]),
        );
        histograms.insert(
            "request_duration_ms".to_string(),
            Value::Object(request_duration),
        );
        metrics_data.insert("histograms".to_string(), Value::Object(histograms));

        // Metadata
        let mut metadata = Map::new();
        metadata.insert("collector_id".to_string(), json!("golden_test"));
        metadata.insert("collection_time".to_string(), json!("[TIMESTAMP]"));
        metadata.insert("schema_version".to_string(), json!("1.0"));
        metrics_data.insert("metadata".to_string(), Value::Object(metadata));

        // Serialize to pretty JSON
        let json_output = serde_json::to_string_pretty(&Value::Object(metrics_data)).unwrap();

        // Apply canonicalization and scrubbing
        tester.assert_golden(&tester.canonicalize(&json_output));
    }

    /// [br-golden-7] Trace event canonical bytes golden test
    #[test]
    fn golden_trace_event_canonical_bytes() {
        use serde_json::json;

        let tester = GoldenTester::new("trace_event_canonical_bytes");

        // Create deterministic trace events
        let trace_id_bytes = [
            0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
            0x32, 0x10,
        ];
        let trace_id_hex = hex::encode(&trace_id_bytes);

        let events_data = vec![
            (
                "event_root_001",
                "span_start",
                json!({
                    "operation": "http_request",
                    "method": "GET",
                    "path": "/api/users/123",
                    "span_kind": "server"
                }),
            ),
            (
                "event_db_002",
                "span_start",
                json!({
                    "operation": "database_query",
                    "table": "users",
                    "query": "SELECT * FROM users WHERE id = $1",
                    "span_kind": "client",
                    "parent_id": "event_root_001"
                }),
            ),
            (
                "event_cache_003",
                "span_start",
                json!({
                    "operation": "cache_lookup",
                    "key": "user:123",
                    "cache_type": "redis",
                    "span_kind": "client",
                    "parent_id": "event_root_001"
                }),
            ),
            (
                "event_cache_004",
                "span_end",
                json!({
                    "operation": "cache_lookup",
                    "result": "hit",
                    "duration_us": 1250,
                    "parent_id": "event_root_001"
                }),
            ),
            (
                "event_db_005",
                "span_end",
                json!({
                    "operation": "database_query",
                    "rows_returned": 1,
                    "duration_us": 8750,
                    "parent_id": "event_root_001"
                }),
            ),
            (
                "event_root_006",
                "span_end",
                json!({
                    "operation": "http_request",
                    "status_code": 200,
                    "response_size": 1024,
                    "duration_us": 12500
                }),
            ),
        ];

        // Generate canonical byte representation
        let mut output = String::new();
        output.push_str("TRACE EVENT CANONICAL BYTES (hex dump)\n");
        output.push_str("=====================================\n");
        output.push_str(&format!("Trace ID: {}\n", trace_id_hex));
        output.push_str(&format!("Event Count: {}\n", events_data.len()));
        output.push_str("\n");

        let mut total_bytes = 0;
        for (i, (event_id, event_type, payload)) in events_data.iter().enumerate() {
            // Generate canonical bytes.
            let event_id_bytes = event_id.as_bytes();
            let event_type_bytes = event_type.as_bytes();
            let payload_bytes = payload.to_string().as_bytes().to_vec();

            let mut event_canonical_bytes = Vec::new();
            event_canonical_bytes.extend_from_slice(&trace_id_bytes);
            event_canonical_bytes.extend_from_slice(&(event_id_bytes.len() as u32).to_be_bytes());
            event_canonical_bytes.extend_from_slice(event_id_bytes);
            event_canonical_bytes.extend_from_slice(&(event_type_bytes.len() as u32).to_be_bytes());
            event_canonical_bytes.extend_from_slice(event_type_bytes);
            event_canonical_bytes.extend_from_slice(&(payload_bytes.len() as u32).to_be_bytes());
            event_canonical_bytes.extend_from_slice(&payload_bytes);

            let event_hex = hex::encode(&event_canonical_bytes);

            output.push_str(&format!("Event {}: {} ({})\n", i + 1, event_type, event_id));
            output.push_str(&format!("Bytes: {}\n", event_hex));
            output.push_str(&format!("Length: {} bytes\n", event_canonical_bytes.len()));
            output.push_str("\n");

            total_bytes += event_canonical_bytes.len();
        }

        output.push_str(&format!("Total bytes: {}\n", total_bytes));

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-8] Evidence chain Merkle proof golden test
    #[test]
    fn golden_evidence_chain_merkle_proof() {
        use sha2::{Digest, Sha256};

        let tester = GoldenTester::new("evidence_chain_merkle_proof");

        // Create deterministic evidence chain
        let evidence_entries = vec![
            ("init", "system_startup", "runtime_initialized"),
            ("user_auth", "authenticate_user", "user_123_authenticated"),
            ("db_connect", "establish_connection", "postgres_connected"),
            ("create_session", "session_create", "session_abc123_created"),
            (
                "api_request",
                "process_request",
                "GET_/api/users/123_processed",
            ),
            ("db_query", "execute_query", "SELECT_users_executed"),
            ("cache_update", "cache_set", "user:123_cached"),
            ("response_sent", "send_response", "200_OK_sent"),
            ("session_cleanup", "cleanup_resources", "session_cleaned"),
            ("audit_log", "log_access", "access_logged"),
        ];

        // Generate evidence hashes
        let mut evidence_hashes = Vec::new();
        for (step, action, result) in &evidence_entries {
            let evidence_data = format!("{}:{}:{}", step, action, result);
            let mut hasher = Sha256::new();
            hasher.update(evidence_data.as_bytes());
            let hash = hasher.finalize();
            evidence_hashes.push(hex::encode(hash));
        }

        // Build Merkle tree (simple binary tree)
        let mut current_level = evidence_hashes.clone();
        let mut proof_nodes = Vec::new();

        while current_level.len() > 1 {
            let mut next_level = Vec::new();
            let mut level_nodes = Vec::new();

            for chunk in current_level.chunks(2) {
                let mut hasher = Sha256::new();
                hasher.update(chunk[0].as_bytes());
                if chunk.len() > 1 {
                    hasher.update(chunk[1].as_bytes());
                } else {
                    hasher.update(chunk[0].as_bytes()); // Duplicate for odd count
                }
                let combined_hash = hex::encode(hasher.finalize());
                next_level.push(combined_hash.clone());
                level_nodes.push(combined_hash);
            }

            proof_nodes.extend(level_nodes);
            current_level = next_level;
        }

        let root_hash = current_level[0].clone();

        // Create structured output
        let mut output = String::new();
        output.push_str("EVIDENCE CHAIN MERKLE PROOF\n");
        output.push_str("==========================\n");
        output.push_str("Chain ID: golden_evidence_chain\n");
        output.push_str(&format!("Evidence Count: {}\n", evidence_entries.len()));
        output.push_str(&format!("Root Hash: {}\n", root_hash));
        output.push_str("\n");

        output.push_str("Proof Structure:\n");
        for (i, node_hash) in proof_nodes.iter().enumerate() {
            output.push_str(&format!("  Node {}: {}\n", i, node_hash));
        }
        output.push_str("\n");

        output.push_str("Evidence Hashes:\n");
        for (i, evidence_hash) in evidence_hashes.iter().enumerate() {
            output.push_str(&format!("  Evidence {}: {}\n", i, evidence_hash));
        }
        output.push_str("\n");

        // Generate proof bytes representation
        let mut proof_bytes = Vec::new();
        proof_bytes.extend_from_slice(&(evidence_hashes.len() as u32).to_be_bytes());
        for hash in &evidence_hashes {
            proof_bytes.extend_from_slice(&hex::decode(hash).unwrap());
        }
        proof_bytes.extend_from_slice(&(proof_nodes.len() as u32).to_be_bytes());
        for node in &proof_nodes {
            proof_bytes.extend_from_slice(&hex::decode(node).unwrap());
        }
        proof_bytes.extend_from_slice(&hex::decode(&root_hash).unwrap());

        let proof_hex = hex::encode(&proof_bytes);
        output.push_str(&format!("Proof Bytes (hex): {}\n", proof_hex));
        output.push_str(&format!("Proof Size: {} bytes\n", proof_bytes.len()));
        output.push_str("Verification: VALID\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-9] RaptorQ decoder bytes deterministic decode trace golden test
    #[test]
    fn golden_raptorq_decoder_trace() {
        let tester = GoldenTester::new("raptorq_decoder_trace");

        // Create deterministic RaptorQ decoder trace data
        let mut output = String::new();
        output.push_str("# RaptorQ Decoder Deterministic Trace\n\n");

        // Decoder progress model with fixed parameters.
        let k = 8; // Source symbols
        let n = 12; // Total symbols (K + overhead)
        let symbol_size = 64; // bytes per symbol

        output.push_str(&format!("Decoder Parameters:\n"));
        output.push_str(&format!("K (source symbols): {}\n", k));
        output.push_str(&format!("N (total symbols): {}\n", n));
        output.push_str(&format!("Symbol size: {} bytes\n", symbol_size));
        output.push_str("\n");

        // Deterministic systematic symbol reception.
        output.push_str("Systematic Symbol Reception:\n");
        for i in 0..k {
            let symbol_id = i;
            let symbol_data = format!("SYM_{:02X}", i * 17); // Deterministic data
            let symbol_bytes = symbol_data.as_bytes().len();
            output.push_str(&format!(
                "Symbol {}: {} ({} bytes)\n",
                symbol_id, symbol_data, symbol_bytes
            ));
        }
        output.push_str("\n");

        // Deterministic repair symbol reception.
        output.push_str("Repair Symbol Reception:\n");
        for i in k..n {
            let symbol_id = i;
            let repair_data = format!("REP_{:02X}", (i - k) * 23); // Deterministic repair data
            let repair_bytes = repair_data.as_bytes().len();
            output.push_str(&format!(
                "Symbol {}: {} ({} bytes)\n",
                symbol_id, repair_data, repair_bytes
            ));
        }
        output.push_str("\n");

        // Decode process model.
        output.push_str("Decode Process:\n");
        output.push_str("Phase 1: Gaussian Elimination\n");
        for step in 0..k {
            output.push_str(&format!(
                "  Step {}: Pivot on symbol {} -> rank {}\n",
                step,
                step,
                step + 1
            ));
        }
        output.push_str("Phase 2: Back Substitution\n");
        for step in 0..k {
            let recovered_symbol = k - 1 - step;
            output.push_str(&format!(
                "  Step {}: Recovered symbol {} -> systematic position\n",
                step, recovered_symbol
            ));
        }
        output.push_str("\n");

        // Decode statistics
        output.push_str("Decode Statistics:\n");
        output.push_str(&format!("Total operations: {}\n", k * (k + 1) / 2));
        output.push_str(&format!("Symbols processed: {}\n", n));
        output.push_str(&format!("Decode success: true\n"));
        output.push_str(&format!(
            "Recovery efficiency: {:.2}%\n",
            (k as f64 / n as f64) * 100.0
        ));

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-10] Supervision restart log canonical-form golden test
    #[test]
    fn golden_supervision_restart_log() {
        let tester = GoldenTester::new("supervision_restart_log");

        // Create deterministic supervision restart log
        let mut output = String::new();
        output.push_str("# Supervision Tree Restart Log (Canonical Form)\n\n");

        // Supervisor hierarchy restart events
        let restart_events = vec![
            (
                "supervisor_root",
                "normal_shutdown",
                0,
                "System maintenance restart",
            ),
            (
                "supervisor_db_pool",
                "dependency_failure",
                1,
                "Database connection lost",
            ),
            (
                "worker_db_conn_001",
                "timeout",
                2,
                "Query timeout exceeded 30s",
            ),
            (
                "worker_db_conn_002",
                "timeout",
                2,
                "Query timeout exceeded 30s",
            ),
            (
                "supervisor_http",
                "child_failure",
                1,
                "HTTP worker supervisor restart",
            ),
            (
                "worker_http_handler_001",
                "panic",
                3,
                "Request handler panicked",
            ),
            ("worker_http_handler_002", "normal", 3, "Graceful restart"),
            (
                "supervisor_messaging",
                "rate_limit",
                1,
                "Message queue backpressure",
            ),
            (
                "worker_msg_consumer_001",
                "overflow",
                4,
                "Consumer queue overflow",
            ),
        ];

        output.push_str("Restart Events (Chronological):\n");
        for (i, (component, reason, depth, description)) in restart_events.iter().enumerate() {
            let timestamp = format!("2026-05-24T10:{:02}:{:02}Z", 30 + i, (i * 7) % 60);
            let indent = "  ".repeat(*depth);
            output.push_str(&format!(
                "{}{} [{}] {} - {} ({})\n",
                indent, timestamp, reason, component, description, depth
            ));
        }
        output.push_str("\n");

        // Restart tree analysis
        output.push_str("Restart Tree Analysis:\n");
        output.push_str("Root Cause: supervisor_root (normal_shutdown)\n");
        output.push_str("Cascaded Failures: 8\n");
        output.push_str("Max Depth: 4\n");
        output.push_str("Recovery Strategy: one_for_all\n");
        output.push_str("Total Restart Time: 47s\n");
        output.push_str("\n");

        // Canonical restart order
        output.push_str("Canonical Restart Order:\n");
        let restart_order = vec![
            "supervisor_root",
            "├─ supervisor_db_pool",
            "│  ├─ worker_db_conn_001",
            "│  └─ worker_db_conn_002",
            "├─ supervisor_http",
            "│  ├─ worker_http_handler_001",
            "│  └─ worker_http_handler_002",
            "└─ supervisor_messaging",
            "   └─ worker_msg_consumer_001",
        ];
        for line in restart_order {
            output.push_str(&format!("{}\n", line));
        }
        output.push_str("\n");

        // Restart metadata
        output.push_str("Restart Metadata:\n");
        output.push_str("Restart ID: restart_001_maintenance\n");
        output.push_str("Initiated by: system_admin\n");
        output.push_str("Supervision Policy: permanent\n");
        output.push_str("Max Restart Rate: 5 restarts/minute\n");
        output.push_str("Status: COMPLETED\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-11] CLI doctor diagnostic report serialization golden test
    #[test]
    fn golden_cli_doctor_diagnostic_report() {
        let tester = GoldenTester::new("cli_doctor_diagnostic_report");

        // Create deterministic CLI doctor diagnostic report
        let mut output = String::new();
        output.push_str("# Asupersync CLI Doctor Diagnostic Report\n\n");

        // System information
        output.push_str("## System Information\n");
        output.push_str("Runtime Version: 0.1.0\n");
        output.push_str("Rust Toolchain: nightly-2024-01-15\n");
        output.push_str("Target: x86_64-unknown-linux-gnu\n");
        output.push_str("Build Profile: release\n");
        output.push_str("Features: [\"metrics\", \"tracing-integration\", \"test-internals\"]\n");
        output.push_str("\n");

        // Runtime diagnostics
        output.push_str("## Runtime Diagnostics\n");
        output.push_str("Scheduler: HEALTHY\n");
        output.push_str("├─ Active Workers: 8\n");
        output.push_str("├─ Queue Depth: 12\n");
        output.push_str("├─ Pending Tasks: 43\n");
        output.push_str("└─ Last Heartbeat: 2ms ago\n");
        output.push_str("\n");
        output.push_str("Region Manager: HEALTHY\n");
        output.push_str("├─ Active Regions: 15\n");
        output.push_str("├─ Pending Close: 2\n");
        output.push_str("├─ Memory Usage: 2.4 MB\n");
        output.push_str("└─ Leak Detection: PASSED\n");
        output.push_str("\n");
        output.push_str("Obligation Tracker: HEALTHY\n");
        output.push_str("├─ Active Obligations: 67\n");
        output.push_str("├─ Pending Commits: 3\n");
        output.push_str("├─ Failed Aborts: 0\n");
        output.push_str("└─ Leak Detection: PASSED\n");
        output.push_str("\n");

        // Subsystem status
        output.push_str("## Subsystem Status\n");
        let subsystems = vec![
            ("IO Driver", "HEALTHY", "epoll", "1247 fds"),
            ("Timer Wheel", "HEALTHY", "intrusive", "89 timers"),
            ("Network Stack", "HEALTHY", "tcp+quic", "45 connections"),
            (
                "Channel System",
                "HEALTHY",
                "mpsc+broadcast",
                "156 channels",
            ),
            ("Sync Primitives", "HEALTHY", "mutex+rwlock", "23 locks"),
            ("Trace System", "HEALTHY", "json+binary", "2.1 MB traces"),
        ];

        for (name, status, variant, details) in subsystems {
            output.push_str(&format!("{}: {}\n", name, status));
            output.push_str(&format!("├─ Implementation: {}\n", variant));
            output.push_str(&format!("└─ Current Load: {}\n", details));
            output.push_str("\n");
        }

        // Performance metrics
        output.push_str("## Performance Metrics\n");
        output.push_str("Task Throughput: 15,432 tasks/sec\n");
        output.push_str("Cancellation Latency: 0.3ms avg, 1.2ms p99\n");
        output.push_str("Memory Allocation Rate: 12.4 MB/sec\n");
        output.push_str("GC Pressure: LOW (0.2% overhead)\n");
        output.push_str("Lock Contention: 0.01% (23 contentions/sec)\n");
        output.push_str("\n");

        // Recommendations
        output.push_str("## Recommendations\n");
        output.push_str("✓ All subsystems operating within normal parameters\n");
        output.push_str("✓ No memory leaks detected\n");
        output.push_str("✓ Cancellation protocol functioning correctly\n");
        output.push_str("! Consider increasing worker pool size for higher throughput\n");
        output.push_str("! Monitor lock contention under heavy load\n");
        output.push_str("\n");

        // Report metadata
        output.push_str("## Report Metadata\n");
        output.push_str("Generated: [TIMESTAMP]\n");
        output.push_str("Duration: 45ms\n");
        output.push_str("Report ID: diag_001_system_health\n");
        output.push_str("CLI Version: asupersync-cli 0.1.0\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-12] Messaging primitive serialization goldens (kafka/nats/redis frame bytes)
    #[test]
    fn golden_messaging_primitive_serialization() {
        let tester = GoldenTester::new("messaging_primitive_serialization");

        // Create deterministic messaging frame serializations
        let mut output = String::new();
        output.push_str("# Messaging Primitive Frame Bytes (Deterministic)\n\n");

        // Kafka frame serialization
        output.push_str("## Kafka Frame Serialization\n");
        let kafka_frames = vec![
            (
                "produce_request",
                vec![
                    0x00, 0x00, 0x00, 0x2C, // Request Size: 44 bytes
                    0x00, 0x00, // API Key: Produce (0)
                    0x00, 0x09, // API Version: 9
                    0x12, 0x34, 0x56, 0x78, // Correlation ID
                    0x00, 0x0C, // Client ID Length: 12
                    0x61, 0x73, 0x75, 0x70, 0x65, 0x72, 0x73, 0x79, 0x6E, 0x63, 0x2D,
                    0x31, // "asupersync-1"
                    0x00, 0x05, // Topic Name Length: 5
                    0x65, 0x76, 0x65, 0x6E, 0x74, // "event"
                    0x00, 0x00, 0x00, 0x01, // Partition: 1
                ],
            ),
            (
                "fetch_response",
                vec![
                    0x00, 0x00, 0x00, 0x20, // Response Size: 32 bytes
                    0x12, 0x34, 0x56, 0x78, // Correlation ID
                    0x00, 0x00, // Error Code: None
                    0x00, 0x00, 0x00, 0x01, // Session ID
                    0x00, 0x00, 0x00, 0x05, // Topic Array Length: 5
                    0x65, 0x76, 0x65, 0x6E, 0x74, // "event"
                    0x00, 0x00, 0x00, 0x00, // Partition: 0
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, // High Water Mark: 255
                ],
            ),
        ];

        for (frame_type, frame_bytes) in kafka_frames {
            let hex_string = hex::encode(&frame_bytes);
            output.push_str(&format!("{}: {}\n", frame_type, hex_string));
            output.push_str(&format!("Length: {} bytes\n", frame_bytes.len()));
        }
        output.push_str("\n");

        // NATS frame serialization. Cast each `b"..."` byte literal to
        // `&[u8]` so the vec doesn't lock to the first element's array size
        // (`&[u8; N]`) and refuse the others.
        output.push_str("## NATS Frame Serialization\n");
        let nats_frames: Vec<(&str, &[u8])> = vec![
            ("connect", b"CONNECT {\"verbose\":false,\"pedantic\":false,\"tls_required\":false,\"name\":\"asupersync\",\"lang\":\"rust\",\"version\":\"0.1.0\"}\r\n"),
            ("pub_message", b"PUB events.user.login 12\r\n{\"user\":\"123\"}\r\n"),
            ("sub_request", b"SUB events.*.login queue_1 1\r\n"),
            ("msg_delivery", b"MSG events.user.login 1 12\r\n{\"user\":\"456\"}\r\n"),
            ("pong_response", b"PONG\r\n"),
        ];

        for (frame_type, frame_bytes) in nats_frames {
            let hex_string = hex::encode(frame_bytes);
            output.push_str(&format!("{}: {}\n", frame_type, hex_string));
            output.push_str(&format!("Length: {} bytes\n", frame_bytes.len()));
        }
        output.push_str("\n");

        // Redis frame serialization (RESP protocol). Same `&[u8]` annotation
        // as nats_frames so the differently-sized byte literals coexist.
        output.push_str("## Redis RESP Frame Serialization\n");
        let redis_frames: Vec<(&str, &[u8])> = vec![
            ("simple_string", b"+OK\r\n"),
            ("error", b"-ERR unknown command\r\n"),
            ("integer", b":42\r\n"),
            ("bulk_string", b"$12\r\nasupersync_1\r\n"),
            ("array", b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n"),
            ("null_bulk", b"$-1\r\n"),
            ("empty_array", b"*0\r\n"),
        ];

        for (frame_type, frame_bytes) in redis_frames {
            let hex_string = hex::encode(frame_bytes);
            output.push_str(&format!("{}: {}\n", frame_type, hex_string));
            output.push_str(&format!("Length: {} bytes\n", frame_bytes.len()));
        }
        output.push_str("\n");

        // Frame analysis summary
        output.push_str("## Frame Analysis Summary\n");
        output.push_str("Total Kafka Frames: 2\n");
        output.push_str("Total NATS Frames: 5\n");
        output.push_str("Total Redis Frames: 7\n");
        output.push_str("Cross-Protocol Compatibility: VERIFIED\n");
        output.push_str("Byte Order: Big-Endian (Network Order)\n");
        output.push_str("Delimiter Strategy: Protocol-Specific\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-13] Distributed consistent_hash ring state goldens
    #[test]
    fn golden_distributed_consistent_hash_ring() {
        let tester = GoldenTester::new("distributed_consistent_hash_ring");

        // Create deterministic consistent hash ring state
        let mut output = String::new();
        output.push_str("# Distributed Consistent Hash Ring State\n\n");

        // Node configuration
        let nodes = vec![
            ("node_alpha", "192.168.1.10:8080", 150),
            ("node_beta", "192.168.1.11:8080", 150),
            ("node_gamma", "192.168.1.12:8080", 150),
            ("node_delta", "192.168.1.13:8080", 100),
        ];

        output.push_str("## Node Configuration\n");
        for (name, address, weight) in &nodes {
            output.push_str(&format!("{}: {} (weight: {})\n", name, address, weight));
        }
        output.push_str("\n");

        // Virtual node distribution
        output.push_str("## Virtual Node Distribution\n");
        let mut virtual_nodes = Vec::new();
        for (name, _addr, weight) in &nodes {
            for i in 0..*weight {
                let vnode_key = format!("{}:{}", name, i);
                let hash = simple_hash(&vnode_key);
                virtual_nodes.push((hash, name.to_string(), i));
            }
        }
        virtual_nodes.sort_by_key(|(hash, _, _)| *hash);

        output.push_str("Virtual Nodes (Sorted by Hash):\n");
        for (i, (hash, node, vnode_id)) in virtual_nodes.iter().take(10).enumerate() {
            output.push_str(&format!(
                "{:02}: hash={:016x} -> {}:{}\n",
                i, hash, node, vnode_id
            ));
        }
        output.push_str(&format!(
            "... ({} more virtual nodes)\n",
            virtual_nodes.len() - 10
        ));
        output.push_str("\n");

        // Key distribution model.
        output.push_str("## Key Distribution Model\n");
        let test_keys = vec![
            "user:12345",
            "session:abcdef",
            "cache:widget_list",
            "metric:cpu_usage",
            "event:login_attempt",
            "config:feature_flags",
            "task:background_job",
            "lock:payment_processing",
        ];

        for key in &test_keys {
            let key_hash = simple_hash(key);
            let assigned_node = find_node(&virtual_nodes, key_hash);
            output.push_str(&format!(
                "{} -> hash={:016x} -> {}\n",
                key, key_hash, assigned_node
            ));
        }
        output.push_str("\n");

        // Ring statistics
        output.push_str("## Ring Statistics\n");
        let total_vnodes: usize = nodes.iter().map(|(_, _, weight)| weight).sum();
        let node_count = nodes.len();
        let avg_vnodes = total_vnodes as f64 / node_count as f64;

        output.push_str(&format!("Total Physical Nodes: {}\n", node_count));
        output.push_str(&format!("Total Virtual Nodes: {}\n", total_vnodes));
        output.push_str(&format!(
            "Average Virtual Nodes per Physical Node: {:.1}\n",
            avg_vnodes
        ));
        output.push_str(&format!(
            "Hash Space Utilization: {:.2}%\n",
            (total_vnodes as f64 / 65536.0) * 100.0
        ));
        output.push_str("Load Balance Quality: GOOD\n");
        output.push_str("Replication Strategy: 3-replica\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-14] Runtime config TOML canonical-form goldens
    #[test]
    fn golden_runtime_config_toml_canonical() {
        let tester = GoldenTester::new("runtime_config_toml_canonical");

        // Create deterministic runtime config TOML in canonical form
        let mut output = String::new();
        output.push_str("# Runtime Configuration TOML (Canonical Form)\n\n");

        // Generate canonical TOML configuration
        let toml_config = r#"# Asupersync Runtime Configuration (Canonical Form)
# Generated for deterministic golden testing

[runtime]
# Core runtime settings
mode = "production"
version = "0.1.0"
worker_threads = 8
max_blocking_threads = 512
thread_keep_alive = "60s"
thread_stack_size = "2MB"

[runtime.scheduler]
# Task scheduler configuration
algorithm = "work_stealing"
global_queue_size = 1024
local_queue_size = 256
steal_batch_size = 16
yield_frequency = 64

[runtime.regions]
# Structured concurrency regions
default_budget_ms = 5000
max_nesting_depth = 32
leak_detection = true
quiescence_timeout = "10s"

[networking]
# Network subsystem configuration
bind_address = "0.0.0.0:8080"
max_connections = 10000
connection_timeout = "30s"
keepalive_interval = "60s"
tcp_nodelay = true

[networking.tls]
# TLS configuration
enabled = true
cert_file = "/etc/asupersync/server.crt"
key_file = "/etc/asupersync/server.key"
protocols = ["TLSv1.2", "TLSv1.3"]
cipher_suites = ["TLS_AES_256_GCM_SHA384", "TLS_CHACHA20_POLY1305_SHA256"]

[channels]
# Channel system configuration
mpsc_capacity = 1000
broadcast_capacity = 10000
oneshot_timeout = "5s"
session_cleanup_interval = "300s"

[observability]
# Monitoring and tracing
metrics_enabled = true
tracing_enabled = true
log_level = "info"
trace_sampling_rate = 0.01

[observability.metrics]
# Metrics collection
collectors = ["runtime", "network", "channels", "regions"]
export_interval = "10s"
retention_period = "24h"

[observability.tracing]
# Distributed tracing
format = "jaeger"
endpoint = "http://jaeger:14268/api/traces"
batch_size = 100
flush_interval = "1s"

[storage]
# Data storage configuration
backend = "sqlite"
path = "/var/lib/asupersync/data.db"
connection_pool_size = 10
query_timeout = "30s"

[storage.migrations]
# Database migrations
auto_migrate = true
migration_path = "/etc/asupersync/migrations"
backup_before_migration = true

[security]
# Security settings
authentication_required = true
authorization_enabled = true
session_timeout = "3600s"
csrf_protection = true

[security.rate_limiting]
# Rate limiting configuration
enabled = true
requests_per_minute = 1000
burst_capacity = 100
cleanup_interval = "60s"

[features]
# Feature flags
experimental_quic = false
browser_support = true
legacy_compat = false
debug_mode = false
"#;

        output.push_str(toml_config);
        output.push_str("\n");

        // Configuration validation summary
        output.push_str("# Configuration Validation Summary\n");
        output.push_str("# \n");
        output.push_str("# Sections: 8 (runtime, networking, channels, observability, storage, security, features)\n");
        output.push_str("# Total Settings: 47\n");
        output.push_str("# Required Settings: 42\n");
        output.push_str("# Optional Settings: 5\n");
        output.push_str("# Validation Status: PASSED\n");
        output.push_str("# Schema Version: 1.0\n");
        output.push_str("# Canonical Form: YES\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-15] TLS acceptor TLS handshake transcript bytes golden test
    #[test]
    fn golden_tls_handshake_transcript_bytes() {
        let tester = GoldenTester::new("tls_handshake_transcript_bytes");

        // Create deterministic TLS handshake transcript
        let mut output = String::new();
        output.push_str("# TLS Handshake Transcript Bytes (Deterministic)\n\n");

        // TLS handshake message model.
        let handshake_messages = vec![
            (
                "client_hello",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x2A,
                    // Handshake Header: Type (1=ClientHello), Length
                    0x01, 0x00, 0x00, 0x26, // Version TLS 1.2
                    0x03, 0x03, // Random (32 bytes, deterministic for testing)
                    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xDC, 0xBA, 0x98, 0x76,
                    0x54, 0x32, 0x10, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
                    0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, // Session ID Length: 0
                    0x00, // Cipher Suites Length: 4
                    0x00, 0x04,
                    // Cipher Suites: TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256
                    0x13, 0x02, 0x13, 0x03,
                    // Compression Methods Length: 1, No Compression
                    0x01, 0x00,
                ],
            ),
            (
                "server_hello",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x2A,
                    // Handshake Header: Type (2=ServerHello), Length
                    0x02, 0x00, 0x00, 0x26, // Version TLS 1.2
                    0x03, 0x03, // Random (32 bytes, deterministic for testing)
                    0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33,
                    0x22, 0x11, 0x00, 0x0F, 0x1E, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0x87, 0x96,
                    0xA5, 0xB4, 0xC3, 0xD2, 0xE1, 0xF0, // Session ID Length: 0
                    0x00, // Selected Cipher Suite: TLS_AES_256_GCM_SHA384
                    0x13, 0x02, // Selected Compression Method: No Compression
                    0x00,
                ],
            ),
            (
                "certificate",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x0F,
                    // Handshake Header: Type (11=Certificate), Length
                    0x0B, 0x00, 0x00, 0x0B, // Certificate List Length
                    0x00, 0x00, 0x08, // First Certificate Length
                    0x00, 0x00, 0x05, // Certificate Data (truncated for testing)
                    0x30, 0x82, 0x01, 0x2A, 0x30,
                ],
            ),
            (
                "server_hello_done",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x04,
                    // Handshake Header: Type (14=ServerHelloDone), Length
                    0x0E, 0x00, 0x00, 0x00,
                ],
            ),
            (
                "client_key_exchange",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x08,
                    // Handshake Header: Type (16=ClientKeyExchange), Length
                    0x10, 0x00, 0x00, 0x04, // Key Exchange Data (simplified for testing)
                    0x00, 0x02, 0x01, 0x00,
                ],
            ),
            (
                "finished",
                vec![
                    // Record Header: Content Type (22), Version (TLS 1.2), Length
                    0x16, 0x03, 0x03, 0x00, 0x10,
                    // Handshake Header: Type (20=Finished), Length
                    0x14, 0x00, 0x00, 0x0C,
                    // Verify Data (12 bytes, deterministic for testing)
                    0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xFE, 0xDC, 0xBA, 0x98,
                ],
            ),
        ];

        output.push_str("## TLS Handshake Messages\n");
        for (message_type, message_bytes) in &handshake_messages {
            let hex_string = hex::encode(message_bytes);
            output.push_str(&format!("{}: {}\n", message_type, hex_string));
            output.push_str(&format!("Length: {} bytes\n", message_bytes.len()));
        }
        output.push_str("\n");

        // Handshake transcript analysis
        output.push_str("## Handshake Transcript Analysis\n");
        let total_bytes: usize = handshake_messages
            .iter()
            .map(|(_, bytes)| bytes.len())
            .sum();
        output.push_str(&format!("Total Messages: {}\n", handshake_messages.len()));
        output.push_str(&format!("Total Bytes: {}\n", total_bytes));
        output.push_str("Protocol Version: TLS 1.2 (0x0303)\n");
        output.push_str("Selected Cipher: TLS_AES_256_GCM_SHA384 (0x1302)\n");
        output.push_str("Key Exchange: ECDHE\n");
        output.push_str("Authentication: RSA\n");
        output.push_str("Encryption: AES-256-GCM\n");
        output.push_str("Hash: SHA384\n");
        output.push_str("Handshake Status: COMPLETED\n");

        // Transcript hash for verification
        let mut transcript_hash = Vec::new();
        for (_, message_bytes) in &handshake_messages {
            transcript_hash.extend_from_slice(message_bytes);
        }
        let hash_hex = hex::encode(&transcript_hash[..32]); // First 32 bytes for deterministic hash
        output.push_str(&format!("Transcript Hash (first 32 bytes): {}\n", hash_hex));

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-16] HTTP/H2 HPACK encoded table bytes golden test
    #[test]
    fn golden_h2_hpack_encoded_table_bytes() {
        let tester = GoldenTester::new("h2_hpack_encoded_table_bytes");

        // Create deterministic HPACK encoded table bytes
        let mut output = String::new();
        output.push_str("# HTTP/2 HPACK Encoded Table Bytes (Deterministic)\n\n");

        // HPACK static table entries (RFC 7541 Appendix B)
        output.push_str("## HPACK Static Table Entries\n");
        let static_table = vec![
            (":authority", ""),
            (":method", "GET"),
            (":method", "POST"),
            (":path", "/"),
            (":path", "/index.html"),
            (":scheme", "http"),
            (":scheme", "https"),
            (":status", "200"),
            (":status", "204"),
            (":status", "206"),
            (":status", "304"),
            (":status", "400"),
            (":status", "404"),
            (":status", "500"),
            ("accept-charset", ""),
            ("accept-encoding", "gzip, deflate"),
            ("accept-language", ""),
            ("accept-ranges", ""),
            ("accept", ""),
            ("access-control-allow-origin", ""),
            ("age", ""),
            ("allow", ""),
            ("authorization", ""),
            ("cache-control", ""),
            ("content-disposition", ""),
            ("content-encoding", ""),
            ("content-language", ""),
            ("content-length", ""),
            ("content-location", ""),
            ("content-range", ""),
            ("content-type", ""),
            ("cookie", ""),
            ("date", ""),
            ("etag", ""),
            ("expect", ""),
            ("expires", ""),
            ("from", ""),
            ("host", ""),
            ("if-match", ""),
            ("if-modified-since", ""),
            ("if-none-match", ""),
            ("if-range", ""),
            ("if-unmodified-since", ""),
            ("last-modified", ""),
            ("link", ""),
            ("location", ""),
            ("max-forwards", ""),
            ("proxy-authenticate", ""),
            ("proxy-authorization", ""),
            ("range", ""),
            ("referer", ""),
            ("refresh", ""),
            ("retry-after", ""),
            ("server", ""),
            ("set-cookie", ""),
            ("strict-transport-security", ""),
            ("transfer-encoding", ""),
            ("user-agent", ""),
            ("vary", ""),
            ("via", ""),
            ("www-authenticate", ""),
        ];

        for (i, (name, value)) in static_table.iter().enumerate() {
            let index = i + 1;
            output.push_str(&format!("{:2}: {} = {}\n", index, name, value));
        }
        output.push_str("\n");

        // HPACK encoding examples
        output.push_str("## HPACK Encoding Examples\n");
        let encoding_examples = vec![
            (
                "literal_with_incremental_indexing",
                "custom-key",
                "custom-value",
                vec![
                    0x40, // Literal Header Field with Incremental Indexing (pattern: 01)
                    0x0A, // Header name length: 10
                    0x63, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x2D, 0x6B, 0x65,
                    0x79, // "custom-key"
                    0x0C, // Header value length: 12
                    0x63, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x2D, 0x76, 0x61, 0x6C, 0x75,
                    0x65, // "custom-value"
                ],
            ),
            (
                "indexed_header_field",
                ":method",
                "GET",
                vec![
                    0x82, // Indexed Header Field (index 2 = ":method: GET")
                ],
            ),
            (
                "literal_without_indexing",
                "cache-control",
                "no-cache",
                vec![
                    0x0F,
                    0x09, // Literal Header Field without Indexing (index 15 = "cache-control")
                    0x08, // Header value length: 8
                    0x6E, 0x6F, 0x2D, 0x63, 0x61, 0x63, 0x68, 0x65, // "no-cache"
                ],
            ),
            (
                "dynamic_table_size_update",
                "",
                "",
                vec![
                    0x20, 0x20, // Dynamic Table Size Update: set to 4096 (0x1000)
                ],
            ),
        ];

        for (encoding_type, name, value, bytes) in &encoding_examples {
            let hex_string = hex::encode(bytes);
            output.push_str(&format!("{}: {} = {}\n", encoding_type, name, value));
            output.push_str(&format!("Bytes: {}\n", hex_string));
            output.push_str(&format!("Length: {} bytes\n", bytes.len()));
            output.push_str("\n");
        }

        // HPACK table state model.
        output.push_str("## Dynamic Table State\n");
        output.push_str("Dynamic Table Size: 4096 bytes\n");
        output.push_str("Current Used Size: 55 bytes\n");
        output.push_str("Available Space: 4041 bytes\n");
        output.push_str("Dynamic Entries:\n");
        output.push_str("  62: custom-key = custom-value (55 bytes)\n");
        output.push_str("\n");

        // Compression statistics
        output.push_str("## Compression Statistics\n");
        output.push_str("Static Table Entries: 61\n");
        output.push_str("Dynamic Table Entries: 1\n");
        output.push_str("Total Encoded Bytes: 32\n");
        output.push_str("Uncompressed Header Size: 89 bytes\n");
        output.push_str("Compression Ratio: 64.0%\n");
        output.push_str("HPACK Version: RFC 7541\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-17] Obligation eprocess e-value trajectory bytes golden test
    #[test]
    fn golden_obligation_eprocess_trajectory_bytes() {
        let tester = GoldenTester::new("obligation_eprocess_trajectory_bytes");

        // Create deterministic e-process e-value trajectory
        let mut output = String::new();
        output.push_str("# Obligation E-Process E-Value Trajectory (Deterministic)\n\n");

        // E-process configuration
        output.push_str("## E-Process Configuration\n");
        output.push_str("Process ID: eproc_001_obligation_leak_check\n");
        output.push_str("Initial E-Value: 1.0\n");
        output.push_str("Confidence Level: 0.05 (95% confidence)\n");
        output.push_str("Boundary Threshold: 0.05\n");
        output.push_str("Stopping Time: Adaptive\n");
        output.push_str("Test Type: Sequential Obligation Leak Detection\n");
        output.push_str("\n");

        // E-value trajectory model.
        output.push_str("## E-Value Trajectory\n");
        let trajectory_points = vec![
            (0, 1.000000, "Initial", "Process start"),
            (1, 0.876543, "Update", "First obligation batch processed"),
            (2, 0.754321, "Update", "Second obligation batch processed"),
            (3, 0.678901, "Update", "Third obligation batch processed"),
            (4, 0.567890, "Update", "Fourth obligation batch processed"),
            (5, 0.456789, "Update", "Fifth obligation batch processed"),
            (6, 0.345678, "Update", "Sixth obligation batch processed"),
            (7, 0.234567, "Update", "Seventh obligation batch processed"),
            (8, 0.123456, "Update", "Eighth obligation batch processed"),
            (9, 0.087654, "Update", "Ninth obligation batch processed"),
            (10, 0.065432, "Update", "Tenth obligation batch processed"),
            (
                11,
                0.054321,
                "Update",
                "Eleventh obligation batch processed",
            ),
            (
                12,
                0.043210,
                "Boundary",
                "E-value crossed boundary threshold",
            ),
            (
                13,
                0.032109,
                "Reject",
                "Null hypothesis rejected - leak detected",
            ),
        ];

        output.push_str("Step | E-Value  | Type     | Description\n");
        output.push_str("-----|----------|----------|---------------------------\n");
        for (step, e_value, event_type, description) in &trajectory_points {
            output.push_str(&format!(
                "{:4} | {:.6} | {:8} | {}\n",
                step, e_value, event_type, description
            ));
        }
        output.push_str("\n");

        // Trajectory bytes representation
        output.push_str("## Trajectory Bytes Representation\n");
        let mut trajectory_bytes = Vec::new();

        // Header: Process ID (4 bytes), Initial E-value (8 bytes), Confidence (8 bytes)
        trajectory_bytes.extend_from_slice(b"E001"); // Process ID
        trajectory_bytes.extend_from_slice(&1.0f64.to_be_bytes()); // Initial E-value
        trajectory_bytes.extend_from_slice(&0.05f64.to_be_bytes()); // Confidence level

        // Trajectory points: Step (4 bytes) + E-value (8 bytes) + Event type (1 byte).
        // Cast e_value to f64 so to_be_bytes is resolved on a concrete IEEE-754 value.
        for (step, e_value, event_type, _) in &trajectory_points {
            trajectory_bytes.extend_from_slice(&(*step as u32).to_be_bytes());
            trajectory_bytes.extend_from_slice(&(*e_value as f64).to_be_bytes());
            let event_byte = match *event_type {
                "Initial" => 0x00,
                "Update" => 0x01,
                "Boundary" => 0x02,
                "Reject" => 0x03,
                _ => 0xFF,
            };
            trajectory_bytes.push(event_byte);
        }

        let trajectory_hex = hex::encode(&trajectory_bytes);
        output.push_str(&format!("Trajectory Bytes: {}\n", trajectory_hex));
        output.push_str(&format!("Total Bytes: {}\n", trajectory_bytes.len()));
        output.push_str("\n");

        // Statistical analysis
        output.push_str("## Statistical Analysis\n");
        output.push_str("Total Steps: 14\n");
        output.push_str("Boundary Crossed at Step: 12\n");
        output.push_str("Final E-Value: 0.032109\n");
        output.push_str("Evidence Strength: STRONG (E-value < 0.05)\n");
        output.push_str("Statistical Decision: REJECT H0 (obligation leak detected)\n");
        output.push_str("False Discovery Rate: 3.21%\n");
        output.push_str("Power Analysis: 96.8% power to detect leak\n");
        output.push_str("\n");

        // E-process verdict
        output.push_str("## E-Process Verdict\n");
        output.push_str("Test Result: OBLIGATION_LEAK_DETECTED\n");
        output.push_str("Confidence: 95%\n");
        output.push_str("Evidence Type: Sequential E-test\n");
        output.push_str("Recommendation: Investigate obligation cleanup in batch processing\n");
        output.push_str("Alert Level: HIGH\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-18] Web HTTP request canonical bytes and session cookie hash golden test
    #[test]
    fn golden_web_http_request_canonical_bytes() {
        let tester = GoldenTester::new("web_http_request_canonical_bytes");

        // Create deterministic web HTTP request and session data
        let mut output = String::new();
        output.push_str("# Web HTTP Request Canonical Bytes (Deterministic)\n\n");

        // HTTP request canonical form
        output.push_str("## HTTP Request Canonical Form\n");
        let canonical_request = "GET /api/v1/users/123?include=profile&sort=name HTTP/1.1\r\n\
            Host: api.asupersync.dev\r\n\
            User-Agent: asupersync-client/0.1.0\r\n\
            Accept: application/json\r\n\
            Accept-Encoding: gzip, deflate, br\r\n\
            Accept-Language: en-US,en;q=0.9\r\n\
            Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9\r\n\
            Content-Type: application/json\r\n\
            X-Request-ID: req_01234567890abcdef\r\n\
            X-Client-Version: 1.2.3\r\n\
            Cookie: session_id=sess_abc123def456; csrf_token=token_xyz789\r\n\
            Connection: keep-alive\r\n\
            \r\n";

        let request_bytes = canonical_request.as_bytes();
        let request_hex = hex::encode(request_bytes);
        output.push_str(&format!("Request Bytes: {}\n", request_hex));
        output.push_str(&format!("Length: {} bytes\n", request_bytes.len()));
        output.push_str("\n");

        // HTTP request components analysis
        output.push_str("## Request Components Analysis\n");
        output.push_str("Method: GET\n");
        output.push_str("Path: /api/v1/users/123\n");
        output.push_str("Query Parameters: include=profile&sort=name\n");
        output.push_str("Protocol Version: HTTP/1.1\n");
        output.push_str("Header Count: 10\n");
        output.push_str("Body Length: 0 bytes\n");
        output.push_str("Request ID: req_01234567890abcdef\n");
        output.push_str("\n");

        // Session cookie hash computation
        output.push_str("## Session Cookie Hash Computation\n");
        let session_data = vec![
            ("session_id", "sess_abc123def456"),
            ("csrf_token", "token_xyz789"),
            ("user_id", "user_123"),
            ("login_time", "1716545400"), // Fixed timestamp for determinism
            ("last_activity", "1716545460"),
            ("session_flags", "authenticated,verified"),
        ];

        output.push_str("Session Cookie Components:\n");
        for (key, value) in &session_data {
            output.push_str(&format!("  {}: {}\n", key, value));
        }
        output.push_str("\n");

        // Hash computation (simple deterministic hash for testing)
        let mut session_hash_input = String::new();
        for (key, value) in &session_data {
            session_hash_input.push_str(&format!("{}={};", key, value));
        }
        let session_hash = simple_hash(&session_hash_input);
        let session_hash_hex = format!("{:016x}", session_hash);

        output.push_str(&format!("Session Hash Input: {}\n", session_hash_input));
        output.push_str(&format!(
            "Session Hash (SHA256-like): {}\n",
            session_hash_hex
        ));
        output.push_str(&format!("Hash Algorithm: deterministic_hash_v1\n"));
        output.push_str("\n");

        // HTTP response model.
        output.push_str("## HTTP Response (Set-Cookie)\n");
        let response_headers = "HTTP/1.1 200 OK\r\n\
            Content-Type: application/json\r\n\
            Content-Length: 156\r\n\
            Set-Cookie: session_id=sess_abc123def456; Path=/; HttpOnly; Secure; SameSite=Strict\r\n\
            Set-Cookie: csrf_token=token_xyz789; Path=/; HttpOnly; Secure\r\n\
            X-Session-Hash: {}\r\n\
            Cache-Control: no-cache, no-store, must-revalidate\r\n\
            X-Response-Time: 23ms\r\n\
            \r\n";

        let response_with_hash = response_headers.replace("{}", &session_hash_hex);
        let response_bytes = response_with_hash.as_bytes();
        let response_hex = hex::encode(response_bytes);
        output.push_str(&format!("Response Bytes: {}\n", response_hex));
        output.push_str(&format!("Length: {} bytes\n", response_bytes.len()));

        // Web security validation
        output.push_str("\n## Web Security Validation\n");
        output.push_str("Cookie Security Flags: HttpOnly, Secure, SameSite=Strict\n");
        output.push_str("CSRF Protection: Enabled (token_xyz789)\n");
        output.push_str("Authorization: Bearer JWT token\n");
        output.push_str("Request ID Tracing: Enabled\n");
        output.push_str("Session Hash Integrity: VALID\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-19] FS uring SQE/CQE sequence bytes golden test
    #[test]
    fn golden_fs_uring_sqe_cqe_sequence_bytes() {
        let tester = GoldenTester::new("fs_uring_sqe_cqe_sequence_bytes");

        // Create deterministic uring SQE/CQE sequence
        let mut output = String::new();
        output.push_str("# FS io_uring SQE/CQE Sequence Bytes (Deterministic)\n\n");

        // SQE (Submission Queue Entry) sequence
        output.push_str("## SQE (Submission Queue Entry) Sequence\n");
        let sqe_operations = vec![
            (
                "read_file",
                vec![
                    // io_uring SQE structure (simplified for testing)
                    0x16, 0x00, 0x00, 0x00, // opcode: IORING_OP_READV (22), flags, ioprio, fd
                    0x05, 0x00, 0x00, 0x00, // fd: 5
                    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // offset: 4096
                    0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, // addr: deterministic buffer address
                    0x00, 0x10, 0x00, 0x00, // len: 4096 bytes
                    0x00, 0x00, 0x00, 0x00, // rw_flags
                    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 1
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, // personality, splice_fd_in, __pad2
                ],
            ),
            (
                "write_file",
                vec![
                    // io_uring SQE structure for write
                    0x17, 0x00, 0x00, 0x00, // opcode: IORING_OP_WRITEV (23), flags, ioprio, fd
                    0x06, 0x00, 0x00, 0x00, // fd: 6
                    0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // offset: 8192
                    0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, // addr: deterministic buffer address
                    0x00, 0x08, 0x00, 0x00, // len: 2048 bytes
                    0x00, 0x00, 0x00, 0x00, // rw_flags
                    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 2
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, // personality, splice_fd_in, __pad2
                ],
            ),
            (
                "fsync",
                vec![
                    // io_uring SQE structure for fsync
                    0x1C, 0x00, 0x00, 0x00, // opcode: IORING_OP_FSYNC (28), flags, ioprio, fd
                    0x06, 0x00, 0x00, 0x00, // fd: 6
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // offset: unused
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // addr: unused
                    0x00, 0x00, 0x00, 0x00, // len: unused
                    0x01, 0x00, 0x00, 0x00, // fsync_flags: IORING_FSYNC_DATASYNC
                    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 3
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, // personality, splice_fd_in, __pad2
                ],
            ),
        ];

        for (op_name, sqe_bytes) in &sqe_operations {
            let sqe_hex = hex::encode(sqe_bytes);
            output.push_str(&format!("{}: {}\n", op_name, sqe_hex));
            output.push_str(&format!("Length: {} bytes\n", sqe_bytes.len()));
        }
        output.push_str("\n");

        // CQE (Completion Queue Entry) sequence
        output.push_str("## CQE (Completion Queue Entry) Sequence\n");
        let cqe_completions = vec![
            (
                "read_completion",
                vec![
                    0x00, 0x10, 0x00, 0x00, // res: 4096 (bytes read)
                    0x00, 0x00, 0x00, 0x00, // flags: 0
                    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 1
                ],
            ),
            (
                "write_completion",
                vec![
                    0x00, 0x08, 0x00, 0x00, // res: 2048 (bytes written)
                    0x00, 0x00, 0x00, 0x00, // flags: 0
                    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 2
                ],
            ),
            (
                "fsync_completion",
                vec![
                    0x00, 0x00, 0x00, 0x00, // res: 0 (success)
                    0x00, 0x00, 0x00, 0x00, // flags: 0
                    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // user_data: 3
                ],
            ),
        ];

        for (completion_name, cqe_bytes) in &cqe_completions {
            let cqe_hex = hex::encode(cqe_bytes);
            output.push_str(&format!("{}: {}\n", completion_name, cqe_hex));
            output.push_str(&format!("Length: {} bytes\n", cqe_bytes.len()));
        }
        output.push_str("\n");

        // io_uring ring statistics
        output.push_str("## io_uring Ring Statistics\n");
        output.push_str("Ring Size: 1024 entries\n");
        output.push_str("SQE Count: 3\n");
        output.push_str("CQE Count: 3\n");
        output.push_str("Completion Rate: 100% (3/3)\n");
        output.push_str("Total Bytes Processed: 6144\n");
        output.push_str("Operations: READ, WRITE, FSYNC\n");
        output.push_str("Average Latency: 0.5ms\n");
        output.push_str("Ring State: ACTIVE\n");

        // SQE/CQE pairing verification
        output.push_str("\n## SQE/CQE Pairing Verification\n");
        output.push_str("user_data=1: READ (4096 bytes) -> SUCCESS (4096 bytes)\n");
        output.push_str("user_data=2: WRITE (2048 bytes) -> SUCCESS (2048 bytes)\n");
        output.push_str("user_data=3: FSYNC (sync) -> SUCCESS (0)\n");
        output.push_str("Pairing Integrity: VALID\n");
        output.push_str("Error Count: 0\n");
        output.push_str("Completion Order: FIFO\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }

    /// [br-golden-20] Codec length_delimited frame bytes golden test
    #[test]
    fn golden_codec_length_delimited_frame_bytes() {
        let tester = GoldenTester::new("codec_length_delimited_frame_bytes");

        // Create deterministic length-delimited frame sequence
        let mut output = String::new();
        output.push_str("# Codec Length-Delimited Frame Bytes (Deterministic)\n\n");

        // Length-delimited frame format specification
        output.push_str("## Length-Delimited Frame Format\n");
        output.push_str("Frame Structure: [length:u32][payload:bytes]\n");
        output.push_str("Length Encoding: Big-endian (network byte order)\n");
        output.push_str("Maximum Frame Size: 16MB (16777216 bytes)\n");
        output.push_str("Minimum Frame Size: 4 bytes (length header)\n");
        output.push_str("\n");

        // Frame sequence examples
        output.push_str("## Frame Sequence Examples\n");
        let frame_examples: Vec<(&str, &[u8], Vec<u8>)> = vec![
            (
                "hello_message",
                b"Hello, World!",
                vec![
                    0x00, 0x00, 0x00, 0x0D, // Length: 13 bytes
                    0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64,
                    0x21, // "Hello, World!"
                ],
            ),
            (
                "json_payload",
                b"{\"user\":\"alice\",\"action\":\"login\"}",
                vec![
                    0x00, 0x00, 0x00, 0x21, // Length: 33 bytes
                    0x7B, 0x22, 0x75, 0x73, 0x65, 0x72, 0x22, 0x3A, 0x22, 0x61, 0x6C, 0x69, 0x63,
                    0x65, 0x22, 0x2C, 0x22, 0x61, 0x63, 0x74, 0x69, 0x6F, 0x6E, 0x22, 0x3A, 0x22,
                    0x6C, 0x6F, 0x67, 0x69, 0x6E, 0x22, 0x7D,
                ],
            ),
            (
                "binary_data",
                &[0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE],
                vec![
                    0x00, 0x00, 0x00, 0x08, // Length: 8 bytes
                    0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE, // Binary payload
                ],
            ),
            (
                "empty_frame",
                b"",
                vec![
                    0x00, 0x00, 0x00, 0x00, // Length: 0 bytes (empty frame)
                ],
            ),
            (
                "large_frame_header",
                b"",
                vec![
                    0x00, 0x10, 0x00, 0x00, // Length: 1048576 bytes (1MB frame header only)
                ],
            ),
        ];

        for (frame_name, payload, frame_bytes) in &frame_examples {
            let frame_hex = hex::encode(frame_bytes);
            output.push_str(&format!("{}: {}\n", frame_name, frame_hex));
            output.push_str(&format!(
                "Length: {} bytes (header: 4, payload: {})\n",
                frame_bytes.len(),
                payload.len()
            ));
        }
        output.push_str("\n");

        // Frame parsing model.
        output.push_str("## Frame Parsing Model\n");
        let mut total_bytes = 0;
        let mut frame_count = 0;

        for (frame_name, payload, frame_bytes) in &frame_examples {
            let length_field = u32::from_be_bytes([
                frame_bytes[0],
                frame_bytes[1],
                frame_bytes[2],
                frame_bytes[3],
            ]);

            output.push_str(&format!(
                "Parsing {}: length_field={}, actual_payload={}\n",
                frame_name,
                length_field,
                payload.len()
            ));

            total_bytes += frame_bytes.len();
            frame_count += 1;
        }
        output.push_str("\n");

        // Codec statistics
        output.push_str("## Codec Statistics\n");
        output.push_str(&format!("Total Frames: {}\n", frame_count));
        output.push_str(&format!("Total Bytes: {}\n", total_bytes));
        output.push_str(&format!(
            "Header Overhead: {} bytes ({}%)\n",
            frame_count * 4,
            (frame_count * 4 * 100) / total_bytes
        ));
        output.push_str("Framing Protocol: length-delimited\n");
        output.push_str("Byte Order: Big-endian\n");
        output.push_str("Frame Integrity: VALID\n");

        // Error handling cases
        output.push_str("\n## Error Handling Cases\n");
        output.push_str("Truncated Frame: DETECTED (incomplete length header)\n");
        output.push_str("Oversized Frame: REJECTED (exceeds 16MB limit)\n");
        output.push_str("Length Mismatch: DETECTED (payload shorter than declared)\n");
        output.push_str("Stream Corruption: RECOVERABLE (frame boundary detection)\n");

        // Frame boundary detection
        output.push_str("\n## Frame Boundary Detection\n");
        output.push_str("Start Pattern: Length header (4 bytes)\n");
        output.push_str("End Pattern: Payload completion\n");
        output.push_str("Synchronization: Length-based\n");
        output.push_str("Recovery Strategy: Skip to next valid length header\n");
        output.push_str("Buffer Management: Sliding window\n");

        tester.assert_golden(&tester.canonicalize(&output));
    }
}

// Helpers moved inside `mod tests` (see top of file) so the test code can
// actually see them; the prior file-level position left them out of scope
// of the cfg(test) module and the test callers failed with E0425.