beleth 0.2.0-rc.1

Autonomous agent framework - The King commands legions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
//! Tool system for agent actions.

use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use infernum_core::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Risk level of a tool.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RiskLevel {
    /// Pure computation, no side effects.
    Safe,
    /// Reads external state.
    ReadOnly,
    /// Modifies external state.
    Write,
    /// Potentially dangerous operation.
    Dangerous,
}

/// Result from tool execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
    /// Whether the tool succeeded.
    pub success: bool,
    /// Output from the tool.
    pub output: String,
    /// Error message if failed.
    pub error: Option<String>,
    /// Additional data.
    pub data: Option<Value>,
}

impl ToolResult {
    /// Creates a successful result.
    #[must_use]
    pub fn success(output: impl Into<String>) -> Self {
        Self {
            success: true,
            output: output.into(),
            error: None,
            data: None,
        }
    }

    /// Creates a failed result.
    #[must_use]
    pub fn error(message: impl Into<String>) -> Self {
        Self {
            success: false,
            output: String::new(),
            error: Some(message.into()),
            data: None,
        }
    }

    /// Adds data to the result.
    #[must_use]
    pub fn with_data(mut self, data: Value) -> Self {
        self.data = Some(data);
        self
    }

    /// Validates the result against output constraints.
    #[must_use]
    pub fn validate(&self, config: &OutputValidationConfig) -> ValidationResult {
        let mut issues = Vec::new();

        // Check output size
        if self.output.len() > config.max_output_size {
            issues.push(ValidationIssue::OutputTooLarge {
                size: self.output.len(),
                max: config.max_output_size,
            });
        }

        // Check for sensitive patterns if enabled
        if config.check_sensitive_data {
            for pattern in SENSITIVE_PATTERNS {
                if self.output.to_lowercase().contains(pattern) {
                    issues.push(ValidationIssue::SensitiveData {
                        pattern: pattern.to_string(),
                    });
                }
            }
        }

        // Validate JSON data if present
        if let Some(ref data) = self.data {
            if config.max_json_depth > 0 {
                let depth = json_depth(data);
                if depth > config.max_json_depth {
                    issues.push(ValidationIssue::JsonTooDeep {
                        depth,
                        max: config.max_json_depth,
                    });
                }
            }
        }

        // Check for control characters
        if config.strip_control_chars && has_control_chars(&self.output) {
            issues.push(ValidationIssue::ContainsControlChars);
        }

        if issues.is_empty() {
            ValidationResult::Valid
        } else {
            ValidationResult::Invalid(issues)
        }
    }

    /// Sanitizes the result according to configuration.
    #[must_use]
    pub fn sanitize(&self, config: &OutputValidationConfig) -> Self {
        let mut result = self.clone();

        // Truncate if too large
        if result.output.len() > config.max_output_size {
            result.output = result.output.chars().take(config.max_output_size).collect();
            result.output.push_str("... [truncated]");
        }

        // Strip control characters if configured
        if config.strip_control_chars {
            result.output = strip_control_chars(&result.output);
        }

        // Redact sensitive patterns if configured
        if config.check_sensitive_data {
            for pattern in SENSITIVE_PATTERNS {
                if result.output.to_lowercase().contains(pattern) {
                    result.output = redact_pattern(&result.output, pattern);
                }
            }
        }

        result
    }
}

/// Configuration for output validation.
#[derive(Debug, Clone)]
pub struct OutputValidationConfig {
    /// Maximum allowed output size in characters.
    pub max_output_size: usize,
    /// Maximum JSON nesting depth.
    pub max_json_depth: usize,
    /// Whether to check for sensitive data patterns.
    pub check_sensitive_data: bool,
    /// Whether to strip control characters.
    pub strip_control_chars: bool,
    /// Whether to validate JSON structure.
    pub validate_json: bool,
}

impl Default for OutputValidationConfig {
    fn default() -> Self {
        Self {
            max_output_size: 100_000,
            max_json_depth: 20,
            check_sensitive_data: true,
            strip_control_chars: true,
            validate_json: true,
        }
    }
}

impl OutputValidationConfig {
    /// Creates a permissive configuration.
    #[must_use]
    pub fn permissive() -> Self {
        Self {
            max_output_size: 1_000_000,
            max_json_depth: 100,
            check_sensitive_data: false,
            strip_control_chars: false,
            validate_json: false,
        }
    }

    /// Creates a strict configuration.
    #[must_use]
    pub fn strict() -> Self {
        Self {
            max_output_size: 10_000,
            max_json_depth: 10,
            check_sensitive_data: true,
            strip_control_chars: true,
            validate_json: true,
        }
    }

    /// Sets maximum output size.
    #[must_use]
    pub fn with_max_output_size(mut self, size: usize) -> Self {
        self.max_output_size = size;
        self
    }

    /// Enables or disables sensitive data checking.
    #[must_use]
    pub fn with_sensitive_check(mut self, enabled: bool) -> Self {
        self.check_sensitive_data = enabled;
        self
    }
}

/// Result of output validation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationResult {
    /// Output is valid.
    Valid,
    /// Output has validation issues.
    Invalid(Vec<ValidationIssue>),
}

impl ValidationResult {
    /// Returns true if valid.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        matches!(self, ValidationResult::Valid)
    }

    /// Returns validation issues if any.
    #[must_use]
    pub fn issues(&self) -> Option<&[ValidationIssue]> {
        match self {
            ValidationResult::Valid => None,
            ValidationResult::Invalid(issues) => Some(issues),
        }
    }
}

/// Types of validation issues.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationIssue {
    /// Output exceeds size limit.
    OutputTooLarge {
        /// Actual size of the output.
        size: usize,
        /// Maximum allowed size.
        max: usize,
    },
    /// JSON nesting too deep.
    JsonTooDeep {
        /// Actual nesting depth.
        depth: usize,
        /// Maximum allowed depth.
        max: usize,
    },
    /// Contains sensitive data pattern.
    SensitiveData {
        /// The detected sensitive pattern.
        pattern: String,
    },
    /// Contains control characters.
    ContainsControlChars,
    /// Invalid JSON structure.
    InvalidJson {
        /// The JSON parsing error.
        error: String,
    },
}

/// Common sensitive data patterns to detect.
const SENSITIVE_PATTERNS: &[&str] = &[
    "password",
    "api_key",
    "apikey",
    "secret",
    "token",
    "credential",
    "private_key",
    "ssh_key",
    "bearer",
];

/// Calculates the depth of a JSON value.
fn json_depth(value: &Value) -> usize {
    match value {
        Value::Array(arr) => 1 + arr.iter().map(json_depth).max().unwrap_or(0),
        Value::Object(map) => 1 + map.values().map(json_depth).max().unwrap_or(0),
        _ => 0,
    }
}

/// Checks if a string contains control characters.
fn has_control_chars(s: &str) -> bool {
    s.chars()
        .any(|c| c.is_control() && c != '\n' && c != '\r' && c != '\t')
}

/// Strips control characters from a string.
fn strip_control_chars(s: &str) -> String {
    s.chars()
        .filter(|c| !c.is_control() || *c == '\n' || *c == '\r' || *c == '\t')
        .collect()
}

/// Redacts a pattern in the output.
fn redact_pattern(s: &str, pattern: &str) -> String {
    let lower = s.to_lowercase();
    let mut result = String::with_capacity(s.len());
    let mut last_end = 0;

    for (start, _) in lower.match_indices(pattern) {
        result.push_str(&s[last_end..start]);
        result.push_str("[REDACTED]");
        last_end = start + pattern.len();
    }
    result.push_str(&s[last_end..]);
    result
}

// ============================================================================
// Tool Timeout Configuration (Sprint 7 - Leviathan Feature Parity)
// ============================================================================

use std::time::Duration;

/// Configuration for tool execution timeouts.
///
/// Inspired by Leviathan/Persona's flexible timeout system that allows:
/// - Per-tool default timeouts
/// - Per-task timeout overrides
/// - Adaptive timeouts based on complexity
#[derive(Debug, Clone)]
pub struct ToolTimeoutConfig {
    /// Default timeout for all tools.
    pub default_timeout: Duration,
    /// Per-tool timeout overrides.
    pub tool_timeouts: HashMap<String, Duration>,
    /// Timeout multiplier for complex tasks (1.0 = no change).
    pub complexity_multiplier: f32,
    /// Maximum allowed timeout (cap).
    pub max_timeout: Duration,
    /// Minimum allowed timeout (floor).
    pub min_timeout: Duration,
}

