croft-software 0.2.232

VSCode-style TUI written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
//! The rewind buffer: the last few minutes of terminal output, in memory (#357).
//!
//! Local history covers saves. Nothing covers "what did that command print
//! before I cleared the screen", because the bytes are gone the moment the
//! screen scrolls. This keeps them — bounded, so a runaway `yes` cannot eat
//! the machine — so a scrubber can replay the session backwards.
//!
//! # Why frames and keyframes rather than a byte log
//!
//! Replaying a terminal means replaying its *state*, and a terminal's state
//! is not a function of the last N bytes: a single escape sequence early in
//! the stream can set a scroll region that changes how everything after it
//! renders. Replaying from an arbitrary offset therefore produces a screen
//! that never existed.
//!
//! So the buffer stores two things. **Frames** are the raw output chunks with
//! their timestamps. **Keyframes** are full screen snapshots taken every so
//! often; rewinding to time `t` means finding the newest keyframe at or before
//! `t` and replaying only the frames between it and `t`. That bounds the
//! replay work and makes the reconstruction exact rather than approximate.
//!
//! # The cap is on bytes, not on frames
//!
//! The obvious ring buffer holds N frames, which is the wrong bound: a frame
//! is anything from one byte to a megabyte, so a frame count says nothing
//! about memory. `yes` produces millions of tiny frames and a `cat` of a
//! large file produces a few enormous ones, and a frame-capped buffer is
//! either useless for the first or unbounded for the second. The cap here is
//! on bytes held — each frame's payload and its bookkeeping (below) — and
//! eviction is by age until the total fits.
//!
//! A single frame larger than the whole budget is truncated rather than
//! dropped: losing the tail of one enormous write is recoverable, while
//! silently discarding it would leave the replay showing a gap it cannot
//! explain.
//!
//! Everything held counts against the cap (#694): each frame's slot as well
//! as its payload, and keyframes as well as frames. The charge is the worst
//! case (#735): two slots per frame, since a `VecDeque`'s slot array can be
//! up to twice its length (and is shrunk back whenever eviction leaves it
//! more than that), and the payload's allocation rounded up the way malloc
//! rounds it, never under 32 bytes. What the allocator's own free lists and
//! the keyframe deque's slots cost is not counted. Nothing stores keyframes
//! yet (the reader drops the request until the replay overlay lands), so
//! charging them is a guard for that caller, not a saving today.
//!
//! # The cap is shared, not per pane
//!
//! Every pane's buffer draws on one [`RewindBudget`], split evenly between
//! the panes that are open, so the total is a single figure however many
//! panes there are. It is the `terminal_rewind_mb` setting, off when unset
//! until something can replay the buffer ([`configured_budget_bytes`]).
//!
//! # Why this module never renders a screen itself
//!
//! [`RewindBuffer::push`] takes bytes and returns a bool asking for a
//! keyframe; it never reaches for a terminal. That is deliberate, and it is a
//! locking constraint rather than a taste one. The pane's shared state has a
//! documented lock order — `term` → `clock` → `line_times` — and the reader
//! thread calls `push` from inside the section where it already holds `term`.
//! A buffer that rendered its own keyframe would have to take `term` again
//! there, which is the deadlock. So the buffer asks, and the caller — which
//! already holds the lock, or takes it later during a render — supplies the
//! screen through [`RewindBuffer::push_keyframe`].

use std::collections::VecDeque;
use std::sync::{Arc, Mutex, Weak};

/// The rewind memory ALL panes share when `terminal_rewind_mb` is unset, in
/// bytes: none (#1342).
///
/// Nothing replays the buffer yet (there is no Session: Rewind to show it),
/// so recording by default only held memory nobody could use: up to 128 MiB
/// locally and 32 MiB over SSH, where croft was being OOM-killed (#694). The
/// change that adds the overlay gives this a default again. Until then the
/// recorder runs only for someone who sets `terminal_rewind_mb`, and that
/// figure is split evenly across live panes by [`RewindBudget`].
pub const DEFAULT_BUDGET_BYTES: usize = 0;

/// The largest `terminal_rewind_mb` honoured. A typo of a few extra zeros
/// must not hand the rewind buffer the machine.
pub const MAX_BUDGET_MB: usize = 4096;

/// The shared budget in bytes for a `terminal_rewind_mb` setting.
///
/// Unset is [`DEFAULT_BUDGET_BYTES`] (off); `0` turns rewind off; any other
/// value is megabytes, clamped to [`MAX_BUDGET_MB`].
pub fn configured_budget_bytes(setting_mb: Option<usize>) -> usize {
    match setting_mb {
        None => DEFAULT_BUDGET_BYTES,
        // Saturating: 4096 MiB is exactly 2^32, which wraps to 0 on a 32-bit
        // host and would turn rewind off instead of maximising it.
        Some(mb) => mb.min(MAX_BUDGET_MB).saturating_mul(1024 * 1024),
    }
}

/// How often a keyframe is taken, in bytes of output between snapshots.
///
/// Time would be the intuitive interval, but it is the wrong axis: an idle
/// session would accumulate keyframes of an unchanging screen, while a flood
/// would go a long way between them and make the replay after each one
/// expensive. Bytes track the work the replay will actually have to do.
pub const KEYFRAME_INTERVAL_BYTES: usize = 256 * 1024;

/// One chunk of terminal output, as it arrived.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Frame {
    /// Milliseconds since the buffer started, from a MONOTONIC source.
    ///
    /// The pane passes `Instant::elapsed()` rather than a wall clock for a
    /// reason that binds any other caller too: every read here — `span_ms`,
    /// `replay_from`, the orphan-keyframe sweep — compares these values, so a
    /// clock that steps backwards puts a frame before its predecessors and
    /// out of the range a scrubber would ask for. `span_ms` covers such a
    /// frame rather than reporting an inverted range, but the ordering is
    /// still the caller's to keep.
    pub at_ms: u64,
    /// Position in the buffer's own recording order.
    ///
    /// `at_ms` cannot order records on its own: it comes from a millisecond
    /// clock, and a burst of output produces many records sharing a value.
    /// The sweep below has to know whether a frame recorded AFTER a keyframe
    /// was evicted, which a tie makes unanswerable. This counter is total.
    seq: u64,
    pub data: Vec<u8>,
}

/// A full screen snapshot, so replay never has to start from the beginning.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Keyframe {
    pub at_ms: u64,
    /// Position in the recording order, from the same counter as [`Frame`].
    seq: u64,
    /// The rendered screen at `at_ms`, one entry per row.
    pub screen: Vec<String>,
    /// What this snapshot costs against the budget: [`keyframe_cost`] of
    /// `screen`, computed once so eviction does not re-walk every row.
    cost: usize,
}

/// A keyframe's charge against the byte budget: each row's text plus the
/// `String` header that holds it.
///
/// Counted because a keyframe is a whole screen, and one is asked for every
/// [`KEYFRAME_INTERVAL_BYTES`] of output. Nothing stores one yet; left out of
/// the cap, the replay overlay that will would let ~256 of them ride on top
/// of a full frame budget.
fn keyframe_cost(screen: &[String]) -> usize {
    screen
        .iter()
        .map(|row| row.len() + std::mem::size_of::<String>())
        .sum()
}

