lattice-common 2026.1.203

Shared types, configuration, and error handling for Lattice scheduler
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
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

// ─── Identifiers ────────────────────────────────────────────

pub type AllocId = Uuid;
pub type NodeId = String; // xname-style: x1000c0s0b0n0
pub type TenantId = String;
pub type VClusterId = String;
pub type GroupId = u32; // dragonfly group index
pub type UserId = String; // OIDC subject
pub type LaunchId = Uuid;

pub type SessionId = Uuid;

/// An interactive session attached to a running allocation.
/// Tracked in quorum state for global session limit enforcement (F20).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub id: SessionId,
    pub allocation_id: AllocId,
    pub user: UserId,
    pub created_at: DateTime<Utc>,
}

// ─── Allocation ─────────────────────────────────────────────

/// The universal work unit. Replaces both Slurm jobs and K8s pods.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Allocation {
    pub id: AllocId,
    pub tenant: TenantId,
    pub project: String,
    pub vcluster: VClusterId,
    pub user: UserId,
    pub tags: HashMap<String, String>,

    // Type (single or task group)
    pub allocation_type: AllocationType,

    // What to run
    pub environment: Environment,
    pub entrypoint: String,

    // Resources
    pub resources: ResourceRequest,

    // Lifecycle
    pub lifecycle: Lifecycle,

    // Requeue
    pub requeue_policy: RequeuePolicy,
    pub max_requeue: u32,

    // Data
    pub data: DataRequirements,

    // Networking
    pub connectivity: Connectivity,

    // Dependencies
    pub depends_on: Vec<Dependency>,

    // Checkpointing
    pub checkpoint: CheckpointStrategy,

    // Telemetry
    pub telemetry_mode: TelemetryMode,

    // Liveness probe (for Unbounded/Reactive service allocations)
    pub liveness_probe: Option<LivenessProbe>,

    // State (managed by scheduler, not set by user)
    pub state: AllocationState,
    pub created_at: DateTime<Utc>,
    pub started_at: Option<DateTime<Utc>>,
    pub completed_at: Option<DateTime<Utc>>,
    pub assigned_nodes: Vec<NodeId>,

    /// DAG this allocation belongs to (set by scheduler on DAG submission)
    pub dag_id: Option<String>,
    /// Process exit code (set on completion/failure)
    pub exit_code: Option<i32>,
    /// Human-readable status message (failure reason, cancellation note, etc.)
    pub message: Option<String>,
    /// Number of times this allocation has been requeued
    pub requeue_count: u32,
    /// Number of times this allocation has been preempted
    pub preempted_count: u32,
    /// Whether to resume from checkpoint on next scheduling
    pub resume_from_checkpoint: bool,
    /// Whether this allocation requires sensitive workload isolation.
    /// Sensitive allocations get dedicated nodes, encrypted storage, audit logging,
    /// and single-session attach limits (INV-C2).
    #[serde(default)]
    pub sensitive: bool,

    // ─── Dispatch fields (INV-D6, INV-D8, INV-D11) ─────────────────
    /// Monotonic version counter for optimistic concurrency (INV-D6).
    /// Incremented on every state-changing Raft command for this allocation
    /// (SubmitAllocation, AssignNodes, UpdateAllocationState, ApplyCompletionReport,
    /// RollbackDispatch, RequeueAllocation). Separate from `requeue_count`.
    #[serde(default)]
    pub state_version: u64,
    /// Number of Dispatch Failures this allocation has experienced across
    /// potentially different nodes. Bounded by `max_dispatch_retries`; when
    /// reached, the allocation transitions to Failed rather than Pending
    /// on next RollbackDispatch (INV-D11).
    #[serde(default)]
    pub dispatch_retry_count: u32,
    /// Timestamp of the most recent ApplyCompletionReport that advanced this
    /// allocation's phase. Used by INV-D8 extended silent-sweep to detect
    /// ghosting agents (heartbeating but no progress).
    #[serde(default)]
    pub last_completion_report_at: Option<DateTime<Utc>>,
    /// Timestamp when the scheduler first assigned nodes to this allocation
    /// (i.e., when it moved from Pending to Running-with-nodes). Set by the
    /// `AssignNodes` apply-step. Used by INV-D8 silent-sweep to distinguish
    /// freshly-placed allocations from genuinely stuck ones
    /// (D-ADV-ARCH-07 fresh-allocation exemption).
    #[serde(default)]
    pub assigned_at: Option<DateTime<Utc>>,
    /// Per-node phase tracking for DEC-DISP-11 conservative multi-node
    /// aggregation. Written by the `ApplyCompletionReport` apply-step from
    /// each Completion Report's (node_id, phase). The global
    /// `AllocationState` is the conservative aggregate over these
    /// per-node phases.
    #[serde(default)]
    pub per_node_phase: HashMap<NodeId, CompletionPhase>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AllocationState {
    /// Submitted, waiting in queue
    Pending,
    /// Data being staged
    Staging,
    /// Running on assigned nodes
    Running,
    /// Checkpoint in progress (preemption pending)
    Checkpointing,
    /// Suspended (checkpointed, waiting for resources)
    Suspended,
    /// Completed successfully
    Completed,
    /// Failed
    Failed,
    /// Cancelled by user
    Cancelled,
}

// ─── Completion Reports (INV-D4, INV-D7, INV-D12, INV-D13) ──────────

/// Local allocation phase reported by the agent (mirrors proto
/// `CompletionPhase`). The agent's per-allocation state progresses
/// monotonically through these phases; INV-D7 rejects regressions at
/// the quorum. INV-D13 uses latest-wins semantics in the agent buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompletionPhase {
    /// Prologue in progress on this node; runtime prepared but no process yet.
    Staging,
    /// Workload Process spawned and running on this node.
    Running,
    /// Workload Process exited with exit_code 0 and epilogue complete.
    Completed,
    /// Workload Process exited non-zero, liveness probe failed, or runtime
    /// error. `reason` on the report carries the cause.
    Failed,
}

/// A per-allocation state-change record carried on a heartbeat (IP-03).
/// Delivered by the node agent to the quorum at `heartbeat_interval`
/// cadence, subject to INV-D4 idempotency, INV-D7 monotonicity, INV-D12
/// source-auth, and INV-D13 latest-wins buffering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionReport {
    pub allocation_id: AllocId,
    pub phase: CompletionPhase,
    /// Set on the first Running report for this allocation on this node.
    pub pid: Option<u32>,
    /// Set for Completed/Failed.
    pub exit_code: Option<i32>,
    /// Free-form for Failed; short annotation for others.
    pub reason: Option<String>,
}

impl CompletionReport {
    /// Map this agent-side phase to a global allocation state component.
    /// Multi-node aggregation per DEC-DISP-11 is applied at the quorum;
    /// this mapping gives the per-node contribution.
    pub fn to_per_node_allocation_state(&self) -> AllocationState {
        match self.phase {
            CompletionPhase::Staging => AllocationState::Staging,
            CompletionPhase::Running => AllocationState::Running,
            CompletionPhase::Completed => AllocationState::Completed,
            CompletionPhase::Failed => AllocationState::Failed,
        }
    }
}

/// Refusal reasons from agent to dispatcher (DEC-DISP-05). Mirrors proto
/// `RefusalReason`. Dispatcher must treat unknown variants as
/// `MalformedRequest` per D-ADV-ARCH-10 resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RefusalReason {
    /// Agent temporarily saturated; retry within attempt budget.
    Busy,
    /// Agent cannot fulfil this allocation shape; rollback to Pending so
    /// the scheduler re-places on a capable node.
    UnsupportedCapability,
    /// Allocation spec is bad; transition directly to Failed.
    MalformedRequest,
    /// Agent already has this allocation (INV-D3 short-circuit).
    AlreadyRunning,
}

/// Runtime variants for executing the allocation entrypoint.
/// One Runtime is selected per allocation; they are not composed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RuntimeVariant {
    /// Bare-Process Runtime: spawns entrypoint directly in a cgroup scope,
    /// no mount namespace. Used when neither `uenv` nor `image` is set.
    BareProcess,
    /// Uenv Runtime: mounts squashfs and spawns via nsenter.
    Uenv,
    /// Podman Runtime: pulls OCI image and spawns via podman run + nsenter.
    Podman,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AllocationType {
    /// Single allocation
    Single,
    /// Task group (job array equivalent)
    TaskGroup {
        range_start: u32,
        range_end: u32,
        step: u32,
        max_concurrent: u32,
    },
}

