camel-core 0.5.8

Core engine for rust-camel
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
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};

use camel_api::error_handler::ErrorHandlerConfig;
use camel_api::{
    CamelError, HealthReport, HealthStatus, Lifecycle, MetricsCollector, NoOpMetrics,
    RouteController, RuntimeCommandBus, RuntimeQueryBus, ServiceHealth, ServiceStatus,
    SupervisionConfig,
};
use camel_component_api::Component;
use camel_language_api::Language;
use camel_language_api::LanguageError;

use crate::lifecycle::adapters::RuntimeExecutionAdapter;
use crate::lifecycle::adapters::redb_journal::{RedbJournalOptions, RedbRuntimeEventJournal};
use crate::lifecycle::adapters::route_controller::{
    DefaultRouteController, RouteControllerInternal, SharedLanguageRegistry,
};
use crate::lifecycle::application::route_definition::RouteDefinition;
use crate::lifecycle::application::runtime_bus::RuntimeBus;
use crate::lifecycle::application::supervision_service::SupervisingRouteController;
use crate::shared::components::domain::Registry;
use crate::shared::observability::domain::TracerConfig;

static CONTEXT_COMMAND_SEQ: AtomicU64 = AtomicU64::new(0);

/// The CamelContext is the runtime engine that manages components, routes, and their lifecycle.
///
/// # Lifecycle
///
/// A `CamelContext` is single-use: call [`start()`](Self::start) once to launch routes,
/// then [`stop()`](Self::stop) or [`abort()`](Self::abort) to shut down. Restarting a
/// stopped context is not supported — create a new instance instead.
pub struct CamelContext {
    registry: Arc<std::sync::Mutex<Registry>>,
    route_controller: Arc<Mutex<dyn RouteControllerInternal>>,
    runtime: Arc<RuntimeBus>,
    cancel_token: CancellationToken,
    metrics: Arc<dyn MetricsCollector>,
    languages: SharedLanguageRegistry,
    shutdown_timeout: std::time::Duration,
    services: Vec<Box<dyn Lifecycle>>,
    component_configs: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}

/// Opaque handle for runtime side-effect execution operations.
///
/// This intentionally does not expose direct lifecycle mutation APIs to callers.
#[derive(Clone)]
pub struct RuntimeExecutionHandle {
    controller: Arc<Mutex<dyn RouteControllerInternal>>,
    runtime: Arc<RuntimeBus>,
}

impl RuntimeExecutionHandle {
    pub(crate) async fn add_route_definition(
        &self,
        definition: RouteDefinition,
    ) -> Result<(), CamelError> {
        use crate::lifecycle::ports::RouteRegistrationPort;
        self.runtime.register_route(definition).await
    }

    pub(crate) async fn compile_route_definition(
        &self,
        definition: RouteDefinition,
    ) -> Result<camel_api::BoxProcessor, CamelError> {
        let controller = self.controller.lock().await;
        controller.compile_route_definition(definition)
    }

    pub(crate) async fn swap_route_pipeline(
        &self,
        route_id: &str,
        pipeline: camel_api::BoxProcessor,
    ) -> Result<(), CamelError> {
        let controller = self.controller.lock().await;
        controller.swap_pipeline(route_id, pipeline)
    }

    pub(crate) async fn execute_runtime_command(
        &self,
        cmd: camel_api::RuntimeCommand,
    ) -> Result<camel_api::RuntimeCommandResult, CamelError> {
        self.runtime.execute(cmd).await
    }

    pub(crate) async fn runtime_route_status(
        &self,
        route_id: &str,
    ) -> Result<Option<String>, CamelError> {
        match self
            .runtime
            .ask(camel_api::RuntimeQuery::GetRouteStatus {
                route_id: route_id.to_string(),
            })
            .await
        {
            Ok(camel_api::RuntimeQueryResult::RouteStatus { status, .. }) => Ok(Some(status)),
            Ok(_) => Err(CamelError::RouteError(
                "unexpected runtime query response for route status".to_string(),
            )),
            Err(CamelError::RouteError(msg)) if msg.contains("not found") => Ok(None),
            Err(err) => Err(err),
        }
    }

    pub(crate) async fn runtime_route_ids(&self) -> Result<Vec<String>, CamelError> {
        match self.runtime.ask(camel_api::RuntimeQuery::ListRoutes).await {
            Ok(camel_api::RuntimeQueryResult::Routes { route_ids }) => Ok(route_ids),
            Ok(_) => Err(CamelError::RouteError(
                "unexpected runtime query response for route listing".to_string(),
            )),
            Err(err) => Err(err),
        }
    }

    pub(crate) async fn in_flight_count(&self, route_id: &str) -> Result<u64, CamelError> {
        let controller = self.controller.lock().await;
        if !controller.route_exists(route_id) {
            return Err(CamelError::RouteError(format!(
                "Route '{}' not found",
                route_id
            )));
        }
        // in_flight_count returns Some(N) when route exists.
        // Some(0) means either "truly zero in-flight" or "no UoW tracking configured".
        // Both cases are safe to treat as "drained, proceed immediately".
        Ok(controller.in_flight_count(route_id).unwrap_or(0))
    }

    #[cfg(test)]
    pub(crate) async fn force_start_route_for_test(
        &self,
        route_id: &str,
    ) -> Result<(), CamelError> {
        let mut controller = self.controller.lock().await;
        controller.start_route(route_id).await
    }

    #[cfg(test)]
    pub(crate) async fn controller_route_count_for_test(&self) -> usize {
        let controller = self.controller.lock().await;
        controller.route_count()
    }
}

impl CamelContext {
    fn built_in_languages() -> SharedLanguageRegistry {
        let mut languages: HashMap<String, Arc<dyn Language>> = HashMap::new();
        languages.insert(
            "simple".to_string(),
            Arc::new(camel_language_simple::SimpleLanguage),
        );
        #[cfg(feature = "lang-js")]
        {
            let js_lang = camel_language_js::JsLanguage::new();
            languages.insert("js".to_string(), Arc::new(js_lang.clone()));
            languages.insert("javascript".to_string(), Arc::new(js_lang));
        }
        #[cfg(feature = "lang-rhai")]
        {
            let rhai_lang = camel_language_rhai::RhaiLanguage::new();
            languages.insert("rhai".to_string(), Arc::new(rhai_lang));
        }
        #[cfg(feature = "lang-jsonpath")]
        {
            languages.insert(
                "jsonpath".to_string(),
                Arc::new(camel_language_jsonpath::JsonPathLanguage),
            );
        }
        #[cfg(feature = "lang-xpath")]
        {
            languages.insert(
                "xpath".to_string(),
                Arc::new(camel_language_xpath::XPathLanguage),
            );
        }
        Arc::new(std::sync::Mutex::new(languages))
    }

