truth-mirror 0.13.1

Truthfulness gate and adversarial reviewer harness for AI coding agents.
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
//! Queue-tied watcher lifecycle: single-flight lock, crash-safe staleness, and
//! detached spawn.
//!
//! The watcher's lifetime is bound to the review queue, not to any agent session
//! or terminal. [`ensure_watcher`] is an idempotent check-and-spawn: if a live
//! watcher already owns the state dir it does nothing, otherwise it spawns
//! `truth-mirror watch --until-empty` as a detached background process and returns
//! immediately. It is safe to call from a git hook — fast, quiet on success, and
//! never blocking.
//!
//! Single-flight is enforced with a lock file ([`WATCHER_LOCK_FILE`]) recorded in
//! the state dir. The lock carries a [`ProcessIdentity`] — a pid plus a start-time
//! token — so a dead watcher never wedges the next spawn and a reused pid cannot
//! fool the liveness probe: the recorded start token must match the live process's
//! start token for the lock to count as held.

use std::{
    fs,
    io::{self, Write},
    path::{Path, PathBuf},
    process::Stdio,
    sync::atomic::{AtomicU64, Ordering},
    time::{SystemTime, UNIX_EPOCH},
};

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{reviewer::ReviewQueue, time::unix_now};

/// File name of the single-flight watcher lock inside the state dir.
pub const WATCHER_LOCK_FILE: &str = "watcher.lock";
const WATCHER_RECLAIM_DIR: &str = "watcher.lock.reclaim";
static LOCK_TOKEN_COUNTER: AtomicU64 = AtomicU64::new(0);

fn new_lock_token() -> String {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_nanos());
    let counter = LOCK_TOKEN_COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("{}-{nanos}-{counter}", std::process::id())
}

/// A just-written lock is treated as held even if its recorded owner cannot yet
/// be confirmed alive, to cover the window where a parent `ensure-watcher` has
/// claimed the lock and is about to hand it to its detached child (the parent may
/// exit before a racing caller probes it). Without this settling grace, two
/// `ensure-watcher` calls racing within milliseconds could both judge the other's
/// fresh lock "stale" and each spawn a watcher. Kept small so a genuinely crashed
/// spawn is still reclaimed promptly.
pub(crate) const LOCK_SETTLING_SECS: u64 = 10;

#[derive(Debug, Error)]
pub enum WatcherError {
    #[error("watcher lock io error: {0}")]
    Io(io::Error),
    #[error("watcher lock json error: {0}")]
    Json(serde_json::Error),
    #[error("failed to resolve the truth-mirror executable path: {0}")]
    ResolveExe(io::Error),
    #[error("failed to spawn detached watcher: {0}")]
    Spawn(io::Error),
    #[error("failed to read review queue: {0}")]
    Queue(crate::reviewer::ReviewerError),
    #[error("review queue is owned by {owner}")]
    QueueOwned { owner: String },
}

/// A process's identity strong enough to survive pid reuse.
///
/// `pid` alone is not enough: the OS recycles pids, so a *different* process could
/// later hold the pid recorded in a stale lock. `start_token` is an opaque,
/// process-specific string (the OS-reported start time) that changes when the pid
/// is reused, so a lock is only "held" when both the pid is alive AND its live
/// start token matches the recorded one.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct ProcessIdentity {
    pub pid: u32,
    /// OS-reported process start marker. Empty when the platform could not report
    /// one; liveness then degrades to a bare pid-alive check (documented tradeoff).
    #[serde(default)]
    pub start_token: String,
}

impl ProcessIdentity {
    /// The identity of the current process.
    pub fn current() -> Self {
        let pid = std::process::id();
        Self {
            start_token: probe_start_token(pid).unwrap_or_default(),
            pid,
        }
    }

    pub(crate) fn matches(&self, other: &Self) -> bool {
        self.pid == other.pid
            && (self.start_token.is_empty()
                || other.start_token.is_empty()
                || self.start_token == other.start_token)
    }
}

/// On-disk single-flight lock. Records who owns the watcher slot and when it was
/// claimed. Serialized as pretty JSON so it is human-inspectable in the state dir.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct WatcherLock {
    pub identity: ProcessIdentity,
    #[serde(default)]
    pub acquisition_token: String,
    pub created_at_unix: u64,
}

impl WatcherLock {
    fn for_current() -> Self {
        Self {
            acquisition_token: new_lock_token(),
            identity: ProcessIdentity::current(),
            created_at_unix: unix_now(),
        }
    }
}

/// Outcome of probing whether a recorded lock owner is still alive.
///
/// Kept as a distinct type (rather than a bare `bool`) so callers can treat
/// `Unknown` conservatively — an unprobeable owner is not silently reclaimed.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Liveness {
    /// The recorded owner is alive and its start token matches: the lock is held.
    Alive,
    /// Probe error or identity mismatch could not be resolved safely.
    /// A lock with unknown state is treated as held.
    Unknown,
    /// The recorded owner is gone (pid dead) or was replaced (pid reused): the
    /// lock is stale and may be reclaimed.
    Stale,
}

/// Decide whether a recorded lock owner is still the live watcher.
///
/// Pure over its inputs — the probed identity is injected — so the staleness
/// decision is unit-testable without any real processes:
/// - worker proven dead or replaced → `Stale`
/// - worker probe inconclusive → `Unknown`
/// - worker proven alive → `Alive`
pub fn decide_liveness(_recorded: &ProcessIdentity, worker: WorkerLiveness) -> Liveness {
    match worker {
        WorkerLiveness::Alive => Liveness::Alive,
        WorkerLiveness::Dead => Liveness::Stale,
        WorkerLiveness::Unknown => Liveness::Unknown,
    }
}