impl Default for ToolTimeoutConfig {
    fn default() -> Self {
        Self {
            default_timeout: Duration::from_secs(30),
            tool_timeouts: HashMap::new(),
            complexity_multiplier: 1.0,
            max_timeout: Duration::from_secs(300), // 5 minutes max
            min_timeout: Duration::from_millis(100),
        }
    }
}

impl ToolTimeoutConfig {
    /// Creates a new timeout config.
    #[must_use]
    pub fn new(default_timeout: Duration) -> Self {
        Self {
            default_timeout,
            ..Default::default()
        }
    }

    /// Sets the timeout for a specific tool.
    #[must_use]
    pub fn with_tool_timeout(mut self, tool_name: impl Into<String>, timeout: Duration) -> Self {
        self.tool_timeouts.insert(tool_name.into(), timeout);
        self
    }

    /// Sets the complexity multiplier.
    #[must_use]
    pub fn with_complexity_multiplier(mut self, multiplier: f32) -> Self {
        self.complexity_multiplier = multiplier;
        self
    }

    /// Gets the effective timeout for a tool.
    #[must_use]
    pub fn get_timeout(&self, tool_name: &str) -> Duration {
        let base = self
            .tool_timeouts
            .get(tool_name)
            .copied()
            .unwrap_or(self.default_timeout);

        let adjusted = Duration::from_secs_f32(base.as_secs_f32() * self.complexity_multiplier);

        // Clamp to min/max bounds
        adjusted.clamp(self.min_timeout, self.max_timeout)
    }

    /// Creates a config for quick operations.
    #[must_use]
    pub fn quick() -> Self {
        Self {
            default_timeout: Duration::from_secs(5),
            max_timeout: Duration::from_secs(30),
            ..Default::default()
        }
    }

    /// Creates a config for long-running operations.
    #[must_use]
    pub fn long_running() -> Self {
        Self {
            default_timeout: Duration::from_secs(120),
            max_timeout: Duration::from_secs(600), // 10 minutes
            ..Default::default()
        }
    }

    /// Creates a config for I/O-bound operations.
    #[must_use]
    pub fn io_bound() -> Self {
        let mut config = Self::default();
        config
            .tool_timeouts
            .insert("http".to_string(), Duration::from_secs(60));
        config
            .tool_timeouts
            .insert("file_read".to_string(), Duration::from_secs(30));
        config
            .tool_timeouts
            .insert("file_write".to_string(), Duration::from_secs(30));
        config
            .tool_timeouts
            .insert("database".to_string(), Duration::from_secs(60));
        config
    }
}

/// Task complexity level for adaptive timeouts.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskComplexity {
    /// Simple, quick tasks.
    Simple,
    /// Moderate complexity.
    Moderate,
    /// Complex, multi-step tasks.
    Complex,
}

impl TaskComplexity {
    /// Returns the timeout multiplier for this complexity level.
    #[must_use]
    pub fn multiplier(&self) -> f32 {
        match self {
            Self::Simple => 0.5,
            Self::Moderate => 1.0,
            Self::Complex => 2.0,
        }
    }
}

/// Context provided to tools during execution.
#[derive(Clone)]
pub struct ToolContext {
    /// Agent ID.
    pub agent_id: String,
    /// Conversation history.
    pub messages: Vec<infernum_core::Message>,
    /// Key-value store for tool state.
    pub state: HashMap<String, Value>,
    /// Timeout configuration.
    pub timeout_config: ToolTimeoutConfig,
    /// Current task complexity.
    pub task_complexity: TaskComplexity,
}

impl ToolContext {
    /// Creates a new tool context.
    #[must_use]
    pub fn new(agent_id: impl Into<String>) -> Self {
        Self {
            agent_id: agent_id.into(),
            messages: Vec::new(),
            state: HashMap::new(),
            timeout_config: ToolTimeoutConfig::default(),
            task_complexity: TaskComplexity::Moderate,
        }
    }

    /// Sets the timeout configuration.
    #[must_use]
    pub fn with_timeout_config(mut self, config: ToolTimeoutConfig) -> Self {
        self.timeout_config = config;
        self
    }

    /// Sets the task complexity.
    #[must_use]
    pub fn with_complexity(mut self, complexity: TaskComplexity) -> Self {
        self.task_complexity = complexity;
        // Update the multiplier in timeout config
        self.timeout_config.complexity_multiplier = complexity.multiplier();
        self
    }

    /// Gets the effective timeout for a specific tool.
    #[must_use]
    pub fn get_tool_timeout(&self, tool_name: &str) -> Duration {
        self.timeout_config.get_timeout(tool_name)
    }

    /// Gets a value from state.
    #[must_use]
    pub fn get_state(&self, key: &str) -> Option<&Value> {
        self.state.get(key)
    }

    /// Sets a value in state.
    pub fn set_state(&mut self, key: impl Into<String>, value: Value) {
        self.state.insert(key.into(), value);
    }

    /// Gets the file read cache.
    #[must_use]
    pub fn get_file_cache(&self) -> HashMap<String, FileReadCacheEntry> {
        self.state
            .get(FILE_READ_CACHE_KEY)
            .and_then(|v| serde_json::from_value(v.clone()).ok())
            .unwrap_or_default()
    }

    /// Updates the file read cache.
    pub fn update_file_cache(&mut self, path: &str, entry: FileReadCacheEntry) {
        let mut cache = self.get_file_cache();
        cache.insert(path.to_string(), entry);
        if let Ok(value) = serde_json::to_value(&cache) {
            self.state.insert(FILE_READ_CACHE_KEY.to_string(), value);
        }
    }
}

/// Key for file read cache in ToolContext state.
const FILE_READ_CACHE_KEY: &str = "__file_read_cache";

/// Cache entry for file read deduplication.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileReadCacheEntry {
    /// Hash of file content.
    pub content_hash: u64,
    /// Total line count.
    pub line_count: usize,
    /// Last read offset.
    pub last_offset: usize,
    /// Last read limit.
    pub last_limit: usize,
    /// Timestamp of last read (for cache invalidation).
    pub read_at_ms: u64,
}

impl FileReadCacheEntry {
    /// Creates a new cache entry.
    #[must_use]
    pub fn new(content_hash: u64, line_count: usize, offset: usize, limit: usize) -> Self {
        let read_at_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        Self {
            content_hash,
            line_count,
            last_offset: offset,
            last_limit: limit,
            read_at_ms,
        }
    }
}

/// Computes a hash for file content.
/// Used by file caching to detect unchanged files.
#[allow(dead_code)] // Will be used when Tool::execute takes &mut ToolContext
pub fn hash_content(content: &str) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut hasher = DefaultHasher::new();
    content.hash(&mut hasher);
    hasher.finish()
}

/// Trait for tools that agents can use.
#[async_trait]
pub trait Tool: Send + Sync {
    /// Returns the tool name.
    fn name(&self) -> &str;

    /// Returns the tool description.
    fn description(&self) -> &str;

    /// Returns the JSON schema for parameters.
    fn parameters_schema(&self) -> Value;

    /// Returns the risk level.
    fn risk_level(&self) -> RiskLevel;

    /// Executes the tool.
    async fn execute(&self, params: Value, ctx: &ToolContext) -> Result<ToolResult>;
}

/// A tool call parsed from LLM output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCall {
    /// Tool name.
    pub name: String,
    /// Tool parameters.
    pub params: Value,
}

/// Registry of available tools.
#[derive(Default)]
pub struct ToolRegistry {
    tools: HashMap<String, Arc<dyn Tool>>,
}

impl ToolRegistry {
    /// Creates a new empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a registry with built-in tools.
    #[must_use]
    pub fn with_builtins() -> Self {
        let mut registry = Self::new();
        registry.register(Arc::new(CalculatorTool));
        registry.register(Arc::new(JsonTool));
        registry.register(Arc::new(DateTimeTool));
        registry
    }

    /// Creates a registry with built-in tools plus code-relevant tools.
    ///
    /// Includes file I/O (read, write, edit), search (list, grep), and
    /// shell execution. Does NOT include `claude_code` — use
    /// [`with_all_tools`](Self::with_all_tools) for that.
    #[must_use]
    pub fn with_code_tools() -> Self {
        use crate::tools::{
            BashTool, EditFileTool, ListFilesTool, ReadFileTool, SearchFilesTool, WriteFileTool,
        };

        let mut registry = Self::with_builtins();
        registry.register(Arc::new(ReadFileTool));
        registry.register(Arc::new(WriteFileTool));
        registry.register(Arc::new(EditFileTool));
        registry.register(Arc::new(ListFilesTool));
        registry.register(Arc::new(SearchFilesTool));
        registry.register(Arc::new(BashTool::default()));
        registry
    }

