ironclaw 0.22.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! Test harness for constructing `AgentDeps` with sensible defaults.
//!
//! Provides:
//! - [`StubLlm`]: A configurable LLM provider that returns a fixed response
//! - [`StubChannel`]: A configurable channel stub with message injection and response capture
//! - [`TestHarnessBuilder`]: Builder for wiring `AgentDeps` with defaults
//! - [`TestHarness`]: The assembled components ready for use in tests
//!
//! # Usage
//!
//! ```rust,no_run
//! use ironclaw::testing::TestHarnessBuilder;
//!
//! #[tokio::test]
//! async fn test_something() {
//!     let harness = TestHarnessBuilder::new().build().await;
//!     // use harness.deps, harness.db, etc.
//! }
//! ```

pub mod credentials;
pub mod fault_injection;

use std::sync::Arc;
use std::sync::Mutex;

use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};

use async_trait::async_trait;
use rust_decimal::Decimal;
use tokio::sync::{Mutex as AsyncMutex, mpsc};

use crate::agent::AgentDeps;
use crate::channels::{
    Channel, ChannelManager, IncomingMessage, MessageStream, OutgoingResponse, StatusUpdate,
};
use crate::db::Database;
use crate::error::{ChannelError, LlmError};
use crate::llm::{
    CompletionRequest, CompletionResponse, FinishReason, LlmProvider, ToolCompletionRequest,
    ToolCompletionResponse,
};
use crate::tools::ToolRegistry;

/// Create a libSQL-backed test database in a temporary directory.
///
/// Returns the database and a `TempDir` guard — the database file is
/// deleted when the guard is dropped.
#[cfg(feature = "libsql")]
pub async fn test_db() -> (Arc<dyn Database>, tempfile::TempDir) {
    use crate::db::libsql::LibSqlBackend;

    let dir = tempfile::tempdir().expect("failed to create temp dir");
    let path = dir.path().join("test.db");
    let backend = LibSqlBackend::new_local(&path)
        .await
        .expect("failed to create test LibSqlBackend");
    backend
        .run_migrations()
        .await
        .expect("failed to run migrations");
    (Arc::new(backend) as Arc<dyn Database>, dir)
}

/// What kind of error the stub should produce when failing.
#[derive(Clone, Copy, Debug)]
pub enum StubErrorKind {
    /// Transient/retryable error (`LlmError::RequestFailed`).
    Transient,
    /// Non-transient error (`LlmError::ContextLengthExceeded`).
    NonTransient,
}

/// A configurable LLM provider stub for tests.
///
/// Supports:
/// - Fixed response content
/// - Call counting via [`calls()`](Self::calls)
/// - Runtime failure toggling via [`set_failing()`](Self::set_failing)
/// - Configurable error kinds (transient vs non-transient)
///
/// Use this in tests instead of creating ad-hoc stub implementations.
pub struct StubLlm {
    model_name: String,
    response: String,
    call_count: AtomicU32,
    should_fail: AtomicBool,
    error_kind: StubErrorKind,
    /// Optional fault injector for fine-grained failure control.
    /// When set, takes precedence over the `should_fail` / `error_kind` fields.
    fault_injector: Option<Arc<fault_injection::FaultInjector>>,
}

impl StubLlm {
    /// Create a new stub that returns the given response.
    pub fn new(response: impl Into<String>) -> Self {
        Self {
            model_name: "stub-model".to_string(),
            response: response.into(),
            call_count: AtomicU32::new(0),
            should_fail: AtomicBool::new(false),
            error_kind: StubErrorKind::Transient,
            fault_injector: None,
        }
    }

    /// Create a stub that always fails with a transient error.
    pub fn failing(name: impl Into<String>) -> Self {
        Self {
            model_name: name.into(),
            response: String::new(),
            call_count: AtomicU32::new(0),
            should_fail: AtomicBool::new(true),
            error_kind: StubErrorKind::Transient,
            fault_injector: None,
        }
    }

    /// Create a stub that always fails with a non-transient error.
    pub fn failing_non_transient(name: impl Into<String>) -> Self {
        Self {
            model_name: name.into(),
            response: String::new(),
            call_count: AtomicU32::new(0),
            should_fail: AtomicBool::new(true),
            error_kind: StubErrorKind::NonTransient,
            fault_injector: None,
        }
    }

    /// Set the model name.
    pub fn with_model_name(mut self, name: impl Into<String>) -> Self {
        self.model_name = name.into();
        self
    }

    /// Get the number of times `complete` or `complete_with_tools` was called.
    pub fn calls(&self) -> u32 {
        self.call_count.load(Ordering::Relaxed)
    }

    /// Attach a fault injector for fine-grained failure control.
    ///
    /// When set, the injector's `next_action()` is consulted on every call,
    /// taking precedence over the `should_fail` / `error_kind` fields.
    pub fn with_fault_injector(mut self, injector: Arc<fault_injection::FaultInjector>) -> Self {
        self.fault_injector = Some(injector);
        self
    }

    /// Toggle whether calls should fail at runtime.
    pub fn set_failing(&self, fail: bool) {
        self.should_fail.store(fail, Ordering::Relaxed);
    }

    /// Check the fault injector or should_fail flag, returning an error if
    /// the call should fail, or None if it should succeed.
    async fn check_faults(&self) -> Option<LlmError> {
        if let Some(ref injector) = self.fault_injector {
            match injector.next_action() {
                fault_injection::FaultAction::Fail(fault) => {
                    return Some(fault.to_llm_error(&self.model_name));
                }
                fault_injection::FaultAction::Delay(duration) => {
                    tokio::time::sleep(duration).await;
                }
                fault_injection::FaultAction::Succeed => {}
            }
        } else if self.should_fail.load(Ordering::Relaxed) {
            return Some(self.make_error());
        }
        None
    }

