noprop 0.0.4

Imperative property-based testing with no dependencies and no macro DSL
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
//! Property-based test runner.

use std::panic::AssertUnwindSafe;

use crate::error::SearchPolicy;
use crate::rng::{
    ChoiceMeta, ChoiceSequence, Feature, FeedbackState, RejectionState, XoshiroState,
    is_iteration_rejected,
};
use crate::{RunError, RunResult, TestCaseContext, TestResult};

/// Maximum number of candidates kept in the semantic corpus.
///
/// All four search constants (`CORPUS_SIZE`, `MUTATION_DENOM`,
/// `RANDOM_RESTART_DENOM`, `REJECTED_PICK_DENOM`) are initial guesses;
/// their tuning is deferred until benchmark data exists.
const CORPUS_SIZE: usize = 64;

/// Denominator of the per-draw mutation probability: one in
/// `MUTATION_DENOM` draws of a selected candidate are rewritten.
const MUTATION_DENOM: u64 = 4;

/// Denominator of the random-restart probability: one in
/// `RANDOM_RESTART_DENOM` candidates are freshly generated instead of
/// mutated from the corpus.
const RANDOM_RESTART_DENOM: u64 = 8;

/// Denominator of the rejected-entry pick probability: one in
/// `REJECTED_PICK_DENOM` corpus picks target the rejected queue, which
/// is kept with lower energy than accepted entries (a scaffolding
/// toward sparse preconditions, not the main search path).
const REJECTED_PICK_DENOM: u64 = 8;

/// Maximum number of semantic features observed across a whole
/// feedback-guided run. After the cap is reached, new features are not
/// registered and never make a case interesting, so a high-cardinality
/// property cannot grow the registry without bound.
const MAX_GLOBAL_FEATURES: usize = 1024;

/// Observability data from a [`Runner::run`](Runner::run) or
/// [`Runner::run_feedback_guided`](Runner::run_feedback_guided)
/// invocation.
///
/// Read from a [`Runner`] after the run returns via
/// [`Runner::stats`](Runner::stats), and also embedded in
/// [`RunError`](crate::RunError) on
/// failure so the caller can see how far the run progressed before it
/// failed. All counters are cumulative over the whole run (across
/// every case, accepted or rejected). The corpus fields
/// ([`Stats::discovered_features`](Stats::discovered_features) and
/// [`Stats::max_corpus_size`](Stats::max_corpus_size)) are only
/// meaningful for feedback-guided runs and are 0 otherwise.
///
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Stats {
    /// Number of cases whose closure completed without calling
    /// [`TestCaseContext::reject_case`](crate::TestCaseContext::reject_case). On
    /// a successful run, this equals
    /// `cases`. On failure, it is
    /// the number of cases that passed before the failing one
    /// (equivalent to [`RunError::case_index`](RunError::case_index)).
    pub accepted_cases: usize,
    /// Total number of cases discarded via
    /// [`TestCaseContext::reject_case`](crate::TestCaseContext::reject_case), including
    /// exhausted [`sample_with_rejection`](crate::sample_with_rejection)
    /// helpers (they discard via `reject_case` internally, so the two
    /// origins share this single counter), and — under
    /// [`Runner::run_feedback_guided`](Runner::run_feedback_guided) — the
    /// exploratory-replay draw cap discards.
    pub rejected_cases: usize,
    /// Total number of top-level `sample_*` invocations across every
    /// case that ran. Counted per call to the primitive generator
    /// (`sample_u32`, `sample_choice`, `sample_string`, …), not per
    /// underlying byte read or dedup entry — a `sample_char` invocation
    /// that internally retries its 21-bit mask still counts as one
    /// sample. Includes samples produced by rejected cases, since
    /// those cases still consumed generator budget.
    pub total_samples: usize,
    /// Number of distinct semantic features registered in the global
    /// observation set during a feedback-guided run, capped at 1024
    /// (`MAX_GLOBAL_FEATURES`). Features registered by rejected cases
    /// are included; features of the failing case itself are not (a
    /// case is registered only after its verdict, and the failing case
    /// never reaches admission).
    pub discovered_features: usize,
    /// Combined size of the semantic corpus (accepted + rejected) at
    /// the end of a feedback-guided run. The combined size only grows
    /// and is trimmed back to the corpus cap (64) when full, so the
    /// value at the end of the run equals the maximum; the transient
    /// overshoot just before eviction is not counted (admission
    /// pushes, then trims only when over the cap).
    pub max_corpus_size: usize,
}

/// A property-based test runner.
///
/// Construct it with [`Runner::new`] and call [`run`](Runner::run):
///
/// ```
/// let _: noprop::RunResult = noprop::Runner::new(0xDEAD_BEEF).run(16, |ctx| {
///     let x = noprop::sample_u32(ctx);
///     assert_eq!(x, x);
///     Ok(())
/// });
/// ```
///
/// A constructor is used (instead of struct-literal construction) so
/// that runner-wide configuration (default rejection budget, feedback
/// mode, snapshot directory, …) can be added later without breaking
/// existing call sites. Observability data ([`Stats`]) is exposed via
/// [`Runner::stats`] after `run` returns.
///
/// # Configuring the seed
///
/// [`Runner::new`] takes `seed` as a required argument and
/// does not prescribe how to obtain it. A common setup reads it
/// from a project-specific environment variable so that failures are
/// reproducible from a failure report (via the seed). Use
/// [`seed_from_env_or_time`](crate::seed_from_env_or_time) for the
/// standard lookup:
///
/// ```
/// # fn body() -> noprop::TestResult {
/// let seed = noprop::seed_from_env_or_time("MYAPP_SEED")?;
/// noprop::Runner::new(seed).run(256, |_ctx| {
///     // property
///     Ok(())
/// })?;
/// Ok(())
/// }
/// # body().unwrap();
/// ```
///
/// The env var name shown above is a project-specific placeholder;
/// pick a name that fits the calling project. The helper accepts
/// decimal values as well as `0x` / `0b` / `0o` prefixed values with
/// optional `_` separators, so the hex seed printed by a failure
/// report can be pasted into the environment variable directly. The
/// helper surfaces a
/// boxed error — via `?` — when the variable
/// is set to something that cannot be parsed, so a mistyped
/// `MYAPP_SEED=hello` fails loudly instead of silently reverting to the
/// clock-derived fallback.
///
/// # Failing a case via `Err` or panic
///
/// The property closure signals success by returning `Ok(())`. A
/// failure can be signalled either by returning `Err` or by panicking
/// (typically via `assert!` / `assert_eq!`); both are captured into the
/// resulting [`RunError`](crate::RunError) uniformly.
///
/// The `Err` variant is `Box<dyn std::error::Error>`, so the `?`
/// operator works for any error type that implements [`Error`]:
///
/// ```
/// let _: noprop::RunResult = noprop::Runner::new(0).run(1, |_ctx| {
///     let _n: u32 = "42".parse()?;   // ParseIntError -> Box<dyn Error>
///     Ok(())
/// });
/// ```
///
/// Ad-hoc messages work via `Into`:
///
/// ```
/// let _: noprop::RunResult = noprop::Runner::new(0).run(1, |_ctx| {
///     if false { return Err("something bad".into()); }
///     Ok(())
/// });
/// ```
///
/// [`Error`]: std::error::Error
pub struct Runner {
    seed: u64,
    stats: Stats,
}

