rchd 2.1.24

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

use crate::workers::WorkerPool;
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use rch_common::remote_compilation::RemoteCompilationTest;
use rch_common::{SelfTestConfig, SelfTestFailureAction, WorkerConfig, WorkerId};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::io::AsyncWriteExt;
use tokio::sync::Mutex;
use tokio::time::{Duration, sleep};
use tokio_cron_scheduler::{Job, JobScheduler};
use tracing::{info, warn};

pub const DEFAULT_RUN_CAPACITY: usize = 100;
pub const DEFAULT_RESULT_CAPACITY: usize = 500;
const DEFAULT_SELF_TEST_TIMEOUT_SECS: u64 = 300;

/// Type of self-test run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SelfTestRunType {
    Manual,
    Scheduled,
}

/// Summary of a self-test run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelfTestRunRecord {
    pub id: u64,
    pub run_type: SelfTestRunType,
    pub started_at: String,
    pub completed_at: String,
    pub workers_tested: usize,
    pub workers_passed: usize,
    pub workers_failed: usize,
    pub duration_ms: u64,
}

/// Result for a single worker in a self-test run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelfTestResultRecord {
    pub run_id: u64,
    pub worker_id: String,
    pub passed: bool,
    pub local_hash: Option<String>,
    pub remote_hash: Option<String>,
    pub local_time_ms: Option<u64>,
    pub remote_time_ms: Option<u64>,
    pub error: Option<String>,
}

/// Run response with per-worker results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelfTestRunReport {
    pub run: SelfTestRunRecord,
    pub results: Vec<SelfTestResultRecord>,
}

/// Status response for scheduled self-tests.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SelfTestStatus {
    pub enabled: bool,
    pub schedule: Option<String>,
    pub interval: Option<String>,
    pub last_run: Option<SelfTestRunRecord>,
    pub next_run: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum SelfTestHistoryEntry {
    Run(SelfTestRunRecord),
    Result(SelfTestResultRecord),
}

/// Persistent history for self-tests (JSONL append-only).
pub struct SelfTestHistory {
    runs: RwLock<VecDeque<SelfTestRunRecord>>,
    results: RwLock<VecDeque<SelfTestResultRecord>>,
    capacity: usize,
    result_capacity: usize,
    next_id: AtomicU64,
    persistence_path: Option<PathBuf>,
}

impl SelfTestHistory {
    pub fn new(capacity: usize, result_capacity: usize) -> Self {
        Self {
            runs: RwLock::new(VecDeque::with_capacity(capacity)),
            results: RwLock::new(VecDeque::with_capacity(result_capacity)),
            capacity,
            result_capacity,
            next_id: AtomicU64::new(1),
            persistence_path: None,
        }
    }

    pub fn with_persistence(mut self, path: PathBuf) -> Self {
        self.persistence_path = Some(path);
        self
    }

    pub fn next_id(&self) -> u64 {
        self.next_id.fetch_add(1, Ordering::SeqCst)
    }

    pub fn record_run(&self, record: SelfTestRunRecord) {
        let persistence = self
            .persistence_path
            .as_ref()
            .map(|path| (path.clone(), SelfTestHistoryEntry::Run(record.clone())));

        self.push_run(record);

        if let Some((path, entry)) = persistence {
            tokio::spawn(async move {
                if let Err(err) = persist_entry_async(&path, &entry).await {
                    warn!("Failed to persist self-test run: {}", err);
                }
            });
        }
    }

    pub fn record_result(&self, record: SelfTestResultRecord) {
        let persistence = self
            .persistence_path
            .as_ref()
            .map(|path| (path.clone(), SelfTestHistoryEntry::Result(record.clone())));

        self.push_result(record);

        if let Some((path, entry)) = persistence {
            tokio::spawn(async move {
                if let Err(err) = persist_entry_async(&path, &entry).await {
                    warn!("Failed to persist self-test result: {}", err);
                }
            });
        }
    }

    pub fn latest_run(&self) -> Option<SelfTestRunRecord> {
        let runs = self.runs.read().unwrap();
        runs.back().cloned()
    }

    pub fn recent_runs(&self, limit: usize) -> Vec<SelfTestRunRecord> {
        let runs = self.runs.read().unwrap();
        runs.iter().rev().take(limit).cloned().collect()
    }

    pub fn results_for_runs(&self, run_ids: &[u64]) -> Vec<SelfTestResultRecord> {
        let results = self.results.read().unwrap();
        results
            .iter()
            .filter(|r| run_ids.contains(&r.run_id))
            .cloned()
            .collect()
    }

    pub fn load_from_file(
        path: &Path,
        capacity: usize,
        result_capacity: usize,
    ) -> std::io::Result<Self> {
        let history =
            SelfTestHistory::new(capacity, result_capacity).with_persistence(path.to_path_buf());
        if !path.exists() {
            return Ok(history);
        }

        let content = std::fs::read_to_string(path)?;
        let mut max_id = 0u64;
        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let entry: SelfTestHistoryEntry = match serde_json::from_str(line) {
                Ok(entry) => entry,
                Err(err) => {
                    warn!("Skipping invalid self-test history line: {}", err);
                    continue;
                }
            };
            match entry {
                SelfTestHistoryEntry::Run(run) => {
                    max_id = max_id.max(run.id);
                    history.push_run(run);
                }
                SelfTestHistoryEntry::Result(result) => {
                    history.push_result(result);
                }
            }
        }

        history
            .next_id
            .store(max_id.saturating_add(1), Ordering::SeqCst);
        Ok(history)
    }

    fn push_run(&self, record: SelfTestRunRecord) {
        let mut runs = self.runs.write().unwrap();
        if runs.len() >= self.capacity {
            runs.pop_front();
        }
        runs.push_back(record);
    }

    fn push_result(&self, record: SelfTestResultRecord) {
        let mut results = self.results.write().unwrap();
        if results.len() >= self.result_capacity {
            results.pop_front();
        }
        results.push_back(record);
    }
}

async fn persist_entry_async(path: &Path, entry: &SelfTestHistoryEntry) -> std::io::Result<()> {
    let mut file = tokio::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .await?;
    // Concatenate JSON + newline into a single buffer so one `write_all`
    // can land atomically on the tail of an O_APPEND file. Two writes
    // (json, then `\n`) from concurrent callers can interleave and
    // produce `{a}{b}\n\n` — a corrupt JSONL stream that breaks the
    // history replay on the next daemon start.
    let mut line = serde_json::to_vec(entry)?;
    line.push(b'\n');
    file.write_all(&line).await?;
    file.flush().await?;
    Ok(())
}

