prview 0.4.0

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

use crate::config::{
    branch_storage_key, current_branch_name, find_repo_root_from, prview_home, repo_name_from_root,
};
use anyhow::{Context, Result, anyhow, bail};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::{BufRead, ErrorKind, Write};
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// RunEntry
// ---------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RunEntry {
    pub id: String,
    pub repo: String,
    pub branch: String,
    pub commit: String,
    pub path: PathBuf,
    pub created_at: String,
    pub quality_pass: bool,
    pub merge_status: String,
    pub policy_mode: String,
    pub checks_passed: usize,
    pub checks_failed: usize,
    pub files_changed: usize,
    pub size_bytes: u64,
    pub has_dashboard: bool,
}

// ---------------------------------------------------------------------------
// RetentionPolicy
// ---------------------------------------------------------------------------

pub struct RetentionPolicy {
    pub max_runs_per_branch: usize,
    pub max_runs_per_repo: usize,
    pub max_total_bytes: u64,
}

impl Default for RetentionPolicy {
    fn default() -> Self {
        Self {
            max_runs_per_branch: 20,
            max_runs_per_repo: 200,
            max_total_bytes: 5 * 1024 * 1024 * 1024, // 5 GB
        }
    }
}

// ---------------------------------------------------------------------------
// RunIndex
// ---------------------------------------------------------------------------

pub struct RunIndex {
    entries: Vec<RunEntry>,
}

fn index_path() -> PathBuf {
    prview_home().join("index.jsonl")
}

fn lock_path() -> PathBuf {
    prview_home().join("index.jsonl.lock")
}

fn resolve_explicit_index_path(path: &Path) -> Result<PathBuf> {
    let parent = path.parent().ok_or_else(|| {
        anyhow!(
            "Index path must include a parent directory: {}",
            path.display()
        )
    })?;
    let file_name = path
        .file_name()
        .ok_or_else(|| anyhow!("Index path must include a file name: {}", path.display()))?;
    crate::paths::resolve_file_name_within(parent, file_name)
}

impl RunIndex {
    /// Load index from `~/.prview/index.jsonl`. Missing/corrupt lines are skipped.
    pub fn load() -> Self {
        let path = index_path();
        let entries = match fs::File::open(&path) {
            Ok(file) => {
                let reader = std::io::BufReader::new(file);
                reader
                    .lines()
                    .map_while(Result::ok)
                    .filter(|line| !line.trim().is_empty())
                    .filter_map(|line| serde_json::from_str::<RunEntry>(&line).ok())
                    .collect()
            }
            Err(_) => Vec::new(),
        };
        Self { entries }
    }

    /// Load index from an explicit path. Missing/corrupt lines are skipped.
    pub fn load_from(path: &Path) -> Self {
        let resolved = match resolve_explicit_index_path(path) {
            Ok(path) => path,
            Err(_) => {
                return Self {
                    entries: Vec::new(),
                };
            }
        };
        let entries = match fs::File::open(&resolved) {
            Ok(file) => {
                let reader = std::io::BufReader::new(file);
                reader
                    .lines()
                    .map_while(Result::ok)
                    .filter(|line| !line.trim().is_empty())
                    .filter_map(|line| serde_json::from_str::<RunEntry>(&line).ok())
                    .collect()
            }
            Err(_) => Vec::new(),
        };
        Self { entries }
    }

    /// Atomic save to an explicit path.
    pub fn save_to(&self, path: &Path) -> Result<()> {
        let resolved = resolve_explicit_index_path(path)?;
        if let Some(parent) = resolved.parent() {
            fs::create_dir_all(parent)?;
        }

        let tmp = resolved.with_extension("jsonl.tmp");
        {
            let mut f = fs::File::create(&tmp)
                .with_context(|| format!("Cannot create {}", tmp.display()))?;
            for entry in &self.entries {
                let line = serde_json::to_string(entry)?;
                writeln!(f, "{}", line)?;
            }
            f.flush()?;
        }
        fs::rename(&tmp, &resolved)?;
        Ok(())
    }

    /// Atomic save: write to tmp file then rename.
    pub fn save(&self) -> Result<()> {
        let path = resolve_explicit_index_path(&index_path())?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let tmp = path.with_extension("jsonl.tmp");
        {
            let mut f = fs::File::create(&tmp)
                .with_context(|| format!("Cannot create {}", tmp.display()))?;
            for entry in &self.entries {
                let line = serde_json::to_string(entry)?;
                writeln!(f, "{}", line)?;
            }
            f.flush()?;
        }
        fs::rename(&tmp, &path)?;
        Ok(())
    }

    pub fn append(&mut self, entry: RunEntry) {
        self.entries.push(entry);
    }

    pub fn entries(&self) -> &[RunEntry] {
        &self.entries
    }

    pub fn list_for_repo(&self, repo: &str) -> Vec<&RunEntry> {
        self.entries.iter().filter(|e| e.repo == repo).collect()
    }

    pub fn list_for_branch(&self, repo: &str, branch: &str) -> Vec<&RunEntry> {
        let branch_keys = branch_lookup_keys(branch);
        self.entries
            .iter()
            .filter(|e| e.repo == repo && branch_keys.contains(&e.branch))
            .collect()
    }