    fn build_runtime(
        controller: Arc<Mutex<dyn RouteControllerInternal>>,
        store: crate::lifecycle::adapters::InMemoryRuntimeStore,
    ) -> Arc<RuntimeBus> {
        let execution = Arc::new(RuntimeExecutionAdapter::new(Arc::clone(&controller)));
        Arc::new(
            RuntimeBus::new(
                Arc::new(store.clone()),
                Arc::new(store.clone()),
                Arc::new(store.clone()),
                Arc::new(store.clone()),
            )
            .with_uow(Arc::new(store))
            .with_execution(execution),
        )
    }

    /// Create a new, empty CamelContext.
    ///
    /// Runtime state is in-memory/ephemeral by default.
    pub fn new() -> Self {
        Self::with_metrics(Arc::new(NoOpMetrics))
    }

    /// Create a new CamelContext with a custom metrics collector.
    pub fn with_metrics(metrics: Arc<dyn MetricsCollector>) -> Self {
        let registry = Arc::new(std::sync::Mutex::new(Registry::new()));
        let languages = Self::built_in_languages();
        let controller: Arc<Mutex<dyn RouteControllerInternal>> = Arc::new(Mutex::new(
            DefaultRouteController::with_languages(Arc::clone(&registry), Arc::clone(&languages)),
        ));
        let store = crate::lifecycle::adapters::InMemoryRuntimeStore::default();
        let runtime = Self::build_runtime(Arc::clone(&controller), store);

        // Set self-ref so DefaultRouteController can create ProducerContext
        // Use try_lock since we just created it and nobody else has access yet
        let mut controller_guard = controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access");
        controller_guard.set_self_ref(Arc::clone(&controller) as Arc<Mutex<dyn RouteController>>);
        let runtime_handle: Arc<dyn camel_api::RuntimeHandle> = runtime.clone();
        controller_guard.set_runtime_handle(runtime_handle);
        drop(controller_guard);

        Self {
            registry,
            route_controller: controller,
            runtime,
            cancel_token: CancellationToken::new(),
            metrics,
            languages,
            shutdown_timeout: std::time::Duration::from_secs(30),
            services: Vec::new(),
            component_configs: HashMap::new(),
        }
    }

    /// Create a new CamelContext with route supervision enabled.
    ///
    /// The supervision config controls automatic restart behavior for crashed routes.
    pub fn with_supervision(config: SupervisionConfig) -> Self {
        Self::with_supervision_and_metrics(config, Arc::new(NoOpMetrics))
    }

    /// Create a new CamelContext with route supervision and custom metrics.
    ///
    /// The supervision config controls automatic restart behavior for crashed routes.
    pub fn with_supervision_and_metrics(
        config: SupervisionConfig,
        metrics: Arc<dyn MetricsCollector>,
    ) -> Self {
        let registry = Arc::new(std::sync::Mutex::new(Registry::new()));
        let languages = Self::built_in_languages();
        let controller: Arc<Mutex<dyn RouteControllerInternal>> = Arc::new(Mutex::new(
            SupervisingRouteController::with_languages(
                Arc::clone(&registry),
                config,
                Arc::clone(&languages),
            )
            .with_metrics(Arc::clone(&metrics)),
        ));
        let store = crate::lifecycle::adapters::InMemoryRuntimeStore::default();
        let runtime = Self::build_runtime(Arc::clone(&controller), store);

        // Set self-ref so SupervisingRouteController can create ProducerContext
        // Use try_lock since we just created it and nobody else has access yet
        let mut controller_guard = controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access");
        controller_guard.set_self_ref(Arc::clone(&controller) as Arc<Mutex<dyn RouteController>>);
        let runtime_handle: Arc<dyn camel_api::RuntimeHandle> = runtime.clone();
        controller_guard.set_runtime_handle(runtime_handle);
        drop(controller_guard);

        Self {
            registry,
            route_controller: controller,
            runtime,
            cancel_token: CancellationToken::new(),
            metrics,
            languages,
            shutdown_timeout: std::time::Duration::from_secs(30),
            services: Vec::new(),
            component_configs: HashMap::new(),
        }
    }

    /// Create a new CamelContext backed by a redb runtime journal.
    pub async fn new_with_redb_journal(
        path: impl Into<PathBuf>,
        options: RedbJournalOptions,
    ) -> Result<Self, CamelError> {
        Self::with_metrics_and_redb_journal(Arc::new(NoOpMetrics), path, options).await
    }

    /// Create a new CamelContext with custom metrics and a redb runtime journal.
    pub async fn with_metrics_and_redb_journal(
        metrics: Arc<dyn MetricsCollector>,
        path: impl Into<PathBuf>,
        options: RedbJournalOptions,
    ) -> Result<Self, CamelError> {
        let registry = Arc::new(std::sync::Mutex::new(Registry::new()));
        let languages = Self::built_in_languages();
        let controller: Arc<Mutex<dyn RouteControllerInternal>> = Arc::new(Mutex::new(
            DefaultRouteController::with_languages(Arc::clone(&registry), Arc::clone(&languages)),
        ));
        let journal = RedbRuntimeEventJournal::new(path, options).await?;
        let store = crate::lifecycle::adapters::InMemoryRuntimeStore::default()
            .with_journal(Arc::new(journal));
        let runtime = Self::build_runtime(Arc::clone(&controller), store);

        // Set self-ref so DefaultRouteController can create ProducerContext
        // Use try_lock since we just created it and nobody else has access yet
        let mut controller_guard = controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access");
        controller_guard.set_self_ref(Arc::clone(&controller) as Arc<Mutex<dyn RouteController>>);
        let runtime_handle: Arc<dyn camel_api::RuntimeHandle> = runtime.clone();
        controller_guard.set_runtime_handle(runtime_handle);
        drop(controller_guard);

        Ok(Self {
            registry,
            route_controller: controller,
            runtime,
            cancel_token: CancellationToken::new(),
            metrics,
            languages,
            shutdown_timeout: std::time::Duration::from_secs(30),
            services: Vec::new(),
            component_configs: HashMap::new(),
        })
    }

