fp-conformance 0.7.1

Differential conformance harness for frankenpandas against a live pandas oracle — packet fixtures + fuzz seed tests + live-oracle parity gates.
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
use std::collections::{BTreeMap, BTreeSet};

use fp_frame::DataFrame;
use fp_index::{IndexLabel, align_union, validate_alignment_plan};
use fp_io::{
    IoError as FpIoError, read_feather_bytes, read_ipc_stream_bytes, read_parquet_bytes,
    write_feather_bytes, write_ipc_stream_bytes, write_parquet_bytes,
};

use super::{fuzz_feather_frame_from_bytes, fuzz_index_from_bytes};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FuzzArrowFormat {
    Parquet,
    Feather,
    IpcStream,
}

impl FuzzArrowFormat {
    fn from_byte(byte: u8) -> Self {
        match byte % 3 {
            0 => Self::Parquet,
            1 => Self::Feather,
            _ => Self::IpcStream,
        }
    }

    fn distinct_from_byte(primary: Self, byte: u8) -> Self {
        match primary {
            Self::Parquet => {
                if byte.is_multiple_of(2) {
                    Self::Feather
                } else {
                    Self::IpcStream
                }
            }
            Self::Feather => {
                if byte.is_multiple_of(2) {
                    Self::Parquet
                } else {
                    Self::IpcStream
                }
            }
            Self::IpcStream => {
                if byte.is_multiple_of(2) {
                    Self::Parquet
                } else {
                    Self::Feather
                }
            }
        }
    }

    fn name(self) -> &'static str {
        match self {
            Self::Parquet => "parquet",
            Self::Feather => "feather",
            Self::IpcStream => "ipc_stream",
        }
    }
}

fn fuzz_roundtrip_frame_via_format(
    format: FuzzArrowFormat,
    frame: &DataFrame,
) -> Result<DataFrame, FpIoError> {
    match format {
        FuzzArrowFormat::Parquet => {
            let encoded = write_parquet_bytes(frame)?;
            read_parquet_bytes(&encoded)
        }
        FuzzArrowFormat::Feather => {
            let encoded = write_feather_bytes(frame)?;
            read_feather_bytes(&encoded)
        }
        FuzzArrowFormat::IpcStream => {
            let encoded = write_ipc_stream_bytes(frame)?;
            read_ipc_stream_bytes(&encoded)
        }
    }
}

/// Structure-aware fuzz entrypoint for cross-format Arrow-backed IO parity.
///
/// The input always runs in synth mode: bytes are projected into a small typed
/// `DataFrame`, round-tripped through a primary Arrow-backed format, then
/// round-tripped again through a distinct secondary format. The logical frame
/// recovered from both stages must converge to the same content.
pub fn fuzz_format_cross_round_trip_bytes(input: &[u8]) -> Result<(), FpIoError> {
    let primary = FuzzArrowFormat::from_byte(input.first().copied().unwrap_or_default());
    let secondary =
        FuzzArrowFormat::distinct_from_byte(primary, input.get(1).copied().unwrap_or(1));
    let payload = input.get(2..).unwrap_or(&[]);
    let source = fuzz_feather_frame_from_bytes(payload)?;
    let primary_frame = fuzz_roundtrip_frame_via_format(primary, &source)?;
    let secondary_frame = fuzz_roundtrip_frame_via_format(secondary, &primary_frame)?;

    if !primary_frame.equals(&secondary_frame) {
        return Err(FpIoError::Io(std::io::Error::other(format!(
            "cross-format round-trip drifted between {} and {}",
            primary.name(),
            secondary.name()
        ))));
    }

    Ok(())
}

/// Structure-aware fuzz entrypoint for `fp-index` outer alignment semantics.
///
/// Input is split at the first `|` byte into left/right index payloads. Each
/// payload byte is projected onto a small `IndexLabel` domain so fuzzing
/// naturally exercises duplicates, overlap, and mixed Int64/Utf8 labels.
pub fn fuzz_index_align_bytes(input: &[u8]) -> Result<(), String> {
    let Some(split_at) = input.iter().position(|byte| *byte == b'|') else {
        return Ok(());
    };

    let left = fuzz_index_from_bytes(&input[..split_at]);
    let right = fuzz_index_from_bytes(&input[split_at + 1..]);
    let plan = align_union(&left, &right);
    validate_alignment_plan(&plan).map_err(|err| err.to_string())?;

    let mut output_counts = BTreeMap::<IndexLabel, usize>::new();
    let mut left_position_counts = BTreeMap::<IndexLabel, BTreeMap<usize, usize>>::new();
    let mut right_position_counts = BTreeMap::<IndexLabel, BTreeMap<usize, usize>>::new();

    for row in 0..plan.union_index.len() {
        let label = &plan.union_index.labels()[row];
        *output_counts.entry(label.clone()).or_default() += 1;

        match plan.left_positions[row] {
            Some(left_pos) => {
                let actual = left.labels().get(left_pos).ok_or_else(|| {
                    format!("left position out of bounds: row={row} pos={left_pos}")
                })?;
                if actual != label {
                    return Err(format!(
                        "left alignment label mismatch: row={row} pos={left_pos} \
                         output={label:?} actual={actual:?}"
                    ));
                }
                *left_position_counts
                    .entry(label.clone())
                    .or_default()
                    .entry(left_pos)
                    .or_default() += 1;
            }
            None if left.labels().iter().any(|candidate| candidate == label) => {
                return Err(format!(
                    "left position missing despite label presence: row={row} label={label:?}"
                ));
            }
            None => {}
        }

        match plan.right_positions[row] {
            Some(right_pos) => {
                let actual = right.labels().get(right_pos).ok_or_else(|| {
                    format!("right position out of bounds: row={row} pos={right_pos}")
                })?;
                if actual != label {
                    return Err(format!(
                        "right alignment label mismatch: row={row} pos={right_pos} \
                         output={label:?} actual={actual:?}"
                    ));
                }
                *right_position_counts
                    .entry(label.clone())
                    .or_default()
                    .entry(right_pos)
                    .or_default() += 1;
            }
            None if right.labels().iter().any(|candidate| candidate == label) => {
                return Err(format!(
                    "right position missing despite label presence: row={row} label={label:?}"
                ));
            }
            None => {}
        }
    }

    let mut left_counts = BTreeMap::<IndexLabel, usize>::new();
    let mut right_counts = BTreeMap::<IndexLabel, usize>::new();
    let mut left_expected_position_counts = BTreeMap::<IndexLabel, BTreeMap<usize, usize>>::new();
    let mut right_expected_position_counts = BTreeMap::<IndexLabel, BTreeMap<usize, usize>>::new();

    for (position, label) in left.labels().iter().enumerate() {
        *left_counts.entry(label.clone()).or_default() += 1;
        left_expected_position_counts
            .entry(label.clone())
            .or_default()
            .insert(position, 0);
    }

    for (position, label) in right.labels().iter().enumerate() {
        *right_counts.entry(label.clone()).or_default() += 1;
        right_expected_position_counts
            .entry(label.clone())
            .or_default()
            .insert(position, 0);
    }

    let labels = left_counts
        .keys()
        .chain(right_counts.keys())
        .cloned()
        .collect::<BTreeSet<_>>();

    for label in labels {
        let left_count = *left_counts.get(&label).unwrap_or(&0);
        let right_count = *right_counts.get(&label).unwrap_or(&0);
        let expected_output_count = match (left_count, right_count) {
            (0, count) => count,
            (count, 0) => count,
            (left_count, right_count) => left_count * right_count,
        };
        let actual_output_count = *output_counts.get(&label).unwrap_or(&0);
        if actual_output_count != expected_output_count {
            return Err(format!(
                "alignment multiplicity mismatch for {label:?}: expected={expected_output_count} \
                 actual={actual_output_count} left_count={left_count} right_count={right_count}"
            ));
        }

        if left_count > 0 && right_count > 0 {
            if let Some(expected_counts) = left_expected_position_counts.get_mut(&label) {
                for count in expected_counts.values_mut() {
                    *count = right_count;
                }
            }
            if let Some(expected_counts) = right_expected_position_counts.get_mut(&label) {
                for count in expected_counts.values_mut() {
                    *count = left_count;
                }
            }
        } else if right_count == 0 {
            if let Some(expected_counts) = left_expected_position_counts.get_mut(&label) {
                for count in expected_counts.values_mut() {
                    *count = 1;
                }
            }
            right_expected_position_counts.remove(&label);
        } else {
            left_expected_position_counts.remove(&label);
            if let Some(expected_counts) = right_expected_position_counts.get_mut(&label) {
                for count in expected_counts.values_mut() {
                    *count = 1;
                }
            }
        }

        let actual_left = left_position_counts
            .get(&label)
            .cloned()
            .unwrap_or_default();
        let expected_left = left_expected_position_counts
            .get(&label)
            .cloned()
            .unwrap_or_default();
        if actual_left != expected_left {
            return Err(format!(
                "left position multiplicity mismatch for {label:?}: \
                 expected={expected_left:?} actual={actual_left:?}"
            ));
        }

        let actual_right = right_position_counts
            .get(&label)
            .cloned()
            .unwrap_or_default();
        let expected_right = right_expected_position_counts
            .get(&label)
            .cloned()
            .unwrap_or_default();
        if actual_right != expected_right {
            return Err(format!(
                "right position multiplicity mismatch for {label:?}: \
                 expected={expected_right:?} actual={actual_right:?}"
            ));
        }
    }

    Ok(())
}

