asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
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
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
//! Versioned scheduler evidence artifacts for swarm-host tuning.
//!
//! This module defines the compact, deterministic artifact contract consumed by
//! offline tuning workflows. It deliberately focuses on stable observables that
//! are already meaningful for large-host scheduler diagnosis: wake-to-run
//! latency, queue residency, backlog pressure, cancellation debt, and explicit
//! topology/knob metadata.

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

/// Stable version identifier for scheduler swarm-evidence artifacts.
pub const SCHEDULER_EVIDENCE_SCHEMA_VERSION: &str = "asupersync.scheduler-evidence.v1";

/// Compact evidence artifact describing one scheduler tuning run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerEvidenceArtifact {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Stable operator-provided run label.
    pub run_label: String,
    /// High-level workload shape that produced the evidence.
    pub workload_class: SchedulerWorkloadClass,
    /// Explicit host and cohort shape for this run.
    pub topology: SchedulerTopologyDescriptor,
    /// Scheduler knobs in effect when the evidence was captured.
    pub current_knobs: SchedulerKnobProfile,
    /// Tail and backlog metrics used by the offline recommendation pass.
    pub metrics: SchedulerEvidenceMetrics,
    /// Free-form deterministic notes.
    pub notes: Vec<String>,
}

impl SchedulerEvidenceArtifact {
    /// Validate the artifact before it is trusted by offline tooling.
    pub fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.schema_version != SCHEDULER_EVIDENCE_SCHEMA_VERSION {
            return Err(SchedulerEvidenceError::UnsupportedSchemaVersion {
                expected: SCHEDULER_EVIDENCE_SCHEMA_VERSION.to_string(),
                found: self.schema_version.clone(),
            });
        }
        if self.run_label.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyRunLabel);
        }
        if self.topology.worker_threads == 0 {
            return Err(SchedulerEvidenceError::ZeroWorkerThreads);
        }
        if self.topology.cohort_count == 0 {
            return Err(SchedulerEvidenceError::ZeroCohortCount);
        }
        if self.topology.memory_budget_gib == 0 {
            return Err(SchedulerEvidenceError::ZeroMemoryBudget);
        }
        if self.current_knobs.worker_threads == 0 {
            return Err(SchedulerEvidenceError::ZeroCurrentWorkers);
        }
        if self.current_knobs.steal_batch_size == 0 {
            return Err(SchedulerEvidenceError::ZeroStealBatchSize);
        }
        if self.current_knobs.cancel_streak_limit == 0 {
            return Err(SchedulerEvidenceError::ZeroCancelStreakLimit);
        }
        self.metrics.validate()?;
        Ok(())
    }

    /// Produce a deterministic tuning report from the captured evidence.
    pub fn tune_report(&self) -> Result<SchedulerTuneReport, SchedulerEvidenceError> {
        self.validate()?;

        let mut recommended_knobs = self.current_knobs.clone();
        let mut reason_codes = Vec::new();
        let mut explanation = Vec::new();
        let mut global_queue_limit_hint = None;

        let backlog_scale_threshold = self.topology.worker_threads.saturating_mul(4);
        if self.metrics.wake_to_run_p99_ns >= 150_000
            && self.metrics.ready_backlog_p99 >= backlog_scale_threshold
        {
            reason_codes.push(SchedulerRecommendationReason::WorkersSaturated);
            recommended_knobs.worker_threads = recommended_knobs
                .worker_threads
                .saturating_add(self.topology.cohort_count.max(1));
            explanation.push(format!(
                "wake_to_run p99={}ns with ready_backlog_p99={} exceeded the worker saturation envelope",
                self.metrics.wake_to_run_p99_ns, self.metrics.ready_backlog_p99
            ));
        }

        if self.metrics.queue_residency_p99_ns >= self.metrics.wake_to_run_p99_ns.saturating_mul(2)
        {
            reason_codes.push(SchedulerRecommendationReason::QueueResidencyDominant);
            recommended_knobs.steal_batch_size =
                recommended_knobs.steal_batch_size.saturating_mul(2).min(64);
            global_queue_limit_hint = Some(
                self.metrics
                    .ready_backlog_p99
                    .saturating_mul(2)
                    .max(backlog_scale_threshold),
            );
            explanation.push(format!(
                "queue_residency p99={}ns dominated wake_to_run p99={}ns, suggesting deeper burst draining",
                self.metrics.queue_residency_p99_ns, self.metrics.wake_to_run_p99_ns
            ));
        }

        if self.metrics.cancel_debt_p99
            >= self
                .metrics
                .cancel_debt_p95
                .max(self.current_knobs.cancel_streak_limit)
        {
            reason_codes.push(SchedulerRecommendationReason::CancelDebtDominant);
            recommended_knobs.cancel_streak_limit = recommended_knobs
                .cancel_streak_limit
                .saturating_mul(2)
                .min(128);
            explanation.push(format!(
                "cancel_debt p99={} remained above the current drain envelope",
                self.metrics.cancel_debt_p99
            ));
        }

        if let Some(remote_steal_ratio_pct) = self.metrics.remote_steal_ratio_pct
            && self.topology.cohort_count > 1
            && remote_steal_ratio_pct >= 35
        {
            reason_codes.push(SchedulerRecommendationReason::RemoteStealPressure);
            explanation.push(format!(
                "remote steal ratio {}% indicates locality-aware follow-up work should stay enabled",
                remote_steal_ratio_pct
            ));
        }

        if reason_codes.is_empty() {
            reason_codes.push(SchedulerRecommendationReason::BalancedBaseline);
            explanation.push(
                "tail and backlog metrics stayed inside the conservative baseline envelope"
                    .to_string(),
            );
        }

        let profile_name = if reason_codes
            .contains(&SchedulerRecommendationReason::WorkersSaturated)
        {
            "scale_workers"
        } else if reason_codes.contains(&SchedulerRecommendationReason::QueueResidencyDominant) {
            "drain_ready_bursts"
        } else if reason_codes.contains(&SchedulerRecommendationReason::CancelDebtDominant) {
            "drain_cancel_pressure"
        } else {
            "conservative_baseline"
        };

        let confidence_percent = 55u8
            .saturating_add(
                (u8::try_from(reason_codes.len()).unwrap_or(u8::MAX)).saturating_mul(10),
            )
            .min(90);

        Ok(SchedulerTuneReport {
            schema_version: SCHEDULER_EVIDENCE_SCHEMA_VERSION.to_string(),
            source_run_label: self.run_label.clone(),
            workload_class: self.workload_class,
            profile_name: profile_name.to_string(),
            recommended_knobs,
            global_queue_limit_hint,
            fallback_profile: self.current_knobs.clone(),
            confidence_percent,
            reason_codes,
            explanation,
        })
    }
}

/// Explicit workload classes for swarm-host scheduler runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchedulerWorkloadClass {
    /// Interactive agent-swarm traffic with latency-sensitive bursts.
    InteractiveSwarm,
    /// Mixed ready/cancel bursts typical of general-purpose swarm hosts.
    MixedBurst,
    /// Cancellation-dominated storm or cleanup scenario.
    CancellationStorm,
    /// Long-running throughput-biased drain workload.
    ThroughputDrain,
}

/// Stable topology description for a scheduler evidence artifact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerTopologyDescriptor {
    /// Number of scheduler workers participating in the run.
    pub worker_threads: usize,
    /// Number of explicit worker cohorts or locality groups.
    pub cohort_count: usize,
    /// Host memory budget captured with the evidence run.
    pub memory_budget_gib: usize,
}

/// Scheduler knobs subject to offline recommendations.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerKnobProfile {
    /// Worker thread count in the profiled runtime.
    pub worker_threads: usize,
    /// Steal-batch size configured for burst draining.
    pub steal_batch_size: usize,
    /// Maximum consecutive cancel-lane dispatches before yielding.
    pub cancel_streak_limit: usize,
    /// Global queue limit in effect during the run (`0` = unbounded).
    pub global_queue_limit: usize,
    /// Whether worker parking was enabled.
    pub parking_enabled: bool,
}

/// Tail and backlog metrics captured for one run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerEvidenceMetrics {
    /// Wake-to-run latency median in nanoseconds.
    pub wake_to_run_p50_ns: u64,
    /// Wake-to-run latency p95 in nanoseconds.
    pub wake_to_run_p95_ns: u64,
    /// Wake-to-run latency p99 in nanoseconds.
    pub wake_to_run_p99_ns: u64,
    /// Queue-residency latency median in nanoseconds.
    pub queue_residency_p50_ns: u64,
    /// Queue-residency latency p95 in nanoseconds.
    pub queue_residency_p95_ns: u64,
    /// Queue-residency latency p99 in nanoseconds.
    pub queue_residency_p99_ns: u64,
    /// Ready-backlog p95 count.
    pub ready_backlog_p95: usize,
    /// Ready-backlog p99 count.
    pub ready_backlog_p99: usize,
    /// Cancel-debt p95 count.
    pub cancel_debt_p95: usize,
    /// Cancel-debt p99 count.
    pub cancel_debt_p99: usize,
    /// Percentage of steals that crossed cohort boundaries, if known.
    pub remote_steal_ratio_pct: Option<u8>,
    /// Cross-cohort wake-to-run p99 in nanoseconds, if measured.
    pub cross_cohort_wake_p99_ns: Option<u64>,
}

