zoey-core 0.1.1

ZoeyAI core runtime and types — privacy-first AI agent framework optimized for local models
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
//! AgentRuntime implementation
//!
//! # Thread Safety and Lock Ordering
//!
//! The AgentRuntime uses multiple `RwLock`s for thread-safe access to its components.
//! To prevent deadlocks, locks should be acquired in a consistent order when multiple
//! locks are needed simultaneously.
//!
//! ## Lock Ordering (acquire in this order to prevent deadlocks):
//! 1. `plugins` - Plugin registry
//! 2. `actions` - Action handlers
//! 3. `providers` - Context providers
//! 4. `evaluators` - Post-processing evaluators
//! 5. `services` - Long-lived services
//! 6. `models` - Model handlers
//! 7. `events` - Event handlers
//! 8. `routes` - HTTP routes
//! 9. `settings` - Configuration settings
//! 10. `state_cache` - State caching
//! 11. `adapter` - Database adapter
//! 12. `task_workers` - Background workers
//! 13. `send_handlers` - Message send handlers
//! 14. `message_service` - Message queue
//! 15. `action_results` - Action result cache
//! 16. `current_run_id` - Current execution run
//! 17. `observability` - Metrics and monitoring
//! 18. `zoey_os` - Framework integration
//! 19. `logger` - Logging span
//!
//! ## Poisoned Lock Recovery
//!
//! The runtime includes comprehensive poisoned lock handling with multiple strategies:
//!
//! ### Recovery Strategies
//!
//! 1. **AlwaysFail** (default, recommended): Fails fast on poisoned locks
//! 2. **AlwaysRecover**: Always attempts recovery (backward compatible, risky)
//! 3. **RecoverWithLimit**: Recover up to N times, then fail
//! 4. **RecoverWithBackoff**: Recover with exponential backoff delays
//!
//! ### Usage
//!
//! ```rust,ignore
//! use zoey_core::runtime::{RuntimeOpts, LockRecoveryStrategy, AgentRuntime};
//!
//! // Use fail-fast strategy (default, safest)
//! let opts = RuntimeOpts::default();
//!
//! // Or configure custom strategy
//! let opts = RuntimeOpts::default()
//!     .with_lock_recovery_strategy(
//!         LockRecoveryStrategy::RecoverWithLimit { max_recoveries: 3 }
//!     );
//!
//! let runtime = AgentRuntime::default();
//! // Check lock health
//! let health = runtime.get_lock_health_status();
//! if !health.is_healthy {
//!     println!("Warning: {} locks have been poisoned", health.total_poisoned);
//! }
//!
//! // Get metrics
//! let metrics = runtime.get_lock_poison_metrics();
//! println!("Total poisoned: {}", metrics.total_poisoned);
//! ```
//!
//! ### Metrics
//!
//! The runtime tracks:
//! - Total poisoned lock events
//! - Successful recoveries
//! - Failures (when recovery is disabled)
//! - Per-lock poison counts
//! - Last poison timestamp

use crate::dynamic_prompts::{DynamicPromptExecutor, DynamicPromptOptions, SchemaRow};
use crate::error::Result;
use crate::types::*;
use std::collections::HashMap;
use std::sync::{
    atomic::{AtomicU64, Ordering},
    Arc, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

/// Lock recovery strategy configuration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockRecoveryStrategy {
    /// Always recover from poisoned locks (backward compatible, risky)
    /// May operate on corrupted state
    AlwaysRecover,

    /// Always fail fast on poisoned locks (safest, recommended)
    /// Prevents operating on potentially corrupted state
    AlwaysFail,

    /// Recover up to N times, then fail
    /// Allows some recovery but prevents infinite recovery loops
    RecoverWithLimit { max_recoveries: u64 },

    /// Recover with exponential backoff
    /// Attempts recovery but with increasing delays
    RecoverWithBackoff {
        max_recoveries: u64,
        initial_delay_ms: u64,
    },
}

impl Default for LockRecoveryStrategy {
    fn default() -> Self {
        LockRecoveryStrategy::AlwaysFail
    }
}

/// Metrics for tracking poisoned lock events
#[derive(Debug, Default)]
pub struct LockPoisonMetrics {
    /// Total number of poisoned lock events detected
    pub total_poisoned: AtomicU64,

    /// Number of successful recoveries
    pub recoveries: AtomicU64,

    /// Number of failures (when recovery strategy is fail-fast)
    pub failures: AtomicU64,

    /// Number of read lock poisonings
    pub read_poisoned: AtomicU64,

    /// Number of write lock poisonings
    pub write_poisoned: AtomicU64,

    /// Timestamp of last poisoned lock event
    pub last_poisoned_at: Arc<RwLock<Option<u64>>>,

    /// Map of lock name to poison count
    pub lock_poison_counts: Arc<RwLock<HashMap<String, u64>>>,
}

