decompose 0.1.1

A simple and flexible scheduler and orchestrator to manage non-containerized applications
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
use std::collections::{BTreeMap, HashSet, VecDeque};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use anyhow::{Context, Result, bail};
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::model::{
    DependencyCondition, ExecCheck, ExitMode, HealthProbe, ProcessInstanceSpec, ProcessRuntime,
    ProcessStatus, RestartPolicy,
};

// ---------------------------------------------------------------------------
// Environment variable container
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize)]
pub struct EnvVars(pub BTreeMap<String, String>);

impl EnvVars {
    pub fn merged(&self, other: &EnvVars) -> BTreeMap<String, String> {
        let mut out = self.0.clone();
        out.extend(other.0.clone());
        out
    }
}

impl<'de> Deserialize<'de> for EnvVars {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum RawEnv {
            Map(BTreeMap<String, String>),
            List(Vec<String>),
        }

        let raw = RawEnv::deserialize(deserializer)?;
        let mut env = BTreeMap::new();

        match raw {
            RawEnv::Map(m) => env.extend(m),
            RawEnv::List(entries) => {
                for entry in entries {
                    let (k, v) = entry.split_once('=').ok_or_else(|| {
                        serde::de::Error::custom("invalid env entry, expected KEY=VALUE")
                    })?;
                    env.insert(k.to_string(), v.to_string());
                }
            }
        }

        Ok(Self(env))
    }
}

