snapdir-cli 1.8.0

snapdir CLI implementation: the library behind the `snapdir` binary (which ships in the `snapdir` crate).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
//! Phase-30 adversarial spec-tests for the live-progress DX fix
//! (gate `dx-progress-spec-tests`, AUTHORING / BLACK-BOX).
//!
//! THE BUG BEING PINNED (operator's headline finding): `snapdir`'s live
//! progress is useless today — the "files" denominator is actually the total
//! BYTE count (e.g. "209715200 files" for a 200 MiB tree), the bar never fills,
//! and the percentage is frozen at 0%. Root cause: the walk's discovery phase
//! is silent and `objects_total` is never set to the FILE COUNT before hashing.
//!
//! THE FIX (NOT yet implemented) must:
//!   1. show a VISIBLE discovery/enumeration phase, then
//!   2. set the real FILE-COUNT total before hashing, so the hash phase renders
//!      a true determinate %/fraction that advances from 0 -> total.
//!
//! These tests are deliberately authored from the SPEC ALONE; the feature's
//! `src/` was NOT read. Clauses 1-3 are EXPECTED TO FAIL against the current
//! binary (progress is broken); clauses 5-7 (snapshot-id determinism + stdout
//! cleanliness) should already largely hold and are written honestly, not
//! weakened.
//!
//! ## How PTY frames are captured
//!
//! The live line renders ONLY when `stderr().is_terminal()` is true. We mirror
//! the `progress_e2e.rs` harness: `libc::openpty` makes a pty pair, the child is
//! spawned with `pre_exec` dup'ing the pty SLAVE onto fd 2 (stderr), so the
//! child sees a real terminal there and renders the live progress line. The
//! parent reads the pty MASTER (a bounded, non-blocking loop so a stuck child
//! can never hang the suite) — those bytes are the rendered stderr frames. We
//! ALSO set `SNAPDIR_PTY_TEST=1` in the child env (the documented hook that
//! forces progress rendering under test); the real pty already satisfies
//! `is_terminal()`, so the env is belt-and-suspenders.
//!
//! Frames are split on the in-place redraw boundaries (`\r` and `\n`), each
//! frame is ANSI-stripped (CSI sequences removed), and we scan the resulting
//! plain text. "Files-not-bytes" is asserted by extracting the numeric
//! denominator of any `done/total` fraction (or the standalone count behind a
//! files-ish label) and proving it is a plausible FILE count (< `100_000`, near
//! the tree's ~2089) and NOT the multi-million BYTE total.
//!
//! ## Env gating (CI-safe)
//!
//! The PTY-rendering tests (clauses 1-3, 6-pty) run only when `SNAPDIR_PTY_TEST`
//! is set in the OUTER environment; unset, they skip-with-note (mirroring
//! `progress_e2e.rs`) so they never hang headless CI. The pure-stdout
//! determinism / cleanliness tests (clauses 5, 7, and the empty-dir exit check)
//! need no pty and run UNCONDITIONALLY.
//!
//! All fixtures live under `assert_fs` temp dirs (hermetic). The 2089-file
//! sandbox tree at `.gatesmith/evidence/dx-sandbox/tree` is used when present
//! (`sh utils/dx/build-sandbox.sh` builds it); otherwise an in-test tree of a
//! few hundred files is built so the suite is self-contained.

// Threshold consts are intentionally declared next to their use inside each test.
#![allow(clippy::items_after_statements)]

use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};

use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use assert_fs::TempDir;

/// The known, frozen snapshot id of the dx-sandbox tree. Pinning this proves the
/// progress changes do NOT perturb the snapshot id (keystone, clause 5).
const SANDBOX_ID: &str = "ce2b0312911e5da75719a3fb3a23922252583a0a1b5c2044cea48ce0dcab8399";

/// The dx-sandbox tree really contains this many files (clause 1 sanity bound).
const SANDBOX_FILE_COUNT: u64 = 2089;

/// A fresh `snapdir` command with the cache pinned so tests never touch the
/// user's real `$HOME/.cache/snapdir`. `TERM` is set so the renderer has a
/// terminal type and `NO_COLOR` is cleared for determinism.
fn snapdir(cache: &Path) -> Command {
    let mut cmd = Command::cargo_bin("snapdir").expect("snapdir binary built");
    cmd.env("SNAPDIR_CACHE_DIR", cache);
    cmd.env_remove("NO_COLOR");
    cmd.env("TERM", "xterm-256color");
    cmd
}

/// Resolve the committed dx-sandbox tree (2089 files). Returns `None` if absent
/// so the caller can fall back to an in-test tree.
fn sandbox_tree() -> Option<PathBuf> {
    // Tests run with CWD = crate dir (crates/snapdir-cli); the sandbox lives at
    // the repo root under .gatesmith/. Walk up to find it.
    let mut dir = std::env::current_dir().ok()?;
    loop {
        let candidate = dir.join(".gatesmith/evidence/dx-sandbox/tree");
        if candidate.join("README.md").is_file() {
            return Some(candidate);
        }
        if !dir.pop() {
            return None;
        }
    }
}

/// Recursively counts the REGULAR files under `root` (the exact set the walk
/// hashes, hence the determinate progress denominator). Symlinks are not
/// followed and directories are not counted — matching `pending.len()` in the
/// walk for these hermetic, symlink-free fixtures.
fn count_regular_files(root: &Path) -> u64 {
    fn rec(dir: &Path, n: &mut u64) {
        let Ok(entries) = std::fs::read_dir(dir) else {
            return;
        };
        for entry in entries.flatten() {
            // Use symlink_metadata so a symlink is never followed/double-counted.
            let Ok(meta) = entry.path().symlink_metadata() else {
                continue;
            };
            let ft = meta.file_type();
            if ft.is_dir() {
                rec(&entry.path(), n);
            } else if ft.is_file() {
                *n += 1;
            }
        }
    }
    let mut n = 0;
    rec(root, &mut n);
    n
}