/// Whether a lock counts as held right now: either its recorded owner probes
/// [`Liveness::Alive`], or the lock is still within its settling window (a spawn is
/// in flight). `now` is injected so the settling decision is unit-testable.
///
/// The settling clause is one-directional safety: it can only make a fresh lock
/// look *more* held, never reclaim a live one, so it cannot spawn a duplicate.
fn lock_is_held_at(lock: &WatcherLock, worker: WorkerLiveness, now: u64) -> bool {
    match decide_liveness(&lock.identity, worker) {
        Liveness::Alive | Liveness::Unknown => true,
        Liveness::Stale => now.saturating_sub(lock.created_at_unix) < LOCK_SETTLING_SECS,
    }
}

/// Whether a lock counts as held now, probing the live process and clock.
fn lock_is_held(lock: &WatcherLock) -> bool {
    lock_is_held_at(lock, probe_worker_identity(&lock.identity), unix_now())
}

/// Whether a live watcher currently owns the lock in `state_dir`.
///
/// Reads the lock (if any) and probes its recorded owner. A missing, unreadable,
/// or stale lock all mean "no live owner". Never mutates the lock.
pub fn watcher_is_alive(state_dir: &Path) -> bool {
    match read_lock(state_dir) {
        Ok(Some(lock)) => lock_is_held(&lock),
        _ => false,
    }
}

fn lock_path(state_dir: &Path) -> PathBuf {
    state_dir.join(WATCHER_LOCK_FILE)
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum LockRead {
    Missing,
    Valid(WatcherLock),
    Corrupt { modified_at_unix: u64 },
}

impl LockRead {
    fn is_held_at(&self, now: u64) -> bool {
        match self {
            LockRead::Missing => false,
            LockRead::Valid(lock) => {
                lock_is_held_at(lock, probe_worker_identity(&lock.identity), now)
            }
            // A malformed lock is an Unknown state, not proof that its owner is
            // dead. It is held indefinitely after the settling window and must
            // be removed only by an operator who has inspected the state dir.
            // The same fail-closed policy applies to a valid lock whose identity
            // probe remains Unknown: callers keep observing HeldByLiveWatcher
            // until the owner is proven dead or an operator clears the lease.
            LockRead::Corrupt { .. } => true,
        }
    }

    fn is_held_with_unknown_identity(&self) -> bool {
        let LockRead::Valid(lock) = self else {
            return false;
        };
        matches!(
            probe_worker_identity(&lock.identity),
            WorkerLiveness::Unknown
        )
    }
}

fn corrupt_lock_is_held_at(modified_at_unix: u64, now: u64) -> bool {
    now.saturating_sub(modified_at_unix) < LOCK_SETTLING_SECS
}

fn read_lock(state_dir: &Path) -> Result<Option<WatcherLock>, WatcherError> {
    match read_lock_state(state_dir)? {
        LockRead::Valid(lock) => Ok(Some(lock)),
        LockRead::Missing | LockRead::Corrupt { .. } => Ok(None),
    }
}

fn read_lock_state(state_dir: &Path) -> Result<LockRead, WatcherError> {
    let path = lock_path(state_dir);
    match fs::read_to_string(&path) {
        Ok(contents) => match serde_json::from_str(contents.trim()) {
            Ok(lock) => Ok(LockRead::Valid(lock)),
            Err(_) => Ok(LockRead::Corrupt {
                modified_at_unix: file_modified_at_unix(&path),
            }),
        },
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(LockRead::Missing),
        Err(error) => Err(WatcherError::Io(error)),
    }
}

fn file_modified_at_unix(path: &Path) -> u64 {
    fs::metadata(path)
        .and_then(|metadata| metadata.modified())
        .ok()
        .and_then(|modified| modified.duration_since(UNIX_EPOCH).ok())
        .map_or_else(unix_now, |duration| duration.as_secs())
}

fn write_lock(state_dir: &Path, lock: &WatcherLock) -> Result<(), WatcherError> {
    fs::create_dir_all(state_dir).map_err(WatcherError::Io)?;
    let bytes = serde_json::to_vec_pretty(lock).map_err(WatcherError::Json)?;
    let temp_path = temp_lock_path(state_dir);
    {
        let mut file = fs::File::create(&temp_path).map_err(WatcherError::Io)?;
        file.write_all(&bytes).map_err(WatcherError::Io)?;
        file.write_all(b"\n").map_err(WatcherError::Io)?;
        file.sync_all().map_err(WatcherError::Io)?;
    }
    fs::rename(&temp_path, lock_path(state_dir)).map_err(WatcherError::Io)
}

fn temp_lock_path(state_dir: &Path) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_nanos());
    state_dir.join(format!(
        "{WATCHER_LOCK_FILE}.{}.{}.tmp",
        std::process::id(),
        nanos
    ))
}

/// Result of attempting to claim the single-flight watcher lock.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LockClaim {
    /// This process now owns the lock (it was free or the prior owner was stale).
    Acquired,
    /// A live watcher already owns the lock; the caller should stand down.
    HeldByLiveWatcher,
}

/// Try to claim the watcher lock for the current process, atomically and
/// crash-safe.
///
/// The claim uses an O_EXCL create as the racing gate: at most one of two
/// concurrent `create_new` calls succeeds, so two `ensure-watcher` invocations
/// racing (e.g. two pushes) resolve to exactly one [`LockClaim::Acquired`]. If the
/// exclusive create loses the race because a lock file already exists, the loser
/// inspects the existing owner:
/// - a live owner ⇒ [`LockClaim::HeldByLiveWatcher`] (stand down),
/// - a stale owner ⇒ overwrite in place and take it ([`LockClaim::Acquired`]).
///
/// Reclaiming a stale lock never blocks on the dead owner, so a crashed watcher
/// can never wedge the next spawn.
pub fn try_acquire_lock(state_dir: &Path) -> Result<LockClaim, WatcherError> {
    fs::create_dir_all(state_dir).map_err(WatcherError::Io)?;
    let lock = WatcherLock::for_current();
    let bytes = serde_json::to_vec_pretty(&lock).map_err(WatcherError::Json)?;

    match fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(lock_path(state_dir))
    {
        Ok(mut file) => {
            file.write_all(&bytes).map_err(WatcherError::Io)?;
            file.write_all(b"\n").map_err(WatcherError::Io)?;
            file.sync_all().map_err(WatcherError::Io)?;
            Ok(LockClaim::Acquired)
        }
        Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
            // Lost the create race — inspect the incumbent. A held lock (live owner
            // OR still settling after a fresh claim) means stand down; only a truly
            // stale lock is reclaimed. An unparseable lock means the winner is
            // mid-write (it created the file with O_EXCL but has not written its
            // bytes yet): stand down rather than clobber an in-flight claim.
            reclaim_existing_lock(state_dir, &lock)
        }
        Err(error) => Err(WatcherError::Io(error)),
    }
}