// ============================================================================
// Differential Fuzzing Against Live Pandas Oracle (br-frankenpandas-rc0923-epic-full-surface-conformance-j0lg5.6)
// ============================================================================

use std::{
    fs,
    io::{BufRead, BufReader, BufWriter, Write},
    path::{Path, PathBuf},
    process::{Child, ChildStdin, ChildStdout, Command, Stdio},
    sync::{Mutex, OnceLock},
};

use fp_frame::Series;
use fp_groupby::{GroupByOptions, groupby_sum};
use fp_runtime::{EvidenceLedger, RuntimePolicy};
use fp_types::{NullKind, Scalar, nanmax, nanmean, nanmin, nansum};
use serde::{Deserialize, Serialize};

use super::{
    FixtureDataFrame, FixtureExpectedDataFrame, FixtureExpectedSeries, FixtureMultiIndex,
    FixtureSeries, HarnessConfig, OrderedColumnData, build_dataframe, build_series,
    compare_dataframe_expected, compare_scalar, compare_series_expected, pinned_pandas_version,
};

/// Swarm-testing configuration decoded from the first byte of a differential fuzz seed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DifferentialSwarmConfig {
    pub allow_nulls: bool,
    pub allow_duplicate_labels: bool,
    pub allow_multiindex: bool,
    pub allow_float64: bool,
    pub allow_bool: bool,
    pub allow_utf8: bool,
    pub op_family_mask: u8,
}

impl DifferentialSwarmConfig {
    #[must_use]
    pub fn from_byte(byte: u8) -> Self {
        let dtype_bits = (byte >> 3) & 0b0111;
        Self {
            allow_nulls: (byte & 0b0001) != 0,
            allow_duplicate_labels: (byte & 0b0010) != 0,
            allow_multiindex: (byte & 0b0100) != 0,
            allow_float64: (dtype_bits & 0b001) != 0,
            allow_bool: (dtype_bits & 0b010) != 0,
            allow_utf8: (dtype_bits & 0b100) != 0,
            op_family_mask: ((byte >> 6) & 0b0011).max(1),
        }
    }

    #[must_use]
    pub fn subset_tag(&self) -> String {
        format!(
            "nulls={}_dups={}_mi={}_f64={}_bool={}_utf8={}_fam={}",
            u8::from(self.allow_nulls),
            u8::from(self.allow_duplicate_labels),
            u8::from(self.allow_multiindex),
            u8::from(self.allow_float64),
            u8::from(self.allow_bool),
            u8::from(self.allow_utf8),
            self.op_family_mask
        )
    }
}

/// Single step in a differential DataFrame operation sequence.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "step", rename_all = "snake_case")]
pub enum DifferentialChainStep {
    SortIndex { ascending: bool },
    SortValues { column: String, ascending: bool },
    DropNa,
    FillNaZero,
    ResetIndexDrop,
    SelectColumns { columns: Vec<String> },
    Head { n: usize },
    Tail { n: usize },
}

/// Generated differential fuzz operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DifferentialFuzzCase {
    NanSum {
        swarm: DifferentialSwarmConfig,
        series: FixtureSeries,
    },
    NanMean {
        swarm: DifferentialSwarmConfig,
        series: FixtureSeries,
    },
    NanMin {
        swarm: DifferentialSwarmConfig,
        series: FixtureSeries,
    },
    NanMax {
        swarm: DifferentialSwarmConfig,
        series: FixtureSeries,
    },
    SeriesAdd {
        swarm: DifferentialSwarmConfig,
        left: FixtureSeries,
        right: FixtureSeries,
    },
    GroupBySum {
        swarm: DifferentialSwarmConfig,
        keys: FixtureSeries,
        values: FixtureSeries,
    },
    DataFrameOpChain {
        swarm: DifferentialSwarmConfig,
        frame: FixtureDataFrame,
        steps: Vec<DifferentialChainStep>,
    },
}