    pub fn latest(&self, repo: &str, branch: &str) -> Option<&RunEntry> {
        let branch_keys = branch_lookup_keys(branch);
        self.entries
            .iter()
            .rev()
            .find(|e| e.repo == repo && branch_keys.contains(&e.branch))
    }

    /// Remove entries whose path no longer exists on disk.
    pub fn remove_stale(&mut self) {
        self.entries.retain(|e| e.path.is_dir());
    }

    /// Rebuild index by scanning `~/.prview/runs/` and parsing `report.json`.
    pub fn rebuild() -> Self {
        let runs_dir = prview_home().join("runs");
        let mut entries = Vec::new();

        if !runs_dir.is_dir() {
            return Self { entries };
        }

        // runs/<repo>/<branch>/<timestamp>/
        let repos = read_subdirs(&runs_dir);
        for repo_dir in repos {
            let repo_name = dir_name(&repo_dir);
            let branches = read_subdirs(&repo_dir);
            for branch_dir in branches {
                let branch_name = dir_name(&branch_dir);
                let runs = read_subdirs(&branch_dir);
                for run_dir in runs {
                    let id = dir_name(&run_dir);
                    // Skip "latest" symlink
                    if id == "latest" {
                        continue;
                    }
                    if let Some(entry) = entry_from_disk(&run_dir, &id, &repo_name, &branch_name) {
                        entries.push(entry);
                    }
                }
            }
        }

        // Sort by created_at ascending
        entries.sort_by(|a, b| a.created_at.cmp(&b.created_at));

        Self { entries }
    }

    /// Prune runs exceeding retention limits.
    ///
    /// Returns paths of run directories to delete. Modifies `self.entries`
    /// to remove pruned entries. The caller is responsible for deleting the
    /// directories and calling `save()`.
    pub fn prune(&mut self, policy: &RetentionPolicy, current_run: &Path) -> Vec<PathBuf> {
        let mut protected: HashSet<PathBuf> = HashSet::new();
        protected.insert(current_run.to_path_buf());

        // Protect all "latest" symlink targets
        for entry in &self.entries {
            if let Some(parent) = entry.path.parent() {
                let latest = parent.join("latest");
                if let Ok(target) = fs::read_link(&latest) {
                    // Symlink is relative (just dirname), resolve against parent
                    let resolved = parent.join(target);
                    protected.insert(resolved);
                }
            }
        }

        let mut to_remove: HashSet<PathBuf> = HashSet::new();

        // 1. Per-branch cap
        let mut branch_groups: HashMap<(String, String), Vec<usize>> = HashMap::new();
        for (i, e) in self.entries.iter().enumerate() {
            branch_groups
                .entry((e.repo.clone(), e.branch.clone()))
                .or_default()
                .push(i);
        }
        for indices in branch_groups.values() {
            if indices.len() > policy.max_runs_per_branch {
                let mut excess = indices.len() - policy.max_runs_per_branch;
                for &idx in indices {
                    if excess == 0 {
                        break;
                    }
                    let path = &self.entries[idx].path;
                    if !protected.contains(path) {
                        to_remove.insert(path.clone());
                        excess -= 1;
                    }
                }
            }
        }

        // 2. Per-repo cap
        let mut repo_groups: HashMap<String, Vec<usize>> = HashMap::new();
        for (i, e) in self.entries.iter().enumerate() {
            if !to_remove.contains(&e.path) {
                repo_groups.entry(e.repo.clone()).or_default().push(i);
            }
        }
        for indices in repo_groups.values() {
            if indices.len() > policy.max_runs_per_repo {
                let mut excess = indices.len() - policy.max_runs_per_repo;
                for &idx in indices {
                    if excess == 0 {
                        break;
                    }
                    let path = &self.entries[idx].path;
                    if !protected.contains(path) {
                        to_remove.insert(path.clone());
                        excess -= 1;
                    }
                }
            }
        }

        // 3. Global size cap
        let remaining: Vec<usize> = self
            .entries
            .iter()
            .enumerate()
            .filter(|(_, e)| !to_remove.contains(&e.path))
            .map(|(i, _)| i)
            .collect();
        let total_size: u64 = remaining.iter().map(|&i| self.entries[i].size_bytes).sum();
        if total_size > policy.max_total_bytes {
            let mut freed: u64 = 0;
            let needed = total_size - policy.max_total_bytes;
            for &idx in &remaining {
                if freed >= needed {
                    break;
                }
                let path = &self.entries[idx].path;
                if !protected.contains(path) {
                    freed += self.entries[idx].size_bytes;
                    to_remove.insert(path.clone());
                }
            }
        }

        self.entries.retain(|e| !to_remove.contains(&e.path));
        to_remove.into_iter().collect()
    }
}

// ---------------------------------------------------------------------------
// File lock
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub struct LockGuard {
    path: PathBuf,
    token: String,
}

impl Drop for LockGuard {
    fn drop(&mut self) {
        if let Ok(content) = fs::read_to_string(&self.path)
            && content.trim() == self.token
        {
            let _ = fs::remove_file(&self.path);
        }
    }
}

/// Acquire a file lock on index.jsonl.lock.
///
/// The lock file is created with `create_new(true)` so acquisition is atomic.
/// A lock is only considered stale when the owning PID is no longer alive.
pub fn acquire_lock() -> Result<LockGuard> {
    acquire_lock_at(&lock_path())
}