    fn make_error(&self) -> LlmError {
        match self.error_kind {
            StubErrorKind::Transient => LlmError::RequestFailed {
                provider: self.model_name.clone(),
                reason: "server error".to_string(),
            },
            StubErrorKind::NonTransient => LlmError::ContextLengthExceeded {
                used: 100_000,
                limit: 50_000,
            },
        }
    }
}

impl Default for StubLlm {
    fn default() -> Self {
        Self::new("OK")
    }
}

#[async_trait]
impl LlmProvider for StubLlm {
    fn model_name(&self) -> &str {
        &self.model_name
    }

    fn cost_per_token(&self) -> (Decimal, Decimal) {
        (Decimal::ZERO, Decimal::ZERO)
    }

    async fn complete(&self, _request: CompletionRequest) -> Result<CompletionResponse, LlmError> {
        self.call_count.fetch_add(1, Ordering::Relaxed);
        if let Some(err) = self.check_faults().await {
            return Err(err);
        }
        Ok(CompletionResponse {
            content: self.response.clone(),
            input_tokens: 10,
            output_tokens: 5,
            finish_reason: FinishReason::Stop,
            cache_read_input_tokens: 0,
            cache_creation_input_tokens: 0,
        })
    }

    async fn complete_with_tools(
        &self,
        _request: ToolCompletionRequest,
    ) -> Result<ToolCompletionResponse, LlmError> {
        self.call_count.fetch_add(1, Ordering::Relaxed);
        if let Some(err) = self.check_faults().await {
            return Err(err);
        }
        Ok(ToolCompletionResponse {
            content: Some(self.response.clone()),
            tool_calls: Vec::new(),
            input_tokens: 10,
            output_tokens: 5,
            finish_reason: FinishReason::Stop,
            cache_read_input_tokens: 0,
            cache_creation_input_tokens: 0,
        })
    }
}

/// A configurable channel stub for tests.
///
/// Supports:
/// - Message injection via the returned `mpsc::Sender`
/// - Response capture for assertion
/// - Status update capture
/// - Configurable health check failure
///
/// # Usage
///
/// ```rust,no_run
/// let (channel, sender) = StubChannel::new("test");
/// sender.send(IncomingMessage::new("test", "user1", "hello")).await.unwrap();
/// // ... run agent logic that calls channel.respond() ...
/// let responses = channel.captured_responses();
/// ```
pub struct StubChannel {
    name: String,
    rx: tokio::sync::Mutex<Option<mpsc::Receiver<IncomingMessage>>>,
    responses: Arc<Mutex<Vec<(IncomingMessage, OutgoingResponse)>>>,
    statuses: Arc<Mutex<Vec<StatusUpdate>>>,
    healthy: AtomicBool,
}

impl StubChannel {
    /// Create a new stub channel and its message sender.
    ///
    /// The sender is used by tests to inject messages into the channel's stream.
    /// The channel captures all responses and status updates for later assertion.
    pub fn new(name: impl Into<String>) -> (Self, mpsc::Sender<IncomingMessage>) {
        let (tx, rx) = mpsc::channel(64);
        let channel = Self {
            name: name.into(),
            rx: tokio::sync::Mutex::new(Some(rx)),
            responses: Arc::new(Mutex::new(Vec::new())),
            statuses: Arc::new(Mutex::new(Vec::new())),
            healthy: AtomicBool::new(true),
        };
        (channel, tx)
    }

    /// Get all captured (message, response) pairs.
    pub fn captured_responses(&self) -> Vec<(IncomingMessage, OutgoingResponse)> {
        self.responses.lock().expect("poisoned").clone()
    }

    /// Get a shared handle to the response capture list.
    ///
    /// Call this *before* moving the channel into a `ChannelManager`,
    /// since `add()` takes ownership.
    pub fn captured_responses_handle(
        &self,
    ) -> Arc<Mutex<Vec<(IncomingMessage, OutgoingResponse)>>> {
        Arc::clone(&self.responses)
    }

    /// Get all captured status updates.
    pub fn captured_statuses(&self) -> Vec<StatusUpdate> {
        self.statuses.lock().expect("poisoned").clone()
    }

    /// Get a shared handle to the status capture list.
    pub fn captured_statuses_handle(&self) -> Arc<Mutex<Vec<StatusUpdate>>> {
        Arc::clone(&self.statuses)
    }

    /// Set whether `health_check()` succeeds or fails.
    pub fn set_healthy(&self, healthy: bool) {
        self.healthy.store(healthy, Ordering::Relaxed);
    }
}

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

    async fn start(&self) -> Result<MessageStream, ChannelError> {
        let rx = self
            .rx
            .lock()
            .await
            .take()
            .ok_or_else(|| ChannelError::StartupFailed {
                name: self.name.clone(),
                reason: "start() already called".to_string(),
            })?;
        let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
        Ok(Box::pin(stream))
    }

    async fn respond(
        &self,
        msg: &IncomingMessage,
        response: OutgoingResponse,
    ) -> Result<(), ChannelError> {
        self.responses
            .lock()
            .expect("poisoned")
            .push((msg.clone(), response));
        Ok(())
    }

    async fn send_status(
        &self,
        status: StatusUpdate,
        _metadata: &serde_json::Value,
    ) -> Result<(), ChannelError> {
        self.statuses.lock().expect("poisoned").push(status);
        Ok(())
    }

    async fn health_check(&self) -> Result<(), ChannelError> {
        if self.healthy.load(Ordering::Relaxed) {
            Ok(())
        } else {
            Err(ChannelError::HealthCheckFailed {
                name: self.name.clone(),
            })
        }
    }
}