impl DifferentialFuzzCase {
    #[must_use]
    pub fn op_name(&self) -> &'static str {
        match self {
            Self::NanSum { .. } => "nan_sum",
            Self::NanMean { .. } => "nan_mean",
            Self::NanMin { .. } => "nan_min",
            Self::NanMax { .. } => "nan_max",
            Self::SeriesAdd { .. } => "series_add",
            Self::GroupBySum { .. } => "groupby_sum",
            Self::DataFrameOpChain { .. } => "dataframe_op_chain",
        }
    }

    #[must_use]
    pub fn swarm(&self) -> &DifferentialSwarmConfig {
        match self {
            Self::NanSum { swarm, .. }
            | Self::NanMean { swarm, .. }
            | Self::NanMin { swarm, .. }
            | Self::NanMax { swarm, .. }
            | Self::SeriesAdd { swarm, .. }
            | Self::GroupBySum { swarm, .. }
            | Self::DataFrameOpChain { swarm, .. } => swarm,
        }
    }
}

/// Comparator mode for differential fuzzing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DifferentialComparatorMode {
    /// Full conformance comparator checking exact index order, column order, MultiIndex, and values.
    Strict,
    /// Deliberately broken comparator that ignores index order (for NEGATIVE self-test verification).
    IgnoreIndexOrder,
}

/// Optional fault injection on the FrankenPandas side to verify the harness catches divergences.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DifferentialFaultInjection {
    None,
    /// Reverses the output index labels without reordering the values.
    ReverseIndexOrder,
}

/// Serializable reproduction packet emitted when a divergence is found (never auto-committed to fixtures/).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DifferentialReproPacket {
    pub seed_hex: String,
    pub minimized_seed_hex: String,
    pub case: DifferentialFuzzCase,
    pub mismatch: String,
}

impl DifferentialReproPacket {
    pub fn write_to_dir(&self, dir: &Path) -> std::io::Result<PathBuf> {
        fs::create_dir_all(dir)?;
        let file_name = format!(
            "repro_{}_{}.json",
            self.case.op_name(),
            &self.minimized_seed_hex[..self.minimized_seed_hex.len().min(16)]
        );
        let path = dir.join(file_name);
        let json = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
        fs::write(&path, json)?;
        Ok(path)
    }
}

/// Summary report from a bounded differential fuzzing campaign.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DifferentialFuzzCampaignReport {
    pub total_seeds: usize,
    pub executed_vs_oracle: usize,
    pub skipped_no_oracle: usize,
    pub divergences_found: usize,
    pub op_counts: BTreeMap<String, usize>,
    pub swarm_subset_counts: BTreeMap<String, usize>,
    pub repros: Vec<DifferentialReproPacket>,
}

struct LongLivedOracleWorker {
    _child: Child,
    stdin: BufWriter<ChildStdin>,
    stdout: BufReader<ChildStdout>,
    pandas_version: String,
}

#[derive(Debug, Deserialize)]
struct BatchWorkerResponse {
    #[serde(default)]
    pandas_version: Option<String>,
    #[serde(default)]
    expected_series: Option<FixtureExpectedSeries>,
    #[serde(default)]
    expected_frame: Option<FixtureExpectedDataFrame>,
    #[serde(default)]
    expected_scalar: Option<Scalar>,
    #[serde(default)]
    error: Option<String>,
}

const BATCH_WORKER_PYTHON_DRIVER: &str = r#"
import argparse
import json
import os
import sys

oracle_dir = sys.argv[1]
legacy_root = sys.argv[2]
sys.path.insert(0, oracle_dir)

import pandas_oracle as po

ns = argparse.Namespace(
    legacy_root=legacy_root,
    strict_legacy=True,
    allow_system_pandas_fallback=True,
)
try:
    pd = po.setup_pandas(ns)
    po._PD = pd
except Exception as exc:
    sys.stdout.write(json.dumps({"error": str(exc), "error_origin": "oracle_unavailable"}) + "\n")
    sys.stdout.flush()
    raise SystemExit(0)

sys.stdout.write(json.dumps({"pandas_version": str(pd.__version__)}) + "\n")
sys.stdout.flush()

def run_op_chain(pd_mod, frame_payload, steps):
    df = po.dataframe_from_json(pd_mod, frame_payload)
    for step in steps:
        kind = step.get("step")
        if kind == "sort_index":
            df = df.sort_index(ascending=bool(step.get("ascending", True)), kind="mergesort")
        elif kind == "sort_values":
            col = str(step.get("column"))
            if col in df.columns:
                df = df.sort_values(
                    by=col,
                    ascending=bool(step.get("ascending", True)),
                    na_position="last",
                    kind="mergesort",
                )
        elif kind == "drop_na":
            df = df.dropna()
        elif kind == "fill_na_zero":
            df = df.fillna(0)
        elif kind == "reset_index_drop":
            df = df.reset_index(drop=True)
        elif kind == "select_columns":
            cols = [str(c) for c in step.get("columns", []) if str(c) in df.columns]
            if cols:
                df = df.loc[:, cols]
        elif kind == "head":
            df = df.head(int(step.get("n", 1)))
        elif kind == "tail":
            df = df.tail(int(step.get("n", 1)))
    return {"expected_frame": po.dataframe_to_json(df)}

for raw_line in sys.stdin:
    line = raw_line.strip()
    if not line:
        continue
    try:
        req = json.loads(line)
        mode = req.get("mode")
        if mode == "dispatch":
            resp = po.dispatch(pd, req["payload"])
        elif mode == "op_chain":
            resp = run_op_chain(pd, req["frame"], req["steps"])
        else:
            raise po.OracleError(f"unknown batch mode: {mode!r}")
        out = {
            "pandas_version": str(pd.__version__),
            "expected_series": resp.get("expected_series"),
            "expected_frame": resp.get("expected_frame"),
            "expected_scalar": resp.get("expected_scalar"),
            "error": None,
            "error_origin": None,
        }
    except po.OracleError as exc:
        out = {
            "pandas_version": str(pd.__version__),
            "error": str(exc),
            "error_origin": po.oracle_error_origin(exc),
        }
    except Exception as exc:
        out = {
            "pandas_version": str(pd.__version__),
            "error": str(exc),
            "error_origin": "pandas",
        }
    sys.stdout.write(json.dumps(out) + "\n")
    sys.stdout.flush()
"#;

static ORACLE_WORKER: OnceLock<Mutex<Option<LongLivedOracleWorker>>> = OnceLock::new();