/// Self-test execution options.
#[derive(Debug, Clone)]
pub struct SelfTestRunOptions {
    pub run_type: SelfTestRunType,
    pub worker_ids: Option<Vec<WorkerId>>,
    pub project_path: Option<PathBuf>,
    pub timeout: Duration,
    pub release_mode: bool,
    /// Override the configured retry count for THIS run. `None` uses the service
    /// config. A bounded smoke canary sets `Some(0)` (one attempt) so a failure
    /// surfaces immediately instead of being masked by the long retry delay
    /// blowing the run's own timeout budget.
    pub retry_count_override: Option<u32>,
}

impl Default for SelfTestRunOptions {
    fn default() -> Self {
        Self {
            run_type: SelfTestRunType::Manual,
            worker_ids: None,
            project_path: None,
            timeout: Duration::from_secs(DEFAULT_SELF_TEST_TIMEOUT_SECS),
            release_mode: true,
            retry_count_override: None,
        }
    }
}

/// Self-test service coordinating scheduled runs.
pub struct SelfTestService {
    pool: WorkerPool,
    config: SelfTestConfig,
    history: Arc<SelfTestHistory>,
    run_lock: Arc<Mutex<()>>,
    scheduler: Mutex<Option<JobScheduler>>,
}

impl SelfTestService {
    pub fn new(pool: WorkerPool, config: SelfTestConfig, history: Arc<SelfTestHistory>) -> Self {
        Self {
            pool,
            config,
            history,
            run_lock: Arc::new(Mutex::new(())),
            scheduler: Mutex::new(None),
        }
    }

    pub fn history(&self) -> Arc<SelfTestHistory> {
        self.history.clone()
    }

    pub fn status(&self) -> SelfTestStatus {
        let last_run = self.history.latest_run();
        let next_run = self.compute_next_run(&last_run, Utc::now());
        SelfTestStatus {
            enabled: self.config.enabled,
            schedule: self.config.schedule.clone(),
            interval: self.config.interval.clone(),
            last_run,
            next_run,
        }
    }

    pub async fn start(self: Arc<SelfTestService>) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }

        if let Some(schedule) = self.config.schedule.clone() {
            let scheduler = JobScheduler::new().await?;
            let service = self.clone();
            let job = Job::new_async(schedule.as_str(), move |_uuid, _lock| {
                let service = service.clone();
                Box::pin(async move {
                    if let Err(err) = service.run_scheduled().await {
                        warn!("Scheduled self-test failed: {}", err);
                    }
                })
            })?;
            scheduler.add(job).await?;
            scheduler.start().await?;
            *self.scheduler.lock().await = Some(scheduler);
            info!("Self-test scheduler started (cron)");
        }

        if let Some(interval) = self.config.interval.clone() {
            let service = self.clone();
            let duration =
                parse_duration(&interval).unwrap_or_else(|| Duration::from_secs(24 * 60 * 60));
            tokio::spawn(async move {
                let mut ticker = tokio::time::interval(duration);
                ticker.tick().await; // skip immediate tick
                loop {
                    ticker.tick().await;
                    if let Err(err) = service.run_scheduled().await {
                        warn!("Interval self-test failed: {}", err);
                    }
                }
            });
            info!("Self-test scheduler started (interval {})", interval);
        }

        Ok(())
    }

    pub async fn run_manual(&self, options: SelfTestRunOptions) -> Result<SelfTestRunReport> {
        self.run_internal(options).await
    }

    pub async fn run_scheduled_now(&self) -> Result<SelfTestRunReport> {
        self.run_scheduled().await
    }

    async fn run_scheduled(&self) -> Result<SelfTestRunReport> {
        let options = SelfTestRunOptions {
            run_type: SelfTestRunType::Scheduled,
            worker_ids: self.config.workers.resolve(),
            project_path: None,
            timeout: Duration::from_secs(DEFAULT_SELF_TEST_TIMEOUT_SECS),
            release_mode: true,
            retry_count_override: None,
        };
        self.run_internal(options).await
    }

    async fn run_internal(&self, options: SelfTestRunOptions) -> Result<SelfTestRunReport> {
        let _guard = self.run_lock.lock().await;
        let run_id = self.history.next_id();
        let started_at = Utc::now();

        let workers = self.resolve_workers(options.worker_ids.clone()).await?;
        // Each test writes a unique marker into the project's main.rs, builds
        // it locally and restores the file. Concurrent tests on ONE tree
        // overwrite and restore each other's marker and queue on one build
        // directory, so `--all` reported every worker timed out while each
        // passed alone (bd-57nk9). The daemon's own canary is tiny: give each
        // worker a private copy. An operator-supplied tree cannot be copied
        // blindly, so those tests run one at a time instead.
        let isolate = options.project_path.is_none() && workers.len() > 1;
        let sequential = options.project_path.is_some() && workers.len() > 1;
        let shared_project_path = match &options.project_path {
            Some(path) => Some(path.clone()),
            None if isolate => None,
            None => Some(ensure_self_test_project()?),
        };

        // Run all workers in PARALLEL to avoid sequential timeout accumulation
        // Each worker test runs concurrently with its own timeout
        let mut handles = Vec::new();
        let mut results = Vec::new();
        for worker in workers {
            let worker_clone = worker.clone();
            let project_path_clone = match &shared_project_path {
                Some(path) => path.clone(),
                None => ensure_self_test_project_for_worker(worker.id.as_str())?,
            };
            let options_clone = options.clone();
            let config_clone = self.config.clone();
            let pool_clone = self.pool.clone();

            let handle = tokio::spawn(async move {
                run_single_worker_test(
                    run_id,
                    &worker_clone,
                    &project_path_clone,
                    &options_clone,
                    &config_clone,
                    &pool_clone,
                )
                .await
            });
            if sequential {
                // Let this worker's marker cycle finish before the next starts.
                results.push(self_test_task_result(run_id, handle.await));
            } else {
                handles.push(handle);
            }
        }

        // Collect all results (parallel execution means total time ≈ slowest worker)
        for handle in handles {
            results.push(self_test_task_result(run_id, handle.await));
        }

        let workers_tested = results.len();
        let workers_passed = results.iter().filter(|r| r.passed).count();
        let workers_failed = workers_tested.saturating_sub(workers_passed);

        let completed_at = Utc::now();
        let duration_ms = (completed_at - started_at)
            .to_std()
            .unwrap_or_default()
            .as_millis() as u64;

        let run = SelfTestRunRecord {
            id: run_id,
            run_type: options.run_type,
            started_at: started_at.to_rfc3339(),
            completed_at: completed_at.to_rfc3339(),
            workers_tested,
            workers_passed,
            workers_failed,
            duration_ms,
        };

        self.history.record_run(run.clone());
        for result in &results {
            self.history.record_result(result.clone());
        }

        self.apply_failure_actions(&results).await;

        Ok(SelfTestRunReport { run, results })
    }

    async fn resolve_workers(
        &self,
        override_ids: Option<Vec<WorkerId>>,
    ) -> Result<Vec<WorkerConfig>> {
        let workers = self.pool.all_workers().await;
        if workers.is_empty() {
            return Ok(Vec::new());
        }

        let mut result = Vec::new();

        if let Some(ids) = override_ids {
            for worker in workers {
                let config = worker.config.read().await;
                if ids.contains(&config.id) {
                    result.push(config.clone());
                }
            }
        } else {
            // "All" means every worker that takes builds. One an operator
            // disabled or is draining is out of rotation on purpose; testing
            // it only reports a timeout. Name it explicitly to test it anyway.
            for worker in workers {
                if matches!(
                    worker.status().await,
                    rch_common::WorkerStatus::Disabled
                        | rch_common::WorkerStatus::Draining
                        | rch_common::WorkerStatus::Drained
                ) {
                    continue;
                }
                let config = worker.config.read().await;
                result.push(config.clone());
            }
        }

        Ok(result)
    }

    async fn apply_failure_actions(&self, results: &[SelfTestResultRecord]) {
        if results.is_empty() {
            return;
        }

        for result in results.iter().filter(|r| !r.passed) {
            match self.config.on_failure {
                SelfTestFailureAction::Alert => {
                    warn!(
                        "Self-test failed for worker {}: {:?}",
                        result.worker_id, result.error
                    );
                }
                SelfTestFailureAction::DisableWorker => {
                    warn!(
                        "Self-test failed for worker {}: disabling worker",
                        result.worker_id
                    );
                    let id = WorkerId::new(result.worker_id.clone());
                    self.pool
                        .set_status(&id, rch_common::WorkerStatus::Disabled)
                        .await;
                }
                SelfTestFailureAction::AlertAndDisable => {
                    warn!(
                        "Self-test failed for worker {}: alert + disable",
                        result.worker_id
                    );
                    let id = WorkerId::new(result.worker_id.clone());
                    self.pool
                        .set_status(&id, rch_common::WorkerStatus::Disabled)
                        .await;
                }
            }
        }
    }

    fn compute_next_run(
        &self,
        last_run: &Option<SelfTestRunRecord>,
        now: DateTime<Utc>,
    ) -> Option<String> {
        if let Some(schedule) = self.config.schedule.as_ref()
            && let Ok(expr) = cron::Schedule::from_str(schedule)
            && let Some(next) = expr.after(&now).next()
        {
            return Some(next.to_rfc3339());
        }

        if let Some(interval) = self.config.interval.as_ref() {
            let duration = parse_duration(interval)?;
            let base = last_run
                .as_ref()
                .and_then(|run| DateTime::parse_from_rfc3339(&run.completed_at).ok())
                .map(|dt| dt.with_timezone(&Utc))
                .unwrap_or(now);
            let next = base + chrono::Duration::from_std(duration).ok()?;
            return Some(next.to_rfc3339());
        }

        None
    }
}