fn reclaim_existing_lock(state_dir: &Path, lock: &WatcherLock) -> Result<LockClaim, WatcherError> {
    let now = unix_now();
    let state = read_lock_state(state_dir)?;
    if state.is_held_at(now) {
        if state.is_held_with_unknown_identity() {
            tracing::warn!(
                state_dir = %state_dir.display(),
                "watcher lock probe returned unknown identity; leaving lock in place"
            );
        }
        return Ok(LockClaim::HeldByLiveWatcher);
    }

    let LockRead::Valid(stale_lock) = &state else {
        return Ok(LockClaim::HeldByLiveWatcher);
    };
    let stale_token = stale_lock.acquisition_token.clone();

    let Some(_guard) = try_reclaim_guard(state_dir)? else {
        return Ok(LockClaim::HeldByLiveWatcher);
    };

    let state = read_lock_state(state_dir)?;
    let LockRead::Valid(current_lock) = &state else {
        return Ok(LockClaim::HeldByLiveWatcher);
    };
    if current_lock.acquisition_token != stale_token {
        return Ok(LockClaim::HeldByLiveWatcher);
    }
    if state.is_held_at(unix_now()) {
        if state.is_held_with_unknown_identity() {
            tracing::warn!(
                state_dir = %state_dir.display(),
                "watcher lock probe returned unknown identity during reclaim; leaving lock in place"
            );
        }
        return Ok(LockClaim::HeldByLiveWatcher);
    }

    match fs::remove_file(lock_path(state_dir)) {
        Ok(()) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(error) => return Err(WatcherError::Io(error)),
    }

    match fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(lock_path(state_dir))
    {
        Ok(mut file) => {
            let bytes = serde_json::to_vec_pretty(lock).map_err(WatcherError::Json)?;
            file.write_all(&bytes).map_err(WatcherError::Io)?;
            file.write_all(b"\n").map_err(WatcherError::Io)?;
            file.sync_all().map_err(WatcherError::Io)?;
            Ok(LockClaim::Acquired)
        }
        Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
            Ok(LockClaim::HeldByLiveWatcher)
        }
        Err(error) => Err(WatcherError::Io(error)),
    }
}

struct ReclaimGuard {
    path: PathBuf,
}

impl Drop for ReclaimGuard {
    fn drop(&mut self) {
        let _ = fs::remove_dir(&self.path);
    }
}

fn try_reclaim_guard(state_dir: &Path) -> Result<Option<ReclaimGuard>, WatcherError> {
    let path = state_dir.join(WATCHER_RECLAIM_DIR);
    match fs::create_dir(&path) {
        Ok(()) => Ok(Some(ReclaimGuard { path })),
        Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
            if corrupt_lock_is_held_at(file_modified_at_unix(&path), unix_now()) {
                return Ok(None);
            }
            match fs::remove_dir(&path) {
                Ok(()) => {}
                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                Err(error) => return Err(WatcherError::Io(error)),
            }
            match fs::create_dir(&path) {
                Ok(()) => Ok(Some(ReclaimGuard { path })),
                Err(error) if error.kind() == io::ErrorKind::AlreadyExists => Ok(None),
                Err(error) => Err(WatcherError::Io(error)),
            }
        }
        Err(error) => Err(WatcherError::Io(error)),
    }
}

/// Release the watcher lock when a single ownership snapshot matches.
///
/// A watcher that reclaimed a stale slot or lost a race must not delete a lock now
/// held by a *different* live watcher, so this is a no-op unless the recorded
/// identity and token match the current process. The snapshot and unlink are not
/// one atomic filesystem operation; callers must not treat this helper as proof
/// against an uninstrumented replacement interleaving.
pub fn release_lock_if_owned(
    state_dir: &Path,
    expected_acquisition_token: &str,
) -> Result<(), WatcherError> {
    let Some(lock) = read_lock(state_dir)? else {
        return Ok(());
    };
    if expected_acquisition_token.is_empty()
        || lock.acquisition_token != expected_acquisition_token
        || !identity_matches_owner(&lock.identity, &ProcessIdentity::current())
    {
        return Ok(());
    }
    match fs::remove_file(lock_path(state_dir)) {
        Ok(()) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(error) => return Err(WatcherError::Io(error)),
    }
    Ok(())
}

/// Return the token for the currently recorded watcher lease.
pub fn lock_acquisition_token(state_dir: &Path) -> Option<String> {
    read_lock(state_dir)
        .ok()
        .flatten()
        .map(|lock| lock.acquisition_token)
}

fn identity_matches_owner(recorded: &ProcessIdentity, current: &ProcessIdentity) -> bool {
    recorded.matches(current)
}

/// Whether the recorded lock owner is THIS process.
///
/// `ensure-watcher` re-points the lock at the detached child's identity right
/// after spawning it, so a fresh `watch --until-empty` finds its own pid
/// already holding the slot — that is an ownership handoff, not contention.
/// A looping `watch` treats "held by a live watcher" as fatal only when the
/// holder is a DIFFERENT process.
pub fn lock_owned_by_current_process(state_dir: &Path) -> bool {
    match read_lock(state_dir) {
        Ok(Some(lock)) => identity_matches_owner(&lock.identity, &ProcessIdentity::current()),
        _ => false,
    }
}