/// Build a multi-hundred-file tree in `dir` so the suite is self-contained when
/// the committed sandbox is absent. Returns the number of files written.
fn build_many_file_tree(dir: &TempDir) -> u64 {
    let n = 600u64;
    for i in 0..n {
        let sub = i % 12;
        dir.child(format!("d{sub:02}/f{i:05}.bin"))
            .write_str(&"payload-block-".repeat(48))
            .unwrap();
    }
    // a couple of empty files to exercise the 0-byte edge
    dir.child("edge/empty_a.bin").write_str("").unwrap();
    dir.child("edge/empty_b.bin").write_str("").unwrap();
    n + 2
}

/// Run `snapdir <args>` (cache pinned, all stdio piped => progress auto-off),
/// asserting success, returning captured output.
fn run_ok(cache: &Path, args: &[&str]) -> Output {
    let out = snapdir(cache).args(args).output().expect("run snapdir");
    assert!(
        out.status.success(),
        "snapdir {args:?} failed ({:?})\nstderr: {}",
        out.status.code(),
        String::from_utf8_lossy(&out.stderr),
    );
    out
}

/// Trailing-newline-trimmed stdout.
fn stdout_str(out: &Output) -> String {
    String::from_utf8(out.stdout.clone())
        .unwrap()
        .trim_end()
        .to_owned()
}

/// True if `bytes` contains an ANSI ESC (`0x1b`) — the progress line's CSI
/// sequences begin with it; on a scriptable (piped) stream it must be absent.
fn has_ansi(bytes: &[u8]) -> bool {
    bytes.contains(&0x1b)
}

/// True if `bytes` contains a bare CR (`\r`) — the in-place single-line redraw.
fn has_cr(bytes: &[u8]) -> bool {
    bytes.contains(&b'\r')
}

/// Assert `id` is a bare 64-char lowercase-hex snapshot id.
fn assert_is_id(id: &str, label: &str) {
    assert_eq!(id.len(), 64, "{label}: stdout must be a bare id: {id:?}");
    assert!(
        id.chars()
            .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()),
        "{label}: id must be lowercase hex: {id:?}"
    );
}

/// Strip ANSI/CSI escape sequences (`\x1b[...m`, cursor moves, etc.) from a
/// frame, leaving plain text we can scan for numbers and phase words.
fn strip_ansi(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out = String::with_capacity(s.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == 0x1b {
            // ESC: skip an optional '[' then everything up to the final byte in
            // the CSI range 0x40..=0x7e (the command letter).
            i += 1;
            if i < bytes.len() && bytes[i] == b'[' {
                i += 1;
                while i < bytes.len() && !(0x40..=0x7e).contains(&bytes[i]) {
                    i += 1;
                }
                if i < bytes.len() {
                    i += 1; // consume the final command byte
                }
            }
        } else {
            out.push(bytes[i] as char);
            i += 1;
        }
    }
    out
}

/// Split a raw pty capture into the sequence of rendered frames. The live line
/// redraws by overwriting itself with `\r`; phases and the final line end with
/// `\n`. Splitting on BOTH yields the individual frame texts (ANSI-stripped,
/// trimmed). Empty frames are dropped.
fn frames(raw: &[u8]) -> Vec<String> {
    let text = String::from_utf8_lossy(raw);
    text.split(['\r', '\n'])
        .map(strip_ansi)
        .map(|f| f.trim().to_owned())
        .filter(|f| !f.is_empty())
        .collect()
}

/// All run of ASCII digits in `s`, parsed as u64.
fn numbers_in(s: &str) -> Vec<u64> {
    let mut out = Vec::new();
    let mut cur = String::new();
    for ch in s.chars() {
        if ch.is_ascii_digit() {
            cur.push(ch);
        } else if !cur.is_empty() {
            if let Ok(v) = cur.parse::<u64>() {
                out.push(v);
            }
            cur.clear();
        }
    }
    if !cur.is_empty() {
        if let Ok(v) = cur.parse::<u64>() {
            out.push(v);
        }
    }
    out
}

/// Extract candidate FRACTION denominators from a frame: the `total` side of any
/// `done/total` pair (digits, optional spaces, `/`, digits). The fix renders the
/// file-count fraction here.
fn fraction_denominators(frame: &str) -> Vec<u64> {
    let bytes = frame.as_bytes();
    let mut out = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'/' {
            // walk left over the numerator (skip; we only need the denom),
            // walk right over the denominator digits.
            let mut j = i + 1;
            // allow a single space after the slash
            while j < bytes.len() && bytes[j] == b' ' {
                j += 1;
            }
            let start = j;
            while j < bytes.len() && bytes[j].is_ascii_digit() {
                j += 1;
            }
            // require a digit immediately before the slash too (it's a fraction,
            // not a path) — look left skipping one optional space.
            let mut k = i;
            if k > 0 && bytes[k - 1] == b' ' {
                k -= 1;
            }
            let left_is_digit = k > 0 && bytes[k - 1].is_ascii_digit();
            if left_is_digit && j > start {
                if let Ok(v) = frame[start..j].parse::<u64>() {
                    out.push(v);
                }
            }
            i = j.max(i + 1);
        } else {
            i += 1;
        }
    }
    out
}

/// Extract `done/total` pairs (both sides) from a frame.
fn fraction_pairs(frame: &str) -> Vec<(u64, u64)> {
    let bytes = frame.as_bytes();
    let mut out = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'/' {
            // numerator: digits immediately left (allow one space)
            let mut l = i;
            if l > 0 && bytes[l - 1] == b' ' {
                l -= 1;
            }
            let num_end = l;
            while l > 0 && bytes[l - 1].is_ascii_digit() {
                l -= 1;
            }
            let num_start = l;
            // denominator: digits immediately right (allow one space)
            let mut r = i + 1;
            while r < bytes.len() && bytes[r] == b' ' {
                r += 1;
            }
            let den_start = r;
            while r < bytes.len() && bytes[r].is_ascii_digit() {
                r += 1;
            }
            if num_end > num_start && r > den_start {
                if let (Ok(n), Ok(d)) = (
                    frame[num_start..num_end].parse::<u64>(),
                    frame[den_start..r].parse::<u64>(),
                ) {
                    out.push((n, d));
                }
            }
            i = r.max(i + 1);
        } else {
            i += 1;
        }
    }
    out
}