impl LockPoisonMetrics {
    pub fn new() -> Self {
        Self {
            total_poisoned: AtomicU64::new(0),
            recoveries: AtomicU64::new(0),
            failures: AtomicU64::new(0),
            read_poisoned: AtomicU64::new(0),
            write_poisoned: AtomicU64::new(0),
            last_poisoned_at: Arc::new(RwLock::new(None)),
            lock_poison_counts: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    fn record_poisoned(&self, lock_name: &str, is_write: bool) {
        self.total_poisoned.fetch_add(1, Ordering::Relaxed);
        if is_write {
            self.write_poisoned.fetch_add(1, Ordering::Relaxed);
        } else {
            self.read_poisoned.fetch_add(1, Ordering::Relaxed);
        }

        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        if let Ok(mut last) = self.last_poisoned_at.write() {
            *last = Some(timestamp);
        }

        if let Ok(mut counts) = self.lock_poison_counts.write() {
            *counts.entry(lock_name.to_string()).or_insert(0) += 1;
        }
    }

    fn record_recovery(&self) {
        self.recoveries.fetch_add(1, Ordering::Relaxed);
    }

    fn record_failure(&self) {
        self.failures.fetch_add(1, Ordering::Relaxed);
    }

    pub fn get_summary(&self) -> LockPoisonSummary {
        LockPoisonSummary {
            total_poisoned: self.total_poisoned.load(Ordering::Relaxed),
            recoveries: self.recoveries.load(Ordering::Relaxed),
            failures: self.failures.load(Ordering::Relaxed),
            read_poisoned: self.read_poisoned.load(Ordering::Relaxed),
            write_poisoned: self.write_poisoned.load(Ordering::Relaxed),
            last_poisoned_at: self.last_poisoned_at.read().ok().and_then(|g| *g),
            lock_poison_counts: self
                .lock_poison_counts
                .read()
                .ok()
                .map(|g| g.clone())
                .unwrap_or_default(),
        }
    }
}

/// Summary of lock poison metrics
#[derive(Debug, Clone)]
pub struct LockPoisonSummary {
    pub total_poisoned: u64,
    pub recoveries: u64,
    pub failures: u64,
    pub read_poisoned: u64,
    pub write_poisoned: u64,
    pub last_poisoned_at: Option<u64>,
    pub lock_poison_counts: HashMap<String, u64>,
}

/// Context for lock operations
struct LockContext {
    name: String,
    strategy: LockRecoveryStrategy,
    metrics: Arc<LockPoisonMetrics>,
    recovery_count: Arc<RwLock<HashMap<String, u64>>>,
}

impl LockContext {
    fn new(name: String, strategy: LockRecoveryStrategy, metrics: Arc<LockPoisonMetrics>) -> Self {
        Self {
            name,
            strategy,
            metrics,
            recovery_count: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    fn should_recover(&self) -> bool {
        match self.strategy {
            LockRecoveryStrategy::AlwaysRecover => true,
            LockRecoveryStrategy::AlwaysFail => false,
            LockRecoveryStrategy::RecoverWithLimit { max_recoveries } => {
                let count = self
                    .recovery_count
                    .read()
                    .ok()
                    .and_then(|g| g.get(&self.name).copied())
                    .unwrap_or(0);
                count < max_recoveries
            }
            LockRecoveryStrategy::RecoverWithBackoff { max_recoveries, .. } => {
                let count = self
                    .recovery_count
                    .read()
                    .ok()
                    .and_then(|g| g.get(&self.name).copied())
                    .unwrap_or(0);
                count < max_recoveries
            }
        }
    }

    fn record_recovery_attempt(&self) {
        if let Ok(mut counts) = self.recovery_count.write() {
            *counts.entry(self.name.clone()).or_insert(0) += 1;
        }
    }
}

/// Helper trait for recovering from poisoned locks
///
/// # Poisoned Lock Handling
///
/// A poisoned lock indicates that a thread panicked while holding the lock.
/// This is a serious error condition that suggests data corruption or inconsistent state.
///
/// This implementation provides multiple strategies:
/// - `AlwaysRecover`: Always recover (backward compatible, risky)
/// - `AlwaysFail`: Always fail fast (safest, recommended)
/// - `RecoverWithLimit`: Recover up to N times, then fail
/// - `RecoverWithBackoff`: Recover with exponential backoff
pub trait LockRecovery<T> {
    /// Acquire read lock with context-aware recovery
    fn read_with_context(
        &self,
        ctx: &LockContext,
    ) -> std::result::Result<RwLockReadGuard<'_, T>, crate::ZoeyError>;

    /// Acquire write lock with context-aware recovery
    fn write_with_context(
        &self,
        ctx: &LockContext,
    ) -> std::result::Result<RwLockWriteGuard<'_, T>, crate::ZoeyError>;

    /// Acquire read lock, recovering from poison if necessary
    ///
    /// # Warning
    /// This method recovers from poisoned locks by extracting the inner value.
    /// This may result in reading inconsistent state. Use `read_or_fail()` for safer behavior.
    fn read_or_recover(&self) -> RwLockReadGuard<'_, T>;

    /// Acquire write lock, recovering from poison if necessary
    ///
    /// # Warning
    /// This method recovers from poisoned locks by extracting the inner value.
    /// This may result in writing to inconsistent state. Use `write_or_fail()` for safer behavior.
    fn write_or_recover(&self) -> RwLockWriteGuard<'_, T>;

    /// Acquire read lock, failing fast on poison
    ///
    /// Returns an error if the lock is poisoned, indicating a previous panic.
    /// This is the safer approach as it prevents operating on potentially corrupted state.
    fn read_or_fail(&self) -> std::result::Result<RwLockReadGuard<'_, T>, crate::ZoeyError>;

    /// Acquire write lock, failing fast on poison
    ///
    /// Returns an error if the lock is poisoned, indicating a previous panic.
    /// This is the safer approach as it prevents operating on potentially corrupted state.
    fn write_or_fail(&self) -> std::result::Result<RwLockWriteGuard<'_, T>, crate::ZoeyError>;
}

impl<T> LockRecovery<T> for RwLock<T> {
    fn read_with_context(
        &self,
        ctx: &LockContext,
    ) -> std::result::Result<RwLockReadGuard<'_, T>, crate::ZoeyError> {
        match self.read() {
            Ok(guard) => Ok(guard),
            Err(poisoned) => {
                ctx.metrics.record_poisoned(&ctx.name, false);

                if ctx.should_recover() {
                    ctx.record_recovery_attempt();
                    ctx.metrics.record_recovery();

                    error!(
                        lock_name = %ctx.name,
                        "CRITICAL: Recovered from poisoned read lock. This indicates a previous panic. \
                         State may be inconsistent. Recovery attempt #{}",
                        ctx.recovery_count.read()
                            .ok()
                            .and_then(|g| g.get(&ctx.name).copied())
                            .unwrap_or(0)
                    );

                    #[cfg(any())]
                    {
                        error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
                    }

                    // Apply backoff if configured
                    if let LockRecoveryStrategy::RecoverWithBackoff {
                        initial_delay_ms, ..
                    } = ctx.strategy
                    {
                        let recovery_count = ctx
                            .recovery_count
                            .read()
                            .ok()
                            .and_then(|g| g.get(&ctx.name).copied())
                            .unwrap_or(0);
                        let delay_ms = initial_delay_ms * (1 << recovery_count.min(10)); // Cap at 2^10
                        std::thread::sleep(std::time::Duration::from_millis(delay_ms));
                    }

                    Ok(poisoned.into_inner())
                } else {
                    ctx.metrics.record_failure();

                    error!(
                        lock_name = %ctx.name,
                        "CRITICAL: Attempted to acquire read lock but it is poisoned. \
                         A previous thread panicked while holding this lock. State may be corrupted. \
                         Recovery strategy: {:?}",
                        ctx.strategy
                    );

                    #[cfg(any())]
                    {
                        error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
                    }

                    Err(crate::ZoeyError::runtime(format!(
                        "Lock '{}' is poisoned - a previous thread panicked while holding this lock. \
                         This indicates a serious error. The runtime state may be corrupted. \
                         Recovery strategy: {:?}",
                        ctx.name, ctx.strategy
                    )))
                }
            }
        }
    }

    fn write_with_context(
        &self,
        ctx: &LockContext,
    ) -> std::result::Result<RwLockWriteGuard<'_, T>, crate::ZoeyError> {
        match self.write() {
            Ok(guard) => Ok(guard),
            Err(poisoned) => {
                ctx.metrics.record_poisoned(&ctx.name, true);

                if ctx.should_recover() {
                    ctx.record_recovery_attempt();
                    ctx.metrics.record_recovery();

                    error!(
                        lock_name = %ctx.name,
                        "CRITICAL: Recovered from poisoned write lock. This indicates a previous panic. \
                         State may be inconsistent. Recovery attempt #{}",
                        ctx.recovery_count.read()
                            .ok()
                            .and_then(|g| g.get(&ctx.name).copied())
                            .unwrap_or(0)
                    );

                    #[cfg(any())]
                    {
                        error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
                    }

                    // Apply backoff if configured
                    if let LockRecoveryStrategy::RecoverWithBackoff {
                        initial_delay_ms, ..
                    } = ctx.strategy
                    {
                        let recovery_count = ctx
                            .recovery_count
                            .read()
                            .ok()
                            .and_then(|g| g.get(&ctx.name).copied())
                            .unwrap_or(0);
                        let delay_ms = initial_delay_ms * (1 << recovery_count.min(10)); // Cap at 2^10
                        std::thread::sleep(std::time::Duration::from_millis(delay_ms));
                    }

                    Ok(poisoned.into_inner())
                } else {
                    ctx.metrics.record_failure();

                    error!(
                        lock_name = %ctx.name,
                        "CRITICAL: Attempted to acquire write lock but it is poisoned. \
                         A previous thread panicked while holding this lock. State may be corrupted. \
                         Recovery strategy: {:?}",
                        ctx.strategy
                    );

                    #[cfg(any())]
                    {
                        error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
                    }

                    Err(crate::ZoeyError::runtime(format!(
                        "Lock '{}' is poisoned - a previous thread panicked while holding this lock. \
                         This indicates a serious error. The runtime state may be corrupted. \
                         Recovery strategy: {:?}",
                        ctx.name, ctx.strategy
                    )))
                }
            }
        }
    }

    fn read_or_recover(&self) -> RwLockReadGuard<'_, T> {
        self.read().unwrap_or_else(|poisoned| {
            error!(
                "CRITICAL: Recovered from poisoned read lock. This indicates a previous panic. \
                 State may be inconsistent. Consider using read_or_fail() for safer behavior."
            );
            #[cfg(any())]
            {
                error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
            }
            poisoned.into_inner()
        })
    }

    fn write_or_recover(&self) -> RwLockWriteGuard<'_, T> {
        self.write().unwrap_or_else(|poisoned| {
            error!(
                "CRITICAL: Recovered from poisoned write lock. This indicates a previous panic. \
                 State may be inconsistent. Consider using write_or_fail() for safer behavior."
            );
            #[cfg(any())]
            {
                error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
            }
            poisoned.into_inner()
        })
    }