fn query_oracle_worker(
    config: &HarnessConfig,
    request_json: &serde_json::Value,
) -> Result<BatchWorkerResponse, String> {
    let mutex = ORACLE_WORKER.get_or_init(|| Mutex::new(None));
    let mut guard = mutex
        .lock()
        .map_err(|_| "oracle worker mutex poisoned".to_owned())?;

    if guard.is_none() {
        let script_path = config.oracle_script_path();
        let oracle_dir = script_path
            .parent()
            .ok_or_else(|| "invalid oracle script path".to_owned())?;
        let mut child = match Command::new(&config.python_bin)
            .arg("-u")
            .arg("-c")
            .arg(BATCH_WORKER_PYTHON_DRIVER)
            .arg(oracle_dir)
            .arg(&config.oracle_root)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
        {
            Ok(child) => child,
            Err(err) => {
                return Err(format!("oracle_unavailable: failed to spawn python: {err}"));
            }
        };

        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| "oracle_unavailable: missing child stdin".to_owned())?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| "oracle_unavailable: missing child stdout".to_owned())?;
        let mut reader = BufReader::new(stdout);
        let mut banner_line = String::new();
        if reader
            .read_line(&mut banner_line)
            .map_err(|e| format!("oracle_unavailable: banner read failed: {e}"))?
            == 0
        {
            return Err("oracle_unavailable: empty banner from python worker".to_owned());
        }
        let banner: BatchWorkerResponse = serde_json::from_str(&banner_line)
            .map_err(|e| format!("oracle_unavailable: invalid banner json: {e}"))?;
        if let Some(err) = banner.error {
            return Err(format!("oracle_unavailable: {err}"));
        }
        let version = banner.pandas_version.unwrap_or_default();
        if let Some(pin) = pinned_pandas_version(config)
            && version != pin
        {
            return Err(format!(
                "oracle_unavailable: pandas version {version} != pinned {pin}"
            ));
        }
        *guard = Some(LongLivedOracleWorker {
            _child: child,
            stdin: BufWriter::new(stdin),
            stdout: reader,
            pandas_version: version,
        });
    }

    let worker = guard.as_mut().expect("worker initialized above");
    let _ = &worker.pandas_version;
    let line = serde_json::to_string(request_json).map_err(|e| e.to_string())?;
    if writeln!(worker.stdin, "{line}").is_err() || worker.stdin.flush().is_err() {
        *guard = None;
        return Err("oracle_unavailable: failed to write to oracle worker stdin".to_owned());
    }

    let mut resp_line = String::new();
    match worker.stdout.read_line(&mut resp_line) {
        Ok(0) | Err(_) => {
            *guard = None;
            return Err("oracle_unavailable: oracle worker closed stdout".to_owned());
        }
        Ok(_) => {}
    }

    serde_json::from_str::<BatchWorkerResponse>(&resp_line)
        .map_err(|e| format!("invalid oracle worker response JSON: {e}"))
}

fn byte_at(bytes: &[u8], idx: usize) -> u8 {
    if bytes.is_empty() {
        0
    } else {
        bytes[idx % bytes.len()]
    }
}

fn project_index_labels(
    bytes: &[u8],
    rows: usize,
    allow_duplicates: bool,
    use_utf8: bool,
) -> Vec<IndexLabel> {
    let mut labels = Vec::with_capacity(rows);
    for r in 0..rows {
        let raw = byte_at(bytes, r);
        let label = if allow_duplicates {
            let bucket = i64::from(raw % 4);
            if use_utf8 {
                IndexLabel::Utf8(format!("k{bucket}"))
            } else {
                IndexLabel::Int64(bucket)
            }
        } else {
            // Permutation of distinct labels so sort_index exercises non-trivial reordering
            let val = i64::from((raw as usize + r * 3) as u8);
            let mut candidate = if use_utf8 {
                IndexLabel::Utf8(format!("k{val:03}"))
            } else {
                IndexLabel::Int64(val)
            };
            let mut salt = 1_i64;
            while labels.contains(&candidate) {
                candidate = if use_utf8 {
                    IndexLabel::Utf8(format!("k{:03}", val + salt))
                } else {
                    IndexLabel::Int64(val + salt)
                };
                salt += 1;
            }
            candidate
        };
        labels.push(label);
    }
    labels
}

fn project_numeric_series(
    bytes: &[u8],
    name: &str,
    rows: usize,
    swarm: &DifferentialSwarmConfig,
) -> FixtureSeries {
    let use_utf8_idx = swarm.allow_utf8;
    let index = project_index_labels(bytes, rows, swarm.allow_duplicate_labels, use_utf8_idx);
    let use_float = swarm.allow_float64 || swarm.allow_nulls;
    let mut values = Vec::with_capacity(rows);
    for r in 0..rows {
        let b = byte_at(bytes, r + 1);
        if swarm.allow_nulls && r > 0 && b.is_multiple_of(5) {
            values.push(Scalar::Null(NullKind::NaN));
        } else if use_float {
            let v = f64::from(i16::from(b) - 64) * 0.5;
            values.push(Scalar::Float64(v));
        } else {
            let v = i64::from(b % 31) - 15;
            values.push(Scalar::Int64(v));
        }
    }
    FixtureSeries {
        name: name.to_owned(),
        index,
        values,
    }
}