/// Global rejection limit for a single [`Runner::run`](Runner::run) or
/// [`Runner::run_feedback_guided`](Runner::run_feedback_guided)
/// invocation.
///
/// Total rejected cases (across all case indices) are capped
/// so that a generator which always calls
/// [`TestCaseContext::reject_case`](crate::TestCaseContext::reject_case) still terminates in
/// finite time with a `TooManyRejections` failure.
///
/// Scaled with `cases` so that a generous case budget also
/// gets a generous rejection budget, with a floor for very small
/// `cases` (including `0`). The concrete formula and floor are
/// deliberately kept crate-private; both are subject to change once
/// real-world usage produces measurement data.
fn rejection_limit(cases: usize) -> usize {
    const FLOOR: usize = 1024;
    FLOOR.max(cases.saturating_mul(10))
}

impl Runner {
    /// Construct a runner that invokes the property closure against a
    /// [`TestCaseContext`] seeded with `seed`.
    ///
    /// The number of *accepted* cases to invoke the closure for is
    /// given per run, via [`run`](Runner::run) / [`run_feedback_guided`](Runner::run_feedback_guided).
    ///
    /// A case is "accepted" when the closure reaches a verdict
    /// (`Ok(())` / `Err` / panic) without calling
    /// [`TestCaseContext::reject_case`](crate::TestCaseContext::reject_case)
    /// (directly or via
    /// [`sample_with_rejection`](crate::sample_with_rejection)). Rejected
    /// cases are retried and are *not* counted toward the budget.
    ///
    /// Rejected cases are still bounded — the runner enforces an
    /// internal global limit on the total number of rejections it will
    /// tolerate across the whole [`run`](Runner::run) invocation, so a
    /// generator that always rejects still terminates with a
    /// `TooManyRejections` failure instead of looping forever. The
    /// initial limit is a crate-private constant that scales with
    /// `cases`; there is no public knob for it yet.
    pub fn new(seed: u64) -> Self {
        Self {
            seed,
            stats: Stats::default(),
        }
    }

    /// Observability counters from the most recent
    /// [`run`](Runner::run) or
    /// [`run_feedback_guided`](Runner::run_feedback_guided)
    /// call on this runner. Returns [`Stats::default`] (all zeros)
    /// before a run has been invoked.
    pub fn stats(&self) -> Stats {
        self.stats
    }

    /// Feedback-guided search over semantic features reported via
    /// [`TestCaseContext::event`](crate::TestCaseContext::event) /
    /// [`bucket`](crate::TestCaseContext::bucket) /
    /// [`transition`](crate::TestCaseContext::transition).
    ///
    /// The property closure has the same shape as
    /// [`run`](Runner::run), so the same property can be exercised
    /// under both entry points. Feedback is not mandatory: an accepted
    /// case that reports no semantic feature is simply not interesting
    /// (it never enters the corpus), and no missing / invalid feedback
    /// error is raised.
    ///
    /// Candidates are produced from a bounded corpus of interesting
    /// cases: a case that registers at least one globally unobserved
    /// feature is admitted, mutated within its recorded constraints,
    /// and replayed with exploratory generation for draws the mutation
    /// introduces. Accepted and rejected cases live in
    /// separate queues (the rejected queue is picked with probability
    /// `1 / REJECTED_PICK_DENOM`); with probability
    /// `1 / RANDOM_RESTART_DENOM` a candidate is freshly generated
    /// instead, so the search can escape local optima.
    ///
    /// The rejection semantics (global rejection cap, `Stats`, and the
    /// `cases` budget counting only accepted cases) match
    /// [`run`](Runner::run).
    ///
    /// # Example
    ///
    /// ```
    /// let mut runner = noprop::Runner::new(0xDEAD_BEEF);
    /// runner
    ///     .run_feedback_guided(16, |ctx| {
    ///         let x = noprop::sample_u32(ctx);
    ///         if x == 0 {
    ///             ctx.event("zero");
    ///         }
    ///         Ok(())
    ///     })
    ///     .expect("feedback-guided run must succeed");
    /// ```
    pub fn run_feedback_guided<F>(&mut self, cases: usize, f: F) -> RunResult
    where
        F: Fn(&mut TestCaseContext) -> TestResult,
    {
        self.stats = Stats::default();
        let mut search = CorpusGuidedSearch::new(self.seed);
        let rejection_cap = rejection_limit(cases);
        let mut accepted: usize = 0;
        let mut rejected: usize = 0;
        let mut total_samples: usize = 0;

        while accepted < cases {
            // Each iteration gets a fresh context (recording or
            // exploratory), so there is no per-case state to clear.
            let mut ctx = search.next_context();
            ctx.set_inside_runner();
            ctx.enable_corpus_guided();
            let verdict = run_case(&f, &mut ctx);
            total_samples = total_samples.saturating_add(ctx.total_samples());
            match verdict {
                CaseVerdict::Rejected(state) => {
                    rejected += 1;
                    if rejected > rejection_cap {
                        // Cap exceeded: report before consuming the
                        // feedback, so the error can carry the last
                        // rejected case's semantic features and
                        // candidate index (drained inside
                        // `too_many_rejections`).
                        record_corpus_stats(self, &search, accepted, rejected, total_samples);
                        return Err(too_many_rejections(
                            self,
                            &mut ctx,
                            state.location,
                            SearchPolicy::FeedbackGuided,
                            cases,
                        ));
                    }
                    // A rejected case may still register novel features
                    // and enter the rejected queue as scaffolding toward
                    // sparse preconditions.
                    let feedback = ctx.take_feedback();
                    if let FeedbackState::SemanticCoverage(mut cov) = feedback {
                        let features = cov.take_features();
                        if let Some(sequence) = ctx.take_sequence() {
                            search.corpus.admit_rejected(sequence, features);
                        }
                    }
                    continue;
                }
                CaseVerdict::Completed(CaseOutcome::Passed) => {
                    // Accepted case: the reported features decide
                    // interest.
                    let feedback = ctx.take_feedback();
                    let mut cov = match feedback {
                        FeedbackState::SemanticCoverage(cov) => cov,
                        _ => unreachable!(
                            "run_feedback_guided enables feedback-guided mode before each case"
                        ),
                    };
                    let features = cov.take_features();
                    if let Some(sequence) = ctx.take_sequence() {
                        search.corpus.admit_accepted(sequence, features);
                    }
                    accepted += 1;
                    continue;
                }
                CaseVerdict::Completed(CaseOutcome::Failed(message)) => {
                    record_corpus_stats(self, &search, accepted, rejected, total_samples);
                    let generated = ctx.take_generated();
                    let feedback = ctx.take_feedback();
                    let semantic_features = match feedback {
                        FeedbackState::SemanticCoverage(mut cov) => cov.take_features(),
                        _ => unreachable!(
                            "run_feedback_guided enables feedback-guided mode before each case"
                        ),
                    };
                    return Err(RunError::from_panic(
                        self.seed,
                        cases,
                        message,
                        generated,
                        self.stats,
                        SearchPolicy::FeedbackGuided,
                    )
                    .with_semantic(
                        semantic_features,
                        // The failing case is the current attempt: it
                        // is neither accepted nor rejected, so its
                        // candidate index is one past the completed
                        // attempts.
                        accepted + rejected + 1,
                    ));
                }
            }
        }
        record_corpus_stats(self, &search, accepted, rejected, total_samples);
        Ok(())
    }

