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
//! Ruling 271's ACK cadence, pinned by a blind author.
//!
//! Written from `SPEC.md` §12.4/§12.1/§12.2, §13.1/§13.2/§13.3, §7.4/§7.5's
//! liveness accounting and ruling 33, the fixtures in
//! [`testfix`](super::testfix), and round 42's committed measurements
//! (`.spec-v2-clean-slate/round42-G-datapath-attribution.md` "ACK ratio",
//! `round42-H-residual-profile.md` "The true ACK-lever value"). The author
//! never read `src/shell/driver.rs`, `ack.rs`, `recv.rs` or `send.rs`, and
//! wrote in parallel with the implementer (working rule 6).
//!
//! # What is being changed, and what is not
//!
//! §12.4's **first** bullet — *"An ACK is owed after every **2nd**
//! ack-eliciting packet"* — becomes per-drain coalescing: **a burst of N
//! ack-eliciting datagrams processed in one receive-drain produces at most
//! one ACK emission.** Everything else in §12.4 is untouched:
//!
//! * the immediate ACK on out-of-order arrival (bullet two);
//! * `MAX_ACK_DELAY` = 25 ms as the **outer bound** — an ack-eliciting
//! packet is acknowledged within it even when no further drain ever
//! happens (the idle-after-burst tail);
//! * ruling 33's liveness-neutrality of ACKs, and §7.4's `seal_quiet`;
//! * every wire format, `MAX_ACK_RANGES`, and §12.1's range arithmetic.
//!
//! # The recovery half is the point
//!
//! Our ACKs are the peer's only feedback: §13.1 takes RTT samples from
//! them, §13.2 declares loss from them, §14.2 opens the window from them.
//! A cadence fix that starves them buys throughput by blinding the sender,
//! and every such fix must go **red** in this file. Five of the tests
//! below exist only for that:
//! [`a_lone_ack_eliciting_packet_is_acked_within_max_ack_delay`],
//! [`the_tail_of_an_idle_burst_is_acked_within_max_ack_delay`],
//! [`a_coalesced_ack_reports_every_range_a_per_packet_ack_would`],
//! [`a_single_lost_packet_is_recovered_without_advancing_the_clock`] and
//! [`the_estimator_still_samples_across_a_coalesced_exchange`].
//!
//! # What "one drain" is read as, at the core seam
//!
//! §16.4 is explicit that *"**every mutating call** (`handle_datagram`, …)
//! **is followed by draining `poll_output()` to the terminal
//! `Timeout(Option<Instant>)`**"*, and the fixtures obey it: `Solo::deliver`
//! (`testfix.rs:1170`) and `Pair::flush_a_to_b_from` (`testfix.rs:701`)
//! both drain **after each datagram**. So "one drain over N datagrams" is
//! not a state the driver contract permits and not a state any fixture
//! helper expresses. This file therefore reads ruling 271's unit as **one
//! receive batch**: the N datagrams a driver turn takes off the socket, all
//! bearing the same `now` (§16.5's once-per-turn instant), each drained per
//! §16.4. That reading is red at base, honours §16.4, and reproduces the
//! shape round 42-G measured on the real socket.
//!
//! The **literal** reading — N `handle_datagram` calls before a single
//! `poll_output` loop — is written too, as
//! [`twenty_datagrams_before_a_single_poll_output_loop_emit_at_most_one_ack`],
//! and reported in `AUTHOR-ACK-REPORT.md` §C1 as an unresolved conflict.
//! The author does **not** pick between them (working rule 3).
use RangeInclusive;
use ;
use Dir;
use StreamRef;
use *;
use crate;
/// The reference suite, named locally: `testfix`'s own alias is private to
/// that module and this file must not reach into it.
type Suite = crateReferenceSuite;
/// The burst size. Twenty is the brief's number and is comfortably above
/// §12.4's every-2nd trigger, so a base core emits ten ACKs where the
/// ratified cadence emits one — a factor of ten, not a boundary.
const BURST: usize = 20;
// ═══════════════════════════════════════════════════════════════════════
// §12.1, decoded in this file's own vocabulary
// ═══════════════════════════════════════════════════════════════════════
/// §8.4's ACK as its wire fields.
///
/// Deliberately reconstructed from [`Wire::Ack`] rather than read back
/// through `frame::Ack`: a derivation asked whether it agrees with itself
/// always says yes.
// ═══════════════════════════════════════════════════════════════════════
// Counting ACK emissions
// ═══════════════════════════════════════════════════════════════════════
/// Every ACK frame a drain produced, in generation order.
/// How many **packets** in a drain carried an ACK.
///
/// This, and not the ACK-frame count, is what ruling 271 bounds: §12.4
/// lets an owed ACK ride an outgoing data packet, so a build that
/// piggybacks perfectly has emitted nothing extra even though an ACK frame
/// went out. Counting frames would make a correct piggyback look like a
/// cadence violation; counting *standalone* ACK packets only would let a
/// build hide ten ACKs inside ten credit packets.
// ═══════════════════════════════════════════════════════════════════════
// Frames and delivery
// ═══════════════════════════════════════════════════════════════════════
/// A PING — §8.3's ack-eliciting frame the core has no verb to produce.
///
/// PING rather than STREAM on purpose: a STREAM burst also moves flow
/// credit, so a build could answer it with `MAX_STREAM_DATA` packets and
/// the ACK count would depend on §10.3's advance threshold rather than on
/// §12.4. PING isolates the cadence.
/// §3.4's counter, read from the cleartext header.
/// Seal `pt` as the peer so it lands on exactly `counter`, burning and
/// discarding the packets in between — the only way to open a gap, since
/// hiss owns the counter and only moves it forward (§7.1).
/// `handle_datagram` then drain, without re-sealing.
/// Deliver `n` in-order ack-eliciting datagrams **at one instant**, each
/// drained per §16.4, and return how many packets carrying an ACK left.
///
/// This is the "one receive batch" of the module header: the shape a
/// driver turn produces when the socket had `n` datagrams queued.
/// Advance only the core's own deadlines — **no further input** — until an
/// ACK covering `counter` leaves, or `horizon` passes. Returns the instant
/// the ACK left and every ACK that left with it.
///
/// A pure timer walk: this is the idle-after-burst tail, so the peer is
/// silent by construction and nothing but §12.4's `AckDelay` can produce
/// the ACK.
// ═══════════════════════════════════════════════════════════════════════
// 1 — the cadence itself
// ═══════════════════════════════════════════════════════════════════════
/// **Ruling 271, the whole of it.** A burst of [`BURST`] ack-eliciting
/// datagrams taken off the socket in one receive batch produces **at most
/// one** ACK emission.
///
/// §12.4 as amended: a burst of N ack-eliciting datagrams processed in one
/// receive-drain produces at most one ACK emission. The warm-up packet is
/// excluded because §12.4's surviving second bullet ACKs the session's
/// first ack-eliciting packet immediately — *"the rule applies vacuously
/// and yields an immediate ACK"* — and that ACK is ratified.
///
/// **Expected RED at base**: §12.4's pre-271 first bullet owes an ACK
/// *"after every **2nd** ack-eliciting packet"*, so twenty in-order
/// packets emit ten. That is round 42-G's measured 1 ACK per 2 data
/// datagrams (33.6 % of all datagrams ACK-only, against quinn's 1.68 %),
/// reproduced at the core seam.
///
/// Separating mutations (working rule 9). `<= 1` fails a build that keeps
/// the every-2nd trigger (10), one that ACKs each packet (20), and one
/// that ACKs every 4th (5) — a "fewer than before" bound would pass the
/// last two. The **second** assertion is what stops the collapsed fix: a
/// build that simply never ACKs satisfies `<= 1` for free, and is caught
/// here rather than only in the tail tests, because a bound the degenerate
/// implementation meets asserts nothing.
/// The **literal** reading of "one receive-drain": [`BURST`] datagrams fed
/// to the core before a single `poll_output()` loop runs.
///
/// This sequence is the one §16.4 forbids a driver to perform — *"every
/// mutating call … is followed by draining `poll_output()`"* — so it is
/// unreachable from any conforming shell, and no fixture helper expresses
/// it. It is written because ruling 271's wording is "one receive-drain"
/// and someone has to state what that is; the conflict is reported in
/// `AUTHOR-ACK-REPORT.md` §C1 rather than resolved here (working rule 3).
///
/// Separating mutation: none that
/// [`a_burst_of_twenty_in_one_receive_batch_emits_at_most_one_ack`] does
/// not already catch — a debt latch consumed once per drain satisfies this
/// for free. This doc once continued *"if this passes at base it pins
/// nothing … not as a gate"*; the recorded matrices settled it the other
/// way (`AUTHOR-ACK-REPORT.md`, `MUTATION-271-REPORT.md`): red at base
/// before the fix, red under the `ACK_COALESCE_MAX` → 2 revert after it.
/// It **is** a working gate for the drain-scoped reading, kept as one —
/// author conflict C1, closed in ruling 271's record.
// ═══════════════════════════════════════════════════════════════════════
// 2 — MAX_ACK_DELAY is the outer bound
// ═══════════════════════════════════════════════════════════════════════
/// One ack-eliciting datagram, then silence: the ACK leaves within
/// `MAX_ACK_DELAY`.
///
/// §12.4: the `AckDelay` timer is *"armed at `MAX_ACK_DELAY` on receipt of
/// the first unacknowledged ack-eliciting packet"*, and firing it owes the
/// ACK. Nothing in ruling 271 touches that clause, and it is the clause
/// that makes coalescing safe: the peer's §13.1 estimator and §13.2 loss
/// detection never wait longer than 25 ms for feedback, however quiet the
/// receiver's own drain schedule becomes.
///
/// A warm-up packet precedes it so the packet under test is not the
/// session's first — §12.4's vacuous gap rule would ACK that one
/// immediately and the timer would never be exercised.
///
/// **Expected GREEN at base, and must stay green.** Separating mutations:
/// a coalescing fix that emits an ACK *only when a subsequent drain
/// happens* never emits here at all — there is no subsequent drain — and
/// `idle_until_acked` returns `None`, failing the `expect`. A fix that
/// arms the timer at `2 · MAX_ACK_DELAY`, or at `KEEPALIVE_TIMEOUT`, fails
/// the bound instead; the two are asserted separately so the failure
/// message says which. The bound is an inequality against the *deadline*
/// `at + MAX_ACK_DELAY`, not "some ACK eventually arrives" — the latter is
/// satisfied by a build that waits a PTO.
/// The **tail of a burst**: after [`BURST`] + 1 datagrams arrive at one
/// instant and the peer goes quiet, the largest counter is still
/// acknowledged within `MAX_ACK_DELAY`.
///
/// An odd count is deliberate. At base §12.4's every-2nd trigger fires on
/// the even arrivals, so the final packet is *"the first unacknowledged
/// ack-eliciting packet"* and only the timer can discharge it — the base
/// core walks the same timer path the ratified cadence will walk for the
/// whole burst. Under ruling 271 the single coalesced ACK may already
/// cover the largest, which is why the check is a disjunction rather than
/// an assertion that the timer fired.
///
/// **Expected GREEN at base, and must stay green.** Separating mutation: a
/// fix that coalesces by dropping the `AckDelay` arming leaves the tail of
/// every burst unacknowledged until the next inbound packet — which, at
/// the end of a transfer, is never. On a bulk send that is the last window
/// of data, and the sender recovers it by PTO
/// (`srtt + max(4·rttvar, K_GRANULARITY) + MAX_ACK_DELAY`, §13.3) instead
/// of by ACK. This test is that regression.
// ═══════════════════════════════════════════════════════════════════════
// 3 — immediate on gap, mid-batch
// ═══════════════════════════════════════════════════════════════════════
/// A gap opening in the middle of a receive batch is ACKed **in that
/// batch**, not deferred to its end.
///
/// §12.4's second bullet, unchanged by ruling 271: *"An ACK is owed
/// **immediately** on out-of-order arrival: an ack-eliciting packet whose
/// counter is not exactly one greater than the window's previous greatest
/// (it opens, fills, or sits inside a gap)."* This is the peer's fast path
/// to §13.2 — the gap **is** the loss signal, and the packet threshold
/// (`K_PACKET_THRESHOLD` = 3) can only fire on an ACK that reports it.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutations: a fix that folds the gap into the end-of-batch
/// ACK emits nothing at this delivery and fails the `unwrap_or_else`. A
/// fix that reacts to the gap but reports a contiguous `0..=8` — the
/// tempting simplification once ranges are being coalesced — passes that
/// and fails the block assertion, having told the sender that two packets
/// it must retransmit arrived.
///
/// **This test deliberately does not bound how many ACKs the batch
/// emitted.** Under ruling 271 the gap rule is the only thing that can
/// produce a second emission from one batch, so bounding the count would
/// be this author deciding whether "at most one per drain" admits the gap
/// exception — reported as `AUTHOR-ACK-REPORT.md` §C2 instead (working
/// rule 3).
// ═══════════════════════════════════════════════════════════════════════
// 4 — loss detection survives the cadence
// ═══════════════════════════════════════════════════════════════════════
/// A coalesced ACK carries **every** range a per-packet ACK would.
///
/// The content half of the recovery obligation. §12.2 derives the ACK from
/// the replay window's snapshot — *"the single received-packet record;
/// there is no second tracker"* — so coalescing may change *when* an ACK
/// leaves and must not change *what it says*. §12.5's sender intersects
/// the ranges with its in-flight set, so a range the receiver drops is a
/// packet the sender keeps believing lost, or believes arrived when it did
/// not.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutations, and the first is the one a naive coalescing patch
/// actually commits: collapsing the batch to `largest..=largest` — "we saw
/// everything up to here" — reports `[9..=9]` and silently claims counters
/// 0..=3 were never received, so the sender retransmits four packets that
/// arrived. Second: reporting the contiguous `0..=9`, which hides the real
/// loss at 4..=5 and starves §13.2 of the very gap it needs. Only the
/// exact block list separates both; "the ACK mentions 9" separates
/// neither.
/// A single lost packet is recovered **without the clock advancing**.
///
/// The latency half of the recovery obligation, stated so that no timer
/// value has to be reproduced here. §13.2's packet threshold declares a
/// packet lost as soon as *"a later packet in its space has been
/// acknowledged"* and it is `K_PACKET_THRESHOLD` = 3 or more counters
/// below the largest acknowledged — no waiting. The loss opens a gap in
/// the receiver's window, §12.4's second bullet ACKs that gap immediately,
/// and the sender retransmits in the same instant. So on a paused clock
/// the whole repair completes at `t`, and a build that needs virtual time
/// to advance is a build whose feedback arrived late.
///
/// `Pair::pump` moves datagrams both ways at one instant until the wire is
/// quiet, so the transfer either completes at `t` or does not complete.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutations: a fix that defers the gap ACK to the `AckDelay`
/// timer leaves B's report of the hole 25 ms away, `pump(t)` never carries
/// it, and A recovers only by PTO — `read_all` returns short and this
/// fails. A fix that coalesces the *ranges* into a contiguous block hides
/// the hole entirely, A never retransmits, and it fails the same way. A
/// fix that starves ACKs enough to stall §14.5's admission gate fails at
/// the same assertion. The test is stated as a completion, not as a
/// retransmission count, precisely so that it cannot be satisfied by a
/// build that retransmits the wrong packet.
// ═══════════════════════════════════════════════════════════════════════
// 5 — RTT sampling survives
// ═══════════════════════════════════════════════════════════════════════
/// The estimator still gets a sample across a coalesced exchange, and the
/// sample is bounded by the real RTT plus `MAX_ACK_DELAY`.
///
/// §13.1: *"An ACK yields an RTT sample when its `largest` is newly
/// acknowledged"*, and *"Before any sample the estimator seeds from
/// `K_INITIAL_RTT`"* = 333 ms. Coalescing reduces how many ACKs arrive; it
/// must not reduce them to zero, and the delay it adds is exactly what
/// §12.3's `ack_delay` and §13.1's capped subtraction exist to absorb.
///
/// The exchange injects a 20 ms one-way delay by handing each direction's
/// datagrams over at a later instant — the sans-io equivalent of
/// `FlakyPolicy::with_delay`, and cheaper.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutations. `has_sample()` fails a fix that emits no ACK at
/// all in this window, even after its own timer — the collapsed cadence.
/// The **lower** bound `min_rtt >= RTT` fails a build that samples but
/// computes zero (a `now`-minus-`now` bug, which every "an RTT exists"
/// assertion passes). The **upper** bound `min_rtt <= RTT + MAX_ACK_DELAY`
/// is the cadence statement proper: it fails a fix whose ACK waited for a
/// PTO or for a second burst rather than for §12.4's own timer. And
/// `smoothed_rtt != K_INITIAL_RTT` fails a build that takes the sample and
/// never folds it, which the two `min_rtt` bounds alone would not see.
// ═══════════════════════════════════════════════════════════════════════
// 6 — liveness neutrality (ruling 33)
// ═══════════════════════════════════════════════════════════════════════
/// Emitting a coalesced ACK moves no liveness clock.
///
/// Ruling 33 / §12.4: pure-ACK packets are *"sealed `seal_quiet` (§7.4),
/// are not ack-eliciting …, are never tracked for loss"*. §7.5's death
/// clock is the thing this protects: a send that marks defers the peer's
/// keepalive obligation, and a send that is ack-eliciting arms the death
/// deadline. An ACK does neither, or an ACK-only exchange keeps a
/// half-open session alive forever — SECV5-2's immortal session.
///
/// The emission here is driven by the **timer**, with no inbound packet at
/// that instant, which is the only way to observe the send side alone:
/// §7.4's death deadline is derived from `last_authenticated_recv`, so any
/// test that delivers a packet in order to provoke an ACK moves the very
/// clock it is checking.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutations: a fix that seals the coalesced ACK with `seal`
/// rather than `seal_quiet` — an easy slip when the ACK starts riding
/// packet-building paths it did not before — moves `last_send()` and fails
/// the first assertion. A fix that makes the coalesced ACK ack-eliciting
/// (say by packing a PING beside it to force a prompt response) arms the
/// death deadline and fails the second and third. The three are asserted
/// separately because they fail for different reasons and §14.5's
/// exemption depends on the third.
/// An ACK-only emission cannot push out a death deadline that real traffic
/// armed.
///
/// The second half of ruling 33's neutrality, and the one that is a
/// security property rather than bookkeeping: §7.4's *"The send clock never
/// defers death, it only enables it"*. Once the death deadline is armed,
/// the **receive** clock is what moves it; our own outbound ACKs must not,
/// or a connection whose peer has vanished stays alive as long as it keeps
/// ACKing.
///
/// **Expected GREEN at base, and must stay green.**
///
/// Separating mutation: any fix that arms or re-derives liveness on the
/// coalesced-ACK emission path — including the plausible one of treating
/// "we built a packet" as "we sent, so re-anchor". Equality of
/// `deadline()` and of `last_authenticated_recv()` across the emission is
/// the assertion; "the connection is still alive" would pass every one of
/// those builds.
// ═══════════════════════════════════════════════════════════════════════
// Local fixture helpers
// ═══════════════════════════════════════════════════════════════════════
/// Write every byte of `data` on A's stream `r`.
///
/// No pumping inside the loop, deliberately: the caller of
/// [`a_single_lost_packet_is_recovered_without_advancing_the_clock`] needs
/// A's whole flight sitting in `a_to_b` so it can drop one datagram, and a
/// helper that pumped would have delivered it first.
/// Read everything currently readable on `r`, stopping at FIN or at the
/// first empty read.
///
/// Unlike [`read_exactly`] this never panics on short data — the test
/// asserts the length itself, so a starved build reports how far it got
/// instead of dying inside the helper with a message that names the
/// fixture rather than the defect.