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
//! Slice 7b's core-level coverage for the **reassembly re-coalescing
//! amplification** (F3), plus ruling 253's **work** and **capacity** bounds.
//!
//! Written from `CONTRACT-7b.md` §5 and `ADVERSARIAL-liveness.md` F3 by an
//! author who never read the fix (CLAUDE.md working rule 6), in a worktree
//! cut at `c131904`; extended for ruling 253 by a second author blind to
//! *that* implementation, in a worktree cut at `9c557ce`.
//!
//! # The defect
//!
//! `Reassembly::insert` allocates `vec![0u8; span]` and copies the whole
//! merged span for **every** accepted STREAM frame, including one lying
//! wholly inside offset space it already holds. `check_stream` accepts such a
//! frame (`end <= high_water`) and charges `delta = 0`, so the peer picks
//! both the span and the rate for free. The fix is an early return before any
//! allocation when the arriving range is entirely covered by one stored
//! chunk.
//!
//! # The hard part: what separates the two builds
//!
//! Almost nothing does, and this is worth stating plainly before the tests.
//!
//! * **Chunk count** is the same — merging one chunk with a range inside it
//! yields one chunk.
//! * **Byte content** is the same — for a range whose bytes match what is
//! stored, which is what every test here sends. §9.5 lets the receiver keep
//! **either** copy of a byte received twice with differing values (ruling
//! 253 relaxed `recv.rs`'s old *"stored bytes win"* claim, because
//! small-to-large can invert which copy survives), so no test here may lean
//! on which one wins, and none does.
//! * **Flow credit** is the same — `CONTRACT-7b.md` §5 forbids touching
//! `check_stream`, so a duplicate is free in **both** builds by design.
//! * **Wall-clock time** is the actual difference and is not assertable: it
//! is flaky, and this project runs on a paused clock.
//!
//! So the naive test the contract sketches — *"assert the buffer's chunk
//! count and byte content are unchanged and that no read becomes
//! available"* — **passes the broken build**. It is a correctness test, and
//! the contract says so; it is not an amplification test. Working rule 9:
//! *a bound is only a test if the degenerate case violates it*, and every
//! one of those bounds is satisfied by the code that has the defect.
//!
//! # The separating quantity: `reassembly_copy_work()`
//!
//! **[Re-derived 2026/08/17 — ruling 253 [S-77].]** This section used to
//! nominate `reassembly_capacity()`. The argument was that a covered insert
//! re-allocates `vec![0u8; span]`, so a chunk holding **slack** — `Vec::drain`
//! removes bytes without shrinking the allocation, so after reading `n` of
//! `N` bytes the chunk holds `N - n` bytes in an `N`-byte buffer — announces
//! the re-coalesce as a capacity *collapse* from `N` to `N - n`.
//!
//! That argument is a property of `vec![0u8; span]`, **the exact line ruling
//! 253 replaces**, and it does not survive the replacement. Under a
//! small-to-large merge the arriving side is copied into the stored chunk's
//! existing allocation, so a build that **lost** F3's early return would
//! re-copy bytes and leave the capacity *unchanged*. A separator derived
//! against the code being deleted is not a separator; this is why the ruling
//! makes re-deriving it an obligation rather than an assumption.
//!
//! The re-derived quantity is the one ruling 253 states its bound over:
//! `Connection::reassembly_copy_work()`, the monotone total of bytes written
//! into chunk storage by `insert` — the arriving frame's bytes on store, plus
//! every stored byte re-copied during a merge, never reset. F3's early return
//! happens **before** any of that, so a covered frame's contribution is
//! exactly zero:
//!
//! | build | `copy_work` delta, one covered frame over a stored `span` | `capacity` delta |
//! |---|---|---|
//! | **F3 present**, either merge | **0** | 0 |
//! | **F3 lost**, pre-253 whole-span merge | `span + 1` | 0, or a collapse if the chunk held slack |
//! | **F3 lost**, small-to-large merge | **0** — measured, refuting this row's blind draft of "≥ 1" | **0** |
//!
//! **[Integrator, slice R40-E — the third row is the implementation's
//! measurement, not the blind draft's prediction.]** The draft reasoned a
//! lost early return would still write the arriving bytes (≥ 1); the landed
//! merge writes the frame **only into gaps no stored chunk covers**, and a
//! covered frame has no gap — so with the return disabled every test here
//! still passes (the implementer measured exactly that, `if false && …`).
//! Consequence, worth stating precisely: **F3's defence is now structural.**
//! The zero-progress amplification cannot be bought from this merge at all;
//! the early return survives as a CPU short-circuit, and no core accessor
//! separates its loss. The `== 0` assertions below therefore pin the
//! covered case's *cost* — the property F3 exists for — not the return's
//! presence, and any bound phrased in spans is working rule 9's trap under
//! either broken build.
//!
//! `copy_work` is also a **strictly** better instrument than capacity was: it
//! separates in the attack's own configuration — byte 0 held back, nothing
//! ever read, no slack anywhere — where capacity was blind under *both*
//! merges and the test had to be contorted into reading first to see
//! anything.
//!
//! `reassembly_capacity()` is still asserted alongside it. It is no longer
//! the separator, but "a covered frame allocates nothing" is a true invariant
//! and a cheap one to keep, and §7 below uses capacity for what it *is* the
//! instrument for: ruling 253(ii)'s ceiling.
//!
//! # What the rest of the file is for
//!
//! An early return that is **too eager** silently drops received bytes, and
//! that is a far worse defect than the one being fixed. §3 tests both
//! one-sided boundaries of the covered predicate — a frame overhanging the
//! chunk's end, and one overhanging its start — because slice 1 shipped a
//! boundary tested on one side only and this is the same shape.
//!
//! §4 pins the **load-bearing lemma** the fix rests on: `CONTRACT-7b.md` §5
//! argues that *"covered-by-the-union is exactly covered-by-one-chunk"*
//! because stored chunks are pairwise disjoint **and non-adjacent**. That is
//! a claim about `insert`'s own invariant, and if it ever stops holding the
//! one-chunk lookup is wrong. Working rule 11: a rationale must name a
//! mechanism that exists, so the mechanism gets a test.
//!
//! §5 pins the amplification's **fuel** — a wholly duplicate frame is legal
//! and costs the peer nothing — which is a characterisation of both builds
//! *and* a guard against the wrong fix, the one that rejects duplicates as a
//! protocol violation and kills connections over ordinary retransmission.
//!
//! §6 and §7 are ruling 253's two bounds, and they are about the case F3
//! deliberately left alone. F3 closed the **zero**-progress insert; the
//! ruling's finding is that the *one*-byte-of-progress insert — a frame
//! bridging two stored chunks — was still worth ~916× its wire bytes to a
//! peer, because the pre-253 merge rebuilt the whole merged span for it.
//! §6 asserts the work bound over an alternating-bridging workload; §7
//! asserts that the fix does not pay for it with headroom the §10.6 ceiling
//! forbids.
//!
//! # What was measured rather than reasoned about
//!
//! `reassembly_copy_work()` does not exist on the base this file was written
//! against, so every number here would otherwise have been a derivation about
//! a state machine — which this project has lost to the build repeatedly. The
//! contract's accounting was therefore added to the **pre-253** merge in a
//! throwaway build, the file run against it, and the instrumentation reverted
//! before anything was committed. What that run established:
//!
//! * §6's pre-253 total is **548 750 144 B**, agreeing with the
//! hand-derivation to the byte, and §6 is the *only* test that fails on the
//! old merge — which is what it means for the other twelve to be pins on
//! behaviour that already ships rather than new-behaviour reds.
//! * §7 **passes** on the pre-253 merge at exactly 262 000 bytes of capacity,
//! as it must: `vec![0u8; span]` allocates the arrived span and nothing
//! more. §7 is aimed at the fix, not at the defect.
//! * Both new workloads stay inside flow control and inside
//! `REASSEMBLY_CHUNKS_MAX`, and both read back every byte they sent — so
//! nothing in either is measuring an accident.
//!
//! # No clock, so no runtime
//!
//! Sans-io core tests: `now: Instant` is an argument and nothing here reads a
//! clock. Plain `#[test]`, no `sleep`.
use Instant;
use *;
use *;
use crate;
// ═══════════════════════════════════════════════════════════════════════
// 1. Scaffolding
// ═══════════════════════════════════════════════════════════════════════
/// The span the amplification tests build. Large enough that the collapse is
/// unmistakable, small enough that the test is a few hundred AEAD
/// operations.
const SPAN: usize = 16_384;
/// How much of [`SPAN`] is read out before the covered frame arrives. The
/// slack the read leaves behind — `SPAN - READ` bytes held in a `SPAN`-byte
/// allocation — is what makes the re-coalesce visible.
const READ: usize = 16_000;
/// A core with a peer-opened uni stream carrying `[0, SPAN)`, of which
/// `READ` bytes have been read out.
///
/// Returns the core and the claimed `StreamRef`. The core is
/// `Solo::installed_at`, i.e. §3.2's **validated** anchor: no amplification
/// budget is armed, so nothing here can be perturbed by §7.3.
/// Total bytes of reassembly capacity across every receive half (ruling 94).
/// Ruling 253's accounting hook: the monotone total of bytes written into
/// chunk storage by `insert` — the arriving frame's bytes on store, plus
/// every stored byte re-copied during a merge. Never reset, so every
/// assertion here is over a *delta* or over a fresh core's total.
///
/// **The scope question this doc used to leave open is answered: ruling
/// 263 picked "never reset".** It read, verbatim:
///
/// > `reassembly_capacity()` sums over the *live* receive halves, which is
/// > right for capacity — an abandoned half's allocation really is gone.
/// > Summed the same way, copy **work** is not monotone: retiring a half
/// > would subtract its history, and a peer could reset the accounting by
/// > opening and abandoning streams. […] "never reset" and "sum the live
/// > halves" cannot both be true, and the integrator picks one.
///
/// The reported conflict was real and the diagnosis of the consequence was
/// exact. `Streams` now carries a `retired_copy_work` accumulator, so a
/// retirement moves the total by zero and the peer-triggered reset is gone.
/// Every assertion in this file was written when the answer was the other
/// one and every one of them still holds — they are over deltas, or over a
/// fresh core's total, and neither is affected by an addend that is zero
/// until something retires. [`retirement_does_not_hand_back_copy_work`] is
/// the test that pins the choice.
///
/// [`retirement_does_not_hand_back_copy_work`]: self::retirement_does_not_hand_back_copy_work
/// **[RATIFIED 2026/08/18 — ruling 263]** Retiring a receive half does not
/// give its copy work back.
///
/// # Why this is the unsafe direction, and not a tidiness point
///
/// The meter is the instrument §10.6's *work* bound is measured with, and
/// retirement made it **under**-report. An Appendix B work-bound test that
/// happens to retire a half mid-workload therefore reads a *better* ratio
/// than the truth and **passes a build with the defect** — working rule 9's
/// trap arriving through the fixture instead of the assertion. Worse, the
/// likeliest such workload retires on every unit of work: §9.8's
/// `claim_message` retires the receive half, so a work-bound test written
/// over messages measures approximately nothing.
///
/// # Why `abandon_recv` is the right retirement to use here
///
/// All five retirement paths — the message claim, a fully-read FIN, a read
/// reset, this one, and §9.8's overflow reset — funnel through
/// `Streams::retire_recv`, and the sum could only ever drop at the single
/// `recv.take()` inside it. Driving the cheapest of the five exercises that
/// line; driving all five would exercise it five times.
///
/// # Separation (working rule 9)
///
/// On the build this ruling replaced the first assertion reads 0 against a
/// `before` of `SPAN`, and the second is what stops the fix from being a
/// counter that is *assigned* rather than accumulated.
/// Assert a covered frame did **nothing** — no copying, no allocation.
///
/// **This is F3's pin, re-derived under ruling 253.** The copy-work half is
/// the separator (see the module header's table: a lost early return is
/// non-zero under either merge, and cheap under the new one, so `== 0` is the
/// only phrasing that separates). The capacity half is the older assertion,
/// kept because "allocated nothing" stays true and stays worth pinning — but
/// it is no longer claimed to separate anything on its own.
///
/// The three failure directions are different defects and the messages say
/// which is which, because a bare `assert_eq!` on two numbers is unreadable
/// at 3 a.m.
// ═══════════════════════════════════════════════════════════════════════
// 2. F3 — a covered frame must allocate nothing
// ═══════════════════════════════════════════════════════════════════════
/// The pin. A 1-byte STREAM frame at an offset inside the buffered span must
/// do no copying and touch no allocation.
///
/// Mutation caught: `Reassembly::insert` taking the merge branch for a
/// wholly-covered range. Under the **pre-253** merge that costs `span + 1`
/// bytes of copy work and, because this fixture leaves slack, also announces
/// itself as a capacity collapse from 16 384 to 384. Under the
/// **small-to-large** merge it costs 1 byte of copy work and the capacity
/// never moves — which is why the pin is `copy_work` at `== 0` and the
/// capacity assertion rides along rather than leading.
///
/// Not caught by, and deliberately not the only assertion: chunk count, byte
/// content, readability and flow credit, every one of which is identical in
/// every build.
/// The amplification, stated as the invariant it is: **an unbounded number of
/// covered frames does exactly zero work.**
///
/// Mutation caught: the same merge branch, on every iteration. Under the old
/// capacity separator this loop was decoration — the broken build collapsed
/// the capacity once and then re-allocated the same span on every subsequent
/// frame, so iterations 2..N were structurally indistinguishable and the
/// doc comment said so. `copy_work` is **cumulative**, so the loop now
/// carries its own weight: 64 covered frames add 64 separate contributions
/// to a total that must not move at all, and a build that leaked even one
/// byte per frame is caught 64 times over instead of once.
/// The attack's own configuration — **and, since ruling 253, the measurement
/// as well.**
///
/// `ADVERSARIAL-liveness.md` F3's sequence: offsets `1..N` arrive with byte 0
/// withheld, so `read_offset` stays 0, nothing is ever readable, and the
/// application can never drain the buffer. Then 1-byte frames dribble in at
/// an interior offset.
///
/// This doc comment used to open *"this test does not separate the builds and
/// is not claimed to"*, and the reason was the old separator: with nothing
/// ever read there is no slack, so the re-allocated span had the same capacity
/// as the chunk it replaced and `reassembly_capacity()` was blind to it. Every
/// other F3 test therefore had to read 16 000 bytes out first — manufacturing
/// a state the attacker never produces — to make the defect visible at all.
///
/// `reassembly_copy_work()` is blind to nothing. In **exactly** the attack's
/// configuration, with no read and no slack, 64 covered frames must add zero;
/// a pre-253 build without the early return adds `64 × (SPAN + 1)` ≈ 1.05 MB
/// for 64 wire bytes, which is F3 measured where F3 actually lives. That is
/// the sharpening ruling 253 [S-77] asked for, and it is the reason the
/// re-derivation was worth doing rather than re-asserting the old table.
// ═══════════════════════════════════════════════════════════════════════
// 3. The covered predicate's two boundaries — a too-eager fix loses data
// ═══════════════════════════════════════════════════════════════════════
//
// The early return must fire for `[o, e) ⊆ [c.offset, c.end())` and for
// nothing else. Two natural mis-statements each drop received bytes:
//
// * `c.offset <= o && o < c.end()` — right end unchecked;
// * `c.offset <= o && e <= c.end()` written as `e <= c.end()` alone —
// left end unchecked.
//
// Slice 1 shipped a boundary tested on one side only (`LEN` and `LEN-1`, not
// `LEN+1`). Both sides are here.
/// A core holding exactly one chunk `[100, 200)`, with `[0, 100)` withheld so
/// nothing can be read away and the chunk's edges stay where they are.
/// A frame that starts inside the chunk and **overhangs its end by one byte**
/// is not covered, and its last byte must be stored.
///
/// Mutation caught: an early return keyed on the start offset alone. Under it
/// byte 200 is silently discarded, the stream is short by one byte forever,
/// and — because `high_water` was raised to 201 by `check_stream` — a FIN at
/// 201 would make the stream unreadable to its end. Data loss, from the fix
/// for a CPU cost.
/// A frame that ends inside the chunk and **starts one byte before it** is
/// not covered, and its first byte must be stored.
///
/// Mutation caught: an early return keyed on the end offset alone. Under it
/// byte 99 is discarded; the reader then stops at 99 even after `[0, 99)`
/// arrives, and the hole is invisible until the application notices its
/// stream has stalled with the peer certain it sent everything.
/// The positive case at both edges at once: a frame **exactly equal** to the
/// stored chunk is covered.
///
/// This is the value the two tests above bracket. Without it they would be
/// satisfied by a fix that never early-returns at all, which is the
/// degenerate case working rule 9 asks about. With it, the three together fix
/// the predicate to `[100, 200)` and nothing wider.
///
/// **The capacity assertion here never did that job**, and ruling 253 is what
/// made it visible: a frame exactly equal to the stored chunk merges into a
/// span of exactly the chunk's size, so `vec![0u8; 100]` replaces a 100-byte
/// allocation and the capacity does not move. The build with no early return
/// passed this line. `copy_work` is what fixes it — 0 against the pre-253
/// merge's 200.
/// A frame **adjacent** to the chunk, touching neither of its bytes, is not
/// covered and must be stored.
///
/// The third boundary, and the one the merge loop is most likely to confuse
/// with the second: `insert`'s span walk merges chunks that are adjacent
/// (`chunks[hi].offset <= end`), so "touching" and "covered" are one
/// comparison apart in the same function.
// ═══════════════════════════════════════════════════════════════════════
// 4. The load-bearing lemma — stored chunks are disjoint AND non-adjacent
// ═══════════════════════════════════════════════════════════════════════
/// `CONTRACT-7b.md` §5: *"a range covered by the union of two or more stored
/// chunks would require them to be adjacent or overlapping, which the
/// invariant forbids. Therefore covered-by-the-union is exactly
/// covered-by-one-chunk, and the check is a single lookup."*
///
/// The lemma is only as good as the invariant, so the invariant gets a test.
/// 1 025 adjacent one-byte frames arrive with byte 0 withheld, so nothing is
/// ever read away and nothing is ever popped: a build that coalesces adjacent
/// ranges holds **one** chunk, and a build that does not holds 1 025 and
/// trips §10.6's `REASSEMBLY_CHUNKS_MAX` (1 024) — which is a connection
/// **death**, and therefore observable without any chunk-count accessor.
///
/// Mutation caught: any change to the span walk's `<=` that stops merging
/// touching ranges. Under it the single-chunk lookup the fix performs is
/// unsound — a range spanning two stored chunks would be covered by their
/// union and covered by neither alone, so the fix would re-coalesce anyway
/// and F3 would quietly come back for exactly the shapes that matter.
/// The other half of the invariant: ranges with a **gap** between them stay
/// separate, and the gap is real.
///
/// Mutation caught: a span walk that merged across a hole. It would fabricate
/// the missing bytes as zeroes and hand them to the application as received
/// data — silent corruption, and it would also make the fix's covered check
/// return `true` for ranges the peer never sent.
// ═══════════════════════════════════════════════════════════════════════
// 5. The fuel — a covered frame is legal and costs the peer nothing
// ═══════════════════════════════════════════════════════════════════════
/// A wholly-duplicate frame charges **zero** flow credit, at volume.
///
/// **This does not separate the F3 builds and is not claimed to** — the
/// contract forbids touching `check_stream`, so the charge is 0 in both. It
/// separates the *wrong* fix: one that rejects a fully-duplicate frame as a
/// protocol violation, which `CONTRACT-7b.md` §5 rules out because it *"would
/// kill connections over ordinary retransmission"*. That build dies on the
/// first repeat here.
///
/// It is also the amplification's fuel, measured: **1 024 re-deliveries of
/// one 1 KiB range** — 1.2 MiB of accepted STREAM traffic, past the 1 MiB
/// connection window — pass without a `FlowControl` violation, because only
/// the first 1 KiB was ever new. A build computing `delta` as the frame's
/// length rather than `end - high_water` is dead well before the last one.
/// That is what "invisible to flow control" means, stated as a test rather
/// than as a sentence in a review.
///
/// The unique span is deliberately one frame rather than the finding's
/// 256 KiB: the property is about the *charge*, which is per frame, and a
/// 256 KiB span would make the broken build re-coalesce a quarter of a
/// megabyte a thousand times over for no extra assertion.
// ═══════════════════════════════════════════════════════════════════════
// 6. Ruling 253's work bound — O(credit · log credit), from the separating
// side
// ═══════════════════════════════════════════════════════════════════════
//
// §10.6, amended: *"Coalesce-on-insert's total copy work per stream MUST be
// O(that stream's advertised credit · log credit) — every stored byte is
// copied O(log) times across its lifetime (the small-to-large discipline),
// never once per bridging frame."*
//
// F3 closed the **zero**-progress insert and said so in as many words: a
// frame bridging two stored chunks *"makes progress, is bounded by credit,
// and is left alone"*. Ruling 253's finding is that "bounded by credit" is a
// bound on the **number** of such frames and says nothing about what each one
// costs, and the gap between those two is worth ~916× a peer's wire bytes.
//
// The workload below is that gap, driven: one large stored run, and then a
// peer that parks a single byte two positions past the run's end and sends
// the byte that closes the hole. Each pair is two bytes on the wire and one
// merge, and the pre-253 merge rebuilds the entire run for it.
/// The per-stream credit `C` the bound is stated over (§10.6).
const CREDIT: u64 = INITIAL_MAX_STREAM_DATA;
/// The run the bridging frames extend, and the number of bridging rounds.
///
/// `BRIDGE_BASE + 2 × BRIDGE_ROUNDS` = 139 072, comfortably inside [`CREDIT`]
/// so the workload never touches flow control, and inside
/// [`INITIAL_MAX_DATA`] so it never touches the connection ledger either. At
/// most **two** chunks are stored at any instant, so
/// [`REASSEMBLY_CHUNKS_MAX`] is never in play: this is a pure work test and
/// nothing else may be what fails it.
const BRIDGE_BASE: usize = 131_072;
/// See [`BRIDGE_BASE`].
const BRIDGE_ROUNDS: u64 = 4_000;
/// §10.6's work bound as an integer: `ceil(0.95 · C · log₂ C)`.
///
/// `C` is 2¹⁸, so `log₂ C` is exactly 18 and the logarithm rounds nowhere;
/// the 0.95 is carried as `95/100` with a ceiling division so the test does
/// not depend on float behaviour either. The value is 4 482 663.
/// **The obligation.** An alternating-bridging workload's total copy work
/// stays inside `0.95 · C · log₂ C`.
///
/// # What each broken build does
///
/// * **Pre-253 whole-span merge — measured, not estimated.** Building the run
/// costs `Σ 1024·(j+1)` = 8 454 144 B, because `deliver_stream_bytes`
/// arrives in 1 KiB frames and each one rebuilds the whole run so far: the
/// bound is already blown by 1.9× before a single bridging round. Then each
/// round rebuilds the run again for its 2 wire bytes,
/// `Σ (131 072 + 2k + 2)` = 540 292 000 B, plus 4 000 for parking the
/// bytes. **Total 548 750 144 B for 139 072 B of wire** — 122× this bound
/// and ~3 946× per wire byte, the same lever the audit measured at 916×
/// sustained.
///
/// That figure is this test's own output, not arithmetic: the pre-253
/// merge was instrumented with the contract's accounting in a throwaway
/// build, run, and reverted before committing. It agreed with the
/// hand-derivation to the byte, which is the only reason the derivation
/// above is quoted at all.
/// * **Small-to-large merge (the fix).** The arriving side of every merge is
/// 1 byte and the parked chunk is 1 byte, so a round copies ~3 bytes.
/// Total ≈ the arrived bytes plus whatever the growth policy re-copies as
/// the run's allocation grows — a few hundred KB, an order of magnitude
/// under the bound. The margin is deliberate: the accessor's contract does
/// not say whether a reallocation *inside* the large chunk counts as copy
/// work, and the test must pass either way.
/// * **A build that refuses the work.** Dropping or rejecting the bridging
/// frames satisfies an upper bound for free — working rule 9's degenerate
/// case, and the reason for the two assertions after the bound.
// ═══════════════════════════════════════════════════════════════════════
// 7. Ruling 253's capacity ceiling — the fix may not be bought with headroom
// ═══════════════════════════════════════════════════════════════════════
//
// §10.6, amended: *"allocated **capacity** stays ≈ the arrived span, its
// per-stream ceiling ≈ the advertised credit; shrink-at-quiescence and capped
// growth both qualify …, a bare doubling policy holding ~1.5 × credit does
// not"*.
//
// This is the bill for §6. A small-to-large merge only avoids re-copying the
// large side if it can **reuse** the large side's allocation, which means
// growth-amortised buffers, which means slack — and slack is allocation ahead
// of arrival, the exact thing ruling 94 forbade and the exact thing
// `reassembly_capacity()` was built to observe. §6 and §7 are a matched pair:
// either alone is passed by a build that fails the other.
/// The ceiling workload's frame size. **Deliberately not a power of two**,
/// and the test's separating power depends on it — see
/// [`heavy_merging_to_the_credit_limit_holds_no_growth_headroom`].
const GROW_FRAME: usize = 1_000;
/// 262 × [`GROW_FRAME`]: the largest multiple of it inside [`CREDIT`], so the
/// arrived span sits 144 bytes under the per-stream window.
const GROW_BLOCKS: usize = 262;
/// **The ceiling.** After a heavily-merging workload that fills the per-stream
/// window, allocated capacity is the arrived span and not a growth policy's
/// headroom.
///
/// # Why the numbers are what they are
///
/// The workload arrives 262 000 bytes in 1 000-byte blocks: every *even*
/// block first (131 separate chunks, each with a 1 000-byte hole after it),
/// then every *odd* block in ascending order, each of which bridges the
/// growing run to the next stored chunk. 131 bridging merges, ending in one
/// chunk of 262 000 bytes — heavy merging, and the growth path a
/// small-to-large merge has to walk.
///
/// The assertion is `262 000 ≤ capacity ≤ 262 144`, and each side is doing
/// work:
///
/// * **Lower** — the chunk holds 262 000 bytes, so anything less means bytes
/// were lost. Without it a build storing nothing passes the ceiling for
/// free (working rule 9).
/// * **Upper** — [`CREDIT`], ruling 253(ii)'s per-stream ceiling, *not*
/// relaxed to the measured 1.49 × credit.
///
/// # What each broken build does
///
/// * **A bare doubling policy.** Every chunk in this workload is a multiple
/// of 1 000 bytes, and `1000 · m = 2ᵏ` has no integer solution (2ᵏ is never
/// divisible by 125), so no doubling sequence present can land on 2¹⁸ —
/// whatever base it starts from it overshoots the window. From a
/// 1 000-byte base it holds 512 000 (1.95 × credit); from the 3 000-byte
/// first merge, 384 000 (1.46 × credit, which is the ruling's measured
/// figure to within a rounding). This is why `GROW_FRAME` is 1 000 and not
/// 1 024: at 1 024 a doubling policy lands on exactly 262 144, passes, and
/// the test asserts nothing.
/// * **An eager allocator** (ruling 94's defect, §10.6's own worked example)
/// sits at the window from the first byte. The two snapshots before the
/// merging phase catch it: one chunk after one block, 131 after 131.
/// * **The pre-253 whole-span merge** passes this test, and is meant to —
/// `vec![0u8; span]` is exactly the arrived span, and it was measured here
/// at exactly 262 000. §7 is not aimed at it; §7 is aimed at what §6 tempts
/// a fix into doing.
/// The last covered-frame boundary the fix must not swallow: a frame wholly
/// **below `read_offset`**.
///
/// `insert` already returns early for these (`skip >= data.len()`), before any
/// of the fix's new machinery. The test exists because the fix inserts its
/// check right after that skip, and the two returns are one edit apart: a fix
/// that reorders them so the covered check runs on the *unskipped* offset
/// would compare a range that no longer describes the arriving bytes.
///
/// Mutation caught: a covered check placed before the `read_offset` skip.