aegis-orchestrator 0.15.0-pre-alpha

100monkeys.ai AEGIS orchestrator CLI and daemon
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
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
// Copyright (c) 2026 100monkeys.ai
// SPDX-License-Identifier: AGPL-3.0
//! Daemon HTTP server implementation
//!
//! # Architecture
//!
//! - **Layer:** Interface / Presentation Layer
//! - **Purpose:** Implements internal responsibilities for server
//!
//! # Code Quality Principles
//!
//! - Keep HTTP handlers thin and delegate business logic to application services.
//! - Fail closed on auth, readiness, and runtime initialization boundaries.
//! - Avoid exposing partial protocol support through the public transport surface.

use anyhow::{Context, Result};
use std::sync::Arc;

const DEFAULT_ORCHESTRATOR_URL: &str = "http://localhost:8088";

// Type alias for repository tuple to avoid clippy "very complex type" lint
type RepositoryTuple = (
    Arc<dyn AgentRepository>,
    Arc<dyn aegis_orchestrator_core::domain::repository::WorkflowRepository>,
    Arc<dyn aegis_orchestrator_core::domain::repository::ExecutionRepository>,
    Arc<dyn aegis_orchestrator_core::domain::repository::WorkflowExecutionRepository>,
);
use sqlx::postgres::PgPool;
use std::path::PathBuf;
use tokio::net::TcpListener;

// Helper to establish a Temporal client connection with retry logic.
// Extracted to module scope for improved testability and separation of concerns.
async fn connect_temporal_with_retry(
    temporal_address: &str,
    temporal_namespace: &str,
    temporal_task_queue: &str,
    worker_http_endpoint: &str,
    max_retries: i32,
) -> Result<Arc<TemporalClient>> {
    let mut retries: i32 = 0;

    loop {
        match TemporalClient::new(
            temporal_address,
            temporal_namespace,
            temporal_task_queue,
            worker_http_endpoint,
        )
        .await
        {
            Ok(client) => {
                tracing::info!("Temporal Client connected successfully");
                return Ok(Arc::new(client));
            }
            Err(e) => {
                retries += 1;
                if retries >= max_retries {
                    return Err(e).with_context(|| {
                        format!(
                            "Failed to connect to Temporal at {temporal_address} after {retries} attempts"
                        )
                    });
                }

                if retries % 5 == 0 {
                    tracing::info!(
                        attempt = retries,
                        max_retries = max_retries,
                        "Still verifying Temporal connection"
                    );
                }
                tracing::debug!(error = %e, "Failed to connect to Temporal, retrying in 2s");
                tokio::time::sleep(std::time::Duration::from_secs(2)).await;
            }
        }
    }
}
use tokio::signal;
use tracing::{debug, error, info, warn};

use super::{remove_pid_file, write_pid_file};
use aegis_orchestrator_core::domain::rate_limit::{RateLimitEnforcer, RateLimitPolicyResolver};
use aegis_orchestrator_core::{
    application::{
        agent::AgentLifecycleService,
        execution::ExecutionService,
        execution::StandardExecutionService,
        lifecycle::StandardAgentLifecycleService,
        register_workflow::{RegisterWorkflowUseCase, StandardRegisterWorkflowUseCase},
        start_workflow_execution::StandardStartWorkflowExecutionUseCase,
        validation_service::ValidationService,
        CorrelatedActivityStreamService,
    },
    domain::{
        cluster::{
            ConfigLayerRepository, ConfigType, EffectiveConfigValidator, MergedConfig,
            NodeClusterRepository, NodeId, NodeRole,
        },
        iam::IdentityProvider,
        node_config::{resolve_env_value, IamConfig, IamRealmConfig, NodeConfigManifest},
        repository::AgentRepository,
        runtime_registry::StandardRuntimeRegistry,
        supervisor::Supervisor,
    },
    infrastructure::{
        event_bus::EventBus,
        iam::StandardIamService,
        llm::registry::ProviderRegistry,
        rate_limit::{
            CompositeRateLimitEnforcer, GovernorBurstEnforcer, HierarchicalPolicyResolver,
            PostgresWindowEnforcer,
        },
        repositories::{
            InMemoryAgentRepository, InMemoryExecutionRepository,
            InMemoryWorkflowExecutionRepository,
        },
        runtime::{connect_container_runtime, ContainerRuntime},
        temporal_client::TemporalClient,
        TemporalEventListener,
    },
};

use aegis_orchestrator_core::application::credential_service::{
    CredentialManagementService, StandardCredentialManagementService,
};
use aegis_orchestrator_core::domain::credential::CredentialBindingRepository;
use aegis_orchestrator_core::domain::security_context::SecurityContextRepository;
use aegis_orchestrator_core::infrastructure::repositories::PostgresCredentialBindingRepository;
use aegis_orchestrator_core::presentation::webhook_guard::EnvWebhookSecretProvider;
use aegis_orchestrator_swarm::infrastructure::StandardSwarmService;

use super::operator_read_models::OperatorReadModelStore;
use crate::daemon::container_helpers::cleanup_orphaned_agent_containers;
pub use crate::daemon::handlers::DEFAULT_MAX_EXECUTION_LIST_LIMIT;
use crate::daemon::ports::{DaemonAgentActivity, DaemonWorkflowExecutionControl};
use crate::daemon::router::create_router;
use crate::daemon::state::AppState;

// ---------------------------------------------------------------------------
// Port implementations moved to ports.rs
// DaemonWorkflowExecutionControl and DaemonAgentActivity are imported above.
// ---------------------------------------------------------------------------

fn default_local_host_mount_point() -> String {
    if let Ok(path) = std::env::var("AEGIS_LOCAL_HOST_MOUNT_POINT") {
        return path;
    }

    let default_path = PathBuf::from("/var/lib/aegis/local-host-volumes");
    default_path.to_string_lossy().into_owned()
}

fn resolve_generated_artifacts_root(config_path: Option<PathBuf>) -> PathBuf {
    let config_path = config_path.or_else(NodeConfigManifest::discover_config);
    crate::commands::builtins::resolve_generated_root(config_path.as_ref())
}

// tenant_id_from_identity moved to handlers/mod.rs

fn temporal_connection_max_retries(raw_value: Option<i32>) -> i32 {
    raw_value.unwrap_or(30).max(1)
}

// ClusterNodeView, ClusterStatusView moved to cluster_helpers.rs
// SwarmMessageView, SwarmLockView, SwarmView moved to handlers/swarms.rs
// DashboardSummaryView moved to handlers/observability.rs

// HealthView moved to handlers/health.rs
// LimitQuery, CortexQueryParams, bounded_limit moved to handlers/mod.rs

// managed_container_reap_reason and cleanup_orphaned_agent_containers moved to container_helpers.rs

// cluster_role_to_string, node_status_to_string, cluster_node_view, fallback_cluster_node,
// cluster_status_view, load_cluster_nodes moved to cluster_helpers.rs