    fn read_or_fail(&self) -> std::result::Result<RwLockReadGuard<'_, T>, crate::ZoeyError> {
        self.read().map_err(|_poisoned| {
            error!(
                "CRITICAL: Attempted to acquire read lock but it is poisoned. \
                 A previous thread panicked while holding this lock. State may be corrupted."
            );
            #[cfg(any())]
            {
                error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
            }
            crate::ZoeyError::runtime(
                "Lock is poisoned - a previous thread panicked while holding this lock. \
                 This indicates a serious error. The runtime state may be corrupted.",
            )
        })
    }

    fn write_or_fail(&self) -> std::result::Result<RwLockWriteGuard<'_, T>, crate::ZoeyError> {
        self.write().map_err(|_poisoned| {
            error!(
                "CRITICAL: Attempted to acquire write lock but it is poisoned. \
                 A previous thread panicked while holding this lock. State may be corrupted."
            );
            #[cfg(any())]
            {
                error!("Backtrace: {:?}", std::backtrace::Backtrace::capture());
            }
            crate::ZoeyError::runtime(
                "Lock is poisoned - a previous thread panicked while holding this lock. \
                 This indicates a serious error. The runtime state may be corrupted.",
            )
        })
    }
}

/// Agent runtime - main implementation of IAgentRuntime
///
/// See module-level documentation for thread safety guarantees and lock ordering.
pub struct AgentRuntime {
    /// Agent ID
    pub agent_id: Uuid,

    /// Character configuration
    pub character: Character,

    /// Database adapter
    pub(crate) adapter: Arc<RwLock<Option<Arc<dyn IDatabaseAdapter + Send + Sync>>>>,

    /// Registered actions
    pub(crate) actions: Arc<RwLock<Vec<Arc<dyn Action>>>>,

    /// Registered evaluators
    pub(crate) evaluators: Arc<RwLock<Vec<Arc<dyn Evaluator>>>>,

    /// Registered providers
    pub(crate) providers: Arc<RwLock<Vec<Arc<dyn Provider>>>>,

    /// Registered services
    pub(crate) services: Arc<RwLock<HashMap<ServiceTypeName, Vec<Arc<dyn Service>>>>>,
    pub(crate) typed_services: Arc<RwLock<HashMap<String, Arc<dyn Service>>>>,

    /// Model handlers
    pub(crate) models: Arc<RwLock<HashMap<String, Vec<ModelProvider>>>>,

    /// Registered plugins
    plugins: Arc<RwLock<Vec<Arc<dyn Plugin>>>>,

    /// Event handlers
    pub(crate) events: Arc<RwLock<HashMap<String, Vec<EventHandler>>>>,

    /// State cache
    pub(crate) state_cache: Arc<RwLock<HashMap<String, State>>>,

    /// Logger for structured logging
    logger: Arc<RwLock<tracing::Span>>,

    /// Settings
    pub(crate) settings: Arc<RwLock<HashMap<String, serde_json::Value>>>,

    /// Routes
    routes: Arc<RwLock<Vec<Route>>>,

    /// Task workers for background task management
    task_workers: Arc<RwLock<HashMap<String, Arc<dyn TaskWorker>>>>,

    /// Send handlers for custom message sending
    send_handlers: Arc<RwLock<HashMap<String, SendHandlerFunction>>>,

    /// Message service for message queue integration
    message_service: Arc<RwLock<Option<Arc<dyn IMessageService>>>>,

    /// Conversation length for context window management
    pub(crate) conversation_length: usize,

    /// Current run ID
    pub(crate) current_run_id: Arc<RwLock<Option<Uuid>>>,

    /// Action results cache
    pub(crate) action_results: Arc<RwLock<HashMap<Uuid, Vec<ActionResult>>>>,

    /// ZoeyOS instance reference for framework integration
    zoey_os: Arc<RwLock<Option<Arc<dyn std::any::Any + Send + Sync>>>>,

    /// Dynamic prompt executor for schema-based prompting
    dynamic_prompt_executor: Arc<DynamicPromptExecutor>,

    /// Observability for cost tracking and monitoring
    pub observability: Arc<RwLock<Option<Arc<crate::observability::Observability>>>>,

    /// Lock recovery strategy configuration
    lock_recovery_strategy: LockRecoveryStrategy,

    /// Metrics for tracking poisoned lock events
    lock_poison_metrics: Arc<LockPoisonMetrics>,

    /// Training data collector for RLHF and model fine-tuning
    training_collector: Option<Arc<crate::training::TrainingCollector>>,
}