/// Extract the determinate `done/total` pairs that the HASHING line renders as
/// `done/total files` — i.e. only `/`-fractions IMMEDIATELY followed by the
/// `files` label. This deliberately EXCLUDES the concurrency readout
/// (`in_flight/jobs`, e.g. `0/12`) which shares the `n/m` shape but is NOT a
/// files fraction. Used by the impl-revealed clauses that must reason about the
/// real file-count fraction alone.
fn files_fraction_pairs(frame: &str) -> Vec<(u64, u64)> {
    let mut out = Vec::new();
    for (num, den) in fraction_pairs(frame) {
        // Re-find this `num/den` occurrence and require it is followed (after
        // optional spaces) by the word "files". Simplest robust check: scan for
        // the literal "<num>/<den>" then look at what follows.
        let needle = format!("{num}/{den}");
        if let Some(pos) = frame.find(&needle) {
            let after = &frame[pos + needle.len()..];
            let after = after.trim_start();
            if after.starts_with("files") {
                out.push((num, den));
            }
        }
    }
    out
}

/// Extract every `NN%` percentage value in a frame.
fn percents(frame: &str) -> Vec<u64> {
    let bytes = frame.as_bytes();
    let mut out = Vec::new();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            let mut l = i;
            while l > 0 && bytes[l - 1].is_ascii_digit() {
                l -= 1;
            }
            if l < i {
                if let Ok(v) = frame[l..i].parse::<u64>() {
                    out.push(v);
                }
            }
        }
        i += 1;
    }
    out
}

// ---------------------------------------------------------------------------
// PTY harness (mirrors progress_e2e.rs). Spawns `snapdir <args>` with the pty
// SLAVE dup'd onto child stderr (so it renders the live line) and STDOUT on a
// normal pipe; returns (raw_stderr_pty_bytes, child Output). Any *setup* failure
// returns Err so callers can skip-with-note rather than fail/hang.
// ---------------------------------------------------------------------------

#[cfg(unix)]
#[allow(clippy::borrow_as_ptr, clippy::too_many_lines)]
fn run_under_pty(cache: &Path, args: &[&str]) -> Result<(Vec<u8>, Output), String> {
    use std::io::Read;
    use std::os::unix::io::FromRawFd;
    use std::os::unix::process::CommandExt;
    use std::time::{Duration, Instant};

    let mut master: libc::c_int = -1;
    let mut slave: libc::c_int = -1;
    // SAFETY: openpty writes two valid fds; null out-params for name/termios/winsize.
    let rc = unsafe {
        libc::openpty(
            &mut master,
            &mut slave,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
        )
    };
    if rc != 0 {
        return Err(format!(
            "openpty failed: {}",
            std::io::Error::last_os_error()
        ));
    }

    let mut child = {
        let mut cmd = snapdir(cache);
        cmd.args(args);
        cmd.env("TERM", "xterm");
        // Documented under-test hook that forces progress rendering; harmless
        // here since the pty already makes stderr a terminal.
        cmd.env("SNAPDIR_PTY_TEST", "1");
        cmd.stdout(Stdio::piped());
        cmd.stdin(Stdio::null());
        let slave_for_child = slave;
        // SAFETY: in the forked child (pre-exec) we only dup the slave fd onto
        // stderr (fd 2) — async-signal-safe — then return.
        unsafe {
            cmd.pre_exec(move || {
                if libc::dup2(slave_for_child, 2) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        cmd.spawn().map_err(|e| {
            unsafe {
                libc::close(master);
                libc::close(slave);
            }
            format!("spawn under pty: {e}")
        })?
    };

    // Parent drops the slave so the master sees EOF when the child exits.
    // SAFETY: slave is a valid fd owned by the parent here.
    unsafe {
        libc::close(slave);
    }

    // SAFETY: master is a valid fd we own; from_raw_fd takes ownership.
    let mut master_file = unsafe { std::fs::File::from_raw_fd(master) };
    set_nonblocking(master).map_err(|e| format!("set master nonblocking: {e}"))?;

    let mut pty_bytes: Vec<u8> = Vec::new();
    let deadline = Instant::now() + Duration::from_mins(1);
    let mut buf = [0u8; 8192];
    loop {
        match master_file.read(&mut buf) {
            Ok(0) => break,
            Ok(n) => pty_bytes.extend_from_slice(&buf[..n]),
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                if Instant::now() >= deadline {
                    let _ = child.kill();
                    return Err("timeout reading pty master".into());
                }
                std::thread::sleep(Duration::from_millis(5));
            }
            Err(e) if e.raw_os_error() == Some(libc::EIO) => break,
            Err(e) => return Err(format!("read pty master: {e}")),
        }
        if Instant::now() >= deadline {
            let _ = child.kill();
            return Err("timeout draining pty master".into());
        }
    }

    let out = child
        .wait_with_output()
        .map_err(|e| format!("wait for child: {e}"))?;
    Ok((pty_bytes, out))
}

#[cfg(not(unix))]
fn run_under_pty(_cache: &Path, _args: &[&str]) -> Result<(Vec<u8>, Output), String> {
    Err("pty harness is unix-only".into())
}

#[cfg(unix)]
fn set_nonblocking(fd: libc::c_int) -> std::io::Result<()> {
    // SAFETY: fd is a valid open fd; F_GETFL/F_SETFL read/modify its flags.
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
    if flags == -1 {
        return Err(std::io::Error::last_os_error());
    }
    if unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } == -1 {
        return Err(std::io::Error::last_os_error());
    }
    Ok(())
}

/// True when the outer env opted into the (potentially hanging) live pty tests.
/// Unset => skip-with-note, mirroring `progress_e2e.rs`.
fn pty_enabled() -> bool {
    std::env::var_os("SNAPDIR_PTY_TEST").is_some()
}

/// Skip helper: prints a uniform note and signals the caller to early-return.
fn skip_unless_pty(test: &str) -> bool {
    if pty_enabled() {
        return false;
    }
    eprintln!(
        "{test}: SKIP (set SNAPDIR_PTY_TEST=1 to run the live pty progress tests). \
         Determinism/cleanliness clauses 5 & 7 still run headless."
    );
    true
}

/// Prepare a cache + the tree under test. Prefer the committed 2089-file sandbox
/// (and report its true file count); else build an in-test multi-hundred tree in
/// a `TempDir` the caller must keep alive. Returns (cache, `tree_path`, `file_count`,
/// _keepalive).
fn prepare_tree() -> (TempDir, PathBuf, u64, Option<TempDir>) {
    let cache = TempDir::new().unwrap();
    if let Some(tree) = sandbox_tree() {
        (cache, tree, SANDBOX_FILE_COUNT, None)
    } else {
        let dir = TempDir::new().unwrap();
        let n = build_many_file_tree(&dir);
        let path = dir.path().to_path_buf();
        (cache, path, n, Some(dir))
    }
}

// ===========================================================================
// Clause 1 — DENOMINATOR IS FILES, NOT BYTES (the precise bug).
// ===========================================================================

/// SPEC clause 1: during `id`/`stage` of the tree, the progress total/denominator
/// must equal the FILE COUNT (~2089), NOT the byte total (tens of millions).
/// We collect every fraction denominator and every standalone count rendered and
/// require that the dominant "total" is a plausible FILE count (< `100_000`, and in
/// the same order of magnitude as the real file count) — and that NO frame ever
/// presents the byte total (>= `1_000_000`) as the progress denominator/total.
#[test]
fn dx_progress_denominator_is_files_not_bytes() {
    if skip_unless_pty("dx_progress_denominator_is_files_not_bytes") {
        return;
    }
    let (cache, tree, file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_denominator_is_files_not_bytes: SKIP ({reason})");
            return;
        }
    };
    assert!(
        out.status.success(),
        "id under pty must succeed; stderr(pty): {}",
        String::from_utf8_lossy(&pty)
    );

    let fs = frames(&pty);
    assert!(
        !fs.is_empty(),
        "progress must render at least one frame on a pty; captured {} bytes",
        pty.len()
    );

    // The plausible-file-count ceiling: well above any tree we test, far below
    // the byte total of a multi-MB tree.
    const FILE_CEIL: u64 = 100_000;
    // The byte total of the tree is millions; treat any "total" >= this as the
    // smoking-gun bytes-as-files bug.
    const BYTES_FLOOR: u64 = 1_000_000;

    // Collect every fraction denominator across all frames.
    let mut denoms: Vec<u64> = Vec::new();
    for f in &fs {
        denoms.extend(fraction_denominators(f));
    }

    // There MUST be at least one determinate fraction whose denominator is a
    // plausible file count near the real count.
    let plausible_total = denoms.iter().copied().find(|&d| {
        d > 0 && d < FILE_CEIL && d >= file_count / 4 && d <= file_count.saturating_mul(4)
    });
    assert!(
        plausible_total.is_some(),
        "no frame showed a FILE-COUNT denominator near {file_count}; \
         denominators seen = {denoms:?}; frames = {fs:?}"
    );

    // And NO fraction denominator may be the byte total (the exact bug: bytes
    // rendered as the 'files' total).
    let byteish: Vec<u64> = denoms
        .iter()
        .copied()
        .filter(|&d| d >= BYTES_FLOOR)
        .collect();
    assert!(
        byteish.is_empty(),
        "progress denominator must be FILES, not BYTES — saw byte-sized totals \
         {byteish:?} (>= {BYTES_FLOOR}); frames = {fs:?}"
    );

    // Defense-in-depth: a frame that literally labels a byte-sized count as
    // "files" is the headline bug — forbid it outright.
    for f in &fs {
        let lower = f.to_lowercase();
        if lower.contains("file") {
            for n in numbers_in(f) {
                assert!(
                    n < BYTES_FLOOR,
                    "a 'files'-labelled frame shows a byte-sized count {n}: {f:?}"
                );
            }
        }
    }
}