// ─── Requeue Policy ─────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RequeuePolicy {
    /// Allocation fails permanently on any node failure.
    Never,
    /// Requeue only on node-side failures (hardware, agent crash, partition).
    OnNodeFailure,
    /// Requeue on any failure including application crash.
    Always,
}

// ─── Software Delivery ──────────────────────────────────────

/// Image type: uenv (SquashFS) or OCI container.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ImageType {
    #[default]
    Uenv,
    Oci,
}

/// A content-addressed reference to an image (uenv or OCI).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ImageRef {
    /// Original user-provided spec (e.g., "prgenv-gnu/24.11:v1").
    pub spec: String,
    /// Whether this is a uenv or OCI image.
    pub image_type: ImageType,
    /// Registry URL (e.g., "jfrog.cscs.ch/uenv" or "nvcr.io").
    pub registry: String,
    /// Image name (e.g., "prgenv-gnu").
    pub name: String,
    /// Image version (e.g., "24.11").
    pub version: String,
    /// Original tag preserved for audit (INV-SD1).
    pub original_tag: String,
    /// Content hash (empty if deferred resolution).
    pub sha256: String,
    /// Image size in bytes.
    pub size_bytes: u64,
    /// Mount point (e.g., "/user-environment" for uenv, "/" for container).
    pub mount_point: String,
    /// If true, resolution is deferred to scheduler time (INV-SD4).
    pub resolve_on_schedule: bool,
}

/// Operation to apply to an environment variable.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum EnvOp {
    Prepend,
    Append,
    #[default]
    Set,
    Unset,
}

/// A single patch to an environment variable (view activation).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvPatch {
    /// Variable name (e.g., "PATH").
    pub variable: String,
    /// Operation to apply.
    pub op: EnvOp,
    /// Value to use (ignored for Unset).
    pub value: String,
    /// Separator for Prepend/Append (default ":").
    pub separator: String,
}

impl Default for EnvPatch {
    fn default() -> Self {
        Self {
            variable: String::new(),
            op: EnvOp::Set,
            value: String::new(),
            separator: ":".to_string(),
        }
    }
}

/// A named view definition from image metadata.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ViewDef {
    pub name: String,
    pub description: String,
    pub patches: Vec<EnvPatch>,
}

/// Metadata extracted from a resolved image.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ImageMetadata {
    pub name: String,
    pub description: String,
    pub mount_point: String,
    pub views: Vec<ViewDef>,
    pub default_view: Option<String>,
}

/// OCI container specification (when running via Sarus/Podman).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContainerSpec {
    /// EDF inheritance chain (base environment names).
    pub base_environments: Vec<String>,
    /// Additional bind mounts.
    pub mounts: Vec<MountSpec>,
    /// CDI device specs (e.g., "nvidia.com/gpu=all").
    pub devices: Vec<String>,
    /// Working directory inside the container.
    pub workdir: String,
    /// Whether the container rootfs is writable.
    pub writable: bool,
    /// Additional environment variables.
    pub env: Vec<(String, String)>,
    /// OCI annotations.
    pub annotations: Vec<(String, String)>,
}

/// A bind mount specification.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MountSpec {
    pub source: String,
    pub target: String,
    /// Mount options (e.g., "ro", "rw").
    pub options: String,
}

// ─── Environment ────────────────────────────────────────────

/// Software environment for an allocation. Replaces the legacy flat struct
/// with content-addressed images, env patches from views, and optional
/// OCI container spec.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Environment {
    /// Resolved image references (uenv and/or OCI).
    pub images: Vec<ImageRef>,
    /// Environment variable patches (from view activation / EDF).
    pub env_patches: Vec<EnvPatch>,
    /// CDI device specs (e.g., "nvidia.com/gpu=all").
    pub devices: Vec<String>,
    /// Additional bind mounts.
    pub mounts: Vec<MountSpec>,
    /// Full container spec (set when running OCI images).
    pub container: Option<ContainerSpec>,
    /// Whether the container rootfs is writable.
    pub writable: bool,
    /// For sensitive: require signed images.
    pub sign_required: bool,
    /// Require vulnerability scan before scheduling (sensitive-workloads).
    pub scan_required: bool,
    /// Restrict to approved base images only (sensitive-workloads).
    pub approved_bases_only: bool,
}

// ─── Resources ──────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceRequest {
    /// Number of nodes (exact or range)
    pub nodes: NodeCount,
    /// Hardware constraints
    pub constraints: ResourceConstraints,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NodeCount {
    Exact(u32),
    /// Elastic node range. Invariants: min >= 1, max >= min.
    Range {
        min: u32,
        max: u32,
    },
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceConstraints {
    pub gpu_type: Option<String>,
    pub features: Vec<String>,
    /// Topology hint: "tight" (fewest groups), "spread" (max bandwidth), "any"
    pub topology: Option<TopologyHint>,
    /// Feature count requirements (e.g., at least 4 nodes with "nvme_scratch")
    pub feature_counts: HashMap<String, u32>,
    /// Require nodes with unified memory (GH200/MI300A)
    #[serde(default)]
    pub require_unified_memory: bool,
    /// Prefer allocating CPUs+GPUs within the same NUMA domain
    #[serde(default)]
    pub prefer_same_numa: bool,
    /// Allow CXL-attached memory to satisfy capacity requirements
    #[serde(default)]
    pub allow_cxl_memory: bool,
    /// Memory binding policy (numactl)
    pub memory_policy: Option<MemoryPolicy>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TopologyHint {
    Tight,  // pack into fewest dragonfly groups
    Spread, // spread across groups for max bisection bandwidth
    Any,    // no preference
}

// ─── Lifecycle ──────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lifecycle {
    pub lifecycle_type: LifecycleType,
    pub preemption_class: u8, // 0 = lowest priority, higher = harder to preempt
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LifecycleType {
    /// Traditional batch job with walltime
    Bounded { walltime: chrono::Duration },
    /// Long-running service (inference endpoint, monitoring)
    Unbounded,
    /// Autoscaling based on metrics
    Reactive {
        min_nodes: u32,
        max_nodes: u32,
        metric: String,
        target: String,
    },
}

// ─── Data ───────────────────────────────────────────────────

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DataRequirements {
    pub mounts: Vec<DataMount>,
    /// Use sane defaults (home dir, scratch, output dir)
    pub use_defaults: bool,
    /// Per-node scratch size hint
    pub scratch_per_node: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataMount {
    pub source: String, // s3://... or nfs://...
    pub target: String, // mount point inside allocation
    pub access: DataAccess,
    pub tier_hint: Option<StorageTier>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DataAccess {
    ReadOnly,
    ReadWrite,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StorageTier {
    Hot,
    Warm,
    Cold,
}

// ─── Connectivity ───────────────────────────────────────────

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Connectivity {
    /// Shared network domain name — allocations in the same domain can communicate
    pub network_domain: Option<String>,
    /// Exposed endpoints (for services)
    pub expose: Vec<ServiceEndpoint>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceEndpoint {
    pub name: String,
    pub port: u16,
    pub protocol: Option<String>,
}

/// A registered service endpoint — published into the service registry
/// when an allocation with `expose` endpoints transitions to Running.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegisteredEndpoint {
    pub allocation_id: AllocId,
    pub tenant: TenantId,
    pub nodes: Vec<NodeId>,
    pub port: u16,
    pub protocol: Option<String>,
}

/// A service registry entry — groups all endpoints for a named service.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ServiceRegistryEntry {
    pub endpoints: Vec<RegisteredEndpoint>,
}

// ─── Liveness Probe ─────────────────────────────────────────

/// A liveness probe checks whether a service allocation's workload is healthy.
/// If the probe fails `failure_threshold` consecutive times, the allocation is
/// marked Failed (and the reconciler can requeue it).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LivenessProbe {
    /// The type of probe to run.
    pub probe_type: ProbeType,
    /// How often (in seconds) to run the probe.
    pub period_secs: u32,
    /// Seconds to wait before the first probe (gives the workload time to start).
    pub initial_delay_secs: u32,
    /// Number of consecutive failures before marking the allocation as Failed.
    pub failure_threshold: u32,
    /// Seconds to wait for the probe to succeed before counting as a failure.
    pub timeout_secs: u32,
}

impl Default for LivenessProbe {
    fn default() -> Self {
        Self {
            probe_type: ProbeType::Tcp { port: 8080 },
            period_secs: 30,
            initial_delay_secs: 10,
            failure_threshold: 3,
            timeout_secs: 5,
        }
    }
}

/// The mechanism used to check workload liveness.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProbeType {
    /// Open a TCP connection to the given port on the allocation's node.
    Tcp { port: u16 },
    /// Send an HTTP GET request and expect a 2xx response.
    Http { port: u16, path: String },
}

// ─── Network Domain ────────────────────────────────────────

/// A network domain groups allocations that need L3 reachability via Slingshot VNI.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkDomain {
    pub name: String,
    pub tenant: TenantId,
    /// Assigned Slingshot VNI
    pub vni: u32,
    pub state: NetworkDomainState,
    /// Allocation IDs currently in this domain
    pub member_allocations: Vec<AllocId>,
    pub created_at: DateTime<Utc>,
    /// Grace deadline after last member completes (for DAG domain persistence)
    pub grace_deadline: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NetworkDomainState {
    /// Active, accepting new members
    Active,
    /// Last member completed, grace timer running
    Draining,
    /// VNI released back to pool
    Released,
}

// ─── Dependencies ───────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Dependency {
    /// Reference to another allocation (by ID or name)
    pub ref_id: String,
    /// Condition for dependency satisfaction
    pub condition: DependencyCondition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DependencyCondition {
    /// afterok — only if dependency succeeded
    Success,
    /// afternotok — only if dependency failed
    Failure,
    /// afterany — regardless of outcome
    Any,
    /// aftercorr — corresponding task in task group
    Corresponding,
    /// singleton — only one allocation with this name runs at a time
    Mutex,
}

// ─── Checkpointing ─────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CheckpointStrategy {
    /// Scheduler decides based on cost function
    Auto,
    /// Application manages its own checkpointing
    Manual,
    /// Non-checkpointable (treated as non-preemptible)
    None,
}

// ─── Telemetry ──────────────────────────────────────────────

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub enum TelemetryMode {
    /// 30s aggregation, bicubic smoothing, low overhead
    #[default]
    Prod,
    /// 1s raw streams, full profiling, higher overhead
    Debug { duration_seconds: u64 },
    /// Access logging for compliance, moderate overhead
    Audit,
}

// ─── Observability: Attach ─────────────────────────────────

/// An active interactive terminal session attached to a running allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachSession {
    pub session_id: Uuid,
    pub allocation_id: AllocId,
    pub node_id: NodeId,
    pub user: UserId,
    pub command: String,
    pub started_at: DateTime<Utc>,
    pub ended_at: Option<DateTime<Utc>>,
}