/// Runtime options for constructing an AgentRuntime.
///
/// # Required vs Optional Fields
///
/// While all fields have defaults, you typically want to provide:
/// - `character` - Defines the agent's identity and behavior (defaults to empty character)
/// - `adapter` - Database for persistence (defaults to None, meaning no persistence)
///
/// # Example
///
/// ```rust,ignore
/// use zoey_core::*;
/// use std::sync::Arc;
/// use zoey_plugin_bootstrap::BootstrapPlugin;
/// use zoey_plugin_sql::SqliteAdapter;
///
/// // Minimal setup (uses defaults)
/// let minimal = RuntimeOpts::default();
///
/// // Typical setup with character and database
/// let typical = RuntimeOpts {
///     character: Some(Character {
///         name: "MyAgent".to_string(),
///         ..Default::default()
///     }),
///     adapter: Some(Arc::new(SqliteAdapter::new("sqlite::memory:").await.unwrap())),
///     plugins: vec![Arc::new(BootstrapPlugin::new())],
///     ..Default::default()
/// };
///
/// // Using the builder pattern
/// let built = RuntimeOpts::new()
///     .with_character(Character::default())
///     .with_plugins(vec![Arc::new(BootstrapPlugin::new())]);
/// ```
#[derive(Default)]
pub struct RuntimeOpts {
    /// Agent ID - if not provided, generated deterministically from character name.
    /// Providing an explicit ID ensures stability across restarts.
    pub agent_id: Option<Uuid>,

    /// Character configuration defining the agent's identity, personality, and behavior.
    /// If not provided, a minimal default character is used.
    pub character: Option<Character>,

    /// Initial plugins to register with the runtime.
    /// Plugins provide actions, providers, evaluators, and services.
    /// Empty by default - you typically want at least BootstrapPlugin.
    pub plugins: Vec<Arc<dyn Plugin>>,

    /// Database adapter for persistence.
    /// If None, the runtime operates without persistence (in-memory only).
    /// For production use, provide SqliteAdapter or PostgresAdapter.
    pub adapter: Option<Arc<dyn IDatabaseAdapter + Send + Sync>>,

    /// Additional configuration settings as key-value pairs.
    /// These can be accessed via runtime.get_setting().
    pub settings: Option<HashMap<String, serde_json::Value>>,

    /// Maximum conversation length for context window management.
    /// Defaults to 32 messages if not specified.
    pub conversation_length: Option<usize>,

    /// All available plugins for dependency resolution.
    /// Used when plugins have dependencies on other plugins.
    /// If not provided, only the plugins in `plugins` field are considered.
    pub all_available_plugins: Option<Vec<Arc<dyn Plugin>>>,

    /// Lock recovery strategy
    /// Defaults to `AlwaysFail` for safety
    pub lock_recovery_strategy: Option<LockRecoveryStrategy>,

    /// Test mode: minimal initialization suitable for unit tests
    /// When enabled, background workers and heavy initializations are skipped
    pub test_mode: Option<bool>,
}

impl RuntimeOpts {
    /// Create a new RuntimeOpts with all defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the character configuration.
    pub fn with_character(mut self, character: Character) -> Self {
        self.character = Some(character);
        self
    }

    /// Set the agent ID explicitly.
    pub fn with_agent_id(mut self, agent_id: Uuid) -> Self {
        self.agent_id = Some(agent_id);
        self
    }

    /// Set the database adapter.
    pub fn with_adapter(mut self, adapter: Arc<dyn IDatabaseAdapter + Send + Sync>) -> Self {
        self.adapter = Some(adapter);
        self
    }

    /// Set the plugins to register.
    pub fn with_plugins(mut self, plugins: Vec<Arc<dyn Plugin>>) -> Self {
        self.plugins = plugins;
        self
    }

    /// Add a single plugin.
    pub fn with_plugin(mut self, plugin: Arc<dyn Plugin>) -> Self {
        self.plugins.push(plugin);
        self
    }

    /// Set the conversation length.
    pub fn with_conversation_length(mut self, length: usize) -> Self {
        self.conversation_length = Some(length);
        self
    }

    /// Set configuration settings.
    pub fn with_settings(mut self, settings: HashMap<String, serde_json::Value>) -> Self {
        self.settings = Some(settings);
        self
    }

    /// Set lock recovery strategy
    pub fn with_lock_recovery_strategy(mut self, strategy: LockRecoveryStrategy) -> Self {
        self.lock_recovery_strategy = Some(strategy);
        self
    }
}