// ===========================================================================
// Clause 2 — DETERMINATE, ADVANCING % (not stuck at 0%, not jump-to-100).
// ===========================================================================

/// SPEC clause 2: the rendered frame stream must show a DETERMINATE indicator
/// that advances — at least one frame with a real `NN%` (NN>0) OR a `done/total`
/// fraction with 0 < done <= total. The current bug is frozen at 0% / shows an
/// indeterminate "N files" only. We also reject a stream that ONLY ever shows 0%
/// or 100% with nothing in between when there are enough files to expect motion.
#[test]
fn dx_progress_determinate_and_advancing() {
    if skip_unless_pty("dx_progress_determinate_and_advancing") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_determinate_and_advancing: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // (a) A real, positive percentage somewhere.
    let positive_pct = fs
        .iter()
        .flat_map(|f| percents(f))
        .any(|p| p > 0 && p <= 100);

    // (b) OR a determinate fraction with done>0 and done<=total.
    let advancing_fraction = fs
        .iter()
        .flat_map(|f| fraction_pairs(f))
        .any(|(done, total)| total > 0 && done > 0 && done <= total);

    assert!(
        positive_pct || advancing_fraction,
        "progress must be DETERMINATE and advancing — no frame showed a positive \
         percentage or a 0<done<=total fraction (the 0%-frozen bug). frames = {fs:?}"
    );

    // (c) Not frozen at exactly 0% for the whole run: if any percentage rendered,
    // at least one must exceed 0.
    let all_pcts: Vec<u64> = fs.iter().flat_map(|f| percents(f)).collect();
    if !all_pcts.is_empty() {
        assert!(
            all_pcts.iter().any(|&p| p > 0),
            "percentage rendered but stayed frozen at 0% throughout: {all_pcts:?}"
        );
    }
}

// ===========================================================================
// Clause 3 — VISIBLE DISCOVERY PHASE.
// ===========================================================================