impl SchedulerEvidenceMetrics {
    fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        validate_percentiles(
            self.wake_to_run_p50_ns,
            self.wake_to_run_p95_ns,
            self.wake_to_run_p99_ns,
            "wake_to_run",
        )?;
        validate_percentiles(
            self.queue_residency_p50_ns,
            self.queue_residency_p95_ns,
            self.queue_residency_p99_ns,
            "queue_residency",
        )?;
        validate_percentiles(
            self.ready_backlog_p95,
            self.ready_backlog_p99,
            self.ready_backlog_p99,
            "ready_backlog",
        )?;
        validate_percentiles(
            self.cancel_debt_p95,
            self.cancel_debt_p99,
            self.cancel_debt_p99,
            "cancel_debt",
        )?;
        if let Some(remote_steal_ratio_pct) = self.remote_steal_ratio_pct
            && remote_steal_ratio_pct > 100
        {
            return Err(SchedulerEvidenceError::RemoteStealRatioOutOfRange(
                remote_steal_ratio_pct,
            ));
        }
        Ok(())
    }
}

/// Deterministic offline tuning report emitted from one evidence artifact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerTuneReport {
    /// Output schema version for the tuning report.
    pub schema_version: String,
    /// Source run label copied from the input artifact.
    pub source_run_label: String,
    /// Workload class the recommendation is based on.
    pub workload_class: SchedulerWorkloadClass,
    /// Human-readable profile label for the recommendation.
    pub profile_name: String,
    /// Recommended worker/batch/cancel knobs.
    pub recommended_knobs: SchedulerKnobProfile,
    /// Optional queue-capacity hint derived from backlog pressure.
    pub global_queue_limit_hint: Option<usize>,
    /// Exact conservative fallback profile (the input knobs).
    pub fallback_profile: SchedulerKnobProfile,
    /// Coarse confidence score for operator triage.
    pub confidence_percent: u8,
    /// Stable reason codes explaining why the recommendation fired.
    pub reason_codes: Vec<SchedulerRecommendationReason>,
    /// Human-readable explanation lines for operators and artifacts.
    pub explanation: Vec<String>,
}

/// Stable reason codes for why a recommendation was made.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SchedulerRecommendationReason {
    /// Wake-to-run latency and ready backlog imply more workers are needed.
    WorkersSaturated,
    /// Queue residency dominates wake latency, suggesting deeper burst draining.
    QueueResidencyDominant,
    /// Cancel backlog remains high enough to justify stronger cancel draining.
    CancelDebtDominant,
    /// Cross-cohort stealing pressure suggests locality work should stay enabled.
    RemoteStealPressure,
    /// Current knobs remain appropriate for the observed envelope.
    BalancedBaseline,
}

/// Stable schema for synthesized scheduler inputs from coordination bundles.
pub const SCHEDULER_COORDINATION_EVIDENCE_SCHEMA_VERSION: &str =
    "asupersync.scheduler-coordination-evidence-inputs.v1";

/// Scheduler-facing evidence inputs synthesized from coordination workload packs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerCoordinationEvidenceInputs {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Expansion-pack identifier that produced the inputs.
    pub source_pack_id: String,
    /// Hash of the redacted source bundle used for deterministic replay.
    pub source_bundle_hash: String,
    /// Source collector run identifier.
    pub source_run_id: String,
    /// One evidence input per covered coordination-pressure family.
    pub evidence_inputs: Vec<SchedulerCoordinationEvidenceInput>,
}

impl SchedulerCoordinationEvidenceInputs {
    /// Validate that synthesized coordination inputs are complete enough for
    /// downstream tuning without promoting provenance-only context to semantics.
    pub fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.schema_version != SCHEDULER_COORDINATION_EVIDENCE_SCHEMA_VERSION {
            return Err(SchedulerEvidenceError::UnsupportedSchemaVersion {
                expected: SCHEDULER_COORDINATION_EVIDENCE_SCHEMA_VERSION.to_string(),
                found: self.schema_version.clone(),
            });
        }
        validate_hash(&self.source_bundle_hash)?;
        if self.source_pack_id.trim().is_empty() || self.source_run_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyEvidenceInputId);
        }
        if self.evidence_inputs.is_empty() {
            return Err(SchedulerEvidenceError::EmptyEvidenceInputSet);
        }
        for input in &self.evidence_inputs {
            input.validate()?;
        }
        Ok(())
    }
}

/// One scheduler evidence input for a real coordination-pressure family.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerCoordinationEvidenceInput {
    /// Stable input id used by planner and profile consumers.
    pub evidence_input_id: String,
    /// Runtime workload corpus id for replay.
    pub workload_id: String,
    /// Scheduler workload class used for recommendation grouping.
    pub workload_class: SchedulerWorkloadClass,
    /// Coordination family represented by this input.
    pub scenario_family: CoordinationPressureFamily,
    /// Dimensions that carry actual runtime pressure.
    pub semantic_pressure: Vec<String>,
    /// Redacted context retained only for replay/audit provenance.
    pub provenance_only_context: Vec<String>,
    /// Accepted source events folded into this input.
    pub source_event_count: usize,
    /// Stable event hashes backing the input.
    pub source_hashes: Vec<String>,
    /// Hash of the source bundle backing the input.
    pub source_bundle_hash: String,
}

impl SchedulerCoordinationEvidenceInput {
    /// Validate the semantic/provenance split and deterministic source anchors.
    pub fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.evidence_input_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyEvidenceInputId);
        }
        if self.workload_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyCoordinationWorkloadId);
        }
        if self.semantic_pressure.is_empty()
            || self
                .semantic_pressure
                .iter()
                .any(|item| item.trim().is_empty())
        {
            return Err(SchedulerEvidenceError::EmptySemanticPressure);
        }
        if self.provenance_only_context.is_empty()
            || self
                .provenance_only_context
                .iter()
                .any(|item| item.trim().is_empty())
        {
            return Err(SchedulerEvidenceError::EmptyProvenanceContext);
        }
        if self.source_event_count == 0 {
            return Err(SchedulerEvidenceError::ZeroSourceEventCount);
        }
        validate_hash(&self.source_bundle_hash)?;
        if self.source_hashes.is_empty() {
            return Err(SchedulerEvidenceError::EmptySourceHash);
        }
        for hash in &self.source_hashes {
            validate_hash(hash)?;
        }
        Ok(())
    }
}

/// Coordination pressure families that can be promoted into scheduler inputs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CoordinationPressureFamily {
    /// Advisory tracker/file-lock contention.
    TrackerLockContention,
    /// Concurrent remote proof/build activity.
    ConcurrentRchProofs,
    /// Dirty-frontier refusal and retry pressure.
    FailClosedDirtyFrontier,
    /// Tail pressure from collecting and indexing proof artifacts.
    ArtifactRetrievalTail,
    /// Fan-out from proof runner and robot-plan work.
    ProofRunnerFanout,
    /// Stale in-progress issue reclaim loops.
    StaleInProgressReclaim,
    /// Mail acknowledgement and coordination latency bursts.
    CoordinationLatencyBurst,
}

/// Stable schema for explicit host/resource snapshots used by swarm-scale policy.
pub const SWARM_CAPACITY_SNAPSHOT_SCHEMA_VERSION: &str = "asupersync.swarm-capacity-snapshot.v1";

/// Stable schema for admission reports derived from swarm capacity snapshots.
pub const SWARM_ADMISSION_POLICY_REPORT_SCHEMA_VERSION: &str =
    "asupersync.swarm-admission-policy-report.v1";

/// Stable schema for memory budget plans derived from swarm capacity snapshots.
pub const SWARM_MEMORY_BUDGET_PLAN_SCHEMA_VERSION: &str = "asupersync.swarm-memory-budget-plan.v1";

/// Stable schema for memory residency and brownout policy plans.
pub const SWARM_MEMORY_RESIDENCY_POLICY_SCHEMA_VERSION: &str =
    "asupersync.swarm-memory-residency-policy.v1";

/// Deterministic, capability-supplied host capacity snapshot.
///
/// This type is intentionally inert: it records capacity and coordination
/// signals supplied by an adapter or fixture, but it never reads ambient OS
/// state and never authorizes cleanup by itself.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmCapacitySnapshot {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Stable snapshot/run identifier.
    pub snapshot_id: String,
    /// CPU topology hints supplied by the caller.
    pub cpu: SwarmCpuTopologyHints,
    /// Memory availability and pressure tier.
    pub memory: SwarmMemoryCapacity,
    /// Disk availability and pressure tier.
    pub disk: SwarmDiskCapacity,
    /// Remote build/admission state for rch-backed proof work.
    pub rch: SwarmRchCapacity,
    /// Agent Mail and Beads coordination backlog signals.
    pub coordination: SwarmCoordinationBacklogSignals,
}