/// Acquire a file lock at an explicit path.
pub fn acquire_lock_at(path: &Path) -> Result<LockGuard> {
    let path = path.to_path_buf();
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }

    let token = format!(
        "{}:{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0)
    );

    for _ in 0..3 {
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
        {
            Ok(mut file) => {
                file.write_all(token.as_bytes())?;
                file.flush()?;
                return Ok(LockGuard { path, token });
            }
            Err(err) if err.kind() == ErrorKind::AlreadyExists => {
                let content = fs::read_to_string(&path).unwrap_or_default();
                if lock_is_stale(&content) {
                    // Claim the stale lock atomically instead of removing it in
                    // place. A blind remove is a TOCTOU: between reading the dead
                    // pid and deleting the file, another process can clear the
                    // stale lock and create a fresh live one, which we would then
                    // destroy. Rename is atomic, so only one racer moves the file;
                    // we verify we moved the exact stale file we inspected before
                    // discarding it, and restore it otherwise.
                    let claim = path.with_file_name(format!(
                        "{}.claim.{}",
                        path.file_name()
                            .and_then(|n| n.to_str())
                            .unwrap_or("index.jsonl.lock"),
                        token.replace(':', "-")
                    ));
                    match fs::rename(&path, &claim) {
                        Ok(()) => {
                            let moved = fs::read_to_string(&claim).unwrap_or_default();
                            if moved.trim() == content.trim() {
                                // The file we claimed is the stale one we validated.
                                let _ = fs::remove_file(&claim);
                                continue;
                            }
                            // A newer lock replaced the stale file after our read;
                            // put it back and treat the owner as live.
                            let _ = fs::rename(&claim, &path);
                        }
                        // Another racer already cleared it — retry from scratch.
                        Err(rename_err) if rename_err.kind() == ErrorKind::NotFound => continue,
                        Err(rename_err) => {
                            return Err(rename_err).with_context(|| {
                                format!("Failed to claim stale lock {}", path.display())
                            });
                        }
                    }
                }

                bail!(
                    "Index lock held by another live process ({}). If this is stale, delete {}",
                    content.trim(),
                    path.display()
                );
            }
            Err(err) => {
                return Err(err)
                    .with_context(|| format!("Failed to create lock {}", path.display()));
            }
        }
    }

    bail!(
        "Failed to acquire index lock after clearing stale lock races at {}",
        path.display()
    );
}

fn lock_is_stale(content: &str) -> bool {
    let pid = content
        .trim()
        .split(':')
        .next()
        .and_then(|part| part.parse::<u32>().ok());

    match pid {
        Some(pid) => !is_process_alive(pid),
        None => true,
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Check whether a process is alive via `kill(pid, 0)`.
///
/// Shared with the MCP run-liveness reader (`mcp::read::run_status`) which
/// derives deep-run status deterministically from a pid marker.
pub(crate) fn is_process_alive(pid: u32) -> bool {
    // kill(pid, 0) checks if process exists without sending signal
    let rc = unsafe { libc::kill(pid as libc::pid_t, 0) };
    if rc == 0 {
        return true;
    }

    matches!(
        std::io::Error::last_os_error().raw_os_error(),
        Some(libc::EPERM)
    )
}

fn read_subdirs(dir: &Path) -> Vec<PathBuf> {
    fs::read_dir(dir)
        .ok()
        .map(|rd| {
            rd.filter_map(|e| e.ok())
                .filter(|e| e.path().is_dir())
                .map(|e| e.path())
                .collect()
        })
        .unwrap_or_default()
}

fn dir_name(p: &Path) -> String {
    p.file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown")
        .to_string()
}

fn branch_lookup_keys(branch: &str) -> Vec<String> {
    let primary = branch_storage_key(branch);
    let legacy = branch.replace('/', "-");

    if primary == legacy {
        vec![primary]
    } else {
        vec![primary, legacy]
    }
}

/// Build RunEntry from an existing run directory by reading report.json.
fn entry_from_disk(run_dir: &Path, id: &str, repo: &str, branch: &str) -> Option<RunEntry> {
    let report_path = run_dir.join("report.json");
    let size = dir_size(run_dir);
    let has_dashboard = run_dir.join("dashboard.html").exists();

    // Try to extract data from report.json
    if let Ok(data) = fs::read_to_string(&report_path)
        && let Ok(val) = serde_json::from_str::<serde_json::Value>(&data)
    {
        let commit = val
            .pointer("/meta/range/target/commit")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let quality_pass = val
            .pointer("/gate/quality_pass")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let merge_status = match val
            .pointer("/gate/merge_recommendation")
            .and_then(|v| v.as_str())
        {
            Some("approve") => "ALLOW",
            Some("review_required") => "HOLD",
            Some("block") => "BLOCK",
            _ => val
                .pointer("/gate/verdict")
                .and_then(|v| v.as_str())
                .map(|verdict| match verdict {
                    "PASS" | "ALLOW" => "ALLOW",
                    "HOLD" | "CONDITIONAL" => "HOLD",
                    _ => "BLOCK",
                })
                .unwrap_or("BLOCK"),
        };
        let policy_mode = val
            .pointer("/gate/policy_mode")
            .and_then(|v| v.as_str())
            .unwrap_or("shadow")
            .to_string();
        let checks_passed = val
            .pointer("/checks")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter(|c| c.get("status").and_then(|s| s.as_str()) == Some("PASS"))
                    .count()
            })
            .unwrap_or(0);
        // Count only FAIL/ERROR as failed, matching how a run is registered
        // (Failed|Error). `total - passed` wrongly folded WARN and SKIP into
        // failed, so a rebuild reported a different failed count than the
        // original registration for any run that warned or skipped a check.
        let checks_failed = val
            .pointer("/checks")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter(|c| {
                        matches!(
                            c.get("status").and_then(|s| s.as_str()),
                            Some("FAIL") | Some("ERROR")
                        )
                    })
                    .count()
            })
            .unwrap_or(0);
        let files_changed = val
            .pointer("/diff/stats/files_changed")
            .or_else(|| val.pointer("/diff/files_changed"))
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;
        let created_at = val
            .pointer("/meta/generated_at")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();

        return Some(RunEntry {
            id: id.to_string(),
            repo: repo.to_string(),
            branch: branch.to_string(),
            commit: crate::git::short_sha(&commit).to_string(),
            path: run_dir.to_path_buf(),
            created_at,
            quality_pass,
            merge_status: merge_status.to_string(),
            policy_mode,
            checks_passed,
            checks_failed,
            files_changed,
            size_bytes: size,
            has_dashboard,
        });
    }

    // Fallback: no report.json, use minimal data from filesystem
    let created_at = id_to_iso(id).unwrap_or_default();
    Some(RunEntry {
        id: id.to_string(),
        repo: repo.to_string(),
        branch: branch.to_string(),
        commit: String::new(),
        path: run_dir.to_path_buf(),
        created_at,
        quality_pass: false,
        merge_status: "N/A".to_string(),
        policy_mode: "shadow".to_string(),
        checks_passed: 0,
        checks_failed: 0,
        files_changed: 0,
        size_bytes: size,
        has_dashboard,
    })
}