/// Captured broadcast deliveries keyed by the target user or chat identifier.
pub type BroadcastCapture = Arc<AsyncMutex<Vec<(String, OutgoingResponse)>>>;

/// A lightweight channel double that only records `broadcast()` traffic.
///
/// This is useful for unit tests that need to assert message routing without
/// spinning up a full interactive channel harness.
pub struct RecordingBroadcastChannel {
    name: &'static str,
    captures: BroadcastCapture,
}

impl RecordingBroadcastChannel {
    pub fn new(name: &'static str) -> (Self, BroadcastCapture) {
        let captures = Arc::new(AsyncMutex::new(Vec::new()));
        (
            Self {
                name,
                captures: Arc::clone(&captures),
            },
            captures,
        )
    }
}

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

    async fn start(&self) -> Result<MessageStream, ChannelError> {
        let (_tx, rx) = mpsc::channel::<IncomingMessage>(1);
        Ok(Box::pin(tokio_stream::wrappers::ReceiverStream::new(rx)))
    }

    async fn respond(
        &self,
        _msg: &IncomingMessage,
        _response: OutgoingResponse,
    ) -> Result<(), ChannelError> {
        Ok(())
    }

    async fn send_status(
        &self,
        _status: StatusUpdate,
        _metadata: &serde_json::Value,
    ) -> Result<(), ChannelError> {
        Ok(())
    }

    async fn broadcast(
        &self,
        user_id: &str,
        response: OutgoingResponse,
    ) -> Result<(), ChannelError> {
        self.captures
            .lock()
            .await
            .push((user_id.to_string(), response));
        Ok(())
    }

    async fn health_check(&self) -> Result<(), ChannelError> {
        Ok(())
    }
}

/// Assembled test components.
pub struct TestHarness {
    /// The agent dependencies, ready for use.
    pub deps: AgentDeps,
    /// Direct reference to the database (as `Arc<dyn Database>`).
    pub db: Arc<dyn Database>,
    /// Stub channel sender + manager, present if `with_stub_channel()` was called.
    pub channel: Option<(mpsc::Sender<IncomingMessage>, ChannelManager)>,
    /// Temp directory guard — keeps the test database alive. Dropped
    /// automatically when the harness goes out of scope.
    #[cfg(feature = "libsql")]
    _temp_dir: tempfile::TempDir,
}

/// Builder for constructing a [`TestHarness`] with sensible defaults.
///
/// All defaults are designed to work without any external services:
/// - Database: libSQL in a temp directory (real SQL, FTS5, no network)
/// - LLM: `StubLlm` returning "OK"
/// - Safety: permissive config
/// - Tools: builtin tools registered
/// - Hooks: empty registry
/// - Cost guard: no limits
pub struct TestHarnessBuilder {
    db: Option<Arc<dyn Database>>,
    llm: Option<Arc<dyn LlmProvider>>,
    tools: Option<Arc<ToolRegistry>>,
    stub_channel: bool,
}

impl TestHarnessBuilder {
    /// Create a new builder with all defaults.
    pub fn new() -> Self {
        Self {
            db: None,
            llm: None,
            tools: None,
            stub_channel: false,
        }
    }

    /// Override the database backend.
    pub fn with_db(mut self, db: Arc<dyn Database>) -> Self {
        self.db = Some(db);
        self
    }

    /// Override the LLM provider.
    pub fn with_llm(mut self, llm: Arc<dyn LlmProvider>) -> Self {
        self.llm = Some(llm);
        self
    }