impl SwarmCapacitySnapshot {
    /// Validate that the snapshot is complete enough to feed deterministic
    /// admission or replay tests without implying ambient authority.
    pub fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.schema_version != SWARM_CAPACITY_SNAPSHOT_SCHEMA_VERSION {
            return Err(SchedulerEvidenceError::UnsupportedSchemaVersion {
                expected: SWARM_CAPACITY_SNAPSHOT_SCHEMA_VERSION.to_string(),
                found: self.schema_version.clone(),
            });
        }
        if self.snapshot_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyCapacitySnapshotId);
        }
        self.cpu.validate()?;
        self.memory.validate()?;
        self.disk.validate()?;
        Ok(())
    }

    /// Classify work lanes from this explicit snapshot.
    ///
    /// The policy is work-conserving but fail-closed for local artifact and
    /// cleanup surfaces: disk pressure never blocks source-only planning, but
    /// it does defer artifact-heavy local work and requires explicit cleanup
    /// authorization.
    pub fn admission_report(&self) -> Result<SwarmAdmissionReport, SchedulerEvidenceError> {
        self.validate()?;

        let lanes = vec![
            self.classify_source_only_lane(),
            self.classify_tracker_planning_lane(),
            self.classify_remote_proof_lane(),
            self.classify_local_artifact_lane(),
            self.classify_cleanup_authorization_lane(),
        ];
        let recommended_lane = self.recommend_admission_lane(&lanes);

        Ok(SwarmAdmissionReport {
            schema_version: SWARM_ADMISSION_POLICY_REPORT_SCHEMA_VERSION.to_string(),
            source_snapshot_id: self.snapshot_id.clone(),
            recommended_lane,
            lanes,
        })
    }

    /// Derive explicit per-lane memory budgets from this snapshot.
    ///
    /// The planner is deterministic and capability-fed: it only consumes this
    /// snapshot, preserves an emergency reserve first, and reduces proof/cache
    /// growth before interactive runtime state when pressure rises.
    pub fn memory_budget_plan(&self) -> Result<SwarmMemoryBudgetPlan, SchedulerEvidenceError> {
        self.validate()?;

        let host_tier = SwarmMemoryHostTier::classify(self.memory.total_bytes);
        let available_bytes = self.memory.available_bytes.unwrap_or(0);
        let total_bytes = self.memory.total_bytes.unwrap_or(available_bytes);
        let emergency_reserve_bytes = host_tier.emergency_reserve_bytes(
            total_bytes,
            available_bytes,
            self.memory.pressure_tier,
        );
        let allocatable_bytes = available_bytes.saturating_sub(emergency_reserve_bytes);
        let (interactive_weight, trace_weight, proof_weight, cache_weight) =
            host_tier.budget_weights(self.memory.pressure_tier);
        let total_weight = interactive_weight + trace_weight + proof_weight + cache_weight;

        let lane_budget = |weight| {
            if total_weight == 0 {
                0
            } else {
                allocatable_bytes.saturating_mul(weight) / total_weight
            }
        };

        let interactive_runtime_bytes = lane_budget(interactive_weight);
        let trace_replay_bytes = lane_budget(trace_weight);
        let proof_artifact_staging_bytes = lane_budget(proof_weight);
        let compiler_cache_bytes = lane_budget(cache_weight);
        let total_planned_bytes = interactive_runtime_bytes
            .saturating_add(trace_replay_bytes)
            .saturating_add(proof_artifact_staging_bytes)
            .saturating_add(compiler_cache_bytes);

        Ok(SwarmMemoryBudgetPlan {
            schema_version: SWARM_MEMORY_BUDGET_PLAN_SCHEMA_VERSION.to_string(),
            source_snapshot_id: self.snapshot_id.clone(),
            host_tier,
            pressure_tier: self.memory.pressure_tier,
            available_bytes,
            total_bytes,
            emergency_reserve_bytes,
            interactive_runtime_bytes,
            trace_replay_bytes,
            proof_artifact_staging_bytes,
            compiler_cache_bytes,
            total_planned_bytes,
        })
    }

    /// Plan region/workload memory residency without reading ambient host state.
    ///
    /// The decision uses the explicit capacity snapshot plus a caller-supplied
    /// request. It never authorizes cleanup or disables critical runtime
    /// surfaces; when inputs are stale or contradictory it emits a no-win
    /// receipt instead of overclaiming available memory.
    pub fn memory_residency_policy(
        &self,
        request: &SwarmMemoryResidencyRequest,
    ) -> Result<SwarmMemoryResidencyPlan, SchedulerEvidenceError> {
        self.validate()?;
        request.validate()?;

        let budget = self.memory_budget_plan()?;
        let lane_budget_bytes = request.workload_class.lane_budget_bytes(&budget);
        let before = SwarmMemoryResidencyEnvelope {
            available_bytes: budget.available_bytes,
            emergency_reserve_bytes: budget.emergency_reserve_bytes,
            lane_budget_bytes,
            pressure_tier: budget.pressure_tier,
            host_tier: budget.host_tier,
        };
        let metrics_stale = request.metrics_age_secs > request.max_metrics_age_secs;
        let contradictory_policy = request.minimum_hot_bytes > request.requested_bytes;

        let (decision, fallback_reason) = if contradictory_policy {
            (
                SwarmMemoryResidencyDecision::RefuseNoWin,
                Some(SwarmMemoryResidencyFallbackReason::ContradictoryPolicy),
            )
        } else if metrics_stale {
            (
                SwarmMemoryResidencyDecision::RefuseNoWin,
                Some(SwarmMemoryResidencyFallbackReason::StaleMetrics),
            )
        } else if lane_budget_bytes == 0 && request.requested_bytes > 0 {
            (
                SwarmMemoryResidencyDecision::RefuseNoWin,
                Some(SwarmMemoryResidencyFallbackReason::EmptyBudget),
            )
        } else if request.requested_bytes <= lane_budget_bytes {
            (SwarmMemoryResidencyDecision::AdmitHot, None)
        } else if request.spill_allowed && request.minimum_hot_bytes <= lane_budget_bytes {
            (SwarmMemoryResidencyDecision::SpillCold, None)
        } else if request.brownout_allowed && request.workload_class.brownout_eligible() {
            (SwarmMemoryResidencyDecision::BrownoutOptional, None)
        } else {
            (
                SwarmMemoryResidencyDecision::RefuseNoWin,
                Some(SwarmMemoryResidencyFallbackReason::NoSafeResidencyTier),
            )
        };

        let minimum_hot_bytes = request.minimum_hot_bytes.min(request.requested_bytes);
        let (
            hot_resident_bytes,
            warm_resident_bytes,
            spilled_bytes,
            browned_out_bytes,
            refused_bytes,
        ) = memory_residency_counts(
            request.requested_bytes,
            minimum_hot_bytes,
            lane_budget_bytes,
            decision,
        );
        let after = SwarmMemoryResidencyEnvelope {
            available_bytes: before
                .available_bytes
                .saturating_sub(hot_resident_bytes.saturating_add(warm_resident_bytes)),
            emergency_reserve_bytes: before.emergency_reserve_bytes,
            lane_budget_bytes: before.lane_budget_bytes,
            pressure_tier: before.pressure_tier,
            host_tier: before.host_tier,
        };
        let residency_tier = residency_tier_for_decision(decision);
        let brownout_class = brownout_class_for_decision(decision, budget.pressure_tier);
        let no_win_decision = decision == SwarmMemoryResidencyDecision::RefuseNoWin;
        let explanation = memory_residency_explanation(
            request,
            decision,
            fallback_reason,
            lane_budget_bytes,
            hot_resident_bytes,
            warm_resident_bytes,
            spilled_bytes,
            browned_out_bytes,
            refused_bytes,
        );

        Ok(SwarmMemoryResidencyPlan {
            schema_version: SWARM_MEMORY_RESIDENCY_POLICY_SCHEMA_VERSION.to_string(),
            source_snapshot_id: self.snapshot_id.clone(),
            policy_id: request.policy_id.clone(),
            workload_id: request.workload_id.clone(),
            workload_class: request.workload_class,
            affected_region_id: request.affected_region_id.clone(),
            affected_task_ids: sorted_unique_strings(&request.affected_task_ids),
            proof_lane_id: request.proof_lane_id.clone(),
            proof_command: request.proof_command.clone(),
            decision,
            residency_tier,
            brownout_class,
            fallback_reason,
            before,
            after,
            requested_bytes: request.requested_bytes,
            minimum_hot_bytes,
            hot_resident_bytes,
            warm_resident_bytes,
            spilled_bytes,
            browned_out_bytes,
            refused_bytes,
            metrics_age_secs: request.metrics_age_secs,
            max_metrics_age_secs: request.max_metrics_age_secs,
            metrics_stale,
            no_win_decision,
            preserved_invariants: SwarmMemoryProtectedInvariant::all(),
            explanation,
        })
    }

    fn classify_source_only_lane(&self) -> SwarmLaneAdmission {
        let mut reason_codes = vec![SwarmAdmissionReasonCode::SourceOnlyAlwaysAvailable];
        if self.disk.pressure_level == SwarmDiskPressureLevel::Critical {
            reason_codes.push(SwarmAdmissionReasonCode::DiskCriticalPreferSourceOnly);
        }
        if self.coordination.active_dirty_paths > 0 {
            reason_codes.push(SwarmAdmissionReasonCode::PeerDirtyPathsRequireNarrowReservations);
        }
        if self.coordination.ready_beads == 0 {
            reason_codes.push(SwarmAdmissionReasonCode::SparseReadyQueueUseFallback);
        }
        SwarmLaneAdmission {
            lane: SwarmAdmissionLane::InteractiveSourceOnly,
            decision: SwarmAdmissionDecision::Admit,
            validation_class: SwarmValidationClass::SourceOnly,
            reason_codes,
        }
    }

    fn recommend_admission_lane(&self, lanes: &[SwarmLaneAdmission]) -> SwarmAdmissionLane {
        let is_admitted = |candidate| {
            lanes.iter().any(|lane| {
                lane.lane == candidate && lane.decision == SwarmAdmissionDecision::Admit
            })
        };

        if self.coordination.ready_beads == 0
            || self.disk.pressure_level == SwarmDiskPressureLevel::Critical
            || matches!(
                self.memory.pressure_tier,
                SwarmMemoryPressureTier::Saturated | SwarmMemoryPressureTier::Critical
            )
        {
            if is_admitted(SwarmAdmissionLane::InteractiveSourceOnly) {
                return SwarmAdmissionLane::InteractiveSourceOnly;
            }
        }

        if is_admitted(SwarmAdmissionLane::RemoteProof) {
            return SwarmAdmissionLane::RemoteProof;
        }
        if is_admitted(SwarmAdmissionLane::InteractiveSourceOnly) {
            return SwarmAdmissionLane::InteractiveSourceOnly;
        }
        if is_admitted(SwarmAdmissionLane::TrackerOnlyPlanning) {
            return SwarmAdmissionLane::TrackerOnlyPlanning;
        }

        SwarmAdmissionLane::TrackerOnlyPlanning
    }

    fn classify_tracker_planning_lane(&self) -> SwarmLaneAdmission {
        let mut reason_codes = vec![SwarmAdmissionReasonCode::TrackerPlanningAlwaysAvailable];
        if self.coordination.ready_beads == 0 {
            reason_codes.push(SwarmAdmissionReasonCode::SparseReadyQueueUseFallback);
        }
        SwarmLaneAdmission {
            lane: SwarmAdmissionLane::TrackerOnlyPlanning,
            decision: SwarmAdmissionDecision::Admit,
            validation_class: SwarmValidationClass::SourceOnly,
            reason_codes,
        }
    }

    fn classify_remote_proof_lane(&self) -> SwarmLaneAdmission {
        let mut reason_codes = Vec::new();
        let decision = match self.rch.admissibility {
            SwarmRchAdmissibility::Available => {
                reason_codes.push(SwarmAdmissionReasonCode::RchAvailable);
                SwarmAdmissionDecision::Admit
            }
            SwarmRchAdmissibility::Degraded => {
                reason_codes.push(SwarmAdmissionReasonCode::RchDegraded);
                SwarmAdmissionDecision::Admit
            }
            SwarmRchAdmissibility::Unavailable => {
                reason_codes.push(SwarmAdmissionReasonCode::RchUnavailable);
                SwarmAdmissionDecision::Defer
            }
            SwarmRchAdmissibility::DeferredByPolicy => {
                reason_codes.push(SwarmAdmissionReasonCode::RchDeferredByPolicy);
                SwarmAdmissionDecision::Defer
            }
            SwarmRchAdmissibility::Unknown => {
                reason_codes.push(SwarmAdmissionReasonCode::RchUnknown);
                SwarmAdmissionDecision::Defer
            }
        };
        if self.disk.pressure_level == SwarmDiskPressureLevel::Critical {
            reason_codes.push(SwarmAdmissionReasonCode::DiskCriticalRemoteOnly);
        }
        SwarmLaneAdmission {
            lane: SwarmAdmissionLane::RemoteProof,
            decision,
            validation_class: SwarmValidationClass::RemoteRch,
            reason_codes,
        }
    }

    fn classify_local_artifact_lane(&self) -> SwarmLaneAdmission {
        let mut reason_codes = Vec::new();
        let mut decision = SwarmAdmissionDecision::Admit;

        match self.disk.pressure_level {
            SwarmDiskPressureLevel::Healthy => {
                reason_codes.push(SwarmAdmissionReasonCode::DiskHealthy);
            }
            SwarmDiskPressureLevel::Low => {
                decision = SwarmAdmissionDecision::Defer;
                reason_codes.push(SwarmAdmissionReasonCode::DiskLowPreferRemoteOrSourceOnly);
            }
            SwarmDiskPressureLevel::Critical => {
                decision = SwarmAdmissionDecision::Defer;
                reason_codes.push(SwarmAdmissionReasonCode::DiskCriticalBlocksLocalArtifacts);
            }
            SwarmDiskPressureLevel::Unknown => {
                decision = SwarmAdmissionDecision::Defer;
                reason_codes.push(SwarmAdmissionReasonCode::DiskUnknownBlocksLocalArtifacts);
            }
        }
        if matches!(
            self.memory.pressure_tier,
            SwarmMemoryPressureTier::Saturated | SwarmMemoryPressureTier::Critical
        ) {
            decision = SwarmAdmissionDecision::Defer;
            reason_codes.push(SwarmAdmissionReasonCode::MemoryPressureBlocksArtifactGrowth);
        }

        SwarmLaneAdmission {
            lane: SwarmAdmissionLane::LocalArtifactRetrieval,
            decision,
            validation_class: SwarmValidationClass::LocalArtifact,
            reason_codes,
        }
    }

    fn classify_cleanup_authorization_lane(&self) -> SwarmLaneAdmission {
        let mut reason_codes = vec![SwarmAdmissionReasonCode::CleanupRequiresAuthorization];
        if self.disk.pressure_level == SwarmDiskPressureLevel::Critical {
            reason_codes.push(SwarmAdmissionReasonCode::DiskCriticalNeedsCleanupReview);
        }
        SwarmLaneAdmission {
            lane: SwarmAdmissionLane::CleanupAuthorization,
            decision: SwarmAdmissionDecision::RequireAuthorization,
            validation_class: SwarmValidationClass::HumanAuthorizedCleanup,
            reason_codes,
        }
    }
}