    /// Invoke `f(&mut ctx)` up to `cases` times against a shared
    /// [`TestCaseContext`] seeded with `seed`.
    ///
    /// Each invocation is one property case. A returned `Ok(())`
    /// counts as a pass; a returned `Err` or a panic (via `assert!`,
    /// `assert_eq!`, or explicit `panic!`) counts as a failure. Panics
    /// are caught by `catch_unwind`. Either failure mode is wrapped in
    /// a [`RunError`](crate::RunError) carrying the seed, the failing
    /// case's index,
    /// the failure message, and the generated-value trace, and returned
    /// as `Err`. Subsequent cases past the first failure are
    /// skipped.
    ///
    /// A call to [`TestCaseContext::reject_case`](crate::TestCaseContext::reject_case) (either
    /// directly or via
    /// [`sample_with_rejection`](crate::sample_with_rejection)
    /// exhaustion) discards the current case, does not count it
    /// toward `cases`, and retries. A stored rejection state
    /// wins over the closure's own `Ok` / `Err` / non-marker panic
    /// outcome, so user code cannot swallow rejection by catching the
    /// private control-flow marker and returning normally. Total
    /// rejections are bounded — see
    /// `cases`.
    ///
    /// # Property purity
    ///
    /// The closure is bound as `Fn`, not `FnMut`, so it cannot capture
    /// enclosing variables by mutable reference. Property tests are
    /// meant to be pure functions of the `TestCaseContext`-derived input: keeping
    /// mutation off the closure's captures makes each case
    /// independent and each failure reproducible from the seed alone.
    ///
    /// If a test genuinely needs shared state (a debug counter, a
    /// cache, a report sink), reach for interior mutability
    /// (`std::cell::Cell` / `std::cell::RefCell` / atomics) so the
    /// escape from purity is spelled out in the code rather than
    /// hidden behind an unassuming `let mut`.
    pub fn run<F>(&mut self, cases: usize, f: F) -> RunResult
    where
        F: Fn(&mut TestCaseContext) -> TestResult,
    {
        self.stats = Stats::default();
        let mut ctx = TestCaseContext::new(self.seed);
        ctx.set_inside_runner();
        let rejection_cap = rejection_limit(cases);
        let mut accepted: usize = 0;
        let mut rejected: usize = 0;

        while accepted < cases {
            ctx.clear_generated();
            match run_case(&f, &mut ctx) {
                CaseVerdict::Rejected(state) => {
                    rejected += 1;
                    if rejected > rejection_cap {
                        record_stats(self, accepted, rejected, ctx.total_samples(), 0, 0);
                        let generated = ctx.take_generated();
                        return Err(RunError::from_too_many_rejections(
                            self.seed,
                            cases,
                            state.location,
                            generated,
                            self.stats,
                            SearchPolicy::Uniform,
                        ));
                    }
                    continue;
                }
                CaseVerdict::Completed(CaseOutcome::Passed) => {
                    accepted += 1;
                    continue;
                }
                CaseVerdict::Completed(CaseOutcome::Failed(message)) => {
                    record_stats(self, accepted, rejected, ctx.total_samples(), 0, 0);
                    let generated = ctx.take_generated();
                    return Err(RunError::from_panic(
                        self.seed,
                        cases,
                        message,
                        generated,
                        self.stats,
                        SearchPolicy::Uniform,
                    ));
                }
            }
        }
        record_stats(self, accepted, rejected, ctx.total_samples(), 0, 0);
        Ok(())
    }
}

/// Human-oriented summary of the runner's seed and the most recent
/// run's observability counters, for embedding in assertion messages.
///
/// The exact string format is not part of the API contract; machine
/// checks should read [`Runner::stats`](Runner::stats) instead.
impl std::fmt::Display for Runner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "noprop::Runner {{ seed: {:#018x}, stats: {{ accepted: {}, rejected: {}, total_samples: {}, discovered_features: {}, max_corpus_size: {} }} }}",
            self.seed,
            self.stats.accepted_cases,
            self.stats.rejected_cases,
            self.stats.total_samples,
            self.stats.discovered_features,
            self.stats.max_corpus_size,
        )
    }
}

/// The closure's own verdict for one property case.
enum CaseOutcome {
    /// The closure returned `Ok(())` without rejecting.
    Passed,
    /// The closure returned `Err` or panicked; the run must fail with
    /// this message.
    Failed(String),
}

/// The runner-side verdict for one property case, after any stored
/// rejection has been resolved.
enum CaseVerdict {
    /// The iteration was rejected (`reject_case`, or a stray
    /// `IterationRejected` marker without stored state).
    Rejected(RejectionState),
    /// The closure finished without rejecting.
    Completed(CaseOutcome),
}