impl AgentRuntime {
    /// Create a new agent runtime
    pub async fn new(opts: RuntimeOpts) -> Result<Arc<RwLock<Self>>> {
        let character = opts.character.unwrap_or_default();

        // Generate deterministic UUID from character name
        let agent_id = opts.agent_id.unwrap_or_else(|| {
            character
                .id
                .unwrap_or_else(|| crate::utils::string_to_uuid(&character.name))
        });

        #[cfg(feature = "otel")]
        {
            crate::infrastructure::otel::init_otel();
        }
        let logger_span = tracing::span!(tracing::Level::INFO, "agent", name = %character.name);

        // Get max entries for dynamic prompt cache from env or use default
        let max_cache_entries = std::env::var("DYNAMIC_PROMPT_MAX_ENTRIES")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(1000);

        let lock_recovery_strategy = opts.lock_recovery_strategy.unwrap_or_default();
        let lock_poison_metrics = Arc::new(LockPoisonMetrics::new());

        let mut initial_settings = opts.settings.unwrap_or_default();
        initial_settings
            .entry("ui:streaming".to_string())
            .or_insert(serde_json::json!(true));
        initial_settings
            .entry("ui:provider_racing".to_string())
            .or_insert(serde_json::json!(true));
        initial_settings
            .entry("ui:fast_mode".to_string())
            .or_insert(serde_json::json!(true));
        initial_settings
            .entry("ui:verbosity".to_string())
            .or_insert(serde_json::json!("short"));
        initial_settings
            .entry("ui:avoid_cutoff".to_string())
            .or_insert(serde_json::json!(false));
        let test_mode = opts.test_mode.unwrap_or_else(|| {
            std::env::var("ZOEY_TEST_MODE")
                .ok()
                .and_then(|v| v.parse::<bool>().ok())
                .unwrap_or(false)
        });
        if test_mode {
            initial_settings.insert("ui:streaming".to_string(), serde_json::json!(false));
            initial_settings.insert("ui:provider_racing".to_string(), serde_json::json!(false));
            initial_settings.insert("ui:fast_mode".to_string(), serde_json::json!(true));
        }
        // Create training collector with default config (RLHF enabled)
        let training_config = crate::training::TrainingConfig {
            enabled: true,
            min_quality_score: 0.5,
            max_samples: 10000,
            auto_save_interval: 300, // 5 minutes
            output_dir: std::path::PathBuf::from("./training_data"),
            default_format: crate::training::TrainingFormat::Jsonl,
            include_thoughts: true,
            include_negative_examples: true,
            negative_example_ratio: 0.1,
            enable_rlhf: true,
            auto_label: true,
        };
        let training_collector = Some(Arc::new(crate::training::TrainingCollector::new(training_config)));
        
        let runtime = Self {
            agent_id,
            character,
            adapter: Arc::new(RwLock::new(opts.adapter)),
            actions: Arc::new(RwLock::new(Vec::new())),
            evaluators: Arc::new(RwLock::new(Vec::new())),
            providers: Arc::new(RwLock::new(Vec::new())),
            services: Arc::new(RwLock::new(HashMap::new())),
            typed_services: Arc::new(RwLock::new(HashMap::new())),
            models: Arc::new(RwLock::new(HashMap::new())),
            plugins: Arc::new(RwLock::new(Vec::new())),
            events: Arc::new(RwLock::new(HashMap::new())),
            state_cache: Arc::new(RwLock::new(HashMap::new())),
            logger: Arc::new(RwLock::new(logger_span)),
            settings: Arc::new(RwLock::new(initial_settings)),
            routes: Arc::new(RwLock::new(Vec::new())),
            task_workers: Arc::new(RwLock::new(HashMap::new())),
            send_handlers: Arc::new(RwLock::new(HashMap::new())),
            message_service: Arc::new(RwLock::new(None)),
            conversation_length: opts.conversation_length.unwrap_or(32),
            current_run_id: Arc::new(RwLock::new(None)),
            action_results: Arc::new(RwLock::new(HashMap::new())),
            zoey_os: Arc::new(RwLock::new(None)),
            dynamic_prompt_executor: Arc::new(DynamicPromptExecutor::new(Some(max_cache_entries))),
            observability: Arc::new(RwLock::new(None)),
            lock_recovery_strategy,
            lock_poison_metrics,
            training_collector,
        };

        let runtime_arc = Arc::new(RwLock::new(runtime));

        // Register initial plugins
        if !test_mode {
            for plugin in opts.plugins {
                let mut rt = runtime_arc.write().unwrap();
                rt.register_plugin_internal(plugin).await?;
            }
        }

        debug!("runtime_new:post_plugins");

        if !test_mode {
            {
                let mut rt = runtime_arc.write().unwrap();
                // Register embedding worker
                rt.register_task_worker(
                    "embedding_generation".to_string(),
                    Arc::new(crate::workers::embedding_worker::EmbeddingWorker::new()),
                );
            }
        }

        debug!("runtime_new:return");

        Ok(runtime_arc)
    }

    /// Register a plugin (internal method without lock)
    ///
    /// Acquires locks in order: plugins -> actions -> providers -> evaluators ->
    /// services -> models -> events -> routes (following the documented lock ordering)
    async fn register_plugin_internal(&mut self, plugin: Arc<dyn Plugin>) -> Result<()> {
        info!("Registering plugin: {}", plugin.name());
        debug!("plugin_register:start name={}", plugin.name());

        // Register actions
        for action in plugin.actions() {
            self.actions.write_or_recover().push(action);
        }
        debug!("plugin_register:actions name={}", plugin.name());

        // Register providers
        for provider in plugin.providers() {
            self.providers.write_or_recover().push(provider);
        }
        debug!("plugin_register:providers name={}", plugin.name());

        // Register evaluators
        for evaluator in plugin.evaluators() {
            self.evaluators.write_or_recover().push(evaluator);
        }
        debug!("plugin_register:evaluators name={}", plugin.name());

        // Register services
        for service in plugin.services() {
            let service_type = service.service_type().to_string();
            self.services
                .write_or_recover()
                .entry(service_type.clone())
                .or_insert_with(Vec::new)
                .push(service.clone());
            // Store primary service under its type for direct retrieval
            self.typed_services
                .write_or_recover()
                .entry(service_type)
                .or_insert_with(|| service.clone());
        }
        debug!("plugin_register:services name={}", plugin.name());

        // Register model handlers
        for (model_type, handler) in plugin.models() {
            let provider_info = ModelProvider {
                name: plugin.name().to_string(),
                handler,
                priority: plugin.priority(),
            };

            self.models
                .write_or_recover()
                .entry(model_type)
                .or_insert_with(Vec::new)
                .push(provider_info);
        }

        // Sort model handlers by priority (highest first)
        for handlers in self.models.write_or_recover().values_mut() {
            handlers.sort_by(|a, b| b.priority.cmp(&a.priority));
        }
        debug!("plugin_register:models name={}", plugin.name());

        // Register event handlers
        for (event_type, handlers) in plugin.events() {
            self.events
                .write_or_recover()
                .entry(event_type)
                .or_insert_with(Vec::new)
                .extend(handlers);
        }
        debug!("plugin_register:events name={}", plugin.name());

        // Register routes
        self.routes.write_or_recover().extend(plugin.routes());
        debug!("plugin_register:routes name={}", plugin.name());

        // Add to plugins list
        self.plugins.write_or_recover().push(plugin.clone());
        debug!("plugin_register:done name={}", plugin.name());

        Ok(())
    }