fn project_dataframe_and_chain(
    bytes: &[u8],
    swarm: &DifferentialSwarmConfig,
    chain_len: usize,
) -> (FixtureDataFrame, Vec<DifferentialChainStep>) {
    let rows = usize::from(byte_at(bytes, 0) % 5) + 1;
    let col_count = usize::from(byte_at(bytes, 1) % 3) + 1;
    let col_names = ["a", "b", "c"][..col_count]
        .iter()
        .map(|s| (*s).to_owned())
        .collect::<Vec<_>>();

    let (index, row_multiindex) = if swarm.allow_multiindex {
        let mut tuples = Vec::with_capacity(rows);
        let mut flat = Vec::with_capacity(rows);
        for r in 0..rows {
            let b0 = byte_at(bytes, 2 + r * 2);
            let b1 = byte_at(bytes, 3 + r * 2);
            let l0_val = if swarm.allow_duplicate_labels {
                i64::from(b0 % 3) * 8 + 2
            } else {
                i64::from((b0 % 7) + (r as u8) * 3)
            };
            let l1_str = format!("s{}", b1 % 3);
            flat.push(IndexLabel::Utf8(format!("{l0_val}|{l1_str}")));
            tuples.push(vec![IndexLabel::Int64(l0_val), IndexLabel::Utf8(l1_str)]);
        }
        (
            flat,
            Some(FixtureMultiIndex {
                tuples,
                names: vec![Some("l0".to_owned()), Some("l1".to_owned())],
            }),
        )
    } else {
        let use_utf8_idx = (byte_at(bytes, 2) & 1) != 0;
        (
            project_index_labels(
                bytes.get(3..).unwrap_or(bytes),
                rows,
                swarm.allow_duplicate_labels,
                use_utf8_idx,
            ),
            None,
        )
    };

    let mut enabled_kinds = vec![0_u8]; // 0 = int64/float64 numeric
    if swarm.allow_float64 {
        enabled_kinds.push(1);
    }
    if swarm.allow_bool && !swarm.allow_nulls {
        enabled_kinds.push(2);
    }
    if swarm.allow_utf8 && !swarm.allow_nulls {
        enabled_kinds.push(3);
    }

    let mut col_map = BTreeMap::new();
    let mut all_numeric = true;
    for (c_idx, col_name) in col_names.iter().enumerate() {
        let kind_tag =
            enabled_kinds[(usize::from(byte_at(bytes, 4 + c_idx))) % enabled_kinds.len()];
        let mut vals = Vec::with_capacity(rows);
        for r in 0..rows {
            let b = byte_at(bytes, 7 + c_idx * rows + r);
            match kind_tag {
                0 => {
                    if swarm.allow_nulls && r > 0 && b.is_multiple_of(4) {
                        vals.push(Scalar::Null(NullKind::NaN));
                    } else if swarm.allow_nulls {
                        vals.push(Scalar::Float64(f64::from(i16::from(b % 23) - 10)));
                    } else {
                        vals.push(Scalar::Int64(i64::from(b % 23) - 10));
                    }
                }
                1 => {
                    if swarm.allow_nulls && r > 0 && b.is_multiple_of(4) {
                        vals.push(Scalar::Null(NullKind::NaN));
                    } else {
                        vals.push(Scalar::Float64(f64::from(i16::from(b % 29) - 14) * 0.5));
                    }
                }
                2 => {
                    all_numeric = false;
                    vals.push(Scalar::Bool(b.is_multiple_of(2)));
                }
                _ => {
                    all_numeric = false;
                    vals.push(Scalar::Utf8(format!("v{}", b % 5)));
                }
            }
        }
        col_map.insert(col_name.clone(), vals);
    }

    let frame = FixtureDataFrame {
        index,
        columns: OrderedColumnData::from(col_map),
        column_order: Some(col_names.clone()),
        categorical_columns: None,
        row_multiindex,
    };

    let mut steps = Vec::with_capacity(chain_len);
    let mut has_multiindex = frame.row_multiindex.is_some();
    let mut current_cols = col_names;
    for s in 0..chain_len {
        let step_byte = byte_at(bytes, 16 + s);
        let raw_choice = (step_byte.wrapping_add(swarm.op_family_mask)) % 7;
        let step = match raw_choice {
            0 => DifferentialChainStep::SortIndex {
                ascending: (step_byte & 0b1000) == 0,
            },
            1 => {
                let col = current_cols[usize::from(step_byte) % current_cols.len()].clone();
                DifferentialChainStep::SortValues {
                    column: col,
                    ascending: (step_byte & 0b1000) == 0,
                }
            }
            2 if !has_multiindex => DifferentialChainStep::DropNa,
            3 if all_numeric => DifferentialChainStep::FillNaZero,
            4 => {
                has_multiindex = false;
                DifferentialChainStep::ResetIndexDrop
            }
            5 => {
                let keep_n = (usize::from(step_byte) % current_cols.len()).max(1);
                let kept = current_cols
                    .iter()
                    .take(keep_n)
                    .cloned()
                    .collect::<Vec<_>>();
                current_cols.clone_from(&kept);
                DifferentialChainStep::SelectColumns { columns: kept }
            }
            6 => DifferentialChainStep::Head {
                n: usize::from(step_byte % 4) + 1,
            },
            _ => DifferentialChainStep::Tail {
                n: usize::from(step_byte % 4) + 1,
            },
        };
        steps.push(step);
    }

    (frame, steps)
}

/// Deterministic projection from arbitrary bytes into a [`DifferentialFuzzCase`].
#[must_use]
pub fn project_differential_fuzz_case(input: &[u8]) -> DifferentialFuzzCase {
    let swarm = DifferentialSwarmConfig::from_byte(byte_at(input, 0));
    let op_selector = byte_at(input, 1);
    let payload = input.get(2..).unwrap_or(&[]);
    let rows = usize::from(byte_at(payload, 0) % 5) + 1;

    match op_selector % 8 {
        0 => DifferentialFuzzCase::NanSum {
            series: project_numeric_series(payload, "s", rows, &swarm),
            swarm,
        },
        1 => DifferentialFuzzCase::NanMean {
            series: project_numeric_series(payload, "s", rows, &swarm),
            swarm,
        },
        2 => {
            if (op_selector & 0b1000) == 0 {
                DifferentialFuzzCase::NanMin {
                    series: project_numeric_series(payload, "s", rows, &swarm),
                    swarm,
                }
            } else {
                DifferentialFuzzCase::NanMax {
                    series: project_numeric_series(payload, "s", rows, &swarm),
                    swarm,
                }
            }
        }
        3 => {
            let mid = payload.len() / 2;
            let left = project_numeric_series(
                &payload[..mid.max(1).min(payload.len())],
                "s",
                rows,
                &swarm,
            );
            let right_rows = usize::from(byte_at(payload, 3) % 5) + 1;
            let right = project_numeric_series(
                payload.get(mid..).unwrap_or(payload),
                "s",
                right_rows,
                &swarm,
            );
            DifferentialFuzzCase::SeriesAdd { swarm, left, right }
        }
        4 => {
            let mut key_values = Vec::with_capacity(rows);
            for r in 0..rows {
                let b = byte_at(payload, r + 1);
                key_values.push(Scalar::Utf8(format!("g{}", b % 3)));
            }
            let values =
                project_numeric_series(payload.get(1..).unwrap_or(payload), "val", rows, &swarm);
            let keys = FixtureSeries {
                name: "key".to_owned(),
                index: values.index.clone(),
                values: key_values,
            };
            DifferentialFuzzCase::GroupBySum {
                swarm,
                keys,
                values,
            }
        }
        _ => {
            let chain_len = usize::from((op_selector >> 3) % 4) + 1;
            let (frame, steps) = project_dataframe_and_chain(payload, &swarm, chain_len);
            DifferentialFuzzCase::DataFrameOpChain {
                swarm,
                frame,
                steps,
            }
        }
    }
}

fn compare_series_with_mode(
    actual: &Series,
    expected: &FixtureExpectedSeries,
    mode: DifferentialComparatorMode,
) -> Result<(), String> {
    match mode {
        DifferentialComparatorMode::Strict => compare_series_expected(actual, expected),
        DifferentialComparatorMode::IgnoreIndexOrder => {
            if actual.values().len() != expected.values.len() {
                return Err(format!(
                    "value length mismatch: actual={}, expected={}",
                    actual.values().len(),
                    expected.values.len()
                ));
            }
            let mut actual_labels = actual.index().labels().to_vec();
            let mut expected_labels = expected.index.clone();
            actual_labels.sort();
            expected_labels.sort();
            if actual_labels != expected_labels {
                return Err(format!(
                    "index multiset mismatch: actual={actual_labels:?}, expected={expected_labels:?}"
                ));
            }
            for (idx, (left, right)) in actual
                .values()
                .iter()
                .zip(expected.values.iter())
                .enumerate()
            {
                if !left.semantic_eq(right) {
                    return Err(format!(
                        "value mismatch at idx={idx}: actual={left:?}, expected={right:?}"
                    ));
                }
            }
            Ok(())
        }
    }
}