/// What one frame's slot costs: two `Frame`s, since the deque's slot array
/// holds up to twice as many slots as frames ([`RewindBuffer::shrink_slots`]
/// keeps it there).
const FRAME_OVERHEAD: usize = 2 * std::mem::size_of::<Frame>();

/// The smallest heap chunk a payload's allocation takes: glibc's minimum,
/// and at least musl's.
const MIN_ALLOCATION: usize = 32;

/// What a payload of `len` bytes costs to allocate: malloc's header and
/// 16-byte rounding, and never under [`MIN_ALLOCATION`].
fn allocation_cost(len: usize) -> usize {
    (len + 16).next_multiple_of(16).max(MIN_ALLOCATION)
}

/// The longest payload whose [`allocation_cost`] fits in `room`, if any.
fn largest_payload(room: usize) -> Option<usize> {
    (room >= MIN_ALLOCATION).then(|| room / 16 * 16 - 16)
}

/// A frame's charge against the byte budget: its payload's allocation plus
/// its slots.
///
/// Payload alone undercounts badly where it matters most (#694 review, and
/// #735): the reader records one frame per `read()`, and output that
/// trickles in a byte at a time costs about 110 real bytes per byte counted.
fn frame_cost(payload: usize) -> usize {
    allocation_cost(payload) + FRAME_OVERHEAD
}

/// The session's recent output, bounded by total bytes held.
#[derive(Debug)]
pub struct RewindBuffer {
    frames: VecDeque<Frame>,
    keyframes: VecDeque<Keyframe>,
    /// [`frame_cost`] of every frame held plus the [`keyframe_cost`] of every
    /// keyframe — the quantity actually capped.
    bytes: usize,
    capacity: usize,
    /// Bytes seen since the last keyframe, driving the next snapshot.
    since_keyframe: usize,
    /// Hands out [`Frame::seq`] / [`Keyframe::seq`]. Shared by both so the
    /// two interleave in one order rather than two.
    next_seq: u64,
}

impl RewindBuffer {
    pub fn new(capacity: usize) -> Self {
        Self {
            frames: VecDeque::new(),
            keyframes: VecDeque::new(),
            bytes: 0,
            capacity,
            since_keyframe: 0,
            next_seq: 0,
        }
    }

    /// Record one chunk of output. Returns `true` when the caller should take
    /// a keyframe — the buffer cannot render a screen itself, so it asks.
    pub fn push(&mut self, at_ms: u64, data: &[u8]) -> bool {
        if self.capacity == 0 {
            return false;
        }
        // An empty write records nothing: a frame with no output replays as
        // nothing, and would still cost its slots and a minimum allocation
        // (`frame_cost(0)`). When `bytes` summed payload alone, it also cost
        // nothing, and repeated empty pushes grew `frames` without bound.
        // The reader cannot emit one today (`Ok(0)` ends its loop), but this
        // is public and the replay half will add callers.
        if data.is_empty() {
            return false;
        }
        // A cap too small to hold even one frame's bookkeeping holds nothing.
        let Some(room) = self
            .capacity
            .checked_sub(FRAME_OVERHEAD)
            .and_then(largest_payload)
            .filter(|&r| r > 0)
        else {
            return false;
        };
        // One frame bigger than the whole budget is truncated to the budget
        // rather than dropped: the replay then shows a shortened write, which
        // is explicable, instead of a hole that looks like lost output.
        let data = if data.len() > room {
            &data[data.len() - room..]
        } else {
            data
        };
        self.bytes += frame_cost(data.len());
        let seq = self.take_seq();
        self.frames.push_back(Frame {
            at_ms,
            seq,
            data: data.to_vec(),
        });
        self.evict();
        // Counts RETAINED bytes: a write truncated to the budget, or one
        // evicted moments later, contributes what the buffer actually holds.
        self.since_keyframe += data.len();
        if self.since_keyframe >= KEYFRAME_INTERVAL_BYTES {
            // Reduced, not zeroed. A single write spanning several intervals
            // would otherwise ask once and discard the surplus, so the next
            // keyframe would come a full interval after a write that had
            // already earned three.
            self.since_keyframe -= KEYFRAME_INTERVAL_BYTES;
            return true;
        }
        false
    }

    /// Store a screen snapshot the caller rendered after [`push`] asked for one.
    ///
    /// The snapshot counts toward the cap like output does, so storing one
    /// can evict the oldest frames. A snapshot bigger than the whole budget
    /// is not stored: making room for it would evict every frame, and a
    /// start point with nothing after it to replay is worth nothing.
    pub fn push_keyframe(&mut self, at_ms: u64, screen: Vec<String>) {
        let cost = keyframe_cost(&screen);
        if self.capacity == 0 || cost > self.capacity {
            return;
        }
        let seq = self.take_seq();
        self.bytes += cost;
        self.keyframes.push_back(Keyframe {
            at_ms,
            seq,
            screen,
            cost,
        });
        // `evict` ends with the orphan sweep, so this both makes room and
        // collapses keyframes taken before any output.
        self.evict();
    }

    /// Change the cap, evicting at once if the buffer now holds too much.
    ///
    /// [`RewindBudget`] calls this when a pane opens or closes, so the shared
    /// budget is re-split without waiting for each pane's next write. A
    /// capacity of 0 empties the buffer entirely.
    pub fn set_capacity(&mut self, capacity: usize) {
        self.capacity = capacity;
        if capacity == 0 {
            // New deques, not `clear`: clearing keeps the slot arrays, and a
            // pane that once held a million frames would keep 40 MB of empty
            // slots after rewind was turned off.
            self.frames = VecDeque::new();
            self.keyframes = VecDeque::new();
            self.bytes = 0;
            self.since_keyframe = 0;
            return;
        }
        self.evict();
        // A smaller share leaves the slot array sized for the old one; give
        // back what is well past what the new share holds.
        self.shrink_slots();
        self.keyframes.shrink_to(self.keyframes.len() * 2);
    }

    /// Keep the frame deque's slot array within twice its length, the most
    /// [`FRAME_OVERHEAD`] pays for. A deque never shrinks by itself, so a
    /// burst of tiny frames that larger ones then evict would otherwise
    /// leave its slots allocated and uncharged. Shrinking to the length
    /// makes the next shrink wait until half of it is evicted, so the cost
    /// stays amortised.
    fn shrink_slots(&mut self) {
        if self.frames.capacity() > 2 * self.frames.len() {
            self.frames.shrink_to(self.frames.len());
        }
    }

    /// The cap currently in force.
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    fn take_seq(&mut self) -> u64 {
        let s = self.next_seq;
        self.next_seq += 1;
        s
    }

    /// Drop the oldest records until everything held fits the cap.
    ///
    /// Frames go first, oldest first, and each one can orphan keyframes that
    /// then go with it. The newest frame is kept while keyframes remain: once
    /// only one frame is left, what still exceeds the cap is keyframes, and
    /// they go oldest first before that last frame does. The last frame goes
    /// only when the cap itself shrank below it ([`Self::set_capacity`]).
    fn evict(&mut self) {
        while self.bytes > self.capacity {
            if self.frames.len() > 1 {
                if let Some(f) = self.frames.pop_front() {
                    self.bytes -= frame_cost(f.data.len());
                }
                self.drop_orphan_keyframes();
            } else if self.pop_keyframe_front() {
                // Loop again: the remaining keyframes may still not fit.
            } else if let Some(f) = self.frames.pop_front() {
                self.bytes -= frame_cost(f.data.len());
            } else {
                // Unreachable while `bytes` is the sum of what is held, but
                // a `while` on a counter that a future change could desync
                // is worth ending rather than spinning.
                self.bytes = 0;
                break;
            }
        }
        self.drop_orphan_keyframes();
        self.shrink_slots();
    }