    /// Creates a registry with all tools, including Claude Code delegation.
    ///
    /// Requires the `claude` CLI to be installed and on `$PATH`.
    #[must_use]
    pub fn with_all_tools() -> Self {
        use crate::tools::ClaudeCodeTool;

        let mut registry = Self::with_code_tools();
        registry.register(Arc::new(ClaudeCodeTool::new()));
        registry
    }

    /// Registers a tool.
    pub fn register(&mut self, tool: Arc<dyn Tool>) {
        self.tools.insert(tool.name().to_string(), tool);
    }

    /// Gets a tool by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<Arc<dyn Tool>> {
        self.tools.get(name).cloned()
    }

    /// Lists all tool names.
    #[must_use]
    pub fn list(&self) -> Vec<&str> {
        self.tools.keys().map(String::as_str).collect()
    }

    /// Returns all tools.
    #[must_use]
    pub fn tools(&self) -> Vec<Arc<dyn Tool>> {
        self.tools.values().cloned().collect()
    }

    /// Returns the number of registered tools.
    #[must_use]
    pub fn len(&self) -> usize {
        self.tools.len()
    }

    /// Returns true if no tools are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.tools.is_empty()
    }

    /// Generates tool definitions for LLM function calling.
    #[must_use]
    pub fn to_function_definitions(&self) -> Vec<Value> {
        self.tools
            .values()
            .map(|tool| {
                serde_json::json!({
                    "type": "function",
                    "function": {
                        "name": tool.name(),
                        "description": tool.description(),
                        "parameters": tool.parameters_schema()
                    }
                })
            })
            .collect()
    }

    /// Generates tool descriptions for prompting (generic format).
    #[must_use]
    pub fn to_prompt_description(&self) -> String {
        let mut desc = String::from("Available tools:\n\n");
        for tool in self.tools.values() {
            desc.push_str(&format!(
                "- {}: {}\n  Parameters: {}\n\n",
                tool.name(),
                tool.description(),
                serde_json::to_string_pretty(&tool.parameters_schema()).unwrap_or_default()
            ));
        }
        desc
    }

    /// Generates tool descriptions in Qwen2.5 native format.
    ///
    /// Matches the format from Qwen2.5's Jinja chat template: tools wrapped
    /// in `<tools></tools>` XML tags as full JSON function definitions.
    ///
    /// See TOOL-CALLING-SPEC.md §5.6.
    #[must_use]
    pub fn to_qwen_native_description(&self) -> String {
        let mut desc = String::from(
            "You may call one or more functions to assist with the user query.\n\n\
             You are provided with function signatures within <tools></tools> XML tags:\n\
             <tools>",
        );

        for tool_def in self.to_function_definitions() {
            desc.push('\n');
            desc.push_str(&serde_json::to_string(&tool_def).unwrap_or_default());
        }

        desc.push_str(
            "\n</tools>\n\n\
             For each function call, return a json object with function name and arguments \
             within <tool_call></tool_call> XML tags:\n\
             <tool_call>\n\
             {\"name\": <function-name>, \"arguments\": <args-json-object>}\n\
             </tool_call>",
        );

        desc
    }

    /// Executes a tool call.
    ///
    /// # Errors
    ///
    /// Returns an error if the tool is not found or execution fails.
    pub async fn execute(&self, call: &ToolCall, ctx: &ToolContext) -> Result<ToolResult> {
        let tool = self.get(&call.name).ok_or_else(|| {
            infernum_core::Error::internal(format!("Tool '{}' not found", call.name))
        })?;

        tool.execute(call.params.clone(), ctx).await
    }
}

// === Built-in Tools ===

/// Calculator tool for mathematical expressions.
pub struct CalculatorTool;

#[async_trait]
impl Tool for CalculatorTool {
    fn name(&self) -> &str {
        "calculator"
    }

    fn description(&self) -> &str {
        "Evaluates mathematical expressions. Supports basic arithmetic (+, -, *, /), \
         parentheses, and common functions (sqrt, sin, cos, tan, log, exp, pow)."
    }

    fn parameters_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "expression": {
                    "type": "string",
                    "description": "Mathematical expression to evaluate"
                }
            },
            "required": ["expression"]
        })
    }

    fn risk_level(&self) -> RiskLevel {
        RiskLevel::Safe
    }

    async fn execute(&self, params: Value, _ctx: &ToolContext) -> Result<ToolResult> {
        let expression = params
            .get("expression")
            .and_then(|v| v.as_str())
            .ok_or_else(|| infernum_core::Error::internal("Missing expression"))?;

        // Simple expression evaluator using a basic parser
        match evaluate_expression(expression) {
            Ok(result) => Ok(ToolResult::success(format!("{} = {}", expression, result))
                .with_data(serde_json::json!({ "result": result }))),
            Err(e) => Ok(ToolResult::error(format!("Failed to evaluate: {}", e))),
        }
    }
}

/// JSON manipulation tool.
pub struct JsonTool;

#[async_trait]
impl Tool for JsonTool {
    fn name(&self) -> &str {
        "json"
    }

    fn description(&self) -> &str {
        "Parses, formats, and queries JSON data. Supports JSONPath queries."
    }

    fn parameters_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": ["parse", "format", "query"],
                    "description": "Operation to perform"
                },
                "data": {
                    "type": "string",
                    "description": "JSON string or data"
                },
                "query": {
                    "type": "string",
                    "description": "JSONPath query (for query operation)"
                }
            },
            "required": ["operation", "data"]
        })
    }

    fn risk_level(&self) -> RiskLevel {
        RiskLevel::Safe
    }

    async fn execute(&self, params: Value, _ctx: &ToolContext) -> Result<ToolResult> {
        let operation = params
            .get("operation")
            .and_then(|v| v.as_str())
            .ok_or_else(|| infernum_core::Error::internal("Missing operation"))?;

        let data = params
            .get("data")
            .and_then(|v| v.as_str())
            .ok_or_else(|| infernum_core::Error::internal("Missing data"))?;

        match operation {
            "parse" => match serde_json::from_str::<Value>(data) {
                Ok(parsed) => Ok(ToolResult::success("JSON parsed successfully").with_data(parsed)),
                Err(e) => Ok(ToolResult::error(format!("Parse error: {}", e))),
            },
            "format" => match serde_json::from_str::<Value>(data) {
                Ok(parsed) => match serde_json::to_string_pretty(&parsed) {
                    Ok(formatted) => Ok(ToolResult::success(formatted)),
                    Err(e) => Ok(ToolResult::error(format!("Format error: {}", e))),
                },
                Err(e) => Ok(ToolResult::error(format!("Parse error: {}", e))),
            },
            "query" => {
                // Simple key access (full JSONPath would need a library)
                let query = params.get("query").and_then(|v| v.as_str()).unwrap_or("");
                match serde_json::from_str::<Value>(data) {
                    Ok(parsed) => {
                        // Simple dot notation query
                        let result = query_json(&parsed, query);
                        match result {
                            Some(v) => Ok(ToolResult::success(v.to_string()).with_data(v)),
                            None => Ok(ToolResult::error("Query returned no results")),
                        }
                    },
                    Err(e) => Ok(ToolResult::error(format!("Parse error: {}", e))),
                }
            },
            _ => Ok(ToolResult::error(format!(
                "Unknown operation: {}",
                operation
            ))),
        }
    }
}

/// DateTime tool.
pub struct DateTimeTool;

#[async_trait]
impl Tool for DateTimeTool {
    fn name(&self) -> &str {
        "datetime"
    }

    fn description(&self) -> &str {
        "Gets current date/time or performs date calculations."
    }