fn parse_duration(value: &str) -> Option<Duration> {
    humantime::parse_duration(value).ok()
}

/// Run a single worker self-test with retries, each bounded by the per-worker
/// `options.timeout`, and with a fleet-safe total budget so a wedged worker
/// can never starve `--all` mode of attention (bd-nuuqt).
///
/// The `options.timeout` value is enforced in two places:
///
/// 1. **Per attempt**: each call to the remote compilation test is wrapped in
///    `tokio::time::timeout(options.timeout, ...)`.
/// 2. **Per worker total**: the retry loop itself is wrapped in
///    `tokio::time::timeout(options.timeout, ...)`. This guarantees the total
///    wall clock time a single worker can consume is strictly bounded,
///    regardless of retry count.
///
/// On timeout, `RCH-E203` (`WorkerSelfTestFailed`) is prefixed to the error
/// message so downstream automation can branch on a stable code.
async fn run_single_worker_test(
    run_id: u64,
    worker: &WorkerConfig,
    project_path: &Path,
    options: &SelfTestRunOptions,
    config: &SelfTestConfig,
    _pool: &WorkerPool, // Reserved for future use (e.g., status updates)
) -> SelfTestResultRecord {
    let worker_id = worker.id.to_string();
    let timeout_total = options.timeout;

    let retry_loop = async {
        let retry_delay =
            parse_duration(&config.retry_delay).unwrap_or_else(|| Duration::from_secs(300));

        let mut attempt = 0u32;
        loop {
            attempt += 1;
            let test = RemoteCompilationTest::new(worker.clone(), project_path.to_path_buf())
                .with_timeout(options.timeout)
                .with_release_mode(options.release_mode);

            let result = tokio::time::timeout(options.timeout, test.run()).await;

            let record = match result {
                Ok(Ok(outcome)) => SelfTestResultRecord {
                    run_id,
                    worker_id: worker_id.clone(),
                    passed: outcome.success,
                    local_hash: Some(outcome.local_hash.code_hash),
                    remote_hash: Some(outcome.remote_hash.code_hash),
                    local_time_ms: Some(outcome.local_build_ms),
                    remote_time_ms: Some(outcome.compilation_ms),
                    error: outcome.error,
                },
                Ok(Err(err)) => failed_worker_result(run_id, &worker_id, &err),
                Err(_elapsed) => SelfTestResultRecord {
                    run_id,
                    worker_id: worker_id.clone(),
                    passed: false,
                    local_hash: None,
                    remote_hash: None,
                    local_time_ms: None,
                    remote_time_ms: None,
                    error: Some(format!(
                        "[RCH-E203] Self-test attempt timed out after {:?}",
                        options.timeout
                    )),
                },
            };

            let retry_count = options.retry_count_override.unwrap_or(config.retry_count);
            if record.passed || attempt > retry_count {
                return record;
            }

            warn!(
                "Self-test failed for worker {} (attempt {}/{}), retrying in {:?}",
                worker_id, attempt, retry_count, retry_delay
            );
            sleep(retry_delay).await;
        }
    };

    match tokio::time::timeout(timeout_total, retry_loop).await {
        Ok(record) => record,
        Err(_elapsed) => SelfTestResultRecord {
            run_id,
            worker_id,
            passed: false,
            local_hash: None,
            remote_hash: None,
            local_time_ms: None,
            remote_time_ms: None,
            error: Some(format!(
                "[RCH-E203] Self-test hit per-worker deadline {:?} (including retries)",
                timeout_total
            )),
        },
    }
}