    /// Create a new CamelContext with supervision, custom metrics, and a redb runtime journal.
    pub async fn with_supervision_and_metrics_and_redb_journal(
        config: SupervisionConfig,
        metrics: Arc<dyn MetricsCollector>,
        path: impl Into<PathBuf>,
        options: RedbJournalOptions,
    ) -> Result<Self, CamelError> {
        let registry = Arc::new(std::sync::Mutex::new(Registry::new()));
        let languages = Self::built_in_languages();
        let controller: Arc<Mutex<dyn RouteControllerInternal>> = Arc::new(Mutex::new(
            SupervisingRouteController::with_languages(
                Arc::clone(&registry),
                config,
                Arc::clone(&languages),
            )
            .with_metrics(Arc::clone(&metrics)),
        ));
        let journal = RedbRuntimeEventJournal::new(path, options).await?;
        let store = crate::lifecycle::adapters::InMemoryRuntimeStore::default()
            .with_journal(Arc::new(journal));
        let runtime = Self::build_runtime(Arc::clone(&controller), store);

        // Set self-ref so SupervisingRouteController can create ProducerContext
        // Use try_lock since we just created it and nobody else has access yet
        let mut controller_guard = controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access");
        controller_guard.set_self_ref(Arc::clone(&controller) as Arc<Mutex<dyn RouteController>>);
        let runtime_handle: Arc<dyn camel_api::RuntimeHandle> = runtime.clone();
        controller_guard.set_runtime_handle(runtime_handle);
        drop(controller_guard);

        Ok(Self {
            registry,
            route_controller: controller,
            runtime,
            cancel_token: CancellationToken::new(),
            metrics,
            languages,
            shutdown_timeout: std::time::Duration::from_secs(30),
            services: Vec::new(),
            component_configs: HashMap::new(),
        })
    }

    /// Set a global error handler applied to all routes without a per-route handler.
    pub fn set_error_handler(&mut self, config: ErrorHandlerConfig) {
        self.route_controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access")
            .set_error_handler(config);
    }

    /// Enable or disable tracing globally.
    pub fn set_tracing(&mut self, enabled: bool) {
        self.route_controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access")
            .set_tracer_config(&TracerConfig {
                enabled,
                ..Default::default()
            });
    }

    /// Configure tracing with full config.
    pub fn set_tracer_config(&mut self, config: TracerConfig) {
        // Inject metrics collector if not already set
        let config = if config.metrics_collector.is_none() {
            TracerConfig {
                metrics_collector: Some(Arc::clone(&self.metrics)),
                ..config
            }
        } else {
            config
        };

        self.route_controller
            .try_lock()
            .expect("BUG: CamelContext lock contention — try_lock should always succeed here since &mut self prevents concurrent access")
            .set_tracer_config(&config);
    }

    /// Builder-style: enable tracing with default config.
    pub fn with_tracing(mut self) -> Self {
        self.set_tracing(true);
        self
    }

    /// Builder-style: configure tracing with custom config.
    /// Note: tracing subscriber initialization (stdout/file output) is handled
    /// separately via init_tracing_subscriber (called in camel-config bridge).
    pub fn with_tracer_config(mut self, config: TracerConfig) -> Self {
        self.set_tracer_config(config);
        self
    }

    /// Register a lifecycle service (Apache Camel: addService pattern)
    pub fn with_lifecycle<L: Lifecycle + 'static>(mut self, service: L) -> Self {
        // Auto-register MetricsCollector if available
        if let Some(collector) = service.as_metrics_collector() {
            self.metrics = collector;
        }