/// Idempotent check-and-spawn for the queue-tied watcher.
///
/// If a live watcher already owns `state_dir`, returns without spawning. Otherwise
/// claims the single-flight lock and spawns a detached `truth-mirror watch
/// --until-empty` that outlives this process (and its terminal / git hook), then
/// returns immediately. Quiet on success; safe to call after every enqueue.
///
/// The spawned watcher — not this caller — is responsible for releasing the lock
/// when it exits, so this returning `Acquired` means "a watcher is now starting",
/// not "a watcher is done".
pub fn ensure_watcher(state_dir: &Path) -> Result<LockClaim, WatcherError> {
    ensure_watcher_with_spawner(state_dir, spawn_detached_watcher)
}

fn ensure_watcher_with_spawner(
    state_dir: &Path,
    spawn: impl FnOnce(&Path) -> Result<u32, WatcherError>,
) -> Result<LockClaim, WatcherError> {
    // Reap orphaned `running` review rows left by a killed watcher BEFORE the
    // spawn decision: a dead worker's rows must never masquerade as live
    // work, and 0.9.2 left them `running` forever with no self-healing path.
    // Best-effort — a reconciliation hiccup must not block spawning the
    // watcher that would drain the queue.
    match crate::reviewer::ReviewRunStore::new(state_dir).reconcile_stale_runs() {
        Ok(reaped) if reaped > 0 => {
            tracing::info!(
                reaped,
                "ensure-watcher: reaped stale review run(s) from a dead worker"
            );
        }
        Ok(_) => {}
        Err(error) => {
            tracing::warn!(%error, "ensure-watcher: failed to reconcile stale review runs");
        }
    }

    // Fast path: a live watcher already owns the slot, so do nothing without even
    // touching the lock file. Keeps hook latency negligible in the common case.
    if watcher_is_alive(state_dir) {
        return Ok(LockClaim::HeldByLiveWatcher);
    }

    match try_acquire_lock(state_dir)? {
        LockClaim::HeldByLiveWatcher => Ok(LockClaim::HeldByLiveWatcher),
        LockClaim::Acquired => {
            // We hold the lock, but this parent is about to exit — so immediately
            // re-point the lock at the detached child's identity. Otherwise a
            // second `ensure-watcher` racing right after this one could see a lock
            // owned by our (now-dead) pid, judge it stale, and spawn a *second*
            // watcher. Recording the child's identity closes that window without
            // depending on the child having started up yet.
            let acquisition_token = match read_lock(state_dir)? {
                Some(lock) => lock.acquisition_token,
                None => {
                    return Err(WatcherError::Io(io::Error::other(
                        "watcher lock disappeared after acquisition",
                    )));
                }
            };
            let child_pid = match spawn(state_dir) {
                Ok(pid) => pid,
                Err(error) => {
                    if let Err(release_error) = release_lock_if_owned(state_dir, &acquisition_token)
                    {
                        eprintln!(
                            "truth-mirror ensure-watcher: failed to release lock after spawn failure: {release_error}"
                        );
                    }
                    return Err(error);
                }
            };
            let child_lock = WatcherLock {
                identity: ProcessIdentity {
                    start_token: probe_start_token(child_pid).unwrap_or_default(),
                    pid: child_pid,
                },
                acquisition_token,
                created_at_unix: unix_now(),
            };
            write_lock(state_dir, &child_lock)?;
            Ok(LockClaim::Acquired)
        }
    }
}

/// Spawn `truth-mirror watch --until-empty` detached from this process, returning
/// the child's pid.
///
/// Detachment is platform-specific but kept thin:
/// - unix: a new process group ([`process_group(0)`]) so the child is not killed
///   when the parent's terminal / git hook exits, with stdio redirected to null.
/// - windows: `DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP` creation flags.
///
/// The state dir is passed explicitly so the child watches the same queue.
fn spawn_detached_watcher(state_dir: &Path) -> Result<u32, WatcherError> {
    let exe = std::env::current_exe().map_err(WatcherError::ResolveExe)?;
    let mut command = std::process::Command::new(exe);
    command
        .arg("--state-dir")
        .arg(state_dir)
        .arg("watch")
        .arg("--until-empty")
        // The child must absorb the lock handoff window: this parent re-points
        // the lock at the child right after spawn returns, and a child that
        // checked before the re-point landed would otherwise refuse its own
        // slot and strand the queue.
        .arg("--expect-lock-handoff")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null());

    detach(&mut command);

    let child = command.spawn().map_err(WatcherError::Spawn)?;
    Ok(child.id())
}

#[cfg(unix)]
fn detach(command: &mut std::process::Command) {
    use std::os::unix::process::CommandExt as _;
    // Own process group: the child survives the parent terminal / hook exiting.
    command.process_group(0);
}

#[cfg(windows)]
fn detach(command: &mut std::process::Command) {
    use std::os::windows::process::CommandExt as _;
    // DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW.
    const DETACHED_PROCESS: u32 = 0x0000_0008;
    const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
    const CREATE_NO_WINDOW: u32 = 0x0800_0000;
    command.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW);
}

#[cfg(not(any(unix, windows)))]
fn detach(_command: &mut std::process::Command) {}

/// Tri-state answer to "is this pid alive?".
///
/// Reconciliation decisions MUST distinguish a proven-dead process from an
/// unanswerable probe: treating a probe failure as death fails live runs —
/// 0.9.2 marked healthy reviews failed whenever the probe hiccuped.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PidLiveness {
    /// The process exists (or exists but is owned by someone else — EPERM).
    Alive,
    /// The OS confirmed no such process exists.
    Dead,
    /// The probe itself failed (spawn error, unparsable result) — NOT proof
    /// of death. Callers must not reap on this answer.
    Unknown,
}