/// Invoke the property closure once and classify the verdict.
///
/// Shared by the uniform and feedback-guided entry points: the
/// `catch_unwind` boundary, the stored-rejection precedence, the
/// stray-marker guard, and panic-message extraction are identical
/// across modes.
///
/// A stored rejection wins over any closure outcome: a non-marker user
/// panic raised alongside a rejection is dropped, so user code can
/// neither swallow rejection nor escalate it into a property failure.
fn run_case<F>(f: &F, ctx: &mut TestCaseContext) -> CaseVerdict
where
    F: Fn(&mut TestCaseContext) -> TestResult,
{
    let outcome = std::panic::catch_unwind(AssertUnwindSafe(|| f(ctx)));
    if let Some(state) = ctx.take_rejection() {
        return CaseVerdict::Rejected(state);
    }
    let message = match outcome {
        Ok(Ok(())) => return CaseVerdict::Completed(CaseOutcome::Passed),
        Ok(Err(err)) => format!("{err}"),
        Err(panic) => {
            // Defensive: a stray IterationRejected marker without a
            // stored rejection state shouldn't happen because
            // `reject_case` (and the exploratory draw cap) always set
            // the state before resuming unwind. If it somehow does,
            // treat it as rejection rather than as a property failure
            // with an opaque payload.
            if is_iteration_rejected(&*panic) {
                let location = std::panic::Location::caller();
                return CaseVerdict::Rejected(RejectionState { location });
            }
            panic_message(panic)
        }
    };
    CaseVerdict::Completed(CaseOutcome::Failed(message))
}

/// Extract a human-readable message from a `catch_unwind` payload.
///
/// Panic payloads come in two common shapes (`&'static str` and `String`).
/// Anything else collapses to a placeholder — this is not a typical case,
/// but we don't want to swallow the failure entirely.
fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String {
    if let Some(s) = payload.downcast_ref::<&'static str>() {
        (*s).to_string()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "unknown panic payload".to_string()
    }
}

/// Record the run progress counters on the runner. Every exit path
/// (success, property failure, rejection cap, feedback failure)
/// reports the same counters.
fn record_stats(
    runner: &mut Runner,
    accepted: usize,
    rejected: usize,
    total_samples: usize,
    discovered_features: usize,
    max_corpus_size: usize,
) {
    runner.stats = Stats {
        accepted_cases: accepted,
        rejected_cases: rejected,
        total_samples,
        discovered_features,
        max_corpus_size,
    };
}

/// The corpus-derived stats fields: the size of the global feature
/// observation set and the combined (accepted + rejected) corpus size
/// at the current search state.
fn corpus_stats(search: &CorpusGuidedSearch) -> (usize, usize) {
    (
        search.corpus.observed.len(),
        search.corpus.accepted.len() + search.corpus.rejected.len(),
    )
}

/// Record the run progress counters including the corpus-derived
/// fields at the current search state.
fn record_corpus_stats(
    runner: &mut Runner,
    search: &CorpusGuidedSearch,
    accepted: usize,
    rejected: usize,
    total_samples: usize,
) {
    let (discovered_features, max_corpus_size) = corpus_stats(search);
    record_stats(
        runner,
        accepted,
        rejected,
        total_samples,
        discovered_features,
        max_corpus_size,
    );
}

/// Build the too-many-rejections exit error. The caller must have
/// recorded the run progress counters first (via `record_stats` or
/// `record_corpus_stats`); this helper reads them back from
/// `runner.stats`.
///
/// For feedback-guided runs, the error additionally carries the semantic
/// features of the last rejected case and its candidate index (the
/// ordinal of the attempt that exceeded the cap, derived from the
/// recorded stats: `accepted + rejected`).
fn too_many_rejections(
    runner: &mut Runner,
    ctx: &mut TestCaseContext,
    location: &'static std::panic::Location<'static>,
    policy: SearchPolicy,
    cases: usize,
) -> RunError {
    let generated = ctx.take_generated();
    let err = RunError::from_too_many_rejections(
        runner.seed,
        cases,
        location,
        generated,
        runner.stats,
        policy,
    );
    if matches!(policy, SearchPolicy::FeedbackGuided) {
        let features = match ctx.take_feedback() {
            FeedbackState::SemanticCoverage(mut cov) => cov.take_features(),
            _ => Vec::new(),
        };
        let last_candidate = runner.stats.accepted_cases + runner.stats.rejected_cases;
        return err.with_semantic(features, last_candidate);
    }
    err
}

/// One candidate in the semantic corpus: the recorded choice sequence
/// of an interesting case and the features it newly registered.
///
/// The sequence carries whatever the accepted case recorded: recorded
/// cases keep their attempt spans, exploratory cases have none
/// (exploration records no spans). Mutation reads only the draws and
/// their metadata, never the spans.
struct SemanticEntry {
    sequence: ChoiceSequence,
    /// The features this case newly registered in the global
    /// observation set. The count is the eviction criterion (fewest
    /// novel features evicted first).
    novel: Vec<Feature>,
}

/// Bounded corpus of interesting cases for feedback-guided search.
///
/// Accepted and rejected cases live in separate queues; the combined
/// size is capped at `CORPUS_SIZE`. Admission and eviction are
/// deterministic:
///
/// - A case that registers at least one novel feature is admitted
///   while the combined size is below the cap.
/// - Once full, the entry with the fewest newly registered features is
///   evicted; ties evict the earliest arrival.
/// - A case with no novel feature is never admitted.
struct SemanticCorpus {
    accepted: Vec<SemanticEntry>,
    rejected: Vec<SemanticEntry>,
    /// Globally observed features in first-registration order, capped
    /// at `MAX_GLOBAL_FEATURES`. A feature already present here is not
    /// interesting again.
    observed: Vec<Feature>,
}

impl SemanticCorpus {
    fn new() -> Self {
        Self {
            accepted: Vec::with_capacity(CORPUS_SIZE),
            rejected: Vec::with_capacity(CORPUS_SIZE),
            observed: Vec::new(),
        }
    }

    fn is_empty(&self) -> bool {
        self.accepted.is_empty() && self.rejected.is_empty()
    }

    /// The features of `case_features` that are not yet in the global
    /// observation set, in report order.
    fn novel_features(&self, case_features: &[Feature]) -> Vec<Feature> {
        case_features
            .iter()
            .filter(|f| !self.observed.contains(f))
            .cloned()
            .collect()
    }