    /// Initialize the runtime
    pub async fn initialize(&mut self, options: InitializeOptions) -> Result<()> {
        info!("Initializing runtime for agent: {}", self.character.name);

        // Initialize database adapter if present
        if let Some(adapter) = self.adapter.read_or_recover().clone() {
            debug!("Initializing database connection...");

            // Check if database is ready
            match adapter.is_ready().await {
                Ok(true) => {
                    info!("✓ Database connection is ready");
                }
                Ok(false) => {
                    warn!("Database is not ready, attempting initialization...");
                    // Note: We can't call initialize() here as it requires &mut
                    // This should be done before passing to runtime
                    return Err(crate::ZoeyError::database(
                        "Database is not ready. Please initialize the adapter before passing to runtime."
                    ));
                }
                Err(e) => {
                    warn!("Failed to check database readiness: {}", e);
                    return Err(crate::ZoeyError::database(format!(
                        "Database readiness check failed: {}",
                        e
                    )));
                }
            }

            // Run plugin migrations if needed
            if !options.skip_migrations {
                debug!("Checking for plugin migrations...");
                let plugins = self.plugins.read_or_recover();

                if !plugins.is_empty() {
                    info!("Running migrations for {} plugin(s)...", plugins.len());

                    // Collect plugin migrations
                    let mut plugin_migrations = Vec::new();
                    for plugin in plugins.iter() {
                        let schema = plugin.schema();
                        plugin_migrations.push(PluginMigration {
                            name: plugin.name().to_string(),
                            schema,
                        });
                    }

                    if !plugin_migrations.is_empty() {
                        match adapter
                            .run_plugin_migrations(
                                plugin_migrations,
                                MigrationOptions {
                                    verbose: false,
                                    force: false,
                                    dry_run: false,
                                },
                            )
                            .await
                        {
                            Ok(_) => info!("✓ Plugin migrations completed successfully"),
                            Err(e) => {
                                warn!("Plugin migration failed: {}", e);
                                return Err(crate::ZoeyError::database(format!(
                                    "Plugin migration failed: {}",
                                    e
                                )));
                            }
                        }
                    } else {
                        debug!("No plugin migrations required");
                    }
                } else {
                    debug!("No plugins registered, skipping migrations");
                }
            } else {
                debug!("Skipping migrations (skip_migrations=true)");
            }

            // Ensure agent is registered in the database
            match adapter.get_agent(self.agent_id).await {
                Ok(Some(_)) => debug!("Agent already exists in database"),
                Ok(None) | Err(_) => {
                    info!("Registering agent '{}' in database...", self.character.name);
                    let agent = crate::types::Agent {
                        id: self.agent_id,
                        name: self.character.name.clone(),
                        character: serde_json::to_value(&self.character).unwrap_or_default(),
                        created_at: Some(chrono::Utc::now().timestamp()),
                        updated_at: None,
                    };
                    match adapter.create_agent(&agent).await {
                        Ok(true) => info!("✓ Agent registered successfully"),
                        Ok(false) => warn!("Agent may already exist (create returned false)"),
                        Err(e) => warn!("Failed to register agent: {} - continuing anyway", e),
                    }
                }
            }
        } else {
            warn!("No database adapter configured - running without persistence");
        }

        // Initialize services
        let service_map = self.services.read_or_recover();
        if !service_map.is_empty() {
            info!("Initializing {} service type(s)...", service_map.len());
            for (service_type, services) in service_map.iter() {
                for _service in services {
                    debug!("Initializing service: {}", service_type);
                    // Service initialization would happen here
                    // Note: Service.initialize() requires &mut, should be done before registration
                }
            }
        }

        info!(
            "✓ Runtime initialization complete for agent '{}'",
            self.character.name
        );
        Ok(())
    }

    /// Compose state from providers
    pub async fn compose_state(
        &self,
        message: &Memory,
        include_list: Option<Vec<String>>,
        only_include: bool,
        skip_cache: bool,
    ) -> Result<State> {
        crate::runtime::RuntimeState::new()
            .compose_state_impl(self, message, include_list, only_include, skip_cache)
            .await
    }

    /// Add embedding to memory and persist via adapter
    pub async fn add_embedding_to_memory(&self, memory: &Memory) -> Result<Memory> {
        if let Some(adapter) = crate::runtime::RuntimeState::get_adapter(self) {
            let mut updated = memory.clone();
            let _ = adapter.update_memory(&updated).await?;
            Ok(updated)
        } else {
            Err(crate::ZoeyError::runtime("No adapter configured"))
        }
    }

    /// Queue embedding generation: resolve TEXT_EMBEDDING provider, generate, and persist
    pub async fn queue_embedding_generation(
        &self,
        memory: &Memory,
        _priority: crate::types::EmbeddingPriority,
    ) -> Result<()> {
        // Resolve provider without holding locks across await
        let provider_opt = {
            let models = self.models.read_or_recover();
            models
                .get(&crate::types::ModelType::TextEmbedding.to_string())
                .and_then(|v| v.first().cloned())
        };
        let adapter_opt = crate::runtime::RuntimeState::get_adapter(self);
        if let (Some(provider), Some(adapter)) = (provider_opt, adapter_opt) {
            let params = crate::types::GenerateTextParams {
                prompt: memory.content.text.clone(),
                max_tokens: None,
                temperature: None,
                top_p: None,
                stop: None,
                model: None,
                frequency_penalty: None,
                presence_penalty: None,
            };
            let mh_params = crate::types::ModelHandlerParams {
                runtime: Arc::new(()),
                params,
            };
            let raw = (provider.handler)(mh_params).await?;
            if let Ok(vec) = serde_json::from_str::<Vec<f32>>(&raw) {
                let mut updated = memory.clone();
                updated.embedding = Some(vec);
                let _ = adapter.update_memory(&updated).await?;
            }
            Ok(())
        } else {
            Err(crate::ZoeyError::runtime(
                "TEXT_EMBEDDING provider or adapter not available",
            ))
        }
    }

    /// Get all memories (messages table)
    pub async fn get_all_memories(&self) -> Result<Vec<Memory>> {
        if let Some(adapter) = crate::runtime::RuntimeState::get_adapter(self) {
            adapter
                .get_memories(crate::types::MemoryQuery {
                    table_name: "messages".to_string(),
                    ..Default::default()
                })
                .await
        } else {
            Err(crate::ZoeyError::runtime("No adapter configured"))
        }
    }

    /// Create a new run ID
    pub fn create_run_id(&self) -> Uuid {
        crate::runtime::lifecycle::create_run_id(self)
    }

    /// Start a run
    pub fn start_run(&mut self) -> Uuid {
        crate::runtime::lifecycle::start_run(self)
    }

    /// End the current run
    pub fn end_run(&mut self) {
        crate::runtime::lifecycle::end_run(self)
    }

    /// Get current run ID
    pub fn get_current_run_id(&self) -> Option<Uuid> {
        crate::runtime::lifecycle::get_current_run_id(self)
    }

    /// Get action results for a message
    pub fn get_action_results(&self, message_id: Uuid) -> Vec<ActionResult> {
        self.action_results
            .read_or_recover()
            .get(&message_id)
            .cloned()
            .unwrap_or_default()
    }

    /// Set action results for a message
    pub fn set_action_results(&self, message_id: Uuid, results: Vec<ActionResult>) {
        self.action_results
            .write_or_recover()
            .insert(message_id, results);
    }
}

impl AgentRuntime {
    /// Get the training data collector for RLHF and model fine-tuning
    pub fn get_training_collector(&self) -> Option<Arc<crate::training::TrainingCollector>> {
        self.training_collector.clone()
    }

    /// Get actions (for inspection)
    pub fn get_actions(&self) -> Vec<Arc<dyn Action>> {
        crate::plugin_system::registry::get_actions(self)
    }

    /// Get providers (for inspection)
    pub fn get_providers(&self) -> Vec<Arc<dyn Provider>> {
        crate::plugin_system::registry::get_providers(self)
    }

    /// Get evaluators (for inspection)
    pub fn get_evaluators(&self) -> Vec<Arc<dyn Evaluator>> {
        crate::plugin_system::registry::get_evaluators(self)
    }

    /// Get service by type
    pub fn get_service(&self, service_type: &str) -> Option<Arc<dyn Service>> {
        crate::runtime::RuntimeState::get_service(self, service_type)
    }

    /// Get typed MultiAgentService if present (disabled)
    // removed duplicate

    /// Get services count
    pub fn get_services_count(&self) -> usize {
        crate::runtime::RuntimeState::get_services_count(self)
    }