/// Calculate total directory size in bytes.
fn dir_size(dir: &Path) -> u64 {
    walkdir::WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
        .filter_map(|e| e.metadata().ok())
        .map(|m| m.len())
        .sum()
}

/// Convert timestamp ID like "20260305-022829" to ISO 8601.
fn id_to_iso(id: &str) -> Option<String> {
    if id.len() >= 15 {
        let dt = chrono::NaiveDateTime::parse_from_str(id, "%Y%m%d-%H%M%S").ok()?;
        Some(dt.format("%Y-%m-%dT%H:%M:%S").to_string())
    } else {
        None
    }
}

/// Format bytes as human-readable size (e.g. "1.2M", "942K").
pub fn format_size(bytes: u64) -> String {
    if bytes >= 1_073_741_824 {
        format!("{:.1}G", bytes as f64 / 1_073_741_824.0)
    } else if bytes >= 1_048_576 {
        format!("{:.1}M", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{}K", bytes / 1024)
    } else {
        format!("{}B", bytes)
    }
}

// ---------------------------------------------------------------------------
// Subcommand: `prview runs`
// ---------------------------------------------------------------------------

pub struct RunsOpts {
    pub all: bool,
    pub branch: Option<String>,
    pub status: Option<String>,
    pub json: bool,
    pub rebuild: bool,
}

pub fn run_runs_command(opts: &RunsOpts) -> Result<()> {
    let index = if opts.rebuild {
        eprintln!("Rebuilding index from disk...");
        // Hold the lock across the disk scan AND the save so a concurrent
        // register_and_prune cannot have its freshly appended entry clobbered
        // by the rebuilt snapshot.
        let _lock = acquire_lock()?;
        let idx = RunIndex::rebuild();
        idx.save()?;
        eprintln!("Rebuilt index with {} entries", idx.entries().len());
        idx
    } else {
        // The stale-entry cleanup is a read-modify-write: load, drop entries
        // whose run directory is gone, then save. Serialize the whole cycle
        // under the index lock so it cannot overwrite an entry a concurrent
        // register_and_prune appended between our load and our save
        // (P1: lost-update race on index.jsonl).
        //
        // A pure reader (e.g. the MCP server) needs no lock: save() renames the
        // temp file atomically, so a reader always sees a complete index. When a
        // live writer already holds the lock we skip the opportunistic cleanup
        // and just display what is on disk, rather than blocking or failing a
        // read-oriented command.
        match acquire_lock() {
            Ok(_lock) => {
                let mut idx = RunIndex::load();
                let before = idx.entries().len();
                idx.remove_stale();
                if idx.entries().len() < before {
                    idx.save()?;
                }
                idx
            }
            Err(_) => RunIndex::load(),
        }
    };

    let mut entries: Vec<&RunEntry> = if opts.all {
        index.entries().iter().collect()
    } else {
        // Detect current repo name from cwd
        let repo_name = detect_current_repo_name();
        if let Some(ref branch) = opts.branch {
            index.list_for_branch(&repo_name, branch)
        } else {
            index.list_for_repo(&repo_name)
        }
    };

    // Apply status filter
    if let Some(ref status) = opts.status {
        let want_pass = status.to_lowercase() == "pass" || status.to_lowercase() == "ok";
        entries.retain(|e| e.quality_pass == want_pass);
    }

    if opts.json {
        let json = serde_json::to_string_pretty(&entries)?;
        if let Err(err) = write_json_stdout(&json)
            && err.kind() != std::io::ErrorKind::BrokenPipe
        {
            return Err(err.into());
        }
        return Ok(());
    }

    if entries.is_empty() {
        println!("No runs found.");
        return Ok(());
    }

    // Group by (repo, branch)
    print_runs_table(&entries);

    Ok(())
}

fn write_json_stdout(json: &str) -> std::io::Result<()> {
    use std::io::Write;

    let stdout = std::io::stdout();
    let mut handle = stdout.lock();
    writeln!(handle, "{json}")
}

fn print_runs_table(entries: &[&RunEntry]) {
    use std::collections::BTreeMap;

    let mut groups: BTreeMap<(String, String), Vec<&RunEntry>> = BTreeMap::new();
    for e in entries {
        groups
            .entry((e.repo.clone(), e.branch.clone()))
            .or_default()
            .push(e);
    }

    for ((repo, branch), runs) in &groups {
        println!("{} / {}", repo, branch);
        for run in runs.iter().rev() {
            let status = if run.quality_pass {
                "\u{2713}"
            } else {
                "\u{2717}"
            };
            let status_label = if run.quality_pass { "PASS" } else { "FAIL" };
            let checks_total = run.checks_passed + run.checks_failed;
            println!(
                "  {}  {} {}  {}/{} checks  {} files  {}",
                run.id,
                status,
                status_label,
                run.checks_passed,
                checks_total,
                run.files_changed,
                format_size(run.size_bytes),
            );
        }
        println!();
    }
}

fn detect_current_repo_name() -> String {
    std::env::current_dir()
        .ok()
        .and_then(|cwd| find_repo_root_from(&cwd).ok())
        .map(|repo_root| repo_name_from_root(&repo_root))
        .or_else(|| {
            std::env::current_dir()
                .ok()
                .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
        })
        .unwrap_or_else(|| "unknown".to_string())
}

// ---------------------------------------------------------------------------
// Subcommand: `prview open`
// ---------------------------------------------------------------------------

pub struct OpenOpts {
    pub run_id: Option<String>,
    pub dir_only: bool,
}

pub fn run_open_command(opts: &OpenOpts) -> Result<()> {
    let index = RunIndex::load();
    let repo_name = detect_current_repo_name();
    let branch_name = detect_current_branch();

    let entry = if let Some(ref run_id) = opts.run_id {
        // Find by run ID (across all branches of current repo)
        index
            .entries()
            .iter()
            .find(|e| e.id == *run_id && e.repo == repo_name)
            .or_else(|| {
                // Try all repos
                index.entries().iter().find(|e| e.id == *run_id)
            })
    } else {
        // Latest for current repo+branch
        index.latest(&repo_name, &branch_name)
    };

    let entry = match entry {
        Some(e) => e,
        None => {
            if let Some(ref id) = opts.run_id {
                bail!("Run '{}' not found in index", id);
            }
            bail!(
                "No runs found for {} / {}. Run `prview` first or try `prview runs --all`",
                repo_name,
                branch_name
            );
        }
    };

    let dashboard = entry.path.join("dashboard.html");

    if opts.dir_only {
        println!("{}", entry.path.display());
        return Ok(());
    }

    if !dashboard.exists() {
        bail!(
            "Dashboard not found at {}. Use `prview open --dir` for the directory path.",
            dashboard.display()
        );
    }

    #[cfg(target_os = "macos")]
    {
        let status = std::process::Command::new("open")
            .arg(&dashboard)
            .status()
            .context("Failed to open dashboard")?;
        ensure_opener_succeeded("open", &dashboard, status)?;
    }
    #[cfg(not(target_os = "macos"))]
    {
        // xdg-open on Linux
        let status = std::process::Command::new("xdg-open")
            .arg(&dashboard)
            .status()
            .context("Failed to open dashboard")?;
        ensure_opener_succeeded("xdg-open", &dashboard, status)?;
    }

    Ok(())
}

fn ensure_opener_succeeded(
    opener: &str,
    dashboard: &Path,
    status: std::process::ExitStatus,
) -> Result<()> {
    if status.success() {
        return Ok(());
    }

    match status.code() {
        Some(code) => bail!(
            "{} exited with status {} while opening {}",
            opener,
            code,
            dashboard.display()
        ),
        None => bail!(
            "{} terminated before opening {}",
            opener,
            dashboard.display()
        ),
    }
}

fn detect_current_branch() -> String {
    std::env::current_dir()
        .ok()
        .and_then(|cwd| find_repo_root_from(&cwd).ok())
        .and_then(|repo_root| current_branch_name(&repo_root))
        .unwrap_or_else(|| "HEAD".to_string())
}

// ---------------------------------------------------------------------------
// Integration: register a run after generate()
// ---------------------------------------------------------------------------

/// Register a completed run in the index and prune old runs.
///
/// Call this at the end of `artifacts::generate()` with lock held.
/// Returns the number of pruned directories.
pub fn register_and_prune(
    out_dir: &Path,
    entry: RunEntry,
    emit_human_stdout: bool,
) -> Result<usize> {
    let _lock = acquire_lock()?;
    let mut index = RunIndex::load();
    index.append(entry);

    let pruned = index.prune(&RetentionPolicy::default(), out_dir);
    let pruned_count = pruned.len();

    // Delete pruned directories
    for path in &pruned {
        if path.is_dir() {
            let _ = fs::remove_dir_all(path);
        }
    }

    index.save()?;

    if emit_human_stdout && pruned_count > 0 {
        use colored::Colorize;
        println!(
            "  {} Pruned {} old run{}",
            "\u{267b}".green(),
            pruned_count,
            if pruned_count == 1 { "" } else { "s" },
        );
    }

    Ok(pruned_count)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::Path;

    #[cfg(unix)]
    fn exit_status(code: i32) -> std::process::ExitStatus {
        use std::os::unix::process::ExitStatusExt;

        std::process::ExitStatus::from_raw(code << 8)
    }

    #[cfg(windows)]
    fn exit_status(code: u32) -> std::process::ExitStatus {
        use std::os::windows::process::ExitStatusExt;

        std::process::ExitStatus::from_raw(code)
    }

    fn make_entry(id: &str, repo: &str, branch: &str, size: u64) -> RunEntry {
        RunEntry {
            id: id.to_string(),
            repo: repo.to_string(),
            branch: branch.to_string(),
            commit: "abc1234".to_string(),
            path: PathBuf::from(format!("/tmp/test-runs/{}/{}/{}", repo, branch, id)),
            created_at: format!("2026-01-01T00:00:{}Z", id),
            quality_pass: true,
            merge_status: "ALLOW".to_string(),
            policy_mode: "shadow".to_string(),
            checks_passed: 3,
            checks_failed: 0,
            files_changed: 10,
            size_bytes: size,
            has_dashboard: true,
        }
    }

    #[test]
    fn test_load_save_roundtrip() {
        let tmp = tempfile::tempdir().unwrap();
        let idx_path = tmp.path().join("index.jsonl");

        let mut index = RunIndex { entries: vec![] };
        index.append(make_entry("001", "myrepo", "main", 1000));
        index.append(make_entry("002", "myrepo", "feat-x", 2000));
        index.save_to(&idx_path).unwrap();

        assert!(idx_path.exists());

        let loaded = RunIndex::load_from(&idx_path);
        assert_eq!(loaded.entries().len(), 2);
        assert_eq!(loaded.entries()[0].id, "001");
        assert_eq!(loaded.entries()[1].id, "002");
    }

    #[test]
    fn test_list_for_repo_and_branch() {
        let mut index = RunIndex { entries: vec![] };
        index.append(make_entry("001", "repo-a", "main", 1000));
        index.append(make_entry("002", "repo-a", "feat", 1000));
        index.append(make_entry("003", "repo-b", "main", 1000));

        assert_eq!(index.list_for_repo("repo-a").len(), 2);
        assert_eq!(index.list_for_branch("repo-a", "main").len(), 1);
        assert_eq!(index.list_for_branch("repo-b", "main").len(), 1);
        assert_eq!(index.list_for_branch("repo-c", "main").len(), 0);
    }

    #[test]
    fn test_latest() {
        let mut index = RunIndex { entries: vec![] };
        index.append(make_entry("001", "repo", "main", 1000));
        index.append(make_entry("002", "repo", "main", 2000));

        let latest = index.latest("repo", "main").unwrap();
        assert_eq!(latest.id, "002");
    }

    #[test]
    fn test_prune_per_branch() {
        let tmp = tempfile::tempdir().unwrap();
        let current = tmp.path().join("current");
        fs::create_dir_all(&current).unwrap();

        let mut index = RunIndex { entries: vec![] };
        let policy = RetentionPolicy {
            max_runs_per_branch: 3,
            max_runs_per_repo: 100,
            max_total_bytes: u64::MAX,
        };

        // Add 5 runs to same branch
        for i in 0..5 {
            let id = format!("{:03}", i);
            let dir = tmp.path().join(format!("run-{}", i));
            fs::create_dir_all(&dir).unwrap();
            let mut entry = make_entry(&id, "repo", "main", 1000);
            entry.path = dir;
            index.append(entry);
        }

        let pruned = index.prune(&policy, &current);
        // 5 - 3 = 2 should be pruned (oldest first)
        assert_eq!(pruned.len(), 2);
        assert_eq!(index.entries().len(), 3);
        // Remaining should be the 3 newest
        assert_eq!(index.entries()[0].id, "002");
    }

    #[test]
    fn test_prune_protects_current_run() {
        let tmp = tempfile::tempdir().unwrap();

        let mut index = RunIndex { entries: vec![] };
        let policy = RetentionPolicy {
            max_runs_per_branch: 1,
            max_runs_per_repo: 100,
            max_total_bytes: u64::MAX,
        };

        // Current run is oldest, should be protected
        let current = tmp.path().join("run-0");
        fs::create_dir_all(&current).unwrap();
        let mut entry0 = make_entry("000", "repo", "main", 1000);
        entry0.path = current.clone();
        index.append(entry0);

        let dir1 = tmp.path().join("run-1");
        fs::create_dir_all(&dir1).unwrap();
        let mut entry1 = make_entry("001", "repo", "main", 1000);
        entry1.path = dir1;
        index.append(entry1);

        let pruned = index.prune(&policy, &current);
        // Should prune run-1 (not current run-0 even though it's oldest)
        assert_eq!(pruned.len(), 1);
        assert!(index.entries().iter().any(|e| e.path == current));
    }

    #[test]
    fn test_prune_global_size() {
        let tmp = tempfile::tempdir().unwrap();
        let current = tmp.path().join("current");
        fs::create_dir_all(&current).unwrap();

        let mut index = RunIndex { entries: vec![] };
        let policy = RetentionPolicy {
            max_runs_per_branch: 100,
            max_runs_per_repo: 100,
            max_total_bytes: 2500, // 2.5KB limit
        };

        // 3 runs × 1000 bytes = 3000 > 2500
        for i in 0..3 {
            let dir = tmp.path().join(format!("run-{}", i));
            fs::create_dir_all(&dir).unwrap();
            let mut entry = make_entry(&format!("{:03}", i), "repo", "main", 1000);
            entry.path = dir;
            index.append(entry);
        }

        let pruned = index.prune(&policy, &current);
        // Need to free 500 bytes, one run of 1000 bytes is enough
        assert_eq!(pruned.len(), 1);
    }

    #[test]
    fn test_remove_stale() {
        let tmp = tempfile::tempdir().unwrap();
        let existing = tmp.path().join("existing");
        fs::create_dir_all(&existing).unwrap();

        let mut index = RunIndex { entries: vec![] };
        let mut e1 = make_entry("001", "repo", "main", 1000);
        e1.path = existing;
        let mut e2 = make_entry("002", "repo", "main", 1000);
        e2.path = PathBuf::from("/nonexistent/path/run-002");
        index.append(e1);
        index.append(e2);

        index.remove_stale();
        assert_eq!(index.entries().len(), 1);
        assert_eq!(index.entries()[0].id, "001");
    }

    #[test]
    fn test_format_size() {
        assert_eq!(format_size(500), "500B");
        assert_eq!(format_size(1024), "1K");
        assert_eq!(format_size(965_100), "942K");
        assert_eq!(format_size(1_200_000), "1.1M");
        assert_eq!(format_size(5_368_709_120), "5.0G");
    }

    #[test]
    fn test_id_to_iso() {
        assert_eq!(
            id_to_iso("20260305-022829"),
            Some("2026-03-05T02:28:29".to_string())
        );
        assert_eq!(id_to_iso("short"), None);
    }

    #[test]
    fn test_entry_from_disk_reads_diff_stats_files_changed() {
        let tmp = tempfile::tempdir().unwrap();
        let run_dir = tmp.path().join("20260305-022829");
        fs::create_dir_all(&run_dir).unwrap();
        fs::write(
            run_dir.join("report.json"),
            r#"{
                "meta":{"generated_at":"2026-03-05T02:28:29Z","range":{"target":{"commit":"abcdef1234567"}}},
                "gate":{"quality_pass":true,"allow_merge":true,"policy_mode":"warn"},
                "checks":[{"status":"PASS"},{"status":"FAIL"}],
                "diff":{"stats":{"files_changed":7}}
            }"#,
        )
        .unwrap();

        let entry = entry_from_disk(&run_dir, "20260305-022829", "repo", "main").unwrap();
        assert_eq!(entry.files_changed, 7);
        assert_eq!(entry.checks_passed, 1);
        assert_eq!(entry.checks_failed, 1);
    }

    #[test]
    fn test_entry_from_disk_counts_only_failed_and_errored_as_failed() {
        // A rebuild must classify checks the same way registration does: only
        // FAIL/ERROR are failed. WARN and SKIP are neither passed nor failed, so
        // `total - passed` would wrongly inflate the failed count.
        let tmp = tempfile::tempdir().unwrap();
        let run_dir = tmp.path().join("20260305-022829");
        fs::create_dir_all(&run_dir).unwrap();
        fs::write(
            run_dir.join("report.json"),
            r#"{
                "meta":{"generated_at":"2026-03-05T02:28:29Z","range":{"target":{"commit":"abcdef1234567"}}},
                "gate":{"quality_pass":false,"allow_merge":false,"policy_mode":"warn"},
                "checks":[{"status":"PASS"},{"status":"WARN"},{"status":"SKIP"},{"status":"FAIL"},{"status":"ERROR"}],
                "diff":{"stats":{"files_changed":1}}
            }"#,
        )
        .unwrap();

        let entry = entry_from_disk(&run_dir, "20260305-022829", "repo", "main").unwrap();
        assert_eq!(entry.checks_passed, 1, "only PASS counts as passed");
        assert_eq!(
            entry.checks_failed, 2,
            "only FAIL and ERROR count as failed; WARN/SKIP must not inflate it"
        );
    }

    #[test]
    fn test_acquire_lock_rejects_live_owner_and_cleans_up_on_drop() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("index.jsonl.lock");

        let guard = acquire_lock_at(&lock_file).unwrap();
        let second = acquire_lock_at(&lock_file).unwrap_err();
        assert!(second.to_string().contains("another live process"));
        drop(guard);
        assert!(!lock_file.exists());
    }

    #[test]
    fn test_acquire_lock_replaces_stale_dead_pid_lock() {
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("index.jsonl.lock");
        fs::create_dir_all(tmp.path()).unwrap();
        fs::write(&lock_file, "999999:1").unwrap();

        let guard = acquire_lock_at(&lock_file).unwrap();
        let content = fs::read_to_string(&lock_file).unwrap();
        assert_eq!(content.trim(), guard.token);
        drop(guard);
    }

    #[test]
    fn test_acquire_lock_claims_stale_lock_without_leaving_artifacts() {
        // Replacing a stale lock goes through an atomic rename-claim rather than
        // a blind remove. The claimed temp file must be cleaned up, leaving only
        // our own live lock behind — no leaked `.claim.` files.
        let tmp = tempfile::tempdir().unwrap();
        let lock_file = tmp.path().join("index.jsonl.lock");
        fs::write(&lock_file, "999999:1").unwrap();

        let guard = acquire_lock_at(&lock_file).unwrap();

        let leftovers: Vec<String> = fs::read_dir(tmp.path())
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|name| name.contains(".claim."))
            .collect();
        assert!(
            leftovers.is_empty(),
            "stale-lock claim must leave no temp artifacts, found: {leftovers:?}"
        );
        assert!(lock_file.exists(), "our live lock must be held");
        drop(guard);
        assert!(!lock_file.exists());
    }

    #[test]
    fn test_list_for_branch_matches_legacy_branch_keys() {
        let mut index = RunIndex { entries: vec![] };
        index.append(make_entry("001", "repo-a", "feature-user-auth", 1000));

        let matches = index.list_for_branch("repo-a", "feature/user-auth");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].branch, "feature-user-auth");
    }

    #[test]
    fn test_ensure_opener_succeeded_accepts_zero_exit() {
        let dashboard = Path::new("/tmp/dashboard.html");

        ensure_opener_succeeded("open", dashboard, exit_status(0)).expect("zero exit succeeds");
    }

    #[test]
    fn test_ensure_opener_succeeded_rejects_non_zero_exit() {
        let dashboard = Path::new("/tmp/dashboard.html");

        let err = ensure_opener_succeeded("open", dashboard, exit_status(7)).unwrap_err();
        let msg = err.to_string();

        assert!(msg.contains("open exited with status 7"));
        assert!(msg.contains("/tmp/dashboard.html"));
    }

    // `run_runs_command` reaches the global index/lock via PRVIEW_HOME, so these
    // tests serialize env mutation. No other storage test uses the global paths
    // (they all pass explicit paths), so scoping PRVIEW_HOME here is safe.
    static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn with_prview_home<R>(f: impl FnOnce(&Path) -> R) -> R {
        let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let home = tempfile::tempdir().unwrap();
        let prev = std::env::var("PRVIEW_HOME").ok();
        // SAFETY: serialized by ENV_LOCK; restored before returning.
        unsafe { std::env::set_var("PRVIEW_HOME", home.path()) };
        let result = f(home.path());
        match prev {
            Some(v) => unsafe { std::env::set_var("PRVIEW_HOME", v) },
            None => unsafe { std::env::remove_var("PRVIEW_HOME") },
        }
        result
    }

    fn runs_opts_all_json() -> RunsOpts {
        RunsOpts {
            all: true,
            branch: None,
            status: None,
            json: true,
            rebuild: false,
        }
    }

    fn live_entry(home: &Path, id: &str) -> RunEntry {
        let dir = home.join("runs/myrepo/main").join(id);
        fs::create_dir_all(&dir).unwrap();
        let mut e = make_entry(id, "myrepo", "main", 100);
        e.path = dir;
        e
    }

    fn stale_entry(home: &Path, id: &str) -> RunEntry {
        // Path intentionally NOT created on disk → remove_stale should drop it.
        let mut e = make_entry(id, "myrepo", "main", 100);
        e.path = home.join("runs/myrepo/main").join(id);
        e
    }

    #[test]
    fn run_runs_command_cleanup_removes_stale_and_keeps_live() {
        with_prview_home(|home| {
            let mut index = RunIndex { entries: vec![] };
            index.append(live_entry(home, "live-001"));
            index.append(stale_entry(home, "stale-002"));
            index.save().unwrap();

            run_runs_command(&runs_opts_all_json()).expect("runs command");

            // The stale-entry cleanup is a locked read-modify-write: the live
            // entry survives, the stale one is dropped, and the change is
            // persisted.
            let reloaded = RunIndex::load();
            let ids: Vec<&str> = reloaded.entries().iter().map(|e| e.id.as_str()).collect();
            assert!(
                ids.contains(&"live-001"),
                "live entry must survive: {ids:?}"
            );
            assert!(
                !ids.contains(&"stale-002"),
                "stale entry must be pruned: {ids:?}"
            );
        });
    }

    #[test]
    fn run_runs_command_skips_cleanup_write_when_lock_is_held() {
        with_prview_home(|home| {
            let mut index = RunIndex { entries: vec![] };
            index.append(live_entry(home, "live-001"));
            index.append(stale_entry(home, "stale-002"));
            index.save().unwrap();

            // A concurrent writer owns the index lock. `runs` must not perform an
            // unlocked cleanup write; it degrades to a read and leaves the index
            // (including the not-yet-pruned stale entry) untouched.
            let _held = acquire_lock().expect("acquire lock");
            run_runs_command(&runs_opts_all_json()).expect("runs command stays read-only");

            let reloaded = RunIndex::load();
            let ids: Vec<&str> = reloaded.entries().iter().map(|e| e.id.as_str()).collect();
            assert!(
                ids.contains(&"stale-002"),
                "cleanup must be gated on the lock; stale entry must remain: {ids:?}"
            );
        });
    }
}