        self.services.push(Box::new(service));
        self
    }

    /// Register a component with this context.
    pub fn register_component<C: Component + 'static>(&mut self, component: C) {
        info!(scheme = component.scheme(), "Registering component");
        self.registry
            .lock()
            .expect("mutex poisoned: another thread panicked while holding this lock")
            .register(component);
    }

    /// Register a language with this context, keyed by name.
    ///
    /// Returns `Err(LanguageError::AlreadyRegistered)` if a language with the
    /// same name is already registered. Use [`resolve_language`](Self::resolve_language)
    /// to check before registering, or choose a distinct name.
    pub fn register_language(
        &mut self,
        name: impl Into<String>,
        lang: Box<dyn Language>,
    ) -> Result<(), LanguageError> {
        let name = name.into();
        let mut languages = self
            .languages
            .lock()
            .expect("mutex poisoned: another thread panicked while holding this lock");
        if languages.contains_key(&name) {
            return Err(LanguageError::AlreadyRegistered(name));
        }
        languages.insert(name, Arc::from(lang));
        Ok(())
    }

    /// Resolve a language by name. Returns `None` if not registered.
    pub fn resolve_language(&self, name: &str) -> Option<Arc<dyn Language>> {
        let languages = self
            .languages
            .lock()
            .expect("mutex poisoned: another thread panicked while holding this lock");
        languages.get(name).cloned()
    }

    /// Add a route definition to this context.
    ///
    /// The route must have an ID. Steps are resolved immediately using registered components.
    pub async fn add_route_definition(
        &self,
        definition: RouteDefinition,
    ) -> Result<(), CamelError> {
        use crate::lifecycle::ports::RouteRegistrationPort;
        info!(
            from = definition.from_uri(),
            route_id = %definition.route_id(),
            "Adding route definition"
        );
        self.runtime.register_route(definition).await
    }

    fn next_context_command_id(op: &str, route_id: &str) -> String {
        let seq = CONTEXT_COMMAND_SEQ.fetch_add(1, Ordering::Relaxed);
        format!("context:{op}:{route_id}:{seq}")
    }

    /// Access the component registry.
    pub fn registry(&self) -> std::sync::MutexGuard<'_, Registry> {
        self.registry
            .lock()
            .expect("mutex poisoned: another thread panicked while holding this lock")
    }

    /// Get runtime execution handle for file-watcher integrations.
    pub fn runtime_execution_handle(&self) -> RuntimeExecutionHandle {
        RuntimeExecutionHandle {
            controller: Arc::clone(&self.route_controller),
            runtime: Arc::clone(&self.runtime),
        }
    }

    /// Get the metrics collector.
    pub fn metrics(&self) -> Arc<dyn MetricsCollector> {
        Arc::clone(&self.metrics)
    }

    /// Get runtime command/query bus handle.
    pub fn runtime(&self) -> Arc<dyn camel_api::RuntimeHandle> {
        self.runtime.clone()
    }

    /// Build a producer context wired to this runtime.
    pub fn producer_context(&self) -> camel_api::ProducerContext {
        camel_api::ProducerContext::new().with_runtime(self.runtime())
    }

    /// Query route status via runtime read-model.
    pub async fn runtime_route_status(&self, route_id: &str) -> Result<Option<String>, CamelError> {
        match self
            .runtime()
            .ask(camel_api::RuntimeQuery::GetRouteStatus {
                route_id: route_id.to_string(),
            })
            .await
        {
            Ok(camel_api::RuntimeQueryResult::RouteStatus { status, .. }) => Ok(Some(status)),
            Ok(_) => Err(CamelError::RouteError(
                "unexpected runtime query response for route status".to_string(),
            )),
            Err(CamelError::RouteError(msg)) if msg.contains("not found") => Ok(None),
            Err(err) => Err(err),
        }
    }

    /// Start all routes. Each route's consumer will begin producing exchanges.
    ///
    /// Only routes with `auto_startup == true` will be started, in order of their
    /// `startup_order` (lower values start first).
    pub async fn start(&mut self) -> Result<(), CamelError> {
        info!("Starting CamelContext");

        // Start lifecycle services first
        for (i, service) in self.services.iter_mut().enumerate() {
            info!("Starting service: {}", service.name());
            if let Err(e) = service.start().await {
                // Rollback: stop already started services in reverse order
                warn!(
                    "Service {} failed to start, rolling back {} services",
                    service.name(),
                    i
                );
                for j in (0..i).rev() {
                    if let Err(rollback_err) = self.services[j].stop().await {
                        warn!(
                            "Failed to stop service {} during rollback: {}",
                            self.services[j].name(),
                            rollback_err
                        );
                    }
                }
                return Err(e);
            }
        }

        // Then start routes via runtime command bus (aggregate-first),
        // preserving route controller startup ordering metadata.
        let route_ids = {
            let controller = self.route_controller.lock().await;
            controller.auto_startup_route_ids()
        };
        for route_id in route_ids {
            self.runtime
                .execute(camel_api::RuntimeCommand::StartRoute {
                    route_id: route_id.clone(),
                    command_id: Self::next_context_command_id("start", &route_id),
                    causation_id: None,
                })
                .await?;
        }

        info!("CamelContext started");
        Ok(())
    }

    /// Graceful shutdown with default 30-second timeout.
    pub async fn stop(&mut self) -> Result<(), CamelError> {
        self.stop_timeout(self.shutdown_timeout).await
    }

    /// Graceful shutdown with custom timeout.
    ///
    /// Note: The timeout parameter is currently not used directly; the RouteController
    /// manages its own shutdown timeout. This may change in a future version.
    pub async fn stop_timeout(&mut self, _timeout: std::time::Duration) -> Result<(), CamelError> {
        info!("Stopping CamelContext");

        // Signal cancellation (for any legacy code that might use it)
        self.cancel_token.cancel();

        // Stop all routes via runtime command bus (aggregate-first),
        // preserving route controller shutdown ordering metadata.
        let route_ids = {
            let controller = self.route_controller.lock().await;
            controller.shutdown_route_ids()
        };
        for route_id in route_ids {
            if let Err(err) = self
                .runtime
                .execute(camel_api::RuntimeCommand::StopRoute {
                    route_id: route_id.clone(),
                    command_id: Self::next_context_command_id("stop", &route_id),
                    causation_id: None,
                })
                .await
            {
                warn!(route_id = %route_id, error = %err, "Runtime stop command failed during context shutdown");
            }
        }

        // Then stop lifecycle services in reverse insertion order (LIFO)
        // Continue stopping all services even if some fail
        let mut first_error = None;
        for service in self.services.iter_mut().rev() {
            info!("Stopping service: {}", service.name());
            if let Err(e) = service.stop().await {
                warn!("Service {} failed to stop: {}", service.name(), e);
                if first_error.is_none() {
                    first_error = Some(e);
                }
            }
        }

        info!("CamelContext stopped");

        if let Some(e) = first_error {
            Err(e)
        } else {
            Ok(())
        }
    }

    /// Get the graceful shutdown timeout used by [`stop()`](Self::stop).
    pub fn shutdown_timeout(&self) -> std::time::Duration {
        self.shutdown_timeout
    }

    /// Set the graceful shutdown timeout used by [`stop()`](Self::stop).
    pub fn set_shutdown_timeout(&mut self, timeout: std::time::Duration) {
        self.shutdown_timeout = timeout;
    }

    /// Immediate abort — kills all tasks without draining.
    pub async fn abort(&mut self) {
        self.cancel_token.cancel();
        let route_ids = {
            let controller = self.route_controller.lock().await;
            controller.shutdown_route_ids()
        };
        for route_id in route_ids {
            let _ = self
                .runtime
                .execute(camel_api::RuntimeCommand::StopRoute {
                    route_id: route_id.clone(),
                    command_id: Self::next_context_command_id("abort-stop", &route_id),
                    causation_id: None,
                })
                .await;
        }
    }

    /// Check health status of all registered services.
    pub fn health_check(&self) -> HealthReport {
        let services: Vec<ServiceHealth> = self
            .services
            .iter()
            .map(|s| ServiceHealth {
                name: s.name().to_string(),
                status: s.status(),
            })
            .collect();

        let status = if services.iter().all(|s| s.status == ServiceStatus::Started) {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unhealthy
        };

        HealthReport {
            status,
            services,
            ..Default::default()
        }
    }

    /// Store a component config. Overwrites any previously stored config of the same type.
    pub fn set_component_config<T: 'static + Send + Sync>(&mut self, config: T) {
        self.component_configs
            .insert(TypeId::of::<T>(), Box::new(config));
    }

    /// Retrieve a stored component config by type. Returns None if not stored.
    pub fn get_component_config<T: 'static + Send + Sync>(&self) -> Option<&T> {
        self.component_configs
            .get(&TypeId::of::<T>())
            .and_then(|b| b.downcast_ref::<T>())
    }
}