/// SPEC clause 3: a discovery/enumeration phase must be visible BEFORE hashing
/// completes — either a frame containing a discovery-ish phase word
/// (discover/enumerat/scan/walk/index/count/find), OR a growing file count seen
/// during enumeration. The current bug leaves the long discovery phase silent.
#[test]
fn dx_progress_discovery_phase_is_visible() {
    if skip_unless_pty("dx_progress_discovery_phase_is_visible") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_discovery_phase_is_visible: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // (a) An explicit discovery-ish phase word anywhere in the stream.
    const PHASE_WORDS: &[&str] = &[
        "discover",
        "enumerat",
        "scan",
        "walk",
        "index",
        "count",
        "finding",
        "find files",
    ];
    let phase_word = fs.iter().any(|f| {
        let l = f.to_lowercase();
        PHASE_WORDS.iter().any(|w| l.contains(w))
    });

    // (b) OR a count that grows over the stream (enumeration counting up) before
    // the final determinate hashing fraction appears.
    let counts: Vec<u64> = fs
        .iter()
        .map(|f| numbers_in(f).into_iter().max().unwrap_or(0))
        .collect();
    let growing = counts.windows(2).any(|w| w[1] > w[0]);

    assert!(
        phase_word || growing,
        "no VISIBLE discovery phase — no discovery-ish phase word and no growing \
         enumeration count before hashing. frames = {fs:?}"
    );
}

// ===========================================================================
// Clause 4 — --no-progress and --quiet SUPPRESS ALL PROGRESS (under a pty).
// ===========================================================================

/// SPEC clause 4: under the SAME pty stderr, `id --no-progress <tree>` and
/// `id --quiet <tree>` must emit NO progress frames — no ANSI, no CR redraw —
/// even though stderr IS a terminal. stdout stays exactly the id.
#[test]
fn dx_progress_no_progress_and_quiet_suppress_all() {
    if skip_unless_pty("dx_progress_no_progress_and_quiet_suppress_all") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    for flag in ["--no-progress", "--quiet"] {
        let (pty, out) = match run_under_pty(cache.path(), &["id", flag, &tree_str]) {
            Ok(v) => v,
            Err(reason) => {
                eprintln!("dx_progress_no_progress_and_quiet_suppress_all: SKIP ({reason})");
                return;
            }
        };
        assert!(out.status.success(), "id {flag} under pty must succeed");

        assert!(
            !has_ansi(&pty),
            "id {flag}: progress suppressed => no ANSI on the pty stderr; got {:?}",
            String::from_utf8_lossy(&pty)
        );
        assert!(
            !has_cr(&pty),
            "id {flag}: progress suppressed => no CR redraw on the pty stderr; got {:?}",
            String::from_utf8_lossy(&pty)
        );

        let id = String::from_utf8_lossy(&out.stdout).trim_end().to_owned();
        assert_is_id(&id, &format!("id {flag} stdout"));
    }
}

// ===========================================================================
// Clause 5 — KEYSTONE DETERMINISM (snapshot-id freeze).
// ===========================================================================