pub async fn start_daemon(config_path: Option<PathBuf>, port: u16) -> Result<()> {
    // Daemonize on Unix
    // NOTE: We skip internal daemonization because calling fork() (via daemonize)
    // inside a Tokio runtime (#[tokio::main]) breaks the reactor.
    // The CLI 'daemon start' command already spawns this process as a detached background child.
    /*
    #[cfg(unix)]
    {
        daemonize_process()?;
    }
    */

    // Write PID file
    let pid = std::process::id();
    write_pid_file(pid)?;

    // Ensure PID file cleanup on exit
    let _guard = PidFileGuard;

    info!("AEGIS daemon starting (PID: {})", pid);
    // Load configuration
    info!("Loading configuration...");
    let mut config = NodeConfigManifest::load_or_default(config_path.clone())
        .context("Failed to load configuration")?;

    // Prefer the discovered config path so Docker deployments that set
    // AEGIS_CONFIG_PATH resolve generated artifacts under the mounted stack root.
    let generated_artifacts_root = resolve_generated_artifacts_root(config_path.clone());

    config
        .validate()
        .context("Configuration validation failed")?;

    // Initialize metrics if enabled (ADR-058 Step 2)
    if config
        .spec
        .observability
        .as_ref()
        .and_then(|o| o.metrics.as_ref())
        .map(|m| m.enabled)
        .unwrap_or(false)
    {
        let metrics_cfg = config
            .spec
            .observability
            .as_ref()
            .unwrap()
            .metrics
            .as_ref()
            .unwrap();
        let region = config.spec.node.region.as_deref();
        let version = env!("CARGO_PKG_VERSION");

        aegis_orchestrator_core::infrastructure::telemetry::init_metrics(
            metrics_cfg.port,
            &config.spec.node.id,
            &config.metadata.name,
            region,
            version,
        )
        .context("Failed to initialize metrics")?;
        info!(port = metrics_cfg.port, "Metrics exporter initialized");
    }

    if let Some(seal_gateway) = &config.spec.seal_gateway {
        let resolved_url =
            resolve_env_value(&seal_gateway.url).unwrap_or_else(|_| seal_gateway.url.clone());
        tracing::info!(
            "Configured SEAL tooling gateway URL from node config: {}",
            resolved_url
        );
    }

    if config.spec.llm_providers.is_empty() {
        warn!(
            "No LLM providers configured. Agents will fail to generate text. Please check your config file or ensure one is discovered."
        );
    }

    info!("Configuration loaded. Initializing services...");

    // Initialize repositories — resolve database URL from config (spec.database)
    let database_url: Option<String> =
        config
            .spec
            .database
            .as_ref()
            .and_then(|db| match resolve_env_value(&db.url) {
                Ok(url) => Some(url),
                Err(e) => {
                    tracing::warn!(
                        "Failed to resolve database URL: {}. Falling back to InMemory.",
                        e
                    );
                    None
                }
            });
    let db_max_connections: u32 = config
        .spec
        .database
        .as_ref()
        .map(|db| db.max_connections)
        .unwrap_or(5);

    // Store pool separately for later volume repo initialization
    let db_pool: Option<PgPool> = if let Some(url) = database_url.as_ref() {
        info!(url = %url, "Initializing repositories with PostgreSQL");
        match sqlx::postgres::PgPoolOptions::new()
            .max_connections(db_max_connections)
            .connect(url)
            .await
        {
            Ok(db_pool) => {
                info!("Connected to PostgreSQL");

                // Check migration status
                static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations");

                let total_known = MIGRATOR.iter().count();
                if total_known == 0 {
                    return Err(anyhow::anyhow!(
                        "CRITICAL: No migrations found in binary! Check build process."
                    ));
                }

                // Check applied migrations
                let applied_result = sqlx::query("SELECT version FROM _sqlx_migrations")
                    .fetch_all(&db_pool)
                    .await;

                let applied_count = match applied_result {
                    Ok(rows) => rows.len(),
                    Err(_) => 0,
                };

                info!(
                    applied = applied_count,
                    total = total_known,
                    "Database migration status"
                );

                if applied_count < total_known {
                    info!("Applying pending migrations...");
                    match MIGRATOR.run(&db_pool).await {
                        Ok(_) => info!("Database migrations applied successfully"),
                        Err(e) => {
                            return Err(anyhow::anyhow!("Failed to apply migrations: {e}"));
                        }
                    }
                } else {
                    info!("Database is up to date");
                }

                Some(db_pool)
            }
            Err(e) => {
                if config.is_production() {
                    return Err(anyhow::anyhow!(
                        "Failed to connect to PostgreSQL in production mode: {e}"
                    ));
                }

                error!(error = %e, "Failed to connect to PostgreSQL, falling back to InMemory");
                None
            }
        }
    } else {
        if config.is_production() {
            return Err(anyhow::anyhow!(
                "Production mode requires spec.database; refusing to fall back to InMemory repositories"
            ));
        }

        info!("No database configured (spec.database omitted), using InMemory repositories");
        None
    };

    let (agent_repo, workflow_repo, execution_repo, workflow_execution_repo): RepositoryTuple =
        if let Some(db_pool) = db_pool.as_ref() {
            (
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_agent::PostgresAgentRepository::new(db_pool.clone())),
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_workflow::PostgresWorkflowRepository::new_with_pool(db_pool.clone())),
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_execution::PostgresExecutionRepository::new(db_pool.clone())),
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_workflow_execution::PostgresWorkflowExecutionRepository::new(db_pool.clone())),
        )
        } else {
            (
                Arc::new(InMemoryAgentRepository::new()),
                Arc::new(
                    aegis_orchestrator_core::infrastructure::repositories::InMemoryWorkflowRepository::new(),
                ),
                Arc::new(InMemoryExecutionRepository::new()),
                Arc::new(InMemoryWorkflowExecutionRepository::new()),
            )
        };

    let cluster_repo: Option<Arc<dyn NodeClusterRepository>> = None;

    // ── ADR-060: Load effective config by merging database layers over bootstrap YAML ──
    if let Some(ref pool) = db_pool {
        use aegis_orchestrator_core::infrastructure::cluster::PgConfigLayerRepository;

        let config_repo: Arc<dyn ConfigLayerRepository> =
            Arc::new(PgConfigLayerRepository::new(Arc::new(pool.clone())));
        let node_id = NodeId(
            uuid::Uuid::parse_str(&config.spec.node.id).unwrap_or_else(|_| uuid::Uuid::new_v4()),
        );

        match config_repo
            .get_merged_config(&node_id, None, &ConfigType::AegisConfig)
            .await
        {
            Ok(merged) => {
                // An empty payload means no DB layers exist yet; the bootstrap YAML
                // is the sole source of truth and requires no validation here.
                let has_db_overlay = merged
                    .payload
                    .as_object()
                    .map(|o| !o.is_empty())
                    .unwrap_or(false);

                if has_db_overlay {
                    if let Err(e) = config.apply_merged_overlay(&merged) {
                        warn!(
                            error = %e,
                            "Failed to apply merged database config overlay, continuing with YAML-only config"
                        );
                    } else {
                        // Validate the final effective config (bootstrap YAML + DB overlay)
                        // after the overlay has been applied (ADR-060 §4, Gap 059-7).
                        let effective_payload = serde_json::to_value(&config.spec)
                            .unwrap_or(serde_json::Value::Object(serde_json::Map::new()));
                        let effective = MergedConfig {
                            payload: effective_payload,
                            version: merged.version.clone(),
                        };
                        if let Err(missing) = EffectiveConfigValidator::validate(&effective) {
                            return Err(anyhow::anyhow!(
                                "Effective configuration missing required fields: {:?}",
                                missing
                            ));
                        }
                        info!(
                            version = %merged.version,
                            "Applied merged database config overlay (ADR-060)"
                        );
                    }
                } else {
                    debug!("No database config layers found, using YAML-only config");
                }
            }
            Err(e) => {
                debug!(
                    error = %e,
                    "No database config layers found, using YAML-only config"
                );
            }
        }
    }

    let event_bus = Arc::new(EventBus::new(100));
    let operator_read_model = OperatorReadModelStore::spawn_collector(event_bus.clone());
    let swarm_service = Arc::new(StandardSwarmService::new());
    swarm_service.start_gc_task();
    let iam_service: Option<Arc<dyn IdentityProvider>> = config.spec.iam.as_ref().map(|iam| {
        let resolved_realms: Vec<IamRealmConfig> = iam
            .realms
            .iter()
            .map(|realm| IamRealmConfig {
                slug: resolve_env_value(&realm.slug).unwrap_or_else(|_| realm.slug.clone()),
                issuer_url: resolve_env_value(&realm.issuer_url)
                    .unwrap_or_else(|_| realm.issuer_url.clone()),
                jwks_uri: resolve_env_value(&realm.jwks_uri)
                    .unwrap_or_else(|_| realm.jwks_uri.clone()),
                audience: resolve_env_value(&realm.audience)
                    .unwrap_or_else(|_| realm.audience.clone()),
                kind: resolve_env_value(&realm.kind).unwrap_or_else(|_| realm.kind.clone()),
            })
            .collect();
        let resolved_iam = IamConfig {
            realms: resolved_realms,
            jwks_cache_ttl_seconds: iam.jwks_cache_ttl_seconds,
            claims: iam.claims.clone(),
            keycloak_admin: iam.keycloak_admin.clone(),
        };
        Arc::new(StandardIamService::new(&resolved_iam, event_bus.clone()))
            as Arc<dyn IdentityProvider>
    });

    if config.is_production() && config.spec.iam.is_none() {
        anyhow::bail!(
            "Production nodes require spec.iam to be configured. \
             See config-with-examples.yaml for the iam block schema."
        );
    }
    if config.is_production()
        && config
            .spec
            .iam
            .as_ref()
            .is_some_and(|iam| iam.realms.is_empty())
    {
        anyhow::bail!("Production nodes must configure at least one IAM realm");
    }
    if let Some(iam) = &config.spec.iam {
        for realm in &iam.realms {
            if realm.issuer_url.trim().is_empty() {
                anyhow::bail!("IAM realm '{}' has empty issuer_url", realm.slug);
            }
            if realm.jwks_uri.trim().is_empty() {
                anyhow::bail!("IAM realm '{}' has empty jwks_uri", realm.slug);
            }
        }
    }

    info!("Initializing LLM registry...");
    let llm_registry = Arc::new(
        ProviderRegistry::from_config(&config).context("Failed to initialize LLM providers")?,
    );

    info!("Initializing Docker runtime...");

    // Resolve the orchestrator URL, supporting `env:VAR_NAME` syntax and a shared default.
    fn resolve_orchestrator_url(config: &NodeConfigManifest) -> String {
        resolve_env_value(&config.spec.runtime.orchestrator_url).unwrap_or_else(|e| {
            tracing::warn!("Failed to resolve orchestrator URL: {}. Using default.", e);
            DEFAULT_ORCHESTRATOR_URL.to_string()
        })
    }

    // Resolve orchestrator URL (supports env:VAR_NAME syntax via resolve_env_value)
    let orchestrator_url = resolve_orchestrator_url(&config);

    // Resolve NFS server host (supports env:VAR_NAME syntax) - ADR-036
    // Note: The Docker daemon relies on this to mount volumes from the host environment.
    let nfs_server_host = config.spec.runtime.nfs_server_host.as_ref().and_then(|host| {
        match resolve_env_value(host) {
            Ok(resolved) if !resolved.is_empty() => Some(resolved),
            Ok(_) => None,
            Err(e) => {
                tracing::debug!("Failed to resolve NFS server host: {}. NFS mounts will default to '127.0.0.1' which works for native Linux/WSL2 deployments, but will fail with 'connection refused' in Docker Desktop unless set to 'host.docker.internal'.", e);
                None
            }
        }
    });

    // Resolve Docker network mode (supports env:VAR_NAME syntax)
    let network_mode = config
        .spec
        .runtime
        .container_network_mode
        .as_ref()
        .and_then(|nm| match resolve_env_value(nm) {
            Ok(resolved) if !resolved.is_empty() => Some(resolved),
            Ok(_) => None,
            Err(e) => {
                tracing::debug!(
                    "Failed to resolve Docker network mode: {}. Using no explicit Docker network.",
                    e
                );
                None
            }
        });

    // ─── FUSE FSAL Daemon (ADR-107) ─────────────────────────────────────────────
    // Create a shared FUSE daemon that will be injected into both ContainerRuntime
    // and ContainerStepRunner. Uses the same FSAL instance as the NFS gateway.
    let fuse_mount_prefix = config
        .spec
        .runtime
        .fuse_mount_prefix
        .clone()
        .unwrap_or_else(|| "/tmp/aegis-fuse-mounts".to_string());

    // Create mount prefix directory on startup
    if let Err(e) = std::fs::create_dir_all(&fuse_mount_prefix) {
        warn!(
            error = %e,
            path = %fuse_mount_prefix,
            "Failed to create FUSE mount prefix directory — FUSE transport will be unavailable"
        );
    }

    // The FUSE daemon is created after the NFS gateway (which owns the FSAL).
    // We store None here and set it after nfs_gateway is available.
    let fuse_daemon_placeholder: Option<
        Arc<aegis_orchestrator_core::infrastructure::fuse::daemon::FuseFsalDaemon>,
    > = None;

    let runtime = Arc::new(
        ContainerRuntime::new(aegis_orchestrator_core::infrastructure::runtime::ContainerRuntimeConfig {
            bootstrap_script: config.spec.runtime.bootstrap_script.clone(),
            socket_path: config.spec.runtime.container_socket_path.clone(),
            network_mode: network_mode.clone(),
            orchestrator_url,
            nfs_server_host: nfs_server_host.clone(),
            nfs_port: config.spec.runtime.nfs_port,
            nfs_mountport: config.spec.runtime.nfs_mountport,
            event_bus: event_bus.clone(),
            credential_resolver: Arc::new(
                aegis_orchestrator_core::infrastructure::image_manager::NodeConfigCredentialResolver::new(
                    config.spec.registry_credentials.clone(),
                ),
            ),
            fuse_daemon: fuse_daemon_placeholder.clone(),
            fuse_mount_prefix: fuse_mount_prefix.clone(),
            fuse_mount_client: None,
        })
        .context("Failed to initialize Docker runtime")?,
    );

    // Only healthcheck Docker if it's the configured isolation mode
    if config.spec.runtime.default_isolation == "docker" {
        runtime.healthcheck().await
            .context("Docker healthcheck failed. Docker isolation is configured but Docker daemon is not accessible.")?;
        info!("Docker runtime connected and healthy");
    } else {
        info!(
            isolation_mode = %config.spec.runtime.default_isolation,
            "Docker runtime initialized (healthcheck skipped)"
        );
    }

    let supervisor = Arc::new(
        Supervisor::new(runtime.clone()).with_execution_repository(execution_repo.clone()),
    );

    // Initialize volume service (with SeaweedFS or fallback to local)
    info!("Initializing volume service...");
    let storage_config = config
        .spec
        .storage
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Storage configuration not found in node config"))?;

    let filer_url = if storage_config.backend == "seaweedfs" {
        storage_config
            .seaweedfs
            .as_ref()
            .map(|s| s.filer_url.clone())
            .unwrap_or_else(|| "http://localhost:8888".to_string())
    } else {
        "http://localhost:8888".to_string() // Fallback even for local mode
    };

    // Reuse existing pool for volume repository (avoid redundant connection)
    let volume_repo: Arc<dyn aegis_orchestrator_core::domain::repository::VolumeRepository> =
        if let Some(db_pool) = db_pool.as_ref() {
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_volume::PostgresVolumeRepository::new(db_pool.clone()))
        } else {
            warn!("Volume persistence disabled (no database pool available)");
            return Err(anyhow::anyhow!(
                "Database connection required for volume management"
            ));
        };

    let storage_provider: Arc<dyn aegis_orchestrator_core::domain::storage::StorageProvider> =
        match storage_config.backend.as_str() {
            "seaweedfs" => {
                aegis_orchestrator_core::infrastructure::storage::create_storage_provider(
                    aegis_orchestrator_core::infrastructure::storage::StorageBackend::SeaweedFS {
                        filer_url: filer_url.clone(),
                    },
                )?
            }
            "local_host" => {
                let mount_point = storage_config
                    .local_host
                    .as_ref()
                    .map(|l| l.mount_point.clone())
                    .unwrap_or_else(default_local_host_mount_point);
                aegis_orchestrator_core::infrastructure::storage::create_storage_provider(
                    aegis_orchestrator_core::infrastructure::storage::StorageBackend::LocalHost {
                        mount_point,
                    },
                )?
            }
            "opendal" => {
                let opendal_config = storage_config.opendal.as_ref().cloned().unwrap_or_default();
                // Resolve env variables in config map
                let mut resolved_options = std::collections::HashMap::new();
                for (k, v) in opendal_config.options {
                    resolved_options.insert(k, resolve_env_value(&v).unwrap_or(v));
                }
                aegis_orchestrator_core::infrastructure::storage::create_storage_provider(
                    aegis_orchestrator_core::infrastructure::storage::StorageBackend::OpenDal {
                        provider: opendal_config.provider,
                        options: resolved_options,
                    },
                )?
            }
            other => return Err(anyhow::anyhow!("Unsupported storage backend: {other}")),
        };

    let volume_service = Arc::new(
        aegis_orchestrator_core::application::volume_manager::StandardVolumeService::new(
            volume_repo.clone(),
            storage_provider.clone(),
            event_bus.clone(),
            filer_url,
            storage_config.backend.clone(),
        )?,
    );

    info!(mode = %storage_config.backend, "Volume service initialized");

    // Spawn TTL cleanup background task for ephemeral volumes
    let volume_service_cleanup = volume_service.clone();
    tokio::spawn(async move {
        use aegis_orchestrator_core::application::volume_manager::VolumeService as _;
        let mut interval = tokio::time::interval(std::time::Duration::from_secs(300)); // 5 minutes
        loop {
            interval.tick().await;
            match volume_service_cleanup.cleanup_expired_volumes().await {
                Ok(count) => {
                    if count > 0 {
                        tracing::info!("Volume cleanup: {} expired volumes deleted", count);
                    }
                }
                Err(e) => {
                    tracing::error!("Volume cleanup failed: {}", e);
                }
            }
        }
    });
    info!("Volume cleanup background task spawned (interval: 5 minutes)");

    let agent_container_reaper_runtime = runtime.clone();
    let agent_container_reaper_execution_repo = execution_repo.clone();
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(std::time::Duration::from_secs(300));
        // First tick fires immediately — clean up any orphans left from a prior crash.
        let mut first_run = true;
        loop {
            interval.tick().await;
            if first_run {
                tracing::info!("Running startup orphan container cleanup");
                first_run = false;
            }
            match cleanup_orphaned_agent_containers(
                agent_container_reaper_runtime.clone(),
                agent_container_reaper_execution_repo.clone(),
            )
            .await
            {
                Ok(count) => {
                    if count > 0 {
                        tracing::info!(
                            "Agent container cleanup: {} orphaned container(s) deleted",
                            count
                        );
                    }
                }
                Err(e) => {
                    tracing::error!("Agent container cleanup failed: {}", e);
                }
            }
        }
    });
    info!("Agent container cleanup background task spawned (interval: 5 minutes)");

    // Initialize Storage Event Persister for audit trail (ADR-036)
    info!("Initializing Storage Event Persister...");
    let storage_event_repo: Arc<
        dyn aegis_orchestrator_core::domain::repository::StorageEventRepository,
    > = if let Some(db_pool) = db_pool.as_ref() {
        Arc::new(aegis_orchestrator_core::infrastructure::repositories::postgres_storage_event::PostgresStorageEventRepository::new(db_pool.clone()))
    } else {
        warn!("Storage event persistence disabled (no database pool available)");
        Arc::new(
                aegis_orchestrator_core::infrastructure::repositories::InMemoryStorageEventRepository::new(),
            )
    };

    let storage_event_persister = Arc::new(
        aegis_orchestrator_core::application::storage_event_persister::StorageEventPersister::new(
            storage_event_repo.clone(),
            event_bus.clone(),
        ),
    );

    // Start background task for event persistence
    let _persister_handle = storage_event_persister.start();
    info!("Storage Event Persister started (audit trail enabled)");

    // Initialize NFS Server Gateway (ADR-036)
    info!("Initializing NFS Server Gateway...");
    let nfs_bind_port = config
        .spec
        .storage
        .as_ref()
        .and_then(|s| s.nfs_port)
        .unwrap_or(2049);

    // Wrap EventBus in EventBusPublisher adapter for FSAL
    let event_publisher = Arc::new(
        aegis_orchestrator_core::application::nfs_gateway::EventBusPublisher::new(
            event_bus.clone(),
        ),
    );

    let nfs_gateway = Arc::new(
        aegis_orchestrator_core::application::nfs_gateway::NfsGatewayService::new(
            storage_provider.clone(),
            volume_repo.clone(),
            event_publisher.clone(),
            Some(nfs_bind_port),
        ),
    );

    // Start NFS server and await successful startup before continuing
    if let Err(e) = nfs_gateway.start_server().await {
        error!(error = %e, "NFS Server Gateway failed to start, this is a fatal error");
        // Allow shutdown of daemon via signal
        std::process::exit(1);
    }
    info!(port = nfs_bind_port, "NFS Server Gateway started");

    // ─── Create FUSE FSAL Daemon (ADR-107) ───────────────────────────────────
    // Shares the same FSAL instance as the NFS gateway. The FUSE daemon provides
    // an alternative volume transport using host-local FUSE mountpoints + bind
    // mounts, which works in rootless container runtimes.
    let fuse_daemon: Option<
        Arc<aegis_orchestrator_core::infrastructure::fuse::daemon::FuseFsalDaemon>,
    > = {
        let fsal = nfs_gateway.fsal().clone();
        let daemon =
            aegis_orchestrator_core::infrastructure::fuse::daemon::FuseFsalDaemon::new(fsal);
        info!(
            mount_prefix = %fuse_mount_prefix,
            "FUSE FSAL daemon initialized (ADR-107)"
        );
        Some(Arc::new(daemon))
    };

    // Initialize security context repository early — needed by StandardAgentLifecycleService (ADR-102).
    let security_context_repo: Arc<
        dyn aegis_orchestrator_core::domain::security_context::repository::SecurityContextRepository,
    > = {
        let repo = aegis_orchestrator_core::infrastructure::security_context::InMemorySecurityContextRepository::new();

        // Seed security contexts from aegis-config.yaml (ADR-071 §ZaruTier SecurityContext Definitions)
        if let Some(definitions) = &config.spec.security_contexts {
            for def in definitions {
                let capabilities = def
                    .capabilities
                    .iter()
                    .map(|cap| aegis_orchestrator_core::domain::security_context::Capability {
                        tool_pattern: cap.tool_pattern.clone(),
                        path_allowlist: cap.path_allowlist.as_ref().map(|paths| {
                            paths.iter().map(std::path::PathBuf::from).collect()
                        }),
                        command_allowlist: cap.command_allowlist.clone(),
                        subcommand_allowlist: None,
                        domain_allowlist: cap.domain_allowlist.clone(),
                        max_response_size: None,
                        rate_limit: None,
                        max_concurrent: None,
                    })
                    .collect();

                let context = aegis_orchestrator_core::domain::security_context::SecurityContext {
                    name: def.name.clone(),
                    description: def.description.clone(),
                    capabilities,
                    deny_list: def.deny_list.clone(),
                    metadata: aegis_orchestrator_core::domain::security_context::SecurityContextMetadata {
                        created_at: chrono::Utc::now(),
                        updated_at: chrono::Utc::now(),
                        version: 1,
                    },
                };

                repo.save(context).await?;
                info!("Loaded security context: {}", def.name);
            }
            info!("Loaded {} security contexts from config", definitions.len());
        }

        Arc::new(repo)
    };

    let agent_service = Arc::new(StandardAgentLifecycleService::new(
        agent_repo.clone(),
        event_bus.clone(),
        security_context_repo.clone(),
    ));

    // Load StandardRuntime registry (ADR-043 / ADR-060)
    // Try database-backed merged registry first, fall back to file-based.
    let registry_path = &config.spec.runtime.runtime_registry_path;
    let runtime_registry = {
        let merged_registry = if let Some(ref pool) = db_pool {
            use aegis_orchestrator_core::infrastructure::cluster::PgConfigLayerRepository;
            let config_repo: Arc<dyn ConfigLayerRepository> =
                Arc::new(PgConfigLayerRepository::new(Arc::new(pool.clone())));
            let node_id = NodeId(
                uuid::Uuid::parse_str(&config.spec.node.id)
                    .unwrap_or_else(|_| uuid::Uuid::new_v4()),
            );
            config_repo
                .get_merged_config(&node_id, None, &ConfigType::RuntimeRegistry)
                .await
                .ok()
        } else {
            None
        };

        if let Some(ref merged) = merged_registry {
            match StandardRuntimeRegistry::from_merged_config(merged) {
                Ok(registry) => {
                    info!("StandardRuntime registry loaded from merged config (ADR-060)");
                    Arc::new(registry)
                }
                Err(e) => {
                    debug!(error = %e, "Merged runtime registry unavailable, falling back to file");
                    Arc::new(
                        StandardRuntimeRegistry::from_file(registry_path).map_err(|e| {
                            anyhow::anyhow!(
                                "Failed to load StandardRuntime registry from '{}': {e}. \
                             Ensure runtime-registry.yaml exists at the configured path \
                             (spec.runtime.runtime_registry_path in aegis-config.yaml).",
                                registry_path
                            )
                        })?,
                    )
                }
            }
        } else {
            match StandardRuntimeRegistry::from_file(registry_path) {
                Ok(registry) => {
                    info!(path = %registry_path, "StandardRuntime registry loaded");
                    Arc::new(registry)
                }
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "Failed to load StandardRuntime registry from '{}': {e}. \
                         Ensure runtime-registry.yaml exists at the configured path \
                         (spec.runtime.runtime_registry_path in aegis-config.yaml).",
                        registry_path
                    ));
                }
            }
        }
    };

    // Execution service initialization deferred until after ToolRouter is created

    // ADR-036: Event-driven NFS volume deregistration (security requirement)
    // Listen for VolumeExpired and VolumeDeleted events and immediately remove
    // the volume from the NFS gateway registry so the path becomes inaccessible.
    // This prevents orphaned volume registrations from remaining accessible after
    // their corresponding storage has been cleaned up.
    {
        let nfs_deregister_gateway = nfs_gateway.clone();
        let mut nfs_deregister_sub = event_bus.subscribe();
        tokio::spawn(async move {
            loop {
                match nfs_deregister_sub.recv().await {
                    Ok(
                        aegis_orchestrator_core::infrastructure::event_bus::DomainEvent::Volume(
                            vol_event,
                        ),
                    ) => {
                        let volume_id = match &vol_event {
                            aegis_orchestrator_core::domain::events::VolumeEvent::VolumeExpired {
                                volume_id,
                                ..
                            } => Some(*volume_id),
                            aegis_orchestrator_core::domain::events::VolumeEvent::VolumeDeleted {
                                volume_id,
                                ..
                            } => Some(*volume_id),
                            _ => None,
                        };
                        if let Some(vid) = volume_id {
                            nfs_deregister_gateway.deregister_volume(vid);
                            tracing::info!(
                                "NFS: Deregistered volume {} from gateway (volume expired/deleted)",
                                vid
                            );
                        }
                    }
                    Ok(_) => {} // Ignore non-volume events
                    Err(
                        aegis_orchestrator_core::infrastructure::event_bus::EventBusError::Lagged(
                            n,
                        ),
                    ) => {
                        tracing::warn!(
                            "NFS deregistration listener lagged by {} events — some volume deregistrations may have been missed",
                            n
                        );
                    }
                    Err(
                        aegis_orchestrator_core::infrastructure::event_bus::EventBusError::Closed,
                    ) => {
                        tracing::info!(
                            "NFS deregistration listener: event bus closed, shutting down"
                        );
                        break;
                    }
                    Err(_) => {}
                }
            }
        });
        tracing::info!("NFS deregistration listener started (ADR-036)");
    }

    info!("Initializing workflow engine...");

    // Initialize Temporal Client — read from config (spec.temporal)
    let temporal_required = config.spec.temporal.is_some();
    let temporal_config = config.spec.temporal.clone().unwrap_or_default();
    let temporal_address =
        resolve_env_value(&temporal_config.address).unwrap_or_else(|_| "temporal:7233".to_string());
    let worker_http_endpoint = resolve_env_value(&temporal_config.worker_http_endpoint)
        .unwrap_or_else(|_| "http://localhost:3000".to_string());
    let temporal_namespace = temporal_config.namespace.clone();
    let temporal_task_queue = temporal_config.task_queue.clone();
    let temporal_connection_max_retries =
        temporal_connection_max_retries(temporal_config.max_connection_retries);
    info!(address = %temporal_address, "Initializing Temporal Client");

    // Create shared containers for the concrete Temporal client and the workflow engine port.
    let temporal_client_container: Arc<
        tokio::sync::RwLock<
            Option<Arc<aegis_orchestrator_core::infrastructure::temporal_client::TemporalClient>>,
        >,
    > = Arc::new(tokio::sync::RwLock::new(None));
    let workflow_engine_container: Arc<
        tokio::sync::RwLock<
            Option<Arc<dyn aegis_orchestrator_core::application::ports::WorkflowEnginePort>>,
        >,
    > = Arc::new(tokio::sync::RwLock::new(None));
    let temporal_client_container_clone = temporal_client_container.clone();
    let workflow_engine_container_clone = workflow_engine_container.clone();

    // Clone for async task
    let temporal_address_clone = temporal_address.clone();
    let worker_http_endpoint_clone = worker_http_endpoint.clone();

    if temporal_required {
        let client = connect_temporal_with_retry(
            &temporal_address_clone,
            &temporal_namespace,
            &temporal_task_queue,
            &worker_http_endpoint_clone,
            temporal_connection_max_retries,
        )
        .await?;

        let mut lock = temporal_client_container_clone.write().await;
        *lock = Some(client.clone());
        drop(lock);

        let mut workflow_lock = workflow_engine_container_clone.write().await;
        *workflow_lock = Some(
            client as Arc<dyn aegis_orchestrator_core::application::ports::WorkflowEnginePort>,
        );
    }

    // Rate Limiting Infrastructure (ADR-072)
    #[allow(clippy::type_complexity)]
    let (rate_limit_enforcer, rate_limit_resolver): (
        Option<Arc<dyn RateLimitEnforcer>>,
        Option<Arc<dyn RateLimitPolicyResolver>>,
    ) = if let Some(ref pool) = db_pool {
        let burst = Arc::new(GovernorBurstEnforcer::new());
        let postgres = Arc::new(PostgresWindowEnforcer::new(pool.clone()));
        let enforcer: Arc<dyn RateLimitEnforcer> = Arc::new(
            CompositeRateLimitEnforcer::new(burst, postgres).with_event_bus(event_bus.clone()),
        );
        let resolver: Arc<dyn RateLimitPolicyResolver> =
            Arc::new(HierarchicalPolicyResolver::new(pool.clone()));
        info!("Rate limiting enabled (ADR-072)");
        (Some(enforcer), Some(resolver))
    } else {
        info!("Rate limiting disabled (no database connection)");
        (None, None)
    };

    // Rate limit counter cleanup task (ADR-072)
    if let Some(ref pool) = db_pool {
        let cleanup_pool = pool.clone();
        tokio::spawn(async move {
            let enforcer = PostgresWindowEnforcer::new(cleanup_pool);
            let mut interval = tokio::time::interval(std::time::Duration::from_secs(3600)); // hourly
            loop {
                interval.tick().await;
                match enforcer.cleanup_expired_counters().await {
                    Ok(deleted) => {
                        if deleted > 0 {
                            tracing::info!(deleted, "rate limit counter cleanup completed");
                        }
                    }
                    Err(e) => {
                        tracing::warn!(error = %e, "rate limit counter cleanup failed");
                    }
                }
            }
        });
        info!("Rate limit counter cleanup background task spawned (interval: 1 hour)");
    }

    // Initialize SEAL / Tool Routing Services (now hoisted for ExecutionService dependency)
    info!("Initializing SEAL & Tool Routing services...");

    let seal_middleware = Arc::new(
        aegis_orchestrator_core::infrastructure::seal::middleware::SealMiddleware::with_rate_limiting(
            rate_limit_enforcer.clone(),
            rate_limit_resolver.clone(),
        ),
    );
    let tool_registry =
        Arc::new(aegis_orchestrator_core::infrastructure::tool_router::InMemoryToolRegistry::new());

    // Shared tool servers state
    let tool_servers = Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::<
        aegis_orchestrator_core::domain::mcp::ToolServerId,
        aegis_orchestrator_core::domain::mcp::ToolServer,
    >::new()));

    // Load configured servers from NodeConfig
    if let Some(mcp_configs) = &config.spec.mcp_servers {
        let mut servers_lock = tool_servers.write().await;
        for srv_cfg in mcp_configs {
            if srv_cfg.enabled {
                let tool_server =
                    aegis_orchestrator_core::domain::mcp::ToolServer::from_config(srv_cfg);

                // Prevent silent overwrites when multiple MCP servers share the same
                // logical identity. Use the configured name (stable identifier) rather
                // than the randomly generated ToolServer ID for duplicate detection.
                if servers_lock
                    .values()
                    .any(|existing| existing.name == srv_cfg.name)
                {
                    return Err(anyhow::anyhow!(
                        "Duplicate MCP server name '{}' detected in configuration. \
                         MCP server names must be unique.",
                        srv_cfg.name
                    ));
                }

                servers_lock.insert(tool_server.id, tool_server);
            }
        }
    }

    // Derive builtin dispatchers from the canonical tool router registry.
    // User-configured dispatchers from aegis-config.yaml take precedence.
    let mut builtin_dispatchers = config.spec.builtin_dispatchers.clone().unwrap_or_default();
    {
        let canonical =
            aegis_orchestrator_core::infrastructure::tool_router::ToolRouter::builtin_dispatchers();
        let existing_names: std::collections::HashSet<String> =
            builtin_dispatchers.iter().map(|d| d.name.clone()).collect();
        for dispatcher in canonical {
            if !existing_names.contains(&dispatcher.name) {
                builtin_dispatchers.push(dispatcher);
            }
        }
    }

    let tool_router = Arc::new(
        aegis_orchestrator_core::infrastructure::tool_router::ToolRouter::new(
            tool_registry.clone(),
            tool_servers.clone(),
            builtin_dispatchers,
        ),
    );

    // Build initial capabilities index
    tool_router.rebuild_index().await;

    // Connect to the standalone Cortex service if configured (ADR-042).
    // Absence of spec.cortex.grpc_url means memoryless mode — no error, no retry.
    let cortex_grpc_url: Option<String> = config
        .spec
        .cortex
        .as_ref()
        .and_then(|c| c.grpc_url.as_ref())
        .and_then(|url| resolve_env_value(url).ok());
    let cortex_api_key: Option<String> = config
        .spec
        .cortex
        .as_ref()
        .and_then(|c| c.api_key.as_ref())
        .and_then(|k| resolve_env_value(k).ok());
    let cortex_client: Option<
        std::sync::Arc<aegis_orchestrator_core::infrastructure::CortexGrpcClient>,
    > = match cortex_grpc_url {
        Some(url) => {
            match aegis_orchestrator_core::infrastructure::CortexGrpcClient::new(
                url.clone(),
                cortex_api_key,
            )
            .await
            {
                Ok(client) => {
                    tracing::info!(url = %url, "Connected to Cortex gRPC service");
                    Some(std::sync::Arc::new(client))
                }
                Err(e) => {
                    tracing::warn!(
                        url = %url,
                        error = %e,
                        "Failed to connect to Cortex gRPC service; running in memoryless mode"
                    );
                    None
                }
            }
        }
        None => {
            tracing::info!(
                "Cortex gRPC URL not configured (spec.cortex omitted) — Orchestrator running in memoryless mode"
            );
            None
        }
    };

    // ─── Discovery Service (ADR-075) ───────────────────────────────────────
    // Discovery is backed by the Cortex service when connected.
    let discovery_service: Option<
        Arc<dyn aegis_orchestrator_core::application::discovery_service::DiscoveryService>,
    > = cortex_client.as_ref().map(|cx| {
        let svc = Arc::new(
            aegis_orchestrator_core::application::discovery_service::CortexDiscoveryService::new(
                cx.clone(),
            ),
        );

        // Spawn event handler to keep Cortex discovery index in sync
        let handler = Arc::new(
            aegis_orchestrator_core::infrastructure::discovery::DiscoveryIndexEventHandler::new(
                cx.clone(),
                agent_repo.clone(),
                workflow_repo.clone(),
                event_bus.clone(),
            ),
        );
        handler.clone().spawn();

        // Backfill on startup
        let handler_bg = handler.clone();
        tokio::spawn(async move {
            match handler_bg.backfill().await {
                Ok((agents, workflows)) => {
                    tracing::info!(
                        agents_indexed = agents,
                        workflows_indexed = workflows,
                        "Cortex discovery index backfill complete"
                    );
                }
                Err(e) => {
                    tracing::warn!(error = %e, "Cortex discovery index backfill failed");
                }
            }
        });

        // Periodic reconciliation — catches index drift from event lag or transient Cortex failures
        handler
            .clone()
            .spawn_reconciler(std::time::Duration::from_secs(300));

        tracing::info!("Discovery service initialized (Cortex-backed)");
        svc as Arc<dyn aegis_orchestrator_core::application::discovery_service::DiscoveryService>
    });

    // Token Issuer — create early so it can be shared with both ExecutionService and AttestationService (ADR-088 §A8).
    // AEGIS_SEAL_PRIVATE_KEY must be set to a PEM-encoded RSA private key.
    let private_key_for_issuer = std::env::var("AEGIS_SEAL_PRIVATE_KEY").map_err(|_| {
        anyhow::anyhow!(
            "SEAL private key not configured: set AEGIS_SEAL_PRIVATE_KEY \
             (PEM-encoded RSA private key; see ADR-034/ADR-035)"
        )
    })?;
    let private_key_for_issuer = normalize_seal_private_key(&private_key_for_issuer);
    let token_issuer: Arc<
        dyn aegis_orchestrator_core::application::ports::SecurityTokenIssuerPort,
    > = Arc::new(
        aegis_orchestrator_core::infrastructure::seal::signature::SecurityTokenIssuer::new(
            &private_key_for_issuer,
            "aegis-orchestrator",
        )
        .map_err(|e| {
            anyhow::anyhow!(
                "Failed to initialize SEAL token issuer from AEGIS_SEAL_PRIVATE_KEY: {e}"
            )
        })?,
    );

    // SEAL gateway client for session pre-creation (ADR-088 §A8).
    let seal_gateway_client: Arc<
        dyn aegis_orchestrator_core::application::ports::SealGatewayClient,
    > = {
        let gateway_url = std::env::var("AEGIS_SEAL_GATEWAY_URL")
            .unwrap_or_else(|_| "http://localhost:8089".to_string());
        let operator_token = std::env::var("AEGIS_SEAL_OPERATOR_TOKEN").ok();
        Arc::new(
            aegis_orchestrator_core::infrastructure::seal::gateway_client::HttpSealGatewayClient::new(
                gateway_url,
                operator_token,
            ),
        )
    };

    // Finally initialize ExecutionService now that ToolRouter is ready
    let mut execution_service_builder = StandardExecutionService::new(
        agent_service.clone(),
        volume_service.clone(),
        supervisor,
        execution_repo.clone(),
        event_bus.clone(),
        Arc::new(config.clone()),
    )
    .with_nfs_gateway(nfs_gateway.clone())
    .with_runtime_registry(runtime_registry.clone())
    .with_tool_router(tool_router.clone());

    if let Some(c_client) = cortex_client.clone() {
        execution_service_builder = execution_service_builder.with_cortex_client(c_client);
    }

    if let (Some(ref enforcer), Some(ref resolver)) = (&rate_limit_enforcer, &rate_limit_resolver) {
        execution_service_builder =
            execution_service_builder.with_rate_limiting(enforcer.clone(), resolver.clone());
    }

    // Wire swarm cascade cancellation so parent execution cancel propagates to child swarms (BC-6).
    execution_service_builder = execution_service_builder
        .with_swarm_cancellation(swarm_service.clone()
            as Arc<dyn aegis_orchestrator_core::application::ports::SwarmCancellationPort>);

    // Wire SEAL session pre-creation so containers get credentials at birth (ADR-088 §A8).
    // Clone before consuming — attestation_service also needs a reference.
    let attestation_gateway_client = seal_gateway_client.clone();
    execution_service_builder = execution_service_builder
        .with_seal_session_precreation(seal_gateway_client, token_issuer.clone());

    let execution_service = Arc::new(execution_service_builder);
    // Wire the self-reference so judge agents can be spawned as child executions (ADR-016).
    execution_service.set_child_execution_service(execution_service.clone());

    let validation_service = Arc::new(ValidationService::new(
        event_bus.clone(),
        execution_service.clone(),
        agent_service.clone(),
    ));

    // Create human input service
    let human_input_service =
        Arc::new(aegis_orchestrator_core::infrastructure::HumanInputService::new());

    // Legacy WorkflowEngine removed as part of Temporal migration

    let temporal_event_listener = Arc::new(TemporalEventListener::new(
        event_bus.clone(),
        workflow_execution_repo.clone(),
    ));

    info!("Temporal event listener initialized");

    let register_workflow_use_case = Arc::new(StandardRegisterWorkflowUseCase::new(
        workflow_repo.clone(),
        workflow_engine_container.clone(),
        event_bus.clone(),
        agent_service.clone(),
    ));

    let start_workflow_execution_use_case = {
        let mut uc = StandardStartWorkflowExecutionUseCase::new(
            workflow_repo.clone(),
            workflow_execution_repo.clone(),
            workflow_engine_container.clone(),
            event_bus.clone(),
        );
        if let (Some(ref enforcer), Some(ref resolver)) =
            (&rate_limit_enforcer, &rate_limit_resolver)
        {
            uc = uc.with_rate_limiting(enforcer.clone(), resolver.clone());
        }
        Arc::new(uc)
    };

    // --- Initialize SEAL / Tool Routing Services ---
    info!("Configuring SEAL & Tool Routing repositories and services...");

    // Note: security_context_repo was initialized earlier (before agent_service) — see above.

    let seal_session_repo: Arc<
        dyn aegis_orchestrator_core::domain::seal_session_repository::SealSessionRepository,
    > = Arc::new(
        aegis_orchestrator_core::infrastructure::seal::session_repository::InMemorySealSessionRepository::new(),
    );

    // Application Services — token_issuer was created earlier (ADR-088 §A8) and shared with ExecutionService.
    let mut attestation_service_builder =
        aegis_orchestrator_core::application::attestation_service::AttestationServiceImpl::new(
            security_context_repo.clone(),
            seal_session_repo.clone(),
            token_issuer,
        )
        .with_gateway_client(attestation_gateway_client)
        .with_agent_manifest_tools(execution_service.clone(), agent_service.clone());

    match aegis_orchestrator_core::infrastructure::docker::BollardContainerVerifier::new() {
        Ok(v) => {
            attestation_service_builder =
                attestation_service_builder.with_container_verifier(std::sync::Arc::new(v));
        }
        Err(e) => {
            warn!("Docker socket unavailable; container identity verification disabled: {e}");
        }
    }

    let attestation_service: Arc<
        dyn aegis_orchestrator_core::infrastructure::seal::attestation::AttestationService,
    > = Arc::new(attestation_service_builder);

    // Secrets manager: initialize from `spec.secrets.backend`, otherwise use an in-memory store for local development/testing.
    let secrets_manager: Arc<aegis_orchestrator_core::infrastructure::secrets_manager::SecretsManager> =
        match config.spec.secrets.as_ref().and_then(|s| s.backend.as_ref()) {
            Some(secret_backend_config) => {
                match aegis_orchestrator_core::infrastructure::secrets_manager::SecretsManager::from_config(
                    secret_backend_config,
                    event_bus.clone(),
                ).await {
                    Ok(manager) => {
                        info!("OpenBao secrets manager initialized");
                        Arc::new(manager)
                    }
                    Err(e) => {
                        return Err(anyhow::anyhow!("Failed to initialize OpenBao secrets manager: {e}"));
                    }
                }
            }
            None => {
                warn!("No spec.secrets.backend configured; using in-memory secret store (development/testing only, not production-safe)");
                Arc::new(aegis_orchestrator_core::infrastructure::secrets_manager::SecretsManager::from_store(
                    Arc::new(aegis_orchestrator_core::infrastructure::secrets_manager::TestSecretStore::new()),
                    event_bus.clone(),
                ))
            }
        };

    // ─── Credential Management Service (BC-11, ADR-078) ────────────────────────
    let credential_service: Option<Arc<dyn CredentialManagementService>> =
        db_pool.as_ref().map(|pool| {
            let repo = Arc::new(PostgresCredentialBindingRepository::new(pool.clone()))
                as Arc<dyn CredentialBindingRepository>;
            Arc::new(StandardCredentialManagementService::new(
                repo,
                secrets_manager.clone(),
                event_bus.clone(),
            )) as Arc<dyn CredentialManagementService>
        });

    // ─── Container Step Runner (ADR-050) ──────────────────────────────────────
    // Dedicated Docker client + image manager + step runner for CI/CD container
    // steps executed by ContainerRun / ParallelContainerRun workflow states.
    // Delegates credential resolution to SecretsManager for secret-store paths and
    // to environment variables for env: paths.
    let docker_for_steps =
        connect_container_runtime(config.spec.runtime.container_socket_path.as_deref())
            .context("Failed to connect container runtime for ContainerStepRunner (ADR-050)")?;
    let step_credential_resolver: Arc<
        dyn aegis_orchestrator_core::infrastructure::image_manager::CredentialResolver,
    > = Arc::new(
        aegis_orchestrator_core::infrastructure::image_manager::NodeConfigCredentialResolver::new(
            config.spec.registry_credentials.clone(),
        ),
    );
    let step_image_manager: Arc<
        dyn aegis_orchestrator_core::infrastructure::image_manager::DockerImageManager,
    > = Arc::new(
        aegis_orchestrator_core::infrastructure::image_manager::StandardDockerImageManager::new(
            docker_for_steps.clone(),
            step_credential_resolver,
        ),
    );
    let container_step_runner: Arc<
        dyn aegis_orchestrator_core::domain::runtime::ContainerStepRunner,
    > = Arc::new(
        aegis_orchestrator_core::infrastructure::container_step_runner::ContainerStepRunnerImpl::new(
            docker_for_steps,
            step_image_manager,
            aegis_orchestrator_core::infrastructure::container_step_runner::ContainerStepRunnerConfig {
                nfs_server_host,
                nfs_port: config.spec.runtime.nfs_port,
                nfs_mountport: config.spec.runtime.nfs_mountport,
                network_mode: network_mode.clone(),
                fuse_daemon: fuse_daemon.clone(),
                fuse_mount_prefix: fuse_mount_prefix.clone(),
                fuse_mount_client: None,
            },
            event_bus.clone(),
            secrets_manager.clone(),
            Arc::new(nfs_gateway.volume_registry().clone()),
        ),
    );
    let run_container_step_use_case = Arc::new(
        aegis_orchestrator_core::application::run_container_step::RunContainerStepUseCase::new(
            container_step_runner,
        ),
    );
    info!("Container step runner initialized");

    let tool_manager = Arc::new(
        aegis_orchestrator_core::infrastructure::tool_router::ToolServerManager::new(
            tool_registry,
            tool_servers.clone(),
            event_bus.clone(),
            secrets_manager.clone(),
        ),
    );

    // Start MCP servers and spawn health check loop
    let tool_manager_clone = tool_manager.clone();
    tokio::spawn(async move {
        if let Err(e) = tool_manager_clone.start_all().await {
            tracing::error!("Failed to start some MCP servers: {}", e);
        }
        tool_manager_clone.health_check_loop().await;
    });

    let tool_catalog =
        Arc::new(aegis_orchestrator_core::application::tool_catalog::StandardToolCatalog::new());

    let mut tool_invocation_service_builder =
        aegis_orchestrator_core::application::tool_invocation_service::ToolInvocationService::new(
            seal_session_repo.clone(),
            security_context_repo.clone(),
            seal_middleware,
            tool_router.clone(),
            nfs_gateway.fsal().clone(),
            nfs_gateway.volume_registry().clone(),
            agent_service.clone(),
            execution_service.clone(),
            Arc::new(
                aegis_orchestrator_core::infrastructure::web_tools::ReqwestWebToolAdapter::new(),
            ),
            event_bus.clone(),
            config.spec.seal_gateway.as_ref().map(|gateway| {
                resolve_env_value(&gateway.url).unwrap_or_else(|_| gateway.url.clone())
            }),
        )
        .with_workflow_authoring(
            register_workflow_use_case.clone(),
            validation_service.clone(),
        )
        .with_workflow_repository(workflow_repo.clone())
        .with_workflow_execution_repo(workflow_execution_repo.clone())
        .with_workflow_execution(start_workflow_execution_use_case.clone())
        .with_generated_manifests_root(generated_artifacts_root.clone())
        .with_node_config_path(config_path.clone())
        .with_workflow_execution_control(Arc::new(DaemonWorkflowExecutionControl {
            config: config.clone(),
            temporal_client_container: temporal_client_container.clone(),
        }))
        .with_agent_activity(Arc::new(DaemonAgentActivity {
            execution_repo: execution_repo.clone(),
        }))
        .with_tool_catalog(tool_catalog.clone())
        .with_runtime_registry(runtime_registry.clone());

    // Wire discovery service into ToolInvocationService if available (ADR-075)
    if let Some(ref disc_svc) = discovery_service {
        tool_invocation_service_builder =
            tool_invocation_service_builder.with_discovery_service(disc_svc.clone());
    }

    let tool_invocation_service = Arc::new(tool_invocation_service_builder);

    info!(path = %generated_artifacts_root.display(), "Generated manifests will be written to configured path");

    // Initial tool catalog population + periodic refresh loop
    {
        let tis = tool_invocation_service.clone();
        let catalog = tool_catalog.clone();
        if let Ok(tools) = tis.get_available_tools().await {
            catalog.refresh_from(tools).await;
            tracing::info!("Tool catalog populated with initial tool set");
        }
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(std::time::Duration::from_secs(60));
            interval.tick().await; // skip immediate first tick (already populated above)
            loop {
                interval.tick().await;
                if let Ok(tools) = tis.get_available_tools().await {
                    catalog.refresh_from(tools).await;
                    tracing::debug!("Tool catalog refreshed");
                }
            }
        });
    }

    let inner_loop_service = {
        let mut ils =
            aegis_orchestrator_core::application::inner_loop_service::InnerLoopService::new(
                tool_invocation_service.clone(),
                execution_service.clone(),
                llm_registry,
            );
        if let (Some(ref enforcer), Some(ref resolver)) =
            (&rate_limit_enforcer, &rate_limit_resolver)
        {
            ils = ils.with_rate_limiting(enforcer.clone(), resolver.clone());
        }
        Arc::new(ils)
    };

    let workflow_scope_service = Arc::new(
        aegis_orchestrator_core::application::workflow_scope::WorkflowScopeService::new(
            workflow_repo.clone(),
            event_bus.clone(),
        ),
    );

    let agent_scope_service = Arc::new(
        aegis_orchestrator_core::application::agent_scope::AgentScopeService::new(
            agent_repo.clone(),
            event_bus.clone(),
        ),
    );

    // Tenant Provisioning Service (ADR-097)
    // Requires both a database pool (for TenantRepository) and Keycloak admin credentials.
    let tenant_provisioning_service: Option<
        Arc<aegis_orchestrator_core::application::tenant_provisioning::TenantProvisioningService>,
    > = {
        let tenant_repo: Option<Arc<dyn aegis_orchestrator_core::domain::repository::TenantRepository>> =
            db_pool.as_ref().map(|pool| {
                Arc::new(
                    aegis_orchestrator_core::infrastructure::repositories::postgres_tenant::PostgresTenantRepository::new(pool.clone()),
                ) as Arc<dyn aegis_orchestrator_core::domain::repository::TenantRepository>
            });

        let keycloak_admin_client = config
            .spec
            .iam
            .as_ref()
            .and_then(|iam| iam.keycloak_admin.as_ref())
            .map(|admin_cfg| {
                let host = resolve_env_value(&admin_cfg.host)
                    .unwrap_or_else(|_| admin_cfg.host.clone());
                let username = resolve_env_value(&admin_cfg.admin_username)
                    .unwrap_or_else(|_| admin_cfg.admin_username.clone());
                let password = resolve_env_value(&admin_cfg.admin_password)
                    .unwrap_or_else(|_| admin_cfg.admin_password.clone());
                Arc::new(
                    aegis_orchestrator_core::infrastructure::iam::keycloak_admin_client::KeycloakAdminClient::new(
                        aegis_orchestrator_core::infrastructure::iam::keycloak_admin_client::KeycloakAdminConfig {
                            host,
                            admin_username: username,
                            admin_password: password,
                        },
                    ),
                )
            });

        match (tenant_repo, keycloak_admin_client) {
            (Some(repo), Some(client)) => {
                info!("Tenant provisioning service initialized (ADR-097)");
                Some(Arc::new(
                    aegis_orchestrator_core::application::tenant_provisioning::TenantProvisioningService::new(
                        repo,
                        client,
                        event_bus.clone(),
                    ),
                ))
            }
            _ => {
                debug!(
                    "Tenant provisioning service disabled (requires database + keycloak_admin config)"
                );
                None
            }
        }
    };

    // Initialize user volume service and file operations service (Gap 079)
    let user_volume_service = Arc::new(
        aegis_orchestrator_core::application::user_volume_service::UserVolumeService::new(
            volume_repo.clone(),
            volume_service.clone(),
            event_bus.clone(),
            aegis_orchestrator_core::domain::volume::StorageTierLimits::default(),
        ),
    );

    let file_operations_service = Arc::new(
        aegis_orchestrator_core::application::file_operations_service::FileOperationsService::new(
            nfs_gateway.fsal().clone(),
        ),
    );

    let app_state = AppState {
        agent_service: agent_service.clone(),
        execution_service: execution_service.clone(),
        execution_repo: execution_repo.clone(),
        correlated_activity_stream_service: Arc::new(CorrelatedActivityStreamService::new(
            event_bus.clone(),
            execution_repo.clone(),
            Some(workflow_execution_repo.clone()),
        )),
        cluster_repo: cluster_repo.clone(),
        event_bus: event_bus.clone(),
        inner_loop_service: inner_loop_service.clone(),
        human_input_service: human_input_service.clone(),
        temporal_event_listener,
        register_workflow_use_case: register_workflow_use_case.clone(),
        start_workflow_execution_use_case,
        workflow_repo: workflow_repo.clone(),
        workflow_execution_repo: workflow_execution_repo.clone(),
        workflow_scope_service,
        agent_scope_service,
        temporal_client_container: temporal_client_container.clone(),
        storage_event_repo: storage_event_repo.clone(),
        tool_invocation_service: tool_invocation_service.clone(),
        attestation_service: attestation_service.clone(),
        swarm_service: swarm_service.clone(),
        operator_read_model: operator_read_model.clone(),
        cortex_client: cortex_client.clone(),
        rate_limit_override_repo: db_pool.as_ref().map(|pool| {
            Arc::new(aegis_orchestrator_core::infrastructure::rate_limit::RateLimitOverrideRepository::new(pool.clone()))
        }),
        api_key_repo: db_pool.as_ref().map(|pool| {
            Arc::new(aegis_orchestrator_core::infrastructure::repositories::PostgresApiKeyRepository::new(pool.clone()))
        }),
        iam_service: iam_service.clone(),
        tenant_provisioning_service,
        realm_repo: db_pool.as_ref().map(|pool| {
            Arc::new(
                aegis_orchestrator_core::infrastructure::repositories::PostgresRealmRepository::new(
                    pool.clone(),
                ),
            ) as Arc<dyn aegis_orchestrator_core::domain::iam::RealmRepository>
        }),
        credential_service,
        secrets_manager: secrets_manager.clone(),
        webhook_secret_provider: Arc::new(EnvWebhookSecretProvider),
        stimulus_service: None,
        user_volume_service,
        file_operations_service,
        config: config.clone(),
        start_time: std::time::Instant::now(),
    };

    info!("Building router...");
    // Build HTTP router
    let app = create_router(Arc::new(app_state), iam_service.clone());

    // Start HTTP server
    let bind_addr = if let Some(network) = &config.spec.network {
        network.bind_address.clone()
    } else {
        "0.0.0.0".to_string()
    };

    // Config port takes precedence over CLI default if we consider config the source of truth for the node.
    // However, start_daemon receives `port`.
    // Let's use the config port if network config is present, otherwise use the passed port.
    let final_port = if let Some(network) = &config.spec.network {
        network.port
    } else {
        port
    };

    // Start gRPC Server
    let grpc_port = if let Some(network) = &config.spec.network {
        network.grpc_port
    } else {
        50051
    };

    let grpc_addr_str = format!("{bind_addr}:{grpc_port}");
    let grpc_addr: std::net::SocketAddr = grpc_addr_str
        .parse()
        .with_context(|| format!("Failed to parse gRPC address: {grpc_addr_str}"))?;

    // Tool routing services moved above AppState!

    // Security contexts are loaded from aegis-config.yaml (spec.security_contexts)
    // during repository initialization above. See ADR-071 §ZaruTier SecurityContext Definitions.

    // Spawn gRPC server
    let exec_service_clone: Arc<dyn ExecutionService> = execution_service.clone();
    let val_service_clone = validation_service.clone();
    let agent_service_for_grpc: Arc<dyn AgentLifecycleService> = agent_service.clone();
    let volume_service_for_grpc: Arc<
        dyn aegis_orchestrator_core::application::volume_manager::VolumeService,
    > = volume_service.clone();
    let output_handler_service: Arc<
        dyn aegis_orchestrator_core::application::output_handler_service::OutputHandlerService,
    > = Arc::new(
        aegis_orchestrator_core::application::output_handler_service::StandardOutputHandlerService::new(
            execution_service.clone(),
            agent_service.clone(),
            event_bus.clone(),
        ),
    );
    let grpc_auth = match (&iam_service, config.spec.grpc_auth.clone()) {
        (Some(iam), Some(grpc_auth)) if grpc_auth.enabled => Some(
            aegis_orchestrator_core::presentation::grpc::auth_interceptor::GrpcIamAuthInterceptor::new(
                iam.clone(),
                &grpc_auth,
            ),
        ),
        (Some(iam), None) => Some(
            aegis_orchestrator_core::presentation::grpc::auth_interceptor::GrpcIamAuthInterceptor::new(
                iam.clone(),
                &aegis_orchestrator_core::domain::node_config::GrpcAuthConfig {
                    enabled: true,
                    exempt_methods: vec![],
                },
            ),
        ),
        _ => None,
    };

    tokio::spawn(async move {
        tracing::info!(address = %grpc_addr, "Starting gRPC server");
        if let Err(e) = aegis_orchestrator_core::presentation::grpc::server::start_grpc_server(
            aegis_orchestrator_core::presentation::grpc::server::GrpcServerConfig {
                addr: grpc_addr,
                execution_service: exec_service_clone,
                validation_service: val_service_clone,
                grpc_auth,
                attestation_service: Some(attestation_service),
                tool_invocation_service: Some(tool_invocation_service),
                cortex_client,
                run_container_step_use_case: Some(run_container_step_use_case),
                agent_service: Some(agent_service_for_grpc),
                // GrpcServerConfig now accepts an optional StimulusService, making
                // stimulus routing configurable per callsite. In the CLI daemon it remains
                // disabled (None) because StandardStimulusService requires a WorkflowRegistry
                // (the routing table for stimulus-to-workflow dispatch) that is not set up
                // here. To enable ingest_stimulus in the daemon, construct a WorkflowRegistry,
                // build a StandardStimulusService, and pass it as Some(...).
                stimulus_service: None,
                discovery_service: discovery_service.clone(),
                volume_service: Some(volume_service_for_grpc),
                output_handler_service: Some(output_handler_service),
            },
        )
        .await
        {
            tracing::error!(error = %e, "gRPC server failed");
        }
    });

    // ─── Cluster gRPC server (ADR-059) ─────────────────────────────────────
    // When clustering is enabled, start a second gRPC server on the dedicated
    // cluster port (default 50056) that exposes the inter-node
    // NodeClusterService (attestation, heartbeat, execution routing, etc.).
    if config.spec.cluster.as_ref().is_some_and(|c| c.enabled) {
        let cluster_grpc_port = config
            .spec
            .cluster
            .as_ref()
            .map(|c| c.cluster_grpc_port)
            .unwrap_or(50056);

        let cluster_addr_str = format!("{bind_addr}:{cluster_grpc_port}");
        let cluster_addr: std::net::SocketAddr = cluster_addr_str
            .parse()
            .with_context(|| format!("Failed to parse cluster gRPC address: {cluster_addr_str}"))?;

        let controller_node_id = {
            let id_str = &config.spec.node.id;
            aegis_orchestrator_core::domain::cluster::NodeId(
                uuid::Uuid::parse_str(id_str).unwrap_or_else(|_| uuid::Uuid::new_v4()),
            )
        };

        // The cluster repositories require a PostgreSQL pool. When a pool is
        // available we construct production Pg-backed repos; otherwise we log a
        // warning and skip – clustering without persistence is unsupported.
        if let Some(ref pool) = db_pool {
            use aegis_orchestrator_core::infrastructure::cluster::{
                NodeClusterServiceHandler, PgConfigLayerRepository, PgNodeChallengeRepository,
                PgNodeClusterRepository, PgNodeRegistryRepository, RoundRobinNodeRouter,
            };
            use aegis_orchestrator_core::infrastructure::aegis_cluster_proto::node_cluster_service_server::NodeClusterServiceServer;

            let cluster_repo: Arc<dyn NodeClusterRepository> =
                Arc::new(PgNodeClusterRepository::new(pool.clone()));
            let challenge_repo: Arc<
                dyn aegis_orchestrator_core::domain::cluster::NodeChallengeRepository,
            > = Arc::new(PgNodeChallengeRepository::new(pool.clone()));

            let secret_store = secrets_manager.secret_store();

            let attest_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::AttestNodeUseCase::new(
                    challenge_repo.clone(),
                ),
            );
            let challenge_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::ChallengeNodeUseCase::new(
                    challenge_repo.clone(),
                    cluster_repo.clone(),
                    secret_store,
                ),
            );
            let registry_repo: Arc<
                dyn aegis_orchestrator_core::domain::cluster::NodeRegistryRepository,
            > = Arc::new(PgNodeRegistryRepository::new(pool.clone()));
            let register_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::RegisterNodeUseCase::new(
                    cluster_repo.clone(),
                    registry_repo.clone(),
                    controller_node_id,
                ),
            );
            let heartbeat_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::HeartbeatUseCase::new(
                    cluster_repo.clone(),
                ),
            );
            let router: Arc<dyn aegis_orchestrator_core::domain::cluster::NodeRouter> =
                Arc::new(RoundRobinNodeRouter::new());
            let route_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::RouteExecutionUseCase::new(
                    cluster_repo.clone(),
                    router,
                    controller_node_id,
                ),
            );
            let forward_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::ForwardExecutionUseCase::new(
                    execution_service.clone(),
                ),
            );

            let config_layer_repo: Arc<
                dyn aegis_orchestrator_core::domain::cluster::ConfigLayerRepository,
            > = Arc::new(PgConfigLayerRepository::new(Arc::new(pool.clone())));

            let sync_config_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::SyncConfigUseCase::new(
                    config_layer_repo.clone(),
                    cluster_repo.clone(),
                ),
            );
            let push_config_uc = Arc::new(
                aegis_orchestrator_core::application::cluster::PushConfigUseCase::new(
                    config_layer_repo,
                ),
            );

            let handler = NodeClusterServiceHandler::new(
                attest_uc,
                challenge_uc,
                register_uc,
                heartbeat_uc,
                route_uc,
                forward_uc,
                sync_config_uc,
                push_config_uc,
                cluster_repo.clone(),
            );

            // Remote storage gRPC handler (ADR-064)
            // Route through AegisFSAL for path sanitization, volume authorization,
            // quota enforcement, and audit trail with cross-node provenance.
            use aegis_orchestrator_core::infrastructure::aegis_remote_storage_proto::remote_storage_service_server::RemoteStorageServiceServer;
            use aegis_orchestrator_core::infrastructure::storage::RemoteStorageServiceHandler;

            let health_sweeper_repo = cluster_repo.clone();

            let remote_fsal = {
                use aegis_orchestrator_core::application::storage_router::StorageRouter;
                use aegis_orchestrator_core::infrastructure::storage::{
                    LocalHostStorageProvider, SealStorageProvider,
                };
                use parking_lot::RwLock;
                use std::collections::HashMap;

                let primary_local_path = std::env::temp_dir().join("aegis");
                let fallback_local_path = std::env::temp_dir();
                let local_provider = Arc::new(
                    LocalHostStorageProvider::new(&primary_local_path).unwrap_or_else(|_| {
                        LocalHostStorageProvider::new(&fallback_local_path).expect(
                            "failed to initialize fallback local storage provider in temp directory",
                        )
                    }),
                );
                let seal_provider = Arc::new(SealStorageProvider::new());
                let storage_router = Arc::new(StorageRouter::new(
                    storage_provider.clone(),
                    local_provider,
                    seal_provider,
                ));
                let borrowed_volumes = Arc::new(RwLock::new(HashMap::new()));

                Arc::new(aegis_orchestrator_core::domain::fsal::AegisFSAL::new(
                    storage_router,
                    volume_repo.clone(),
                    borrowed_volumes,
                    event_publisher.clone(),
                ))
            };

            let storage_handler =
                RemoteStorageServiceHandler::new(remote_fsal, cluster_repo, controller_node_id);

            tokio::spawn(async move {
                tracing::info!(address = %cluster_addr, "Starting cluster gRPC server on port {cluster_grpc_port}");
                if let Err(e) = tonic::transport::Server::builder()
                    .add_service(NodeClusterServiceServer::new(handler))
                    .add_service(RemoteStorageServiceServer::new(storage_handler))
                    .serve(cluster_addr)
                    .await
                {
                    tracing::error!(error = %e, "Cluster gRPC server failed");
                }
            });

            // ADR-062: Spawn health sweeper for stale heartbeat detection
            {
                let stale_threshold = std::time::Duration::from_secs(
                    config
                        .spec
                        .cluster
                        .as_ref()
                        .and_then(|c| c.stale_threshold_secs)
                        .unwrap_or(90),
                );
                let sweep_interval = std::time::Duration::from_secs(
                    config
                        .spec
                        .cluster
                        .as_ref()
                        .and_then(|c| c.sweep_interval_secs)
                        .unwrap_or(30),
                );
                let sweeper = aegis_orchestrator_core::application::cluster::HealthSweeper::new(
                    health_sweeper_repo,
                    event_bus.clone(),
                    stale_threshold,
                    sweep_interval,
                );
                let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
                tokio::spawn(async move {
                    sweeper.run(shutdown_rx).await;
                });
                // Deliberately forget the sender so the channel stays open for the
                // lifetime of the process; the sweeper loop exits when the process
                // terminates.
                std::mem::forget(shutdown_tx);
                tracing::info!(
                    stale_threshold_secs = stale_threshold.as_secs(),
                    sweep_interval_secs = sweep_interval.as_secs(),
                    "ADR-062: Health sweeper started"
                );
            }
        } else {
            tracing::warn!(
                "Cluster mode enabled but no database configured; \
                 skipping cluster gRPC server (PostgreSQL is required for cluster state)"
            );
        }
    }

    // ─── Worker lifecycle task (ADR-059) ─────────────────────────────────────
    // When the node's cluster role is Worker or Hybrid AND a controller
    // endpoint is configured, spawn a background task that performs
    // attestation, registration, heartbeat loop, and graceful deregistration.
    //
    // The sender is kept alive in this binding for the duration of `start_daemon`
    // (i.e., until the HTTP server exits). Dropping it signals the lifecycle task
    // to execute its graceful deregistration step.
    let mut _worker_lifecycle_shutdown: Option<tokio::sync::watch::Sender<bool>> = None;
    if let Some(ref cluster_config) = config.spec.cluster {
        if cluster_config.enabled {
            let role = &cluster_config.role;
            if matches!(role, NodeRole::Worker | NodeRole::Hybrid) {
                if let Some(ref controller) = cluster_config.controller {
                    let worker_node_id = {
                        let id_str = &config.spec.node.id;
                        aegis_orchestrator_core::domain::cluster::NodeId(
                            uuid::Uuid::parse_str(id_str).unwrap_or_else(|_| uuid::Uuid::new_v4()),
                        )
                    };

                    // Load Ed25519 signing key from the configured keypair path
                    let keypair_path = &cluster_config.node_keypair_path;
                    match tokio::fs::read(keypair_path).await {
                        Ok(key_bytes) => {
                            // Node keypair is stored as raw 32-byte Ed25519 seed
                            // (written by `aegis node init` via `SigningKey::to_bytes()`).
                            let key_result: Result<ed25519_dalek::SigningKey, anyhow::Error> =
                                if key_bytes.len() == 32 {
                                    let seed: [u8; 32] = key_bytes[..32].try_into().unwrap();
                                    Ok(ed25519_dalek::SigningKey::from_bytes(&seed))
                                } else {
                                    Err(anyhow::anyhow!(
                                        "Expected 32-byte Ed25519 seed, got {} bytes",
                                        key_bytes.len()
                                    ))
                                };
                            match key_result {
                                Ok(signing_key) => {
                                    let signing_key = std::sync::Arc::new(signing_key);

                                    let proto_role = match role {
                                        NodeRole::Controller => {
                                            aegis_orchestrator_core::infrastructure::aegis_cluster_proto::NodeRole::Controller
                                                as i32
                                        }
                                        NodeRole::Worker => {
                                            aegis_orchestrator_core::infrastructure::aegis_cluster_proto::NodeRole::Worker
                                                as i32
                                        }
                                        NodeRole::Hybrid => {
                                            aegis_orchestrator_core::infrastructure::aegis_cluster_proto::NodeRole::Hybrid
                                                as i32
                                        }
                                    };

                                    let capabilities =
                                        aegis_orchestrator_core::infrastructure::aegis_cluster_proto::NodeCapabilities {
                                            gpu_count: config
                                                .spec
                                                .node
                                                .resources
                                                .as_ref()
                                                .map(|r| r.gpu_count)
                                                .unwrap_or(0),
                                            vram_gb: config
                                                .spec
                                                .node
                                                .resources
                                                .as_ref()
                                                .map(|r| r.vram_gb)
                                                .unwrap_or(0),
                                            cpu_cores: config
                                                .spec
                                                .node
                                                .resources
                                                .as_ref()
                                                .map(|r| r.cpu_cores)
                                                .unwrap_or(0),
                                            available_memory_gb: config
                                                .spec
                                                .node
                                                .resources
                                                .as_ref()
                                                .map(|r| r.memory_gb)
                                                .unwrap_or(0),
                                            supported_runtimes: vec!["docker".to_string()],
                                            tags: config.spec.node.tags.clone(),
                                        };

                                    let grpc_address = format!(
                                        "{}:{}",
                                        bind_addr,
                                        config
                                            .spec
                                            .network
                                            .as_ref()
                                            .map(|n| n.grpc_port)
                                            .unwrap_or(50051)
                                    );

                                    let heartbeat_interval = std::time::Duration::from_secs(
                                        cluster_config.heartbeat_interval_secs,
                                    );
                                    let token_refresh_margin = std::time::Duration::from_secs(
                                        cluster_config.token_refresh_margin_secs,
                                    );

                                    let client =
                                        aegis_orchestrator_core::infrastructure::cluster::NodeClusterClient::new(
                                            controller.endpoint.clone(),
                                            signing_key.clone(),
                                            worker_node_id,
                                        );

                                    let lifecycle = super::worker_lifecycle::WorkerLifecycle::new(
                                        client,
                                        worker_node_id,
                                        proto_role,
                                        capabilities,
                                        grpc_address,
                                        heartbeat_interval,
                                        token_refresh_margin,
                                        signing_key,
                                    );

                                    let (shutdown_tx, shutdown_rx) =
                                        tokio::sync::watch::channel(false);

                                    tokio::spawn(async move {
                                        tracing::info!("Starting worker lifecycle background task");
                                        if let Err(e) = lifecycle.run(shutdown_rx).await {
                                            tracing::error!(
                                                error = %e,
                                                "Worker lifecycle task failed"
                                            );
                                        }
                                    });

                                    // Keep the sender alive until the daemon exits.
                                    // Dropping it will signal the lifecycle task to
                                    // execute its graceful deregistration step.
                                    _worker_lifecycle_shutdown = Some(shutdown_tx);
                                }
                                Err(e) => {
                                    tracing::warn!(
                                        error = %e,
                                        path = %keypair_path.display(),
                                        "Failed to load node keypair; skipping worker lifecycle"
                                    );
                                }
                            }
                        }
                        Err(e) => {
                            tracing::warn!(
                                error = %e,
                                path = %keypair_path.display(),
                                "Failed to read node keypair file; skipping worker lifecycle"
                            );
                        }
                    }
                } else {
                    tracing::info!(
                        "Worker/Hybrid role but no controller endpoint configured; \
                         skipping worker lifecycle (standalone mode)"
                    );
                }
            }
        }
    }

    // --- Deploy vendored built-in agents and workflows ---
    if config.spec.deploy_builtins {
        use crate::commands::builtins;

        let force_builtins = config
            .spec
            .force_deploy_builtins
            .as_deref()
            .map(|v| resolve_env_value(v).unwrap_or_default())
            .map(|v| v.eq_ignore_ascii_case("true"))
            .unwrap_or(false);

        for (name, yaml) in builtins::BUILTIN_AGENTS {
            match serde_yaml::from_str::<aegis_orchestrator_sdk::AgentManifest>(yaml) {
                Ok(manifest) => {
                    let already_exists = agent_service
                        .lookup_agent(&manifest.metadata.name)
                        .await
                        .ok()
                        .flatten()
                        .is_some();
                    if already_exists && !force_builtins {
                        info!("Built-in agent '{}' already registered, skipping", name);
                    } else {
                        match agent_service
                            .deploy_agent_for_tenant(
                                &aegis_orchestrator_core::domain::tenant::TenantId::system(),
                                manifest,
                                force_builtins,
                                aegis_orchestrator_core::domain::agent::AgentScope::Global,
                                None,
                            )
                            .await
                        {
                            Ok(id) => info!("Deployed built-in agent '{}' (id: {})", name, id),
                            Err(e) => warn!("Failed to deploy built-in agent '{}': {}", name, e),
                        }
                    }
                }
                Err(e) => warn!("Failed to parse built-in agent template '{}': {}", name, e),
            }
        }

        for (wf_name, wf_template) in builtins::BUILTIN_WORKFLOWS {
            let system_tenant = aegis_orchestrator_core::domain::tenant::TenantId::system();
            let already_exists = workflow_repo
                .find_by_name_for_tenant(&system_tenant, wf_name)
                .await
                .ok()
                .flatten()
                .is_some();
            if already_exists && !force_builtins {
                info!(
                    "Built-in workflow '{}' already registered, skipping",
                    wf_name
                );
            } else {
                match register_workflow_use_case
                    .register_workflow_for_tenant(
                        &system_tenant,
                        wf_template,
                        force_builtins,
                        aegis_orchestrator_core::domain::workflow::WorkflowScope::Global,
                    )
                    .await
                {
                    Ok(_) => info!("Deployed built-in workflow '{}'", wf_name),
                    Err(e) => warn!("Failed to deploy built-in workflow '{}': {:#}", wf_name, e),
                }
            }
        }

        info!("Built-in template deployment complete");
    } else {
        info!("Built-in template deployment disabled (spec.deploy_builtins = false)");
    }

    let addr = format!("{bind_addr}:{final_port}");
    debug!(address = %addr, "Binding to address");
    let listener = TcpListener::bind(&addr)
        .await
        .with_context(|| format!("Failed to bind to {addr}"))?;

    info!(address = %addr, "Daemon listening");

    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal())
        .await
        .context("HTTP server failed")?;

    info!("Daemon shutting down");

    Ok(())
}