/// Memory host tier used by the deterministic budget planner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryHostTier {
    /// Total memory was not reported.
    Unknown,
    /// Small or laptop-class host, up to roughly 64 GiB.
    Small,
    /// Workstation/server host below the high-memory threshold.
    Standard,
    /// High-memory swarm host, 256 GiB or more.
    HighMemory,
}

impl SwarmMemoryHostTier {
    fn classify(total_bytes: Option<u64>) -> Self {
        const GIB: u64 = 1_024 * 1_024 * 1_024;

        match total_bytes {
            None => Self::Unknown,
            Some(bytes) if bytes >= 256 * GIB => Self::HighMemory,
            Some(bytes) if bytes > 64 * GIB => Self::Standard,
            Some(_) => Self::Small,
        }
    }

    fn emergency_reserve_bytes(
        self,
        total_bytes: u64,
        available_bytes: u64,
        pressure_tier: SwarmMemoryPressureTier,
    ) -> u64 {
        const GIB: u64 = 1_024 * 1_024 * 1_024;

        let tier_floor = match self {
            Self::Unknown => 0,
            Self::Small => 4 * GIB,
            Self::Standard => 12 * GIB,
            Self::HighMemory => 32 * GIB,
        };
        let ratio_floor = total_bytes / 10;
        let healthy_reserve = tier_floor.max(ratio_floor);

        let pressure_reserve = match pressure_tier {
            SwarmMemoryPressureTier::Unknown => healthy_reserve.max(available_bytes / 4),
            SwarmMemoryPressureTier::Healthy => healthy_reserve,
            SwarmMemoryPressureTier::Low => healthy_reserve.max(available_bytes / 5),
            SwarmMemoryPressureTier::Saturated => healthy_reserve.max(available_bytes / 3),
            SwarmMemoryPressureTier::Critical => healthy_reserve.max(available_bytes / 2),
        };
        if available_bytes == 0 {
            0
        } else {
            pressure_reserve.min(available_bytes.saturating_sub(1))
        }
    }