// ─── Observability: Logs ───────────────────────────────────

/// A single log entry from an allocation's stdout/stderr.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    pub node_id: NodeId,
    pub stream: LogStream,
    pub data: Vec<u8>,
    pub timestamp: DateTime<Utc>,
}

/// Which output stream a log entry came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogStream {
    Stdout,
    Stderr,
}

/// Configuration for log capture on a running allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogConfig {
    /// Size of the per-allocation ring buffer on each node (bytes).
    pub ring_buffer_size: u64,
    /// Whether to persist logs to S3.
    pub s3_persistence: bool,
    /// Retention duration for persisted logs.
    pub retention: Option<chrono::Duration>,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            ring_buffer_size: 64 * 1024 * 1024, // 64 MB
            s3_persistence: true,
            retention: None, // use system default
        }
    }
}

// ─── Observability: Metrics ────────────────────────────────

/// Per-node metrics snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeMetricsSnapshot {
    pub node_id: NodeId,
    pub timestamp: DateTime<Utc>,
    pub cpu_utilization: f64,
    pub memory_used_bytes: u64,
    pub memory_total_bytes: u64,
    pub network_tx_bytes_per_sec: f64,
    pub network_rx_bytes_per_sec: f64,
    pub io_read_bytes_per_sec: f64,
    pub io_write_bytes_per_sec: f64,
    pub io_latency_p99_us: f64,
    pub gpus: Vec<GpuMetricsSnapshot>,
}

/// Per-GPU metrics snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuMetricsSnapshot {
    pub index: u32,
    pub utilization: f64,
    pub memory_used_bytes: u64,
    pub memory_total_bytes: u64,
    pub power_draw_watts: f64,
    pub temperature_celsius: f64,
    pub ecc_errors: u64,
}

/// Aggregated metrics across all nodes in an allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllocationMetricsSummary {
    pub allocation_id: AllocId,
    pub timestamp: DateTime<Utc>,
    pub gpu_utilization_mean: f64,
    pub cpu_utilization_mean: f64,
    pub memory_used_bytes: u64,
    pub memory_total_bytes: u64,
    pub gpu_memory_used_bytes: u64,
    pub gpu_memory_total_bytes: u64,
    pub network_tx_bytes_per_sec: f64,
    pub network_rx_bytes_per_sec: f64,
    pub io_read_bytes_per_sec: f64,
    pub io_write_bytes_per_sec: f64,
    pub io_latency_p99_us: f64,
}

// ─── Observability: Diagnostics ────────────────────────────

/// Network diagnostics for a running allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkDiagnostics {
    pub group_span: u32,
    pub groups: Vec<GroupId>,
    pub csig_congestion_avg: f64,
    pub inter_node_bandwidth_gbps: f64,
    pub target_bandwidth_gbps: f64,
    pub node_pairs: Vec<NodePairBandwidth>,
    pub nvlink_throughput_gbps: f64,
    pub network_errors: u64,
}

/// Measured bandwidth between a pair of nodes.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodePairBandwidth {
    pub source_node: NodeId,
    pub target_node: NodeId,
    pub bandwidth_gbps: f64,
    pub latency_us: f64,
}

/// Storage diagnostics for a running allocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageDiagnostics {
    pub mounts: Vec<MountDiagnostics>,
}

/// Per-mount storage diagnostics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MountDiagnostics {
    pub mount_path: String,
    pub mount_type: String,
    pub read_throughput_gbps: f64,
    pub write_throughput_gbps: f64,
    pub qos_floor_gbps: f64,
    pub latency_p50_us: f64,
    pub latency_p95_us: f64,
    pub latency_p99_us: f64,
    pub iops_read: f64,
    pub iops_write: f64,
    pub health: String,
}

// ─── Observability: Alerts ─────────────────────────────────

/// A threshold alert generated by a node agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricAlert {
    pub node_id: NodeId,
    pub metric_name: String,
    pub current_value: f64,
    pub threshold: f64,
    pub severity: AlertSeverity,
    pub message: String,
    pub timestamp: DateTime<Utc>,
}

/// Severity level for metric alerts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
}