/// A panicked worker task becomes a failure record instead of losing the run.
fn self_test_task_result(
    run_id: u64,
    joined: std::result::Result<SelfTestResultRecord, tokio::task::JoinError>,
) -> SelfTestResultRecord {
    joined.unwrap_or_else(|join_err| {
        warn!("Self-test task panicked: {}", join_err);
        SelfTestResultRecord {
            run_id,
            worker_id: "unknown".to_string(),
            passed: false,
            local_hash: None,
            remote_hash: None,
            local_time_ms: None,
            remote_time_ms: None,
            error: Some(format!("Task panicked: {}", join_err)),
        }
    })
}

/// Keep the complete failure chain in persisted and returned self-test results.
/// Unknown hashes/timings stay absent; diagnostics do not establish execution.
fn failed_worker_result(
    run_id: u64,
    worker_id: &str,
    error: &anyhow::Error,
) -> SelfTestResultRecord {
    SelfTestResultRecord {
        run_id,
        worker_id: worker_id.to_owned(),
        passed: false,
        local_hash: None,
        remote_hash: None,
        local_time_ms: None,
        remote_time_ms: None,
        error: Some(format!("{error:#}")),
    }
}

/// Canonical source of the self-test canary's `src/main.rs`. Kept as a constant
/// so [`ensure_self_test_project`] can both create AND heal the fixture.
const SELF_TEST_MAIN_RS: &str = "fn main() {\n    println!(\"rch self-test\");\n}\n";

/// Whether the on-disk canary `main.rs` is still buildable as-is — i.e. it
/// declares a `fn main`. The verification pipeline applies a test marker to
/// `main.rs`, builds, then reverts; if a run is interrupted between apply and
/// revert (or the revert is partial), the file can be left WITHOUT a `main`,
/// which makes every future local reference build fail with E0601 and wedges the
/// canary permanently. Detect that so we can heal it.
fn self_test_main_rs_is_healthy(contents: &str) -> bool {
    contents.contains("fn main(")
}

fn ensure_self_test_project() -> Result<PathBuf> {
    ensure_self_test_project_at(&default_self_test_dir()?.join("project"))
}

/// A private copy of the canary for one worker, so concurrent tests share no
/// file and no build directory. Reused across runs to keep its local build warm.
fn ensure_self_test_project_for_worker(worker_id: &str) -> Result<PathBuf> {
    ensure_self_test_project_at(&self_test_worker_project_dir(
        &default_self_test_dir()?,
        worker_id,
    ))
}

fn self_test_worker_project_dir(cache_dir: &Path, worker_id: &str) -> PathBuf {
    let name: String = worker_id
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || matches!(c, '-' | '_') {
                c
            } else {
                '_'
            }
        })
        .collect();
    let name = if name.is_empty() {
        "worker".to_owned()
    } else {
        name
    };
    cache_dir.join("workers").join(name).join("project")
}

fn ensure_self_test_project_at(project_dir: &Path) -> Result<PathBuf> {
    let src_dir = project_dir.join("src");

    fs::create_dir_all(&src_dir)
        .with_context(|| format!("Failed to create self-test project dir {:?}", src_dir))?;

    let project_name = "rch_self_test";
    let cargo_toml = project_dir.join("Cargo.toml");
    if !cargo_toml.exists() {
        let contents = format!(
            "[package]\nname = \"{project_name}\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[dependencies]\n",
        );
        fs::write(&cargo_toml, contents)
            .with_context(|| format!("Failed to write {:?}", cargo_toml))?;
    }

    // Write `main.rs` when missing, and HEAL it when a previous interrupted
    // marker apply/revert left it without a `fn main` (which would fail every
    // local reference build and silently wedge the canary forever). Healing to
    // the canonical baseline is safe: the file is a daemon-owned ephemeral
    // fixture the marker cycle overwrites on each run anyway.
    let main_rs = src_dir.join("main.rs");
    let needs_main_rs = match fs::read_to_string(&main_rs) {
        Ok(existing) => !self_test_main_rs_is_healthy(&existing),
        Err(_) => true,
    };
    if needs_main_rs {
        fs::write(&main_rs, SELF_TEST_MAIN_RS)
            .with_context(|| format!("Failed to write {:?}", main_rs))?;
    }

    Ok(project_dir.to_path_buf())
}

fn default_self_test_dir() -> Result<PathBuf> {
    let dirs = directories::ProjectDirs::from("com", "rch", "rch")
        .context("Failed to resolve cache directory")?;
    let cache = dirs.cache_dir().join("self_test");
    fs::create_dir_all(&cache)
        .with_context(|| format!("Failed to create cache dir {:?}", cache))?;
    Ok(cache)
}