    fn parameters_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "enum": ["now", "format", "parse"],
                    "description": "Operation to perform"
                },
                "format": {
                    "type": "string",
                    "description": "Date format string (for format operation)"
                },
                "timezone": {
                    "type": "string",
                    "description": "Timezone (e.g., 'UTC', 'America/New_York')"
                }
            },
            "required": ["operation"]
        })
    }

    fn risk_level(&self) -> RiskLevel {
        RiskLevel::Safe
    }

    async fn execute(&self, params: Value, _ctx: &ToolContext) -> Result<ToolResult> {
        let operation = params
            .get("operation")
            .and_then(|v| v.as_str())
            .ok_or_else(|| infernum_core::Error::internal("Missing operation"))?;

        match operation {
            "now" => {
                use std::time::{SystemTime, UNIX_EPOCH};
                let now = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default();
                let secs = now.as_secs();

                // Simple ISO-8601 format
                let datetime = format_unix_timestamp(secs);

                Ok(
                    ToolResult::success(format!("Current time: {}", datetime)).with_data(
                        serde_json::json!({
                            "timestamp": secs,
                            "iso8601": datetime
                        }),
                    ),
                )
            },
            "format" => {
                use std::time::{SystemTime, UNIX_EPOCH};
                let now = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or_default();
                let secs = now.as_secs();

                let format_str = params
                    .get("format")
                    .and_then(|v| v.as_str())
                    .unwrap_or("%Y-%m-%d %H:%M:%S");

                let formatted = format_with_pattern(secs, format_str);

                Ok(
                    ToolResult::success(format!("Formatted: {}", formatted)).with_data(
                        serde_json::json!({
                            "timestamp": secs,
                            "formatted": formatted
                        }),
                    ),
                )
            },
            "parse" => {
                let date_str = params
                    .get("date")
                    .or_else(|| params.get("data"))
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| infernum_core::Error::internal("Missing date string"))?;

                match parse_datetime(date_str) {
                    Ok(timestamp) => {
                        let iso = format_unix_timestamp(timestamp);
                        Ok(
                            ToolResult::success(format!("Parsed: {} -> {}", date_str, iso))
                                .with_data(serde_json::json!({
                                    "timestamp": timestamp,
                                    "iso8601": iso,
                                    "input": date_str
                                })),
                        )
                    },
                    Err(e) => Ok(ToolResult::error(format!("Parse error: {}", e))),
                }
            },
            _ => Ok(ToolResult::error(format!(
                "Unknown operation: {}. Supported: now, format, parse",
                operation
            ))),
        }
    }
}

// === Helper Functions ===

/// Simple expression evaluator for basic arithmetic.
fn evaluate_expression(expr: &str) -> std::result::Result<f64, String> {
    // Basic tokenizer and evaluator
    let expr = expr.trim().replace(' ', "");

    // Handle simple arithmetic expressions
    if let Ok(num) = expr.parse::<f64>() {
        return Ok(num);
    }

    // Try to find operators and evaluate
    for (i, c) in expr.chars().rev().enumerate() {
        let pos = expr.len() - 1 - i;
        if c == '+' && pos > 0 {
            let left = evaluate_expression(&expr[..pos])?;
            let right = evaluate_expression(&expr[pos + 1..])?;
            return Ok(left + right);
        }
        if c == '-' && pos > 0 {
            // Check it's not a negative number
            if pos > 0 {
                let prev = expr.chars().nth(pos - 1);
                if prev
                    .map(|p| p.is_ascii_digit() || p == ')')
                    .unwrap_or(false)
                {
                    let left = evaluate_expression(&expr[..pos])?;
                    let right = evaluate_expression(&expr[pos + 1..])?;
                    return Ok(left - right);
                }
            }
        }
    }

    for (i, c) in expr.chars().rev().enumerate() {
        let pos = expr.len() - 1 - i;
        if c == '*' && pos > 0 {
            let left = evaluate_expression(&expr[..pos])?;
            let right = evaluate_expression(&expr[pos + 1..])?;
            return Ok(left * right);
        }
        if c == '/' && pos > 0 {
            let left = evaluate_expression(&expr[..pos])?;
            let right = evaluate_expression(&expr[pos + 1..])?;
            if right == 0.0 {
                return Err("Division by zero".to_string());
            }
            return Ok(left / right);
        }
    }

    // Handle parentheses
    if expr.starts_with('(') && expr.ends_with(')') {
        return evaluate_expression(&expr[1..expr.len() - 1]);
    }

    // Handle functions
    if let Some(inner) = expr.strip_prefix("sqrt(").and_then(|s| s.strip_suffix(')')) {
        return Ok(evaluate_expression(inner)?.sqrt());
    }

    Err(format!("Cannot evaluate: {}", expr))
}

/// Simple JSON query using dot notation.
fn query_json(value: &Value, query: &str) -> Option<Value> {
    if query.is_empty() {
        return Some(value.clone());
    }

    let parts: Vec<&str> = query.split('.').collect();
    let mut current = value;

    for part in parts {
        match current {
            Value::Object(map) => {
                current = map.get(part)?;
            },
            Value::Array(arr) => {
                let idx: usize = part.parse().ok()?;
                current = arr.get(idx)?;
            },
            _ => return None,
        }
    }

    Some(current.clone())
}

/// Formats a Unix timestamp as ISO-8601.
fn format_unix_timestamp(secs: u64) -> String {
    // Simple calculation without chrono dependency
    const SECS_PER_DAY: u64 = 86400;
    const SECS_PER_HOUR: u64 = 3600;
    const SECS_PER_MIN: u64 = 60;

    // Days since Unix epoch
    let days = secs / SECS_PER_DAY;
    let remaining = secs % SECS_PER_DAY;

    let hours = remaining / SECS_PER_HOUR;
    let remaining = remaining % SECS_PER_HOUR;

    let minutes = remaining / SECS_PER_MIN;
    let seconds = remaining % SECS_PER_MIN;

    // Simple year calculation (approximate, doesn't handle leap years perfectly)
    let mut year = 1970;
    let mut day_count = days;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if day_count < days_in_year {
            break;
        }
        day_count -= days_in_year;
        year += 1;
    }

    // Calculate month and day
    let (month, day) = day_of_year_to_month_day(day_count as u32, is_leap_year(year));

    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

fn is_leap_year(year: u64) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

fn day_of_year_to_month_day(day: u32, leap: bool) -> (u32, u32) {
    let days_in_months = if leap {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    let mut remaining = day;
    for (i, &days) in days_in_months.iter().enumerate() {
        if remaining < days {
            return ((i + 1) as u32, remaining + 1);
        }
        remaining -= days;
    }
    (12, 31)
}

/// Formats a Unix timestamp with a strftime-like pattern.
fn format_with_pattern(secs: u64, pattern: &str) -> String {
    const SECS_PER_DAY: u64 = 86400;
    const SECS_PER_HOUR: u64 = 3600;
    const SECS_PER_MIN: u64 = 60;

    let days = secs / SECS_PER_DAY;
    let remaining = secs % SECS_PER_DAY;

    let hours = remaining / SECS_PER_HOUR;
    let remaining = remaining % SECS_PER_HOUR;

    let minutes = remaining / SECS_PER_MIN;
    let seconds = remaining % SECS_PER_MIN;

    // Calculate year
    let mut year = 1970u64;
    let mut day_count = days;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if day_count < days_in_year {
            break;
        }
        day_count -= days_in_year;
        year += 1;
    }

    let (month, day) = day_of_year_to_month_day(day_count as u32, is_leap_year(year));

    // Day of week (0 = Thursday for Unix epoch)
    let dow = ((days + 4) % 7) as u32; // 0 = Sunday

    let month_names = [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    ];
    let month_abbrev = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    let day_names = [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
    ];
    let day_abbrev = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

    let mut result = pattern.to_string();
    result = result.replace("%Y", &format!("{:04}", year));
    result = result.replace("%y", &format!("{:02}", year % 100));
    result = result.replace("%m", &format!("{:02}", month));
    result = result.replace("%d", &format!("{:02}", day));
    result = result.replace("%H", &format!("{:02}", hours));
    result = result.replace("%M", &format!("{:02}", minutes));
    result = result.replace("%S", &format!("{:02}", seconds));
    result = result.replace("%B", month_names[(month - 1) as usize]);
    result = result.replace("%b", month_abbrev[(month - 1) as usize]);
    result = result.replace("%A", day_names[dow as usize]);
    result = result.replace("%a", day_abbrev[dow as usize]);
    result = result.replace("%w", &dow.to_string());
    result = result.replace("%%", "%");

    result
}

/// Parses a datetime string into Unix timestamp.
fn parse_datetime(s: &str) -> std::result::Result<u64, String> {
    let s = s.trim();

    // Try ISO-8601 format: YYYY-MM-DDTHH:MM:SSZ
    if let Some(parsed) = parse_iso8601(s) {
        return Ok(parsed);
    }

    // Try date only: YYYY-MM-DD
    if let Some(parsed) = parse_date_only(s) {
        return Ok(parsed);
    }

    // Try common formats: MM/DD/YYYY, DD-MM-YYYY
    if let Some(parsed) = parse_common_formats(s) {
        return Ok(parsed);
    }

    Err(format!("Unable to parse date: {}", s))
}