    /// Override the tool registry.
    pub fn with_tools(mut self, tools: Arc<ToolRegistry>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Include a `StubChannel` wired into a `ChannelManager`.
    ///
    /// The harness will expose the sender (for injecting messages) and
    /// the manager (for routing responses) via [`TestHarness::channel`].
    pub fn with_stub_channel(mut self) -> Self {
        self.stub_channel = true;
        self
    }

    /// Build the harness with defaults applied.
    #[cfg(feature = "libsql")]
    pub async fn build(self) -> TestHarness {
        use crate::agent::cost_guard::{CostGuard, CostGuardConfig};
        use crate::config::{SafetyConfig, SkillsConfig};
        use crate::hooks::HookRegistry;
        use crate::safety::SafetyLayer;

        let (db, temp_dir) = if let Some(db) = self.db {
            // Caller provided a DB; create a dummy temp dir to satisfy the struct.
            let dir = tempfile::tempdir().expect("failed to create temp dir");
            (db, dir)
        } else {
            test_db().await
        };

        let llm: Arc<dyn LlmProvider> = self.llm.unwrap_or_else(|| Arc::new(StubLlm::default()));

        let tools = self.tools.unwrap_or_else(|| {
            let t = Arc::new(ToolRegistry::new());
            t.register_builtin_tools();
            t
        });

        let safety = Arc::new(SafetyLayer::new(&SafetyConfig {
            max_output_length: 100_000,
            injection_check_enabled: false,
        }));

        let hooks = Arc::new(HookRegistry::new());

        let cost_guard = Arc::new(CostGuard::new(CostGuardConfig {
            max_cost_per_day_cents: None,
            max_actions_per_hour: None,
        }));

        let channel = if self.stub_channel {
            let (stub, sender) = StubChannel::new("stub");
            let manager = ChannelManager::new();
            manager.add(Box::new(stub)).await;
            Some((sender, manager))
        } else {
            None
        };

        let deps = AgentDeps {
            owner_id: "default".to_string(),
            store: Some(Arc::clone(&db)),
            llm,
            cheap_llm: None,
            safety,
            tools,
            workspace: None,
            extension_manager: None,
            skill_registry: None,
            skill_catalog: None,
            skills_config: SkillsConfig::default(),
            hooks,
            cost_guard,
            sse_tx: None,
            http_interceptor: None,
            transcription: None,
            document_extraction: None,
            sandbox_readiness: crate::agent::routine_engine::SandboxReadiness::DisabledByConfig,
            builder: None,
            llm_backend: "nearai".to_string(),
        };

        TestHarness {
            deps,
            db,
            channel,
            _temp_dir: temp_dir,
        }
    }
}

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

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

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_harness_builds_with_defaults() {
        let harness = TestHarnessBuilder::new().build().await;
        assert!(harness.deps.store.is_some());
        assert_eq!(harness.deps.llm.model_name(), "stub-model");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_harness_custom_llm() {
        let custom_llm = Arc::new(StubLlm::new("custom response").with_model_name("my-model"));
        let harness = TestHarnessBuilder::new().with_llm(custom_llm).build().await;
        assert_eq!(harness.deps.llm.model_name(), "my-model");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_harness_db_works() {
        let harness = TestHarnessBuilder::new().build().await;

        let id = harness
            .db
            .create_conversation("test", "user1", None)
            .await
            .expect("create conversation");
        assert!(!id.is_nil());
    }

    // === QA Plan P1 - 2.2: Turn persistence round-trip tests ===

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_conversation_message_round_trip() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = db
            .create_conversation("tui", "alice", None)
            .await
            .expect("create conversation");

        // Add several messages in order.
        let m1 = db
            .add_conversation_message(conv_id, "user", "Hello!")
            .await
            .expect("add msg 1");
        let m2 = db
            .add_conversation_message(conv_id, "assistant", "Hi there!")
            .await
            .expect("add msg 2");
        let m3 = db
            .add_conversation_message(conv_id, "user", "How are you?")
            .await
            .expect("add msg 3");

        // IDs must be unique.
        assert_ne!(m1, m2);
        assert_ne!(m2, m3);

        // List messages and verify content + ordering.
        let msgs = db
            .list_conversation_messages(conv_id)
            .await
            .expect("list messages");
        assert_eq!(msgs.len(), 3);
        assert_eq!(msgs[0].role, "user");
        assert_eq!(msgs[0].content, "Hello!");
        assert_eq!(msgs[1].role, "assistant");
        assert_eq!(msgs[1].content, "Hi there!");
        assert_eq!(msgs[2].role, "user");
        assert_eq!(msgs[2].content, "How are you?");

        // Timestamps should be monotonically non-decreasing.
        assert!(msgs[0].created_at <= msgs[1].created_at);
        assert!(msgs[1].created_at <= msgs[2].created_at);
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_conversation_metadata_persistence() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = db
            .create_conversation("web", "bob", None)
            .await
            .expect("create conversation");

        // Initially no metadata.
        let meta = db
            .get_conversation_metadata(conv_id)
            .await
            .expect("get metadata");
        // May be None or empty object depending on backend.
        if let Some(m) = &meta {
            assert!(m.is_null() || m.as_object().is_none_or(|o| o.is_empty()));
        }

        // Set a metadata field.
        db.update_conversation_metadata_field(
            conv_id,
            "thread_type",
            &serde_json::json!("assistant"),
        )
        .await
        .expect("set thread_type");

        // Read it back.
        let meta = db
            .get_conversation_metadata(conv_id)
            .await
            .expect("get metadata after update")
            .expect("metadata should exist");
        assert_eq!(meta["thread_type"], "assistant");

        // Update with a second field — first field should still be there.
        db.update_conversation_metadata_field(conv_id, "model", &serde_json::json!("gpt-4"))
            .await
            .expect("set model");

        let meta = db
            .get_conversation_metadata(conv_id)
            .await
            .expect("get metadata after second update")
            .expect("metadata should exist");
        assert_eq!(meta["thread_type"], "assistant");
        assert_eq!(meta["model"], "gpt-4");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_conversation_belongs_to_user() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = db
            .create_conversation("tui", "alice", None)
            .await
            .expect("create conversation");

        // Owner check should pass.
        assert!(
            db.conversation_belongs_to_user(conv_id, "alice")
                .await
                .expect("belongs check")
        );

        // Different user should NOT own it.
        assert!(
            !db.conversation_belongs_to_user(conv_id, "mallory")
                .await
                .expect("belongs check other user")
        );
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_ensure_conversation_idempotent() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = uuid::Uuid::new_v4();

        // ensure_conversation should create the row.
        assert!(
            db.ensure_conversation(conv_id, "web", "carol", None)
                .await
                .expect("ensure first"),
            "first ensure_conversation should create the row"
        );

        // Calling again with the same ID should not error.
        assert!(
            db.ensure_conversation(conv_id, "web", "carol", None)
                .await
                .expect("ensure second (idempotent)"),
            "second ensure_conversation should touch owned row"
        );

        // Should be able to add messages to it.
        let msg_id = db
            .add_conversation_message(conv_id, "user", "test message")
            .await
            .expect("add message to ensured conversation");
        assert!(!msg_id.is_nil());

        // Verify the message is there.
        let msgs = db
            .list_conversation_messages(conv_id)
            .await
            .expect("list messages");
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].content, "test message");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_ensure_conversation_foreign_conflict_does_not_touch_last_activity() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = db
            .create_conversation("web", "alice", None)
            .await
            .expect("create conversation");