    fn budget_weights(self, pressure_tier: SwarmMemoryPressureTier) -> (u64, u64, u64, u64) {
        match pressure_tier {
            SwarmMemoryPressureTier::Critical => (90, 10, 0, 0),
            SwarmMemoryPressureTier::Saturated => (70, 20, 5, 5),
            SwarmMemoryPressureTier::Low => (45, 30, 15, 10),
            SwarmMemoryPressureTier::Unknown => (80, 20, 0, 0),
            SwarmMemoryPressureTier::Healthy => match self {
                Self::Unknown => (80, 20, 0, 0),
                Self::Small => (45, 25, 15, 15),
                Self::Standard => (35, 30, 20, 15),
                Self::HighMemory => (25, 35, 25, 15),
            },
        }
    }
}

/// Deterministic per-lane memory budget plan.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmMemoryBudgetPlan {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Snapshot identifier copied from the source snapshot.
    pub source_snapshot_id: String,
    /// Host tier selected from explicit total memory.
    pub host_tier: SwarmMemoryHostTier,
    /// Pressure tier copied from the source snapshot.
    pub pressure_tier: SwarmMemoryPressureTier,
    /// Available memory supplied by the adapter.
    pub available_bytes: u64,
    /// Total memory supplied by the adapter, or available bytes if total is absent.
    pub total_bytes: u64,
    /// Reserve preserved before assigning any lane budget.
    pub emergency_reserve_bytes: u64,
    /// Interactive runtime state and source-only coordination budget.
    pub interactive_runtime_bytes: u64,
    /// Trace and replay buffer budget.
    pub trace_replay_bytes: u64,
    /// Proof artifact staging budget.
    pub proof_artifact_staging_bytes: u64,
    /// Reusable compiler/build cache budget.
    pub compiler_cache_bytes: u64,
    /// Sum of lane budgets, excluding emergency reserve.
    pub total_planned_bytes: u64,
}

/// Workload class for the memory residency planner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryResidencyWorkloadClass {
    /// Region/task state that must stay resident for runtime progress.
    InteractiveRuntime,
    /// Trace replay buffers that can be resized but should not disappear.
    TraceReplay,
    /// Remote-proof artifacts that can spill to external artifact storage.
    ProofArtifact,
    /// Compiler/cache growth that is useful but noncritical.
    CompilerCache,
    /// Rich diagnostics and formatting that can be browned out first.
    OptionalDiagnostics,
}

impl SwarmMemoryResidencyWorkloadClass {
    const fn lane_budget_bytes(self, budget: &SwarmMemoryBudgetPlan) -> u64 {
        match self {
            Self::InteractiveRuntime => budget.interactive_runtime_bytes,
            Self::TraceReplay => budget.trace_replay_bytes,
            Self::ProofArtifact => budget.proof_artifact_staging_bytes,
            Self::CompilerCache | Self::OptionalDiagnostics => budget.compiler_cache_bytes,
        }
    }

    const fn brownout_eligible(self) -> bool {
        matches!(
            self,
            Self::ProofArtifact | Self::CompilerCache | Self::OptionalDiagnostics
        )
    }
}

/// Residency tier selected for a workload.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryResidencyTier {
    /// The whole request remains hot and resident.
    Hot,
    /// The request remains resident, but not all bytes are hot-priority.
    Warm,
    /// Cold bytes should spill to an explicit artifact/cache surface.
    SpillEligible,
    /// Optional bytes should be browned out before runtime state is touched.
    BrownoutEligible,
    /// No safe residency tier exists for this request.
    Refused,
}

/// Brownout class selected by the residency planner.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryBrownoutClass {
    /// No brownout is needed.
    None,
    /// Inputs are healthy enough to observe only.
    Observe,
    /// Optional bytes are degraded under pressure.
    DegradeOptional,
    /// Optional bytes are shed or excluded.
    ShedOptional,
    /// The planner emitted a no-win receipt.
    NoWin,
}

/// Deterministic memory residency decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryResidencyDecision {
    /// Admit the whole request as hot resident state.
    AdmitHot,
    /// Keep hot/warm bytes resident and spill the cold remainder.
    SpillCold,
    /// Preserve hot/warm bytes and brown out optional remainder.
    BrownoutOptional,
    /// Refuse or defer with an explicit no-win reason.
    RefuseNoWin,
}

/// Fail-closed fallback reason for no-win memory decisions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryResidencyFallbackReason {
    /// The selected workload lane had no usable memory budget.
    EmptyBudget,
    /// Metrics were older than the request allows.
    StaleMetrics,
    /// The request contradicted itself.
    ContradictoryPolicy,
    /// No hot, spill, or brownout tier could preserve invariants.
    NoSafeResidencyTier,
}

/// Runtime invariants that memory brownout must preserve.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryProtectedInvariant {
    /// Scheduler and worker progress remain available.
    CoreScheduling,
    /// Cancellation request/drain/finalize remains available.
    CancellationDrain,
    /// Race losers remain drainable.
    LoserDrain,
    /// Region close still implies quiescence.
    RegionQuiescence,
    /// Permits, acks, and leases still resolve.
    ObligationCleanup,
}

impl SwarmMemoryProtectedInvariant {
    fn all() -> Vec<Self> {
        vec![
            Self::CoreScheduling,
            Self::CancellationDrain,
            Self::LoserDrain,
            Self::RegionQuiescence,
            Self::ObligationCleanup,
        ]
    }
}

/// Memory envelope before or after a residency decision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmMemoryResidencyEnvelope {
    /// Available bytes from the explicit capacity snapshot.
    pub available_bytes: u64,
    /// Emergency reserve that must remain outside lane budgets.
    pub emergency_reserve_bytes: u64,
    /// Budget selected for the workload class.
    pub lane_budget_bytes: u64,
    /// Memory pressure tier copied from the capacity snapshot.
    pub pressure_tier: SwarmMemoryPressureTier,
    /// Host tier derived from the capacity snapshot.
    pub host_tier: SwarmMemoryHostTier,
}

/// Request for a deterministic memory residency decision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmMemoryResidencyRequest {
    /// Stable policy/run identifier.
    pub policy_id: String,
    /// Stable workload identifier.
    pub workload_id: String,
    /// Class used to select the applicable lane budget.
    pub workload_class: SwarmMemoryResidencyWorkloadClass,
    /// Region affected by this decision, if known.
    pub affected_region_id: Option<String>,
    /// Task ids affected by this decision.
    pub affected_task_ids: Vec<String>,
    /// Bytes requested by the workload.
    pub requested_bytes: u64,
    /// Minimum bytes that must remain hot to preserve progress.
    pub minimum_hot_bytes: u64,
    /// Whether cold bytes may spill to a non-resident artifact/cache surface.
    pub spill_allowed: bool,
    /// Whether optional bytes may brown out.
    pub brownout_allowed: bool,
    /// Age of the capacity and scheduler metrics.
    pub metrics_age_secs: u64,
    /// Maximum accepted metrics age for this request.
    pub max_metrics_age_secs: u64,
    /// Proof lane that should validate this policy, if known.
    pub proof_lane_id: Option<String>,
    /// Exact proof command for handoff, if known.
    pub proof_command: Option<String>,
}

impl SwarmMemoryResidencyRequest {
    fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.policy_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyMemoryResidencyPolicyId);
        }
        if self.workload_id.trim().is_empty() {
            return Err(SchedulerEvidenceError::EmptyMemoryResidencyWorkloadId);
        }
        Ok(())
    }
}

/// Deterministic memory residency and brownout policy report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmMemoryResidencyPlan {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Capacity snapshot id copied from the source snapshot.
    pub source_snapshot_id: String,
    /// Policy id copied from the request.
    pub policy_id: String,
    /// Workload id copied from the request.
    pub workload_id: String,
    /// Workload class copied from the request.
    pub workload_class: SwarmMemoryResidencyWorkloadClass,
    /// Region affected by this decision, if known.
    pub affected_region_id: Option<String>,
    /// Task ids affected by this decision, sorted and deduplicated.
    pub affected_task_ids: Vec<String>,
    /// Proof lane that should validate this policy, if known.
    pub proof_lane_id: Option<String>,
    /// Exact proof command for handoff, if known.
    pub proof_command: Option<String>,
    /// Deterministic residency decision.
    pub decision: SwarmMemoryResidencyDecision,
    /// Residency tier selected for the workload.
    pub residency_tier: SwarmMemoryResidencyTier,
    /// Brownout class selected for optional work.
    pub brownout_class: SwarmMemoryBrownoutClass,
    /// Fail-closed fallback reason, if any.
    pub fallback_reason: Option<SwarmMemoryResidencyFallbackReason>,
    /// Memory envelope before applying the decision.
    pub before: SwarmMemoryResidencyEnvelope,
    /// Memory envelope after resident bytes are admitted.
    pub after: SwarmMemoryResidencyEnvelope,
    /// Requested bytes copied from the request.
    pub requested_bytes: u64,
    /// Minimum hot bytes after request normalization.
    pub minimum_hot_bytes: u64,
    /// Bytes kept in the hot resident tier.
    pub hot_resident_bytes: u64,
    /// Bytes kept in the warm resident tier.
    pub warm_resident_bytes: u64,
    /// Bytes assigned to an explicit spill surface.
    pub spilled_bytes: u64,
    /// Bytes browned out from optional surfaces.
    pub browned_out_bytes: u64,
    /// Bytes refused by a no-win decision.
    pub refused_bytes: u64,
    /// Age of the metrics used by the decision.
    pub metrics_age_secs: u64,
    /// Maximum metrics age accepted by the request.
    pub max_metrics_age_secs: u64,
    /// Whether metrics age exceeded the accepted bound.
    pub metrics_stale: bool,
    /// Whether this plan is a no-win receipt.
    pub no_win_decision: bool,
    /// Runtime invariants preserved by every decision.
    pub preserved_invariants: Vec<SwarmMemoryProtectedInvariant>,
    /// Deterministic operator-facing explanation lines.
    pub explanation: Vec<String>,
}