    /// Register novel features into the global observation set,
    /// stopping at `MAX_GLOBAL_FEATURES`. Returns the features that
    /// were actually registered (an empty result means the case added
    /// nothing new and is not interesting).
    ///
    /// `novel` must be the output of [`novel_features`](Self::novel_features)
    /// (or otherwise contain only features absent from `observed`);
    /// duplicates are not checked here.
    fn register(&mut self, novel: &[Feature]) -> Vec<Feature> {
        let mut registered = Vec::new();
        for feature in novel {
            if self.observed.len() >= MAX_GLOBAL_FEATURES {
                break;
            }
            self.observed.push(feature.clone());
            registered.push(feature.clone());
        }
        registered
    }

    /// Admit an accepted case. Returns `true` when the case entered
    /// the corpus.
    fn admit_accepted(&mut self, sequence: ChoiceSequence, case_features: Vec<Feature>) -> bool {
        let novel = self.novel_features(&case_features);
        let registered = self.register(&novel);
        if !registered.is_empty() {
            self.accepted.push(SemanticEntry {
                sequence,
                novel: registered,
            });
            self.evict_if_over_capacity();
            return true;
        }
        false
    }

    /// Admit a rejected case that registered novel features, into the
    /// rejected queue (kept as low-energy scaffolding).
    fn admit_rejected(&mut self, sequence: ChoiceSequence, case_features: Vec<Feature>) -> bool {
        let novel = self.novel_features(&case_features);
        let registered = self.register(&novel);
        if registered.is_empty() {
            return false;
        }
        self.rejected.push(SemanticEntry {
            sequence,
            novel: registered,
        });
        self.evict_if_over_capacity();
        true
    }

    /// Index of the weakest entry across both queues: fewest newly
    /// registered features first; ties keep the earlier arrival.
    fn weakest_overall(&self) -> usize {
        let mut weakest: Option<(&SemanticEntry, usize)> = None;
        for (i, entry) in self.accepted.iter().chain(self.rejected.iter()).enumerate() {
            match weakest {
                None => weakest = Some((entry, i)),
                Some((incumbent, _)) => {
                    if entry.novel.len() < incumbent.novel.len() {
                        weakest = Some((entry, i));
                    }
                }
            }
        }
        weakest.expect("corpus is non-empty while evicting").1
    }

    /// Evict the weakest entry (across both queues) while the combined
    /// size exceeds `CORPUS_SIZE`.
    fn evict_if_over_capacity(&mut self) {
        while self.accepted.len() + self.rejected.len() > CORPUS_SIZE {
            let idx = self.weakest_overall();
            if idx < self.accepted.len() {
                self.accepted.remove(idx);
            } else {
                self.rejected.remove(idx - self.accepted.len());
            }
        }
    }
}

/// Per-run search state owned by the runner: the semantic corpus and
/// the search PRNG from which every exploratory decision (entry pick,
/// mutation site, restart roll, exploratory draw seed) is derived in
/// fixed order.
struct CorpusGuidedSearch {
    corpus: SemanticCorpus,
    prng: XoshiroState,
}

impl CorpusGuidedSearch {
    fn new(seed: u64) -> Self {
        Self {
            corpus: SemanticCorpus::new(),
            prng: XoshiroState::from_seed(seed),
        }
    }

    /// Build the context for the next candidate.
    ///
    /// - While the corpus is empty, or with probability
    ///   `1 / RANDOM_RESTART_DENOM`, a fresh candidate is generated
    ///   from a case seed derived from the search PRNG (recording
    ///   mode).
    /// - Otherwise, with probability `1 / REJECTED_PICK_DENOM` the
    ///   rejected queue is picked (when non-empty); otherwise an
    ///   accepted entry is picked uniformly — its draws are mutated
    ///   within their recorded constraints, and the result is explored
    ///   (replay + generated tail draws).
    ///
    /// The search PRNG is consumed per candidate in this fixed order:
    /// the restart roll first (skipped while the corpus is empty),
    /// then — for exploratory candidates — the explore seed, the
    /// rejected-queue roll (skipped while it is empty), the index roll,
    /// and finally the mutation rolls. This fixed order keeps the run
    /// reproducible from the seed.
    fn next_context(&mut self) -> TestCaseContext {
        let restart = self.corpus.is_empty() || self.prng.sample_below(RANDOM_RESTART_DENOM) == 0;
        if restart {
            let case_seed = self.prng.next_u64();
            TestCaseContext::recording(case_seed)
        } else {
            let explore_seed = self.prng.next_u64();
            // Prefer the accepted queue, except with probability
            // `1 / REJECTED_PICK_DENOM` pick the rejected queue when it
            // is non-empty. The rejected queue is the only source while
            // the accepted queue is empty (an early rejected case that
            // registered novel features).
            let use_rejected = !self.corpus.rejected.is_empty()
                && (self.corpus.accepted.is_empty()
                    || self.prng.sample_below(REJECTED_PICK_DENOM) == 0);
            let mut sequence = if use_rejected {
                let idx = self.prng.sample_below(self.corpus.rejected.len() as u64) as usize;
                self.corpus.rejected[idx].sequence.clone()
            } else {
                let idx = self.prng.sample_below(self.corpus.accepted.len() as u64) as usize;
                self.corpus.accepted[idx].sequence.clone()
            };
            mutate_sequence(&mut sequence, &mut self.prng);
            TestCaseContext::exploring(sequence, explore_seed)
        }
    }
}