fn compare_dataframe_with_mode(
    actual: &DataFrame,
    expected: &FixtureExpectedDataFrame,
    mode: DifferentialComparatorMode,
) -> Result<(), String> {
    match mode {
        DifferentialComparatorMode::Strict => compare_dataframe_expected(actual, expected),
        DifferentialComparatorMode::IgnoreIndexOrder => {
            let mut actual_labels = actual.index().labels().to_vec();
            let mut expected_labels = expected.index.clone();
            actual_labels.sort();
            expected_labels.sort();
            if actual_labels != expected_labels {
                return Err(format!(
                    "dataframe index multiset mismatch: actual={actual_labels:?}, expected={expected_labels:?}"
                ));
            }
            for (name, expected_values) in &expected.columns {
                let Some(column) = actual.column(name) else {
                    return Err(format!("dataframe column missing in actual: {name}"));
                };
                if column.values().len() != expected_values.len() {
                    return Err(format!(
                        "dataframe column '{name}' length mismatch: actual={}, expected={}",
                        column.values().len(),
                        expected_values.len()
                    ));
                }
            }
            Ok(())
        }
    }
}

fn apply_fault_injection_series(
    series: Series,
    fault: DifferentialFaultInjection,
) -> Result<Series, String> {
    match fault {
        DifferentialFaultInjection::None => Ok(series),
        DifferentialFaultInjection::ReverseIndexOrder => {
            let mut rev_labels = series.index().labels().to_vec();
            rev_labels.reverse();
            Series::new(
                series.name().to_owned(),
                fp_index::Index::new(rev_labels),
                series.column().clone(),
            )
            .map_err(|e| e.to_string())
        }
    }
}

fn apply_fault_injection_dataframe(
    df: DataFrame,
    fault: DifferentialFaultInjection,
) -> Result<DataFrame, String> {
    match fault {
        DifferentialFaultInjection::None => Ok(df),
        DifferentialFaultInjection::ReverseIndexOrder => {
            let mut rev_labels = df.index().labels().to_vec();
            rev_labels.reverse();
            let mut cols = BTreeMap::new();
            let order = df
                .column_names()
                .into_iter()
                .map(ToOwned::to_owned)
                .collect::<Vec<_>>();
            for name in &order {
                if let Some(col) = df.column(name) {
                    cols.insert(name.clone(), col.clone());
                }
            }
            let out =
                DataFrame::new_with_column_order(fp_index::Index::new(rev_labels), cols, order)
                    .map_err(|e| e.to_string())?;
            if let Some(multi) = df.row_multiindex() {
                let rev_pos = (0..multi.len()).rev().collect::<Vec<_>>();
                let rev_multi = multi.take(&rev_pos).map_err(|e| e.to_string())?;
                out.with_row_multiindex(rev_multi)
                    .map_err(|e| e.to_string())
            } else {
                Ok(out)
            }
        }
    }
}

/// Execute a [`DifferentialFuzzCase`] in FrankenPandas and compare against the live pandas 2.2.3 oracle.
///
/// Returns `Ok(true)` when compared against the live oracle and matched,
/// `Ok(false)` when the live oracle is unavailable and `!config.require_live_oracle`,
/// or `Err(mismatch)` on divergence.
pub fn execute_differential_fuzz_case(
    config: &HarnessConfig,
    case: &DifferentialFuzzCase,
    comparator_mode: DifferentialComparatorMode,
    fault: DifferentialFaultInjection,
) -> Result<bool, String> {
    match case {
        DifferentialFuzzCase::NanSum { series, .. }
        | DifferentialFuzzCase::NanMean { series, .. }
        | DifferentialFuzzCase::NanMin { series, .. }
        | DifferentialFuzzCase::NanMax { series, .. } => {
            let fp_series = build_series(series)?;
            let actual = if matches!(case, DifferentialFuzzCase::NanSum { .. }) {
                nansum(fp_series.values())
            } else if matches!(case, DifferentialFuzzCase::NanMean { .. }) {
                nanmean(fp_series.values())
            } else if matches!(case, DifferentialFuzzCase::NanMin { .. }) {
                match nanmin(fp_series.values()) {
                    Scalar::Int64(i) => Scalar::Float64(i as f64),
                    other => other,
                }
            } else {
                match nanmax(fp_series.values()) {
                    Scalar::Int64(i) => Scalar::Float64(i as f64),
                    other => other,
                }
            };

            let req = serde_json::json!({
                "mode": "dispatch",
                "payload": {
                    "operation": case.op_name(),
                    "left": series,
                }
            });
            let resp = match query_oracle_worker(config, &req) {
                Ok(resp) => resp,
                Err(err)
                    if err.starts_with("oracle_unavailable:") && !config.require_live_oracle =>
                {
                    return Ok(false);
                }
                Err(err) => return Err(err),
            };
            if let Some(err) = resp.error {
                return Err(format!("oracle error for {}: {err}", case.op_name()));
            }
            let expected = resp
                .expected_scalar
                .ok_or_else(|| format!("oracle omitted expected_scalar for {}", case.op_name()))?;
            compare_scalar(&actual, &expected, case.op_name())?;
            Ok(true)
        }
        DifferentialFuzzCase::SeriesAdd { left, right, .. } => {
            let left_s = build_series(left)?;
            let right_s = build_series(right)?;
            let policy = RuntimePolicy::strict();
            let mut ledger = EvidenceLedger::new();
            let actual = left_s
                .add_with_policy(&right_s, &policy, &mut ledger)
                .map_err(|e| e.to_string())?;
            let actual = apply_fault_injection_series(actual, fault)?;

            let req = serde_json::json!({
                "mode": "dispatch",
                "payload": {
                    "operation": "series_add",
                    "left": left,
                    "right": right,
                }
            });
            let resp = match query_oracle_worker(config, &req) {
                Ok(resp) => resp,
                Err(err)
                    if err.starts_with("oracle_unavailable:") && !config.require_live_oracle =>
                {
                    return Ok(false);
                }
                Err(err) => return Err(err),
            };
            if let Some(err) = resp.error {
                return Err(format!("oracle error for series_add: {err}"));
            }
            let expected = resp
                .expected_series
                .ok_or_else(|| "oracle omitted expected_series for series_add".to_owned())?;
            compare_series_with_mode(&actual, &expected, comparator_mode)?;
            Ok(true)
        }
        DifferentialFuzzCase::GroupBySum { keys, values, .. } => {
            let keys_s = build_series(keys)?;
            let vals_s = build_series(values)?;
            let policy = RuntimePolicy::strict();
            let mut ledger = EvidenceLedger::new();
            let actual = groupby_sum(
                &keys_s,
                &vals_s,
                GroupByOptions::default(),
                &policy,
                &mut ledger,
            )
            .map_err(|e| e.to_string())?;
            let actual = apply_fault_injection_series(actual, fault)?;

            let req = serde_json::json!({
                "mode": "dispatch",
                "payload": {
                    "operation": "groupby_sum",
                    "left": keys,
                    "right": values,
                }
            });
            let resp = match query_oracle_worker(config, &req) {
                Ok(resp) => resp,
                Err(err)
                    if err.starts_with("oracle_unavailable:") && !config.require_live_oracle =>
                {
                    return Ok(false);
                }
                Err(err) => return Err(err),
            };
            if let Some(err) = resp.error {
                return Err(format!("oracle error for groupby_sum: {err}"));
            }
            let expected = resp
                .expected_series
                .ok_or_else(|| "oracle omitted expected_series for groupby_sum".to_owned())?;
            compare_series_with_mode(&actual, &expected, comparator_mode)?;
            Ok(true)
        }
        DifferentialFuzzCase::DataFrameOpChain { frame, steps, .. } => {
            let mut df = build_dataframe(frame)?;
            for step in steps {
                df = match step {
                    DifferentialChainStep::SortIndex { ascending } => {
                        df.sort_index(*ascending).map_err(|e| e.to_string())?
                    }
                    DifferentialChainStep::SortValues { column, ascending } => {
                        if df.column(column).is_some() {
                            df.sort_values(column, *ascending)
                                .map_err(|e| e.to_string())?
                        } else {
                            df
                        }
                    }
                    DifferentialChainStep::DropNa => df.dropna().map_err(|e| e.to_string())?,
                    DifferentialChainStep::FillNaZero => {
                        df.fillna(&Scalar::Int64(0)).map_err(|e| e.to_string())?
                    }
                    DifferentialChainStep::ResetIndexDrop => {
                        df.reset_index(true).map_err(|e| e.to_string())?
                    }
                    DifferentialChainStep::SelectColumns { columns } => {
                        let valid = columns
                            .iter()
                            .filter(|c| df.column(c).is_some())
                            .map(String::as_str)
                            .collect::<Vec<_>>();
                        if valid.is_empty() {
                            df
                        } else {
                            df.select_columns(&valid).map_err(|e| e.to_string())?
                        }
                    }
                    DifferentialChainStep::Head { n } => df
                        .head(i64::try_from(*n).unwrap_or(i64::MAX))
                        .map_err(|e| e.to_string())?,
                    DifferentialChainStep::Tail { n } => df
                        .tail(i64::try_from(*n).unwrap_or(i64::MAX))
                        .map_err(|e| e.to_string())?,
                };
            }
            let actual = apply_fault_injection_dataframe(df, fault)?;

            let req = serde_json::json!({
                "mode": "op_chain",
                "frame": frame,
                "steps": steps,
            });
            let resp = match query_oracle_worker(config, &req) {
                Ok(resp) => resp,
                Err(err)
                    if err.starts_with("oracle_unavailable:") && !config.require_live_oracle =>
                {
                    return Ok(false);
                }
                Err(err) => return Err(err),
            };
            if let Some(err) = resp.error {
                return Err(format!("oracle error for dataframe_op_chain: {err}"));
            }
            let expected = resp
                .expected_frame
                .ok_or_else(|| "oracle omitted expected_frame for dataframe_op_chain".to_owned())?;
            compare_dataframe_with_mode(&actual, &expected, comparator_mode)?;
            Ok(true)
        }
    }
}