// ─── Node ───────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Node {
    pub id: NodeId,
    pub group: GroupId,
    pub capabilities: NodeCapabilities,
    pub state: NodeState,
    pub owner: Option<NodeOwnership>,
    /// Conformance fingerprint (hash of OS image, kernel, driver versions)
    pub conformance_fingerprint: Option<String>,
    /// Last heartbeat received from this node's agent
    pub last_heartbeat: Option<DateTime<Utc>>,
    /// Monotonic version counter for ownership changes.
    /// Incremented on every ClaimNode/ReleaseNode. Heartbeats must carry
    /// the current owner_version to be accepted (ADV-06).
    #[serde(default)]
    pub owner_version: u64,

    // ─── Dispatch fields (INV-D1, INV-D11, INV-D14, INV-D5) ─────────
    /// Reachable gRPC endpoint for this node's agent (host:port).
    /// Required for the node to participate in scheduling (INV-D1).
    /// Must match a SAN in the agent's workload certificate (INV-D14).
    /// Empty string means "unregistered" — node is scheduler-invisible (INV-D2).
    #[serde(default)]
    pub agent_address: String,
    /// Consecutive dispatch failures attributed to this node. Incremented
    /// atomically with RollbackDispatch (INV-D11). Reset to 0 on any
    /// successful dispatch on this node. Threshold `max_node_dispatch_failures`
    /// transitions the node to Degraded (subject to cluster-wide ratio guard).
    #[serde(default)]
    pub consecutive_dispatch_failures: u32,
    /// Timestamp when the node entered Degraded via INV-D11. Used by
    /// DEC-DISP-01 auto-probe-recovery — after `degraded_probe_interval`
    /// since this timestamp, the node is probed back to Ready with halved counter.
    #[serde(default)]
    pub degraded_at: Option<DateTime<Utc>>,
    /// Agent has completed boot but is still reattaching to surviving
    /// Workload Processes (DEC-DISP-10). Raft-committed via extended
    /// Command::RecordHeartbeat. Silent-sweep (INV-D8) honours this flag
    /// subject to the lifecycle rules in INV-D5.
    #[serde(default)]
    pub reattach_in_progress: bool,
    /// Timestamp of the first heartbeat that set `reattach_in_progress = true`
    /// after the most recent re-registration. Used by INV-D5 lifecycle:
    /// the grace timer is absolute from this timestamp; it does not reset
    /// on subsequent true-valued heartbeats.
    #[serde(default)]
    pub reattach_first_set_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeCapabilities {
    pub gpu_type: Option<String>,
    pub gpu_count: u32,
    pub cpu_cores: u32,
    pub memory_gb: u64,
    pub features: Vec<String>,
    /// Detailed GPU topology (interconnect, NIC affinity)
    pub gpu_topology: Option<GpuTopology>,
    /// Memory topology (NUMA, CXL, unified memory)
    pub memory_topology: Option<MemoryTopology>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NodeState {
    /// Node exists in inventory but has never reported
    Unknown,
    /// OpenCHAMI booting/reimaging the node
    Booting,
    /// Healthy, agent reporting, available for scheduling
    Ready,
    /// Heartbeat missed or minor issue detected
    Degraded { reason: String },
    /// Confirmed failure, grace period expired
    Down { reason: String },
    /// Operator or scheduler requested drain, waiting for allocations to finish
    Draining,
    /// All allocations completed after drain
    Drained,
    /// Boot failure or unrecoverable hardware error
    Failed { reason: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeOwnership {
    pub tenant: TenantId,
    pub vcluster: VClusterId,
    pub allocation: AllocId,
    /// For sensitive: the specific user who claimed this node
    pub claimed_by: Option<UserId>,
    /// Home vCluster (permanent assignment) vs current (may be borrowed)
    pub is_borrowed: bool,
}

// ─── Tenant ─────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tenant {
    pub id: TenantId,
    pub name: String,
    pub quota: TenantQuota,
    pub isolation_level: IsolationLevel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TenantQuota {
    /// Maximum nodes this tenant can use simultaneously
    pub max_nodes: u32,
    /// Target fair-share fraction (0.0 - 1.0)
    pub fair_share_target: f64,
    /// GPU-hours budget (None = unlimited)
    pub gpu_hours_budget: Option<f64>,
    /// Node-hours budget (None = unlimited). Universal metric that works
    /// for both GPU and CPU-only nodes. When both gpu_hours_budget and
    /// node_hours_budget are set, the worse utilization drives the penalty.
    #[serde(default)]
    pub node_hours_budget: Option<f64>,
    /// Maximum concurrent allocations (hard limit per quota-enforcement)
    pub max_concurrent_allocations: Option<u32>,
    /// Burst allowance multiplier (e.g. 1.5 = allow up to 150% of fair share
    /// when resources are idle). Burst allocations are first to be preempted.
    /// None means no bursting beyond fair_share_target.
    #[serde(default)]
    pub burst_allowance: Option<f64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IsolationLevel {
    /// Standard HPC: shared nodes possible, elastic borrowing
    Standard,
    /// Strict: dedicated nodes, no sharing, audit logging
    Strict,
}

// ─── VCluster ───────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VCluster {
    pub id: VClusterId,
    pub name: String,
    pub tenant: TenantId,
    pub scheduler_type: SchedulerType,
    pub cost_weights: CostWeights,
    /// Nodes permanently assigned to this vCluster
    pub dedicated_nodes: Vec<NodeId>,
    /// Can borrow from other vClusters' idle nodes?
    pub allow_borrowing: bool,
    /// Can lend idle nodes to other vClusters?
    pub allow_lending: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SchedulerType {
    /// Priority + backfill + topology-aware (traditional HPC)
    HpcBackfill,
    /// Bin-packing + autoscale (services)
    ServiceBinPack,
    /// User-claim reservation (sensitive)
    SensitiveReservation,
    /// FIFO, short-lived (interactive sessions)
    InteractiveFifo,
}

/// Weights for the composite cost function. Weights are relative;
/// normalization (sum to 1.0) is optional — the solver normalizes internally.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostWeights {
    pub priority: f64,
    pub wait_time: f64,
    pub fair_share: f64,
    pub topology: f64,
    pub data_readiness: f64,
    pub backlog: f64,
    pub energy: f64,
    pub checkpoint_efficiency: f64,
    pub conformance: f64,
}

impl Default for CostWeights {
    fn default() -> Self {
        // Default: balanced HPC profile
        Self {
            priority: 0.20,
            wait_time: 0.20,
            fair_share: 0.20,
            topology: 0.15,
            data_readiness: 0.10,
            backlog: 0.05,
            energy: 0.00,
            checkpoint_efficiency: 0.00,
            conformance: 0.10,
        }
    }
}

// ─── GPU Topology ───────────────────────────────────────────

/// Intra-node GPU topology: devices, interconnect links, and NIC affinity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuTopology {
    pub devices: Vec<GpuDevice>,
    /// Mapping of GPU index → closest NIC index (for Slingshot/UE affinity)
    pub nic_affinity: HashMap<u32, u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuDevice {
    pub index: u32,
    pub vendor: GpuVendor,
    pub model: String,
    pub memory_bytes: u64,
    pub links: Vec<GpuLink>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuLink {
    pub peer_index: u32,
    pub link_type: GpuLinkType,
    pub bandwidth_gbps: f64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GpuLinkType {
    NVLink,
    NVSwitch,
    InfinityFabric,
    PCIe,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GpuVendor {
    Nvidia,
    Amd,
}

// ─── Memory Topology ────────────────────────────────────────

/// Intra-node memory topology: NUMA domains, CXL tiers, and unified memory.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryTopology {
    pub domains: Vec<MemoryDomain>,
    pub interconnects: Vec<MemoryInterconnect>,
    pub total_capacity_bytes: u64,
}

/// A memory domain (NUMA node, HBM bank, CXL-attached pool, or unified).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryDomain {
    pub id: u32,
    pub domain_type: MemoryDomainType,
    pub capacity_bytes: u64,
    pub numa_node: Option<u32>,
    pub attached_cpus: Vec<u32>,
    pub attached_gpus: Vec<u32>,
}

/// Interconnect link between two memory domains.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryInterconnect {
    pub domain_a: u32,
    pub domain_b: u32,
    pub link_type: MemoryLinkType,
    pub bandwidth_gbps: f64,
    pub latency_ns: u64,
}

/// Type of memory in a domain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MemoryDomainType {
    Dram,
    Hbm,
    CxlAttached,
    Unified,
}

/// Type of link between memory domains.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MemoryLinkType {
    NumaLink,
    CxlSwitch,
    CoherentFabric,
}

/// Memory binding policy for numactl.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MemoryPolicy {
    Local,
    Interleave,
    Preferred,
    Bind,
}

// ─── Topology ───────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopologyModel {
    pub groups: Vec<TopologyGroup>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopologyGroup {
    pub id: GroupId,
    pub nodes: Vec<NodeId>,
    /// Adjacent groups (lower inter-group hop cost)
    pub adjacent_groups: Vec<GroupId>,
}

// ─── State Machine Validation ──────────────────────────────