    /// Get all services as a HashMap
    pub fn get_all_services(&self) -> HashMap<ServiceTypeName, Vec<Arc<dyn Service>>> {
        crate::runtime::RuntimeState::get_all_services(self)
    }

    /// Get a primary service by type name
    pub fn get_service_by_name(&self, name: &str) -> Option<Arc<dyn Service>> {
        self.typed_services.read_or_recover().get(name).cloned()
    }

    /// Register a provider with the runtime
    pub fn register_provider(&mut self, provider: Arc<dyn Provider>) {
        crate::plugin_system::registry::register_provider(self, provider)
    }

    /// Register an action with the runtime
    pub fn register_action(&mut self, action: Arc<dyn Action>) {
        crate::plugin_system::registry::register_action(self, action)
    }

    /// Register an evaluator with the runtime
    pub fn register_evaluator(&mut self, evaluator: Arc<dyn Evaluator>) {
        crate::plugin_system::registry::register_evaluator(self, evaluator)
    }

    /// Set a configuration setting
    pub fn set_setting(&mut self, key: &str, value: serde_json::Value, _secret: bool) {
        crate::runtime::RuntimeState::set_setting(self, key, value);
    }

    /// Get a configuration setting
    pub fn get_setting(&self, key: &str) -> Option<serde_json::Value> {
        crate::runtime::RuntimeState::get_setting(self, key)
    }

    /// Get a configuration setting as string
    pub fn get_setting_string(&self, key: &str) -> Option<String> {
        crate::runtime::RuntimeState::get_setting_string(self, key)
    }

    /// List settings with a given prefix, returning string values
    pub fn get_settings_with_prefix(&self, prefix: &str) -> Vec<(String, String)> {
        crate::runtime::RuntimeState::get_settings_with_prefix(self, prefix)
    }

    /// Get the logger span
    pub fn logger(&self) -> Arc<RwLock<tracing::Span>> {
        Arc::clone(&self.logger)
    }

    /// Get conversation length
    pub fn get_conversation_length(&self) -> usize {
        crate::runtime::RuntimeState::get_conversation_length(self)
    }

    /// Get message service
    pub fn message_service(&self) -> Option<Arc<dyn IMessageService>> {
        crate::runtime::RuntimeState::message_service(self)
    }

    /// Register a send handler
    pub fn register_send_handler(&mut self, source: String, handler: SendHandlerFunction) {
        self.send_handlers
            .write_or_recover()
            .insert(source, handler);
    }

    /// Get send handler for source
    pub fn get_send_handler(&self, source: &str) -> Option<SendHandlerFunction> {
        self.send_handlers.read_or_recover().get(source).cloned()
    }

    /// Register a task worker
    pub fn register_task_worker(&mut self, name: String, worker: Arc<dyn TaskWorker>) {
        self.task_workers.write_or_recover().insert(name, worker);
    }

    /// Get task worker
    pub fn get_task_worker(&self, name: &str) -> Option<Arc<dyn TaskWorker>> {
        self.task_workers.read_or_recover().get(name).cloned()
    }

    /// Get all task workers
    pub fn get_task_workers(&self) -> HashMap<String, Arc<dyn TaskWorker>> {
        self.task_workers.read_or_recover().clone()
    }

    /// Get ZoeyOS instance
    pub fn zoey_os(&self) -> Option<Arc<dyn std::any::Any + Send + Sync>> {
        self.zoey_os.read_or_recover().clone()
    }

    /// Set ZoeyOS instance
    pub fn set_zoey_os(&mut self, instance: Arc<dyn std::any::Any + Send + Sync>) {
        *self.zoey_os.write_or_recover() = Some(instance);
    }

    /// Get dynamic prompt executor
    pub fn dynamic_prompt_executor(&self) -> Arc<DynamicPromptExecutor> {
        Arc::clone(&self.dynamic_prompt_executor)
    }

    /// Execute dynamic prompt from state
    ///
    /// Schema-driven prompt execution with validation, retries, and metrics.
    /// Replaces ad-hoc `useModel` + manual XML parsing patterns.
    ///
    /// # Arguments
    /// * `state` - Current state for template rendering
    /// * `schema` - Expected output schema
    /// * `prompt_template` - Handlebars template for prompt
    /// * `options` - Execution options (model, retries, validation)
    ///
    /// # Returns
    /// Validated response as HashMap matching schema
    ///
    /// # Example
    /// ```rust,ignore
    /// use zoey_core::*;
    /// use std::collections::HashMap;
    ///
    /// let schema = vec![
    ///     SchemaRow {
    ///         field: "thought".to_string(),
    ///         description: "Agent's reasoning".to_string(),
    ///         field_type: SchemaType::String,
    ///         required: true,
    ///         example: None,
    ///         validation: None,
    ///     },
    ///     SchemaRow {
    ///         field: "action".to_string(),
    ///         description: "Action to take".to_string(),
    ///         field_type: SchemaType::String,
    ///         required: true,
    ///         example: Some("RESPOND".to_string()),
    ///         validation: None,
    ///     },
    /// ];
    ///
    /// let runtime = AgentRuntime::default();
    /// let state = State::new();
    /// let _ = runtime.dynamic_prompt_exec_from_state(
    ///     &state,
    ///     schema,
    ///     "Analyze: {{userMessage}}",
    ///     DynamicPromptOptions::default(),
    /// );
    /// ```
    pub async fn dynamic_prompt_exec_from_state(
        &self,
        state: &State,
        schema: Vec<SchemaRow>,
        prompt_template: &str,
        options: DynamicPromptOptions,
    ) -> Result<HashMap<String, serde_json::Value>> {
        let executor = &self.dynamic_prompt_executor;

        // Create model function that uses runtime's model providers
        let models = self.models.read_or_recover();
        let model_identifier =
            options
                .model
                .clone()
                .unwrap_or_else(|| match options.model_size.as_deref() {
                    Some("small") => "TEXT_SMALL".to_string(),
                    _ => "TEXT_LARGE".to_string(),
                });

        // Get model handler
        let _handlers = models.get(&model_identifier).cloned();

        let model_fn = |_prompt: String, _opts: DynamicPromptOptions| async move {
            // This would call the actual model provider
            // For now, return a placeholder
            Ok(format!(
                "<response><error>Model not connected</error></response>"
            ))
        };

        executor
            .execute_from_state(state, schema, prompt_template, options, model_fn)
            .await
    }

    /// Get dynamic prompt metrics summary
    pub fn get_dynamic_prompt_metrics(&self) -> crate::dynamic_prompts::MetricsSummary {
        self.dynamic_prompt_executor.get_metrics_summary()
    }

    /// Clear dynamic prompt metrics
    pub fn clear_dynamic_prompt_metrics(&self) {
        self.dynamic_prompt_executor.clear_metrics();
    }