/// Rewrite a candidate's draws for the next generation.
///
/// Each draw is rewritten with probability `1 / MUTATION_DENOM`:
/// bounded-domain draws (Bounded / Choice) get a fresh value inside
/// their recorded constraint, while constraint-free draws (Raw: raw
/// bytes, string payload, …) are regenerated as a whole — never
/// byte-mutated in place. The draw count and the recorded attempt-span
/// structure are preserved, so the mutated sequence still replays
/// structurally.
fn mutate_sequence(sequence: &mut ChoiceSequence, prng: &mut XoshiroState) {
    let (draws, metas) = sequence.draws_and_metas();
    for (draw, meta) in draws.iter_mut().zip(metas.iter()) {
        if prng.sample_below(MUTATION_DENOM) != 0 {
            continue;
        }
        match meta {
            ChoiceMeta::Bounded { bound } => {
                if draw.len() != 8 {
                    // Bounded/Choice draws are exactly eight bytes (the
                    // rejection-sampling core draws a u64); anything
                    // else cannot be rewritten in place.
                    continue;
                }
                // bound <= 1 never occurs (sample_below returns early
                // for n == 1 without drawing), kept as a defensive
                // guard against a future primitive that records a
                // singleton domain.
                if *bound <= 1 {
                    continue;
                }
                let new_value = prng.sample_below(*bound);
                draw[..8].copy_from_slice(&new_value.to_le_bytes());
            }
            ChoiceMeta::Choice { len } => {
                if draw.len() != 8 {
                    continue;
                }
                if *len <= 1 {
                    continue;
                }
                let new_value = prng.sample_below(*len as u64);
                draw[..8].copy_from_slice(&new_value.to_le_bytes());
            }
            ChoiceMeta::Integer => {
                // Plain integer draw: rewrite to any value of the same
                // width (the full domain is valid).
                match draw.len() {
                    1 => draw.copy_from_slice(&[prng.next_u64() as u8]),
                    2 => draw.copy_from_slice(&(prng.next_u64() as u16).to_le_bytes()),
                    4 => draw.copy_from_slice(&(prng.next_u64() as u32).to_le_bytes()),
                    8 => draw.copy_from_slice(&prng.next_u64().to_le_bytes()),
                    16 => {
                        let lo = prng.next_u64();
                        let hi = prng.next_u64();
                        draw[..8].copy_from_slice(&lo.to_le_bytes());
                        draw[8..].copy_from_slice(&hi.to_le_bytes());
                    }
                    width => {
                        // Unreachable: Integer draws come from the
                        // sample_u*/sample_i* primitives whose widths
                        // are 1, 2, 4, 8, or 16 bytes. Fall back to a
                        // full regeneration if a future primitive
                        // records a different width.
                        debug_assert!(false, "unexpected integer draw width: {width}");
                        prng.fill(draw);
                    }
                }
            }
            ChoiceMeta::Raw => {
                // Constraint-free draw: regenerate the whole draw
                // rather than byte-mutating it.
                prng.fill(draw);
            }
        }
    }
}

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

    // === SemanticCorpus admission / eviction ===

    fn event_feature(label: &'static str) -> Feature {
        Feature {
            label,
            kind: crate::rng::FeatureKind::Event(crate::rng::EventBucket::One),
        }
    }

    fn bucket_feature(label: &'static str, value: u64) -> Feature {
        Feature {
            label,
            kind: crate::rng::FeatureKind::Bucket { value },
        }
    }

    #[test]
    fn semantic_corpus_admits_novel_features() {
        let mut corpus = SemanticCorpus::new();
        assert!(
            corpus.admit_accepted(ChoiceSequence::default(), vec![event_feature("a")]),
            "a case with a novel feature must be admitted"
        );
        assert!(
            !corpus.admit_accepted(ChoiceSequence::default(), vec![event_feature("a")]),
            "a case with only known features must not be admitted"
        );
        assert_eq!(corpus.accepted.len(), 1);
    }

    #[test]
    fn semantic_corpus_register_stops_at_global_cap() {
        // The global feature registry must stop growing at
        // MAX_GLOBAL_FEATURES: a novel feature reported after the cap
        // never makes a case interesting, so a high-cardinality
        // property cannot grow memory without bound.
        let mut corpus = SemanticCorpus::new();
        for i in 0..MAX_GLOBAL_FEATURES {
            assert!(corpus.admit_accepted(
                ChoiceSequence::default(),
                vec![bucket_feature("f", i as u64)],
            ));
        }
        assert_eq!(corpus.observed.len(), MAX_GLOBAL_FEATURES);
        assert!(
            !corpus.admit_accepted(
                ChoiceSequence::default(),
                vec![bucket_feature("f", MAX_GLOBAL_FEATURES as u64)],
            ),
            "a novel feature after the cap must not be admitted"
        );
        assert_eq!(corpus.observed.len(), MAX_GLOBAL_FEATURES);
        assert_eq!(
            corpus.accepted.len(),
            CORPUS_SIZE,
            "the corpus itself stays bounded by eviction"
        );
    }

    #[test]
    fn semantic_corpus_register_partially_registers_at_cap_boundary() {
        // At the cap boundary, only the features that fit are
        // registered: the case is admitted with the truncated novel
        // set, and the overflow feature is not registered.
        let mut corpus = SemanticCorpus::new();
        let prefix: Vec<Feature> = (0..MAX_GLOBAL_FEATURES - 1)
            .map(|i| bucket_feature("g", i as u64))
            .collect();
        assert_eq!(corpus.register(&prefix).len(), MAX_GLOBAL_FEATURES - 1);
        let fits = bucket_feature("g", (MAX_GLOBAL_FEATURES - 1) as u64);
        let overflows = bucket_feature("g", MAX_GLOBAL_FEATURES as u64);
        assert!(corpus.admit_accepted(ChoiceSequence::default(), vec![fits.clone(), overflows],));
        assert_eq!(corpus.accepted[0].novel, vec![fits]);
        assert_eq!(corpus.observed.len(), MAX_GLOBAL_FEATURES);
    }

    #[test]
    fn semantic_corpus_rejected_queue_holds_novel_cases() {
        let mut corpus = SemanticCorpus::new();
        assert!(
            corpus.admit_rejected(ChoiceSequence::default(), vec![event_feature("r")]),
            "a rejected case with a novel feature must enter the rejected queue"
        );
        assert!(
            !corpus.admit_rejected(ChoiceSequence::default(), vec![event_feature("r")]),
            "a rejected case with only known features must not be kept"
        );
        assert_eq!(corpus.rejected.len(), 1);
        assert_eq!(corpus.observed.len(), 1);
    }

    #[test]
    fn semantic_corpus_evicts_fewest_features_first() {
        let mut corpus = SemanticCorpus::new();
        // Fill the corpus with distinct single-feature entries, then a
        // two-feature entry on top.
        for i in 0..CORPUS_SIZE {
            corpus.admit_accepted(
                ChoiceSequence::default(),
                vec![bucket_feature("single", i as u64)],
            );
        }
        assert_eq!(corpus.accepted.len(), CORPUS_SIZE);
        corpus.admit_accepted(
            ChoiceSequence::default(),
            vec![event_feature("two-a"), event_feature("two-b")],
        );
        assert_eq!(corpus.accepted.len(), CORPUS_SIZE);
        // The two-feature entry survives; a single-feature entry was
        // evicted (the earliest arrival).
        assert!(
            corpus
                .accepted
                .iter()
                .any(|e| e.novel.contains(&event_feature("two-a"))),
            "the feature-richest entry must survive eviction"
        );
        assert!(
            !corpus
                .accepted
                .iter()
                .any(|e| e.novel.contains(&bucket_feature("single", 0))),
            "the earliest single-feature entry must be evicted"
        );
    }

    #[test]
    fn semantic_corpus_evicts_across_both_queues() {
        let mut corpus = SemanticCorpus::new();
        for i in 0..CORPUS_SIZE {
            corpus.admit_accepted(
                ChoiceSequence::default(),
                vec![bucket_feature("accepted", i as u64)],
            );
        }
        assert_eq!(corpus.accepted.len(), CORPUS_SIZE);
        corpus.admit_rejected(ChoiceSequence::default(), vec![event_feature("rejected")]);
        // The combined size is capped: the rejected entry ties the
        // accepted single-feature entries on novel count, and ties
        // evict the earliest arrival, so the first accepted entry is
        // evicted and the rejected entry survives.
        assert_eq!(corpus.accepted.len() + corpus.rejected.len(), CORPUS_SIZE);
        assert_eq!(
            corpus.rejected.len(),
            1,
            "the rejected entry must survive the earliest-arrival tie-break"
        );
        assert!(
            !corpus
                .accepted
                .iter()
                .any(|e| e.novel.contains(&bucket_feature("accepted", 0))),
            "the earliest accepted entry must be evicted"
        );
    }

    // === CorpusGuidedSearch wiring ===

    #[test]
    fn corpus_guided_search_replays_accepted_entries() {
        // With a one-eighth restart probability, most picks are
        // exploratory: they replay the admitted draw under the same
        // declared constraint (possibly mutated, always in domain).
        // Recording (fresh) candidates draw unconstrained random bytes
        // instead. Deterministic per seed.
        let mut search = CorpusGuidedSearch::new(42);
        let mut seq = ChoiceSequence::default();
        seq.push_draw(
            7u64.to_le_bytes().to_vec(),
            ChoiceMeta::Bounded { bound: 10 },
        );
        search.corpus.admit_accepted(seq, vec![event_feature("a")]);
        let mut in_domain = 0;
        for _ in 0..16 {
            let mut ctx = search.next_context();
            ctx.set_next_choice_meta(ChoiceMeta::Bounded { bound: 10 });
            let mut buf = [0u8; 8];
            ctx.fill(&mut buf);
            let x = u64::from_le_bytes(buf);
            let _ = ctx.take_sequence();
            if x < 10 {
                in_domain += 1;
            }
        }
        assert!(
            in_domain >= 8,
            "most candidates must replay an in-domain draw: {in_domain}"
        );
    }

    #[test]
    fn corpus_guided_search_picks_from_rejected_queue_when_accepted_empty() {
        // While the accepted queue is empty, the rejected queue is the
        // only source for exploratory candidates (forced pick, no
        // rejected-queue roll consumed). The replayed draw must match
        // the admitted rejected sequence.
        let mut search = CorpusGuidedSearch::new(7);
        let mut seq = ChoiceSequence::default();
        seq.push_draw(
            3u64.to_le_bytes().to_vec(),
            ChoiceMeta::Bounded { bound: 10 },
        );
        search.corpus.admit_rejected(seq, vec![event_feature("r")]);
        let mut seen_replay = false;
        for _ in 0..16 {
            let mut ctx = search.next_context();
            ctx.set_next_choice_meta(ChoiceMeta::Bounded { bound: 10 });
            let mut buf = [0u8; 8];
            ctx.fill(&mut buf);
            let x = u64::from_le_bytes(buf);
            let _ = ctx.take_sequence();
            if x == 3 {
                seen_replay = true;
            }
        }
        assert!(
            seen_replay,
            "the rejected entry must be replayed while the accepted queue is empty"
        );
    }

    // === mutate_sequence ===

    #[test]
    fn mutation_stays_within_bounded_domain() {
        let mut prng = XoshiroState::from_seed(1234);
        let mut mutated = false;
        let mut raw_regenerated = false;
        let original_raw = vec![0xAB; 8];
        for _ in 0..100 {
            let mut seq = ChoiceSequence::default();
            seq.push_draw(
                7u64.to_le_bytes().to_vec(),
                ChoiceMeta::Bounded { bound: 10 },
            );
            // Raw draw: regenerated as a whole when mutated (never
            // byte-mutated in place).
            seq.push_draw(original_raw.clone(), ChoiceMeta::Raw);
            mutate_sequence(&mut seq, &mut prng);
            let x = u64::from_le_bytes(seq.draws()[0][..8].try_into().unwrap());
            assert!(x < 10, "mutated bounded draw {x} escaped its domain");
            if x != 7 {
                mutated = true;
            }
            if seq.draws()[1] != original_raw {
                raw_regenerated = true;
            }
        }
        assert!(mutated, "mutation must have occurred at least once");
        assert!(
            raw_regenerated,
            "raw draw regeneration must have occurred at least once"
        );
    }

    #[test]
    fn mutation_preserves_choice_index_domain() {
        let mut prng = XoshiroState::from_seed(99);
        for _ in 0..100 {
            let mut seq = ChoiceSequence::default();
            seq.push_draw(3u64.to_le_bytes().to_vec(), ChoiceMeta::Choice { len: 4 });
            mutate_sequence(&mut seq, &mut prng);
            let idx = u64::from_le_bytes(seq.draws()[0][..8].try_into().unwrap());
            assert!(idx < 4, "mutated choice index {idx} escaped its domain");
        }
    }
}