/// Probe whether `pid` is alive, distinguishing a proven-dead process from a
/// failed probe. Prefer this over [`pid_is_alive`] wherever a wrong "dead"
/// answer would destroy state.
#[cfg(unix)]
pub fn probe_pid_liveness(pid: u32) -> PidLiveness {
    let Some(pid) = i32::try_from(pid)
        .ok()
        .and_then(rustix::process::Pid::from_raw)
    else {
        return PidLiveness::Unknown;
    };
    classify_pid_probe(rustix::process::test_kill_process(pid))
}

#[cfg(unix)]
fn classify_pid_probe(result: Result<(), rustix::io::Errno>) -> PidLiveness {
    match result {
        Ok(()) => PidLiveness::Alive,
        Err(error) if error == rustix::io::Errno::SRCH => PidLiveness::Dead,
        // EPERM means the process exists but is owned by someone else.
        Err(error) if error == rustix::io::Errno::PERM => PidLiveness::Alive,
        Err(_) => PidLiveness::Unknown,
    }
}

/// Probe whether `pid` is alive, distinguishing a proven-dead process from a
/// failed probe.
#[cfg(windows)]
pub fn probe_pid_liveness(pid: u32) -> PidLiveness {
    // `tasklist` filtered to the pid prints a row iff the process exists.
    let output = std::process::Command::new("tasklist")
        .args(["/FI", &format!("PID eq {pid}"), "/NH", "/FO", "CSV"])
        .stderr(Stdio::null())
        .output();
    match output {
        Ok(output) if output.status.success() => {
            let text = String::from_utf8_lossy(&output.stdout);
            if text.contains(&pid.to_string()) {
                PidLiveness::Alive
            } else {
                PidLiveness::Dead
            }
        }
        _ => PidLiveness::Unknown,
    }
}

#[cfg(not(any(unix, windows)))]
pub fn probe_pid_liveness(_pid: u32) -> PidLiveness {
    PidLiveness::Unknown
}

/// Tri-state liveness for a recorded worker identity (pid + start token).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WorkerLiveness {
    /// pid alive AND start token matches (or no token was recorded to compare).
    Alive,
    /// pid dead, OR pid alive under a DIFFERENT start token (pid reuse).
    Dead,
    /// The probe could not answer; NOT proof of death — the row must be left
    /// alone and the inconclusive probe logged.
    Unknown,
}

/// Pure worker-liveness decision over injected probe results, unit-testable
/// without real processes.
///
/// - pid proven dead → `Dead`
/// - pid probe inconclusive → `Unknown`
/// - pid alive, no recorded token (legacy row) → `Alive` (bare-pid fallback)
/// - pid alive, tokens differ → `Dead` (the pid was reused by another process)
/// - pid alive, recorded token but token unprobeable → `Unknown` (never guess
///   death when the identity check itself failed)
pub fn decide_worker_liveness(
    pid: PidLiveness,
    recorded_token: &str,
    probed_token: Option<String>,
) -> WorkerLiveness {
    match pid {
        PidLiveness::Dead => WorkerLiveness::Dead,
        PidLiveness::Unknown => WorkerLiveness::Unknown,
        PidLiveness::Alive => {
            if recorded_token.is_empty() {
                return WorkerLiveness::Alive;
            }
            match probed_token {
                Some(token) if token == recorded_token => WorkerLiveness::Alive,
                Some(_) => WorkerLiveness::Dead,
                None => WorkerLiveness::Unknown,
            }
        }
    }
}

/// Probe a recorded worker identity against the live system: pid liveness
/// plus the pid-reuse-defeating start-token comparison.
pub fn probe_worker_identity(recorded: &ProcessIdentity) -> WorkerLiveness {
    let pid = probe_pid_liveness(recorded.pid);
    let probed_token = if pid == PidLiveness::Alive && !recorded.start_token.is_empty() {
        probe_start_token(recorded.pid)
    } else {
        None
    };
    decide_worker_liveness(pid, &recorded.start_token, probed_token)
}

/// Probe whether `pid` is alive.
#[cfg(unix)]
pub fn pid_is_alive(pid: u32) -> bool {
    probe_pid_liveness(pid) == PidLiveness::Alive
}

#[cfg(windows)]
pub fn pid_is_alive(pid: u32) -> bool {
    probe_pid_liveness(pid) == PidLiveness::Alive
}

#[cfg(not(any(unix, windows)))]
pub fn pid_is_alive(_pid: u32) -> bool {
    false
}

/// OS-reported start marker for `pid`, used as the pid-reuse-defeating token.
#[cfg(unix)]
pub(crate) fn probe_start_token(pid: u32) -> Option<String> {
    // `ps -o lstart=` prints the absolute process start timestamp on both Linux
    // and macOS. It changes when the pid is reused, which is exactly the identity
    // signal we need. Pin the locale so the identity token is stable across the
    // caller's environment. Empty/failed output degrades to bare pid liveness.
    let output = std::process::Command::new("ps")
        .args(["-o", "lstart=", "-p", &pid.to_string()])
        .env("LC_ALL", "C")
        .stderr(Stdio::null())
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let token = String::from_utf8_lossy(&output.stdout).trim().to_owned();
    (!token.is_empty()).then_some(token)
}

#[cfg(windows)]
pub(crate) fn probe_start_token(pid: u32) -> Option<String> {
    // Query the process creation date; it changes on pid reuse. `wmic` is gone
    // from current Windows 11 installs, so use CIM through PowerShell instead.
    let script = format!(
        "$p = Get-CimInstance Win32_Process -Filter 'ProcessId = {pid}' -ErrorAction Stop; \
         if ($null -ne $p) {{ $p.CreationDate.ToUniversalTime().ToString('o') }}"
    );
    let output = std::process::Command::new("powershell")
        .args(["-NoProfile", "-Command", &script])
        .stderr(Stdio::null())
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    parse_powershell_start_token_output(&output.stdout)
}