/// Support `.env` single-line PEM values where newlines are escaped as `\n`.
fn normalize_seal_private_key(raw: &str) -> String {
    if raw.contains("\\n") && !raw.contains('\n') {
        raw.replace("\\n", "\n")
    } else {
        raw.to_string()
    }
}

struct PidFileGuard;

impl Drop for PidFileGuard {
    fn drop(&mut self) {
        let _ = remove_pid_file();
    }
}

async fn shutdown_signal() {
    let ctrl_c = async {
        if let Err(e) = signal::ctrl_c().await {
            warn!("Ctrl+C handler error: {}", e);
        }
    };

    #[cfg(unix)]
    let terminate = async {
        match signal::unix::signal(signal::unix::SignalKind::terminate()) {
            Ok(mut sigterm) => {
                sigterm.recv().await;
            }
            Err(e) => {
                warn!("SIGTERM handler error: {}", e);
                std::future::pending::<()>().await;
            }
        }
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {
            info!("Received Ctrl+C signal");
        },
        _ = terminate => {
            info!("Received SIGTERM signal");
        },
    }
}

// Temporal Workflow HTTP Handlers moved to handlers/workflows.rs
// temporal_events_handler moved to handlers/dispatch.rs

// ---------------------------------------------------------------------------
// Cortex REST handlers
// ---------------------------------------------------------------------------
// Moved to handlers/cortex.rs