/// Deterministic shrinker for differential fuzzing seeds.
pub fn shrink_differential_fuzz_input(
    seed: &[u8],
    mut is_failing: impl FnMut(&[u8]) -> bool,
) -> Vec<u8> {
    let mut best = seed.to_vec();
    // Pass 1: truncate tail bytes down to minimum 3 bytes
    while best.len() > 3 {
        let candidate_len = (best.len() - 1).max(3);
        let candidate = best[..candidate_len].to_vec();
        if is_failing(&candidate) {
            best = candidate;
        } else {
            break;
        }
    }
    // Pass 2: reduce chain length bits in byte 1 if possible
    if best.len() >= 2 {
        let op_byte = best[1];
        for chain_bits in 0..(op_byte >> 3) % 4 {
            let mut candidate = best.clone();
            candidate[1] = (op_byte & 0b0000_0111) | (chain_bits << 3);
            if is_failing(&candidate) {
                best = candidate;
                break;
            }
        }
    }
    // Pass 3: simplify individual payload bytes toward 0
    for idx in 2..best.len() {
        if best[idx] != 0 {
            let mut candidate = best.clone();
            candidate[idx] = 0;
            if is_failing(&candidate) {
                best = candidate;
            }
        }
    }
    best
}

fn hex_encode(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        use std::fmt::Write as _;
        let _ = write!(&mut out, "{b:02x}");
    }
    out
}

/// Differential fuzz entrypoint against the live pandas 2.2.3 oracle (`DIFFERENTIAL_FUZZ_DESIGN.md`).
pub fn fuzz_differential_vs_pandas_bytes(input: &[u8]) -> Result<(), String> {
    if input.is_empty() || input.len() > 4096 {
        return Ok(());
    }
    let config = HarnessConfig::default_paths();
    let case = project_differential_fuzz_case(input);
    execute_differential_fuzz_case(
        &config,
        &case,
        DifferentialComparatorMode::Strict,
        DifferentialFaultInjection::None,
    )
    .map(|_| ())
}