#[cfg(any(windows, test))]
fn parse_powershell_start_token_output(stdout: &[u8]) -> Option<String> {
    let text = String::from_utf8_lossy(stdout);
    let token = text
        .lines()
        .find_map(|line| {
            let trimmed = line.trim();
            (!trimmed.is_empty()).then_some(trimmed)
        })
        .map(str::trim)
        .map(str::to_owned)?;
    (!token.is_empty()).then_some(token)
}

#[cfg(not(any(unix, windows)))]
pub(crate) fn probe_start_token(_pid: u32) -> Option<String> {
    None
}

/// Summarize the watcher lock for `status` output: `None` when no live watcher,
/// otherwise the owning pid.
pub fn live_watcher_pid(state_dir: &Path) -> Option<u32> {
    match read_lock(state_dir) {
        Ok(Some(lock)) => lock_is_held(&lock).then_some(lock.identity.pid),
        _ => None,
    }
}

/// Whether the review queue in `state_dir` currently has pending items.
pub fn queue_has_pending(state_dir: &Path) -> Result<bool, WatcherError> {
    let summary = ReviewQueue::new(state_dir)
        .summary()
        .map_err(WatcherError::Queue)?;
    Ok(summary.pending_count > 0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{
        Arc, Barrier,
        atomic::{AtomicUsize, Ordering},
    };

    fn identity(pid: u32, token: &str) -> ProcessIdentity {
        ProcessIdentity {
            pid,
            start_token: token.to_owned(),
        }
    }

    /// A pid guaranteed to be dead: spawn a trivial process, reap it, return
    /// its pid. Copilot (PR #13, watcher.rs ~844): spawning the external
    /// `true` binary isn't portable (notably absent on Windows). Spawning
    /// this test binary itself with `--help` (which the standard libtest
    /// harness understands and exits 0 for) is a guaranteed-dead-pid source
    /// without any external command dependency.
    fn reaped_pid() -> u32 {
        let mut child = std::process::Command::new(std::env::current_exe().unwrap())
            .arg("--help")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn()
            .unwrap();
        let pid = child.id();
        child.wait().unwrap();
        pid
    }

    #[test]
    fn worker_liveness_dead_pid_is_dead() {
        assert_eq!(
            decide_worker_liveness(PidLiveness::Dead, "token", None),
            WorkerLiveness::Dead
        );
    }

    #[cfg(unix)]
    #[test]
    fn pid_probe_classifies_os_errno_without_localized_output() {
        assert_eq!(
            classify_pid_probe(Err(rustix::io::Errno::SRCH)),
            PidLiveness::Dead
        );
        assert_eq!(
            classify_pid_probe(Err(rustix::io::Errno::PERM)),
            PidLiveness::Alive
        );
        assert_eq!(
            classify_pid_probe(Err(rustix::io::Errno::INVAL)),
            PidLiveness::Unknown
        );
    }

    #[test]
    fn worker_liveness_unknown_pid_probe_is_unknown() {
        // A failed pid probe is never proof of death.
        assert_eq!(
            decide_worker_liveness(PidLiveness::Unknown, "token", None),
            WorkerLiveness::Unknown
        );
    }

    #[test]
    fn worker_liveness_alive_without_recorded_token_degrades_to_pid() {
        // Legacy row: no start identity recorded, so bare pid liveness is the
        // best available signal.
        assert_eq!(
            decide_worker_liveness(PidLiveness::Alive, "", None),
            WorkerLiveness::Alive
        );
    }

    #[test]
    fn worker_liveness_matching_token_is_alive() {
        assert_eq!(
            decide_worker_liveness(
                PidLiveness::Alive,
                "Mon Jan 1 00:00:00 2026",
                Some("Mon Jan 1 00:00:00 2026".to_owned())
            ),
            WorkerLiveness::Alive
        );
    }

    #[test]
    fn worker_liveness_mismatched_token_is_pid_reuse_death() {
        assert_eq!(
            decide_worker_liveness(
                PidLiveness::Alive,
                "Mon Jan 1 00:00:00 2026",
                Some("Tue Feb 2 11:11:11 2026".to_owned())
            ),
            WorkerLiveness::Dead
        );
    }

    #[test]
    fn worker_liveness_unprobeable_token_is_unknown_not_death() {
        // The pid is alive but its start marker could not be read: never
        // guess death when the identity check itself failed.
        assert_eq!(
            decide_worker_liveness(PidLiveness::Alive, "Mon Jan 1 00:00:00 2026", None),
            WorkerLiveness::Unknown
        );
    }

    #[test]
    fn dead_pid_is_stale() {
        let recorded = identity(4242, "Mon Jan 1 00:00:00 2026");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Dead),
            Liveness::Stale
        );
    }

    #[test]
    fn reused_pid_with_different_start_token_is_stale() {
        let recorded = identity(4242, "Mon Jan 1 00:00:00 2026");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Dead),
            Liveness::Stale
        );
    }

    #[test]
    fn same_pid_and_start_token_is_alive() {
        let recorded = identity(4242, "Mon Jan 1 00:00:00 2026");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Alive),
            Liveness::Alive
        );
    }

    #[test]
    fn empty_start_token_falls_back_to_pid_liveness() {
        let recorded = identity(4242, "");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Alive),
            Liveness::Alive
        );
    }

    #[test]
    fn different_pid_is_stale_even_with_matching_token() {
        let recorded = identity(4242, "same-token");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Dead),
            Liveness::Stale
        );
    }

    #[test]
    fn unknown_probe_is_not_stale_even_after_settling_window() {
        let recorded = identity(4242, "recorded-token");
        assert_eq!(
            decide_liveness(&recorded, WorkerLiveness::Unknown),
            Liveness::Unknown
        );
    }

    #[test]
    fn acquire_then_reacquire_by_live_current_process_stands_down() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        // First claim succeeds via the exclusive create.
        assert_eq!(try_acquire_lock(state).unwrap(), LockClaim::Acquired);

        // The current process IS the recorded owner and is alive, so a second
        // claim from this same process observes a live owner and stands down.
        assert_eq!(
            try_acquire_lock(state).unwrap(),
            LockClaim::HeldByLiveWatcher
        );

        // And a liveness check agrees.
        assert!(watcher_is_alive(state));
        assert_eq!(live_watcher_pid(state), Some(std::process::id()));
    }

    #[test]
    fn empty_or_corrupt_lock_is_held_only_during_settling_window() {
        assert!(corrupt_lock_is_held_at(1_000, 1_000));
        assert!(corrupt_lock_is_held_at(
            1_000,
            1_000 + LOCK_SETTLING_SECS - 1
        ));
        assert!(!corrupt_lock_is_held_at(1_000, 1_000 + LOCK_SETTLING_SECS));
    }

    #[test]
    fn empty_lock_file_does_not_error_or_get_reclaimed_while_settling() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();
        fs::create_dir_all(state).unwrap();
        fs::write(lock_path(state), "").unwrap();

        assert!(matches!(
            read_lock_state(state).unwrap(),
            LockRead::Corrupt { .. }
        ));
        assert_eq!(
            try_acquire_lock(state).unwrap(),
            LockClaim::HeldByLiveWatcher
        );
    }

    #[test]
    fn stale_lock_from_dead_pid_is_reclaimed() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        // Plant a lock owned by a pid that is (almost certainly) dead, with a
        // start token so liveness cannot silently pass on the fallback path.
        let stale = WatcherLock {
            identity: identity(reaped_pid(), "definitely-not-a-live-token"),
            acquisition_token: "stale-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state, &stale).unwrap();
        assert!(!watcher_is_alive(state));

        // The next claim reclaims the stale slot rather than wedging.
        assert_eq!(try_acquire_lock(state).unwrap(), LockClaim::Acquired);
        assert_eq!(live_watcher_pid(state), Some(std::process::id()));
    }

    #[test]
    fn unknown_pid_probe_is_held_after_settling_instead_of_reclaimed() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        let stale = WatcherLock {
            identity: identity(4_000_000_000, ""),
            acquisition_token: "unknown-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state, &stale).unwrap();

        assert_eq!(
            try_acquire_lock(state).unwrap(),
            LockClaim::HeldByLiveWatcher
        );
        assert_eq!(live_watcher_pid(state), Some(4_000_000_000));
    }

    #[test]
    fn threaded_stale_lock_reclaim_race_yields_exactly_one_winner() {
        const THREADS: usize = 8;

        let temp = tempfile::tempdir().unwrap();
        let state = Arc::new(temp.path().to_path_buf());
        let stale = WatcherLock {
            identity: identity(reaped_pid(), "definitely-not-a-live-token"),
            acquisition_token: "stale-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state.as_ref(), &stale).unwrap();

        let barrier = Arc::new(Barrier::new(THREADS));
        let acquired = Arc::new(AtomicUsize::new(0));
        let held = Arc::new(AtomicUsize::new(0));
        let mut threads = Vec::with_capacity(THREADS);

        for _ in 0..THREADS {
            let state = Arc::clone(&state);
            let barrier = Arc::clone(&barrier);
            let acquired = Arc::clone(&acquired);
            let held = Arc::clone(&held);
            threads.push(std::thread::spawn(move || {
                barrier.wait();
                match try_acquire_lock(state.as_ref()).unwrap() {
                    LockClaim::Acquired => {
                        acquired.fetch_add(1, Ordering::SeqCst);
                    }
                    LockClaim::HeldByLiveWatcher => {
                        held.fetch_add(1, Ordering::SeqCst);
                    }
                }
            }));
        }

        for thread in threads {
            thread.join().unwrap();
        }

        assert_eq!(acquired.load(Ordering::SeqCst), 1);
        assert_eq!(held.load(Ordering::SeqCst), THREADS - 1);
    }

    #[test]
    fn release_is_a_noop_when_not_owner() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        let other = WatcherLock {
            identity: identity(4_000_000_000, "other-token"),
            acquisition_token: "other-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state, &other).unwrap();

        // We are not the recorded owner, so release leaves the lock intact.
        release_lock_if_owned(state, "not-our-token").unwrap();
        assert!(read_lock(state).unwrap().is_some());
    }

    #[test]
    fn release_removes_lock_when_owner() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        assert_eq!(try_acquire_lock(state).unwrap(), LockClaim::Acquired);
        let token = lock_acquisition_token(state).unwrap();
        release_lock_if_owned(state, &token).unwrap();
        assert!(read_lock(state).unwrap().is_none());
    }

    #[test]
    fn release_removes_lock_when_owner_start_token_is_unknown() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        let current = WatcherLock {
            identity: identity(std::process::id(), ""),
            acquisition_token: "current-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state, &current).unwrap();

        release_lock_if_owned(state, "current-token").unwrap();
        assert!(read_lock(state).unwrap().is_none());
    }

    #[test]
    fn release_does_not_remove_same_process_replacement_with_new_token() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();
        let owner = WatcherLock::for_current();
        write_lock(state, &owner).unwrap();

        let replacement = WatcherLock {
            identity: owner.identity.clone(),
            acquisition_token: "replacement-token".to_owned(),
            created_at_unix: unix_now(),
        };
        write_lock(state, &replacement).unwrap();

        release_lock_if_owned(state, &owner.acquisition_token).unwrap();
        assert_eq!(read_lock(state).unwrap(), Some(replacement));
    }

    #[test]
    fn ensure_watcher_releases_claimed_lock_when_spawn_fails() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        let error = ensure_watcher_with_spawner(state, |_| {
            Err(WatcherError::Spawn(io::Error::other("spawn failed")))
        })
        .unwrap_err();

        assert!(matches!(error, WatcherError::Spawn(_)));
        assert!(read_lock(state).unwrap().is_none());
        assert_eq!(try_acquire_lock(state).unwrap(), LockClaim::Acquired);
    }

    #[test]
    fn ensure_watcher_reconciles_orphaned_running_rows_before_deciding() {
        // 0.9.2 field failure: after a killed watcher left `running` rows
        // behind, ensure-watcher silently no-opped. It must liveness-check
        // recorded pids and reap dead rows as part of check-and-spawn.
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        // Plant an orphaned running row whose recorded worker pid is provably
        // dead (a real reaped child — a made-up pid number probes as
        // UNPROVABLE on some platforms and would correctly be left alone).
        let dead_pid = reaped_pid();
        let runs_dir = state.join(crate::reviewer::REVIEW_RUNS_DIR);
        fs::create_dir_all(&runs_dir).unwrap();
        fs::write(
            runs_dir.join("orphan.json"),
            serde_json::json!({
                "id": "orphan",
                "commit_sha": "abc123",
                "target": "commit",
                "status": "running",
                "phase": "reviewing",
                "ledger_entries": 0,
                "error": null,
                "worker_pid": dead_pid,
                "created_at_unix": 1,
                "updated_at_unix": 1,
                "started_at_unix": 1,
                "completed_at_unix": null
            })
            .to_string(),
        )
        .unwrap();

        let spawned = AtomicUsize::new(0);
        let claim = ensure_watcher_with_spawner(state, |_| {
            spawned.fetch_add(1, Ordering::SeqCst);
            Ok(4242)
        })
        .unwrap();

        // The orphan was reaped (marked failed, persisted) AND, with no live
        // watcher, a spawn happened.
        assert_eq!(claim, LockClaim::Acquired);
        assert_eq!(spawned.load(Ordering::SeqCst), 1);
        let reaped: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(runs_dir.join("orphan.json")).unwrap())
                .unwrap();
        assert_eq!(reaped["status"], "failed");
        assert!(
            reaped["error"].as_str().unwrap().contains("stale run"),
            "stale reason should be recorded, got: {reaped}"
        );
    }

    #[test]
    fn powershell_start_token_parser_uses_first_nonempty_line() {
        let output = b"\r\n  2026-07-09T01:02:03.0000000Z  \r\n";

        assert_eq!(
            parse_powershell_start_token_output(output).as_deref(),
            Some("2026-07-09T01:02:03.0000000Z")
        );
    }

    #[test]
    fn powershell_start_token_parser_rejects_empty_output() {
        assert_eq!(parse_powershell_start_token_output(b"\r\n  \n"), None);
    }

    #[test]
    fn current_identity_has_our_pid() {
        assert_eq!(ProcessIdentity::current().pid, std::process::id());
    }

    #[test]
    fn lock_owned_by_current_process_true_for_own_identity() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();
        write_lock(state, &WatcherLock::for_current()).unwrap();

        assert!(lock_owned_by_current_process(state));
    }

    #[test]
    fn lock_owned_by_current_process_false_for_foreign_or_missing_lock() {
        let temp = tempfile::tempdir().unwrap();
        let state = temp.path();

        // No lock at all.
        assert!(!lock_owned_by_current_process(state));

        // A lock owned by a different (here: dead) identity.
        let foreign = WatcherLock {
            identity: identity(4_000_000_000, "not-us"),
            acquisition_token: "foreign-token".to_owned(),
            created_at_unix: 1,
        };
        write_lock(state, &foreign).unwrap();
        assert!(!lock_owned_by_current_process(state));
    }

    #[test]
    fn fresh_lock_with_dead_owner_is_held_during_settling_window() {
        // A just-claimed lock whose recorded pid can't be confirmed alive (the
        // parent that claimed it may have already exited) still counts as held
        // while settling, so a racing caller stands down instead of double-spawning.
        let lock = WatcherLock {
            identity: identity(4_000_000_000, "dead-token"),
            acquisition_token: "dead-token".to_owned(),
            created_at_unix: 1_000,
        };
        // now == created (elapsed 0) → within settling → held.
        assert!(lock_is_held_at(&lock, WorkerLiveness::Unknown, 1_000));
        // Just before the settling boundary → still held.
        assert!(lock_is_held_at(
            &lock,
            WorkerLiveness::Unknown,
            1_000 + LOCK_SETTLING_SECS - 1
        ));
    }

    #[test]
    fn stale_lock_past_settling_window_is_not_held() {
        let own_pid = std::process::id();
        let lock = WatcherLock {
            identity: identity(own_pid, "dead-token"),
            acquisition_token: "dead-token".to_owned(),
            created_at_unix: 1_000,
        };
        // At/after the settling boundary with a dead owner → reclaimable.
        assert!(!lock_is_held_at(
            &lock,
            WorkerLiveness::Dead,
            1_000 + LOCK_SETTLING_SECS
        ));
        assert!(!lock_is_held_at(&lock, WorkerLiveness::Dead, 5_000));
    }

    #[test]
    fn unknown_probe_identity_is_held_even_after_settling_window() {
        let own_pid = std::process::id();
        let lock = WatcherLock {
            identity: identity(own_pid, "unknown-token"),
            acquisition_token: "unknown-token".to_owned(),
            created_at_unix: 1_000,
        };
        // A probe that confirms liveness but cannot recover a start marker
        // (empty token) must be treated as uncertain/held, never reclaimed.
        assert!(lock_is_held_at(
            &lock,
            WorkerLiveness::Unknown,
            1_000 + LOCK_SETTLING_SECS
        ));
    }

    #[test]
    fn live_owner_is_held_regardless_of_lock_age() {
        // Even an old lock is held if its recorded owner is alive with a matching
        // start token — settling only ever *adds* held-ness, never removes it.
        let lock = WatcherLock {
            identity: identity(4242, "live-token"),
            acquisition_token: "live-token".to_owned(),
            created_at_unix: 1,
        };
        assert!(lock_is_held_at(&lock, WorkerLiveness::Alive, 9_999_999));
    }
}