fn parse_iso8601(s: &str) -> Option<u64> {
    // YYYY-MM-DDTHH:MM:SS or YYYY-MM-DDTHH:MM:SSZ
    let s = s.trim_end_matches('Z');
    let parts: Vec<&str> = s.split('T').collect();
    if parts.len() != 2 {
        return None;
    }

    let date_parts: Vec<u32> = parts[0].split('-').filter_map(|p| p.parse().ok()).collect();
    if date_parts.len() != 3 {
        return None;
    }

    let time_parts: Vec<u32> = parts[1].split(':').filter_map(|p| p.parse().ok()).collect();
    if time_parts.len() < 2 {
        return None;
    }

    let year = date_parts[0] as u64;
    let month = date_parts[1];
    let day = date_parts[2];
    let hour = time_parts[0] as u64;
    let min = time_parts[1] as u64;
    let sec = time_parts.get(2).copied().unwrap_or(0) as u64;

    Some(datetime_to_timestamp(year, month, day, hour, min, sec))
}

fn parse_date_only(s: &str) -> Option<u64> {
    let parts: Vec<u32> = s.split('-').filter_map(|p| p.parse().ok()).collect();
    if parts.len() != 3 {
        return None;
    }

    let year = parts[0] as u64;
    let month = parts[1];
    let day = parts[2];

    Some(datetime_to_timestamp(year, month, day, 0, 0, 0))
}

fn parse_common_formats(s: &str) -> Option<u64> {
    // Try MM/DD/YYYY
    if s.contains('/') {
        let parts: Vec<u32> = s.split('/').filter_map(|p| p.parse().ok()).collect();
        if parts.len() == 3 {
            let (month, day, year) = (parts[0], parts[1], parts[2] as u64);
            return Some(datetime_to_timestamp(year, month, day, 0, 0, 0));
        }
    }

    None
}