impl Default for CamelContext {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lifecycle::application::route_definition::{
        BuilderStep, LanguageExpressionDef, RouteDefinition,
    };
    use crate::lifecycle::domain::{RouteRuntimeAggregate, RouteRuntimeState};
    use async_trait::async_trait;
    use camel_api::CamelError;
    use camel_api::{
        CanonicalRouteSpec, RuntimeCommand, RuntimeCommandResult, RuntimeQuery, RuntimeQueryResult,
    };
    use camel_component_api::{Component, ConcurrencyModel, Consumer, ConsumerContext, Endpoint};

    /// Mock component for testing
    struct MockComponent;

    impl Component for MockComponent {
        fn scheme(&self) -> &str {
            "mock"
        }

        fn create_endpoint(&self, _uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
            Err(CamelError::ComponentNotFound("mock".to_string()))
        }
    }

    struct HoldConsumer;

    #[async_trait]
    impl Consumer for HoldConsumer {
        async fn start(&mut self, ctx: ConsumerContext) -> Result<(), CamelError> {
            ctx.cancelled().await;
            Ok(())
        }

        async fn stop(&mut self) -> Result<(), CamelError> {
            Ok(())
        }

        fn concurrency_model(&self) -> ConcurrencyModel {
            ConcurrencyModel::Sequential
        }
    }

    struct HoldEndpoint;

    impl Endpoint for HoldEndpoint {
        fn uri(&self) -> &str {
            "hold:test"
        }

        fn create_consumer(&self) -> Result<Box<dyn Consumer>, CamelError> {
            Ok(Box::new(HoldConsumer))
        }

        fn create_producer(
            &self,
            _ctx: &camel_api::ProducerContext,
        ) -> Result<camel_api::BoxProcessor, CamelError> {
            Err(CamelError::RouteError("no producer".to_string()))
        }
    }

    struct HoldComponent;

    impl Component for HoldComponent {
        fn scheme(&self) -> &str {
            "hold"
        }

        fn create_endpoint(&self, _uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
            Ok(Box::new(HoldEndpoint))
        }
    }