/// SPEC clause 5 (KEYSTONE): `id` STDOUT is byte-identical with progress ON vs
/// `--no-progress`, and equals the frozen sandbox id — progress must never
/// perturb the snapshot id, and never leak a fragment into stdout. Progress-ON
/// is exercised via the pty (real terminal stderr); the `--no-progress` baseline
/// over a plain pipe. The stdout comparison is BYTE-EXACT.
#[test]
fn dx_progress_id_stdout_byte_identical_keystone() {
    // Baseline (--no-progress, plain pipes) always runs.
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let baseline = run_ok(cache.path(), &["id", "--no-progress", &tree_str]);
    let baseline_stdout = baseline.stdout.clone();
    let baseline_id = stdout_str(&baseline);
    assert_is_id(&baseline_id, "id --no-progress");

    // Only the committed sandbox tree has the known frozen id; pin it there.
    if sandbox_tree().is_some() {
        assert_eq!(
            baseline_id, SANDBOX_ID,
            "sandbox id must equal the frozen keystone id"
        );
    }

    // Plain (piped) progress-ON run: stdout must still be byte-identical.
    let plain_on = run_ok(cache.path(), &["id", &tree_str]);
    assert_eq!(
        plain_on.stdout, baseline_stdout,
        "id stdout must be byte-identical with vs without --no-progress (piped)"
    );

    // Progress-ON under a pty (stderr is a real terminal => the live line
    // renders): stdout (a normal pipe) must STILL be byte-identical to the
    // baseline — progress is stderr-only and must never touch stdout.
    if !pty_enabled() {
        eprintln!(
            "dx_progress_id_stdout_byte_identical_keystone: pty leg SKIPPED \
             (set SNAPDIR_PTY_TEST=1); piped byte-identity leg ran."
        );
        return;
    }
    let (_pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_id_stdout_byte_identical_keystone: pty leg SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");
    assert_eq!(
        out.stdout, baseline_stdout,
        "id stdout under a TTY stderr must be byte-identical to the --no-progress baseline"
    );
    assert!(
        !has_ansi(&out.stdout) && !has_cr(&out.stdout),
        "stdout must never carry a progress fragment (ANSI/CR) even while the pty renders"
    );
}

/// SPEC clause 5 (KEYSTONE, manifest): `manifest <tree>` STDOUT is byte-identical
/// with progress ON vs `--no-progress` (piped), and under a pty stderr stays
/// byte-identical and ANSI/CR-free. The manifest is large, so a leaked progress
/// fragment would be obvious.
#[test]
fn dx_progress_manifest_stdout_byte_identical_keystone() {
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let baseline = run_ok(cache.path(), &["manifest", "--no-progress", &tree_str]);
    let baseline_stdout = baseline.stdout.clone();
    assert!(
        !baseline_stdout.is_empty(),
        "manifest --no-progress must print a manifest"
    );

    let plain_on = run_ok(cache.path(), &["manifest", &tree_str]);
    assert_eq!(
        plain_on.stdout, baseline_stdout,
        "manifest stdout must be byte-identical with vs without --no-progress (piped)"
    );

    if !pty_enabled() {
        eprintln!(
            "dx_progress_manifest_stdout_byte_identical_keystone: pty leg SKIPPED \
             (set SNAPDIR_PTY_TEST=1); piped byte-identity leg ran."
        );
        return;
    }
    let (_pty, out) = match run_under_pty(cache.path(), &["manifest", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!(
                "dx_progress_manifest_stdout_byte_identical_keystone: pty leg SKIP ({reason})"
            );
            return;
        }
    };
    assert!(out.status.success(), "manifest under pty must succeed");
    assert_eq!(
        out.stdout, baseline_stdout,
        "manifest stdout under a TTY stderr must be byte-identical to the --no-progress baseline"
    );
    assert!(
        !has_ansi(&out.stdout) && !has_cr(&out.stdout),
        "manifest stdout must never carry a progress fragment even while the pty renders"
    );
}

// ===========================================================================
// Clause 6 — EMPTY / ZERO-FILE DIR: no panic, no divide-by-zero.
// ===========================================================================

/// SPEC clause 6: `id <empty-dir>` and a dir of only empty files complete
/// cleanly (exit 0, valid id) WITH progress on — no panic from a 0-total
/// fraction. The piped leg always runs (exit + id check); the pty leg (progress
/// actually rendering over a 0/near-0 total) runs when enabled.
#[test]
fn dx_progress_empty_dir_no_divide_by_zero() {
    let cache = TempDir::new().unwrap();

    // (a) Truly empty directory.
    let empty = TempDir::new().unwrap();
    let empty_str = empty.path().to_string_lossy().into_owned();
    let out = run_ok(cache.path(), &["id", &empty_str]);
    let id = stdout_str(&out);
    assert_is_id(&id, "id empty-dir");

    // (b) Directory of ONLY empty (0-byte) files — zero total bytes, nonzero
    // file count: another way to hit a 0-total fraction in a broken renderer.
    let zeros = TempDir::new().unwrap();
    zeros.child("a").write_str("").unwrap();
    zeros.child("b").write_str("").unwrap();
    zeros.child("sub/c").write_str("").unwrap();
    let zeros_str = zeros.path().to_string_lossy().into_owned();
    let zout = run_ok(cache.path(), &["id", &zeros_str]);
    assert_is_id(&stdout_str(&zout), "id zero-byte-files dir");

    if !pty_enabled() {
        eprintln!(
            "dx_progress_empty_dir_no_divide_by_zero: pty leg SKIPPED (set SNAPDIR_PTY_TEST=1); \
             piped exit/id legs ran."
        );
        return;
    }
    // With progress actively rendering, the empty/zero cases must still exit 0
    // (no panic, no divide-by-zero) and keep stdout id-clean.
    for dir in [&empty_str, &zeros_str] {
        let (pty, out) = match run_under_pty(cache.path(), &["id", dir]) {
            Ok(v) => v,
            Err(reason) => {
                eprintln!("dx_progress_empty_dir_no_divide_by_zero: pty leg SKIP ({reason})");
                return;
            }
        };
        assert!(
            out.status.success(),
            "id {dir} under pty must exit 0 (no panic/divide-by-zero); stderr(pty): {}",
            String::from_utf8_lossy(&pty)
        );
        // A panic would print 'panicked at' to stderr; forbid it.
        let stderr = String::from_utf8_lossy(&pty).to_lowercase();
        assert!(
            !stderr.contains("panic"),
            "id {dir} under pty must not panic; stderr(pty): {stderr}"
        );
        assert_is_id(
            String::from_utf8_lossy(&out.stdout).trim_end(),
            &format!("id {dir} under pty"),
        );
    }
}

// ===========================================================================
// Clause 7 — STDOUT CLEANLINESS (stdout pipe, stderr pty).
// ===========================================================================

/// SPEC clause 7: when stdout is a pipe (redirected) but stderr is the pty, the
/// stdout of `id`/`manifest` contains EXACTLY the id/manifest bytes — never a
/// progress fragment (no ANSI, no CR). This is the precise "progress is
/// stderr-only" contract under a live render. The harness already keeps stdout
/// on a normal pipe while stderr is the pty, so this is a direct check.
#[test]
fn dx_progress_stdout_clean_under_live_render() {
    if skip_unless_pty("dx_progress_stdout_clean_under_live_render") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    // id: stdout must be exactly the bare id + single newline, ANSI/CR-free.
    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_stdout_clean_under_live_render: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");
    let id = String::from_utf8_lossy(&out.stdout).trim_end().to_owned();
    assert_is_id(&id, "id stdout under pty");
    assert_eq!(
        out.stdout,
        format!("{id}\n").into_bytes(),
        "id stdout under a live render must be EXACTLY the id + one newline (no progress fragment)"
    );
    assert!(
        !has_ansi(&out.stdout) && !has_cr(&out.stdout),
        "id stdout must carry no ANSI/CR even though stderr(pty) does ({} pty bytes)",
        pty.len()
    );

    // manifest: stdout must be exactly the manifest bytes, ANSI/CR-free, and
    // byte-identical to the piped --no-progress manifest.
    let baseline = run_ok(cache.path(), &["manifest", "--no-progress", &tree_str]);
    let (_pty2, mout) = match run_under_pty(cache.path(), &["manifest", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_stdout_clean_under_live_render (manifest): SKIP ({reason})");
            return;
        }
    };
    assert!(mout.status.success(), "manifest under pty must succeed");
    assert!(
        !has_ansi(&mout.stdout) && !has_cr(&mout.stdout),
        "manifest stdout must carry no ANSI/CR under a live render"
    );
    assert_eq!(
        mout.stdout, baseline.stdout,
        "manifest stdout under a live render must equal the --no-progress manifest byte-for-byte"
    );
}

// ===========================================================================
// Clause 8 (IMPL-REVEALED) — DETERMINATE DENOMINATOR EQUALS THE EXACT FILE
// COUNT (not merely "< some bound").
// ===========================================================================

/// The renderer (`LineFields::build`) prints the hashing fraction as
/// `NN% done/total files`, where `total = snap.objects_total`, which the walk
/// sets to `pending.len()` — the count of regular files discovered. So the
/// determinate denominator MUST equal the tree's EXACT regular-file count. We
/// count the regular files in the fixture ourselves and assert at least one
/// determinate hash fraction carries that exact denominator. This pins the
/// operator's headline bug to the precise number, not an order-of-magnitude
/// bound.
#[test]
fn dx_progress_denominator_equals_exact_file_count() {
    if skip_unless_pty("dx_progress_denominator_equals_exact_file_count") {
        return;
    }
    let (cache, tree, file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    // Independently count the regular files in the fixture (the walk hashes
    // every regular file; that count is the determinate denominator).
    let counted = count_regular_files(&tree);
    assert_eq!(
        counted, file_count,
        "fixture file-count bookkeeping disagrees: walked-from-helper={file_count}, \
         counted-on-disk={counted}"
    );

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_denominator_equals_exact_file_count: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // Every determinate hash fraction's denominator must be EXACTLY the file
    // count. Collect them; require at least one, and require all of them equal.
    let mut hash_denoms: Vec<u64> = Vec::new();
    for f in &fs {
        // Only the `done/total files` fraction counts — `files_fraction_pairs`
        // excludes the `in_flight/jobs` concurrency readout (e.g. `0/12`) which
        // shares the `n/m` shape. Discovery frames carry an indeterminate
        // "N files" count with no fraction, so they contribute nothing.
        for (_done, total) in files_fraction_pairs(f) {
            hash_denoms.push(total);
        }
    }
    assert!(
        !hash_denoms.is_empty(),
        "no determinate hash fraction rendered; frames = {fs:?}"
    );
    assert!(
        hash_denoms.iter().all(|&d| d == counted),
        "every determinate denominator must equal the EXACT file count {counted}; \
         saw denominators {hash_denoms:?}; frames = {fs:?}"
    );
}

// ===========================================================================
// Clause 9 (IMPL-REVEALED) — DISCOVERY PHASE FRAME PRECEDES THE FIRST
// DETERMINATE HASH FRAME (phase ordering).
// ===========================================================================

/// The walk sets `Phase::Discovering` first, then flips to `Phase::Hashing`
/// only AFTER enumeration sets the total. The synchronous first frame is drawn
/// while still in `Discovering`. So in the rendered stream the first frame that
/// carries the discovery label ("discovering") must appear at an index at or
/// before the first frame that carries a determinate hash fraction (`done/total`
/// with `total>0`). A hashing fraction appearing with NO preceding discovery
/// frame would mean the discovery phase was invisible (the original bug).
#[test]
fn dx_progress_discovery_precedes_first_hash_fraction() {
    if skip_unless_pty("dx_progress_discovery_precedes_first_hash_fraction") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_discovery_precedes_first_hash_fraction: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // Index of the first discovery-labelled frame. The renderer prints the
    // literal phase word "discovering" for `Phase::Discovering`.
    let first_discovery = fs
        .iter()
        .position(|f| f.to_lowercase().contains("discovering"));

    // Index of the first determinate hash `done/total files` fraction frame.
    let first_hash_fraction = fs
        .iter()
        .position(|f| files_fraction_pairs(f).iter().any(|&(_d, t)| t > 0));

    assert!(
        first_discovery.is_some(),
        "no 'discovering' phase frame rendered at all; frames = {fs:?}"
    );
    // If a determinate hash frame rendered (it should), the discovery frame must
    // not come strictly after it.
    if let Some(hash_idx) = first_hash_fraction {
        let disc_idx = first_discovery.unwrap();
        assert!(
            disc_idx <= hash_idx,
            "discovery frame (idx {disc_idx}) must precede or coincide with the first \
             determinate hash fraction (idx {hash_idx}); frames = {fs:?}"
        );
    }
}

// ===========================================================================
// Clause 10 (IMPL-REVEALED) — DONE COUNT IS MONOTONIC AND REACHES 100% / total.
// ===========================================================================

/// The hash pass only ever increments `objects_done`, so across the rendered
/// hash fractions the `done` numerator must be NON-DECREASING, and the final
/// determinate frame must reach `done == total` (100%) — not stall mid-way at,
/// say, 0% (the original frozen bug) or some partial value. We look at the
/// determinate `done/total` pairs in render order.
#[test]
fn dx_progress_done_count_monotonic_and_reaches_total() {
    if skip_unless_pty("dx_progress_done_count_monotonic_and_reaches_total") {
        return;
    }
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();

    let (pty, out) = match run_under_pty(cache.path(), &["id", &tree_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_done_count_monotonic_and_reaches_total: SKIP ({reason})");
            return;
        }
    };
    assert!(out.status.success(), "id under pty must succeed");

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // Ordered determinate (done, total) `done/total files` pairs across the
    // stream (the concurrency `in_flight/jobs` readout is excluded).
    let pairs: Vec<(u64, u64)> = fs
        .iter()
        .flat_map(|f| files_fraction_pairs(f))
        .filter(|&(_d, t)| t > 0)
        .collect();
    assert!(
        !pairs.is_empty(),
        "no determinate hash fraction rendered; frames = {fs:?}"
    );

    // (a) `done` is non-decreasing across frames (hashing only ever advances).
    let mut prev = 0u64;
    for (done, total) in &pairs {
        assert!(
            *done >= prev,
            "hash done-count regressed: {done} after {prev} (total {total}); \
             pairs = {pairs:?}"
        );
        assert!(*done <= *total, "done {done} exceeded total {total}");
        prev = *done;
    }

    // (b) The hash count must climb SUBSTANTIALLY past the start — proving real
    // progress, NOT a bar frozen at/near 0% and NOT a 0→100 jump. We deliberately
    // do NOT assert it reaches `total` (or ≥99%): the renderer's `finish()` CLEARS
    // the final line, so on a fast machine the last ~5% of frames complete AND get
    // cleared before the PTY capture sees them — the exact 2089/2089 (100%) frame
    // is simply not capturable. (Measured across ~10 isolated runs on a fast box:
    // captured max_done lands ~96.6%–98.8% of total, i.e. ~2018–2063 of 2089, so a
    // ≥99% ceiling is environment-flaky.) Half the tree is captured with an
    // enormous margin every single run, so `>= total / 2` reliably proves the
    // climb while never depending on the cleared tail. The exact-completion
    // guarantee is covered by the monotonic-`done` chain in (a) plus core's
    // `objects_done` unit tests; HERE the climb itself is the guarantee.
    let total = pairs[0].1;
    let max_done = pairs.iter().map(|&(d, _)| d).max().unwrap();
    assert!(
        max_done >= total / 2,
        "hash done-count must climb substantially past the start (≥ half of {total}, \
         i.e. real progress not a frozen/near-zero bar); max done seen = {max_done}; \
         pairs = {pairs:?}"
    );

    // (c) The rendered percentage must likewise climb to a clearly-non-trivial
    // level, corroborating (b) via the `NN%` the hashing line prints. Same
    // cleared-final-frame caveat as (b): the exact 100% frame isn't reliably
    // captured, so we assert the percentage reaches well past a low floor (>40)
    // rather than ≥99 — high enough to be impossible for a frozen-at-0% bar, low
    // enough to be captured deterministically across machines.
    let max_pct = fs.iter().flat_map(|f| percents(f)).max();
    if let Some(p) = max_pct {
        assert!(
            p > 40,
            "rendered percentage must climb well past a low floor (>40%, not frozen \
             near 0); max seen = {p}; frames = {fs:?}"
        );
    }
}

// ===========================================================================
// Clause 11 (IMPL-REVEALED) — KEYSTONE: id AND manifest byte-identical across
// progress-on vs --no-progress vs --quiet, and id == the frozen sandbox id.
// ===========================================================================

/// Strengthens the keystone: progress must never perturb output across ALL
/// three modes. For both `id` and `manifest`, stdout must be byte-identical for
/// {default progress-on (piped)} vs {`--no-progress`} vs {`--quiet`}, and the
/// printed id must equal the frozen sandbox id. Piped (no pty) so it runs
/// unconditionally and is a pure stdout-determinism check.
#[test]
fn dx_progress_keystone_three_modes_byte_identical() {
    let (cache, tree, _file_count, _keep) = prepare_tree();
    let tree_str = tree.to_string_lossy().into_owned();
    let is_sandbox = sandbox_tree().is_some();

    // --- id ---
    let id_on = run_ok(cache.path(), &["id", &tree_str]).stdout;
    let id_nop = run_ok(cache.path(), &["id", "--no-progress", &tree_str]).stdout;
    let id_quiet = run_ok(cache.path(), &["id", "--quiet", &tree_str]).stdout;
    assert_eq!(
        id_on, id_nop,
        "id stdout: progress-on must equal --no-progress byte-for-byte"
    );
    assert_eq!(
        id_on, id_quiet,
        "id stdout: progress-on must equal --quiet byte-for-byte"
    );
    let id = String::from_utf8(id_on.clone())
        .unwrap()
        .trim_end()
        .to_owned();
    assert_is_id(&id, "id three-mode");
    if is_sandbox {
        assert_eq!(
            id, SANDBOX_ID,
            "printed id must equal the frozen sandbox id"
        );
    }

    // --- manifest ---
    let man_on = run_ok(cache.path(), &["manifest", &tree_str]).stdout;
    let man_nop = run_ok(cache.path(), &["manifest", "--no-progress", &tree_str]).stdout;
    let man_quiet = run_ok(cache.path(), &["manifest", "--quiet", &tree_str]).stdout;
    assert!(!man_on.is_empty(), "manifest must print a manifest");
    assert_eq!(
        man_on, man_nop,
        "manifest stdout: progress-on must equal --no-progress byte-for-byte"
    );
    assert_eq!(
        man_on, man_quiet,
        "manifest stdout: progress-on must equal --quiet byte-for-byte"
    );
}

// ===========================================================================
// Clause 12 (IMPL-REVEALED) — SINGLE-FILE / TINY TREE: determinate total == 1.
// ===========================================================================

/// A one-regular-file tree must produce a coherent determinate total of exactly
/// 1 (no off-by-one, no 0/0 divide-by-zero). Complements the empty-dir case:
/// here `pending.len() == 1`, so the renderer's hash fraction denominator must
/// be 1 and it must reach `1/1` (100%). Piped legs (exit + id) always run; the
/// determinate-total assertion needs the pty render.
#[test]
fn dx_progress_single_file_tree_total_is_one() {
    let cache = TempDir::new().unwrap();
    let one = TempDir::new().unwrap();
    one.child("only.bin")
        .write_str(&"payload-".repeat(64))
        .unwrap();
    let one_str = one.path().to_string_lossy().into_owned();

    // Sanity: exactly one regular file on disk.
    assert_eq!(
        count_regular_files(one.path()),
        1,
        "single-file fixture must contain exactly one regular file"
    );

    // Piped leg: exits 0 with a valid id.
    let out = run_ok(cache.path(), &["id", &one_str]);
    assert_is_id(&stdout_str(&out), "id single-file");

    if !pty_enabled() {
        eprintln!(
            "dx_progress_single_file_tree_total_is_one: pty leg SKIPPED (set SNAPDIR_PTY_TEST=1); \
             piped exit/id leg ran."
        );
        return;
    }

    let (pty, out) = match run_under_pty(cache.path(), &["id", &one_str]) {
        Ok(v) => v,
        Err(reason) => {
            eprintln!("dx_progress_single_file_tree_total_is_one: pty leg SKIP ({reason})");
            return;
        }
    };
    assert!(
        out.status.success(),
        "id single-file under pty must exit 0; stderr(pty): {}",
        String::from_utf8_lossy(&pty)
    );
    let stderr = String::from_utf8_lossy(&pty).to_lowercase();
    assert!(
        !stderr.contains("panic"),
        "single-file id under pty must not panic; stderr(pty): {stderr}"
    );

    let fs = frames(&pty);
    assert!(!fs.is_empty(), "progress must render frames on a pty");

    // A single-file tree hashes almost instantly, so the determinate hash frame
    // may be cleared before capture — we therefore do NOT require a files
    // fraction to be present. But WHENEVER one is rendered, its denominator must
    // be EXACTLY 1 (no off-by-one, no 0/0). And `done` must never exceed 1.
    let pairs: Vec<(u64, u64)> = fs
        .iter()
        .flat_map(|f| files_fraction_pairs(f))
        .filter(|&(_d, t)| t > 0)
        .collect();
    assert!(
        pairs.iter().all(|&(d, t)| t == 1 && d <= 1),
        "single-file determinate denominator must be exactly 1 with done<=1 \
         (no off-by-one, no divide-by-zero); pairs = {pairs:?}; frames = {fs:?}"
    );
    // Any rendered percentage must be a sane 0..=100 (never a >100 from a 0-total
    // divide). The discovery frame shows no `%`; the hash frame shows 0% or 100%.
    for f in &fs {
        for p in percents(f) {
            assert!(
                p <= 100,
                "single-file percentage out of range ({p}); frame = {f:?}"
            );
        }
    }
}