    /// Get database adapter
    pub fn get_adapter(&self) -> Option<Arc<dyn IDatabaseAdapter + Send + Sync>> {
        self.adapter.read_or_recover().clone()
    }

    /// Get model providers
    pub fn get_models(&self) -> HashMap<String, Vec<ModelProvider>> {
        crate::plugin_system::registry::get_models(self)
    }

    /// Get lock with context-aware recovery
    ///
    /// This is the recommended method for acquiring locks in the runtime.
    /// It uses the configured recovery strategy and tracks metrics.
    fn get_lock_context(&self, lock_name: &str) -> LockContext {
        LockContext::new(
            lock_name.to_string(),
            self.lock_recovery_strategy,
            Arc::clone(&self.lock_poison_metrics),
        )
    }

    /// Get lock recovery strategy
    pub fn get_lock_recovery_strategy(&self) -> LockRecoveryStrategy {
        self.lock_recovery_strategy
    }

    /// Set lock recovery strategy
    pub fn set_lock_recovery_strategy(&mut self, strategy: LockRecoveryStrategy) {
        self.lock_recovery_strategy = strategy;
        info!("Lock recovery strategy changed to: {:?}", strategy);
    }

    /// Get lock poison metrics summary
    pub fn get_lock_poison_metrics(&self) -> LockPoisonSummary {
        self.lock_poison_metrics.get_summary()
    }

    /// Reset lock poison metrics
    pub fn reset_lock_poison_metrics(&self) {
        self.lock_poison_metrics
            .total_poisoned
            .store(0, Ordering::Relaxed);
        self.lock_poison_metrics
            .recoveries
            .store(0, Ordering::Relaxed);
        self.lock_poison_metrics
            .failures
            .store(0, Ordering::Relaxed);
        self.lock_poison_metrics
            .read_poisoned
            .store(0, Ordering::Relaxed);
        self.lock_poison_metrics
            .write_poisoned
            .store(0, Ordering::Relaxed);
        if let Ok(mut last) = self.lock_poison_metrics.last_poisoned_at.write() {
            *last = None;
        }
        if let Ok(mut counts) = self.lock_poison_metrics.lock_poison_counts.write() {
            counts.clear();
        }
    }

    /// Check if any locks are currently poisoned
    pub fn has_poisoned_locks(&self) -> bool {
        self.lock_poison_metrics
            .total_poisoned
            .load(Ordering::Relaxed)
            > 0
    }

    /// Get health status including lock poison status
    pub fn get_lock_health_status(&self) -> LockHealthStatus {
        let summary = self.lock_poison_metrics.get_summary();
        let mut counts: Vec<(String, u64)> = summary.lock_poison_counts.into_iter().collect();
        counts.sort_by(|a, b| b.1.cmp(&a.1));
        let most_poisoned_locks = counts.into_iter().take(5).collect();
        let is_healthy = summary.total_poisoned == 0 && summary.failures == 0;
        LockHealthStatus {
            is_healthy,
            total_poisoned: summary.total_poisoned,
            recoveries: summary.recoveries,
            failures: summary.failures,
            most_poisoned_locks,
        }
    }
}

use super::lifecycle::LockHealthStatus;

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

    #[tokio::test]
    async fn test_runtime_creation() {
        let opts = RuntimeOpts {
            character: Some(Character {
                name: "TestAgent".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let runtime = AgentRuntime::new(opts).await.unwrap();
        let rt = runtime.read().unwrap();

        // Verify character was set correctly
        assert_eq!(rt.character.name, "TestAgent");

        // Verify agent ID is deterministically generated from name
        let expected_id = crate::utils::string_to_uuid("TestAgent");
        assert_eq!(rt.agent_id, expected_id);

        // Verify default conversation length
        assert_eq!(rt.get_conversation_length(), 32);
    }

    #[tokio::test]
    async fn test_runtime_with_custom_agent_id() {
        let custom_id = Uuid::new_v4();
        let opts = RuntimeOpts {
            agent_id: Some(custom_id),
            character: Some(Character {
                name: "TestAgent".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let runtime = AgentRuntime::new(opts).await.unwrap();
        let rt = runtime.read().unwrap();

        // Custom agent ID should be used
        assert_eq!(rt.agent_id, custom_id);
    }

    #[tokio::test]
    async fn test_state_composition_empty_providers() {
        let opts = RuntimeOpts {
            character: Some(Character {
                name: "TestAgent".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let runtime = AgentRuntime::new(opts).await.unwrap();
        let rt = runtime.read().unwrap();

        let message = Memory {
            id: Uuid::new_v4(),
            entity_id: Uuid::new_v4(),
            agent_id: rt.agent_id,
            room_id: Uuid::new_v4(),
            content: Content::default(),
            embedding: None,
            metadata: None,
            created_at: chrono::Utc::now().timestamp(),
            unique: None,
            similarity: None,
        };

        // With no providers registered, state should be empty
        let state = rt
            .compose_state(&message, None, false, false)
            .await
            .unwrap();
        assert!(
            state.values.is_empty(),
            "State should be empty with no providers registered"
        );
        assert!(
            state.data.is_empty(),
            "Data should be empty with no providers registered"
        );
    }

    #[tokio::test]
    async fn test_settings_management() {
        let opts = RuntimeOpts {
            character: Some(Character {
                name: "TestAgent".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let runtime = AgentRuntime::new(opts).await.unwrap();

        // Set a setting
        {
            let mut rt = runtime.write().unwrap();
            rt.set_setting("test_key", serde_json::json!("test_value"), false);
        }

        // Retrieve the setting
        {
            let rt = runtime.read().unwrap();
            let value = rt.get_setting("test_key");
            assert!(value.is_some(), "Setting should exist");
            assert_eq!(value.unwrap(), serde_json::json!("test_value"));

            // Non-existent setting should return None
            let missing = rt.get_setting("nonexistent");
            assert!(missing.is_none(), "Non-existent setting should return None");
        }
    }

    #[tokio::test]
    async fn test_run_id_management() {
        let opts = RuntimeOpts {
            character: Some(Character {
                name: "TestAgent".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        };

        let runtime = AgentRuntime::new(opts).await.unwrap();

        // Initially no run ID
        {
            let rt = runtime.read().unwrap();
            assert!(rt.get_current_run_id().is_none(), "Initially no run ID");
        }

        // Start a run
        let run_id = {
            let mut rt = runtime.write().unwrap();
            rt.start_run()
        };

        // Verify run ID is set
        {
            let rt = runtime.read().unwrap();
            assert_eq!(
                rt.get_current_run_id(),
                Some(run_id),
                "Run ID should be set"
            );
        }

        // End the run
        {
            let mut rt = runtime.write().unwrap();
            rt.end_run();
        }

        // Verify run ID is cleared
        {
            let rt = runtime.read().unwrap();
            assert!(
                rt.get_current_run_id().is_none(),
                "Run ID should be cleared"
            );
        }
    }
}