    #[test]
    fn test_context_handles_mutex_poisoning_gracefully() {
        let mut ctx = CamelContext::new();

        // Register a component successfully
        ctx.register_component(MockComponent);

        // Access registry should work even after potential panic in another thread
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _guard = ctx.registry();
        }));

        assert!(
            result.is_ok(),
            "Registry access should handle mutex poisoning"
        );
    }

    #[test]
    fn test_context_resolves_simple_language() {
        let ctx = CamelContext::new();
        let lang = ctx
            .resolve_language("simple")
            .expect("simple language not found");
        assert_eq!(lang.name(), "simple");
    }

    #[test]
    fn test_simple_language_via_context() {
        let ctx = CamelContext::new();
        let lang = ctx.resolve_language("simple").unwrap();
        let pred = lang.create_predicate("${header.x} == 'hello'").unwrap();
        let mut msg = camel_api::message::Message::default();
        msg.set_header("x", camel_api::Value::String("hello".into()));
        let ex = camel_api::exchange::Exchange::new(msg);
        assert!(pred.matches(&ex).unwrap());
    }

    #[test]
    fn test_resolve_unknown_language_returns_none() {
        let ctx = CamelContext::new();
        assert!(ctx.resolve_language("nonexistent").is_none());
    }

    #[test]
    fn test_register_language_duplicate_returns_error() {
        use camel_language_api::LanguageError;
        struct DummyLang;
        impl camel_language_api::Language for DummyLang {
            fn name(&self) -> &'static str {
                "dummy"
            }
            fn create_expression(
                &self,
                _: &str,
            ) -> Result<Box<dyn camel_language_api::Expression>, LanguageError> {
                Err(LanguageError::EvalError("not implemented".into()))
            }
            fn create_predicate(
                &self,
                _: &str,
            ) -> Result<Box<dyn camel_language_api::Predicate>, LanguageError> {
                Err(LanguageError::EvalError("not implemented".into()))
            }
        }

        let mut ctx = CamelContext::new();
        ctx.register_language("dummy", Box::new(DummyLang)).unwrap();
        let result = ctx.register_language("dummy", Box::new(DummyLang));
        assert!(result.is_err(), "duplicate registration should fail");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("dummy"),
            "error should mention the language name"
        );
    }

    #[test]
    fn test_register_language_new_key_succeeds() {
        use camel_language_api::LanguageError;
        struct DummyLang;
        impl camel_language_api::Language for DummyLang {
            fn name(&self) -> &'static str {
                "dummy"
            }
            fn create_expression(
                &self,
                _: &str,
            ) -> Result<Box<dyn camel_language_api::Expression>, LanguageError> {
                Err(LanguageError::EvalError("not implemented".into()))
            }
            fn create_predicate(
                &self,
                _: &str,
            ) -> Result<Box<dyn camel_language_api::Predicate>, LanguageError> {
                Err(LanguageError::EvalError("not implemented".into()))
            }
        }

        let mut ctx = CamelContext::new();
        let result = ctx.register_language("dummy", Box::new(DummyLang));
        assert!(result.is_ok(), "first registration should succeed");
    }

    #[tokio::test]
    async fn test_add_route_definition_uses_runtime_registered_language() {
        use camel_language_api::{Expression, LanguageError, Predicate};

        struct DummyExpression;
        impl Expression for DummyExpression {
            fn evaluate(
                &self,
                _exchange: &camel_api::Exchange,
            ) -> Result<camel_api::Value, LanguageError> {
                Ok(camel_api::Value::String("ok".into()))
            }
        }

        struct DummyPredicate;
        impl Predicate for DummyPredicate {
            fn matches(&self, _exchange: &camel_api::Exchange) -> Result<bool, LanguageError> {
                Ok(true)
            }
        }

        struct RuntimeLang;
        impl camel_language_api::Language for RuntimeLang {
            fn name(&self) -> &'static str {
                "runtime"
            }

            fn create_expression(
                &self,
                _script: &str,
            ) -> Result<Box<dyn Expression>, LanguageError> {
                Ok(Box::new(DummyExpression))
            }

            fn create_predicate(&self, _script: &str) -> Result<Box<dyn Predicate>, LanguageError> {
                Ok(Box::new(DummyPredicate))
            }
        }

        let mut ctx = CamelContext::new();
        ctx.register_language("runtime", Box::new(RuntimeLang))
            .unwrap();

        let definition = RouteDefinition::new(
            "timer:tick",
            vec![BuilderStep::DeclarativeScript {
                expression: LanguageExpressionDef {
                    language: "runtime".into(),
                    source: "${body}".into(),
                },
            }],
        )
        .with_route_id("runtime-lang-route");

        let result = ctx.add_route_definition(definition).await;
        assert!(
            result.is_ok(),
            "route should resolve runtime language: {result:?}"
        );
    }

    #[tokio::test]
    async fn test_add_route_definition_fails_for_unregistered_runtime_language() {
        let ctx = CamelContext::new();
        let definition = RouteDefinition::new(
            "timer:tick",
            vec![BuilderStep::DeclarativeSetBody {
                value: crate::route::ValueSourceDef::Expression(LanguageExpressionDef {
                    language: "missing-lang".into(),
                    source: "${body}".into(),
                }),
            }],
        )
        .with_route_id("missing-runtime-lang-route");

        let result = ctx.add_route_definition(definition).await;
        assert!(
            result.is_err(),
            "route should fail when language is missing"
        );
        let error_text = result.unwrap_err().to_string();
        assert!(
            error_text.contains("missing-lang"),
            "error should mention missing language, got: {error_text}"
        );
    }

    #[tokio::test]
    async fn add_route_definition_does_not_require_mut() {
        let ctx = CamelContext::new();
        let definition = RouteDefinition::new("timer:tick", vec![]).with_route_id("immutable-ctx");

        let result = ctx.add_route_definition(definition).await;
        assert!(
            result.is_ok(),
            "immutable context should add route: {result:?}"
        );
    }

    #[test]
    fn test_health_check_empty_context() {
        let ctx = CamelContext::new();
        let report = ctx.health_check();

        assert_eq!(report.status, HealthStatus::Healthy);
        assert!(report.services.is_empty());
    }

    #[tokio::test]
    async fn context_exposes_runtime_command_and_query_buses() {
        let ctx = CamelContext::new();
        let runtime = ctx.runtime();

        let register = runtime
            .execute(RuntimeCommand::RegisterRoute {
                spec: CanonicalRouteSpec::new("runtime-r1", "timer:tick"),
                command_id: "cmd-1".into(),
                causation_id: None,
            })
            .await
            .unwrap();
        assert!(matches!(
            register,
            RuntimeCommandResult::RouteRegistered { ref route_id } if route_id == "runtime-r1"
        ));

        let query = runtime
            .ask(RuntimeQuery::GetRouteStatus {
                route_id: "runtime-r1".into(),
            })
            .await
            .unwrap();
        assert!(matches!(
            query,
            RuntimeQueryResult::RouteStatus { ref status, .. } if status == "Registered"
        ));
    }

    #[tokio::test]
    async fn default_runtime_journal_isolated_per_context_without_env_override() {
        if let Ok(value) = std::env::var("CAMEL_RUNTIME_JOURNAL_PATH")
            && !value.trim().is_empty()
        {
            return;
        }

        let first = CamelContext::new();
        first
            .runtime()
            .execute(RuntimeCommand::RegisterRoute {
                spec: CanonicalRouteSpec::new("default-isolation-r1", "timer:tick"),
                command_id: "iso-c1".into(),
                causation_id: None,
            })
            .await
            .unwrap();

        let second = CamelContext::new();
        second
            .runtime()
            .execute(RuntimeCommand::RegisterRoute {
                spec: CanonicalRouteSpec::new("default-isolation-r1", "timer:tick"),
                command_id: "iso-c2".into(),
                causation_id: None,
            })
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn runtime_commands_drive_real_route_controller_lifecycle() {
        let mut ctx = CamelContext::new();
        ctx.register_component(HoldComponent);
        let runtime = ctx.runtime();

        runtime
            .execute(RuntimeCommand::RegisterRoute {
                spec: CanonicalRouteSpec::new("runtime-hold", "hold:test"),
                command_id: "c1".into(),
                causation_id: None,
            })
            .await
            .unwrap();

        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Registered".to_string())
        );
        assert!(matches!(
            ctx.runtime.repo().load("runtime-hold").await.unwrap(),
            Some(agg)
                if matches!(agg.state(), crate::lifecycle::domain::RouteRuntimeState::Registered)
        ));

        runtime
            .execute(RuntimeCommand::StartRoute {
                route_id: "runtime-hold".into(),
                command_id: "c2".into(),
                causation_id: Some("c1".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Started".to_string())
        );
        assert!(matches!(
            ctx.runtime.repo().load("runtime-hold").await.unwrap(),
            Some(agg)
                if matches!(agg.state(), crate::lifecycle::domain::RouteRuntimeState::Started)
        ));

        runtime
            .execute(RuntimeCommand::SuspendRoute {
                route_id: "runtime-hold".into(),
                command_id: "c3".into(),
                causation_id: Some("c2".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Suspended".to_string())
        );

        runtime
            .execute(RuntimeCommand::ResumeRoute {
                route_id: "runtime-hold".into(),
                command_id: "c4".into(),
                causation_id: Some("c3".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Started".to_string())
        );

        runtime
            .execute(RuntimeCommand::StopRoute {
                route_id: "runtime-hold".into(),
                command_id: "c5".into(),
                causation_id: Some("c4".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Stopped".to_string())
        );

        runtime
            .execute(RuntimeCommand::ReloadRoute {
                route_id: "runtime-hold".into(),
                command_id: "c6".into(),
                causation_id: Some("c5".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Started".to_string())
        );

        runtime
            .execute(RuntimeCommand::StopRoute {
                route_id: "runtime-hold".into(),
                command_id: "c7".into(),
                causation_id: Some("c6".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            Some("Stopped".to_string())
        );

        runtime
            .execute(RuntimeCommand::RemoveRoute {
                route_id: "runtime-hold".into(),
                command_id: "c8".into(),
                causation_id: Some("c7".into()),
            })
            .await
            .unwrap();
        assert_eq!(
            ctx.runtime_route_status("runtime-hold").await.unwrap(),
            None
        );
        assert!(
            ctx.runtime
                .repo()
                .load("runtime-hold")
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn runtime_queries_read_projection_state_when_connected() {
        let mut ctx = CamelContext::new();
        ctx.register_component(HoldComponent);
        let runtime = ctx.runtime();
        runtime
            .execute(RuntimeCommand::RegisterRoute {
                spec: CanonicalRouteSpec::new("rq", "hold:test"),
                command_id: "c1".into(),
                causation_id: None,
            })
            .await
            .unwrap();

        // Diverge live controller state from the projection on purpose.
        ctx.runtime_execution_handle()
            .force_start_route_for_test("rq")
            .await
            .unwrap();

        let result = runtime
            .ask(RuntimeQuery::GetRouteStatus {
                route_id: "rq".into(),
            })
            .await
            .unwrap();

        match result {
            RuntimeQueryResult::RouteStatus { status, .. } => assert_eq!(status, "Registered"),
            _ => panic!("unexpected query result"),
        }
    }

    #[tokio::test]
    async fn add_route_definition_produces_registered_state() {
        let ctx = CamelContext::new();
        let definition =
            RouteDefinition::new("direct:test", vec![]).with_route_id("async-test-route");

        ctx.add_route_definition(definition).await.unwrap();

        let status = ctx
            .runtime()
            .ask(RuntimeQuery::GetRouteStatus {
                route_id: "async-test-route".to_string(),
            })
            .await
            .unwrap();

        match status {
            RuntimeQueryResult::RouteStatus { status, .. } => {
                assert_eq!(
                    status, "Registered",
                    "expected Registered state after add_route_definition"
                );
            }
            _ => panic!("unexpected query result"),
        }
    }

    #[tokio::test]
    async fn add_route_definition_injects_runtime_into_producer_context() {
        use std::sync::atomic::{AtomicBool, Ordering};

        struct RuntimeAwareEndpoint {
            saw_runtime: Arc<AtomicBool>,
        }

        impl Endpoint for RuntimeAwareEndpoint {
            fn uri(&self) -> &str {
                "runtime-aware:test"
            }

            fn create_consumer(
                &self,
            ) -> Result<Box<dyn camel_component_api::Consumer>, CamelError> {
                Err(CamelError::RouteError("no consumer".to_string()))
            }

            fn create_producer(
                &self,
                ctx: &camel_api::ProducerContext,
            ) -> Result<camel_api::BoxProcessor, CamelError> {
                self.saw_runtime
                    .store(ctx.runtime().is_some(), Ordering::SeqCst);
                if ctx.runtime().is_none() {
                    return Err(CamelError::RouteError(
                        "runtime handle missing in ProducerContext".to_string(),
                    ));
                }
                Ok(camel_api::BoxProcessor::new(camel_api::IdentityProcessor))
            }
        }

        struct RuntimeAwareComponent {
            saw_runtime: Arc<AtomicBool>,
        }

        impl Component for RuntimeAwareComponent {
            fn scheme(&self) -> &str {
                "runtime-aware"
            }

            fn create_endpoint(&self, _uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
                Ok(Box::new(RuntimeAwareEndpoint {
                    saw_runtime: Arc::clone(&self.saw_runtime),
                }))
            }
        }

        let saw_runtime = Arc::new(AtomicBool::new(false));
        let mut ctx = CamelContext::new();
        ctx.register_component(RuntimeAwareComponent {
            saw_runtime: Arc::clone(&saw_runtime),
        });

        let definition = RouteDefinition::new(
            "timer:tick",
            vec![BuilderStep::To("runtime-aware:test".to_string())],
        )
        .with_route_id("runtime-aware-route");

        let result = ctx.add_route_definition(definition).await;
        assert!(
            result.is_ok(),
            "route should resolve producer with runtime context: {result:?}"
        );
        assert!(
            saw_runtime.load(Ordering::SeqCst),
            "component producer should observe runtime handle in ProducerContext"
        );
    }

    #[tokio::test]
    async fn add_route_definition_registers_runtime_projection_and_aggregate() {
        let mut ctx = CamelContext::new();
        ctx.register_component(HoldComponent);

        let definition = RouteDefinition::new("hold:test", vec![]).with_route_id("ctx-runtime-r1");
        ctx.add_route_definition(definition).await.unwrap();

        let aggregate = ctx.runtime.repo().load("ctx-runtime-r1").await.unwrap();
        assert!(
            matches!(aggregate, Some(agg) if matches!(agg.state(), RouteRuntimeState::Registered)),
            "route registration should seed aggregate as Registered"
        );

        let status = ctx.runtime_route_status("ctx-runtime-r1").await.unwrap();
        assert_eq!(status.as_deref(), Some("Registered"));
    }

    #[tokio::test]
    async fn add_route_definition_rolls_back_controller_when_runtime_registration_fails() {
        let mut ctx = CamelContext::new();
        ctx.register_component(HoldComponent);

        ctx.runtime
            .repo()
            .save(RouteRuntimeAggregate::new("ctx-runtime-dup"))
            .await
            .unwrap();

        let definition = RouteDefinition::new("hold:test", vec![]).with_route_id("ctx-runtime-dup");
        let result = ctx.add_route_definition(definition).await;
        assert!(result.is_err(), "duplicate runtime registration must fail");

        assert_eq!(
            ctx.runtime_execution_handle()
                .controller_route_count_for_test()
                .await,
            0,
            "controller route should be rolled back on runtime bootstrap failure"
        );
    }

    #[tokio::test]
    async fn context_start_stop_drives_runtime_lifecycle_via_command_bus() {
        let mut ctx = CamelContext::new();
        ctx.register_component(HoldComponent);

        let autostart =
            RouteDefinition::new("hold:test", vec![]).with_route_id("ctx-lifecycle-auto");
        let lazy = RouteDefinition::new("hold:test", vec![])
            .with_route_id("ctx-lifecycle-lazy")
            .with_auto_startup(false);

        ctx.add_route_definition(autostart).await.unwrap();
        ctx.add_route_definition(lazy).await.unwrap();

        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-auto")
                .await
                .unwrap(),
            Some("Registered".to_string())
        );
        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-lazy")
                .await
                .unwrap(),
            Some("Registered".to_string())
        );

        ctx.start().await.unwrap();

        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-auto")
                .await
                .unwrap(),
            Some("Started".to_string())
        );
        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-lazy")
                .await
                .unwrap(),
            Some("Registered".to_string())
        );

        ctx.stop().await.unwrap();

        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-auto")
                .await
                .unwrap(),
            Some("Stopped".to_string())
        );
        assert_eq!(
            ctx.runtime_route_status("ctx-lifecycle-lazy")
                .await
                .unwrap(),
            Some("Registered".to_string())
        );
    }
}

#[cfg(test)]
mod lifecycle_tests {
    use super::*;
    use async_trait::async_trait;
    use camel_api::Lifecycle;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    struct MockService {
        start_count: Arc<AtomicUsize>,
        stop_count: Arc<AtomicUsize>,
    }

    impl MockService {
        fn new() -> (Self, Arc<AtomicUsize>, Arc<AtomicUsize>) {
            let start_count = Arc::new(AtomicUsize::new(0));
            let stop_count = Arc::new(AtomicUsize::new(0));
            (
                Self {
                    start_count: start_count.clone(),
                    stop_count: stop_count.clone(),
                },
                start_count,
                stop_count,
            )
        }
    }

    #[async_trait]
    impl Lifecycle for MockService {
        fn name(&self) -> &str {
            "mock"
        }

        async fn start(&mut self) -> Result<(), CamelError> {
            self.start_count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }

        async fn stop(&mut self) -> Result<(), CamelError> {
            self.stop_count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_context_starts_lifecycle_services() {
        let (service, start_count, stop_count) = MockService::new();

        let mut ctx = CamelContext::new().with_lifecycle(service);

        assert_eq!(start_count.load(Ordering::SeqCst), 0);

        ctx.start().await.unwrap();

        assert_eq!(start_count.load(Ordering::SeqCst), 1);
        assert_eq!(stop_count.load(Ordering::SeqCst), 0);

        ctx.stop().await.unwrap();

        assert_eq!(stop_count.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_service_start_failure_rollback() {
        struct FailingService {
            start_count: Arc<AtomicUsize>,
            stop_count: Arc<AtomicUsize>,
            should_fail: bool,
        }

        #[async_trait]
        impl Lifecycle for FailingService {
            fn name(&self) -> &str {
                "failing"
            }

            async fn start(&mut self) -> Result<(), CamelError> {
                self.start_count.fetch_add(1, Ordering::SeqCst);
                if self.should_fail {
                    Err(CamelError::ProcessorError("intentional failure".into()))
                } else {
                    Ok(())
                }
            }

            async fn stop(&mut self) -> Result<(), CamelError> {
                self.stop_count.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        }

        let start1 = Arc::new(AtomicUsize::new(0));
        let stop1 = Arc::new(AtomicUsize::new(0));
        let start2 = Arc::new(AtomicUsize::new(0));
        let stop2 = Arc::new(AtomicUsize::new(0));
        let start3 = Arc::new(AtomicUsize::new(0));
        let stop3 = Arc::new(AtomicUsize::new(0));

        let service1 = FailingService {
            start_count: start1.clone(),
            stop_count: stop1.clone(),
            should_fail: false,
        };
        let service2 = FailingService {
            start_count: start2.clone(),
            stop_count: stop2.clone(),
            should_fail: true, // This one will fail
        };
        let service3 = FailingService {
            start_count: start3.clone(),
            stop_count: stop3.clone(),
            should_fail: false,
        };

        let mut ctx = CamelContext::new()
            .with_lifecycle(service1)
            .with_lifecycle(service2)
            .with_lifecycle(service3);

        // Attempt to start - should fail
        let result = ctx.start().await;
        assert!(result.is_err());

        // Verify service1 was started and then stopped (rollback)
        assert_eq!(start1.load(Ordering::SeqCst), 1);
        assert_eq!(stop1.load(Ordering::SeqCst), 1);

        // Verify service2 was attempted to start but failed
        assert_eq!(start2.load(Ordering::SeqCst), 1);
        assert_eq!(stop2.load(Ordering::SeqCst), 0);

        // Verify service3 was never started
        assert_eq!(start3.load(Ordering::SeqCst), 0);
        assert_eq!(stop3.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_services_stop_in_reverse_order() {
        use std::sync::Mutex as StdMutex;

        struct OrderTracker {
            name: String,
            order: Arc<StdMutex<Vec<String>>>,
        }

        #[async_trait]
        impl Lifecycle for OrderTracker {
            fn name(&self) -> &str {
                &self.name
            }

            async fn start(&mut self) -> Result<(), CamelError> {
                Ok(())
            }

            async fn stop(&mut self) -> Result<(), CamelError> {
                self.order.lock().unwrap().push(self.name.clone());
                Ok(())
            }
        }

        let order = Arc::new(StdMutex::new(Vec::<String>::new()));

        let s1 = OrderTracker {
            name: "first".into(),
            order: Arc::clone(&order),
        };
        let s2 = OrderTracker {
            name: "second".into(),
            order: Arc::clone(&order),
        };
        let s3 = OrderTracker {
            name: "third".into(),
            order: Arc::clone(&order),
        };

        let mut ctx = CamelContext::new()
            .with_lifecycle(s1)
            .with_lifecycle(s2)
            .with_lifecycle(s3);

        ctx.start().await.unwrap();
        ctx.stop().await.unwrap();

        let stopped = order.lock().unwrap();
        assert_eq!(
            *stopped,
            vec!["third", "second", "first"],
            "services must stop in reverse insertion order"
        );
    }
}

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

    #[derive(Debug, Clone, PartialEq)]
    struct MyConfig {
        value: u32,
    }

    #[test]
    fn test_set_and_get_component_config() {
        let mut ctx = CamelContext::new();
        ctx.set_component_config(MyConfig { value: 42 });
        let got = ctx.get_component_config::<MyConfig>();
        assert_eq!(got, Some(&MyConfig { value: 42 }));
    }

    #[test]
    fn test_get_missing_config_returns_none() {
        let ctx = CamelContext::new();
        assert!(ctx.get_component_config::<MyConfig>().is_none());
    }

    #[test]
    fn test_set_overwrites_previous_config() {
        let mut ctx = CamelContext::new();
        ctx.set_component_config(MyConfig { value: 1 });
        ctx.set_component_config(MyConfig { value: 2 });
        assert_eq!(ctx.get_component_config::<MyConfig>().unwrap().value, 2);
    }
}