// create_router moved to router.rs

// Application state is defined in state.rs and re-exported via AppState import above.

// health_handler, readiness_handler moved to handlers/health.rs
// cluster_status_handler, cluster_nodes_handler moved to handlers/cluster.rs
// list_swarms_handler, get_swarm_handler moved to handlers/swarms.rs
// list_stimuli_handler, get_stimulus_handler, list_security_incidents_handler,
// list_storage_violations_handler, dashboard_summary_handler moved to handlers/observability.rs
// deploy_agent_handler, execute_agent_handler, stream_agent_events_handler,
// list_agents_handler, delete_agent_handler, get_agent_handler, lookup_agent_handler moved to handlers/agents.rs

// dispatch_gateway_handler moved to handlers/dispatch.rs

// ========================================
// Workflow API Handlers
// ========================================
// Moved to handlers/workflows.rs and handlers/workflow_executions.rs

// ============================================================================
// Human Approval Handlers
// ============================================================================
// Moved to handlers/approvals.rs

// SEAL handlers moved to handlers/seal.rs

// ============================================================================
// Admin Rate-Limit Override Handlers (ADR-072)
// ============================================================================
// Moved to handlers/admin.rs

#[cfg(test)]
mod tests {
    use super::{resolve_generated_artifacts_root, temporal_connection_max_retries};
    use crate::daemon::container_helpers::managed_container_reap_reason;
    use aegis_orchestrator_core::domain::execution::ExecutionStatus;
    use aegis_orchestrator_core::infrastructure::runtime::ManagedAgentContainer;
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_dir() -> PathBuf {
        let mut dir = std::env::temp_dir();
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system clock before unix epoch")
            .as_nanos();
        dir.push(format!("aegis-server-test-{}-{nanos}", std::process::id()));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    #[test]
    fn test_create_router_returns_router() {
        // This is a smoke test to ensure create_router compiles and can be called
        // We can't easily test the full router without a complex setup
        // but we can at least verify the function signature works

        let assertion_marker = "router_module_compiles";
        assert_eq!(assertion_marker, "router_module_compiles");
    }

    #[test]
    fn temporal_connection_max_retries_clamps_to_minimum_of_one() {
        assert_eq!(temporal_connection_max_retries(None), 30);
        assert_eq!(temporal_connection_max_retries(Some(0)), 1);
        assert_eq!(temporal_connection_max_retries(Some(-4)), 1);
        assert_eq!(temporal_connection_max_retries(Some(7)), 7);
    }

    #[test]
    fn generated_artifacts_root_uses_discovered_config_directory() {
        let tmp = temp_dir();
        let stack_dir = tmp.join(".aegis");
        fs::create_dir_all(&stack_dir).expect("create stack dir");
        let config_path = stack_dir.join("aegis-config.yaml");
        fs::write(
            &config_path,
            "apiVersion: 100monkeys.ai/v1\nkind: NodeConfig\n",
        )
        .expect("write config");

        let resolved = resolve_generated_artifacts_root(Some(PathBuf::from(&config_path)));

        assert_eq!(resolved, stack_dir.join("generated"));

        let _ = fs::remove_dir_all(tmp);
    }

    #[test]
    fn managed_container_reap_reason_skips_debug_retained_containers() {
        let container = ManagedAgentContainer {
            id: "container-1".to_string(),
            execution_id: Some("00000000-0000-0000-0000-000000000001".to_string()),
            debug_retain: true,
            state: Some("running".to_string()),
        };

        assert_eq!(
            managed_container_reap_reason(&container, Some(ExecutionStatus::Running)),
            None
        );
    }

    #[test]
    fn managed_container_reap_reason_keeps_running_executions_with_running_containers() {
        let container = ManagedAgentContainer {
            id: "container-2".to_string(),
            execution_id: Some("00000000-0000-0000-0000-000000000002".to_string()),
            debug_retain: false,
            state: Some("running".to_string()),
        };

        assert_eq!(
            managed_container_reap_reason(&container, Some(ExecutionStatus::Running)),
            None
        );
    }

    #[test]
    fn managed_container_reap_reason_reaps_completed_or_missing_executions() {
        let running_container = ManagedAgentContainer {
            id: "container-3".to_string(),
            execution_id: Some("00000000-0000-0000-0000-000000000003".to_string()),
            debug_retain: false,
            state: Some("running".to_string()),
        };
        let exited_container = ManagedAgentContainer {
            id: "container-4".to_string(),
            execution_id: Some("00000000-0000-0000-0000-000000000004".to_string()),
            debug_retain: false,
            state: Some("exited".to_string()),
        };

        assert_eq!(
            managed_container_reap_reason(&running_container, Some(ExecutionStatus::Completed)),
            Some("execution_not_running")
        );
        assert_eq!(
            managed_container_reap_reason(&exited_container, Some(ExecutionStatus::Running)),
            Some("container_not_running")
        );
        assert_eq!(
            managed_container_reap_reason(&running_container, None),
            Some("missing_execution_record")
        );
    }
}