/// Work lanes that swarm-scale admission policy classifies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmAdmissionLane {
    /// Latency-sensitive source edits, code review, and lightweight checks.
    InteractiveSourceOnly,
    /// Tracker/mail/planning work that does not need build artifacts.
    TrackerOnlyPlanning,
    /// Remote `rch` proof work without local artifact-heavy retrieval.
    RemoteProof,
    /// Local proof-output retrieval or artifact-heavy analysis.
    LocalArtifactRetrieval,
    /// Cleanup work that may delete or truncate data and needs human approval.
    CleanupAuthorization,
}

/// Deterministic admission decision for one work lane.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmAdmissionDecision {
    /// Work may proceed under the attached validation class.
    Admit,
    /// Work should wait for pressure or capacity to recover.
    Defer,
    /// Work needs explicit human approval before it can proceed.
    RequireAuthorization,
}

/// Validation class an admitted lane should use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmValidationClass {
    /// Source-only checks such as rustfmt, JSON parsing, or diff checks.
    SourceOnly,
    /// Remote `rch` build/test proof.
    RemoteRch,
    /// Local artifact retrieval or artifact inspection.
    LocalArtifact,
    /// Human-authorized cleanup/truncation path.
    HumanAuthorizedCleanup,
}

/// Stable reason codes for lane admission decisions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmAdmissionReasonCode {
    /// Source-only work remains available even under disk pressure.
    SourceOnlyAlwaysAvailable,
    /// Tracker/planning work remains available even when heavy work is unsafe.
    TrackerPlanningAlwaysAvailable,
    /// Red disk should bias agents toward source-only work.
    DiskCriticalPreferSourceOnly,
    /// Red disk does not block remote-only proof submission by itself.
    DiskCriticalRemoteOnly,
    /// Local artifact retrieval is allowed by disk state.
    DiskHealthy,
    /// Low disk should prefer remote or source-only work.
    DiskLowPreferRemoteOrSourceOnly,
    /// Red disk blocks local artifact-heavy work.
    DiskCriticalBlocksLocalArtifacts,
    /// Missing disk pressure data blocks local artifact-heavy work.
    DiskUnknownBlocksLocalArtifacts,
    /// Red disk means cleanup should be reviewed explicitly.
    DiskCriticalNeedsCleanupReview,
    /// rch workers are available.
    RchAvailable,
    /// rch workers are degraded but usable.
    RchDegraded,
    /// rch workers are unavailable.
    RchUnavailable,
    /// rch work was deferred by a higher-level policy.
    RchDeferredByPolicy,
    /// rch capacity is unknown, so remote proof work is deferred.
    RchUnknown,
    /// Memory pressure blocks cache/artifact growth.
    MemoryPressureBlocksArtifactGrowth,
    /// Shared dirty paths require narrow reservations before source work.
    PeerDirtyPathsRequireNarrowReservations,
    /// No ready work is visible; use a safe fallback lane.
    SparseReadyQueueUseFallback,
    /// Cleanup needs explicit authorization and cannot be auto-admitted.
    CleanupRequiresAuthorization,
}

/// Admission decision for one lane.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmLaneAdmission {
    /// Lane being classified.
    pub lane: SwarmAdmissionLane,
    /// Deterministic decision for this lane.
    pub decision: SwarmAdmissionDecision,
    /// Validation/proof class that applies if this lane is taken.
    pub validation_class: SwarmValidationClass,
    /// Stable reason codes explaining the decision.
    pub reason_codes: Vec<SwarmAdmissionReasonCode>,
}

/// Full deterministic report derived from one capacity snapshot.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmAdmissionReport {
    /// Version tag for this schema.
    pub schema_version: String,
    /// Snapshot identifier copied from the source snapshot.
    pub source_snapshot_id: String,
    /// Best work-conserving lane to take under current pressure.
    pub recommended_lane: SwarmAdmissionLane,
    /// Per-lane decisions in stable priority order.
    pub lanes: Vec<SwarmLaneAdmission>,
}

/// Explicit CPU topology hints for swarm-scale admission decisions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SwarmCpuTopologyHints {
    /// Logical CPU count visible to the admission adapter.
    pub logical_cpus: usize,
    /// Physical core count, if the adapter can report it deterministically.
    pub physical_cores: Option<usize>,
    /// NUMA or locality group count, if known.
    pub numa_nodes: Option<usize>,
    /// Worker target that policy should consider for this host.
    pub scheduler_worker_target: Option<usize>,
}

impl SwarmCpuTopologyHints {
    fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        if self.logical_cpus == 0 {
            return Err(SchedulerEvidenceError::InvalidCapacityDimension {
                field: "cpu.logical_cpus",
            });
        }
        if self.physical_cores == Some(0) {
            return Err(SchedulerEvidenceError::InvalidCapacityDimension {
                field: "cpu.physical_cores",
            });
        }
        if self.numa_nodes == Some(0) {
            return Err(SchedulerEvidenceError::InvalidCapacityDimension {
                field: "cpu.numa_nodes",
            });
        }
        if self.scheduler_worker_target == Some(0) {
            return Err(SchedulerEvidenceError::InvalidCapacityDimension {
                field: "cpu.scheduler_worker_target",
            });
        }
        Ok(())
    }
}

/// Coarse memory-pressure tier for deterministic policy fixtures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmMemoryPressureTier {
    /// Adapter did not provide a memory-pressure classification.
    #[default]
    Unknown,
    /// Plenty of memory is available.
    Healthy,
    /// Memory is reduced but not yet blocking ordinary work.
    Low,
    /// Memory pressure should throttle cache/artifact growth.
    Saturated,
    /// Memory pressure should fail closed for memory-heavy lanes.
    Critical,
}

/// Explicit memory capacity inputs for swarm-scale policy.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SwarmMemoryCapacity {
    /// Available memory in bytes, if reported.
    pub available_bytes: Option<u64>,
    /// Total memory in bytes, if reported.
    pub total_bytes: Option<u64>,
    /// Coarse pressure tier supplied by the adapter or fixture.
    #[serde(default)]
    pub pressure_tier: SwarmMemoryPressureTier,
}

impl SwarmMemoryCapacity {
    fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        validate_optional_capacity_pair(
            self.available_bytes,
            self.total_bytes,
            "memory.available_bytes",
            "memory.total_bytes",
        )
    }
}

/// Coarse disk-pressure level for proof/artifact admission.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmDiskPressureLevel {
    /// Adapter did not provide a disk-pressure classification.
    #[default]
    Unknown,
    /// Disk has enough space for normal proof/artifact work.
    Healthy,
    /// Disk is low; source-only and remote-only lanes should be preferred.
    Low,
    /// Disk is red; local artifact retrieval and cleanup need explicit handling.
    Critical,
}

/// Explicit disk capacity inputs for swarm-scale policy.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SwarmDiskCapacity {
    /// Free bytes on the relevant project/artifact filesystem, if reported.
    pub free_bytes: Option<u64>,
    /// Total bytes on the relevant filesystem, if reported.
    pub total_bytes: Option<u64>,
    /// Coarse pressure level supplied by the adapter or fixture.
    #[serde(default)]
    pub pressure_level: SwarmDiskPressureLevel,
}

impl SwarmDiskCapacity {
    fn validate(&self) -> Result<(), SchedulerEvidenceError> {
        validate_optional_capacity_pair(
            self.free_bytes,
            self.total_bytes,
            "disk.free_bytes",
            "disk.total_bytes",
        )
    }
}

/// rch-backed proof-work availability from the coordinator's point of view.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SwarmRchAdmissibility {
    /// Adapter did not provide an rch availability classification.
    #[default]
    Unknown,
    /// Remote proof work can be admitted.
    Available,
    /// Remote proof work is available but degraded.
    Degraded,
    /// Remote proof work should not be admitted now.
    Unavailable,
    /// A higher-level policy intentionally deferred remote proof work.
    DeferredByPolicy,
}