/// Run a bounded differential fuzzing campaign over deterministic swarm-varied seeds.
pub fn run_differential_fuzz_campaign(
    config: &HarnessConfig,
    num_seeds: usize,
    repro_dir: Option<&Path>,
) -> Result<DifferentialFuzzCampaignReport, String> {
    let mut op_counts = BTreeMap::new();
    let mut swarm_subset_counts = BTreeMap::new();
    let mut executed_vs_oracle = 0usize;
    let mut skipped_no_oracle = 0usize;
    let mut repros = Vec::new();

    for seed_idx in 0..num_seeds {
        // Generate deterministic 24-byte seed covering swarm configurations and op sequences
        let mut seed = Vec::with_capacity(24);
        let mut state = (seed_idx as u64)
            .wrapping_mul(0x9E37_79B9_7F4A_7C15)
            .wrapping_add(0xBF58_476D_1CE4_E5B9);
        for i in 0..24_u64 {
            state ^= state >> 12;
            state ^= state << 25;
            state ^= state >> 27;
            let mixed = state.wrapping_mul(0x2545_F491_4F6C_DD1D);
            seed.push(((mixed >> ((i % 8) * 8)) & 0xFF) as u8);
        }
        // Ensure every op tag 0..8 and swarm combination is evenly sampled
        seed[0] = (seed_idx & 0xFF) as u8;
        seed[1] = (((seed_idx / 8) % 4) as u8) << 3 | ((seed_idx % 8) as u8);

        let case = project_differential_fuzz_case(&seed);
        *op_counts.entry(case.op_name().to_owned()).or_insert(0) += 1;
        *swarm_subset_counts
            .entry(case.swarm().subset_tag())
            .or_insert(0) += 1;

        match execute_differential_fuzz_case(
            config,
            &case,
            DifferentialComparatorMode::Strict,
            DifferentialFaultInjection::None,
        ) {
            Ok(true) => executed_vs_oracle += 1,
            Ok(false) => skipped_no_oracle += 1,
            Err(mismatch) => {
                executed_vs_oracle += 1;
                let minimized = shrink_differential_fuzz_input(&seed, |candidate| {
                    let cand_case = project_differential_fuzz_case(candidate);
                    execute_differential_fuzz_case(
                        config,
                        &cand_case,
                        DifferentialComparatorMode::Strict,
                        DifferentialFaultInjection::None,
                    )
                    .is_err()
                });
                let min_case = project_differential_fuzz_case(&minimized);
                let min_mismatch = execute_differential_fuzz_case(
                    config,
                    &min_case,
                    DifferentialComparatorMode::Strict,
                    DifferentialFaultInjection::None,
                )
                .err()
                .unwrap_or(mismatch);
                let packet = DifferentialReproPacket {
                    seed_hex: hex_encode(&seed),
                    minimized_seed_hex: hex_encode(&minimized),
                    case: min_case,
                    mismatch: min_mismatch,
                };
                if let Some(dir) = repro_dir {
                    let _ = packet.write_to_dir(dir);
                }
                repros.push(packet);
            }
        }
    }

    Ok(DifferentialFuzzCampaignReport {
        total_seeds: num_seeds,
        executed_vs_oracle,
        skipped_no_oracle,
        divergences_found: repros.len(),
        op_counts,
        swarm_subset_counts,
        repros,
    })
}

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

    #[test]
    fn differential_fuzz_self_test_catches_injected_index_order_bug() {
        let mut config = HarnessConfig::default_paths();
        config.allow_system_pandas_fallback = true;
        config.require_live_oracle = true;

        // Construct a 32-byte seed that maps to DataFrameOpChain (op_selector % 8 == 5)
        // with chain_len = 2 (`SortIndex`), 4 rows, and distinct row labels so reversing
        // index order is observable even after shrinking down to 3 bytes.
        let seed: Vec<u8> = vec![
            0b0111_1100,  // swarm: multiindex + float64 + bool + utf8, distinct labels, no nulls
            (1 << 3) | 5, // chain_len = 2, op = 5 (DataFrameOpChain)
            3,            // rows = 4
            2,            // 3 columns
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            11,
            22,
            33,
            44,
        ];
        let case = project_differential_fuzz_case(&seed);
        assert_eq!(case.op_name(), "dataframe_op_chain");

        // 1. Baseline without fault injection must match live oracle under Strict comparator
        let baseline = execute_differential_fuzz_case(
            &config,
            &case,
            DifferentialComparatorMode::Strict,
            DifferentialFaultInjection::None,
        )
        .expect("baseline execution against live oracle must succeed");
        assert!(baseline, "live oracle must be active");

        // 2. Weak comparator (IgnoreIndexOrder) misses the injected ReverseIndexOrder bug
        let weak_result = execute_differential_fuzz_case(
            &config,
            &case,
            DifferentialComparatorMode::IgnoreIndexOrder,
            DifferentialFaultInjection::ReverseIndexOrder,
        );
        assert!(
            weak_result.is_ok(),
            "weak comparator (IgnoreIndexOrder) should miss index order reversal: {weak_result:?}"
        );

        // 3. Strict comparator catches the injected ReverseIndexOrder bug
        let strict_err = execute_differential_fuzz_case(
            &config,
            &case,
            DifferentialComparatorMode::Strict,
            DifferentialFaultInjection::ReverseIndexOrder,
        )
        .expect_err("Strict comparator must catch injected ReverseIndexOrder bug");
        assert!(
            strict_err.contains("index mismatch"),
            "expected index mismatch diagnostic, got: {strict_err}"
        );

        // 4. Deterministic shrinker minimizes the failing seed while preserving the failure
        let minimized = shrink_differential_fuzz_input(&seed, |candidate| {
            let cand_case = project_differential_fuzz_case(candidate);
            execute_differential_fuzz_case(
                &config,
                &cand_case,
                DifferentialComparatorMode::Strict,
                DifferentialFaultInjection::ReverseIndexOrder,
            )
            .is_err()
        });
        assert!(
            minimized.len() < seed.len(),
            "shrinker must reduce seed length ({} -> {})",
            seed.len(),
            minimized.len()
        );

        let min_case = project_differential_fuzz_case(&minimized);
        let min_mismatch = execute_differential_fuzz_case(
            &config,
            &min_case,
            DifferentialComparatorMode::Strict,
            DifferentialFaultInjection::ReverseIndexOrder,
        )
        .expect_err("minimized seed must still fail under Strict comparator");

        // 5. Emit repro packet to scratch temp dir (never touching committed fixtures/)
        let packet = DifferentialReproPacket {
            seed_hex: hex_encode(&seed),
            minimized_seed_hex: hex_encode(&minimized),
            case: min_case,
            mismatch: min_mismatch,
        };
        let repro_dir =
            std::env::temp_dir().join(format!("fp_diff_fuzz_self_test_{}", std::process::id()));
        let path = packet
            .write_to_dir(&repro_dir)
            .expect("writing repro packet must succeed");
        let raw = fs::read_to_string(&path).expect("read repro packet");
        let round_tripped: DifferentialReproPacket =
            serde_json::from_str(&raw).expect("deserialize repro packet");
        assert_eq!(round_tripped, packet);
    }

    #[test]
    fn differential_fuzz_campaign_matches_live_pandas_oracle_j0lg5_6() {
        let mut config = HarnessConfig::default_paths();
        config.allow_system_pandas_fallback = true;
        config.require_live_oracle = true;

        let report = run_differential_fuzz_campaign(&config, 256, None)
            .expect("differential fuzz campaign must succeed");
        println!(
            "differential_fuzz_campaign_report: total={} executed={} divergences={} ops={:?} swarms={}",
            report.total_seeds,
            report.executed_vs_oracle,
            report.divergences_found,
            report.op_counts,
            report.swarm_subset_counts.len()
        );
        assert_eq!(report.executed_vs_oracle, 256);
        assert_eq!(
            report.divergences_found, 0,
            "unexpected divergences vs live pandas 2.2.3: {:?}",
            report.repros
        );
        for expected_op in [
            "nan_sum",
            "nan_mean",
            "nan_min",
            "nan_max",
            "series_add",
            "groupby_sum",
            "dataframe_op_chain",
        ] {
            assert!(
                report.op_counts.get(expected_op).copied().unwrap_or(0) > 0,
                "campaign must exercise op family {expected_op}"
            );
        }
        assert!(
            report.swarm_subset_counts.len() >= 16,
            "campaign must sample diverse swarm configurations"
        );
    }
}