pub fn default_history_path() -> Result<PathBuf> {
    let base = default_self_test_dir()?;
    Ok(base.join("self_test_history.jsonl"))
}

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

    #[test]
    fn each_worker_gets_a_private_canary_project() {
        let cache = tempfile::tempdir().unwrap();
        let a = self_test_worker_project_dir(cache.path(), "vmi1264463");
        let b = self_test_worker_project_dir(cache.path(), "hz3");
        let odd = self_test_worker_project_dir(cache.path(), "../we ird/id");
        assert_ne!(a, b);
        for dir in [&a, &b, &odd] {
            assert!(dir.starts_with(cache.path().join("workers")), "{dir:?}");
            assert_eq!(dir.components().count(), a.components().count());
            let project = ensure_self_test_project_at(dir).unwrap();
            assert_eq!(
                fs::read_to_string(project.join("src/main.rs")).unwrap(),
                SELF_TEST_MAIN_RS
            );
            assert!(project.join("Cargo.toml").is_file());
        }
        // A marker left in one worker's tree never reaches another's.
        fs::write(a.join("src/main.rs"), "// RCH_TEST_1\nfn main() {}\n").unwrap();
        assert_eq!(
            fs::read_to_string(b.join("src/main.rs")).unwrap(),
            SELF_TEST_MAIN_RS
        );
    }

    #[tokio::test]
    async fn all_skips_workers_taken_out_of_rotation_unless_named() {
        let pool = WorkerPool::new();
        for id in ["up", "disabled", "draining"] {
            pool.add_worker(WorkerConfig {
                id: WorkerId::new(id),
                host: format!("{id}.invalid"),
                user: "u".to_string(),
                identity_file: "/dev/null".to_string(),
                total_slots: 2,
                priority: 100,
                tags: vec![],
                tools: Vec::new(),
            })
            .await;
        }
        pool.get(&WorkerId::new("disabled"))
            .await
            .unwrap()
            .disable(Some("maintenance".into()))
            .await;
        pool.get(&WorkerId::new("draining"))
            .await
            .unwrap()
            .set_status(rch_common::WorkerStatus::Draining)
            .await;
        let service = SelfTestService::new(
            pool,
            SelfTestConfig::default(),
            Arc::new(SelfTestHistory::new(4, 4)),
        );
        let ids = |workers: Vec<WorkerConfig>| -> Vec<String> {
            let mut ids: Vec<String> = workers.iter().map(|w| w.id.to_string()).collect();
            ids.sort();
            ids
        };
        assert_eq!(ids(service.resolve_workers(None).await.unwrap()), ["up"]);
        let named = Some(vec![WorkerId::new("disabled"), WorkerId::new("up")]);
        assert_eq!(
            ids(service.resolve_workers(named).await.unwrap()),
            ["disabled", "up"]
        );
    }

    #[test]
    fn failed_worker_result_retains_real_io_cause_through_serialization() {
        let retained = tempfile::tempdir().unwrap().keep();
        let missing = retained.join("never-created-artifact");
        let cause = fs::read(&missing).unwrap_err();
        assert_eq!(cause.kind(), std::io::ErrorKind::NotFound);
        let cause_message = cause.to_string();
        let error = anyhow::Error::new(cause)
            .context("Returned artifact could not be read")
            .context("Canary identity was not verified");
        let expected = format!(
            "Canary identity was not verified: Returned artifact could not be read: {cause_message}"
        );
        let record = failed_worker_result(47, "reader-case", &error);
        assert_eq!(record.error.as_deref(), Some(expected.as_str()));
        assert_eq!(record.run_id, 47);
        assert_eq!(record.worker_id, "reader-case");
        assert!(!record.passed);
        assert!(record.local_hash.is_none() && record.remote_hash.is_none());
        assert!(record.local_time_ms.is_none() && record.remote_time_ms.is_none());

        let document = serde_json::to_vec(&record).unwrap();
        let restored: SelfTestResultRecord = serde_json::from_slice(&document).unwrap();
        assert_eq!(restored.error.as_deref(), Some(expected.as_str()));
        assert_eq!(restored.run_id, 47);
        assert_eq!(restored.worker_id, "reader-case");
        assert!(!restored.passed);
        assert!(restored.local_hash.is_none() && restored.remote_hash.is_none());
        assert!(restored.local_time_ms.is_none() && restored.remote_time_ms.is_none());
        assert!(!missing.exists());
        assert_eq!(fs::read_dir(retained).unwrap().count(), 0);
    }

    // ==================== parse_duration tests ====================

    #[test]
    fn test_parse_duration_minutes() {
        let _guard = test_guard!();
        let duration = parse_duration("5m").expect("parse duration");
        assert_eq!(duration, Duration::from_secs(300));
    }

    #[test]
    fn test_parse_duration_hours() {
        let _guard = test_guard!();
        let duration = parse_duration("2h").expect("parse duration");
        assert_eq!(duration, Duration::from_secs(7200));
    }

    #[test]
    fn test_parse_duration_days() {
        let _guard = test_guard!();
        let duration = parse_duration("1d").expect("parse duration");
        assert_eq!(duration, Duration::from_secs(86400));
    }

    #[test]
    fn test_parse_duration_seconds() {
        let _guard = test_guard!();
        let duration = parse_duration("30s").expect("parse duration");
        assert_eq!(duration, Duration::from_secs(30));
    }

    #[test]
    fn test_parse_duration_combined() {
        let _guard = test_guard!();
        let duration = parse_duration("1h 30m").expect("parse duration");
        assert_eq!(duration, Duration::from_secs(5400));
    }

    #[test]
    fn test_parse_duration_invalid() {
        let _guard = test_guard!();
        assert!(parse_duration("invalid").is_none());
        assert!(parse_duration("").is_none());
    }

    // ==================== SelfTestRunOptions tests ====================

    #[test]
    fn test_self_test_run_options_default() {
        let _guard = test_guard!();
        let options = SelfTestRunOptions::default();
        assert_eq!(options.run_type, SelfTestRunType::Manual);
        assert!(options.worker_ids.is_none());
        assert!(options.project_path.is_none());
        assert_eq!(
            options.timeout,
            Duration::from_secs(DEFAULT_SELF_TEST_TIMEOUT_SECS)
        );
        assert!(options.release_mode);
    }

    #[test]
    fn test_self_test_run_options_custom() {
        let _guard = test_guard!();
        let options = SelfTestRunOptions {
            run_type: SelfTestRunType::Scheduled,
            worker_ids: Some(vec![WorkerId::new("w1")]),
            project_path: Some(PathBuf::from("/tmp/project")),
            timeout: Duration::from_secs(60),
            release_mode: false,
            retry_count_override: Some(0),
        };
        assert_eq!(options.run_type, SelfTestRunType::Scheduled);
        assert!(options.worker_ids.is_some());
        assert!(options.project_path.is_some());
        assert!(!options.release_mode);
        assert_eq!(options.retry_count_override, Some(0));
    }

    // ==================== SelfTestHistory tests ====================

    #[test]
    fn test_self_test_history_new() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);
        assert_eq!(history.capacity, 10);
        assert_eq!(history.result_capacity, 50);
        assert!(history.persistence_path.is_none());
    }

    #[test]
    fn test_self_test_history_with_persistence() {
        let _guard = test_guard!();
        let history =
            SelfTestHistory::new(10, 50).with_persistence(PathBuf::from("/tmp/history.jsonl"));
        assert!(history.persistence_path.is_some());
        assert_eq!(
            history.persistence_path.as_ref().unwrap().to_str().unwrap(),
            "/tmp/history.jsonl"
        );
    }

    #[test]
    fn test_self_test_history_next_id() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);
        assert_eq!(history.next_id(), 1);
        assert_eq!(history.next_id(), 2);
        assert_eq!(history.next_id(), 3);
    }

    #[test]
    fn test_self_test_history_push_and_latest_run() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);
        assert!(history.latest_run().is_none());

        let run = SelfTestRunRecord {
            id: 1,
            run_type: SelfTestRunType::Manual,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 2,
            workers_passed: 2,
            workers_failed: 0,
            duration_ms: 60000,
        };
        history.push_run(run.clone());

        let latest = history.latest_run();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().id, 1);
    }

    #[test]
    fn test_self_test_history_recent_runs() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);

        for i in 1..=5 {
            history.push_run(SelfTestRunRecord {
                id: i,
                run_type: SelfTestRunType::Manual,
                started_at: format!("2024-01-0{}T00:00:00Z", i),
                completed_at: format!("2024-01-0{}T00:01:00Z", i),
                workers_tested: 1,
                workers_passed: 1,
                workers_failed: 0,
                duration_ms: 1000,
            });
        }

        let recent = history.recent_runs(3);
        assert_eq!(recent.len(), 3);
        // Recent returns in reverse order (newest first)
        assert_eq!(recent[0].id, 5);
        assert_eq!(recent[1].id, 4);
        assert_eq!(recent[2].id, 3);
    }

    #[test]
    fn test_self_test_history_capacity_eviction() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(3, 50);

        for i in 1..=5 {
            history.push_run(SelfTestRunRecord {
                id: i,
                run_type: SelfTestRunType::Manual,
                started_at: "2024-01-01T00:00:00Z".to_string(),
                completed_at: "2024-01-01T00:01:00Z".to_string(),
                workers_tested: 1,
                workers_passed: 1,
                workers_failed: 0,
                duration_ms: 1000,
            });
        }

        let recent = history.recent_runs(10);
        // Should only keep 3 most recent
        assert_eq!(recent.len(), 3);
        assert_eq!(recent[0].id, 5);
        assert_eq!(recent[2].id, 3);
    }

    #[test]
    fn test_self_test_history_results_for_runs() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);

        history.push_result(SelfTestResultRecord {
            run_id: 1,
            worker_id: "w1".to_string(),
            passed: true,
            local_hash: Some("abc".to_string()),
            remote_hash: Some("abc".to_string()),
            local_time_ms: Some(100),
            remote_time_ms: Some(200),
            error: None,
        });

        history.push_result(SelfTestResultRecord {
            run_id: 2,
            worker_id: "w2".to_string(),
            passed: false,
            local_hash: None,
            remote_hash: None,
            local_time_ms: None,
            remote_time_ms: None,
            error: Some("connection failed".to_string()),
        });

        history.push_result(SelfTestResultRecord {
            run_id: 1,
            worker_id: "w3".to_string(),
            passed: true,
            local_hash: Some("def".to_string()),
            remote_hash: Some("def".to_string()),
            local_time_ms: Some(150),
            remote_time_ms: Some(250),
            error: None,
        });

        let results = history.results_for_runs(&[1]);
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.run_id == 1));

        let results = history.results_for_runs(&[2]);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].worker_id, "w2");

        let results = history.results_for_runs(&[1, 2]);
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn test_self_test_history_result_capacity_eviction() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 3);

        for i in 1..=5 {
            history.push_result(SelfTestResultRecord {
                run_id: i,
                worker_id: format!("w{}", i),
                passed: true,
                local_hash: None,
                remote_hash: None,
                local_time_ms: None,
                remote_time_ms: None,
                error: None,
            });
        }

        let results = history.results_for_runs(&[1, 2, 3, 4, 5]);
        // Should only keep 3 most recent results
        assert_eq!(results.len(), 3);
    }

    // ==================== SelfTestRunType tests ====================

    #[test]
    fn test_self_test_run_type_equality() {
        let _guard = test_guard!();
        assert_eq!(SelfTestRunType::Manual, SelfTestRunType::Manual);
        assert_eq!(SelfTestRunType::Scheduled, SelfTestRunType::Scheduled);
        assert_ne!(SelfTestRunType::Manual, SelfTestRunType::Scheduled);
    }

    #[test]
    fn test_self_test_run_type_serialization() {
        let _guard = test_guard!();
        let manual = serde_json::to_string(&SelfTestRunType::Manual).unwrap();
        assert_eq!(manual, "\"manual\"");

        let scheduled = serde_json::to_string(&SelfTestRunType::Scheduled).unwrap();
        assert_eq!(scheduled, "\"scheduled\"");
    }

    #[test]
    fn test_self_test_run_type_deserialization() {
        let _guard = test_guard!();
        let manual: SelfTestRunType = serde_json::from_str("\"manual\"").unwrap();
        assert_eq!(manual, SelfTestRunType::Manual);

        let scheduled: SelfTestRunType = serde_json::from_str("\"scheduled\"").unwrap();
        assert_eq!(scheduled, SelfTestRunType::Scheduled);
    }

    // ==================== Record serialization tests ====================

    #[test]
    fn test_self_test_run_record_serialization() {
        let _guard = test_guard!();
        let record = SelfTestRunRecord {
            id: 42,
            run_type: SelfTestRunType::Scheduled,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 3,
            workers_passed: 2,
            workers_failed: 1,
            duration_ms: 60000,
        };

        let json = serde_json::to_string(&record).unwrap();
        assert!(json.contains("\"id\":42"));
        assert!(json.contains("\"scheduled\""));
        assert!(json.contains("\"workers_passed\":2"));

        let deserialized: SelfTestRunRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.id, 42);
        assert_eq!(deserialized.workers_failed, 1);
    }

    #[test]
    fn test_self_test_result_record_serialization() {
        let _guard = test_guard!();
        let record = SelfTestResultRecord {
            run_id: 1,
            worker_id: "test-worker".to_string(),
            passed: true,
            local_hash: Some("abc123".to_string()),
            remote_hash: Some("abc123".to_string()),
            local_time_ms: Some(100),
            remote_time_ms: Some(200),
            error: None,
        };

        let json = serde_json::to_string(&record).unwrap();
        let deserialized: SelfTestResultRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.worker_id, "test-worker");
        assert!(deserialized.passed);
        assert!(deserialized.error.is_none());
    }

    // ==================== SelfTestService tests ====================

    #[test]
    fn self_test_main_rs_health_detects_wedged_fixture() {
        // The canonical baseline is healthy.
        assert!(self_test_main_rs_is_healthy(SELF_TEST_MAIN_RS));
        // A file still carrying a marker alongside main is healthy.
        assert!(self_test_main_rs_is_healthy(
            "fn main() {}\n#[allow(dead_code)] pub fn RCH_TEST_1() {}\n"
        ));
        // The real wedged shape observed in the field: only a leftover marker,
        // no `fn main` -> E0601 on every build -> must be detected as unhealthy.
        assert!(!self_test_main_rs_is_healthy(
            "\n\n// RCH Self-Test Marker\n#[unsafe(no_mangle)]\n#[allow(dead_code)]\npub fn RCH_TEST_123() -> &'static str { \"RCH_TEST_123\" }\n"
        ));
        assert!(!self_test_main_rs_is_healthy(""));
    }

    #[test]
    fn test_status_next_run_interval() {
        let _guard = test_guard!();
        let pool = WorkerPool::new();
        let history = Arc::new(SelfTestHistory::new(5, 5));
        let config = SelfTestConfig {
            enabled: true,
            interval: Some("1h".to_string()),
            ..Default::default()
        };

        let service = SelfTestService::new(pool, config, history);
        let status = service.status();
        assert!(status.next_run.is_some());
    }

    #[test]
    fn test_status_disabled() {
        let _guard = test_guard!();
        let pool = WorkerPool::new();
        let history = Arc::new(SelfTestHistory::new(5, 5));
        let config = SelfTestConfig {
            enabled: false,
            ..Default::default()
        };

        let service = SelfTestService::new(pool, config, history);
        let status = service.status();
        assert!(!status.enabled);
    }

    #[test]
    fn test_service_history_clone() {
        let _guard = test_guard!();
        let pool = WorkerPool::new();
        let history = Arc::new(SelfTestHistory::new(5, 5));
        let config = SelfTestConfig::default();

        let service = SelfTestService::new(pool, config, history.clone());
        let service_history = service.history();

        // Should be the same Arc
        assert!(Arc::ptr_eq(&history, &service_history));
    }

    // ==================== SelfTestStatus tests ====================

    #[test]
    fn test_self_test_status_serialization() {
        let _guard = test_guard!();
        let status = SelfTestStatus {
            enabled: true,
            schedule: Some("0 0 * * *".to_string()),
            interval: None,
            last_run: None,
            next_run: Some("2024-01-02T00:00:00Z".to_string()),
        };

        let json = serde_json::to_string(&status).unwrap();
        let deserialized: SelfTestStatus = serde_json::from_str(&json).unwrap();
        assert!(deserialized.enabled);
        assert_eq!(deserialized.schedule.unwrap(), "0 0 * * *");
    }

    // ==================== Constants tests ====================

    #[test]
    fn test_default_constants() {
        let _guard = test_guard!();
        assert_eq!(DEFAULT_RUN_CAPACITY, 100);
        assert_eq!(DEFAULT_RESULT_CAPACITY, 500);
        assert_eq!(DEFAULT_SELF_TEST_TIMEOUT_SECS, 300);
    }

    // ==================== Additional SelfTestHistory tests ====================

    #[test]
    fn test_self_test_history_capacity_limit_runs() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(3, 50); // Small capacity for testing

        // Add more runs than capacity
        for i in 1..=5 {
            let run = SelfTestRunRecord {
                id: i,
                run_type: SelfTestRunType::Manual,
                started_at: format!("2024-01-0{}T00:00:00Z", i),
                completed_at: format!("2024-01-0{}T00:01:00Z", i),
                workers_tested: 1,
                workers_passed: 1,
                workers_failed: 0,
                duration_ms: 60000,
            };
            history.push_run(run);
        }

        // Should only have the last 3 runs (capacity limit)
        let recent = history.recent_runs(10);
        assert_eq!(recent.len(), 3);
        // Most recent first
        assert_eq!(recent[0].id, 5);
        assert_eq!(recent[1].id, 4);
        assert_eq!(recent[2].id, 3);
    }

    #[test]
    fn test_self_test_history_load_from_file_nonexistent() {
        let _guard = test_guard!();
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("nonexistent.jsonl");

        // Loading from non-existent file should succeed with empty history
        let history = SelfTestHistory::load_from_file(&path, 10, 50).unwrap();
        assert!(history.latest_run().is_none());
        assert!(history.persistence_path.is_some());
    }

    #[test]
    fn test_self_test_history_load_from_file_with_data() {
        let _guard = test_guard!();
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("history.jsonl");

        // Write some test data
        let run_entry = SelfTestHistoryEntry::Run(SelfTestRunRecord {
            id: 42,
            run_type: SelfTestRunType::Scheduled,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 2,
            workers_passed: 2,
            workers_failed: 0,
            duration_ms: 60000,
        });
        let result_entry = SelfTestHistoryEntry::Result(SelfTestResultRecord {
            run_id: 42,
            worker_id: "worker1".to_string(),
            passed: true,
            local_hash: Some("abc123".to_string()),
            remote_hash: Some("abc123".to_string()),
            local_time_ms: Some(100),
            remote_time_ms: Some(150),
            error: None,
        });

        let mut content = serde_json::to_string(&run_entry).unwrap();
        content.push('\n');
        content.push_str(&serde_json::to_string(&result_entry).unwrap());
        content.push('\n');
        fs::write(&path, &content).unwrap();

        // Load and verify
        let history = SelfTestHistory::load_from_file(&path, 10, 50).unwrap();
        let latest = history.latest_run();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().id, 42);

        // Next ID should be 43 (max_id + 1)
        assert_eq!(history.next_id(), 43);

        let results = history.results_for_runs(&[42]);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].worker_id, "worker1");
    }

    #[test]
    fn test_self_test_history_load_from_file_with_invalid_lines() {
        let _guard = test_guard!();
        let temp_dir = tempfile::tempdir().unwrap();
        let path = temp_dir.path().join("history_invalid.jsonl");

        // Write mix of valid and invalid data
        let run_entry = SelfTestHistoryEntry::Run(SelfTestRunRecord {
            id: 1,
            run_type: SelfTestRunType::Manual,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 1,
            workers_passed: 1,
            workers_failed: 0,
            duration_ms: 1000,
        });

        let mut content = serde_json::to_string(&run_entry).unwrap();
        content.push_str("\ninvalid json line\n");
        content.push_str("{\"not_a_valid_entry\": true}\n");
        content.push('\n'); // Empty line
        fs::write(&path, &content).unwrap();

        // Should still load valid entries and skip invalid ones
        let history = SelfTestHistory::load_from_file(&path, 10, 50).unwrap();
        let latest = history.latest_run();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().id, 1);
    }

    #[test]
    fn test_self_test_history_record_run_without_persistence() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);

        let run = SelfTestRunRecord {
            id: 1,
            run_type: SelfTestRunType::Manual,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 1,
            workers_passed: 1,
            workers_failed: 0,
            duration_ms: 1000,
        };

        history.record_run(run.clone());
        let latest = history.latest_run();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().id, 1);
    }

    #[test]
    fn test_self_test_history_record_result_without_persistence() {
        let _guard = test_guard!();
        let history = SelfTestHistory::new(10, 50);

        let result = SelfTestResultRecord {
            run_id: 1,
            worker_id: "worker1".to_string(),
            passed: true,
            local_hash: Some("hash".to_string()),
            remote_hash: Some("hash".to_string()),
            local_time_ms: Some(100),
            remote_time_ms: Some(150),
            error: None,
        };

        history.record_result(result);
        let results = history.results_for_runs(&[1]);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].worker_id, "worker1");
    }

    #[test]
    fn test_self_test_run_report_serialization() {
        let _guard = test_guard!();
        let report = SelfTestRunReport {
            run: SelfTestRunRecord {
                id: 1,
                run_type: SelfTestRunType::Manual,
                started_at: "2024-01-01T00:00:00Z".to_string(),
                completed_at: "2024-01-01T00:01:00Z".to_string(),
                workers_tested: 2,
                workers_passed: 1,
                workers_failed: 1,
                duration_ms: 60000,
            },
            results: vec![
                SelfTestResultRecord {
                    run_id: 1,
                    worker_id: "worker1".to_string(),
                    passed: true,
                    local_hash: None,
                    remote_hash: None,
                    local_time_ms: None,
                    remote_time_ms: None,
                    error: None,
                },
                SelfTestResultRecord {
                    run_id: 1,
                    worker_id: "worker2".to_string(),
                    passed: false,
                    local_hash: None,
                    remote_hash: None,
                    local_time_ms: None,
                    remote_time_ms: None,
                    error: Some("timeout".to_string()),
                },
            ],
        };

        let json = serde_json::to_string(&report).unwrap();
        let deserialized: SelfTestRunReport = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.run.id, 1);
        assert_eq!(deserialized.results.len(), 2);
        assert!(deserialized.results[0].passed);
        assert!(!deserialized.results[1].passed);
    }

    #[test]
    fn test_status_with_cron_schedule() {
        let _guard = test_guard!();
        let pool = WorkerPool::new();
        let history = Arc::new(SelfTestHistory::new(5, 5));
        let config = SelfTestConfig {
            enabled: true,
            schedule: Some("0 0 9 * * MON-FRI".to_string()),
            ..Default::default()
        };

        let service = SelfTestService::new(pool, config, history);
        let status = service.status();
        assert!(status.enabled);
        assert_eq!(status.schedule, Some("0 0 9 * * MON-FRI".to_string()));
        assert!(status.next_run.is_some());
    }

    #[test]
    fn test_next_run_cron_weekday_rollover() {
        let _guard = test_guard!();
        let now = DateTime::parse_from_rfc3339("2026-09-11T09:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        for schedule in ["0 0 9 * * MON-FRI", "0 0 9 * * MON-FRI 2026"] {
            let service = SelfTestService::new(
                WorkerPool::new(),
                SelfTestConfig {
                    schedule: Some(schedule.to_string()),
                    ..Default::default()
                },
                Arc::new(SelfTestHistory::new(5, 5)),
            );
            assert_eq!(
                service.compute_next_run(&None, now).as_deref(),
                Some("2026-09-14T09:00:00+00:00"),
                "schedule {schedule} must advance past Friday's occurrence"
            );
        }
    }

    #[test]
    fn test_next_run_invalid_cron_falls_back_to_interval() {
        let _guard = test_guard!();
        let now = DateTime::parse_from_rfc3339("2026-09-11T09:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        for (interval, expected) in [
            (Some("1h"), Some("2026-09-11T10:00:00+00:00")),
            (None, None),
            (Some("invalid"), None),
        ] {
            let service = SelfTestService::new(
                WorkerPool::new(),
                SelfTestConfig {
                    schedule: Some("invalid".to_string()),
                    interval: interval.map(str::to_string),
                    ..Default::default()
                },
                Arc::new(SelfTestHistory::new(5, 5)),
            );
            assert_eq!(service.compute_next_run(&None, now).as_deref(), expected);
        }
    }

    #[test]
    fn test_status_with_last_run() {
        let _guard = test_guard!();
        let pool = WorkerPool::new();
        let history = Arc::new(SelfTestHistory::new(5, 5));

        // Add a run to history
        let run = SelfTestRunRecord {
            id: 1,
            run_type: SelfTestRunType::Scheduled,
            started_at: "2024-01-01T00:00:00Z".to_string(),
            completed_at: "2024-01-01T00:01:00Z".to_string(),
            workers_tested: 3,
            workers_passed: 3,
            workers_failed: 0,
            duration_ms: 60000,
        };
        history.record_run(run);

        let config = SelfTestConfig {
            enabled: true,
            interval: Some("1h".to_string()),
            ..Default::default()
        };

        let service = SelfTestService::new(pool, config, history);
        let status = service.status();
        assert!(status.last_run.is_some());
        assert_eq!(status.last_run.unwrap().id, 1);
    }
}