fn datetime_to_timestamp(year: u64, month: u32, day: u32, hour: u64, min: u64, sec: u64) -> u64 {
    const SECS_PER_DAY: u64 = 86400;
    const SECS_PER_HOUR: u64 = 3600;
    const SECS_PER_MIN: u64 = 60;

    // Count days from 1970 to year
    let mut days: u64 = 0;
    for y in 1970..year {
        days += if is_leap_year(y) { 366 } else { 365 };
    }

    // Add days for months in current year
    let days_in_months = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    for m in 0..(month - 1) as usize {
        days += days_in_months[m] as u64;
    }

    // Add remaining days
    days += (day - 1) as u64;

    // Convert to seconds
    days * SECS_PER_DAY + hour * SECS_PER_HOUR + min * SECS_PER_MIN + sec
}

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

    #[test]
    fn test_evaluate_expression() {
        assert_eq!(evaluate_expression("2+3").unwrap(), 5.0);
        assert_eq!(evaluate_expression("10*5").unwrap(), 50.0);
        assert_eq!(evaluate_expression("20/4").unwrap(), 5.0);
        assert_eq!(evaluate_expression("sqrt(16)").unwrap(), 4.0);
    }

    #[test]
    fn test_query_json() {
        let json = serde_json::json!({
            "name": "test",
            "nested": {
                "value": 42
            }
        });

        assert_eq!(query_json(&json, "name"), Some(serde_json::json!("test")));
        assert_eq!(
            query_json(&json, "nested.value"),
            Some(serde_json::json!(42))
        );
    }

    #[tokio::test]
    async fn test_calculator_tool() {
        let tool = CalculatorTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({ "expression": "2+2" });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.output.contains("4"));
    }

    #[test]
    fn test_output_validation_valid() {
        let result = ToolResult::success("Hello, world!");
        let config = OutputValidationConfig::default();

        assert!(result.validate(&config).is_valid());
    }

    #[test]
    fn test_output_validation_too_large() {
        let result = ToolResult::success("x".repeat(200));
        let config = OutputValidationConfig::default().with_max_output_size(100);

        let validation = result.validate(&config);
        assert!(!validation.is_valid());

        let issues = validation.issues().unwrap();
        assert!(matches!(issues[0], ValidationIssue::OutputTooLarge { .. }));
    }

    #[test]
    fn test_output_validation_sensitive_data() {
        let result = ToolResult::success("Your password is secret123");
        let config = OutputValidationConfig::default();

        let validation = result.validate(&config);
        assert!(!validation.is_valid());

        let issues = validation.issues().unwrap();
        assert!(issues
            .iter()
            .any(|i| matches!(i, ValidationIssue::SensitiveData { .. })));
    }

    #[test]
    fn test_output_validation_control_chars() {
        let result = ToolResult::success("Hello\x00World");
        let config = OutputValidationConfig::default();

        let validation = result.validate(&config);
        assert!(!validation.is_valid());

        let issues = validation.issues().unwrap();
        assert!(issues
            .iter()
            .any(|i| matches!(i, ValidationIssue::ContainsControlChars)));
    }

    #[test]
    fn test_output_validation_json_depth() {
        let deep_json = serde_json::json!({
            "a": { "b": { "c": { "d": { "e": "value" } } } }
        });
        let result = ToolResult::success("test").with_data(deep_json);
        let config = OutputValidationConfig::default();

        // Default allows depth 20, this is only 5 deep
        assert!(result.validate(&config).is_valid());

        // But with strict config (max depth 10)...
        let strict = OutputValidationConfig::strict();
        assert!(result.validate(&strict).is_valid());
    }

    #[test]
    fn test_sanitize_output() {
        let result = ToolResult::success("Your password is secret123");
        let config = OutputValidationConfig::default();

        let sanitized = result.sanitize(&config);
        assert!(sanitized.output.contains("[REDACTED]"));
        assert!(!sanitized.output.contains("password"));
    }

    #[test]
    fn test_sanitize_truncate() {
        let result = ToolResult::success("x".repeat(200));
        let config = OutputValidationConfig::default().with_max_output_size(50);

        let sanitized = result.sanitize(&config);
        assert!(sanitized.output.len() < 200);
        assert!(sanitized.output.contains("[truncated]"));
    }

    #[test]
    fn test_sanitize_control_chars() {
        let result = ToolResult::success("Hello\x00World\x1BEscape");
        let config = OutputValidationConfig::default();

        let sanitized = result.sanitize(&config);
        assert!(!sanitized.output.contains('\x00'));
        assert!(!sanitized.output.contains('\x1B'));
        assert!(sanitized.output.contains("HelloWorldEscape"));
    }

    #[test]
    fn test_validation_config_permissive() {
        let config = OutputValidationConfig::permissive();
        assert_eq!(config.max_output_size, 1_000_000);
        assert!(!config.check_sensitive_data);
    }

    #[test]
    fn test_validation_config_strict() {
        let config = OutputValidationConfig::strict();
        assert_eq!(config.max_output_size, 10_000);
        assert!(config.check_sensitive_data);
    }

    #[test]
    fn test_json_depth() {
        assert_eq!(json_depth(&serde_json::json!(42)), 0);
        assert_eq!(json_depth(&serde_json::json!([1, 2, 3])), 1);
        assert_eq!(json_depth(&serde_json::json!({"a": 1})), 1);
        assert_eq!(json_depth(&serde_json::json!({"a": {"b": 1}})), 2);
        assert_eq!(json_depth(&serde_json::json!([[[1]]])), 3);
    }

    #[test]
    fn test_strip_control_chars_preserves_newlines() {
        let input = "Line 1\nLine 2\rLine 3\tTabbed";
        let output = strip_control_chars(input);
        assert_eq!(output, input); // newlines, carriage returns, tabs preserved
    }

    #[test]
    fn test_redact_pattern() {
        let input = "The password is ABC123";
        let output = redact_pattern(input, "password");
        assert!(output.contains("[REDACTED]"));
        assert!(output.contains("ABC123")); // The value is preserved, just the pattern key
    }

    // ========================================================================
    // Tool Timeout Configuration Tests
    // ========================================================================

    #[test]
    fn test_tool_timeout_config_default() {
        let config = ToolTimeoutConfig::default();
        assert_eq!(config.default_timeout, Duration::from_secs(30));
        assert_eq!(config.max_timeout, Duration::from_secs(300));
        assert_eq!(config.min_timeout, Duration::from_millis(100));
        assert!((config.complexity_multiplier - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_tool_timeout_config_per_tool() {
        let config = ToolTimeoutConfig::default()
            .with_tool_timeout("slow_tool", Duration::from_secs(120))
            .with_tool_timeout("fast_tool", Duration::from_secs(5));

        assert_eq!(config.get_timeout("slow_tool"), Duration::from_secs(120));
        assert_eq!(config.get_timeout("fast_tool"), Duration::from_secs(5));
        assert_eq!(config.get_timeout("unknown"), Duration::from_secs(30)); // default
    }

    #[test]
    fn test_tool_timeout_config_complexity_multiplier() {
        let config =
            ToolTimeoutConfig::new(Duration::from_secs(30)).with_complexity_multiplier(2.0);

        // 30s * 2.0 = 60s
        assert_eq!(config.get_timeout("any_tool"), Duration::from_secs(60));
    }

    #[test]
    fn test_tool_timeout_config_clamping() {
        // Test max clamping
        let config =
            ToolTimeoutConfig::new(Duration::from_secs(400)).with_complexity_multiplier(1.0);
        assert_eq!(config.get_timeout("tool"), Duration::from_secs(300)); // clamped to max

        // Test min clamping
        let config2 =
            ToolTimeoutConfig::new(Duration::from_millis(10)).with_complexity_multiplier(1.0);
        assert_eq!(config2.get_timeout("tool"), Duration::from_millis(100)); // clamped to min
    }

    #[test]
    fn test_tool_timeout_config_quick() {
        let config = ToolTimeoutConfig::quick();
        assert_eq!(config.default_timeout, Duration::from_secs(5));
        assert_eq!(config.max_timeout, Duration::from_secs(30));
    }

    #[test]
    fn test_tool_timeout_config_long_running() {
        let config = ToolTimeoutConfig::long_running();
        assert_eq!(config.default_timeout, Duration::from_secs(120));
        assert_eq!(config.max_timeout, Duration::from_secs(600));
    }

    #[test]
    fn test_tool_timeout_config_io_bound() {
        let config = ToolTimeoutConfig::io_bound();
        assert_eq!(config.get_timeout("http"), Duration::from_secs(60));
        assert_eq!(config.get_timeout("file_read"), Duration::from_secs(30));
        assert_eq!(config.get_timeout("database"), Duration::from_secs(60));
    }

    #[test]
    fn test_task_complexity_multiplier() {
        assert!((TaskComplexity::Simple.multiplier() - 0.5).abs() < 0.01);
        assert!((TaskComplexity::Moderate.multiplier() - 1.0).abs() < 0.01);
        assert!((TaskComplexity::Complex.multiplier() - 2.0).abs() < 0.01);
    }

    #[test]
    fn test_tool_context_with_timeout() {
        let config =
            ToolTimeoutConfig::default().with_tool_timeout("search", Duration::from_secs(60));

        let ctx = ToolContext::new("agent-1").with_timeout_config(config);

        assert_eq!(ctx.get_tool_timeout("search"), Duration::from_secs(60));
        assert_eq!(ctx.get_tool_timeout("other"), Duration::from_secs(30));
    }

    #[test]
    fn test_tool_context_with_complexity() {
        let ctx = ToolContext::new("agent-1").with_complexity(TaskComplexity::Complex);

        assert_eq!(ctx.task_complexity, TaskComplexity::Complex);
        // 30s default * 2.0 complexity = 60s
        assert_eq!(ctx.get_tool_timeout("any"), Duration::from_secs(60));
    }

    #[test]
    fn test_tool_context_complexity_affects_all_timeouts() {
        let config =
            ToolTimeoutConfig::default().with_tool_timeout("slow", Duration::from_secs(100));

        let ctx = ToolContext::new("agent-1")
            .with_timeout_config(config)
            .with_complexity(TaskComplexity::Simple);

        // 100s * 0.5 (simple) = 50s
        assert_eq!(ctx.get_tool_timeout("slow"), Duration::from_secs(50));
        // 30s * 0.5 = 15s
        assert_eq!(ctx.get_tool_timeout("default"), Duration::from_secs(15));
    }

    // ==========================================================================
    // RiskLevel Tests
    // ==========================================================================

    #[test]
    fn test_risk_level_all_variants() {
        let safe = RiskLevel::Safe;
        let read_only = RiskLevel::ReadOnly;
        let write = RiskLevel::Write;
        let dangerous = RiskLevel::Dangerous;

        assert!(matches!(safe, RiskLevel::Safe));
        assert!(matches!(read_only, RiskLevel::ReadOnly));
        assert!(matches!(write, RiskLevel::Write));
        assert!(matches!(dangerous, RiskLevel::Dangerous));
    }

    #[test]
    fn test_risk_level_eq() {
        assert_eq!(RiskLevel::Safe, RiskLevel::Safe);
        assert_ne!(RiskLevel::Safe, RiskLevel::Dangerous);
    }

    #[test]
    fn test_risk_level_clone() {
        let level = RiskLevel::Write;
        let cloned = level;
        assert_eq!(cloned, RiskLevel::Write);
    }

    #[test]
    fn test_risk_level_debug() {
        let level = RiskLevel::ReadOnly;
        let debug_str = format!("{:?}", level);
        assert_eq!(debug_str, "ReadOnly");
    }

    // ==========================================================================
    // ToolResult Additional Tests
    // ==========================================================================

    #[test]
    fn test_tool_result_success() {
        let result = ToolResult::success("Operation completed");
        assert!(result.success);
        assert_eq!(result.output, "Operation completed");
        assert!(result.error.is_none());
        assert!(result.data.is_none());
    }

    #[test]
    fn test_tool_result_error() {
        let result = ToolResult::error("Something went wrong");
        assert!(!result.success);
        assert!(result.output.is_empty());
        assert_eq!(result.error, Some("Something went wrong".to_string()));
    }

    #[test]
    fn test_tool_result_with_data() {
        let result = ToolResult::success("test").with_data(serde_json::json!({"key": "value"}));
        assert!(result.data.is_some());
        assert_eq!(result.data.unwrap()["key"], "value");
    }

    #[test]
    fn test_tool_result_serialize() {
        let result = ToolResult::success("output").with_data(serde_json::json!(123));
        let json = serde_json::to_string(&result).expect("serialize");
        assert!(json.contains("output"));
        assert!(json.contains("true"));
    }

    #[test]
    fn test_tool_result_deserialize() {
        let json = r#"{"success":true,"output":"test","error":null,"data":{"x":1}}"#;
        let result: ToolResult = serde_json::from_str(json).expect("deserialize");
        assert!(result.success);
        assert_eq!(result.output, "test");
    }

    #[test]
    fn test_tool_result_clone() {
        let result = ToolResult::success("clone test");
        let cloned = result.clone();
        assert_eq!(cloned.output, "clone test");
    }

    // ==========================================================================
    // OutputValidationConfig Additional Tests
    // ==========================================================================

    #[test]
    fn test_output_validation_config_default() {
        let config = OutputValidationConfig::default();
        assert_eq!(config.max_output_size, 100_000);
        assert_eq!(config.max_json_depth, 20);
        assert!(config.check_sensitive_data);
        assert!(config.strip_control_chars);
        assert!(config.validate_json);
    }

    #[test]
    fn test_output_validation_config_debug() {
        let config = OutputValidationConfig::default();
        let debug_str = format!("{:?}", config);
        assert!(debug_str.contains("OutputValidationConfig"));
    }

    #[test]
    fn test_output_validation_config_clone() {
        let config = OutputValidationConfig::strict();
        let cloned = config.clone();
        assert_eq!(cloned.max_output_size, config.max_output_size);
    }

    #[test]
    fn test_output_validation_config_with_max_output_size() {
        let config = OutputValidationConfig::default().with_max_output_size(500);
        assert_eq!(config.max_output_size, 500);
    }

    #[test]
    fn test_output_validation_config_with_sensitive_check() {
        let config = OutputValidationConfig::default().with_sensitive_check(false);
        assert!(!config.check_sensitive_data);
    }

    // ==========================================================================
    // ValidationResult Additional Tests
    // ==========================================================================

    #[test]
    fn test_validation_result_valid() {
        let result = ValidationResult::Valid;
        assert!(result.is_valid());
        assert!(result.issues().is_none());
    }

    #[test]
    fn test_validation_result_invalid() {
        let issues = vec![ValidationIssue::ContainsControlChars];
        let result = ValidationResult::Invalid(issues);
        assert!(!result.is_valid());
        assert!(result.issues().is_some());
        assert_eq!(result.issues().unwrap().len(), 1);
    }

    #[test]
    fn test_validation_result_clone() {
        let result = ValidationResult::Valid;
        let cloned = result.clone();
        assert!(cloned.is_valid());
    }

    #[test]
    fn test_validation_result_eq() {
        assert_eq!(ValidationResult::Valid, ValidationResult::Valid);
        let issues = vec![ValidationIssue::ContainsControlChars];
        let invalid = ValidationResult::Invalid(issues.clone());
        assert_eq!(invalid, ValidationResult::Invalid(issues));
    }

    // ==========================================================================
    // ValidationIssue Tests
    // ==========================================================================

    #[test]
    fn test_validation_issue_output_too_large() {
        let issue = ValidationIssue::OutputTooLarge {
            size: 200,
            max: 100,
        };
        if let ValidationIssue::OutputTooLarge { size, max } = issue {
            assert_eq!(size, 200);
            assert_eq!(max, 100);
        }
    }

    #[test]
    fn test_validation_issue_json_too_deep() {
        let issue = ValidationIssue::JsonTooDeep { depth: 25, max: 20 };
        if let ValidationIssue::JsonTooDeep { depth, max } = issue {
            assert_eq!(depth, 25);
            assert_eq!(max, 20);
        }
    }

    #[test]
    fn test_validation_issue_sensitive_data() {
        let issue = ValidationIssue::SensitiveData {
            pattern: "password".to_string(),
        };
        if let ValidationIssue::SensitiveData { pattern } = issue {
            assert_eq!(pattern, "password");
        }
    }

    #[test]
    fn test_validation_issue_invalid_json() {
        let issue = ValidationIssue::InvalidJson {
            error: "unexpected token".to_string(),
        };
        if let ValidationIssue::InvalidJson { error } = issue {
            assert_eq!(error, "unexpected token");
        }
    }

    #[test]
    fn test_validation_issue_debug() {
        let issue = ValidationIssue::ContainsControlChars;
        let debug_str = format!("{:?}", issue);
        assert!(debug_str.contains("ContainsControlChars"));
    }

    #[test]
    fn test_validation_issue_clone() {
        let issue = ValidationIssue::OutputTooLarge { size: 100, max: 50 };
        let cloned = issue.clone();
        assert_eq!(issue, cloned);
    }

    // ==========================================================================
    // ToolTimeoutConfig Additional Tests
    // ==========================================================================

    #[test]
    fn test_tool_timeout_config_debug() {
        let config = ToolTimeoutConfig::default();
        let debug_str = format!("{:?}", config);
        assert!(debug_str.contains("ToolTimeoutConfig"));
    }

    #[test]
    fn test_tool_timeout_config_clone() {
        let config = ToolTimeoutConfig::quick();
        let cloned = config.clone();
        assert_eq!(cloned.default_timeout, config.default_timeout);
    }

    #[test]
    fn test_tool_timeout_config_new() {
        let config = ToolTimeoutConfig::new(Duration::from_secs(45));
        assert_eq!(config.default_timeout, Duration::from_secs(45));
        // Other fields should use default values
        assert_eq!(config.max_timeout, Duration::from_secs(300));
    }

    // ==========================================================================
    // TaskComplexity Additional Tests
    // ==========================================================================

    #[test]
    fn test_task_complexity_all_variants() {
        let simple = TaskComplexity::Simple;
        let moderate = TaskComplexity::Moderate;
        let complex = TaskComplexity::Complex;

        assert!(matches!(simple, TaskComplexity::Simple));
        assert!(matches!(moderate, TaskComplexity::Moderate));
        assert!(matches!(complex, TaskComplexity::Complex));
    }

    #[test]
    fn test_task_complexity_debug() {
        let complexity = TaskComplexity::Moderate;
        let debug_str = format!("{:?}", complexity);
        assert_eq!(debug_str, "Moderate");
    }

    #[test]
    fn test_task_complexity_eq() {
        assert_eq!(TaskComplexity::Simple, TaskComplexity::Simple);
        assert_ne!(TaskComplexity::Simple, TaskComplexity::Complex);
    }

    // ==========================================================================
    // ToolContext Additional Tests
    // ==========================================================================

    #[test]
    fn test_tool_context_new() {
        let ctx = ToolContext::new("test-agent");
        assert_eq!(ctx.agent_id, "test-agent");
        assert!(ctx.messages.is_empty());
        assert!(ctx.state.is_empty());
    }

    #[test]
    fn test_tool_context_state() {
        let mut ctx = ToolContext::new("test");
        ctx.set_state("key1", serde_json::json!("value1"));
        ctx.set_state("key2", serde_json::json!(42));

        assert_eq!(ctx.get_state("key1"), Some(&serde_json::json!("value1")));
        assert_eq!(ctx.get_state("key2"), Some(&serde_json::json!(42)));
        assert_eq!(ctx.get_state("nonexistent"), None);
    }

    #[test]
    fn test_tool_context_clone() {
        let ctx = ToolContext::new("agent-clone");
        let cloned = ctx.clone();
        assert_eq!(cloned.agent_id, "agent-clone");
    }

    // ==========================================================================
    // ToolCall Tests
    // ==========================================================================

    #[test]
    fn test_tool_call_creation() {
        let call = ToolCall {
            name: "search".to_string(),
            params: serde_json::json!({"query": "test"}),
        };
        assert_eq!(call.name, "search");
        assert_eq!(call.params["query"], "test");
    }

    #[test]
    fn test_tool_call_serialize() {
        let call = ToolCall {
            name: "calculate".to_string(),
            params: serde_json::json!({"a": 1, "b": 2}),
        };
        let json = serde_json::to_string(&call).expect("serialize");
        assert!(json.contains("calculate"));
    }

    #[test]
    fn test_tool_call_deserialize() {
        let json = r#"{"name":"test_tool","params":{"x":10}}"#;
        let call: ToolCall = serde_json::from_str(json).expect("deserialize");
        assert_eq!(call.name, "test_tool");
        assert_eq!(call.params["x"], 10);
    }

    #[test]
    fn test_tool_call_clone() {
        let call = ToolCall {
            name: "clone_test".to_string(),
            params: serde_json::json!({}),
        };
        let cloned = call.clone();
        assert_eq!(cloned.name, "clone_test");
    }

    #[test]
    fn test_tool_call_debug() {
        let call = ToolCall {
            name: "debug_test".to_string(),
            params: serde_json::json!({}),
        };
        let debug_str = format!("{:?}", call);
        assert!(debug_str.contains("debug_test"));
    }

    // ==========================================================================
    // ToolRegistry Tests
    // ==========================================================================

    #[test]
    fn test_tool_registry_new() {
        let registry = ToolRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_tool_registry_with_builtins() {
        let registry = ToolRegistry::with_builtins();
        assert!(!registry.is_empty());
        assert!(registry.get("calculator").is_some());
        assert!(registry.get("json").is_some());
        assert!(registry.get("datetime").is_some());
    }

    #[test]
    fn test_tool_registry_register_and_get() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(CalculatorTool));

        let tool = registry.get("calculator");
        assert!(tool.is_some());
        assert_eq!(tool.unwrap().name(), "calculator");
    }

    #[test]
    fn test_tool_registry_get_nonexistent() {
        let registry = ToolRegistry::new();
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_tool_registry_list() {
        let registry = ToolRegistry::with_builtins();
        let list = registry.list();
        assert!(list.contains(&"calculator"));
        assert!(list.contains(&"json"));
        assert!(list.contains(&"datetime"));
    }

    #[test]
    fn test_tool_registry_tools() {
        let registry = ToolRegistry::with_builtins();
        let tools = registry.tools();
        assert_eq!(tools.len(), 3);
    }

    #[test]
    fn test_tool_registry_len() {
        let mut registry = ToolRegistry::new();
        assert_eq!(registry.len(), 0);
        registry.register(Arc::new(CalculatorTool));
        assert_eq!(registry.len(), 1);
    }

    #[test]
    fn test_tool_registry_to_function_definitions() {
        let registry = ToolRegistry::with_builtins();
        let defs = registry.to_function_definitions();
        assert_eq!(defs.len(), 3);

        for def in &defs {
            assert_eq!(def["type"], "function");
            assert!(def["function"]["name"].is_string());
            assert!(def["function"]["description"].is_string());
        }
    }

    #[test]
    fn test_tool_registry_to_prompt_description() {
        let registry = ToolRegistry::with_builtins();
        let desc = registry.to_prompt_description();

        assert!(desc.contains("Available tools:"));
        assert!(desc.contains("calculator"));
        assert!(desc.contains("json"));
        assert!(desc.contains("datetime"));
    }

    #[tokio::test]
    async fn test_tool_registry_execute() {
        let registry = ToolRegistry::with_builtins();
        let ctx = ToolContext::new("test");
        let call = ToolCall {
            name: "calculator".to_string(),
            params: serde_json::json!({"expression": "3+3"}),
        };

        let result = registry.execute(&call, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.output.contains("6"));
    }

    #[tokio::test]
    async fn test_tool_registry_execute_not_found() {
        let registry = ToolRegistry::new();
        let ctx = ToolContext::new("test");
        let call = ToolCall {
            name: "nonexistent".to_string(),
            params: serde_json::json!({}),
        };

        let result = registry.execute(&call, &ctx).await;
        assert!(result.is_err());
    }

    // ==========================================================================
    // JsonTool Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_json_tool_parse() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "parse",
            "data": r#"{"name":"test","value":42}"#
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.data.is_some());
    }

    #[tokio::test]
    async fn test_json_tool_parse_invalid() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "parse",
            "data": "not valid json {"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(!result.success);
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn test_json_tool_format() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "format",
            "data": r#"{"a":1,"b":2}"#
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.output.contains('\n')); // Pretty printed
    }

    #[tokio::test]
    async fn test_json_tool_query() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "query",
            "data": r#"{"nested":{"value":123}}"#,
            "query": "nested.value"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.output.contains("123"));
    }

    #[tokio::test]
    async fn test_json_tool_query_not_found() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "query",
            "data": r#"{"a":1}"#,
            "query": "nonexistent.path"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(!result.success);
    }

    #[tokio::test]
    async fn test_json_tool_unknown_operation() {
        let tool = JsonTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "unknown",
            "data": "{}"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(!result.success);
        assert!(result.error.unwrap().contains("Unknown operation"));
    }

    #[test]
    fn test_json_tool_properties() {
        let tool = JsonTool;
        assert_eq!(tool.name(), "json");
        assert!(!tool.description().is_empty());
        assert!(matches!(tool.risk_level(), RiskLevel::Safe));
    }

    // ==========================================================================
    // DateTimeTool Tests
    // ==========================================================================

    #[tokio::test]
    async fn test_datetime_tool_now() {
        let tool = DateTimeTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({ "operation": "now" });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.data.is_some());
        assert!(result.data.as_ref().unwrap()["timestamp"].is_number());
    }

    #[tokio::test]
    async fn test_datetime_tool_format() {
        let tool = DateTimeTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "format",
            "format": "%Y-%m-%d"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
    }

    #[tokio::test]
    async fn test_datetime_tool_parse() {
        let tool = DateTimeTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "parse",
            "date": "2024-01-15"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
        assert!(result.data.is_some());
    }

    #[tokio::test]
    async fn test_datetime_tool_parse_iso8601() {
        let tool = DateTimeTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({
            "operation": "parse",
            "date": "2024-06-15T14:30:00Z"
        });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(result.success);
    }

    #[tokio::test]
    async fn test_datetime_tool_unknown_operation() {
        let tool = DateTimeTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({ "operation": "unknown" });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(!result.success);
    }

    #[test]
    fn test_datetime_tool_properties() {
        let tool = DateTimeTool;
        assert_eq!(tool.name(), "datetime");
        assert!(!tool.description().is_empty());
        assert!(matches!(tool.risk_level(), RiskLevel::Safe));
    }

    // ==========================================================================
    // CalculatorTool Additional Tests
    // ==========================================================================

    #[test]
    fn test_calculator_tool_properties() {
        let tool = CalculatorTool;
        assert_eq!(tool.name(), "calculator");
        assert!(!tool.description().is_empty());
        assert!(matches!(tool.risk_level(), RiskLevel::Safe));
    }

    #[tokio::test]
    async fn test_calculator_tool_missing_expression() {
        let tool = CalculatorTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({});

        let result = tool.execute(params, &ctx).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_calculator_tool_invalid_expression() {
        let tool = CalculatorTool;
        let ctx = ToolContext::new("test");
        let params = serde_json::json!({ "expression": "not a math expression" });

        let result = tool.execute(params, &ctx).await.unwrap();
        assert!(!result.success);
    }

    // ==========================================================================
    // evaluate_expression Additional Tests
    // ==========================================================================

    #[test]
    fn test_evaluate_expression_subtraction() {
        assert_eq!(evaluate_expression("10-3").unwrap(), 7.0);
    }

    #[test]
    fn test_evaluate_expression_division_by_zero() {
        let result = evaluate_expression("10/0");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Division by zero"));
    }

    #[test]
    fn test_evaluate_expression_parentheses() {
        // The simple evaluator handles parentheses when they wrap the whole expression
        assert_eq!(evaluate_expression("(42)").unwrap(), 42.0);
        assert_eq!(evaluate_expression("((7))").unwrap(), 7.0);
    }

    #[test]
    fn test_evaluate_expression_sqrt() {
        assert_eq!(evaluate_expression("sqrt(25)").unwrap(), 5.0);
        assert!((evaluate_expression("sqrt(2)").unwrap() - 1.414).abs() < 0.01);
    }

    #[test]
    fn test_evaluate_expression_simple_number() {
        assert_eq!(evaluate_expression("42").unwrap(), 42.0);
        assert_eq!(evaluate_expression("3.14").unwrap(), 3.14);
    }

    #[test]
    fn test_evaluate_expression_with_spaces() {
        assert_eq!(evaluate_expression("  2 + 3  ").unwrap(), 5.0);
    }

    // ==========================================================================
    // Helper Function Tests
    // ==========================================================================

    #[test]
    fn test_is_leap_year() {
        assert!(!is_leap_year(2023));
        assert!(is_leap_year(2024));
        assert!(!is_leap_year(1900));
        assert!(is_leap_year(2000));
    }

    #[test]
    fn test_format_unix_timestamp() {
        // Unix epoch
        let epoch = format_unix_timestamp(0);
        assert_eq!(epoch, "1970-01-01T00:00:00Z");

        // One day after epoch
        let one_day = format_unix_timestamp(86400);
        assert_eq!(one_day, "1970-01-02T00:00:00Z");
    }

    #[test]
    fn test_parse_datetime_iso8601() {
        let result = parse_datetime("2024-01-15T10:30:00Z");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_datetime_date_only() {
        let result = parse_datetime("2024-06-15");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_datetime_us_format() {
        let result = parse_datetime("06/15/2024");
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_datetime_invalid() {
        let result = parse_datetime("not a date");
        assert!(result.is_err());
    }

    #[test]
    fn test_day_of_year_to_month_day() {
        // Jan 1 (day 0)
        assert_eq!(day_of_year_to_month_day(0, false), (1, 1));
        // Jan 31 (day 30)
        assert_eq!(day_of_year_to_month_day(30, false), (1, 31));
        // Feb 1 (day 31)
        assert_eq!(day_of_year_to_month_day(31, false), (2, 1));
    }

    #[test]
    fn test_has_control_chars() {
        assert!(!has_control_chars("normal text"));
        assert!(!has_control_chars("with\nnewline"));
        assert!(!has_control_chars("with\ttab"));
        assert!(has_control_chars("with\x00null"));
        assert!(has_control_chars("with\x1Bescape"));
    }

    #[test]
    fn test_query_json_array() {
        let json = serde_json::json!(["a", "b", "c"]);
        assert_eq!(query_json(&json, "1"), Some(serde_json::json!("b")));
    }

    #[test]
    fn test_query_json_empty() {
        let json = serde_json::json!({"key": "value"});
        assert_eq!(query_json(&json, ""), Some(json.clone()));
    }

    #[test]
    fn test_query_json_deep_nesting() {
        let json = serde_json::json!({
            "a": {
                "b": {
                    "c": 42
                }
            }
        });
        assert_eq!(query_json(&json, "a.b.c"), Some(serde_json::json!(42)));
    }

    #[test]
    fn test_format_with_pattern() {
        // Known timestamp: 2024-01-15 00:00:00 UTC
        let timestamp = 1705276800;
        let formatted = format_with_pattern(timestamp, "%Y-%m-%d");
        assert!(formatted.contains("2024"));
    }

    #[test]
    fn test_sensitive_patterns_detection() {
        let config = OutputValidationConfig::default();

        // Test various sensitive patterns
        let test_cases = vec![
            "my password is secret",
            "api_key: abc123",
            "bearer token here",
            "private_key data",
            "credential info",
        ];

        for test in test_cases {
            let result = ToolResult::success(test);
            assert!(
                !result.validate(&config).is_valid(),
                "Should detect sensitive data in: {}",
                test
            );
        }
    }
}