    /// Drop the oldest keyframe and its cost; false when there was none.
    fn pop_keyframe_front(&mut self) -> bool {
        match self.keyframes.pop_front() {
            Some(k) => {
                self.bytes -= k.cost;
                true
            }
            None => false,
        }
    }

    /// Discard keyframes that can no longer start an exact replay.
    ///
    /// A keyframe is a valid start point only if every frame between it and
    /// the target still survives. Once eviction removes output that followed
    /// a keyframe, replaying from it and applying what remains renders a
    /// screen that never existed — the frames that bridged the gap are gone.
    ///
    /// So a keyframe strictly OLDER than the oldest surviving frame is
    /// dropped, including the last one. An earlier version kept the final
    /// keyframe unconditionally, on the theory that a scrub to the very
    /// start needs something to replay from; that was wrong in the way that
    /// matters, because the thing it kept was precisely a start point with a
    /// hole after it. Having no keyframe is honest — [`replay_from`] then
    /// replays from a blank screen, which is slower and correct.
    fn drop_orphan_keyframes(&mut self) {
        let Some(oldest_seq) = self.frames.front().map(|f| f.seq) else {
            // No frames at all. This is NOT the orphan case: a keyframe
            // taken before any output has arrived is a valid start point for
            // everything that arrives next, and clearing here would drop the
            // screen a fresh pane replays from. Only the newest is worth
            // keeping; the older ones describe screens no surviving frame
            // can reach.
            //
            // Note this arm is NOT reached by frames "ageing out": eviction
            // is byte-capped, not age-based, and stops as soon as the budget
            // is met, while a pushed write is truncated to at most the
            // capacity — so at any nonzero capacity the just-pushed frame
            // always survives and a buffer that has held frames never
            // returns to empty. Nor is it reached by a shrinking cap: `evict`
            // drops every keyframe before it drops the last frame. The one
            // reachable state is before the first output (a zero capacity
            // stores no keyframes at all).
            while self.keyframes.len() > 1 {
                self.pop_keyframe_front();
            }
            return;
        };
        // Keyed on the SEQUENCE, not the timestamp. A keyframe is a valid
        // start point exactly when every frame recorded after it survives —
        // that is, when nothing between it and the oldest surviving frame was
        // evicted. `at_ms` cannot answer that: it cannot tell a keyframe that
        // predates the frames because output was EVICTED from one that
        // predates them because output had not yet ARRIVED, and a burst of
        // same-millisecond records makes even the ordering ambiguous. The
        // counter is total and increments once per record, so the frame
        // immediately after a keyframe has exactly `seq + 1`.
        while self.keyframes.len() > 1 && self.keyframes[1].seq < oldest_seq {
            self.pop_keyframe_front();
        }
        if self
            .keyframes
            .front()
            .is_some_and(|k| k.seq + 1 < oldest_seq)
        {
            self.pop_keyframe_front();
        }
    }

    /// Bytes currently held, keyframes included. Never exceeds the capacity.
    pub fn bytes(&self) -> usize {
        self.bytes
    }

    /// Retained keyframes. Test-only: production reads keyframes through
    /// [`Self::replay_from`], and an unconditional `pub fn` with no caller
    /// would fail the build under `-D warnings` (which implies dead_code).
    #[cfg(test)]
    pub fn keyframe_count(&self) -> usize {
        self.keyframes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.frames.is_empty()
    }

    /// The span the buffer can rewind over, as `(oldest, newest)` in ms.
    ///
    /// Taken as min/max rather than front/back. The pane feeds a monotonic
    /// clock so the two agree there, but `push` is public: a caller using a
    /// wall clock that stepped backwards would otherwise get an INVERTED
    /// span, and a scrubber sizing its timeline from `end - start` on that
    /// would underflow rather than show a short range.
    pub fn span_ms(&self) -> Option<(u64, u64)> {
        let lo = self.frames.iter().map(|f| f.at_ms).min()?;
        let hi = self.frames.iter().map(|f| f.at_ms).max()?;
        Some((lo, hi))
    }

    /// The replay needed to show the screen at `at_ms`: the newest keyframe at
    /// or before it, plus every frame from that keyframe up to and including
    /// `at_ms`.
    ///
    /// Returns no keyframe when none precedes the target — the caller then
    /// replays from a blank screen, which is correct for a buffer whose
    /// keyframes have aged out from under it.
    pub fn replay_from(&self, at_ms: u64) -> (Option<&Keyframe>, Vec<&Frame>) {
        let kf = self.keyframes.iter().rev().find(|k| k.at_ms <= at_ms);
        let start = kf.map(|k| k.at_ms).unwrap_or(0);
        let frames = self
            .frames
            .iter()
            // `>=` on the start: a frame landing in the same millisecond as
            // the keyframe is NOT already in it. The keyframe is rendered
            // from the frames strictly before it, so excluding the tie would
            // drop that output from every replay through this keyframe.
            .filter(|f| f.at_ms >= start && f.at_ms <= at_ms)
            .collect();
        (kf, frames)
    }
}

/// The rewind memory every pane shares, split evenly between live panes.
///
/// Each pane's buffer is capped at `total / live panes`. Opening a pane
/// shrinks everyone's share and trims the buffers already over it BEFORE the
/// new pane records anything, and closing one grows the shares back, so the
/// sum held stays under `total` however many panes come and go. An even
/// split rather than a first-come pool: a pool lets one flooding pane take
/// the whole budget, and a quiet pane would then keep nothing.
///
/// Lock order is budget → buffer, one buffer at a time. The reader thread
/// holds a buffer (under `term`) and never takes the budget, so a rebalance
/// only ever waits on a push, never deadlocks with one.
#[derive(Debug)]
pub struct RewindBudget {
    inner: Mutex<BudgetState>,
}

#[derive(Debug)]
struct BudgetState {
    total: usize,
    panes: Vec<Weak<Mutex<RewindBuffer>>>,
}

impl RewindBudget {
    pub const fn new(total: usize) -> Self {
        Self {
            inner: Mutex::new(BudgetState {
                total,
                panes: Vec::new(),
            }),
        }
    }

    /// A new pane's buffer, sized to its share. Every other pane is trimmed
    /// to the smaller share before this returns.
    pub fn register(&self) -> Arc<Mutex<RewindBuffer>> {
        let mut st = self.lock();
        let buf = Arc::new(Mutex::new(RewindBuffer::new(0)));
        st.panes.push(Arc::downgrade(&buf));
        Self::rebalance(&mut st);
        buf
    }

    /// Return a closing pane's share to the others and free its buffer now,
    /// even if another handle to it (the reader thread's) outlives the call.
    pub fn release(&self, buf: &Arc<Mutex<RewindBuffer>>) {
        let mut st = self.lock();
        let target = Arc::downgrade(buf);
        st.panes.retain(|w| !w.ptr_eq(&target));
        lock_buffer(buf).set_capacity(0);
        Self::rebalance(&mut st);
    }