/// Explicit remote proof-worker state for swarm-scale policy.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct SwarmRchCapacity {
    /// rch admission classification.
    #[serde(default)]
    pub admissibility: SwarmRchAdmissibility,
    /// Number of healthy remote workers, if known.
    pub healthy_worker_count: Option<usize>,
    /// Number of currently available remote build slots, if known.
    pub available_slots: Option<usize>,
    /// Stable reason labels explaining degraded/unavailable states.
    #[serde(default)]
    pub blocked_reason_codes: Vec<String>,
}

/// Coordination-control backlog signals that affect work-conserving decisions.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SwarmCoordinationBacklogSignals {
    /// Ready Beads count.
    pub ready_beads: usize,
    /// Open Beads count.
    pub open_beads: usize,
    /// In-progress Beads count.
    pub in_progress_beads: usize,
    /// Active Agent Mail reservations relevant to this project.
    pub active_reservations: usize,
    /// Dirty paths currently visible in the shared worktree.
    pub active_dirty_paths: usize,
    /// Agents with recent activity in the coordination view.
    pub active_agents: usize,
    /// In-progress Beads that look stale to the coordinator.
    pub stale_in_progress_beads: usize,
}

/// Validation and recommendation failures for scheduler evidence artifacts.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum SchedulerEvidenceError {
    /// The artifact schema version does not match the supported contract.
    #[error("unsupported schema version: expected {expected}, found {found}")]
    UnsupportedSchemaVersion {
        /// Supported schema version for the current parser.
        expected: String,
        /// Schema version found in the provided artifact.
        found: String,
    },
    /// The run label was empty or whitespace-only.
    #[error("run label must not be empty")]
    EmptyRunLabel,
    /// The topology declared no worker threads.
    #[error("topology must declare at least one worker thread")]
    ZeroWorkerThreads,
    /// The topology declared no cohorts.
    #[error("topology must declare at least one cohort")]
    ZeroCohortCount,
    /// The topology declared a zero memory budget.
    #[error("topology must declare a non-zero memory budget")]
    ZeroMemoryBudget,
    /// The profiled knob set declared zero workers.
    #[error("current knob profile must declare at least one worker")]
    ZeroCurrentWorkers,
    /// The profiled knob set declared a zero steal batch size.
    #[error("current knob profile must declare a non-zero steal batch size")]
    ZeroStealBatchSize,
    /// The profiled knob set declared a zero cancel streak limit.
    #[error("current knob profile must declare a non-zero cancel streak limit")]
    ZeroCancelStreakLimit,
    /// One percentile trio regressed from sorted order.
    #[error("{field} percentiles must be monotonic (p50 <= p95 <= p99)")]
    NonMonotonicPercentiles {
        /// Percentile family that violated monotonic ordering.
        field: &'static str,
    },
    /// The remote-steal ratio fell outside the valid percentage range.
    #[error("remote steal ratio must be between 0 and 100 inclusive, found {0}")]
    RemoteStealRatioOutOfRange(u8),
    /// A coordination evidence document had no inputs.
    #[error("coordination evidence input set must not be empty")]
    EmptyEvidenceInputSet,
    /// A coordination evidence id was empty.
    #[error("coordination evidence input id must not be empty")]
    EmptyEvidenceInputId,
    /// A coordination workload id was empty.
    #[error("coordination workload id must not be empty")]
    EmptyCoordinationWorkloadId,
    /// Semantic pressure dimensions were absent.
    #[error("coordination evidence must declare semantic pressure dimensions")]
    EmptySemanticPressure,
    /// Provenance-only context was absent.
    #[error("coordination evidence must declare provenance-only context")]
    EmptyProvenanceContext,
    /// No accepted source event backed the input.
    #[error("coordination evidence must include at least one source event")]
    ZeroSourceEventCount,
    /// A source hash field was empty.
    #[error("coordination evidence source hashes must not be empty")]
    EmptySourceHash,
    /// A source hash was not a stable sha256 reference.
    #[error("coordination evidence source hash must start with sha256:, found {found}")]
    InvalidSourceHash {
        /// Hash value that failed validation.
        found: String,
    },
    /// The capacity snapshot id was empty.
    #[error("swarm capacity snapshot id must not be empty")]
    EmptyCapacitySnapshotId,
    /// A memory residency policy id was empty.
    #[error("swarm memory residency policy id must not be empty")]
    EmptyMemoryResidencyPolicyId,
    /// A memory residency workload id was empty.
    #[error("swarm memory residency workload id must not be empty")]
    EmptyMemoryResidencyWorkloadId,
    /// A required capacity dimension was zero or otherwise invalid.
    #[error("swarm capacity dimension is invalid: {field}")]
    InvalidCapacityDimension {
        /// Stable field path for the invalid dimension.
        field: &'static str,
    },
    /// A free/available capacity value exceeded the corresponding total.
    #[error("swarm capacity available field {available_field} exceeds total field {total_field}")]
    CapacityAvailableExceedsTotal {
        /// Field that reported the available/free value.
        available_field: &'static str,
        /// Field that reported the total value.
        total_field: &'static str,
    },
}

fn memory_residency_counts(
    requested_bytes: u64,
    minimum_hot_bytes: u64,
    lane_budget_bytes: u64,
    decision: SwarmMemoryResidencyDecision,
) -> (u64, u64, u64, u64, u64) {
    match decision {
        SwarmMemoryResidencyDecision::AdmitHot => (requested_bytes, 0, 0, 0, 0),
        SwarmMemoryResidencyDecision::SpillCold => {
            let hot = minimum_hot_bytes.min(lane_budget_bytes);
            let warm = requested_bytes
                .saturating_sub(hot)
                .min(lane_budget_bytes.saturating_sub(hot));
            let spilled = requested_bytes.saturating_sub(hot).saturating_sub(warm);
            (hot, warm, spilled, 0, 0)
        }
        SwarmMemoryResidencyDecision::BrownoutOptional => {
            let hot = minimum_hot_bytes.min(lane_budget_bytes);
            let warm = requested_bytes
                .saturating_sub(hot)
                .min(lane_budget_bytes.saturating_sub(hot));
            let browned_out = requested_bytes.saturating_sub(hot).saturating_sub(warm);
            (hot, warm, 0, browned_out, 0)
        }
        SwarmMemoryResidencyDecision::RefuseNoWin => (0, 0, 0, 0, requested_bytes),
    }
}

const fn residency_tier_for_decision(
    decision: SwarmMemoryResidencyDecision,
) -> SwarmMemoryResidencyTier {
    match decision {
        SwarmMemoryResidencyDecision::AdmitHot => SwarmMemoryResidencyTier::Hot,
        SwarmMemoryResidencyDecision::SpillCold => SwarmMemoryResidencyTier::SpillEligible,
        SwarmMemoryResidencyDecision::BrownoutOptional => {
            SwarmMemoryResidencyTier::BrownoutEligible
        }
        SwarmMemoryResidencyDecision::RefuseNoWin => SwarmMemoryResidencyTier::Refused,
    }
}

const fn brownout_class_for_decision(
    decision: SwarmMemoryResidencyDecision,
    pressure_tier: SwarmMemoryPressureTier,
) -> SwarmMemoryBrownoutClass {
    match decision {
        SwarmMemoryResidencyDecision::BrownoutOptional => SwarmMemoryBrownoutClass::ShedOptional,
        SwarmMemoryResidencyDecision::RefuseNoWin => SwarmMemoryBrownoutClass::NoWin,
        SwarmMemoryResidencyDecision::AdmitHot | SwarmMemoryResidencyDecision::SpillCold => {
            match pressure_tier {
                SwarmMemoryPressureTier::Critical | SwarmMemoryPressureTier::Saturated => {
                    SwarmMemoryBrownoutClass::DegradeOptional
                }
                SwarmMemoryPressureTier::Low | SwarmMemoryPressureTier::Unknown => {
                    SwarmMemoryBrownoutClass::Observe
                }
                SwarmMemoryPressureTier::Healthy => SwarmMemoryBrownoutClass::None,
            }
        }
    }
}

fn memory_residency_explanation(
    request: &SwarmMemoryResidencyRequest,
    decision: SwarmMemoryResidencyDecision,
    fallback_reason: Option<SwarmMemoryResidencyFallbackReason>,
    lane_budget_bytes: u64,
    hot_resident_bytes: u64,
    warm_resident_bytes: u64,
    spilled_bytes: u64,
    browned_out_bytes: u64,
    refused_bytes: u64,
) -> Vec<String> {
    let mut explanation = vec![format!(
        "workload={} class={:?} requested={}B lane_budget={}B decision={:?}",
        request.workload_id,
        request.workload_class,
        request.requested_bytes,
        lane_budget_bytes,
        decision
    )];
    if let Some(reason) = fallback_reason {
        explanation.push(format!("fallback_reason={reason:?}"));
    }
    explanation.push(format!(
        "resident_hot={}B resident_warm={}B spilled={}B browned_out={}B refused={}B",
        hot_resident_bytes, warm_resident_bytes, spilled_bytes, browned_out_bytes, refused_bytes
    ));
    explanation.push(
        "core scheduling, cancellation drain, loser drain, region quiescence, and obligation cleanup remain preserved"
            .to_string(),
    );
    explanation
}