impl AllocationState {
    /// Returns true if transitioning from `self` to `target` is a valid state change.
    ///
    /// Valid transitions:
    /// - Pending → Staging, Running, Cancelled, Failed
    /// - Staging → Running, Failed, Cancelled
    /// - Running → Checkpointing, Completed, Failed, Cancelled
    /// - Checkpointing → Suspended, Failed, Cancelled
    /// - Suspended → Pending (requeue), Cancelled, Failed
    /// - Terminal states (Completed, Failed, Cancelled) → nothing
    pub fn can_transition_to(&self, target: &AllocationState) -> bool {
        matches!(
            (self, target),
            (AllocationState::Pending, AllocationState::Staging)
                | (AllocationState::Pending, AllocationState::Running)
                | (AllocationState::Pending, AllocationState::Cancelled)
                | (AllocationState::Pending, AllocationState::Failed)
                | (AllocationState::Staging, AllocationState::Running)
                | (AllocationState::Staging, AllocationState::Failed)
                | (AllocationState::Staging, AllocationState::Cancelled)
                | (AllocationState::Running, AllocationState::Checkpointing)
                | (AllocationState::Running, AllocationState::Completed)
                | (AllocationState::Running, AllocationState::Failed)
                | (AllocationState::Running, AllocationState::Cancelled)
                | (AllocationState::Checkpointing, AllocationState::Suspended)
                | (AllocationState::Checkpointing, AllocationState::Failed)
                | (AllocationState::Checkpointing, AllocationState::Cancelled)
                | (AllocationState::Suspended, AllocationState::Pending)
                | (AllocationState::Suspended, AllocationState::Cancelled)
                | (AllocationState::Suspended, AllocationState::Failed)
                // Service reconciliation: failed services can be requeued
                | (AllocationState::Failed, AllocationState::Pending)
        )
    }

    /// Returns true if this is a terminal state (no further transitions possible).
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            AllocationState::Completed | AllocationState::Failed | AllocationState::Cancelled
        )
    }
}

impl NodeState {
    /// Returns true if transitioning from `self` to `target` is a valid state change.
    ///
    /// Valid transitions:
    /// - Unknown → Booting, Failed
    /// - Booting → Ready, Failed
    /// - Ready → Degraded, Draining, Down
    /// - Degraded → Ready, Down, Draining
    /// - Down → Booting (reimaging), Failed
    /// - Draining → Drained
    /// - Drained → Ready (undrain), Booting (reimage)
    /// - Failed → Booting (reimage)
    pub fn can_transition_to(&self, target: &NodeState) -> bool {
        matches!(
            (self, target),
            (NodeState::Unknown, NodeState::Booting)
                | (NodeState::Unknown, NodeState::Failed { .. })
                | (NodeState::Booting, NodeState::Ready)
                | (NodeState::Booting, NodeState::Failed { .. })
                | (NodeState::Ready, NodeState::Degraded { .. })
                | (NodeState::Ready, NodeState::Draining)
                | (NodeState::Ready, NodeState::Down { .. })
                | (NodeState::Degraded { .. }, NodeState::Ready)
                | (NodeState::Degraded { .. }, NodeState::Down { .. })
                | (NodeState::Degraded { .. }, NodeState::Draining)
                | (NodeState::Down { .. }, NodeState::Ready)
                | (NodeState::Down { .. }, NodeState::Booting)
                | (NodeState::Down { .. }, NodeState::Failed { .. })
                | (NodeState::Draining, NodeState::Drained)
                | (NodeState::Drained, NodeState::Ready)
                | (NodeState::Drained, NodeState::Booting)
                | (NodeState::Failed { .. }, NodeState::Booting)
        )
    }

    /// Returns true if the node can accept workloads.
    pub fn is_operational(&self) -> bool {
        matches!(self, NodeState::Ready | NodeState::Degraded { .. })
    }
}

impl NetworkDomainState {
    /// Returns true if transitioning from `self` to `target` is a valid state change.
    ///
    /// Valid transitions:
    /// - Active → Draining
    /// - Draining → Active (new member joins), Released (grace expired)
    /// - Released → nothing (terminal)
    pub fn can_transition_to(&self, target: &NetworkDomainState) -> bool {
        matches!(
            (self, target),
            (NetworkDomainState::Active, NetworkDomainState::Draining)
                | (NetworkDomainState::Draining, NetworkDomainState::Active)
                | (NetworkDomainState::Draining, NetworkDomainState::Released)
        )
    }
}

// ─── MPI Process Management ─────────────────────────────────

/// PMI mode for MPI process launch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum PmiMode {
    /// Native PMI-2 wire protocol (default, no external dependencies).
    #[default]
    Pmi2,
    /// OpenPMIx sidecar (requires `pmix` feature on node agent).
    Pmix,
}

/// CXI (Cassini eXtended Interface) credentials for Slingshot fabric access.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CxiCredentials {
    pub vni: u32,
    pub auth_key: Vec<u8>,
    pub svc_id: u32,
}

/// Peer node agent info for cross-node MPI coordination.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerInfo {
    pub node_id: NodeId,
    pub grpc_address: String,
    pub first_rank: u32,
    pub num_ranks: u32,
}

/// Assignment of ranks to a single node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeRankAssignment {
    pub node_id: NodeId,
    pub first_rank: u32,
    pub num_ranks: u32,
}

/// Layout of MPI ranks across nodes.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RankLayout {
    pub total_ranks: u32,
    pub tasks_per_node: u32,
    pub node_assignments: Vec<NodeRankAssignment>,
}

impl RankLayout {
    /// Compute a rank layout from a node list and tasks-per-node.
    pub fn compute(nodes: &[NodeId], tasks_per_node: u32) -> Self {
        let mut assignments = Vec::with_capacity(nodes.len());
        let mut rank = 0u32;
        for node_id in nodes {
            assignments.push(NodeRankAssignment {
                node_id: node_id.clone(),
                first_rank: rank,
                num_ranks: tasks_per_node,
            });
            rank += tasks_per_node;
        }
        RankLayout {
            total_ranks: rank,
            tasks_per_node,
            node_assignments: assignments,
        }
    }
}

/// Exit status of a single MPI rank.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RankExitStatus {
    pub rank: u32,
    pub exit_code: Option<i32>,
    pub signal: Option<i32>,
}

/// Full specification for an MPI launch, computed by the API server before fan-out.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LaunchSpec {
    pub launch_id: LaunchId,
    pub allocation_id: AllocId,
    pub entrypoint: String,
    pub args: Vec<String>,
    pub env: HashMap<String, String>,
    pub rank_layout: RankLayout,
    pub pmi_mode: PmiMode,
    pub cxi_credentials: Option<CxiCredentials>,
}

// ─── Software Delivery Utilities ────────────────────────────

/// Apply a list of environment patches to an env var map, in declaration order (INV-SD7).
///
/// - Prepend: `var = value + separator + existing` (or just `value` if unset)
/// - Append: `var = existing + separator + value` (or just `value` if unset)
/// - Set: `var = value`
/// - Unset: remove `var`
pub fn apply_env_patches(patches: &[EnvPatch], env: &mut HashMap<String, String>) {
    for patch in patches {
        match patch.op {
            EnvOp::Set => {
                env.insert(patch.variable.clone(), patch.value.clone());
            }
            EnvOp::Unset => {
                env.remove(&patch.variable);
            }
            EnvOp::Prepend => {
                let existing = env.get(&patch.variable).cloned().unwrap_or_default();
                let new_val = if existing.is_empty() {
                    patch.value.clone()
                } else {
                    format!("{}{}{}", patch.value, patch.separator, existing)
                };
                env.insert(patch.variable.clone(), new_val);
            }
            EnvOp::Append => {
                let existing = env.get(&patch.variable).cloned().unwrap_or_default();
                let new_val = if existing.is_empty() {
                    patch.value.clone()
                } else {
                    format!("{}{}{}", existing, patch.separator, patch.value)
                };
                env.insert(patch.variable.clone(), new_val);
            }
        }
    }
}