        let before = db
            .list_conversations_all_channels("alice", 10)
            .await
            .expect("list conversations before foreign ensure")
            .into_iter()
            .find(|c| c.id == conv_id)
            .expect("conversation must exist before foreign ensure")
            .last_activity;

        tokio::time::sleep(std::time::Duration::from_millis(25)).await;

        assert!(
            !db.ensure_conversation(conv_id, "web", "mallory", None)
                .await
                .expect("foreign ensure should not error"),
            "foreign ensure_conversation should report not ensured"
        );

        let after = db
            .list_conversations_all_channels("alice", 10)
            .await
            .expect("list conversations after foreign ensure")
            .into_iter()
            .find(|c| c.id == conv_id)
            .expect("conversation must still exist after foreign ensure")
            .last_activity;

        assert_eq!(
            after, before,
            "foreign ensure_conversation should not mutate last_activity"
        );
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_paginated_messages() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let conv_id = db
            .create_conversation("tui", "dave", None)
            .await
            .expect("create conversation");

        // Add messages.
        for i in 0..5 {
            db.add_conversation_message(conv_id, "user", &format!("msg {i}"))
                .await
                .expect("add message");
        }

        // First page with limit 3, no cursor. Returns newest-first.
        let (page1, has_more) = db
            .list_conversation_messages_paginated(conv_id, None, 3)
            .await
            .expect("page 1");
        assert_eq!(page1.len(), 3, "first page should have 3 messages");
        assert!(has_more, "should indicate more messages exist");

        // Verify all messages can be retrieved with a large limit.
        let (all, _) = db
            .list_conversation_messages_paginated(conv_id, None, 100)
            .await
            .expect("all messages");
        assert_eq!(all.len(), 5);

        // Messages are returned oldest-first (ascending created_at).
        for w in all.windows(2) {
            assert!(
                w[0].created_at <= w[1].created_at,
                "messages should be in ascending created_at order"
            );
        }
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_conversations_with_preview() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Create two conversations for the same user.
        let c1 = db
            .create_conversation("tui", "eve", None)
            .await
            .expect("create c1");
        db.add_conversation_message(c1, "user", "First conversation opener")
            .await
            .expect("add msg to c1");

        let c2 = db
            .create_conversation("tui", "eve", None)
            .await
            .expect("create c2");
        db.add_conversation_message(c2, "user", "Second conversation opener")
            .await
            .expect("add msg to c2");

        // List with preview.
        let summaries = db
            .list_conversations_with_preview("eve", "tui", 10)
            .await
            .expect("list with preview");

        assert_eq!(summaries.len(), 2);
        // Both should have message_count >= 1.
        for s in &summaries {
            assert!(s.message_count >= 1);
        }
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_job_action_persistence() {
        use crate::context::{ActionRecord, JobContext, JobState};

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let ctx = JobContext::with_user("user1", "Do something", "test task");

        let job_id = ctx.job_id;

        // Save job.
        db.save_job(&ctx).await.expect("save job");

        // Get job back.
        let fetched = db.get_job(job_id).await.expect("get job");
        assert!(fetched.is_some());
        let fetched = fetched.unwrap();
        assert_eq!(fetched.job_id, job_id);

        // Save an action.
        let action = ActionRecord {
            id: uuid::Uuid::new_v4(),
            sequence: 1,
            tool_name: "echo".to_string(),
            input: serde_json::json!({"message": "hello"}),
            output_raw: Some("hello".to_string()),
            output_sanitized: None,
            sanitization_warnings: vec![],
            cost: None,
            duration: std::time::Duration::from_millis(42),
            success: true,
            error: None,
            executed_at: chrono::Utc::now(),
        };
        db.save_action(job_id, &action).await.expect("save action");

        // Retrieve actions.
        let actions = db.get_job_actions(job_id).await.expect("get actions");
        assert_eq!(actions.len(), 1);
        assert_eq!(actions[0].tool_name, "echo");
        assert_eq!(actions[0].output_raw, Some("hello".to_string()));
        assert!(actions[0].success);
        assert_eq!(actions[0].duration, std::time::Duration::from_millis(42));

        // Update job status.
        db.update_job_status(job_id, JobState::Completed, None)
            .await
            .expect("update status");

        let updated = db
            .get_job(job_id)
            .await
            .expect("get updated job")
            .expect("job should exist");
        assert!(matches!(updated.state, JobState::Completed));
    }

    #[tokio::test]
    async fn test_stub_llm_complete() {
        let llm = StubLlm::new("hello world");
        let response = llm
            .complete(CompletionRequest::new(vec![]))
            .await
            .expect("complete");
        assert_eq!(response.content, "hello world");
        assert_eq!(response.finish_reason, FinishReason::Stop);
    }

    #[tokio::test]
    async fn test_stub_channel_inject_and_capture() {
        use futures::StreamExt;

        let (channel, sender) = StubChannel::new("test-channel");

        // Start the channel to get the message stream
        let mut stream = channel.start().await.expect("start failed");

        // Inject a message
        sender
            .send(IncomingMessage::new("test-channel", "user1", "hello"))
            .await
            .expect("send failed");

        // Read it from the stream
        let msg = stream.next().await.expect("stream ended");
        assert_eq!(msg.content, "hello");
        assert_eq!(msg.user_id, "user1");
        assert_eq!(msg.channel, "test-channel");

        // Send a response and verify it was captured
        let response = OutgoingResponse::text("world");
        channel
            .respond(&msg, response)
            .await
            .expect("respond failed");

        let captured = channel.captured_responses();
        assert_eq!(captured.len(), 1);
        assert_eq!(captured[0].1.content, "world");
    }