// === mutate_sequence ===

#[test]
fn mutation_rewrites_integer_draws() {
    let mut prng = XoshiroState::from_seed(77);
    let mut changed = false;
    for _ in 0..100 {
        let mut seq = ChoiceSequence::default();
        seq.push_draw(5u64.to_le_bytes().to_vec(), ChoiceMeta::Integer);
        mutate_sequence(&mut seq, &mut prng);
        let x = u64::from_le_bytes(seq.draws()[0][..8].try_into().unwrap());
        if x != 5 {
            changed = true;
        }
    }
    assert!(changed, "integer draws must be rewritten to another value");
}

#[test]
fn mutation_rewrites_integer_draws_across_widths() {
    let mut prng = XoshiroState::from_seed(31337);
    // Every recorded width must be rewritable to a new value.
    let mut changed = 0u32;
    for width in [1usize, 2, 4, 8, 16] {
        for _ in 0..64 {
            let mut seq = ChoiceSequence::default();
            seq.push_draw(vec![0xAB; width], ChoiceMeta::Integer);
            let before = seq.draws()[0].clone();
            mutate_sequence(&mut seq, &mut prng);
            if seq.draws()[0] != before {
                changed += 1;
                break;
            }
        }
    }
    assert_eq!(changed, 5, "every integer width must be rewritable");
}