// ---------------------------------------------------------------------------
// Config structs
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ProjectConfig {
    #[serde(default)]
    pub environment: EnvVars,
    pub processes: BTreeMap<String, ProcessConfig>,
    #[serde(default)]
    pub disable_env_expansion: bool,
    #[serde(default)]
    pub exit_mode: ExitMode,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ProcessConfig {
    pub command: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub working_dir: Option<PathBuf>,
    #[serde(default)]
    pub environment: EnvVars,
    #[serde(default)]
    pub env_file: Vec<String>,
    #[serde(default)]
    pub depends_on: BTreeMap<String, ProcessDependency>,
    #[serde(default = "default_replicas")]
    pub replicas: u16,
    #[serde(default)]
    pub ready_log_line: Option<String>,
    #[serde(default)]
    pub restart_policy: Option<RestartPolicy>,
    #[serde(default)]
    pub backoff_seconds: Option<u64>,
    #[serde(default)]
    pub max_restarts: Option<u32>,
    #[serde(default)]
    pub shutdown: Option<ShutdownConfig>,
    #[serde(default)]
    pub readiness_probe: Option<HealthProbe>,
    #[serde(default)]
    pub liveness_probe: Option<HealthProbe>,
    #[serde(default)]
    pub disabled: bool,
}

fn default_replicas() -> u16 {
    1
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ShutdownConfig {
    #[serde(default = "default_signal")]
    pub signal: i32,
    #[serde(default = "default_timeout")]
    pub timeout_seconds: u64,
    #[serde(default)]
    pub command: Option<String>,
}

fn default_signal() -> i32 {
    15
}

fn default_timeout() -> u64 {
    10
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ProcessDependency {
    #[serde(default)]
    pub condition: DependencyCondition,
}

// ---------------------------------------------------------------------------
// Loading and validation
// ---------------------------------------------------------------------------

pub fn load_config(path: &Path) -> Result<ProjectConfig> {
    let data = fs::read_to_string(path)
        .with_context(|| format!("failed to read config file {}", path.display()))?;
    let cfg: ProjectConfig = serde_yaml_ng::from_str(&data).context("invalid yaml")?;
    validate_config(&cfg)?;
    Ok(cfg)
}

pub fn load_and_merge_configs(paths: &[PathBuf]) -> Result<ProjectConfig> {
    assert!(!paths.is_empty(), "at least one config path is required");
    let mut cfg = load_config(&paths[0])?;
    for path in &paths[1..] {
        let overlay = load_config(path)?;
        cfg = merge_configs(cfg, overlay);
    }
    validate_config(&cfg)?;
    Ok(cfg)
}

/// Upper bound on `replicas` per process. Much higher than any sane local-dev
/// workload; designed to catch typos (e.g. `replicas: 1000`) before they fork
/// a thousand children.
pub const MAX_REPLICAS: u16 = 100;

/// Upper bound on `depends_on` DAG depth. Catches pathological configs where
/// the supervisor would walk an absurdly deep chain on every tick.
pub const MAX_DEPENDENCY_DEPTH: usize = 32;

pub fn validate_config(cfg: &ProjectConfig) -> Result<()> {
    if cfg.processes.is_empty() {
        bail!("config has no processes");
    }

    for (name, proc_cfg) in &cfg.processes {
        if proc_cfg.command.trim().is_empty() {
            bail!("process `{name}` has an empty command");
        }
        if proc_cfg.replicas == 0 {
            bail!("process `{name}` has replicas=0");
        }
        if proc_cfg.replicas > MAX_REPLICAS {
            bail!(
                "process `{name}` has replicas={}, which exceeds the limit of {MAX_REPLICAS}",
                proc_cfg.replicas
            );
        }
        if let Some(ref probe) = proc_cfg.readiness_probe {
            validate_probe(name, "readiness_probe", probe)?;
        }
        if let Some(ref probe) = proc_cfg.liveness_probe {
            validate_probe(name, "liveness_probe", probe)?;
        }
        for (dep, dep_cfg) in &proc_cfg.depends_on {
            if !cfg.processes.contains_key(dep) {
                bail!("process `{name}` depends on unknown process `{dep}`");
            }
            if dep_cfg.condition == DependencyCondition::ProcessLogReady {
                if let Some(dep_proc) = cfg.processes.get(dep) {
                    if dep_proc.ready_log_line.is_none() {
                        bail!(
                            "process `{name}` depends on `{dep}` with condition process_log_ready, \
                             but `{dep}` has no ready_log_line defined"
                        );
                    }
                }
            }
        }
    }

    detect_dependency_cycles(cfg)?;
    check_dependency_depth(cfg)?;

    Ok(())
}

/// Shared sanity checks for `readiness_probe` and `liveness_probe`.
///
/// Zero-valued periods/timeouts are nonsensical (the probe would fire every
/// tick and never succeed). `timeout_seconds > period_seconds` means a probe
/// could still be running when the next scheduling tick wants to start
/// another — reject outright. `timeout == period` is allowed but warned
/// because it leaves no slack for clock drift.
fn validate_probe(process: &str, kind: &str, probe: &HealthProbe) -> Result<()> {
    if probe.period_seconds == 0 {
        bail!("process `{process}` {kind}.period_seconds must be > 0");
    }
    if probe.timeout_seconds == 0 {
        bail!("process `{process}` {kind}.timeout_seconds must be > 0");
    }
    if probe.timeout_seconds > probe.period_seconds {
        bail!(
            "process `{process}` {kind}.timeout_seconds ({}) must be <= period_seconds ({})",
            probe.timeout_seconds,
            probe.period_seconds
        );
    }
    if probe.timeout_seconds == probe.period_seconds {
        eprintln!(
            "warning: process `{process}` {kind}.timeout_seconds == period_seconds ({}) \
             leaves no slack between probe attempts",
            probe.timeout_seconds
        );
    }
    if probe.success_threshold == 0 {
        bail!("process `{process}` {kind}.success_threshold must be > 0");
    }
    if probe.failure_threshold == 0 {
        bail!("process `{process}` {kind}.failure_threshold must be > 0");
    }
    Ok(())
}

/// Walk the dependency DAG and bail if any path exceeds [`MAX_DEPENDENCY_DEPTH`].
/// Assumes cycle detection has already run — so recursion terminates.
fn check_dependency_depth(cfg: &ProjectConfig) -> Result<()> {
    fn walk(node: &str, cfg: &ProjectConfig, depth: usize, stack: &mut Vec<String>) -> Result<()> {
        if depth > MAX_DEPENDENCY_DEPTH {
            stack.push(node.to_string());
            bail!(
                "dependency depth exceeds limit of {MAX_DEPENDENCY_DEPTH}: {}",
                stack.join(" -> ")
            );
        }
        stack.push(node.to_string());
        if let Some(proc) = cfg.processes.get(node) {
            for dep in proc.depends_on.keys() {
                walk(dep, cfg, depth + 1, stack)?;
            }
        }
        stack.pop();
        Ok(())
    }

    for start in cfg.processes.keys() {
        let mut stack: Vec<String> = Vec::new();
        walk(start, cfg, 0, &mut stack)?;
    }
    Ok(())
}

/// Walk the `depends_on` graph with a DFS three-coloring. Returns an error
/// describing the cycle if one is found.
///
/// Assumes all dependency names refer to existing processes (checked earlier
/// in `validate_config`). A self-dependency `a -> a` is reported as the
/// one-node cycle `a -> a`.
fn detect_dependency_cycles(cfg: &ProjectConfig) -> Result<()> {
    use std::collections::HashMap;

    #[derive(Clone, Copy, PartialEq)]
    enum Color {
        White,
        Gray,
        Black,
    }

    fn dfs(
        node: &str,
        cfg: &ProjectConfig,
        color: &mut HashMap<String, Color>,
        path: &mut Vec<String>,
    ) -> Result<()> {
        color.insert(node.to_string(), Color::Gray);
        path.push(node.to_string());

        if let Some(proc) = cfg.processes.get(node) {
            for dep in proc.depends_on.keys() {
                match color.get(dep).copied().unwrap_or(Color::White) {
                    Color::Gray => {
                        let cycle_start = path.iter().position(|n| n == dep).unwrap_or(0);
                        let mut cycle: Vec<String> = path[cycle_start..].to_vec();
                        cycle.push(dep.clone());
                        bail!("dependency cycle detected: {}", cycle.join(" -> "));
                    }
                    Color::White => {
                        dfs(dep, cfg, color, path)?;
                    }
                    Color::Black => {}
                }
            }
        }

        color.insert(node.to_string(), Color::Black);
        path.pop();
        Ok(())
    }

    let mut color: HashMap<String, Color> = cfg
        .processes
        .keys()
        .map(|k| (k.clone(), Color::White))
        .collect();

    for start in cfg.processes.keys() {
        if color.get(start).copied() == Some(Color::White) {
            let mut path: Vec<String> = Vec::new();
            dfs(start, cfg, &mut color, &mut path)?;
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Config merging
// ---------------------------------------------------------------------------

pub fn merge_configs(base: ProjectConfig, overlay: ProjectConfig) -> ProjectConfig {
    let mut env = base.environment.0;
    env.extend(overlay.environment.0);

    let mut processes = base.processes;
    for (name, overlay_proc) in overlay.processes {
        if let Some(base_proc) = processes.get_mut(&name) {
            base_proc.command = overlay_proc.command;
            if overlay_proc.description.is_some() {
                base_proc.description = overlay_proc.description;
            }
            if overlay_proc.working_dir.is_some() {
                base_proc.working_dir = overlay_proc.working_dir;
            }
            base_proc.environment.0.extend(overlay_proc.environment.0);
            base_proc.depends_on.extend(overlay_proc.depends_on);
            if !overlay_proc.env_file.is_empty() {
                base_proc.env_file = overlay_proc.env_file;
            }
            if overlay_proc.replicas != 1 {
                base_proc.replicas = overlay_proc.replicas;
            }
            if overlay_proc.ready_log_line.is_some() {
                base_proc.ready_log_line = overlay_proc.ready_log_line;
            }
            if overlay_proc.restart_policy.is_some() {
                base_proc.restart_policy = overlay_proc.restart_policy;
            }
            if overlay_proc.backoff_seconds.is_some() {
                base_proc.backoff_seconds = overlay_proc.backoff_seconds;
            }
            if overlay_proc.max_restarts.is_some() {
                base_proc.max_restarts = overlay_proc.max_restarts;
            }
            if overlay_proc.shutdown.is_some() {
                base_proc.shutdown = overlay_proc.shutdown;
            }
            if overlay_proc.readiness_probe.is_some() {
                base_proc.readiness_probe = overlay_proc.readiness_probe;
            }
            if overlay_proc.liveness_probe.is_some() {
                base_proc.liveness_probe = overlay_proc.liveness_probe;
            }
            if overlay_proc.disabled {
                base_proc.disabled = true;
            }
        } else {
            processes.insert(name, overlay_proc);
        }
    }

    ProjectConfig {
        environment: EnvVars(env),
        processes,
        disable_env_expansion: overlay.disable_env_expansion || base.disable_env_expansion,
        exit_mode: if overlay.exit_mode != ExitMode::WaitAll {
            overlay.exit_mode
        } else {
            base.exit_mode
        },
    }
}

// ---------------------------------------------------------------------------
// Process subset filtering (Phase A3)
// ---------------------------------------------------------------------------

/// Compute the set of process names that should be selected for launch,
/// expanding to include transitive dependencies when `include_deps` is true.
/// Does NOT mutate the config — the caller decides how to handle non-selected
/// services.
pub fn collect_process_subset(
    cfg: &ProjectConfig,
    names: &[String],
    include_deps: bool,
) -> Result<HashSet<String>> {
    for name in names {
        if !cfg.processes.contains_key(name) {
            bail!("unknown process `{name}`");
        }
    }

    let keep: HashSet<String> = if include_deps {
        let mut visited = HashSet::new();
        let mut queue: VecDeque<String> = names.iter().cloned().collect();
        while let Some(current) = queue.pop_front() {
            if !visited.insert(current.clone()) {
                continue;
            }
            if let Some(proc_cfg) = cfg.processes.get(&current) {
                for dep_name in proc_cfg.depends_on.keys() {
                    queue.push_back(dep_name.clone());
                }
            }
        }
        visited
    } else {
        names.iter().cloned().collect()
    };

    Ok(keep)
}

pub fn filter_process_subset(
    cfg: &mut ProjectConfig,
    names: &[String],
    include_deps: bool,
) -> Result<()> {
    let keep = collect_process_subset(cfg, names, include_deps)?;

    cfg.processes.retain(|name, _| keep.contains(name));

    // If --no-deps, strip depends_on references to excluded processes
    if !include_deps {
        for proc_cfg in cfg.processes.values_mut() {
            proc_cfg.depends_on.retain(|dep, _| keep.contains(dep));
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Config path resolution
// ---------------------------------------------------------------------------

pub fn resolve_config_paths(user_supplied: &[PathBuf], cwd: &Path) -> Result<Vec<PathBuf>> {
    if user_supplied.is_empty() {
        let discovered = discover_config(cwd)?;
        let resolved = if discovered.exists() {
            discovered
                .canonicalize()
                .with_context(|| format!("failed to canonicalize {}", discovered.display()))?
        } else {
            discovered
        };
        return Ok(vec![resolved]);
    }

    let mut resolved = Vec::with_capacity(user_supplied.len());
    for path in user_supplied {
        let abs = if path.is_absolute() {
            path.clone()
        } else {
            cwd.join(path)
        };
        let canonical = if abs.exists() {
            abs.canonicalize()
                .with_context(|| format!("failed to canonicalize {}", abs.display()))?
        } else {
            abs
        };
        resolved.push(canonical);
    }
    Ok(resolved)
}

pub fn discover_config(cwd: &Path) -> Result<PathBuf> {
    const CANDIDATES: [&str; 4] = [
        "decompose.yml",
        "decompose.yaml",
        "compose.yml",
        "compose.yaml",
    ];
    for name in CANDIDATES {
        let candidate = cwd.join(name);
        if candidate.exists() {
            return Ok(candidate);
        }
    }
    bail!("no config file found (tried decompose.yml, decompose.yaml, compose.yml, compose.yaml)")
}

// ---------------------------------------------------------------------------
// .env file loading
// ---------------------------------------------------------------------------

pub fn parse_dotenv(path: &Path) -> Result<BTreeMap<String, String>> {
    let data = fs::read_to_string(path)
        .with_context(|| format!("failed to read env file {}", path.display()))?;
    parse_dotenv_with_source(&data, Some(&path.display().to_string()))
}

pub fn parse_dotenv_str(data: &str) -> Result<BTreeMap<String, String>> {
    parse_dotenv_with_source(data, None)
}

/// Parse a `.env`-style file. Malformed lines (no `=` separator, empty key)
/// are skipped but emit a warning on stderr so users can notice typos.
///
/// `source` is an optional label (typically the file path) used in warnings.
pub fn parse_dotenv_with_source(
    data: &str,
    source: Option<&str>,
) -> Result<BTreeMap<String, String>> {
    let mut env = BTreeMap::new();

    for (idx, line) in data.lines().enumerate() {
        let line_no = idx + 1;
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        let trimmed = trimmed.strip_prefix("export ").unwrap_or(trimmed);

        let Some((key, value)) = trimmed.split_once('=') else {
            warn_malformed_dotenv(source, line_no, line, "missing '=' separator");
            continue;
        };

        let key = key.trim();
        if key.is_empty() {
            warn_malformed_dotenv(source, line_no, line, "empty key");
            continue;
        }

        let value = strip_quotes(value.trim());
        env.insert(key.to_string(), value);
    }

    Ok(env)
}

fn warn_malformed_dotenv(source: Option<&str>, line_no: usize, line: &str, reason: &str) {
    let src = source.unwrap_or("<env>");
    eprintln!(
        "warning: {src}:{line_no}: skipping malformed env line ({reason}): {:?}",
        line.trim_end()
    );
}

fn strip_quotes(s: &str) -> String {
    if s.len() >= 2
        && ((s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')))
    {
        return s[1..s.len() - 1].to_string();
    }
    s.to_string()
}

pub fn load_dotenv_files(
    cwd: &Path,
    explicit: &[PathBuf],
    disable_auto: bool,
) -> Result<BTreeMap<String, String>> {
    let mut env = BTreeMap::new();

    if !disable_auto {
        let dotenv_path = cwd.join(".env");
        if dotenv_path.exists() {
            let parsed = parse_dotenv(&dotenv_path)?;
            env.extend(parsed);
        }
    }

    for path in explicit {
        let abs = if path.is_absolute() {
            path.clone()
        } else {
            cwd.join(path)
        };
        let parsed = parse_dotenv(&abs)?;
        env.extend(parsed);
    }

    Ok(env)
}

// ---------------------------------------------------------------------------
// Variable interpolation
// ---------------------------------------------------------------------------

/// Matches, in priority order:
///   1. `$$` — literal `$` escape (consumes both dollars)
///   2. `${...}` — braced expansion (greedy up to the FIRST `}`)
///   3. `$IDENT` — unbraced expansion (alpha/underscore then
///      alphanumeric/underscore)
///   4. `$` — lone `$` with no valid follower; emitted as-is
///
/// The braced form intentionally uses `[^}]*` so a nested `${B:-c}` inside a
/// default is NOT parsed recursively — the scanner grabs the first close brace
/// and emits the default literally. This behavior is locked in by
/// `interpolate_nested_default_is_not_recursive`; do not "fix" it here.
static INTERPOLATE_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\$\$|\$\{([^}]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)|\$").unwrap());

pub fn interpolate_vars(input: &str, vars: &BTreeMap<String, String>) -> String {
    INTERPOLATE_RE
        .replace_all(input, |caps: &Captures<'_>| {
            let whole = &caps[0];
            if whole == "$$" {
                return "$".to_string();
            }
            if whole == "$" {
                // Lone `$` with no valid follower — emit verbatim.
                return "$".to_string();
            }
            if let Some(inner) = caps.get(1) {
                // `${...}` form — optionally with `:-default`.
                let (name, default) = match inner.as_str().split_once(":-") {
                    Some((n, d)) => (n, Some(d)),
                    None => (inner.as_str(), None),
                };
                return match lookup_var(name, vars) {
                    Some(v) => v,
                    None => default.unwrap_or("").to_string(),
                };
            }
            if let Some(name) = caps.get(2) {
                // `$IDENT` form — undefined becomes empty.
                return lookup_var(name.as_str(), vars).unwrap_or_default();
            }
            // Unreachable given the regex above, but fall back to the raw match.
            whole.to_string()
        })
        .into_owned()
}

fn lookup_var(name: &str, vars: &BTreeMap<String, String>) -> Option<String> {
    if let Some(v) = vars.get(name) {
        return Some(v.clone());
    }
    std::env::var(name).ok()
}

/// Walks the tree of interpolated config fields, substituting `${VAR}` and
/// `$VAR` references against `vars`. Each impl is responsible only for its
/// own string-valued fields and for recursing into children; containers like
/// `Option<T>` and `Vec<T>` are handled by blanket impls. Cross-cutting
/// concerns (building the global/per-process var sets, global-env sequential
/// evaluation, the `disable_env_expansion` short-circuit) live in
/// `apply_interpolation` rather than in the trait.
trait Interpolate {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>);
}

impl Interpolate for String {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        *self = interpolate_vars(self, vars);
    }
}

impl Interpolate for PathBuf {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        *self = PathBuf::from(interpolate_vars(&self.to_string_lossy(), vars));
    }
}

impl<T: Interpolate> Interpolate for Option<T> {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        if let Some(inner) = self {
            inner.interpolate(vars);
        }
    }
}

impl Interpolate for ExecCheck {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        self.command.interpolate(vars);
    }
}

impl Interpolate for HealthProbe {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        self.exec.interpolate(vars);
        // `http_get` has no interpolated fields today; if it gains one
        // (e.g. `path`), add it here.
    }
}

impl Interpolate for ShutdownConfig {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        self.command.interpolate(vars);
    }
}

impl Interpolate for ProcessConfig {
    fn interpolate(&mut self, vars: &BTreeMap<String, String>) {
        self.command.interpolate(vars);
        self.description.interpolate(vars);
        self.working_dir.interpolate(vars);
        self.ready_log_line.interpolate(vars);
        self.shutdown.interpolate(vars);
        self.readiness_probe.interpolate(vars);
        self.liveness_probe.interpolate(vars);
        // `environment` is interpolated by `apply_interpolation` after its
        // caller has assembled the process-scoped var set; doing it here
        // would double-apply.
    }
}

pub fn apply_interpolation(cfg: &mut ProjectConfig) {
    if cfg.disable_env_expansion {
        return;
    }

    // Global env is interpolated sequentially so each entry can reference
    // keys that were declared earlier in the map.
    let mut global_vars = BTreeMap::new();
    let keys: Vec<String> = cfg.environment.0.keys().cloned().collect();
    for key in &keys {
        if let Some(raw) = cfg.environment.0.get(key) {
            let interpolated = interpolate_vars(raw, &global_vars);
            global_vars.insert(key.clone(), interpolated.clone());
            cfg.environment.0.insert(key.clone(), interpolated);
        }
    }

    for proc_cfg in cfg.processes.values_mut() {
        let mut vars = global_vars.clone();
        vars.extend(proc_cfg.environment.0.clone());

        proc_cfg.interpolate(&vars);

        // Per-process env entries resolve against a frozen snapshot that
        // already includes their own (uninterpolated) values, matching the
        // previous hand-written behavior.
        let env_keys: Vec<String> = proc_cfg.environment.0.keys().cloned().collect();
        for key in &env_keys {
            if let Some(raw) = proc_cfg.environment.0.get(key) {
                let interpolated = interpolate_vars(raw, &vars);
                proc_cfg.environment.0.insert(key.clone(), interpolated);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Per-service config hash
// ---------------------------------------------------------------------------

/// Serializable view of `ProcessConfig` with the fields that Docker Compose
/// treats as changeable-without-recreate (`depends_on`, `replicas`, `disabled`)
/// stripped out. Used exclusively to compute [`compute_config_hash`]. All
/// remaining fields are serialized through their `Serialize` impls; the
/// containers (`EnvVars` wraps a `BTreeMap`, `env_file` is a `Vec`) emit
/// deterministic key orderings, so `serde_json::to_vec` over this struct is
/// stable across runs.
#[derive(Serialize)]
struct ProcessConfigHashView<'a> {
    command: &'a str,
    description: &'a Option<String>,
    working_dir: &'a Option<PathBuf>,
    environment: &'a EnvVars,
    env_file: &'a Vec<String>,
    ready_log_line: &'a Option<String>,
    restart_policy: &'a Option<RestartPolicy>,
    backoff_seconds: &'a Option<u64>,
    max_restarts: &'a Option<u32>,
    shutdown: &'a Option<ShutdownConfig>,
    readiness_probe: &'a Option<HealthProbe>,
    liveness_probe: &'a Option<HealthProbe>,
}

/// Compute a stable SHA-256 hash of the service's `ProcessConfig`, **excluding**
/// `depends_on`, `replicas`, and `disabled`. These three fields can be changed
/// on a running compose stack without tearing down the underlying service —
/// mirroring Docker Compose's recreate semantics. Everything else (command,
/// environment, env files, working dir, probes, shutdown, restart policy, etc.)
/// contributes to the hash, so any change there means the service must be
/// recreated.
///
/// Used by the reload path to diff services: same hash == same definition,
/// different hash == tear down and respawn.
pub fn compute_config_hash(cfg: &ProcessConfig) -> String {
    let view = ProcessConfigHashView {
        command: &cfg.command,
        description: &cfg.description,
        working_dir: &cfg.working_dir,
        environment: &cfg.environment,
        env_file: &cfg.env_file,
        ready_log_line: &cfg.ready_log_line,
        restart_policy: &cfg.restart_policy,
        backoff_seconds: &cfg.backoff_seconds,
        max_restarts: &cfg.max_restarts,
        shutdown: &cfg.shutdown,
        readiness_probe: &cfg.readiness_probe,
        liveness_probe: &cfg.liveness_probe,
    };
    // serde_json serialization of structs is field-declaration-order stable,
    // and the containers we reference (BTreeMap, Vec, Option) are themselves
    // deterministic. Unwrap is safe: the view contains only json-serializable
    // leaf types (strings, numbers, maps keyed by String).
    let bytes = serde_json::to_vec(&view).expect("ProcessConfigHashView is serializable");
    let mut hasher = Sha256::new();
    hasher.update(&bytes);
    format!("{:x}", hasher.finalize())
}

// ---------------------------------------------------------------------------
// Process instance building
// ---------------------------------------------------------------------------

pub fn build_process_instances(
    cfg: &ProjectConfig,
    cwd: &Path,
    dotenv: &BTreeMap<String, String>,
) -> BTreeMap<String, ProcessRuntime> {
    let mut out = BTreeMap::new();

    for (base_name, proc_cfg) in &cfg.processes {
        // Hash once per service; replicas share the same config hash since
        // they're all spawned from the same ProcessConfig entry.
        let config_hash = compute_config_hash(proc_cfg);
        for idx in 0..proc_cfg.replicas {
            let replica = idx + 1;
            let instance_name = if proc_cfg.replicas > 1 {
                format!("{base_name}[{replica}]")
            } else {
                base_name.clone()
            };

            let mut env = dotenv.clone();
            env.extend(cfg.environment.0.clone());

            for env_file_path in &proc_cfg.env_file {
                let abs = if Path::new(env_file_path).is_absolute() {
                    PathBuf::from(env_file_path)
                } else {
                    cwd.join(env_file_path)
                };
                if let Ok(parsed) = parse_dotenv(&abs) {
                    env.extend(parsed);
                }
            }

            env.extend(proc_cfg.environment.0.clone());

            let working_dir = match &proc_cfg.working_dir {
                Some(d) if d.is_absolute() => d.clone(),
                Some(d) => cwd.join(d),
                None => cwd.to_path_buf(),
            };

            let depends_on = proc_cfg
                .depends_on
                .iter()
                .map(|(k, dep)| (k.clone(), dep.condition))
                .collect::<BTreeMap<_, _>>();

            let disabled = proc_cfg.disabled;

            let spec = ProcessInstanceSpec {
                name: instance_name.clone(),
                base_name: base_name.clone(),
                replica,
                command: proc_cfg.command.clone(),
                description: proc_cfg.description.clone(),
                working_dir,
                environment: env,
                depends_on,
                ready_log_line: proc_cfg.ready_log_line.clone(),
                restart_policy: proc_cfg.restart_policy.unwrap_or(RestartPolicy::No),
                backoff_seconds: proc_cfg.backoff_seconds.unwrap_or(1),
                max_restarts: proc_cfg.max_restarts,
                shutdown_signal: proc_cfg.shutdown.as_ref().map(|s| s.signal),
                shutdown_timeout_seconds: proc_cfg
                    .shutdown
                    .as_ref()
                    .map(|s| s.timeout_seconds)
                    .unwrap_or(10),
                shutdown_command: proc_cfg.shutdown.as_ref().and_then(|s| s.command.clone()),
                readiness_probe: proc_cfg.readiness_probe.clone(),
                liveness_probe: proc_cfg.liveness_probe.clone(),
                disabled,
                config_hash: config_hash.clone(),
            };

            let name_handle = crate::model::make_name_handle(instance_name.clone());
            out.insert(
                instance_name,
                ProcessRuntime {
                    spec,
                    status: if disabled {
                        ProcessStatus::Disabled
                    } else {
                        ProcessStatus::Pending
                    },
                    started_once: false,
                    log_ready: false,
                    restart_count: 0,
                    ready: false,
                    alive: true,
                    name_handle,
                },
            );
        }
    }

    out
}

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

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::tempdir;

    use super::*;

    #[test]
    fn env_vars_deserialize_from_map() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
environment:
  A: "1"
  B: "2"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        assert_eq!(cfg.environment.0.get("A"), Some(&"1".to_string()));
        assert_eq!(cfg.environment.0.get("B"), Some(&"2".to_string()));
    }

    #[test]
    fn env_vars_deserialize_from_list() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
environment:
  - A=1
  - B=2
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        assert_eq!(cfg.environment.0.get("A"), Some(&"1".to_string()));
        assert_eq!(cfg.environment.0.get("B"), Some(&"2".to_string()));
    }

    #[test]
    fn validate_rejects_unknown_dependency() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
    depends_on:
      missing:
        condition: process_started
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject missing dep");
        assert!(err.to_string().contains("depends on unknown process"));
    }

    #[test]
    fn validate_rejects_log_ready_without_ready_log_line() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
    depends_on:
      b:
        condition: process_log_ready
  b:
    command: "echo"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject missing ready_log_line");
        assert!(err.to_string().contains("ready_log_line"));
    }

    #[test]
    fn validate_rejects_self_dependency() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
    depends_on:
      a:
        condition: process_started
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject self dependency");
        assert!(
            err.to_string().contains("dependency cycle detected"),
            "unexpected error: {err}"
        );
        assert!(
            err.to_string().contains("a -> a"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_rejects_two_node_cycle() {
        let yaml = r#"
processes:
  a:
    command: "echo a"
    depends_on:
      b:
        condition: process_started
  b:
    command: "echo b"
    depends_on:
      a:
        condition: process_started
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject cycle");
        assert!(
            err.to_string().contains("dependency cycle detected"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_rejects_transitive_cycle() {
        let yaml = r#"
processes:
  a:
    command: "echo a"
    depends_on:
      b:
        condition: process_started
  b:
    command: "echo b"
    depends_on:
      c:
        condition: process_started
  c:
    command: "echo c"
    depends_on:
      a:
        condition: process_started
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject transitive cycle");
        assert!(
            err.to_string().contains("dependency cycle detected"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_accepts_dag() {
        // a -> b -> c, and a -> c directly. Not a cycle.
        let yaml = r#"
processes:
  a:
    command: "echo a"
    depends_on:
      b:
        condition: process_started
      c:
        condition: process_started
  b:
    command: "echo b"
    depends_on:
      c:
        condition: process_started
  c:
    command: "echo c"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        validate_config(&cfg).expect("dag should validate");
    }

    #[test]
    fn validate_rejects_replicas_over_limit() {
        let yaml = format!(
            r#"
processes:
  a:
    command: "echo hi"
    replicas: {n}
"#,
            n = MAX_REPLICAS + 1
        );
        let cfg: ProjectConfig = serde_yaml_ng::from_str(&yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("must reject over-limit replicas");
        assert!(
            err.to_string().contains("exceeds the limit"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_accepts_replicas_at_limit() {
        let yaml = format!(
            r#"
processes:
  a:
    command: "echo hi"
    replicas: {n}
"#,
            n = MAX_REPLICAS
        );
        let cfg: ProjectConfig = serde_yaml_ng::from_str(&yaml).expect("parse config");
        validate_config(&cfg).expect("exactly at the limit is allowed");
    }

    #[test]
    fn validate_rejects_zero_probe_period() {
        let yaml = r#"
processes:
  a:
    command: "echo"
    readiness_probe:
      exec:
        command: "true"
      period_seconds: 0
      timeout_seconds: 1
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("zero period rejected");
        assert!(
            err.to_string().contains("period_seconds must be > 0"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_rejects_probe_timeout_greater_than_period() {
        let yaml = r#"
processes:
  a:
    command: "echo"
    liveness_probe:
      exec:
        command: "true"
      period_seconds: 5
      timeout_seconds: 10
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("timeout > period rejected");
        assert!(
            err.to_string().contains("must be <= period_seconds"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_accepts_probe_with_sane_period_and_timeout() {
        let yaml = r#"
processes:
  a:
    command: "echo"
    readiness_probe:
      exec:
        command: "true"
      period_seconds: 10
      timeout_seconds: 2
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        validate_config(&cfg).expect("sane probe accepted");
    }

    #[test]
    fn validate_rejects_zero_probe_thresholds() {
        let yaml = r#"
processes:
  a:
    command: "echo"
    readiness_probe:
      exec:
        command: "true"
      period_seconds: 5
      timeout_seconds: 1
      success_threshold: 0
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("zero success threshold rejected");
        assert!(
            err.to_string().contains("success_threshold must be > 0"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_rejects_overly_deep_dependency_chain() {
        // Build a linear chain p0 -> p1 -> ... -> p(MAX+2) so depth exceeds
        // the limit. Every process depends on the next, no cycles.
        let total = MAX_DEPENDENCY_DEPTH + 3;
        let mut yaml = String::from("processes:\n");
        for i in 0..total {
            yaml.push_str(&format!("  p{i}:\n    command: \"echo\"\n"));
            if i + 1 < total {
                yaml.push_str("    depends_on:\n");
                yaml.push_str(&format!(
                    "      p{next}:\n        condition: process_started\n",
                    next = i + 1
                ));
            }
        }
        let cfg: ProjectConfig = serde_yaml_ng::from_str(&yaml).expect("parse config");
        let err = validate_config(&cfg).expect_err("deep chain rejected");
        assert!(
            err.to_string().contains("dependency depth exceeds limit"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn validate_accepts_log_ready_with_ready_log_line() {
        let yaml = r#"
processes:
  a:
    command: "echo hi"
    depends_on:
      b:
        condition: process_log_ready
  b:
    command: "echo ready"
    ready_log_line: "ready"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        validate_config(&cfg).expect("should be valid");
    }

    #[test]
    fn build_instances_applies_replicas_and_injected_env() {
        let yaml = r#"
environment:
  GLOBAL: g
processes:
  api:
    command: "echo hi"
    replicas: 2
    environment:
      LOCAL: l
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        validate_config(&cfg).expect("valid config");
        let cwd = Path::new("/tmp");
        let dotenv = BTreeMap::new();
        let out = build_process_instances(&cfg, cwd, &dotenv);

        assert_eq!(out.len(), 2);
        let first = out.get("api[1]").expect("first replica");
        assert!(out.contains_key("api[2]"), "second replica");
        assert_eq!(first.spec.environment.get("GLOBAL"), Some(&"g".to_string()));
        assert_eq!(first.spec.environment.get("LOCAL"), Some(&"l".to_string()));
    }

    #[test]
    fn build_instances_includes_restart_fields() {
        let yaml = r#"
processes:
  api:
    command: "echo hi"
    restart_policy: on_failure
    backoff_seconds: 5
    max_restarts: 3
    shutdown:
      signal: 9
      timeout_seconds: 30
      command: "cleanup.sh"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).expect("parse config");
        let cwd = Path::new("/tmp");
        let dotenv = BTreeMap::new();
        let out = build_process_instances(&cfg, cwd, &dotenv);

        let api = out.get("api").expect("api process");
        assert_eq!(api.spec.restart_policy, RestartPolicy::OnFailure);
        assert_eq!(api.spec.backoff_seconds, 5);
        assert_eq!(api.spec.max_restarts, Some(3));
        assert_eq!(api.spec.shutdown_signal, Some(9));
        assert_eq!(api.spec.shutdown_timeout_seconds, 30);
        assert_eq!(api.spec.shutdown_command, Some("cleanup.sh".to_string()));
    }

    #[test]
    fn discover_config_uses_documented_priority() {
        let dir = tempdir().expect("tempdir");
        let root = dir.path();
        fs::write(
            root.join("decompose.yml"),
            "processes: {a: {command: 'echo'}}",
        )
        .expect("write");
        fs::write(
            root.join("decompose.yaml"),
            "processes: {a: {command: 'echo'}}",
        )
        .expect("write");
        fs::write(
            root.join("compose.yml"),
            "processes: {a: {command: 'echo'}}",
        )
        .expect("write");
        fs::write(
            root.join("compose.yaml"),
            "processes: {a: {command: 'echo'}}",
        )
        .expect("write");

        let chosen = discover_config(root).expect("discover");
        assert_eq!(chosen, root.join("decompose.yml"));
    }

    #[test]
    fn merge_overlays_process_fields() {
        let base_yaml = r#"
processes:
  api:
    command: "echo base"
    description: "base desc"
    replicas: 1
    environment:
      A: "1"
"#;
        let overlay_yaml = r#"
processes:
  api:
    command: "echo overlay"
    replicas: 3
    environment:
      B: "2"
"#;
        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let overlay: ProjectConfig = serde_yaml_ng::from_str(overlay_yaml).unwrap();
        let merged = merge_configs(base, overlay);

        let api = merged.processes.get("api").unwrap();
        assert_eq!(api.command, "echo overlay");
        assert_eq!(api.description.as_deref(), Some("base desc"));
        assert_eq!(api.replicas, 3);
        assert_eq!(api.environment.0.get("A"), Some(&"1".to_string()));
        assert_eq!(api.environment.0.get("B"), Some(&"2".to_string()));
    }

    #[test]
    fn merge_adds_new_processes() {
        let base_yaml = r#"
processes:
  api:
    command: "echo api"
"#;
        let overlay_yaml = r#"
processes:
  worker:
    command: "echo worker"
"#;
        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let overlay: ProjectConfig = serde_yaml_ng::from_str(overlay_yaml).unwrap();
        let merged = merge_configs(base, overlay);

        assert!(merged.processes.contains_key("api"));
        assert!(merged.processes.contains_key("worker"));
    }

    #[test]
    fn merge_global_env() {
        let base_yaml = r#"
environment:
  A: "1"
  B: "base"
processes:
  x:
    command: "echo"
"#;
        let overlay_yaml = r#"
environment:
  B: "overlay"
  C: "3"
processes:
  x:
    command: "echo"
"#;
        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let overlay: ProjectConfig = serde_yaml_ng::from_str(overlay_yaml).unwrap();
        let merged = merge_configs(base, overlay);

        assert_eq!(merged.environment.0.get("A"), Some(&"1".to_string()));
        assert_eq!(merged.environment.0.get("B"), Some(&"overlay".to_string()));
        assert_eq!(merged.environment.0.get("C"), Some(&"3".to_string()));
    }

    #[test]
    fn load_and_merge_works() {
        let dir = tempdir().unwrap();
        let base_path = dir.path().join("base.yaml");
        let overlay_path = dir.path().join("overlay.yaml");

        fs::write(
            &base_path,
            r#"
processes:
  api:
    command: "echo base"
    environment:
      PORT: "3000"
"#,
        )
        .unwrap();

        fs::write(
            &overlay_path,
            r#"
processes:
  api:
    command: "echo overlay"
    environment:
      PORT: "8080"
"#,
        )
        .unwrap();

        let cfg = load_and_merge_configs(&[base_path, overlay_path]).unwrap();
        let api = cfg.processes.get("api").unwrap();
        assert_eq!(api.command, "echo overlay");
        assert_eq!(api.environment.0.get("PORT"), Some(&"8080".to_string()));
    }

    #[test]
    fn merge_three_layer_overlay_last_wins() {
        // Three layers: base -> staging -> local. Each layer overrides part
        // of the previous one. Locks in that env maps layer additively
        // while simple scalars (command, working_dir) follow last-write-wins
        // and probes/shutdown get replaced as a whole (overlay struct wins).
        let base_yaml = r#"
environment:
  TIER: "base"
  ONLY_BASE: "b"
processes:
  api:
    command: "echo base"
    working_dir: "/srv/base"
    environment:
      PORT: "3000"
      FROM_BASE: "yes"
    readiness_probe:
      exec:
        command: "check-base"
      period_seconds: 5
    shutdown:
      signal: 15
      timeout_seconds: 5
"#;
        let staging_yaml = r#"
environment:
  TIER: "staging"
  ONLY_STAGING: "s"
processes:
  api:
    command: "echo staging"
    environment:
      PORT: "4000"
      FROM_STAGING: "yes"
    shutdown:
      signal: 2
      timeout_seconds: 15
"#;
        let local_yaml = r#"
environment:
  TIER: "local"
processes:
  api:
    command: "echo local"
    environment:
      PORT: "9000"
    readiness_probe:
      exec:
        command: "check-local"
      period_seconds: 2
"#;

        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let staging: ProjectConfig = serde_yaml_ng::from_str(staging_yaml).unwrap();
        let local: ProjectConfig = serde_yaml_ng::from_str(local_yaml).unwrap();

        let merged = merge_configs(merge_configs(base, staging), local);

        // Global env: last-wins on conflicts, additive on distinct keys.
        assert_eq!(merged.environment.0.get("TIER"), Some(&"local".to_string()));
        assert_eq!(
            merged.environment.0.get("ONLY_BASE"),
            Some(&"b".to_string())
        );
        assert_eq!(
            merged.environment.0.get("ONLY_STAGING"),
            Some(&"s".to_string())
        );

        let api = merged.processes.get("api").unwrap();
        // Scalars: last file wins.
        assert_eq!(api.command, "echo local");
        // working_dir only set in base, middle & last layer don't touch it.
        assert_eq!(api.working_dir.as_deref(), Some(Path::new("/srv/base")));

        // Process env: all three layers contribute.
        assert_eq!(api.environment.0.get("PORT"), Some(&"9000".to_string()));
        assert_eq!(api.environment.0.get("FROM_BASE"), Some(&"yes".to_string()));
        assert_eq!(
            api.environment.0.get("FROM_STAGING"),
            Some(&"yes".to_string())
        );

        // Probe replaced by local (base's probe should be gone).
        let probe = api.readiness_probe.as_ref().unwrap();
        assert_eq!(probe.exec.as_ref().unwrap().command, "check-local");
        assert_eq!(probe.period_seconds, 2);

        // Shutdown only set in base+staging; staging wins since local left it
        // untouched — demonstrates "intermediate layers stick" behavior.
        let shutdown = api.shutdown.as_ref().unwrap();
        assert_eq!(shutdown.signal, 2);
        assert_eq!(shutdown.timeout_seconds, 15);
    }

    #[test]
    fn merge_overlay_without_matching_process_is_noop() {
        // Overlay referencing a new process adds it; overlay with *no*
        // process section doesn't wipe the base processes. This used to
        // catch a regression where an empty-processes overlay reset the
        // map.
        let base_yaml = r#"
processes:
  api:
    command: "echo api"
  worker:
    command: "echo worker"
"#;
        let overlay_yaml = r#"
environment:
  EXTRA: "1"
processes: {}
"#;
        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let overlay: ProjectConfig = serde_yaml_ng::from_str(overlay_yaml).unwrap();
        let merged = merge_configs(base, overlay);

        assert_eq!(merged.processes.len(), 2);
        assert!(merged.processes.contains_key("api"));
        assert!(merged.processes.contains_key("worker"));
        assert_eq!(merged.environment.0.get("EXTRA"), Some(&"1".to_string()));
    }

    #[test]
    fn merge_preserves_base_fields_when_overlay_omits_them() {
        // When overlay only tweaks command, the rest of the base process
        // (description, env_file, depends_on, probe) should survive
        // untouched. Prevents accidental "overlay resets to defaults".
        let base_yaml = r#"
processes:
  db:
    command: "echo db"
  api:
    command: "echo base"
    description: "base description"
    env_file: ["base.env"]
    depends_on:
      db:
        condition: process_started
    liveness_probe:
      exec:
        command: "ping"
      period_seconds: 3
"#;
        let overlay_yaml = r#"
processes:
  api:
    command: "echo overlay"
"#;
        let base: ProjectConfig = serde_yaml_ng::from_str(base_yaml).unwrap();
        let overlay: ProjectConfig = serde_yaml_ng::from_str(overlay_yaml).unwrap();
        let merged = merge_configs(base, overlay);

        let api = merged.processes.get("api").unwrap();
        assert_eq!(api.command, "echo overlay");
        assert_eq!(api.description.as_deref(), Some("base description"));
        assert_eq!(api.env_file, vec!["base.env"]);
        assert!(api.depends_on.contains_key("db"));
        let probe = api.liveness_probe.as_ref().unwrap();
        assert_eq!(probe.exec.as_ref().unwrap().command, "ping");
    }

    #[test]
    fn filter_process_subset_with_deps() {
        let yaml = r#"
processes:
  db:
    command: "echo db"
  api:
    command: "echo api"
    depends_on:
      db:
        condition: process_started
  worker:
    command: "echo worker"
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        filter_process_subset(&mut cfg, &["api".to_string()], true).unwrap();
        assert!(cfg.processes.contains_key("api"));
        assert!(cfg.processes.contains_key("db"));
        assert!(!cfg.processes.contains_key("worker"));
    }

    #[test]
    fn filter_process_subset_no_deps() {
        let yaml = r#"
processes:
  db:
    command: "echo db"
  api:
    command: "echo api"
    depends_on:
      db:
        condition: process_started
  worker:
    command: "echo worker"
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        filter_process_subset(&mut cfg, &["api".to_string()], false).unwrap();
        assert!(cfg.processes.contains_key("api"));
        assert!(!cfg.processes.contains_key("db"));
        // depends_on should be stripped for excluded processes
        assert!(cfg.processes.get("api").unwrap().depends_on.is_empty());
    }

    #[test]
    fn filter_process_subset_rejects_unknown() {
        let yaml = r#"
processes:
  api:
    command: "echo"
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        let err = filter_process_subset(&mut cfg, &["nope".to_string()], true).unwrap_err();
        assert!(err.to_string().contains("unknown process"));
    }

    #[test]
    fn parse_dotenv_keeps_valid_lines_when_malformed_present() {
        // Malformed lines (no '=') should be skipped but not cause the parse
        // to fail; valid lines on either side of them must still be returned.
        // A warning is emitted to stderr — we can't capture that here without
        // forking tests, but we assert the good lines survive.
        let data = "GOOD1=yes\nbare_no_equals\n=empty_key_ignored\nGOOD2=also_yes\n";
        let env = parse_dotenv_str(data).expect("parse should not fail on malformed lines");
        assert_eq!(env.get("GOOD1"), Some(&"yes".to_string()));
        assert_eq!(env.get("GOOD2"), Some(&"also_yes".to_string()));
        // Empty-key line is dropped with a warning.
        assert!(!env.contains_key(""));
        // Stray bare line didn't get turned into anything accidentally.
        assert!(!env.contains_key("bare_no_equals"));
        assert_eq!(env.len(), 2);
    }

    #[test]
    fn parse_dotenv_basic() {
        let data = r#"
# comment
KEY1=value1
KEY2="quoted value"
KEY3='single quoted'
export KEY4=exported

"#;
        let env = parse_dotenv_str(data).unwrap();
        assert_eq!(env.get("KEY1"), Some(&"value1".to_string()));
        assert_eq!(env.get("KEY2"), Some(&"quoted value".to_string()));
        assert_eq!(env.get("KEY3"), Some(&"single quoted".to_string()));
        assert_eq!(env.get("KEY4"), Some(&"exported".to_string()));
    }

    #[test]
    fn load_dotenv_from_cwd() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join(".env"), "AUTO=loaded\n").unwrap();

        let env = load_dotenv_files(dir.path(), &[], false).unwrap();
        assert_eq!(env.get("AUTO"), Some(&"loaded".to_string()));
    }

    #[test]
    fn load_dotenv_disabled() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join(".env"), "AUTO=loaded\n").unwrap();

        let env = load_dotenv_files(dir.path(), &[], true).unwrap();
        assert!(env.is_empty());
    }

    #[test]
    fn load_dotenv_explicit_overrides() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join(".env"), "KEY=auto\n").unwrap();
        fs::write(dir.path().join("custom.env"), "KEY=custom\n").unwrap();

        let explicit = vec![dir.path().join("custom.env")];
        let env = load_dotenv_files(dir.path(), &explicit, false).unwrap();
        assert_eq!(env.get("KEY"), Some(&"custom".to_string()));
    }

    #[test]
    fn dotenv_precedence_in_build_instances() {
        let yaml = r#"
environment:
  GLOBAL: from_config
processes:
  api:
    command: "echo"
    environment:
      LOCAL: from_process
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        let cwd = Path::new("/tmp");
        let mut dotenv = BTreeMap::new();
        dotenv.insert("GLOBAL".to_string(), "from_dotenv".to_string());
        dotenv.insert("DOTONLY".to_string(), "dotenv_val".to_string());

        let out = build_process_instances(&cfg, cwd, &dotenv);
        let api = out.get("api").unwrap();
        assert_eq!(
            api.spec.environment.get("GLOBAL"),
            Some(&"from_config".to_string())
        );
        assert_eq!(
            api.spec.environment.get("DOTONLY"),
            Some(&"dotenv_val".to_string())
        );
        assert_eq!(
            api.spec.environment.get("LOCAL"),
            Some(&"from_process".to_string())
        );
    }

    #[test]
    fn interpolate_basic_braced() {
        let mut vars = BTreeMap::new();
        vars.insert("NAME".to_string(), "world".to_string());
        assert_eq!(interpolate_vars("hello ${NAME}", &vars), "hello world");
    }

    #[test]
    fn interpolate_basic_unbraced() {
        let mut vars = BTreeMap::new();
        vars.insert("NAME".to_string(), "world".to_string());
        assert_eq!(interpolate_vars("hello $NAME!", &vars), "hello world!");
    }

    #[test]
    fn interpolate_default_value() {
        let vars = BTreeMap::new();
        assert_eq!(interpolate_vars("${MISSING:-fallback}", &vars), "fallback");
    }

    #[test]
    fn interpolate_default_not_used_when_set() {
        let mut vars = BTreeMap::new();
        vars.insert("VAR".to_string(), "actual".to_string());
        assert_eq!(interpolate_vars("${VAR:-fallback}", &vars), "actual");
    }

    #[test]
    fn interpolate_dollar_escape() {
        let vars = BTreeMap::new();
        assert_eq!(interpolate_vars("price is $$5", &vars), "price is $5");
    }

    #[test]
    fn interpolate_undefined_becomes_empty() {
        let vars = BTreeMap::new();
        assert_eq!(
            interpolate_vars("hello ${UNDEF} world", &vars),
            "hello  world"
        );
    }

    #[test]
    fn interpolate_lone_dollar() {
        let vars = BTreeMap::new();
        assert_eq!(interpolate_vars("just $ here", &vars), "just $ here");
    }

    #[test]
    fn interpolate_multiple_vars() {
        let mut vars = BTreeMap::new();
        vars.insert("A".to_string(), "1".to_string());
        vars.insert("B".to_string(), "2".to_string());
        assert_eq!(interpolate_vars("$A and ${B}", &vars), "1 and 2");
    }

    #[test]
    fn interpolate_double_dollar_then_var() {
        // "$$VAR" should produce a literal "$" followed by the unexpanded
        // text "VAR" — the "$$" escape consumes both dollar signs, so the
        // remaining "VAR" is just plain text (no `$` prefix to kick off
        // another substitution). Set VAR anyway to prove that.
        let mut vars = BTreeMap::new();
        vars.insert("VAR".to_string(), "world".to_string());
        assert_eq!(interpolate_vars("$$VAR", &vars), "$VAR");
        assert_eq!(interpolate_vars("a$$b", &vars), "a$b");
        // Chaining: two escapes in a row still collapse independently.
        assert_eq!(interpolate_vars("$$$$", &vars), "$$");
    }

    #[test]
    fn interpolate_nested_default_is_not_recursive() {
        // Nested `${A:-${B:-c}}` is NOT supported: the scanner grabs the
        // first closing brace and treats everything up to it as the inner
        // expression. The default portion is emitted verbatim — no second
        // pass of interpolation over the fallback text. This test locks in
        // that behavior so a future change is made consciously.
        let mut vars = BTreeMap::new();
        vars.insert("B".to_string(), "bee".to_string());
        // A unset, B set: fallback is the raw literal "${B" (without the
        // inner close) and the outer scanner then prints the trailing "}"
        // as a plain character.
        assert_eq!(interpolate_vars("${A:-${B:-c}}", &vars), "${B:-c}");
        // A unset, simpler nested ${B}: fallback stays literal.
        assert_eq!(interpolate_vars("${A:-${B}}", &vars), "${B}");
        // A set: default branch never runs, so the nesting quirk is
        // invisible and it behaves normally.
        vars.insert("A".to_string(), "ay".to_string());
        assert_eq!(interpolate_vars("${A:-${B}}", &vars), "ay}");
    }

    #[test]
    fn interpolate_adjacent_substitutions() {
        // No separator between two variables — each resolves independently.
        let mut vars = BTreeMap::new();
        vars.insert("A".to_string(), "foo".to_string());
        vars.insert("B".to_string(), "bar".to_string());
        assert_eq!(interpolate_vars("${A}${B}", &vars), "foobar");
        assert_eq!(interpolate_vars("$A$B", &vars), "foobar");
    }

    #[test]
    fn interpolate_empty_default() {
        // `${VAR:-}` with VAR unset should produce the empty string (not
        // something like a literal `${VAR:-}`).
        let vars = BTreeMap::new();
        assert_eq!(interpolate_vars("x${UNSET:-}y", &vars), "xy");
    }

    #[test]
    fn interpolate_unterminated_brace() {
        // `${FOO` with no closing brace is not a valid expansion; the `$` is
        // emitted literally and the rest of the text is left untouched so the
        // user can see what they wrote.
        let mut vars = BTreeMap::new();
        vars.insert("FOO".to_string(), "ignored".to_string());
        assert_eq!(interpolate_vars("hi ${FOO", &vars), "hi ${FOO");
    }

    #[test]
    fn interpolate_var_at_end_of_string() {
        // Expansion that goes right up to the end of the input.
        let mut vars = BTreeMap::new();
        vars.insert("NAME".to_string(), "world".to_string());
        assert_eq!(interpolate_vars("hello ${NAME}", &vars), "hello world");
        assert_eq!(interpolate_vars("hello $NAME", &vars), "hello world");
    }

    #[test]
    fn apply_interpolation_on_config() {
        let yaml = r#"
environment:
  VERSION: "1.0"
processes:
  api:
    command: "run --version ${VERSION}"
    description: "API v${VERSION}"
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        apply_interpolation(&mut cfg);

        let api = cfg.processes.get("api").unwrap();
        assert_eq!(api.command, "run --version 1.0");
        assert_eq!(api.description.as_deref(), Some("API v1.0"));
    }

    #[test]
    fn apply_interpolation_on_probe_commands() {
        let yaml = r#"
environment:
  PORT: "4222"
processes:
  svc:
    command: "echo hi"
    readiness_probe:
      exec:
        command: "check --port ${PORT}"
      period_seconds: 5
    liveness_probe:
      exec:
        command: "alive --port $PORT"
      period_seconds: 10
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        apply_interpolation(&mut cfg);

        let svc = cfg.processes.get("svc").unwrap();
        assert_eq!(
            svc.readiness_probe
                .as_ref()
                .unwrap()
                .exec
                .as_ref()
                .unwrap()
                .command,
            "check --port 4222"
        );
        assert_eq!(
            svc.liveness_probe
                .as_ref()
                .unwrap()
                .exec
                .as_ref()
                .unwrap()
                .command,
            "alive --port 4222"
        );
    }

    #[test]
    fn apply_interpolation_disabled() {
        let yaml = r#"
disable_env_expansion: true
environment:
  VERSION: "1.0"
processes:
  api:
    command: "run --version ${VERSION}"
"#;
        let mut cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        apply_interpolation(&mut cfg);

        let api = cfg.processes.get("api").unwrap();
        assert_eq!(api.command, "run --version ${VERSION}");
    }

    #[test]
    fn resolve_config_paths_empty_uses_discovery() {
        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join("decompose.yaml"),
            "processes: {a: {command: 'echo'}}",
        )
        .unwrap();

        let paths = resolve_config_paths(&[], dir.path()).unwrap();
        assert_eq!(paths.len(), 1);
        assert!(paths[0].ends_with("decompose.yaml"));
    }

    #[test]
    fn resolve_config_paths_explicit() {
        let dir = tempdir().unwrap();
        let p = dir.path().join("custom.yaml");
        fs::write(&p, "processes: {a: {command: 'echo'}}").unwrap();

        let paths = resolve_config_paths(std::slice::from_ref(&p), dir.path()).unwrap();
        assert_eq!(paths.len(), 1);
    }

    #[test]
    fn exit_mode_deserialization() {
        let yaml = r#"
exit_mode: exit_on_failure
processes:
  a:
    command: "echo"
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        assert_eq!(cfg.exit_mode, ExitMode::ExitOnFailure);
    }

    #[test]
    fn config_hash_is_stable_and_sensitive() {
        // Baseline config with a broad mix of fields set.
        let yaml_a = r#"
processes:
  api:
    command: "run server"
    description: "the api"
    working_dir: "/srv"
    environment:
      PORT: "8080"
      LOG_LEVEL: "info"
    env_file:
      - "extra.env"
    ready_log_line: "listening"
    restart_policy: on_failure
    backoff_seconds: 5
    max_restarts: 3
    shutdown:
      signal: 15
      timeout_seconds: 10
      command: "cleanup.sh"
    readiness_probe:
      exec:
        command: "curl -f localhost"
      period_seconds: 5
      timeout_seconds: 1
    depends_on:
      db:
        condition: process_started
    replicas: 2
  db:
    command: "db"
"#;
        let cfg_a: ProjectConfig = serde_yaml_ng::from_str(yaml_a).unwrap();
        let api_a = cfg_a.processes.get("api").unwrap();
        let hash_a = compute_config_hash(api_a);

        // Identical config parsed independently must produce the same hash.
        let cfg_a2: ProjectConfig = serde_yaml_ng::from_str(yaml_a).unwrap();
        let hash_a2 = compute_config_hash(cfg_a2.processes.get("api").unwrap());
        assert_eq!(hash_a, hash_a2, "same config must hash the same");

        // Changing `command` must change the hash.
        let mut cfg_cmd = cfg_a.clone();
        cfg_cmd.processes.get_mut("api").unwrap().command = "run server --port 9000".to_string();
        let hash_cmd = compute_config_hash(cfg_cmd.processes.get("api").unwrap());
        assert_ne!(hash_a, hash_cmd, "command change must change hash");

        // Changing only `depends_on`, `replicas`, or `disabled` must NOT
        // change the hash — these are the Docker-Compose "mutable without
        // recreate" fields.
        let mut cfg_depends = cfg_a.clone();
        cfg_depends
            .processes
            .get_mut("api")
            .unwrap()
            .depends_on
            .clear();
        assert_eq!(
            hash_a,
            compute_config_hash(cfg_depends.processes.get("api").unwrap()),
            "depends_on change must NOT affect hash"
        );

        let mut cfg_replicas = cfg_a.clone();
        cfg_replicas.processes.get_mut("api").unwrap().replicas = 7;
        assert_eq!(
            hash_a,
            compute_config_hash(cfg_replicas.processes.get("api").unwrap()),
            "replicas change must NOT affect hash"
        );

        let mut cfg_disabled = cfg_a.clone();
        cfg_disabled.processes.get_mut("api").unwrap().disabled = true;
        assert_eq!(
            hash_a,
            compute_config_hash(cfg_disabled.processes.get("api").unwrap()),
            "disabled change must NOT affect hash"
        );

        // Changing environment (a non-excluded field) must change the hash.
        let mut cfg_env = cfg_a.clone();
        cfg_env
            .processes
            .get_mut("api")
            .unwrap()
            .environment
            .0
            .insert("NEW_VAR".to_string(), "x".to_string());
        assert_ne!(
            hash_a,
            compute_config_hash(cfg_env.processes.get("api").unwrap()),
            "environment change must change hash"
        );
    }

    #[test]
    fn build_instances_propagates_config_hash_to_replicas() {
        let yaml = r#"
processes:
  web:
    command: "serve"
    replicas: 3
"#;
        let cfg: ProjectConfig = serde_yaml_ng::from_str(yaml).unwrap();
        let expected = compute_config_hash(cfg.processes.get("web").unwrap());
        let out = build_process_instances(&cfg, Path::new("/tmp"), &BTreeMap::new());
        assert_eq!(out.len(), 3);
        for runtime in out.values() {
            assert_eq!(runtime.spec.config_hash, expected);
        }
    }
}