    /// Change the shared total (a settings edit) and re-split it.
    pub fn set_total(&self, total: usize) {
        let mut st = self.lock();
        st.total = total;
        Self::rebalance(&mut st);
    }

    /// The shared total, in bytes.
    pub fn total(&self) -> usize {
        self.lock().total
    }

    /// Bytes held across every live pane — what the budget actually costs.
    pub fn held_bytes(&self) -> usize {
        self.lock()
            .panes
            .iter()
            .filter_map(Weak::upgrade)
            .map(|b| lock_buffer(&b).bytes())
            .sum()
    }

    /// Live panes drawing on the budget.
    pub fn pane_count(&self) -> usize {
        self.lock()
            .panes
            .iter()
            .filter(|w| w.strong_count() > 0)
            .count()
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, BudgetState> {
        self.inner.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn rebalance(st: &mut BudgetState) {
        // A pane whose buffer is gone without a `release` (a panic in a
        // constructor, say) stops counting here rather than holding a share.
        st.panes.retain(|w| w.strong_count() > 0);
        let live: Vec<_> = st.panes.iter().filter_map(Weak::upgrade).collect();
        let share = st.total / live.len().max(1);
        for buf in live {
            lock_buffer(&buf).set_capacity(share);
        }
    }
}

/// A buffer's lock, poisoned or not. A panic under it leaves the contents
/// intact, and trimming them to the budget is still correct — skipping a
/// poisoned buffer would exempt it from the cap for the pane's life.
fn lock_buffer(buf: &Mutex<RewindBuffer>) -> std::sync::MutexGuard<'_, RewindBuffer> {
    buf.lock().unwrap_or_else(|e| e.into_inner())
}

/// The process-wide budget every terminal pane registers with.
///
/// Starts at the local default; the pane constructor sets the configured
/// total ([`configured_budget_bytes`]) before registering, so a settings
/// edit applies at the next pane without a relaunch.
pub fn budget() -> &'static RewindBudget {
    static BUDGET: RewindBudget = RewindBudget::new(DEFAULT_BUDGET_BYTES);
    &BUDGET
}

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

    /// The cap is on bytes, and it holds under a flood of tiny writes.
    ///
    /// The acceptance criterion from #357: "memory stays under the configured
    /// cap on a `yes`-style flood". A frame-counted buffer passes a test like
    /// this while using unbounded memory, so the assertion is on `bytes()`.
    #[test]
    fn a_flood_of_small_writes_stays_under_the_cap() {
        let mut b = RewindBuffer::new(1024);
        for i in 0..10_000u64 {
            b.push(i, b"y\n");
        }
        assert!(
            b.bytes() <= 1024,
            "the buffer grew past its cap: {} bytes",
            b.bytes()
        );
        // And it did not simply throw everything away.
        assert!(!b.is_empty(), "the buffer evicted everything");
    }

    /// A single write larger than the whole budget is truncated, not dropped.
    ///
    /// Dropping it would leave the replay with a gap where a large `cat`
    /// happened; keeping its tail shows a shortened write, which a reader can
    /// interpret.
    #[test]
    fn a_write_larger_than_the_budget_keeps_its_tail() {
        // Room for one frame of 16 payload bytes.
        let mut b = RewindBuffer::new(frame_cost(16));
        // DISTINGUISHABLE payload: every byte differs, so the assertion can
        // tell the tail from the head. A uniform `[b'x'; 100]` would pass
        // just as happily against a slice taken from the wrong end.
        let big: Vec<u8> = (0u8..100).collect();
        b.push(1, &big);
        assert_eq!(
            b.bytes(),
            frame_cost(16),
            "a huge write must be clamped to the budget"
        );
        assert!(!b.is_empty(), "it must not vanish entirely");

        let (_, frames) = b.replay_from(u64::MAX);
        let kept: Vec<u8> = frames.iter().flat_map(|f| f.data.clone()).collect();
        assert_eq!(
            kept,
            (84u8..100).collect::<Vec<u8>>(),
            "the LAST 16 bytes must survive, not the first: a replay shows \
             what the command most recently printed"
        );
    }

    /// Eviction is oldest-first, so the buffer keeps the RECENT past.
    ///
    /// A rewind scrubber that kept the oldest bytes and dropped the newest
    /// would be exactly backwards: the whole point is the last few minutes.
    #[test]
    fn eviction_drops_the_oldest_output_first() {
        // Room for two 5-byte frames.
        let mut b = RewindBuffer::new(2 * frame_cost(5));
        b.push(1, b"aaaaa");
        b.push(2, b"bbbbb");
        b.push(3, b"ccccc");
        let (_, frames) = b.replay_from(3);
        let kept: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert!(
            !kept.contains(&b"aaaaa".as_slice()),
            "the oldest write should have been evicted: {kept:?}"
        );
        assert!(
            kept.contains(&b"ccccc".as_slice()),
            "the newest write must survive: {kept:?}"
        );
    }

    /// Replay starts from the newest keyframe at or before the target.
    ///
    /// Replaying from the beginning is correct but unboundedly slow; replaying
    /// from an arbitrary offset renders a screen that never existed. The
    /// keyframe is what makes a bounded replay also an exact one.
    #[test]
    fn replay_starts_from_the_newest_keyframe_at_or_before_the_target() {
        let mut b = RewindBuffer::new(1 << 20);
        b.push(10, b"one");
        b.push_keyframe(20, vec![String::from("screen at 20")]);
        b.push(30, b"two");
        b.push_keyframe(40, vec![String::from("screen at 40")]);
        b.push(50, b"three");

        let (kf, frames) = b.replay_from(50);
        assert_eq!(
            kf.map(|k| k.at_ms),
            Some(40),
            "must pick the newest keyframe at or before the target"
        );
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(
            data,
            vec![b"three".as_slice()],
            "only the frames after that keyframe are replayed"
        );

        // Scrubbing further back picks the earlier keyframe.
        let (kf, frames) = b.replay_from(35);
        assert_eq!(kf.map(|k| k.at_ms), Some(20));
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(data, vec![b"two".as_slice()]);

        // The boundary itself: "at or before" must include AT. A search using
        // `<` rather than `<=` survives every other test in this module — it
        // falls back to None and replays from a blank screen, reaching the
        // right result by a slower path — so only an equality case catches it.
        let (kf_eq, frames_eq) = b.replay_from(20);
        assert_eq!(
            kf_eq.map(|k| k.at_ms),
            Some(20),
            "a keyframe exactly AT the target must be selected, not skipped"
        );
        // No frames come back: the range is [start, at_ms] = [20, 20], and
        // "two" landed at 30. The KEYFRAME selection is what this case pins;
        // asserting the frames too would only restate the range filter, which
        // `replay_from(35)` above already covers. (I first asserted ["two"]
        // here by copying that case without re-deriving it for a different
        // target -- the empty result is correct.)
        let data_eq: Vec<&[u8]> = frames_eq.iter().map(|f| f.data.as_slice()).collect();
        assert!(
            data_eq.is_empty(),
            "the replay range is [20, 20]; \"two\" at 30 is outside it: {data_eq:?}"
        );
    }

    /// A frame in the same millisecond as the keyframe is replayed, not skipped.
    ///
    /// The keyframe is rendered from the output strictly before it, so a frame
    /// sharing its timestamp is not yet reflected in that screen. Excluding it
    /// would silently lose that output from every replay through the keyframe
    /// — a wrong screen rather than a slow one.
    #[test]
    fn a_frame_sharing_the_keyframes_timestamp_is_still_replayed() {
        let mut b = RewindBuffer::new(1 << 20);
        b.push_keyframe(20, vec![String::from("screen at 20")]);
        b.push(20, b"same tick");
        let (kf, frames) = b.replay_from(30);
        assert_eq!(kf.map(|k| k.at_ms), Some(20));
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(
            data,
            vec![b"same tick".as_slice()],
            "the co-timestamped frame must not be swallowed by the keyframe"
        );
    }

    /// Before any keyframe, replay reports none and starts from blank.
    #[test]
    fn a_target_before_every_keyframe_replays_from_blank() {
        let mut b = RewindBuffer::new(1 << 20);
        b.push(10, b"early");
        b.push_keyframe(20, vec![String::from("later")]);
        let (kf, frames) = b.replay_from(15);
        assert!(kf.is_none(), "no keyframe precedes the target");
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(data, vec![b"early".as_slice()]);
    }

    /// Keyframes whose frames have aged out do not outlive them.
    ///
    /// A keyframe with no surviving frames after it would let `replay_from`
    /// choose a start point with a gap behind the remaining output, rendering
    /// a screen that never existed. One keyframe at or before the oldest frame
    /// is kept, because a scrub to the very start replays from it.
    #[test]
    fn keyframes_do_not_outlive_the_frames_they_precede() {
        // Room for the three keyframes and two of the frames, so the third
        // frame is what evicts the first. Keyframes count against the cap
        // (#694); a bare 10 bytes would now refuse every screen.
        let kf_bytes = ["ancient", "old", "recent"]
            .iter()
            .map(|s| keyframe_cost(&[String::from(*s)]))
            .sum::<usize>();
        let mut b = RewindBuffer::new(kf_bytes + 2 * frame_cost(5));
        b.push_keyframe(1, vec![String::from("ancient")]);
        b.push(2, b"aaaaa");
        b.push_keyframe(3, vec![String::from("old")]);
        b.push(4, b"bbbbb");
        b.push_keyframe(5, vec![String::from("recent")]);
        // Evicts the frame at 2, so the keyframes at 1 and 3 are stale.
        b.push(6, b"ccccc");

        let (kf, _) = b.replay_from(6);
        assert_eq!(
            kf.map(|k| k.screen[0].as_str()),
            Some("recent"),
            "replay must not start from a keyframe whose frames are gone"
        );
        assert!(
            b.keyframes.len() <= 2,
            "stale keyframes accumulated: {:?}",
            b.keyframes.iter().map(|k| k.at_ms).collect::<Vec<_>>()
        );
    }

    /// Replay never starts from a keyframe with evicted output after it.
    ///
    /// The sweep used to keep the LAST keyframe unconditionally, so a lone
    /// stale one survived however far it sat before the oldest frame. A
    /// backward scrub then replayed that keyframe plus the frames after the
    /// gap, silently rendering a screen that never existed — the precise
    /// failure the sweep exists to prevent, reachable through the public API
    /// alone. Asserted at a MIDDLE target: checking only the newest one
    /// picks the newest keyframe and never sees the gap.
    #[test]
    fn replay_never_starts_from_a_keyframe_with_evicted_frames_after_it() {
        // Room for the keyframe and two 5-byte frames.
        let mut b = RewindBuffer::new(keyframe_cost(&[String::from("t=1")]) + 2 * frame_cost(5));
        b.push_keyframe(1, vec![String::from("t=1")]);
        b.push(2, b"aaaaa");
        b.push(3, b"bbbbb");
        // Evicts the frame at 2, so the keyframe at 1 now has a hole after it.
        b.push(4, b"ccccc");

        let oldest = b.span_ms().expect("frames remain").0;
        let (kf, _) = b.replay_from(4);
        assert!(
            kf.is_none_or(|k| k.at_ms >= oldest),
            "replay starts at keyframe {:?} but the oldest surviving frame is \
             {oldest}: the output between them was evicted",
            kf.map(|k| k.at_ms)
        );

        // POSITIVE CONTROL. The assertion above short-circuits on `None`, so
        // it is satisfied by a buffer that simply never offers a keyframe at
        // all — it proves no BAD start point is returned, not that a good one
        // still is. Without this half, a sweep that discarded every keyframe
        // would pass, which is exactly the regression that reached review.
        let mut b = RewindBuffer::new(1 << 20);
        b.push_keyframe(10, vec![String::from("valid")]);
        b.push(20, b"x");
        b.push(30, b"y");
        let (kf, _) = b.replay_from(30);
        assert_eq!(
            kf.map(|k| k.at_ms),
            Some(10),
            "an unorphaned keyframe must still be offered as a start point"
        );
    }

    /// Eviction inside ONE millisecond still invalidates the keyframe.
    ///
    /// A timestamp cannot see this: the evicted frame, the survivors and the
    /// keyframe all share `at_ms`, so any `<` comparison says the keyframe is
    /// fine while output that followed it has gone. The records carry a
    /// sequence number for exactly this case — bursts of output routinely
    /// land within one millisecond, so this is the common shape, not a
    /// contrived one.
    #[test]
    fn eviction_within_one_millisecond_still_invalidates_the_keyframe() {
        // Room for the keyframe and three 5-byte frames.
        let mut b = RewindBuffer::new(
            keyframe_cost(&[String::from("before any output")]) + 3 * frame_cost(5),
        );
        b.push_keyframe(5, vec![String::from("before any output")]);
        b.push(5, b"aaaaa");
        b.push(5, b"bbbbb");
        b.push(5, b"ccccc");
        // Evicts "aaaaa", which followed the keyframe and is not in it.
        b.push(5, b"ddddd");

        let (kf, frames) = b.replay_from(5);
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert!(
            !data.contains(&b"aaaaa".as_slice()),
            "precondition: aaaaa must have been evicted, or this proves nothing"
        );
        assert!(
            kf.is_none(),
            "the keyframe was kept although output recorded after it was \
             evicted: replaying it plus {data:?} shows a screen that never \
             existed"
        );
    }

    /// Timestamps that go backwards are not silently swallowed.
    ///
    /// Every read assumes `frames` is ordered by `at_ms`. The pane now feeds
    /// a monotonic clock so this cannot arise there, but `push` is public and
    /// a caller with a wall clock would otherwise lose output with no signal:
    /// the frame lands before its predecessors and falls outside both
    /// `span_ms` and every `replay_from` window.
    #[test]
    fn a_backwards_timestamp_is_clamped_rather_than_hidden() {
        let mut b = RewindBuffer::new(1 << 20);
        b.push(1000, b"first");
        b.push(500, b"clock stepped back");
        b.push(1100, b"after");

        let (_, frames) = b.replay_from(u64::MAX);
        let seen: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert!(
            seen.contains(&b"clock stepped back".as_slice()),
            "output recorded across a backward step became unreachable: {seen:?}"
        );
        // The span must COVER every frame, not merely be non-inverted: taken
        // positionally it reports (1000, 1100) while a frame sits at 500,
        // and a scrubber sizing its timeline from that cannot address it.
        let (start, end) = b.span_ms().expect("frames remain");
        assert_eq!(
            (start, end),
            (500, 1100),
            "the span must cover the earliest and latest frame, not the first \
             and last recorded"
        );
    }

    /// A write spanning several keyframe intervals does not lose the surplus.
    #[test]
    fn a_multi_interval_write_keeps_its_remainder() {
        let mut b = RewindBuffer::new(1 << 30);
        assert!(
            b.push(1, &[b'x'; KEYFRAME_INTERVAL_BYTES * 3]),
            "three intervals in one write must ask for a keyframe"
        );
        // The surplus carries: two intervals' worth is still outstanding, so
        // two more requests are owed before the count restarts.
        assert!(
            b.push(2, b"x"),
            "the remainder from the big write was discarded"
        );
        assert!(b.push(3, b"x"), "the second owed keyframe was discarded");
        // Negative control: three intervals owe exactly three, not more.
        assert!(
            !b.push(4, b"x"),
            "a fourth keyframe was requested for three intervals of output"
        );
    }

    /// SEVERAL adjacent stale keyframes are all swept, not just the last.
    ///
    /// The trailing `if` in `drop_orphan_keyframes` removes one stale
    /// keyframe; the `while` above it is what removes a RUN of them. Nothing
    /// else in this module builds that state, so deleting the loop leaves
    /// every other test green while orphans accumulate — measured: a brute
    /// force over 16384 push/keyframe interleavings retains 7900 of them
    /// without it.
    ///
    /// Several keyframes taken back to back with no frames between them is
    /// the ordinary shape here: the reader asks for a keyframe when a write
    /// crosses the interval, and a burst of large writes asks repeatedly
    /// before the next frame lands.
    #[test]
    fn a_run_of_stale_keyframes_is_swept_not_just_the_newest() {
        // Room for the three keyframes and two 8-byte frames, so the third
        // frame evicts the first.
        let kf_bytes = 3 * keyframe_cost(&[String::from("s1")]);
        let mut b = RewindBuffer::new(kf_bytes + 2 * frame_cost(8));
        b.push(10, b"aaaaaaaa");
        // Three keyframes back to back, all describing screens that only the
        // first frame can reach.
        b.push_keyframe(11, vec![String::from("s1")]);
        b.push_keyframe(12, vec![String::from("s2")]);
        b.push_keyframe(13, vec![String::from("s3")]);
        b.push(20, b"bbbbbbbb");
        b.push(30, b"cccccccc");

        // PRESENCE half, so the count assertion cannot pass over an empty
        // buffer: the surviving frames are the two most recent writes.
        let (kf, frames) = b.replay_from(u64::MAX);
        let data: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(
            data,
            vec![b"bbbbbbbb".as_slice(), b"cccccccc".as_slice()],
            "the two newest writes must survive in the 16-byte budget"
        );
        // Every keyframe predates the oldest surviving frame, so none is a
        // valid start point and at most one may be retained.
        assert!(
            b.keyframe_count() <= 1,
            "a run of stale keyframes was left behind: {} retained",
            b.keyframe_count()
        );
        // And the one that may remain must not claim to describe a screen
        // reachable from the surviving frames.
        if let Some(k) = kf {
            assert!(
                k.seq + 1 >= frames[0].seq,
                "retained keyframe seq {} is orphaned before frame seq {}",
                k.seq,
                frames[0].seq
            );
        }
    }

    /// With NO surviving frames, keyframes still collapse to the newest.
    ///
    /// `drop_orphan_keyframes` has two arms, and the sibling test above only
    /// covers the frames-present one. This is the `else` arm, reached when no
    /// frame survives: the collapse loop there is the ONLY thing bounding the
    /// keyframe deque, and deleting it leaves the whole rewind suite green
    /// while keyframes grow without limit — each holding a full screen.
    ///
    /// Its one reachable state is a keyframe taken before any output has
    /// arrived, at an ordinary capacity. Frames "ageing out" does NOT reach
    /// it — eviction is byte-capped rather than age-based and always leaves
    /// the just-pushed frame, so a buffer that has held frames never returns
    /// to empty. A zero capacity used to reach it too; since keyframes count
    /// against the cap (#694) a zero capacity stores none, pinned below.
    #[test]
    fn keyframes_collapse_to_the_newest_when_no_frames_survive() {
        let mut b = RewindBuffer::new(1 << 20);
        for i in 0..5u64 {
            b.push_keyframe(i, vec![format!("pre{i}")]);
        }
        assert!(
            b.is_empty(),
            "precondition: no output has arrived, so there are no frames"
        );
        assert_eq!(
            b.keyframe_count(),
            1,
            "keyframes must collapse to the newest when no frame survives"
        );
        // PRESENCE half: pin WHICH keyframe survived. A count of 1 alone
        // cannot tell "kept the newest" from "kept the oldest" — the two are
        // indistinguishable by count, and only the newest is a valid start
        // point for output arriving next.
        let (kf, _) = b.replay_from(u64::MAX);
        assert_eq!(
            kf.map(|k| k.screen.clone()),
            Some(vec![String::from("pre4")]),
            "the newest pre-output keyframe must be the one retained"
        );
        assert_eq!(
            b.bytes(),
            keyframe_cost(&[String::from("pre4")]),
            "the collapsed keyframes must give their bytes back"
        );

        // Capacity 0 holds nothing, keyframes included: a disabled buffer
        // that still kept a screen would be neither off nor useful.
        let mut b = RewindBuffer::new(0);
        for i in 0..5u64 {
            b.push(i, b"never retained");
            b.push_keyframe(i, vec![format!("k{i}")]);
        }
        assert!(b.is_empty());
        assert_eq!(b.keyframe_count(), 0);
        assert_eq!(b.bytes(), 0);
    }

    /// Empty writes must not accumulate frames the cap can never evict.
    ///
    /// When `bytes` summed payload lengths alone, a zero-length write added
    /// a `Frame` while adding nothing to the figure that triggers eviction,
    /// so repeated empty pushes grew `frames` without bound, in a buffer
    /// whose entire purpose is to be bounded. Every frame is now charged its
    /// slots too, but an empty one is still refused: it records nothing. The reader cannot emit one today (`Ok(0) => break` ends the
    /// loop), but `push` is public and the replay half will add callers.
    #[test]
    fn empty_writes_do_not_accumulate_unevictable_frames() {
        let mut b = RewindBuffer::new(1 << 20);
        for _ in 0..10_000 {
            b.push(1, b"");
        }
        assert!(
            b.is_empty(),
            "empty writes must record nothing at all, not unevictable frames"
        );
        assert_eq!(b.bytes(), 0);

        // PRESENCE half: a real write still records, so the guard rejects
        // only the empty case rather than everything.
        b.push(2, b"real");
        let (_, frames) = b.replay_from(u64::MAX);
        let kept: Vec<&[u8]> = frames.iter().map(|f| f.data.as_slice()).collect();
        assert_eq!(
            kept,
            vec![b"real".as_slice()],
            "a non-empty write must still be recorded"
        );
    }

    /// A zero capacity records nothing rather than panicking.
    #[test]
    fn a_zero_capacity_buffer_records_nothing() {
        let mut b = RewindBuffer::new(0);
        assert!(!b.push(1, b"anything"));
        assert!(b.is_empty());
        assert_eq!(b.bytes(), 0);
        assert_eq!(b.span_ms(), None);
    }

    /// The span is the range a scrubber can address.
    #[test]
    fn the_span_reports_the_rewindable_range() {
        let mut b = RewindBuffer::new(1 << 20);
        assert_eq!(b.span_ms(), None, "an empty buffer spans nothing");
        b.push(100, b"a");
        b.push(900, b"b");
        assert_eq!(b.span_ms(), Some((100, 900)));
    }

    /// The keyframe request fires on bytes, not on frame count.
    ///
    /// Asserted through the public signal rather than the private counter, so
    /// the test still means something if the accounting changes.
    #[test]
    fn a_keyframe_is_requested_once_the_byte_interval_is_passed() {
        let mut b = RewindBuffer::new(1 << 30);
        let chunk = vec![b'x'; 1024];
        let mut asked = 0;
        // Just over one interval's worth of output.
        for i in 0..(KEYFRAME_INTERVAL_BYTES / 1024) as u64 + 1 {
            if b.push(i, &chunk) {
                asked += 1;
            }
        }
        assert_eq!(asked, 1, "exactly one keyframe should have been requested");

        // Many small writes totalling less than an interval ask for none.
        let mut b = RewindBuffer::new(1 << 30);
        let mut asked = 0;
        for i in 0..1000u64 {
            if b.push(i, b"y\n") {
                asked += 1;
            }
        }
        assert_eq!(asked, 0, "2 KB of output must not trigger a keyframe");

        // The boundary is `>=`, so a write landing EXACTLY on the interval
        // asks. Mutating it to `>` survives every other test here.
        let mut b2 = RewindBuffer::new(1 << 20);
        assert!(
            b2.push(1, &vec![b'x'; KEYFRAME_INTERVAL_BYTES]),
            "a write landing exactly on the interval must ask for a keyframe"
        );
    }

    /// What the buffer holds, summed from the records themselves, to check
    /// the running `bytes` counter against.
    fn recount(b: &RewindBuffer) -> usize {
        b.frames
            .iter()
            .map(|f| frame_cost(f.data.len()))
            .sum::<usize>()
            + b.keyframes.iter().map(|k| k.cost).sum::<usize>()
    }

    fn screen(rows: usize, width: usize) -> Vec<String> {
        vec!["x".repeat(width); rows]
    }

    /// #694: keyframes count toward the cap, so they cannot pile up on top
    /// of a full frame budget.
    ///
    /// The first version capped frame payloads only. Pushing a screen per
    /// interval then grew memory by a whole screen each time while `bytes()`
    /// reported the buffer within its cap.
    #[test]
    fn keyframes_count_toward_the_cap() {
        let cap = 64 * 1024;
        let mut b = RewindBuffer::new(cap);
        for i in 0..2_000u64 {
            b.push(i, &[b'y'; 512]);
            // A 24-row, 80-column screen: about 2.5 KB with row headers.
            b.push_keyframe(i, screen(24, 80));
        }
        assert!(
            b.bytes() <= cap,
            "frames plus keyframes grew past the cap: {} > {cap}",
            b.bytes()
        );
        assert_eq!(
            b.bytes(),
            recount(&b),
            "the running total drifted from what is actually held"
        );
        // PRESENCE half: the keyframes were counted, not simply refused.
        assert!(b.keyframe_count() > 0, "no keyframe survived at all");
        assert!(
            b.keyframe_count() < 2_000,
            "keyframes accumulated without bound: {}",
            b.keyframe_count()
        );
    }

    /// Storing a keyframe makes room by evicting the OLDEST frames, and the
    /// newest frame survives it.
    #[test]
    fn a_keyframe_evicts_old_output_to_make_room() {
        let kf = screen(2, 10);
        let cap = 3 * frame_cost(10) + keyframe_cost(&kf);
        let mut b = RewindBuffer::new(cap);
        b.push(1, &[b'a'; 10]);
        b.push(2, &[b'b'; 10]);
        b.push(3, &[b'c'; 10]);
        b.push(4, &[b'd'; 10]);
        b.push_keyframe(5, kf);
        assert!(b.bytes() <= cap);
        assert_eq!(b.bytes(), recount(&b));
        assert_eq!(b.keyframe_count(), 1, "the keyframe was stored");
        let kept: Vec<u8> = b.frames.iter().map(|f| f.data[0]).collect();
        assert!(!kept.contains(&b'a'), "the oldest frame must go: {kept:?}");
        assert!(kept.contains(&b'd'), "the newest frame must stay: {kept:?}");
    }

    /// A screen bigger than the whole budget is refused, not stored at the
    /// cost of every frame.
    #[test]
    fn a_keyframe_larger_than_the_budget_is_not_stored() {
        let mut b = RewindBuffer::new(200);
        b.push(1, b"output worth keeping");
        b.push_keyframe(2, screen(10, 80));
        assert_eq!(b.keyframe_count(), 0, "an oversized keyframe was stored");
        let (_, frames) = b.replay_from(u64::MAX);
        assert_eq!(
            frames.len(),
            1,
            "refusing the keyframe must not cost the output it would have evicted"
        );
    }

    /// Lowering the cap trims at once, keyframes included; zero empties it.
    #[test]
    fn set_capacity_trims_immediately_and_zero_empties() {
        let mut b = RewindBuffer::new(1 << 20);
        for i in 0..100u64 {
            b.push(i, &[b'z'; 1000]);
            b.push_keyframe(i, screen(4, 40));
        }
        let before = b.bytes();
        b.set_capacity(10_000);
        assert!(b.bytes() <= 10_000, "{} bytes after shrinking", b.bytes());
        assert!(b.bytes() < before);
        assert_eq!(b.bytes(), recount(&b));
        assert!(!b.is_empty(), "shrinking must keep the recent past");

        // Below a single frame: nothing fits, so nothing is left — and the
        // counter agrees.
        b.set_capacity(10);
        assert_eq!(b.bytes(), recount(&b));
        assert!(b.bytes() <= 10);

        b.set_capacity(0);
        assert!(b.is_empty());
        assert_eq!(b.keyframe_count(), 0);
        assert_eq!(b.bytes(), 0);
        assert!(!b.push(1, b"after"), "a zero cap records nothing");
    }

    /// Fill a buffer from the budget with more than its share.
    fn flood(buf: &Arc<Mutex<RewindBuffer>>, bytes: usize) {
        let mut b = buf.lock().unwrap();
        for i in 0..(bytes / 1024) as u64 {
            b.push(i, &[b'q'; 1024]);
        }
    }

    /// #694: the budget is one figure for every pane, not a per-pane cap.
    ///
    /// Ten flooding panes under a per-pane cap hold ten caps. Under the
    /// shared budget they hold one, and opening another pane trims the
    /// existing ones BEFORE it records, so the sum never overshoots.
    #[test]
    fn the_budget_is_shared_across_panes() {
        let total = 1024 * 1024;
        let budget = RewindBudget::new(total);
        let panes: Vec<_> = (0..10).map(|_| budget.register()).collect();
        for p in &panes {
            flood(p, total);
        }
        assert!(
            budget.held_bytes() <= total,
            "ten panes hold {} against a shared {total}",
            budget.held_bytes()
        );
        // PRESENCE: every pane kept something, so the bound is a split and
        // not one pane starving the rest.
        for p in &panes {
            assert!(!p.lock().unwrap().is_empty(), "a pane was starved");
        }

        // An eleventh pane: the ten are trimmed to the smaller share first.
        let extra = budget.register();
        assert!(budget.held_bytes() <= total);
        flood(&extra, total);
        assert!(
            budget.held_bytes() <= total,
            "the new pane pushed the total over: {}",
            budget.held_bytes()
        );
        assert_eq!(budget.pane_count(), 11);
        assert_eq!(panes[0].lock().unwrap().capacity(), total / 11);
    }

    /// Closing a pane frees its buffer now and returns its share.
    #[test]
    fn releasing_a_pane_frees_it_and_returns_its_share() {
        let total = 1024 * 1024;
        let budget = RewindBudget::new(total);
        let a = budget.register();
        let b = budget.register();
        flood(&a, total);
        assert_eq!(b.lock().unwrap().capacity(), total / 2);

        // The reader thread's clone of `a` can outlive the pane: releasing
        // must empty the buffer rather than wait for the last handle.
        let reader_clone = a.clone();
        budget.release(&a);
        assert!(reader_clone.lock().unwrap().is_empty());
        assert_eq!(reader_clone.lock().unwrap().capacity(), 0);
        assert_eq!(budget.pane_count(), 1);
        assert_eq!(
            b.lock().unwrap().capacity(),
            total,
            "the survivor must get the freed share back"
        );
    }

    /// A settings edit re-splits the new total across open panes.
    #[test]
    fn set_total_resizes_every_pane() {
        let budget = RewindBudget::new(1 << 20);
        let a = budget.register();
        let b = budget.register();
        flood(&a, 1 << 20);
        budget.set_total(4096);
        assert_eq!(budget.total(), 4096);
        assert_eq!(a.lock().unwrap().capacity(), 2048);
        assert_eq!(b.lock().unwrap().capacity(), 2048);
        assert!(budget.held_bytes() <= 4096);
        budget.set_total(0);
        assert_eq!(budget.held_bytes(), 0, "a zero total turns rewind off");
    }

    /// Unset is off until something can show what was recorded (#1342); 0
    /// is off; values are megabytes and clamped.
    #[test]
    fn the_setting_maps_to_a_budget() {
        assert_eq!(configured_budget_bytes(None), 0);
        assert_eq!(configured_budget_bytes(Some(0)), 0);
        assert_eq!(configured_budget_bytes(Some(32)), 32 << 20);
        assert_eq!(
            configured_budget_bytes(Some(usize::MAX)),
            MAX_BUDGET_MB.saturating_mul(1 << 20),
            "an absurd value must clamp, not overflow"
        );
    }

    /// #1342: no Session: Rewind exists to show the buffer, so with no
    /// setting a pane records nothing and holds no memory for it.
    #[test]
    fn with_no_setting_a_pane_records_nothing() {
        let budget = RewindBudget::new(configured_budget_bytes(None));
        let rb = budget.register();
        rb.lock().unwrap().push(1, b"a long build log");
        assert!(rb.lock().unwrap().is_empty());
        assert_eq!(budget.held_bytes(), 0);
    }

    #[test]
    fn an_explicit_setting_still_records() {
        let budget = RewindBudget::new(configured_budget_bytes(Some(64)));
        let rb = budget.register();
        rb.lock().unwrap().push(1, b"a long build log");
        assert!(!rb.lock().unwrap().is_empty());
        assert!(budget.held_bytes() > 0);
    }

    /// #694 review: output that trickles in a byte at a time must not cost
    /// forty times what the cap says.
    ///
    /// Charging payload alone let 4 Mi one-byte frames sit in a 1 MiB buffer
    /// while their slots alone took ~160 MiB. The bound is asserted on the
    /// slots actually held, not on `bytes()`, which is what was wrong.
    #[test]
    fn one_byte_frames_are_charged_their_slot() {
        // #735: what is really held, measured rather than restated from the
        // charging formula: the slot array's CAPACITY, and each payload's
        // allocation at no less than malloc's minimum chunk.
        let held = |b: &RewindBuffer| {
            b.frames.capacity() * std::mem::size_of::<Frame>()
                + b.frames
                    .iter()
                    .map(|f| f.data.capacity().max(MIN_ALLOCATION))
                    .sum::<usize>()
        };
        let cap = 1 << 20;
        let mut b = RewindBuffer::new(cap);
        for i in 0..(4u64 << 20) {
            b.push(i, b".");
            if i % 4096 == 0 {
                assert!(
                    held(&b) <= cap,
                    "{} held against {cap} at push {i}",
                    held(&b)
                );
            }
        }
        assert!(
            held(&b) <= cap,
            "{} frames hold {} bytes against a {cap} cap",
            b.frames.len(),
            held(&b)
        );
        assert_eq!(b.bytes(), recount(&b));
        // PRESENCE: the buffer still keeps the recent past.
        assert!(b.frames.len() > 1000, "only {} frames kept", b.frames.len());

        // Big writes evicting the trickle give its slots back rather than
        // leaving thousands of empty ones uncharged.
        let slots_before = b.frames.capacity();
        for i in 0..8u64 {
            b.push((4 << 20) + i, &vec![b'x'; cap / 8]);
        }
        assert!(
            b.frames.capacity() < slots_before / 100,
            "{}",
            b.frames.capacity()
        );
        assert!(held(&b) <= cap, "{} held after big writes", held(&b));

        // So does a shrinking share, and turning rewind off.
        let mut b = RewindBuffer::new(cap);
        for i in 0..20_000u64 {
            b.push(i, b".");
        }
        let full = b.frames.capacity();
        b.set_capacity(cap / 16);
        assert!(
            b.frames.capacity() <= full / 8,
            "{} of {full}",
            b.frames.capacity()
        );
        assert!(held(&b) <= cap / 16);
        b.set_capacity(0);
        assert_eq!(b.frames.capacity(), 0, "turning rewind off frees the slots");
    }

    /// A cap smaller than one frame's bookkeeping records nothing, rather
    /// than a frame whose payload is truncated to nothing.
    #[test]
    fn a_cap_below_one_frame_records_nothing() {
        // #735: the payload limit is exact — the largest payload whose
        // charge fits, never one byte more.
        for cap in 0..600 {
            let mut b = RewindBuffer::new(cap);
            b.push(1, &[b'x'; 1000]);
            assert!(b.bytes() <= cap, "cap {cap}: charged {}", b.bytes());
            if let Some(len) = b.frames.front().map(|f| f.data.len()) {
                assert!(
                    frame_cost(len + 1) > cap,
                    "cap {cap}: {len} bytes kept, one more fits"
                );
            }
        }
        let mut b = RewindBuffer::new(FRAME_OVERHEAD);
        assert!(!b.push(1, b"x"));
        assert!(b.is_empty());
        assert_eq!(b.bytes(), 0);
    }
}