    #[tokio::test]
    async fn test_stub_channel_health_check() {
        let (channel, _sender) = StubChannel::new("healthy");
        channel.health_check().await.expect("health check failed");

        channel.set_healthy(false);
        assert!(channel.health_check().await.is_err());
    }

    // === Database CRUD coverage for untested trait methods ===

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_settings_crud() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Initially no setting
        let val = db.get_setting("user1", "theme").await.expect("get");
        assert!(val.is_none());

        // Set a value
        db.set_setting("user1", "theme", &serde_json::json!("dark"))
            .await
            .expect("set");

        // Read it back
        let val = db
            .get_setting("user1", "theme")
            .await
            .expect("get")
            .expect("should exist");
        assert_eq!(val, serde_json::json!("dark"));

        // Update it
        db.set_setting("user1", "theme", &serde_json::json!("light"))
            .await
            .expect("set update");
        let val = db
            .get_setting("user1", "theme")
            .await
            .expect("get")
            .expect("should exist");
        assert_eq!(val, serde_json::json!("light"));

        // List settings
        let all = db.list_settings("user1").await.expect("list");
        assert_eq!(all.len(), 1);

        // Delete
        let deleted = db.delete_setting("user1", "theme").await.expect("delete");
        assert!(deleted);

        let val = db.get_setting("user1", "theme").await.expect("get");
        assert!(val.is_none());