#[test]
fn mutation_rewrites_upper_bytes_of_16_byte_integer_draw() {
    // Width 16 is written as two 8-byte halves; a mutation bug that
    // only rewrote the low half would leave high values untouched.
    let mut prng = XoshiroState::from_seed(1337);
    let mut low_changed = false;
    let mut high_changed = false;
    for _ in 0..256 {
        let mut seq = ChoiceSequence::default();
        seq.push_draw(vec![0xAB; 16], ChoiceMeta::Integer);
        let before = seq.draws()[0].clone();
        mutate_sequence(&mut seq, &mut prng);
        let after = &seq.draws()[0];
        low_changed |= after[..8] != before[..8];
        high_changed |= after[8..] != before[8..];
        if low_changed && high_changed {
            break;
        }
    }
    assert!(low_changed, "low 8 bytes must be rewritable");
    assert!(high_changed, "high 8 bytes must be rewritable");
}

// === Stats corpus fields ===

#[test]
fn stats_corpus_fields_are_zero_for_uniform() {
    let mut runner = Runner::new(1);
    runner
        .run(4, |ctx| {
            crate::sample_u32(ctx);
            Ok(())
        })
        .unwrap();
    let stats = runner.stats();
    assert_eq!(stats.discovered_features, 0);
    assert_eq!(stats.max_corpus_size, 0);

    // The corpus fields stay 0 on failure paths too.
    let err = Runner::new(1)
        .run(4, |_ctx| {
            Err::<(), Box<dyn std::error::Error>>("boom".into())
        })
        .expect_err("returned Err must fail the run");
    assert_eq!(err.stats().discovered_features, 0);
    assert_eq!(err.stats().max_corpus_size, 0);

    let err = Runner::new(1)
        .run(4, |ctx| {
            crate::sample_u32(ctx);
            panic!("boom");
        })
        .expect_err("panicking closure must fail the run");
    assert_eq!(err.stats().discovered_features, 0);
    assert_eq!(err.stats().max_corpus_size, 0);
}

#[test]
fn stats_corpus_fields_reflect_observed_features() {
    // Every case reports the same feature, so exactly one feature is
    // observed and exactly one entry is admitted.
    let mut runner = Runner::new(1);
    runner
        .run_feedback_guided(4, |ctx| {
            ctx.bucket("b", 1);
            Ok(())
        })
        .unwrap();
    let stats = runner.stats();
    assert_eq!(stats.discovered_features, 1);
    assert_eq!(stats.max_corpus_size, 1);
}

#[test]
fn stats_corpus_fields_respect_corpus_cap() {
    // Every case reports a fresh feature: the corpus fills to the cap
    // while the observation set keeps growing. Each candidate invokes
    // the closure exactly once (exploratory replay replays draws, not
    // the closure), so 100 accepted cases register exactly 100
    // features.
    let case = std::cell::Cell::new(0u64);
    let mut runner = Runner::new(1);
    runner
        .run_feedback_guided(100, |ctx| {
            let i = case.get();
            case.set(i + 1);
            ctx.bucket("b", i);
            Ok(())
        })
        .unwrap();
    let stats = runner.stats();
    assert_eq!(stats.max_corpus_size, CORPUS_SIZE);
    assert_eq!(
        stats.discovered_features, 100,
        "the observation set must keep growing past the corpus cap"
    );
}

#[test]
fn stats_corpus_fields_on_too_many_rejections() {
    // Every case rejects, so the run ends with too-many-rejections
    // after the rejection cap. Each rejected case reports a fresh
    // feature before rejecting: with 103 cases the rejection cap
    // (1030) exceeds `MAX_GLOBAL_FEATURES`, so the observation set
    // saturates at the cap and the rejected queue fills to
    // `CORPUS_SIZE`; the error must carry both.
    let case = std::cell::Cell::new(0u64);
    let mut runner = Runner::new(1);
    let err = runner
        .run_feedback_guided(103, |ctx| {
            let i = case.get();
            case.set(i + 1);
            ctx.bucket("b", i);
            ctx.reject_case();
        })
        .expect_err("rejecting every case must hit the rejection cap");
    let stats = err.stats();
    assert_eq!(stats.rejected_cases, rejection_limit(103) + 1);
    assert_eq!(stats.discovered_features, MAX_GLOBAL_FEATURES);
    assert_eq!(stats.max_corpus_size, CORPUS_SIZE);
}

#[test]
fn corpus_stats_matches_corpus_state() {
    use crate::rng::{EventBucket, FeatureKind};
    let mut search = CorpusGuidedSearch::new(1);
    assert_eq!(corpus_stats(&search), (0, 0));

    // An accepted case registering a novel feature.
    let feature = Feature {
        label: "a",
        kind: FeatureKind::Event(EventBucket::One),
    };
    assert!(
        search
            .corpus
            .admit_accepted(ChoiceSequence::default(), vec![feature])
    );
    assert_eq!(corpus_stats(&search), (1, 1));

    // A rejected case registering another novel feature: rejected
    // cases also contribute to the observation set and the corpus.
    let feature = Feature {
        label: "b",
        kind: FeatureKind::Event(EventBucket::One),
    };
    assert!(
        search
            .corpus
            .admit_rejected(ChoiceSequence::default(), vec![feature])
    );
    assert_eq!(corpus_stats(&search), (2, 2));
}

#[test]
fn stats_corpus_fields_include_rejected_case_features() {
    // The first case rejects while reporting a novel feature: it
    // enters the rejected queue and its feature enters the observation
    // set. Later cases report the same (now observed) feature and are
    // not admitted.
    let case = std::cell::Cell::new(0u64);
    let mut runner = Runner::new(1);
    runner
        .run_feedback_guided(8, |ctx| {
            let i = case.get();
            case.set(i + 1);
            ctx.bucket("b", 1);
            if i == 0 {
                ctx.reject_case();
            }
            Ok(())
        })
        .expect("run must succeed");
    let stats = runner.stats();
    assert_eq!(stats.rejected_cases, 1);
    assert_eq!(stats.discovered_features, 1);
    assert_eq!(stats.max_corpus_size, 1);
}

#[test]
fn stats_corpus_fields_zero_cases() {
    let mut runner = Runner::new(1);
    runner
        .run_feedback_guided(0, |_ctx| {
            panic!("closure must not be invoked with zero cases");
        })
        .expect("zero cases must succeed");
    assert_eq!(runner.stats(), Stats::default());
}