/// Check that no mount targets overlap (prefix-shadow each other, INV-SD3).
///
/// Two targets overlap when one is a prefix of the other followed by `/` or an
/// exact match. For example, `/opt` overlaps `/opt/env` (prefix shadowing).
pub fn check_mount_overlap(targets: &[&str]) -> Result<(), String> {
    let mut sorted: Vec<&str> = targets.to_vec();
    sorted.sort_by_key(|t| t.len());

    for i in 0..sorted.len() {
        for j in (i + 1)..sorted.len() {
            let short = sorted[i];
            let long = sorted[j];
            if short == long {
                return Err(format!("duplicate mount target: {short}"));
            }
            // Check prefix: /opt shadows /opt/env
            if let Some(rest) = long.strip_prefix(short) {
                if rest.starts_with('/') || short.ends_with('/') {
                    return Err(format!("mount target overlap: {short} shadows {long}"));
                }
            }
        }
    }
    Ok(())
}

// ─── Tests ─────────────────────────────────────────────────

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

    // ── Software Delivery type tests ──

    #[test]
    fn environment_default_has_empty_vecs_and_false_bools() {
        let env = Environment::default();
        assert!(env.images.is_empty());
        assert!(env.env_patches.is_empty());
        assert!(env.devices.is_empty());
        assert!(env.mounts.is_empty());
        assert!(env.container.is_none());
        assert!(!env.writable);
        assert!(!env.sign_required);
        assert!(!env.scan_required);
        assert!(!env.approved_bases_only);
    }

    #[test]
    fn image_ref_default() {
        let img = ImageRef::default();
        assert!(img.spec.is_empty());
        assert_eq!(img.image_type, ImageType::Uenv);
        assert!(img.sha256.is_empty());
        assert_eq!(img.size_bytes, 0);
        assert!(!img.resolve_on_schedule);
    }

    #[test]
    fn image_ref_deferred_has_empty_sha256() {
        let img = ImageRef {
            spec: "prgenv-gnu/24.11:v1".into(),
            resolve_on_schedule: true,
            ..ImageRef::default()
        };
        assert!(img.sha256.is_empty());
        assert!(img.resolve_on_schedule);
    }

    #[test]
    fn env_patch_default_separator_is_colon() {
        let patch = EnvPatch::default();
        assert_eq!(patch.separator, ":");
        assert_eq!(patch.op, EnvOp::Set);
    }

    #[test]
    fn container_spec_default() {
        let cs = ContainerSpec::default();
        assert!(cs.base_environments.is_empty());
        assert!(cs.mounts.is_empty());
        assert!(cs.devices.is_empty());
        assert!(cs.workdir.is_empty());
        assert!(!cs.writable);
    }

    #[test]
    fn mount_spec_default() {
        let ms = MountSpec::default();
        assert!(ms.source.is_empty());
        assert!(ms.target.is_empty());
        assert!(ms.options.is_empty());
    }

    #[test]
    fn view_def_default() {
        let vd = ViewDef::default();
        assert!(vd.name.is_empty());
        assert!(vd.patches.is_empty());
    }

    #[test]
    fn image_metadata_default() {
        let im = ImageMetadata::default();
        assert!(im.views.is_empty());
        assert!(im.default_view.is_none());
    }

    #[test]
    fn apply_env_patches_set() {
        let patches = vec![EnvPatch {
            variable: "FOO".into(),
            op: EnvOp::Set,
            value: "bar".into(),
            separator: ":".into(),
        }];
        let mut env = HashMap::new();
        apply_env_patches(&patches, &mut env);
        assert_eq!(env.get("FOO").unwrap(), "bar");
    }

    #[test]
    fn apply_env_patches_unset() {
        let patches = vec![EnvPatch {
            variable: "FOO".into(),
            op: EnvOp::Unset,
            value: String::new(),
            separator: ":".into(),
        }];
        let mut env = HashMap::new();
        env.insert("FOO".into(), "bar".into());
        apply_env_patches(&patches, &mut env);
        assert!(!env.contains_key("FOO"));
    }

    #[test]
    fn apply_env_patches_prepend() {
        let patches = vec![EnvPatch {
            variable: "PATH".into(),
            op: EnvOp::Prepend,
            value: "/new/bin".into(),
            separator: ":".into(),
        }];
        let mut env = HashMap::new();
        env.insert("PATH".into(), "/usr/bin".into());
        apply_env_patches(&patches, &mut env);
        assert_eq!(env.get("PATH").unwrap(), "/new/bin:/usr/bin");
    }

    #[test]
    fn apply_env_patches_append() {
        let patches = vec![EnvPatch {
            variable: "PATH".into(),
            op: EnvOp::Append,
            value: "/extra/bin".into(),
            separator: ":".into(),
        }];
        let mut env = HashMap::new();
        env.insert("PATH".into(), "/usr/bin".into());
        apply_env_patches(&patches, &mut env);
        assert_eq!(env.get("PATH").unwrap(), "/usr/bin:/extra/bin");
    }

    #[test]
    fn apply_env_patches_prepend_to_empty() {
        let patches = vec![EnvPatch {
            variable: "NEW_VAR".into(),
            op: EnvOp::Prepend,
            value: "/opt/lib".into(),
            separator: ":".into(),
        }];
        let mut env = HashMap::new();
        apply_env_patches(&patches, &mut env);
        assert_eq!(env.get("NEW_VAR").unwrap(), "/opt/lib");
    }

    #[test]
    fn apply_env_patches_declaration_order() {
        let patches = vec![
            EnvPatch {
                variable: "X".into(),
                op: EnvOp::Set,
                value: "first".into(),
                separator: ":".into(),
            },
            EnvPatch {
                variable: "X".into(),
                op: EnvOp::Set,
                value: "second".into(),
                separator: ":".into(),
            },
        ];
        let mut env = HashMap::new();
        apply_env_patches(&patches, &mut env);
        assert_eq!(env.get("X").unwrap(), "second");
    }

    #[test]
    fn check_mount_overlap_no_overlap() {
        assert!(check_mount_overlap(&["/opt", "/usr", "/mnt/data"]).is_ok());
    }

    #[test]
    fn check_mount_overlap_prefix_shadow() {
        let result = check_mount_overlap(&["/opt", "/opt/env"]);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("shadows"));
    }

    #[test]
    fn check_mount_overlap_duplicate() {
        let result = check_mount_overlap(&["/opt", "/opt"]);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("duplicate"));
    }

    #[test]
    fn check_mount_overlap_no_false_positive_on_shared_prefix() {
        // /opt and /optional should NOT overlap (no / separator)
        assert!(check_mount_overlap(&["/opt", "/optional"]).is_ok());
    }

    #[test]
    fn check_mount_overlap_empty_list() {
        assert!(check_mount_overlap(&[]).is_ok());
    }

    // ── AllocationState transition tests ──

    #[test]
    fn pending_can_transition_to_staging() {
        assert!(AllocationState::Pending.can_transition_to(&AllocationState::Staging));
    }

    #[test]
    fn pending_can_transition_to_running() {
        assert!(AllocationState::Pending.can_transition_to(&AllocationState::Running));
    }

    #[test]
    fn pending_can_transition_to_cancelled() {
        assert!(AllocationState::Pending.can_transition_to(&AllocationState::Cancelled));
    }

    #[test]
    fn pending_can_transition_to_failed() {
        assert!(AllocationState::Pending.can_transition_to(&AllocationState::Failed));
    }

    #[test]
    fn pending_cannot_transition_to_completed() {
        assert!(!AllocationState::Pending.can_transition_to(&AllocationState::Completed));
    }

    #[test]
    fn pending_cannot_transition_to_checkpointing() {
        assert!(!AllocationState::Pending.can_transition_to(&AllocationState::Checkpointing));
    }

    #[test]
    fn staging_can_transition_to_running() {
        assert!(AllocationState::Staging.can_transition_to(&AllocationState::Running));
    }

    #[test]
    fn staging_can_transition_to_failed() {
        assert!(AllocationState::Staging.can_transition_to(&AllocationState::Failed));
    }

    #[test]
    fn staging_can_transition_to_cancelled() {
        assert!(AllocationState::Staging.can_transition_to(&AllocationState::Cancelled));
    }

    #[test]
    fn running_can_transition_to_checkpointing() {
        assert!(AllocationState::Running.can_transition_to(&AllocationState::Checkpointing));
    }

    #[test]
    fn running_can_transition_to_completed() {
        assert!(AllocationState::Running.can_transition_to(&AllocationState::Completed));
    }

    #[test]
    fn running_can_transition_to_failed() {
        assert!(AllocationState::Running.can_transition_to(&AllocationState::Failed));
    }

    #[test]
    fn running_can_transition_to_cancelled() {
        assert!(AllocationState::Running.can_transition_to(&AllocationState::Cancelled));
    }

    #[test]
    fn running_cannot_transition_to_pending() {
        assert!(!AllocationState::Running.can_transition_to(&AllocationState::Pending));
    }

    #[test]
    fn checkpointing_can_transition_to_suspended() {
        assert!(AllocationState::Checkpointing.can_transition_to(&AllocationState::Suspended));
    }

    #[test]
    fn checkpointing_can_transition_to_failed() {
        assert!(AllocationState::Checkpointing.can_transition_to(&AllocationState::Failed));
    }

    #[test]
    fn checkpointing_can_transition_to_cancelled() {
        assert!(AllocationState::Checkpointing.can_transition_to(&AllocationState::Cancelled));
    }

    #[test]
    fn suspended_can_transition_to_pending() {
        assert!(AllocationState::Suspended.can_transition_to(&AllocationState::Pending));
    }

    #[test]
    fn suspended_can_transition_to_cancelled() {
        assert!(AllocationState::Suspended.can_transition_to(&AllocationState::Cancelled));
    }

    #[test]
    fn suspended_can_transition_to_failed() {
        assert!(AllocationState::Suspended.can_transition_to(&AllocationState::Failed));
    }

    #[test]
    fn completed_is_terminal() {
        assert!(AllocationState::Completed.is_terminal());
    }

    #[test]
    fn failed_is_terminal() {
        assert!(AllocationState::Failed.is_terminal());
    }

    #[test]
    fn cancelled_is_terminal() {
        assert!(AllocationState::Cancelled.is_terminal());
    }

    #[test]
    fn running_is_not_terminal() {
        assert!(!AllocationState::Running.is_terminal());
    }

    #[test]
    fn terminal_states_cannot_transition_to_anything_except_failed_to_pending() {
        let all_states = [
            AllocationState::Pending,
            AllocationState::Staging,
            AllocationState::Running,
            AllocationState::Checkpointing,
            AllocationState::Suspended,
            AllocationState::Completed,
            AllocationState::Failed,
            AllocationState::Cancelled,
        ];

        // Completed and Cancelled are fully terminal
        for terminal in &[AllocationState::Completed, AllocationState::Cancelled] {
            for target in &all_states {
                assert!(
                    !terminal.can_transition_to(target),
                    "{terminal:?} should not transition to {target:?}"
                );
            }
        }

        // Failed can only transition to Pending (service reconciliation)
        assert!(AllocationState::Failed.can_transition_to(&AllocationState::Pending));
        for target in &all_states {
            if *target != AllocationState::Pending {
                assert!(
                    !AllocationState::Failed.can_transition_to(target),
                    "Failed should not transition to {target:?}"
                );
            }
        }
    }

    // ── NodeState transition tests ──

    #[test]
    fn unknown_can_transition_to_booting() {
        assert!(NodeState::Unknown.can_transition_to(&NodeState::Booting));
    }

    #[test]
    fn unknown_can_transition_to_failed() {
        assert!(NodeState::Unknown.can_transition_to(&NodeState::Failed {
            reason: "hw error".into()
        }));
    }

    #[test]
    fn booting_can_transition_to_ready() {
        assert!(NodeState::Booting.can_transition_to(&NodeState::Ready));
    }

    #[test]
    fn booting_can_transition_to_failed() {
        assert!(NodeState::Booting.can_transition_to(&NodeState::Failed {
            reason: "boot failed".into()
        }));
    }

    #[test]
    fn ready_can_transition_to_degraded() {
        assert!(NodeState::Ready.can_transition_to(&NodeState::Degraded {
            reason: "heartbeat missed".into()
        }));
    }

    #[test]
    fn ready_can_transition_to_draining() {
        assert!(NodeState::Ready.can_transition_to(&NodeState::Draining));
    }

    #[test]
    fn ready_can_transition_to_down() {
        assert!(NodeState::Ready.can_transition_to(&NodeState::Down {
            reason: "operator".into()
        }));
    }

    #[test]
    fn degraded_can_transition_to_ready() {
        assert!(NodeState::Degraded {
            reason: "fixed".into()
        }
        .can_transition_to(&NodeState::Ready));
    }

    #[test]
    fn degraded_can_transition_to_down() {
        assert!(NodeState::Degraded {
            reason: "worsened".into()
        }
        .can_transition_to(&NodeState::Down {
            reason: "confirmed".into()
        }));
    }

    #[test]
    fn degraded_can_transition_to_draining() {
        assert!(NodeState::Degraded {
            reason: "draining".into()
        }
        .can_transition_to(&NodeState::Draining));
    }

    #[test]
    fn down_can_transition_to_ready() {
        assert!(NodeState::Down {
            reason: "operator disabled".into()
        }
        .can_transition_to(&NodeState::Ready));
    }

    #[test]
    fn down_can_transition_to_booting() {
        assert!(NodeState::Down {
            reason: "reimage".into()
        }
        .can_transition_to(&NodeState::Booting));
    }

    #[test]
    fn down_can_transition_to_failed() {
        assert!(NodeState::Down {
            reason: "unrecoverable".into()
        }
        .can_transition_to(&NodeState::Failed {
            reason: "hw".into()
        }));
    }

    #[test]
    fn draining_can_transition_to_drained() {
        assert!(NodeState::Draining.can_transition_to(&NodeState::Drained));
    }

    #[test]
    fn draining_cannot_transition_to_ready() {
        assert!(!NodeState::Draining.can_transition_to(&NodeState::Ready));
    }

    #[test]
    fn drained_can_transition_to_ready() {
        assert!(NodeState::Drained.can_transition_to(&NodeState::Ready));
    }

    #[test]
    fn drained_can_transition_to_booting() {
        assert!(NodeState::Drained.can_transition_to(&NodeState::Booting));
    }

    #[test]
    fn failed_node_can_transition_to_booting() {
        assert!(NodeState::Failed {
            reason: "reimage".into()
        }
        .can_transition_to(&NodeState::Booting));
    }

    #[test]
    fn ready_is_operational() {
        assert!(NodeState::Ready.is_operational());
    }

    #[test]
    fn degraded_is_operational() {
        assert!(NodeState::Degraded {
            reason: "minor".into()
        }
        .is_operational());
    }

    #[test]
    fn down_is_not_operational() {
        assert!(!NodeState::Down {
            reason: "down".into()
        }
        .is_operational());
    }

    #[test]
    fn draining_is_not_operational() {
        assert!(!NodeState::Draining.is_operational());
    }

    // ── NetworkDomainState transition tests ──

    #[test]
    fn active_can_transition_to_draining() {
        assert!(NetworkDomainState::Active.can_transition_to(&NetworkDomainState::Draining));
    }

    #[test]
    fn active_cannot_transition_to_released() {
        assert!(!NetworkDomainState::Active.can_transition_to(&NetworkDomainState::Released));
    }

    #[test]
    fn draining_can_transition_to_active() {
        assert!(NetworkDomainState::Draining.can_transition_to(&NetworkDomainState::Active));
    }

    #[test]
    fn draining_can_transition_to_released() {
        assert!(NetworkDomainState::Draining.can_transition_to(&NetworkDomainState::Released));
    }

    #[test]
    fn released_cannot_transition_to_anything() {
        assert!(!NetworkDomainState::Released.can_transition_to(&NetworkDomainState::Active));
        assert!(!NetworkDomainState::Released.can_transition_to(&NetworkDomainState::Draining));
        assert!(!NetworkDomainState::Released.can_transition_to(&NetworkDomainState::Released));
    }

    // ── Serialization round-trip tests ──

    #[test]
    fn allocation_state_serde_roundtrip() {
        let states = [
            AllocationState::Pending,
            AllocationState::Staging,
            AllocationState::Running,
            AllocationState::Checkpointing,
            AllocationState::Suspended,
            AllocationState::Completed,
            AllocationState::Failed,
            AllocationState::Cancelled,
        ];
        for state in &states {
            let json = serde_json::to_string(state).unwrap();
            let deser: AllocationState = serde_json::from_str(&json).unwrap();
            assert_eq!(*state, deser, "roundtrip failed for {state:?}");
        }
    }

    #[test]
    fn node_state_serde_roundtrip() {
        let states = [
            NodeState::Unknown,
            NodeState::Booting,
            NodeState::Ready,
            NodeState::Degraded {
                reason: "test".into(),
            },
            NodeState::Down {
                reason: "test".into(),
            },
            NodeState::Draining,
            NodeState::Drained,
            NodeState::Failed {
                reason: "test".into(),
            },
        ];
        for state in &states {
            let json = serde_json::to_string(state).unwrap();
            let deser: NodeState = serde_json::from_str(&json).unwrap();
            assert_eq!(*state, deser, "roundtrip failed for {state:?}");
        }
    }

    #[test]
    fn cost_weights_default_sums_to_one() {
        let w = CostWeights::default();
        let sum = w.priority
            + w.wait_time
            + w.fair_share
            + w.topology
            + w.data_readiness
            + w.backlog
            + w.energy
            + w.checkpoint_efficiency
            + w.conformance;
        assert!(
            (sum - 1.0).abs() < 1e-10,
            "default weights sum to {sum}, expected 1.0"
        );
    }

    #[test]
    fn cost_weights_serde_roundtrip() {
        let w = CostWeights::default();
        let json = serde_json::to_string(&w).unwrap();
        let deser: CostWeights = serde_json::from_str(&json).unwrap();
        assert!((deser.priority - w.priority).abs() < f64::EPSILON);
        assert!((deser.topology - w.topology).abs() < f64::EPSILON);
    }

    // ── Memory topology tests ──

    #[test]
    fn memory_domain_type_serde_roundtrip() {
        let types = [
            MemoryDomainType::Dram,
            MemoryDomainType::Hbm,
            MemoryDomainType::CxlAttached,
            MemoryDomainType::Unified,
        ];
        for t in &types {
            let json = serde_json::to_string(t).unwrap();
            let deser: MemoryDomainType = serde_json::from_str(&json).unwrap();
            assert_eq!(*t, deser, "roundtrip failed for {t:?}");
        }
    }

    #[test]
    fn memory_link_type_serde_roundtrip() {
        let types = [
            MemoryLinkType::NumaLink,
            MemoryLinkType::CxlSwitch,
            MemoryLinkType::CoherentFabric,
        ];
        for t in &types {
            let json = serde_json::to_string(t).unwrap();
            let deser: MemoryLinkType = serde_json::from_str(&json).unwrap();
            assert_eq!(*t, deser, "roundtrip failed for {t:?}");
        }
    }

    #[test]
    fn memory_policy_serde_roundtrip() {
        let policies = [
            MemoryPolicy::Local,
            MemoryPolicy::Interleave,
            MemoryPolicy::Preferred,
            MemoryPolicy::Bind,
        ];
        for p in &policies {
            let json = serde_json::to_string(p).unwrap();
            let deser: MemoryPolicy = serde_json::from_str(&json).unwrap();
            assert_eq!(*p, deser, "roundtrip failed for {p:?}");
        }
    }

    #[test]
    fn memory_topology_serde_roundtrip() {
        let topo = MemoryTopology {
            domains: vec![
                MemoryDomain {
                    id: 0,
                    domain_type: MemoryDomainType::Dram,
                    capacity_bytes: 128 * 1024 * 1024 * 1024,
                    numa_node: Some(0),
                    attached_cpus: vec![0, 1, 2, 3],
                    attached_gpus: vec![0, 1],
                },
                MemoryDomain {
                    id: 1,
                    domain_type: MemoryDomainType::CxlAttached,
                    capacity_bytes: 256 * 1024 * 1024 * 1024,
                    numa_node: None,
                    attached_cpus: vec![],
                    attached_gpus: vec![],
                },
            ],
            interconnects: vec![MemoryInterconnect {
                domain_a: 0,
                domain_b: 1,
                link_type: MemoryLinkType::CxlSwitch,
                bandwidth_gbps: 64.0,
                latency_ns: 200,
            }],
            total_capacity_bytes: 384 * 1024 * 1024 * 1024,
        };
        let json = serde_json::to_string(&topo).unwrap();
        let deser: MemoryTopology = serde_json::from_str(&json).unwrap();
        assert_eq!(deser.domains.len(), 2);
        assert_eq!(deser.interconnects.len(), 1);
        assert_eq!(deser.total_capacity_bytes, topo.total_capacity_bytes);
    }

    #[test]
    fn resource_constraints_default_has_memory_fields() {
        let rc = ResourceConstraints::default();
        assert!(!rc.require_unified_memory);
        assert!(!rc.prefer_same_numa);
        assert!(!rc.allow_cxl_memory);
        assert!(rc.memory_policy.is_none());
    }

    #[test]
    fn resource_constraints_memory_serde_roundtrip() {
        let rc = ResourceConstraints {
            require_unified_memory: true,
            prefer_same_numa: true,
            allow_cxl_memory: false,
            memory_policy: Some(MemoryPolicy::Interleave),
            ..Default::default()
        };
        let json = serde_json::to_string(&rc).unwrap();
        let deser: ResourceConstraints = serde_json::from_str(&json).unwrap();
        assert!(deser.require_unified_memory);
        assert!(deser.prefer_same_numa);
        assert!(!deser.allow_cxl_memory);
        assert_eq!(deser.memory_policy, Some(MemoryPolicy::Interleave));
    }

    // ── Property-based tests ──

    mod proptests {
        use super::*;
        use proptest::prelude::*;

        fn arb_allocation_state() -> impl Strategy<Value = AllocationState> {
            prop_oneof![
                Just(AllocationState::Pending),
                Just(AllocationState::Staging),
                Just(AllocationState::Running),
                Just(AllocationState::Checkpointing),
                Just(AllocationState::Suspended),
                Just(AllocationState::Completed),
                Just(AllocationState::Failed),
                Just(AllocationState::Cancelled),
            ]
        }

        fn arb_memory_domain_type() -> impl Strategy<Value = MemoryDomainType> {
            prop_oneof![
                Just(MemoryDomainType::Dram),
                Just(MemoryDomainType::Hbm),
                Just(MemoryDomainType::CxlAttached),
                Just(MemoryDomainType::Unified),
            ]
        }

        fn arb_memory_policy() -> impl Strategy<Value = MemoryPolicy> {
            prop_oneof![
                Just(MemoryPolicy::Local),
                Just(MemoryPolicy::Interleave),
                Just(MemoryPolicy::Preferred),
                Just(MemoryPolicy::Bind),
            ]
        }

        proptest! {
            #[test]
            fn memory_domain_type_roundtrip(dt in arb_memory_domain_type()) {
                let json = serde_json::to_string(&dt).unwrap();
                let back: MemoryDomainType = serde_json::from_str(&json).unwrap();
                prop_assert_eq!(dt, back);
            }

            #[test]
            fn memory_policy_roundtrip(p in arb_memory_policy()) {
                let json = serde_json::to_string(&p).unwrap();
                let back: MemoryPolicy = serde_json::from_str(&json).unwrap();
                prop_assert_eq!(p, back);
            }

            #[test]
            fn terminal_states_block_all_transitions(target in arb_allocation_state()) {
                // Completed and Cancelled are fully terminal
                for terminal in &[AllocationState::Completed, AllocationState::Cancelled] {
                    prop_assert!(!terminal.can_transition_to(&target));
                }
                // Failed → Pending is allowed (service reconciliation), everything else blocked
                if target != AllocationState::Pending {
                    prop_assert!(!AllocationState::Failed.can_transition_to(&target));
                }
            }

            #[test]
            fn no_self_transitions(state in arb_allocation_state()) {
                prop_assert!(!state.can_transition_to(&state));
            }

            #[test]
            fn node_count_range_min_le_max(min in 1u32..1000, max in 1u32..1000) {
                prop_assume!(min <= max);
                let _nc = NodeCount::Range { min, max };
                // Construction succeeds without panic
            }
        }
    }
}