        // Delete non-existent
        let deleted = db.delete_setting("user1", "theme").await.expect("delete");
        assert!(!deleted);
    }

    #[tokio::test]
    async fn test_harness_with_channel() {
        let harness = TestHarnessBuilder::new().with_stub_channel().build().await;

        let (sender, channel_manager) =
            harness.channel.as_ref().expect("channel should be present");

        // Inject a message via sender
        sender
            .send(IncomingMessage::new("stub", "user1", "test message"))
            .await
            .expect("send failed");

        // Verify channel is registered in the manager
        let names = channel_manager.channel_names().await;
        assert!(names.contains(&"stub".to_string()));
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_settings_bulk_operations() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Initially no settings
        let has = db.has_settings("bulk_user").await.expect("has_settings");
        assert!(!has);

        // Set all settings at once
        let mut settings = std::collections::HashMap::new();
        settings.insert("key1".to_string(), serde_json::json!("value1"));
        settings.insert("key2".to_string(), serde_json::json!(42));
        db.set_all_settings("bulk_user", &settings)
            .await
            .expect("set_all");

        // Has settings should now be true
        let has = db.has_settings("bulk_user").await.expect("has_settings");
        assert!(has);

        // Get all settings
        let all = db.get_all_settings("bulk_user").await.expect("get_all");
        assert_eq!(all.len(), 2);
        assert_eq!(all["key1"], serde_json::json!("value1"));
        assert_eq!(all["key2"], serde_json::json!(42));

        // Get full setting row
        let full = db
            .get_setting_full("bulk_user", "key1")
            .await
            .expect("get_full")
            .expect("should exist");
        assert_eq!(full.key, "key1");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_tool_failure_tracking() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Record some failures
        db.record_tool_failure("bad_tool", "connection refused")
            .await
            .expect("record 1");
        db.record_tool_failure("bad_tool", "timeout")
            .await
            .expect("record 2");
        db.record_tool_failure("bad_tool", "parse error")
            .await
            .expect("record 3");

        // Get broken tools (threshold = 2, should include bad_tool with 3 failures)
        let broken = db.get_broken_tools(2).await.expect("get broken");
        assert!(!broken.is_empty());
        let found = broken.iter().find(|b| b.name == "bad_tool");
        assert!(found.is_some(), "bad_tool should be in broken tools list");

        // Mark as repaired
        db.mark_tool_repaired("bad_tool")
            .await
            .expect("mark repaired");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_routine_crud() {
        use crate::agent::routine::{
            NotifyConfig, Routine, RoutineAction, RoutineGuardrails, RoutineRun, RunStatus, Trigger,
        };

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let routine_id = uuid::Uuid::new_v4();
        let routine = Routine {
            id: routine_id,
            name: "test-routine".to_string(),
            description: "A test routine".to_string(),
            user_id: "user1".to_string(),
            enabled: true,
            trigger: Trigger::Cron {
                schedule: "0 * * * *".to_string(),
                timezone: None,
            },
            action: RoutineAction::Lightweight {
                prompt: "Check status".to_string(),
                context_paths: vec![],
                max_tokens: 500,
                use_tools: false,
                max_tool_rounds: 3,
            },
            guardrails: RoutineGuardrails {
                cooldown: std::time::Duration::from_secs(60),
                max_concurrent: 1,
                dedup_window: None,
            },
            notify: NotifyConfig {
                channel: None,
                user: Some("user1".to_string()),
                on_attention: true,
                on_failure: true,
                on_success: false,
            },
            last_run_at: None,
            next_fire_at: None,
            run_count: 0,
            consecutive_failures: 0,
            state: serde_json::json!({}),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };

        // Create
        db.create_routine(&routine).await.expect("create routine");

        // Get by ID
        let fetched = db
            .get_routine(routine_id)
            .await
            .expect("get routine")
            .expect("should exist");
        assert_eq!(fetched.name, "test-routine");
        assert!(fetched.enabled);

        // Get by name
        let by_name = db
            .get_routine_by_name("user1", "test-routine")
            .await
            .expect("get by name")
            .expect("should exist");
        assert_eq!(by_name.id, routine_id);

        // List routines for user
        let list = db.list_routines("user1").await.expect("list routines");
        assert_eq!(list.len(), 1);

        // List all routines
        let all = db.list_all_routines().await.expect("list all");
        assert!(!all.is_empty());

        // Update routine (disable + change description)
        let mut updated = fetched;
        updated.enabled = false;
        updated.description = "Updated description".to_string();
        db.update_routine(&updated).await.expect("update routine");

        let re_fetched = db
            .get_routine(routine_id)
            .await
            .expect("get")
            .expect("exists");
        assert!(!re_fetched.enabled);
        assert_eq!(re_fetched.description, "Updated description");

        // Create a routine run
        let run_id = uuid::Uuid::new_v4();
        let run = RoutineRun {
            id: run_id,
            routine_id,
            trigger_type: "cron".to_string(),
            trigger_detail: Some("0 * * * *".to_string()),
            started_at: chrono::Utc::now(),
            completed_at: None,
            status: RunStatus::Running,
            result_summary: None,
            tokens_used: None,
            job_id: None,
            created_at: chrono::Utc::now(),
        };
        db.create_routine_run(&run).await.expect("create run");

        // List runs
        let runs = db
            .list_routine_runs(routine_id, 10)
            .await
            .expect("list runs");
        assert_eq!(runs.len(), 1);
        assert!(matches!(runs[0].status, RunStatus::Running));

        // Complete the run
        db.complete_routine_run(run_id, RunStatus::Ok, Some("All good"), Some(150))
            .await
            .expect("complete run");

        let runs = db
            .list_routine_runs(routine_id, 10)
            .await
            .expect("list runs after complete");
        assert!(matches!(runs[0].status, RunStatus::Ok));

        // Delete
        let deleted = db.delete_routine(routine_id).await.expect("delete");
        assert!(deleted);

        // Delete non-existent
        let deleted = db.delete_routine(routine_id).await.expect("delete again");
        assert!(!deleted);
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_routine_runtime_update() {
        use crate::agent::routine::{
            NotifyConfig, Routine, RoutineAction, RoutineGuardrails, Trigger,
        };

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let routine_id = uuid::Uuid::new_v4();
        let routine = Routine {
            id: routine_id,
            name: "runtime-test".to_string(),
            description: "Test runtime update".to_string(),
            user_id: "user1".to_string(),
            enabled: true,
            trigger: Trigger::Manual,
            action: RoutineAction::Lightweight {
                prompt: "test".to_string(),
                context_paths: vec![],
                max_tokens: 100,
                use_tools: false,
                max_tool_rounds: 3,
            },
            guardrails: RoutineGuardrails {
                cooldown: std::time::Duration::from_secs(0),
                max_concurrent: 1,
                dedup_window: None,
            },
            notify: NotifyConfig {
                channel: None,
                user: Some("user1".to_string()),
                on_attention: false,
                on_failure: false,
                on_success: false,
            },
            last_run_at: None,
            next_fire_at: None,
            run_count: 0,
            consecutive_failures: 0,
            state: serde_json::json!({}),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };
        db.create_routine(&routine).await.expect("create");

        let now = chrono::Utc::now();
        db.update_routine_runtime(
            routine_id,
            now,
            Some(now + chrono::TimeDelta::seconds(3600)),
            5,
            2,
            &serde_json::json!({"last_result": "ok"}),
        )
        .await
        .expect("update runtime");

        let fetched = db
            .get_routine(routine_id)
            .await
            .expect("get")
            .expect("exists");
        assert_eq!(fetched.run_count, 5);
        assert_eq!(fetched.consecutive_failures, 2);
        assert!(fetched.last_run_at.is_some());
        assert!(fetched.next_fire_at.is_some());

        // Cleanup
        db.delete_routine(routine_id).await.expect("delete");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_llm_call_recording() {
        use crate::history::LlmCallRecord;

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let record = LlmCallRecord {
            job_id: None,
            conversation_id: None,
            provider: "openai",
            model: "gpt-4",
            input_tokens: 100,
            output_tokens: 50,
            cost: Decimal::new(5, 3), // 0.005
            purpose: Some("test"),
        };

        let call_id = db.record_llm_call(&record).await.expect("record llm call");
        assert!(!call_id.is_nil());
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_sandbox_job_lifecycle() {
        use crate::history::SandboxJobRecord;

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let job_id = uuid::Uuid::new_v4();
        let job = SandboxJobRecord {
            id: job_id,
            task: "Build a test tool".to_string(),
            status: "creating".to_string(),
            user_id: "user1".to_string(),
            project_dir: "/workspace/test".to_string(),
            success: None,
            failure_reason: None,
            created_at: chrono::Utc::now(),
            started_at: None,
            completed_at: None,
            credential_grants_json: "[]".to_string(),
        };

        // Create
        db.save_sandbox_job(&job).await.expect("save sandbox job");

        // Get
        let fetched = db
            .get_sandbox_job(job_id)
            .await
            .expect("get")
            .expect("should exist");
        assert_eq!(fetched.task, "Build a test tool");
        assert_eq!(fetched.status, "creating");

        // Update status to running
        db.update_sandbox_job_status(
            job_id,
            "running",
            None,
            None,
            Some(chrono::Utc::now()),
            None,
        )
        .await
        .expect("update to running");

        // Update to completed
        db.update_sandbox_job_status(
            job_id,
            "completed",
            Some(true),
            Some("Done"),
            None,
            Some(chrono::Utc::now()),
        )
        .await
        .expect("update to completed");

        let fetched = db
            .get_sandbox_job(job_id)
            .await
            .expect("get")
            .expect("should exist");
        assert_eq!(fetched.status, "completed");
        assert_eq!(fetched.success, Some(true));

        // List
        let all = db.list_sandbox_jobs().await.expect("list");
        assert!(!all.is_empty());

        // Summary
        let summary = db.sandbox_job_summary().await.expect("summary");
        assert!(summary.total >= 1);

        // Per-user list
        let user_jobs = db
            .list_sandbox_jobs_for_user("user1")
            .await
            .expect("user list");
        assert!(!user_jobs.is_empty());

        // Ownership check
        let belongs = db
            .sandbox_job_belongs_to_user(job_id, "user1")
            .await
            .expect("belongs check");
        assert!(belongs);
        let not_belongs = db
            .sandbox_job_belongs_to_user(job_id, "other_user")
            .await
            .expect("belongs check");
        assert!(!not_belongs);
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_sandbox_job_mode() {
        use crate::history::SandboxJobRecord;

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        let job_id = uuid::Uuid::new_v4();
        let job = SandboxJobRecord {
            id: job_id,
            task: "Mode test".to_string(),
            status: "creating".to_string(),
            user_id: "user1".to_string(),
            project_dir: "/workspace".to_string(),
            success: None,
            failure_reason: None,
            created_at: chrono::Utc::now(),
            started_at: None,
            completed_at: None,
            credential_grants_json: "[]".to_string(),
        };
        db.save_sandbox_job(&job).await.expect("save");

        // Default mode
        let mode = db.get_sandbox_job_mode(job_id).await.expect("get mode");
        // Default is "worker" per schema or NULL
        assert!(mode.is_none() || mode.as_deref() == Some("worker"));

        // Update mode
        db.update_sandbox_job_mode(job_id, "claude_code")
            .await
            .expect("update mode");
        let mode = db
            .get_sandbox_job_mode(job_id)
            .await
            .expect("get mode")
            .expect("should have mode");
        assert_eq!(mode, "claude_code");
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_job_events() {
        use crate::history::SandboxJobRecord;

        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Create a sandbox job first (foreign key)
        let job_id = uuid::Uuid::new_v4();
        let job = SandboxJobRecord {
            id: job_id,
            task: "Event test".to_string(),
            status: "running".to_string(),
            user_id: "user1".to_string(),
            project_dir: "/workspace".to_string(),
            success: None,
            failure_reason: None,
            created_at: chrono::Utc::now(),
            started_at: Some(chrono::Utc::now()),
            completed_at: None,
            credential_grants_json: "[]".to_string(),
        };
        db.save_sandbox_job(&job).await.expect("save job");

        // Save events
        db.save_job_event(
            job_id,
            "tool_call",
            &serde_json::json!({"tool": "shell", "args": {"command": "ls"}}),
        )
        .await
        .expect("save event 1");

        db.save_job_event(
            job_id,
            "tool_result",
            &serde_json::json!({"output": "file1.txt\nfile2.txt"}),
        )
        .await
        .expect("save event 2");

        db.save_job_event(
            job_id,
            "llm_response",
            &serde_json::json!({"content": "Found 2 files"}),
        )
        .await
        .expect("save event 3");

        // List all events
        let events = db.list_job_events(job_id, None).await.expect("list events");
        assert_eq!(events.len(), 3);

        // List with limit
        let events = db
            .list_job_events(job_id, Some(2))
            .await
            .expect("list events limited");
        assert_eq!(events.len(), 2);
    }

    #[cfg(feature = "libsql")]
    #[tokio::test]
    async fn test_estimation_snapshot_round_trip() {
        let harness = TestHarnessBuilder::new().build().await;
        let db = &harness.db;

        // Create a job first
        let job_ctx = crate::context::JobContext::with_user("user1", "Estimate test", "testing");
        let job_id = job_ctx.job_id;
        db.save_job(&job_ctx).await.expect("save job");

        // Save estimation snapshot
        let snap_id = db
            .save_estimation_snapshot(
                job_id,
                "code_generation",
                &["shell".to_string(), "write_file".to_string()],
                Decimal::new(50, 2), // 0.50
                120,
                Decimal::new(500, 2), // 5.00
            )
            .await
            .expect("save snapshot");
        assert!(!snap_id.is_nil());

        // Update with actuals
        db.update_estimation_actuals(
            snap_id,
            Decimal::new(45, 2), // 0.45
            110,
            Some(Decimal::new(600, 2)), // 6.00
        )
        .await
        .expect("update actuals");
    }

    #[tokio::test]
    async fn stub_llm_fault_injector_sequence() {
        use crate::llm::LlmProvider;
        use crate::testing::fault_injection::{FaultAction, FaultInjector, FaultType};

        let injector = Arc::new(FaultInjector::sequence([
            FaultAction::Fail(FaultType::RateLimited { retry_after: None }),
            FaultAction::Succeed,
        ]));

        let stub = StubLlm::new("hello").with_fault_injector(injector);

        let req = crate::llm::CompletionRequest::new(vec![crate::llm::ChatMessage::user("hi")]);

        // First call should fail with RateLimited
        let result = stub.complete(req.clone()).await;
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), LlmError::RateLimited { .. }));

        // Second call should succeed
        let result = stub.complete(req).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().content, "hello");
    }
}