fn sorted_unique_strings(values: &[String]) -> Vec<String> {
    let mut sorted = values
        .iter()
        .map(|value| value.trim())
        .filter(|value| !value.is_empty())
        .map(ToString::to_string)
        .collect::<Vec<_>>();
    sorted.sort();
    sorted.dedup();
    sorted
}

fn validate_percentiles<T: Ord>(
    p50: T,
    p95: T,
    p99: T,
    field: &'static str,
) -> Result<(), SchedulerEvidenceError> {
    if p50 > p95 || p95 > p99 {
        return Err(SchedulerEvidenceError::NonMonotonicPercentiles { field });
    }
    Ok(())
}

fn validate_hash(hash: &str) -> Result<(), SchedulerEvidenceError> {
    if hash.trim().is_empty() {
        return Err(SchedulerEvidenceError::EmptySourceHash);
    }
    if !hash.starts_with("sha256:") {
        return Err(SchedulerEvidenceError::InvalidSourceHash {
            found: hash.to_string(),
        });
    }
    Ok(())
}

fn validate_optional_capacity_pair(
    available: Option<u64>,
    total: Option<u64>,
    available_field: &'static str,
    total_field: &'static str,
) -> Result<(), SchedulerEvidenceError> {
    if let (Some(available), Some(total)) = (available, total)
        && available > total
    {
        return Err(SchedulerEvidenceError::CapacityAvailableExceedsTotal {
            available_field,
            total_field,
        });
    }
    Ok(())
}

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

    fn baseline_artifact() -> SchedulerEvidenceArtifact {
        SchedulerEvidenceArtifact {
            schema_version: SCHEDULER_EVIDENCE_SCHEMA_VERSION.to_string(),
            run_label: "unit-baseline-64c".to_string(),
            workload_class: SchedulerWorkloadClass::InteractiveSwarm,
            topology: SchedulerTopologyDescriptor {
                worker_threads: 64,
                cohort_count: 2,
                memory_budget_gib: 256,
            },
            current_knobs: SchedulerKnobProfile {
                worker_threads: 64,
                steal_batch_size: 8,
                cancel_streak_limit: 16,
                global_queue_limit: 0,
                parking_enabled: true,
            },
            metrics: SchedulerEvidenceMetrics {
                wake_to_run_p50_ns: 5_000,
                wake_to_run_p95_ns: 20_000,
                wake_to_run_p99_ns: 60_000,
                queue_residency_p50_ns: 8_000,
                queue_residency_p95_ns: 30_000,
                queue_residency_p99_ns: 90_000,
                ready_backlog_p95: 32,
                ready_backlog_p99: 96,
                cancel_debt_p95: 4,
                cancel_debt_p99: 8,
                remote_steal_ratio_pct: Some(12),
                cross_cohort_wake_p99_ns: Some(70_000),
            },
            notes: vec!["unit".to_string()],
        }
    }

    fn coordination_input(
        family: CoordinationPressureFamily,
        workload_id: &str,
    ) -> SchedulerCoordinationEvidenceInput {
        SchedulerCoordinationEvidenceInput {
            evidence_input_id: format!("coordination-evidence-{workload_id}"),
            workload_id: workload_id.to_string(),
            workload_class: SchedulerWorkloadClass::InteractiveSwarm,
            scenario_family: family,
            semantic_pressure: vec![
                "ready-backlog".to_string(),
                "queue-residency-tail".to_string(),
            ],
            provenance_only_context: vec![
                "pseudonymized-agent".to_string(),
                "hashed-path".to_string(),
            ],
            source_event_count: 2,
            source_hashes: vec!["sha256:event-a".to_string(), "sha256:event-b".to_string()],
            source_bundle_hash: "sha256:coordination-bundle".to_string(),
        }
    }

    #[test]
    fn validate_rejects_schema_and_required_zero_fields() {
        let mut artifact = baseline_artifact();
        artifact.schema_version = "asupersync.scheduler-evidence.v0".to_string();
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::UnsupportedSchemaVersion {
                expected: SCHEDULER_EVIDENCE_SCHEMA_VERSION.to_string(),
                found: "asupersync.scheduler-evidence.v0".to_string(),
            })
        );

        let mut artifact = baseline_artifact();
        artifact.run_label = "   ".to_string();
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::EmptyRunLabel)
        );

        let mut artifact = baseline_artifact();
        artifact.topology.worker_threads = 0;
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::ZeroWorkerThreads)
        );

        let mut artifact = baseline_artifact();
        artifact.current_knobs.steal_batch_size = 0;
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::ZeroStealBatchSize)
        );
    }

    #[test]
    fn validate_rejects_metric_boundary_violations() {
        let mut artifact = baseline_artifact();
        artifact.metrics.wake_to_run_p95_ns = artifact.metrics.wake_to_run_p50_ns - 1;
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::NonMonotonicPercentiles {
                field: "wake_to_run",
            })
        );

        let mut artifact = baseline_artifact();
        artifact.metrics.remote_steal_ratio_pct = Some(101);
        assert_eq!(
            artifact.validate(),
            Err(SchedulerEvidenceError::RemoteStealRatioOutOfRange(101))
        );
    }

    #[test]
    fn tune_report_keeps_conservative_fallback_for_balanced_baseline() {
        let artifact = baseline_artifact();
        let report = artifact
            .tune_report()
            .expect("balanced artifact should tune");

        assert_eq!(report.profile_name, "conservative_baseline");
        assert_eq!(report.recommended_knobs, artifact.current_knobs);
        assert_eq!(report.fallback_profile, artifact.current_knobs);
        assert_eq!(report.global_queue_limit_hint, None);
        assert_eq!(
            report.reason_codes,
            vec![SchedulerRecommendationReason::BalancedBaseline]
        );
        assert_eq!(report.confidence_percent, 65);
        assert!(
            report
                .explanation
                .iter()
                .any(|line| line.contains("conservative baseline envelope"))
        );
    }

    #[test]
    fn coordination_evidence_inputs_validate_all_pressure_families() {
        let evidence = SchedulerCoordinationEvidenceInputs {
            schema_version: SCHEDULER_COORDINATION_EVIDENCE_SCHEMA_VERSION.to_string(),
            source_pack_id: "agent-swarm-coordination-pressure".to_string(),
            source_bundle_hash: "sha256:coordination-runtime-fixture".to_string(),
            source_run_id: "coordination-runtime-fixture-accepted-all-families".to_string(),
            evidence_inputs: vec![
                coordination_input(
                    CoordinationPressureFamily::TrackerLockContention,
                    "ASWARM-WL-LOCK-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::ConcurrentRchProofs,
                    "ASWARM-WL-RCH-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::FailClosedDirtyFrontier,
                    "ASWARM-WL-DIRTY-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::ArtifactRetrievalTail,
                    "ASWARM-WL-ARTIFACT-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::ProofRunnerFanout,
                    "ASWARM-WL-FANOUT-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::StaleInProgressReclaim,
                    "ASWARM-WL-STALE-001",
                ),
                coordination_input(
                    CoordinationPressureFamily::CoordinationLatencyBurst,
                    "ASWARM-WL-LATENCY-001",
                ),
            ],
        };

        evidence
            .validate()
            .expect("coordination evidence validates");
    }

    #[test]
    fn coordination_evidence_rejects_missing_semantics_and_unstable_hashes() {
        let mut evidence = SchedulerCoordinationEvidenceInputs {
            schema_version: SCHEDULER_COORDINATION_EVIDENCE_SCHEMA_VERSION.to_string(),
            source_pack_id: "agent-swarm-coordination-pressure".to_string(),
            source_bundle_hash: "sha256:coordination-runtime-fixture".to_string(),
            source_run_id: "coordination-runtime-fixture-accepted-all-families".to_string(),
            evidence_inputs: vec![coordination_input(
                CoordinationPressureFamily::TrackerLockContention,
                "ASWARM-WL-LOCK-001",
            )],
        };

        evidence.evidence_inputs[0].semantic_pressure.clear();
        assert_eq!(
            evidence.validate(),
            Err(SchedulerEvidenceError::EmptySemanticPressure)
        );

        evidence.evidence_inputs[0].semantic_pressure = vec!["ready-backlog".to_string()];
        evidence.evidence_inputs[0].source_hashes = vec!["not-a-sha".to_string()];
        assert_eq!(
            evidence.validate(),
            Err(SchedulerEvidenceError::InvalidSourceHash {
                found: "not-a-sha".to_string(),
            })
        );

        evidence.evidence_inputs.clear();
        assert_eq!(
            evidence.validate(),
            Err(SchedulerEvidenceError::EmptyEvidenceInputSet)
        );
    }
}