ucotron-config 0.1.0

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

use serde::{Deserialize, Serialize};

/// Top-level Ucotron configuration.
///
/// Parsed from `ucotron.toml` or constructed programmatically.
/// Environment variables with the `UCOTRON_` prefix override TOML values.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UcotronConfig {
    /// HTTP server settings.
    #[serde(default)]
    pub server: ServerConfig,
    /// Storage backend configuration.
    #[serde(default)]
    pub storage: StorageConfig,
    /// ML model configuration.
    #[serde(default)]
    pub models: ModelsConfig,
    /// Background consolidation settings.
    #[serde(default)]
    pub consolidation: ConsolidationConfig,
    /// MCP server settings.
    #[serde(default)]
    pub mcp: McpConfig,
    /// Multi-tenancy namespace configuration.
    #[serde(default)]
    pub namespaces: NamespacesConfig,
    /// Authentication settings.
    #[serde(default)]
    pub auth: AuthConfig,
    /// Multi-instance configuration.
    #[serde(default)]
    pub instance: InstanceConfig,
    /// GDPR compliance configuration.
    #[serde(default)]
    pub gdpr: GdprConfig,
    /// Audit logging configuration.
    #[serde(default)]
    pub audit: AuditConfig,
    /// OpenTelemetry configuration.
    #[serde(default)]
    pub telemetry: TelemetryConfig,
    /// Mindset auto-detection configuration.
    #[serde(default)]
    pub mindset: MindsetDetectorConfig,
    /// Connector scheduling configuration.
    #[serde(default)]
    pub connectors: ConnectorsConfig,
}

/// HTTP server configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// Bind address (default: "0.0.0.0").
    #[serde(default = "default_host")]
    pub host: String,
    /// HTTP port (default: 8420).
    #[serde(default = "default_port")]
    pub port: u16,
    /// Number of worker threads (default: 4).
    #[serde(default = "default_workers")]
    pub workers: usize,
    /// Log level (default: "info").
    #[serde(default = "default_log_level")]
    pub log_level: String,
    /// Log format: "text" (default) or "json" for structured JSON logging with trace IDs.
    #[serde(default = "default_log_format")]
    pub log_format: String,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            host: default_host(),
            port: default_port(),
            workers: default_workers(),
            log_level: default_log_level(),
            log_format: default_log_format(),
        }
    }
}

fn default_host() -> String {
    "0.0.0.0".to_string()
}
fn default_port() -> u16 {
    8420
}
fn default_workers() -> usize {
    4
}
fn default_log_level() -> String {
    "info".to_string()
}
fn default_log_format() -> String {
    "text".to_string()
}

/// Storage backend configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    /// Storage mode: "embedded" (default) or "external".
    #[serde(default = "default_storage_mode")]
    pub mode: String,
    /// Shared data directory for multi-instance mode.
    /// When `mode = "shared"`, all instances must point to the same directory.
    /// Both vector and graph backends will use sub-directories under this path.
    /// If set, overrides `vector.data_dir` and `graph.data_dir`.
    #[serde(default)]
    pub shared_data_dir: Option<String>,
    /// Directory for persisting uploaded media files (images, audio, video).
    /// Defaults to "data/media". Files are stored as `{node_id}.{ext}`.
    #[serde(default = "default_media_dir")]
    pub media_dir: String,
    /// Vector backend configuration.
    #[serde(default)]
    pub vector: VectorBackendConfig,
    /// Graph backend configuration.
    #[serde(default)]
    pub graph: GraphBackendConfig,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            mode: default_storage_mode(),
            shared_data_dir: None,
            media_dir: default_media_dir(),
            vector: VectorBackendConfig::default(),
            graph: GraphBackendConfig::default(),
        }
    }
}

impl StorageConfig {
    /// Returns the effective data directory for vector storage.
    /// In shared mode with `shared_data_dir` set, returns the shared path.
    /// Otherwise returns the backend's own `data_dir`.
    pub fn effective_vector_data_dir(&self) -> &str {
        if self.mode == "shared" {
            if let Some(ref dir) = self.shared_data_dir {
                return dir;
            }
        }
        &self.vector.data_dir
    }

    /// Returns the effective data directory for graph storage.
    /// In shared mode with `shared_data_dir` set, returns the shared path.
    /// Otherwise returns the backend's own `data_dir`.
    pub fn effective_graph_data_dir(&self) -> &str {
        if self.mode == "shared" {
            if let Some(ref dir) = self.shared_data_dir {
                return dir;
            }
        }
        &self.graph.data_dir
    }

    /// Returns the media storage directory path.
    pub fn effective_media_dir(&self) -> &str {
        &self.media_dir
    }
}

fn default_storage_mode() -> String {
    "embedded".to_string()
}

/// Vector backend settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VectorBackendConfig {
    /// Backend type: "helix" (default), "qdrant", "custom".
    #[serde(default = "default_backend")]
    pub backend: String,
    /// Data directory for embedded backends.
    #[serde(default = "default_data_dir")]
    pub data_dir: String,
    /// Maximum database size in bytes (for LMDB map_size).
    #[serde(default = "default_max_db_size")]
    pub max_db_size: u64,
    /// External service URL (for qdrant, etc.).
    pub url: Option<String>,
    /// HNSW configuration (only used when vector index is HNSW).
    #[serde(default)]
    pub hnsw: HnswConfig,
}

/// HNSW vector index parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HnswConfig {
    /// Number of bi-directional links per node (default: 24).
    /// Higher values improve recall at the cost of memory and build time.
    #[serde(default = "default_hnsw_ef_construction")]
    pub ef_construction: usize,
    /// Search parameter: number of candidates to evaluate during search (default: 200).
    #[serde(default = "default_hnsw_ef_search")]
    pub ef_search: usize,
    /// Enable HNSW index (default: true). When false, falls back to brute-force SIMD.
    #[serde(default = "default_hnsw_enabled")]
    pub enabled: bool,
}

impl Default for HnswConfig {
    fn default() -> Self {
        Self {
            ef_construction: default_hnsw_ef_construction(),
            ef_search: default_hnsw_ef_search(),
            enabled: default_hnsw_enabled(),
        }
    }
}

fn default_hnsw_ef_construction() -> usize {
    200
}
fn default_hnsw_ef_search() -> usize {
    200
}
fn default_hnsw_enabled() -> bool {
    true
}

impl Default for VectorBackendConfig {
    fn default() -> Self {
        Self {
            backend: default_backend(),
            data_dir: default_data_dir(),
            max_db_size: default_max_db_size(),
            url: None,
            hnsw: HnswConfig::default(),
        }
    }
}

/// Graph backend settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphBackendConfig {
    /// Backend type: "helix" (default), "falkordb", "custom".
    #[serde(default = "default_backend")]
    pub backend: String,
    /// Data directory for embedded backends.
    #[serde(default = "default_data_dir")]
    pub data_dir: String,
    /// Maximum database size in bytes.
    #[serde(default = "default_max_db_size")]
    pub max_db_size: u64,
    /// Batch size for bulk operations.
    #[serde(default = "default_batch_size")]
    pub batch_size: usize,
    /// External service URL (for falkordb, etc.).
    pub url: Option<String>,
}

impl Default for GraphBackendConfig {
    fn default() -> Self {
        Self {
            backend: default_backend(),
            data_dir: default_data_dir(),
            max_db_size: default_max_db_size(),
            batch_size: default_batch_size(),
            url: None,
        }
    }
}

fn default_backend() -> String {
    "helix".to_string()
}
fn default_data_dir() -> String {
    "data".to_string()
}
fn default_media_dir() -> String {
    "data/media".to_string()
}
fn default_max_db_size() -> u64 {
    10 * 1024 * 1024 * 1024 // 10GB
}
fn default_batch_size() -> usize {
    10_000
}

/// ML model configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelsConfig {
    /// Embedding model name (default: "all-MiniLM-L6-v2").
    #[serde(default = "default_embedding_model")]
    pub embedding_model: String,
    /// NER model name (default: "gliner-multi-v2.1").
    #[serde(default = "default_ner_model")]
    pub ner_model: String,
    /// LLM model for relation extraction (default: "Qwen3-4B-GGUF").
    #[serde(default = "default_llm_model")]
    pub llm_model: String,
    /// LLM backend: "candle" or "llama_cpp" (default: "candle").
    #[serde(default = "default_llm_backend")]
    pub llm_backend: String,
    /// CLIP model name for image embedding (default: "clip-vit-base-patch32").
    #[serde(default = "default_clip_model")]
    pub clip_model: String,
    /// Directory for storing model files.
    #[serde(default = "default_models_dir")]
    pub models_dir: String,
    /// Enable document OCR pipeline (default: true).
    #[serde(default = "default_enable_ocr")]
    pub enable_ocr: bool,
    /// Language for Tesseract OCR (default: "eng").
    #[serde(default = "default_ocr_language")]
    pub ocr_language: String,
    /// Path to the tesseract binary (default: "tesseract", relies on PATH).
    #[serde(default = "default_tesseract_path")]
    pub tesseract_path: String,
    /// Fine-tuned relation extraction model name on Fireworks (e.g., "accounts/ucotron/models/re-qwen2-5-7b").
    /// When set and non-empty, the extraction pipeline will use this model via Fireworks API
    /// instead of co-occurrence. Falls back to co-occurrence on API errors.
    #[serde(default)]
    pub fine_tuned_re_model: String,
    /// Fireworks inference API endpoint (default: "https://api.fireworks.ai/inference/v1").
    #[serde(default = "default_fine_tuned_re_endpoint")]
    pub fine_tuned_re_endpoint: String,
    /// Name of the environment variable holding the Fireworks API key (default: "FIREWORKS_API_KEY").
    /// The actual key is read from this env var at runtime — never stored in config files.
    #[serde(default = "default_fine_tuned_re_api_key_env")]
    pub fine_tuned_re_api_key_env: String,
}

impl Default for ModelsConfig {
    fn default() -> Self {
        Self {
            embedding_model: default_embedding_model(),
            ner_model: default_ner_model(),
            llm_model: default_llm_model(),
            llm_backend: default_llm_backend(),
            clip_model: default_clip_model(),
            models_dir: default_models_dir(),
            enable_ocr: default_enable_ocr(),
            ocr_language: default_ocr_language(),
            tesseract_path: default_tesseract_path(),
            fine_tuned_re_model: String::new(),
            fine_tuned_re_endpoint: default_fine_tuned_re_endpoint(),
            fine_tuned_re_api_key_env: default_fine_tuned_re_api_key_env(),
        }
    }
}

fn default_embedding_model() -> String {
    "all-MiniLM-L6-v2".to_string()
}
fn default_ner_model() -> String {
    "gliner-multi-v2.1".to_string()
}
fn default_llm_model() -> String {
    "Qwen3-4B-GGUF".to_string()
}
fn default_llm_backend() -> String {
    "candle".to_string()
}
fn default_clip_model() -> String {
    "clip-vit-base-patch32".to_string()
}
fn default_models_dir() -> String {
    "models".to_string()
}
fn default_enable_ocr() -> bool {
    true
}
fn default_ocr_language() -> String {
    "eng".to_string()
}
fn default_tesseract_path() -> String {
    "tesseract".to_string()
}
fn default_fine_tuned_re_endpoint() -> String {
    "https://api.fireworks.ai/inference/v1".to_string()
}
fn default_fine_tuned_re_api_key_env() -> String {
    "FIREWORKS_API_KEY".to_string()
}

/// Background consolidation worker configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsolidationConfig {
    /// Number of messages between consolidation runs (default: 100).
    #[serde(default = "default_trigger_interval")]
    pub trigger_interval: usize,
    /// Enable memory decay for old nodes (default: true).
    #[serde(default = "default_enable_decay")]
    pub enable_decay: bool,
    /// Decay half-life in seconds (default: 30 days).
    #[serde(default = "default_decay_halflife")]
    pub decay_halflife_secs: u64,
}

impl Default for ConsolidationConfig {
    fn default() -> Self {
        Self {
            trigger_interval: default_trigger_interval(),
            enable_decay: default_enable_decay(),
            decay_halflife_secs: default_decay_halflife(),
        }
    }
}

fn default_trigger_interval() -> usize {
    100
}
fn default_enable_decay() -> bool {
    true
}
fn default_decay_halflife() -> u64 {
    30 * 24 * 3600 // 30 days
}

/// MCP (Model Context Protocol) server configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct McpConfig {
    /// Enable MCP server (default: true).
    #[serde(default = "default_mcp_enabled")]
    pub enabled: bool,
    /// Transport mode: "stdio" or "sse" (default: "stdio").
    #[serde(default = "default_mcp_transport")]
    pub transport: String,
    /// SSE port (only used when transport = "sse", default: 8421).
    #[serde(default = "default_mcp_port")]
    pub port: u16,
}

impl Default for McpConfig {
    fn default() -> Self {
        Self {
            enabled: default_mcp_enabled(),
            transport: default_mcp_transport(),
            port: default_mcp_port(),
        }
    }
}

fn default_mcp_enabled() -> bool {
    true
}
fn default_mcp_transport() -> String {
    "stdio".to_string()
}
fn default_mcp_port() -> u16 {
    8421
}

/// Multi-tenancy namespace configuration.
///
/// Namespaces isolate memory data between different tenants,
/// projects, users, agents, or threads.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespacesConfig {
    /// Default namespace when no `X-Ucotron-Namespace` header is provided.
    #[serde(default = "default_namespace")]
    pub default_namespace: String,
    /// If non-empty, only these namespaces are allowed.
    /// Empty means any namespace is allowed.
    #[serde(default)]
    pub allowed_namespaces: Vec<String>,
    /// Maximum number of namespaces allowed (0 = unlimited).
    #[serde(default)]
    pub max_namespaces: usize,
}

impl Default for NamespacesConfig {
    fn default() -> Self {
        Self {
            default_namespace: default_namespace(),
            allowed_namespaces: Vec::new(),
            max_namespaces: 0,
        }
    }
}

fn default_namespace() -> String {
    "default".to_string()
}

/// Authentication configuration.
///
/// Optional authentication for the REST API and MCP server.
/// When `enabled` is false (default), all requests are accepted.
///
/// RBAC roles (ordered by privilege):
/// - `admin`: full access including API key management and admin endpoints
/// - `writer`: read + write (ingest, learn, update, delete, GDPR)
/// - `reader`: read-only (search, augment, get, list, export)
/// - `viewer`: health + metrics only
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Enable authentication (default: false).
    #[serde(default)]
    pub enabled: bool,
    /// Legacy single API key for simple auth (checked via `Authorization: Bearer <key>` header).
    /// Set via TOML or `UCOTRON_AUTH_API_KEY` env var.
    /// When used alone (without `api_keys`), grants `admin` role.
    #[serde(default)]
    pub api_key: Option<String>,
    /// JWT secret for token-based auth (future use).
    #[serde(default)]
    pub jwt_secret: Option<String>,
    /// JWT issuer (future use).
    #[serde(default)]
    pub jwt_issuer: Option<String>,
    /// Named API keys with role-based access control.
    /// Each key has a role and optional namespace scope.
    #[serde(default)]
    pub api_keys: Vec<ApiKeyEntry>,
}

/// A named API key with role and optional namespace scope.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKeyEntry {
    /// Human-readable name for this key (e.g., "backend-service", "analytics-reader").
    pub name: String,
    /// The secret key value (checked via `Authorization: Bearer <key>` header).
    pub key: String,
    /// Role assigned to this key: "admin", "writer", "reader", or "viewer".
    #[serde(default = "default_api_key_role")]
    pub role: String,
    /// Optional namespace scope. If set, this key can only access the specified namespace.
    /// If empty/unset, the key can access all namespaces.
    #[serde(default)]
    pub namespace: Option<String>,
    /// Whether this key is active. Set to false to revoke without deleting.
    #[serde(default = "default_true")]
    pub active: bool,
}

fn default_api_key_role() -> String {
    "reader".to_string()
}

fn default_true() -> bool {
    true
}

/// RBAC role with ordered privilege levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum AuthRole {
    Viewer = 0,
    Reader = 1,
    Writer = 2,
    Admin = 3,
}

impl AuthRole {
    /// Parse a role string into an AuthRole.
    pub fn parse_role(s: &str) -> Option<Self> {
        match s {
            "admin" => Some(AuthRole::Admin),
            "writer" => Some(AuthRole::Writer),
            "reader" => Some(AuthRole::Reader),
            "viewer" => Some(AuthRole::Viewer),
            _ => None,
        }
    }

    /// Whether this role has at least the given privilege level.
    pub fn has_privilege(&self, required: AuthRole) -> bool {
        (*self as u8) >= (required as u8)
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            AuthRole::Admin => "admin",
            AuthRole::Writer => "writer",
            AuthRole::Reader => "reader",
            AuthRole::Viewer => "viewer",
        }
    }
}

impl AuthConfig {
    /// Look up an API key and return its role and namespace scope.
    /// Checks named `api_keys` first, then falls back to legacy `api_key` (admin role).
    pub fn authenticate(&self, bearer_token: &str) -> Option<(AuthRole, Option<String>)> {
        // Check named API keys first.
        for entry in &self.api_keys {
            if entry.active && entry.key == bearer_token {
                if let Some(role) = AuthRole::parse_role(&entry.role) {
                    return Some((role, entry.namespace.clone()));
                }
            }
        }
        // Fall back to legacy single API key (grants admin).
        if let Some(ref legacy_key) = self.api_key {
            if legacy_key == bearer_token {
                return Some((AuthRole::Admin, None));
            }
        }
        None
    }
}

/// Multi-instance configuration.
///
/// Controls how this server instance participates in a multi-instance deployment.
/// In single-instance mode (default), all settings can be left at defaults.
///
/// For multi-instance deployments:
/// - Each instance needs a unique `instance_id`
/// - `role` determines whether this instance can write (`writer`), only read (`reader`), or auto-detect
/// - `id_range_start` and `id_range_size` partition the node ID space to avoid collisions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstanceConfig {
    /// Unique identifier for this server instance.
    /// Auto-generated from hostname + PID if not set.
    #[serde(default = "default_instance_id")]
    pub instance_id: String,
    /// Instance role: "auto" (default), "writer", or "reader".
    /// - "auto": single-instance mode, acts as both reader and writer
    /// - "writer": can perform writes (ingestion, learn, update, delete)
    /// - "reader": read-only (search, augment, get operations only)
    #[serde(default = "default_instance_role")]
    pub role: String,
    /// Starting node ID for this instance's ID allocation range.
    /// Each instance should have a non-overlapping range to avoid ID collisions.
    /// Default: 1_000_000 (same as single-instance).
    #[serde(default = "default_id_range_start")]
    pub id_range_start: u64,
    /// Size of this instance's node ID allocation range.
    /// Default: 1_000_000_000 (1 billion IDs per instance).
    #[serde(default = "default_id_range_size")]
    pub id_range_size: u64,
}

impl Default for InstanceConfig {
    fn default() -> Self {
        Self {
            instance_id: default_instance_id(),
            role: default_instance_role(),
            id_range_start: default_id_range_start(),
            id_range_size: default_id_range_size(),
        }
    }
}

fn default_instance_id() -> String {
    "auto".to_string()
}
fn default_instance_role() -> String {
    "auto".to_string()
}
fn default_id_range_start() -> u64 {
    1_000_000
}
fn default_id_range_size() -> u64 {
    1_000_000_000
}

impl InstanceConfig {
    /// Resolve the instance_id. If set to "auto", generate from hostname + PID.
    pub fn resolved_instance_id(&self) -> String {
        if self.instance_id == "auto" {
            let hostname = hostname::get()
                .ok()
                .and_then(|h| h.into_string().ok())
                .unwrap_or_else(|| "unknown".to_string());
            let pid = std::process::id();
            format!("{}-{}", hostname, pid)
        } else {
            self.instance_id.clone()
        }
    }

    /// Whether this instance can perform write operations.
    pub fn can_write(&self) -> bool {
        matches!(self.role.as_str(), "auto" | "writer")
    }

    /// Whether this instance is a dedicated reader (no writes).
    pub fn is_reader_only(&self) -> bool {
        self.role == "reader"
    }
}

/// GDPR compliance configuration.
///
/// Controls data retention policies and right-to-be-forgotten behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdprConfig {
    /// Enable GDPR endpoints (default: true).
    #[serde(default = "default_gdpr_enabled")]
    pub enabled: bool,
    /// Default data retention TTL in seconds (0 = no automatic expiry).
    #[serde(default)]
    pub default_retention_ttl_secs: u64,
    /// Per-namespace retention policies: list of {namespace, ttl_secs}.
    #[serde(default)]
    pub retention_policies: Vec<GdprRetentionPolicyConfig>,
}

/// A single retention policy entry in configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GdprRetentionPolicyConfig {
    /// Namespace this policy applies to ("*" = all namespaces).
    pub namespace: String,
    /// Time-to-live in seconds (0 = no expiry).
    pub ttl_secs: u64,
}

impl Default for GdprConfig {
    fn default() -> Self {
        Self {
            enabled: default_gdpr_enabled(),
            default_retention_ttl_secs: 0,
            retention_policies: Vec::new(),
        }
    }
}

fn default_gdpr_enabled() -> bool {
    true
}

/// Audit logging configuration.
///
/// Controls the immutable audit trail that records all API operations.
/// Audit entries are stored in an append-only in-memory log and persisted
/// as special graph nodes for durability.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditConfig {
    /// Enable audit logging (default: true).
    #[serde(default = "default_audit_enabled")]
    pub enabled: bool,
    /// Retention period for audit entries in seconds.
    /// Entries older than this are eligible for pruning.
    /// 0 = keep forever. Default: 7776000 (90 days).
    #[serde(default = "default_audit_retention_secs")]
    pub retention_secs: u64,
    /// Maximum number of audit entries kept in memory.
    /// Oldest entries are evicted when this limit is exceeded.
    /// Default: 100000.
    #[serde(default = "default_audit_max_entries")]
    pub max_entries: usize,
}

impl Default for AuditConfig {
    fn default() -> Self {
        Self {
            enabled: default_audit_enabled(),
            retention_secs: default_audit_retention_secs(),
            max_entries: default_audit_max_entries(),
        }
    }
}

fn default_audit_enabled() -> bool {
    true
}

fn default_audit_retention_secs() -> u64 {
    7_776_000 // 90 days
}

fn default_audit_max_entries() -> usize {
    100_000
}

/// OpenTelemetry configuration.
///
/// Controls OTLP trace/metric/log export to an OpenTelemetry collector.
/// Disabled by default — enable and point to a collector (e.g., Jaeger, Grafana Tempo).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryConfig {
    /// Enable OTLP telemetry export (default: false).
    #[serde(default)]
    pub enabled: bool,
    /// OTLP gRPC collector endpoint (default: "http://localhost:4317").
    #[serde(default = "default_telemetry_otlp_endpoint")]
    pub otlp_endpoint: String,
    /// Service name reported in OTLP traces (default: "ucotron").
    #[serde(default = "default_telemetry_service_name")]
    pub service_name: String,
    /// Trace sampling ratio, 0.0 to 1.0 (default: 1.0 = sample everything).
    #[serde(default = "default_telemetry_sample_rate")]
    pub sample_rate: f64,
    /// Export traces via OTLP (default: true).
    #[serde(default = "default_true")]
    pub export_traces: bool,
    /// Export metrics via OTLP (default: true).
    #[serde(default = "default_true")]
    pub export_metrics: bool,
    /// Export logs via OTLP (default: false).
    #[serde(default)]
    pub export_logs: bool,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            otlp_endpoint: default_telemetry_otlp_endpoint(),
            service_name: default_telemetry_service_name(),
            sample_rate: default_telemetry_sample_rate(),
            export_traces: true,
            export_metrics: true,
            export_logs: false,
        }
    }
}

fn default_telemetry_otlp_endpoint() -> String {
    "http://localhost:4317".to_string()
}
fn default_telemetry_service_name() -> String {
    "ucotron".to_string()
}
fn default_telemetry_sample_rate() -> f64 {
    1.0
}

/// Mindset auto-detection configuration.
///
/// Controls automatic detection of cognitive mindset (Convergent, Divergent,
/// Algorithmic) from query keywords. When enabled and no explicit mindset is
/// provided in the search request, the system scans for keyword patterns.
///
/// ```toml
/// [mindset]
/// enabled = true
/// algorithmic_keywords = ["verify", "confirm", "check", "validate", "prove", "correct"]
/// divergent_keywords = ["what if", "explore", "brainstorm", "alternative", "imagine", "creative"]
/// convergent_keywords = ["summarize", "consensus", "agree", "common", "overview", "conclude"]
/// spatial_keywords = ["connected", "path", "route", "bridge", "relationship", "link", "network", "graph"]
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MindsetDetectorConfig {
    /// Enable automatic mindset detection from query keywords (default: true).
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Keywords that trigger Algorithmic mindset (verification, logical checking).
    #[serde(default = "default_algorithmic_keywords")]
    pub algorithmic_keywords: Vec<String>,
    /// Keywords that trigger Divergent mindset (exploration, brainstorming).
    #[serde(default = "default_divergent_keywords")]
    pub divergent_keywords: Vec<String>,
    /// Keywords that trigger Convergent mindset (synthesis, consensus).
    #[serde(default = "default_convergent_keywords")]
    pub convergent_keywords: Vec<String>,
    /// Keywords that trigger Spatial mindset (graph traversal, path-based reasoning).
    #[serde(default = "default_spatial_keywords")]
    pub spatial_keywords: Vec<String>,
}

impl Default for MindsetDetectorConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            algorithmic_keywords: default_algorithmic_keywords(),
            divergent_keywords: default_divergent_keywords(),
            convergent_keywords: default_convergent_keywords(),
            spatial_keywords: default_spatial_keywords(),
        }
    }
}

fn default_algorithmic_keywords() -> Vec<String> {
    vec![
        "verify".into(),
        "confirm".into(),
        "check".into(),
        "validate".into(),
        "prove".into(),
        "correct".into(),
    ]
}

fn default_divergent_keywords() -> Vec<String> {
    vec![
        "what if".into(),
        "explore".into(),
        "brainstorm".into(),
        "alternative".into(),
        "imagine".into(),
        "creative".into(),
    ]
}

fn default_convergent_keywords() -> Vec<String> {
    vec![
        "summarize".into(),
        "consensus".into(),
        "agree".into(),
        "common".into(),
        "overview".into(),
        "conclude".into(),
    ]
}

fn default_spatial_keywords() -> Vec<String> {
    vec![
        "connected".into(),
        "path".into(),
        "route".into(),
        "bridge".into(),
        "relationship".into(),
        "link".into(),
        "network".into(),
        "graph".into(),
    ]
}

/// Connector scheduling configuration.
///
/// Controls cron-based periodic sync for external data source connectors
/// (Slack, GitHub, Notion, etc.). Individual connector schedules are
/// configured as entries in the `[[connectors.schedules]]` array.
///
/// ```toml
/// [connectors]
/// enabled = true
/// check_interval_secs = 60
///
/// [[connectors.schedules]]
/// connector_id = "my-slack"
/// cron_expression = "0 */6 * * * *"
/// timeout_secs = 300
/// max_retries = 3
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectorsConfig {
    /// Enable the connector scheduler (default: false).
    #[serde(default)]
    pub enabled: bool,
    /// How often the scheduler checks for due cron jobs (seconds, default: 60).
    #[serde(default = "default_connector_check_interval")]
    pub check_interval_secs: u64,
    /// Connector schedule entries.
    #[serde(default)]
    pub schedules: Vec<ConnectorScheduleEntry>,
}

impl Default for ConnectorsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            check_interval_secs: default_connector_check_interval(),
            schedules: Vec::new(),
        }
    }
}

/// A single connector schedule entry in the configuration file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectorScheduleEntry {
    /// Connector instance ID (must match a registered connector).
    pub connector_id: String,
    /// Cron expression for periodic sync (e.g., "0 */6 * * * *").
    /// Uses 6-field format: sec min hour day month weekday.
    pub cron_expression: Option<String>,
    /// Whether this schedule is active (default: true).
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Timeout for a single sync operation in seconds (default: 300).
    #[serde(default = "default_connector_timeout")]
    pub timeout_secs: u64,
    /// Number of retries on sync failure (default: 3).
    #[serde(default = "default_connector_retries")]
    pub max_retries: u32,
}

fn default_connector_check_interval() -> u64 {
    60
}

fn default_connector_timeout() -> u64 {
    300
}

fn default_connector_retries() -> u32 {
    3
}

impl UcotronConfig {
    /// Load configuration from a TOML file, then apply environment variable overrides.
    pub fn from_file(path: &str) -> anyhow::Result<Self> {
        let contents = std::fs::read_to_string(path)
            .map_err(|e| anyhow::anyhow!("Failed to read config file '{}': {}", path, e))?;
        Self::parse_toml(&contents)
    }

    /// Parse configuration from a TOML string, apply env overrides, then validate.
    pub fn parse_toml(toml_str: &str) -> anyhow::Result<Self> {
        let mut config: UcotronConfig = toml::from_str(toml_str)
            .map_err(|e| anyhow::anyhow!("Failed to parse TOML config: {}", e))?;
        config.apply_env_overrides();
        config.validate()?;
        Ok(config)
    }

    /// Apply environment variable overrides to the configuration.
    ///
    /// Variables use the `UCOTRON_` prefix with `_` as section separator:
    /// - `UCOTRON_SERVER_HOST` → `server.host`
    /// - `UCOTRON_SERVER_PORT` → `server.port`
    /// - `UCOTRON_SERVER_WORKERS` → `server.workers`
    /// - `UCOTRON_SERVER_LOG_LEVEL` → `server.log_level`
    /// - `UCOTRON_SERVER_LOG_FORMAT` → `server.log_format`
    /// - `UCOTRON_STORAGE_MODE` → `storage.mode`
    /// - `UCOTRON_STORAGE_VECTOR_BACKEND` → `storage.vector.backend`
    /// - `UCOTRON_STORAGE_VECTOR_DATA_DIR` → `storage.vector.data_dir`
    /// - `UCOTRON_STORAGE_GRAPH_BACKEND` → `storage.graph.backend`
    /// - `UCOTRON_STORAGE_GRAPH_DATA_DIR` → `storage.graph.data_dir`
    /// - `UCOTRON_STORAGE_GRAPH_BATCH_SIZE` → `storage.graph.batch_size`
    /// - `UCOTRON_MODELS_EMBEDDING_MODEL` → `models.embedding_model`
    /// - `UCOTRON_MODELS_NER_MODEL` → `models.ner_model`
    /// - `UCOTRON_MODELS_LLM_MODEL` → `models.llm_model`
    /// - `UCOTRON_MODELS_LLM_BACKEND` → `models.llm_backend`
    /// - `UCOTRON_MODELS_DIR` → `models.models_dir`
    /// - `UCOTRON_CONSOLIDATION_TRIGGER_INTERVAL` → `consolidation.trigger_interval`
    /// - `UCOTRON_CONSOLIDATION_ENABLE_DECAY` → `consolidation.enable_decay`
    /// - `UCOTRON_CONSOLIDATION_DECAY_HALFLIFE_SECS` → `consolidation.decay_halflife_secs`
    /// - `UCOTRON_MCP_ENABLED` → `mcp.enabled`
    /// - `UCOTRON_MCP_TRANSPORT` → `mcp.transport`
    /// - `UCOTRON_MCP_PORT` → `mcp.port`
    /// - `UCOTRON_NAMESPACES_DEFAULT` → `namespaces.default_namespace`
    /// - `UCOTRON_AUTH_ENABLED` → `auth.enabled`
    /// - `UCOTRON_AUTH_API_KEY` → `auth.api_key`
    /// - `UCOTRON_AUTH_JWT_SECRET` → `auth.jwt_secret`
    /// - `UCOTRON_TELEMETRY_ENABLED` → `telemetry.enabled`
    /// - `UCOTRON_TELEMETRY_OTLP_ENDPOINT` → `telemetry.otlp_endpoint`
    /// - `UCOTRON_TELEMETRY_SERVICE_NAME` → `telemetry.service_name`
    /// - `UCOTRON_TELEMETRY_SAMPLE_RATE` → `telemetry.sample_rate`
    pub fn apply_env_overrides(&mut self) {
        // Server overrides
        if let Ok(v) = std::env::var("UCOTRON_SERVER_HOST") {
            self.server.host = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_SERVER_PORT") {
            if let Ok(port) = v.parse::<u16>() {
                self.server.port = port;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_SERVER_WORKERS") {
            if let Ok(w) = v.parse::<usize>() {
                self.server.workers = w;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_SERVER_LOG_LEVEL") {
            self.server.log_level = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_SERVER_LOG_FORMAT") {
            self.server.log_format = v;
        }

        // Storage overrides
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_MODE") {
            self.storage.mode = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_SHARED_DATA_DIR") {
            self.storage.shared_data_dir = Some(v);
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_MEDIA_DIR") {
            self.storage.media_dir = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_VECTOR_BACKEND") {
            self.storage.vector.backend = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_VECTOR_DATA_DIR") {
            self.storage.vector.data_dir = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_GRAPH_BACKEND") {
            self.storage.graph.backend = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_GRAPH_DATA_DIR") {
            self.storage.graph.data_dir = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_STORAGE_GRAPH_BATCH_SIZE") {
            if let Ok(bs) = v.parse::<usize>() {
                self.storage.graph.batch_size = bs;
            }
        }

        // Models overrides
        if let Ok(v) = std::env::var("UCOTRON_MODELS_EMBEDDING_MODEL") {
            self.models.embedding_model = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_NER_MODEL") {
            self.models.ner_model = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_LLM_MODEL") {
            self.models.llm_model = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_LLM_BACKEND") {
            self.models.llm_backend = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_DIR") {
            self.models.models_dir = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_ENABLE_OCR") {
            if let Ok(b) = v.parse::<bool>() {
                self.models.enable_ocr = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_OCR_LANGUAGE") {
            self.models.ocr_language = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_TESSERACT_PATH") {
            self.models.tesseract_path = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_FINE_TUNED_RE_MODEL") {
            self.models.fine_tuned_re_model = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_FINE_TUNED_RE_ENDPOINT") {
            self.models.fine_tuned_re_endpoint = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MODELS_FINE_TUNED_RE_API_KEY_ENV") {
            self.models.fine_tuned_re_api_key_env = v;
        }

        // Consolidation overrides
        if let Ok(v) = std::env::var("UCOTRON_CONSOLIDATION_TRIGGER_INTERVAL") {
            if let Ok(ti) = v.parse::<usize>() {
                self.consolidation.trigger_interval = ti;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_CONSOLIDATION_ENABLE_DECAY") {
            if let Ok(b) = v.parse::<bool>() {
                self.consolidation.enable_decay = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_CONSOLIDATION_DECAY_HALFLIFE_SECS") {
            if let Ok(s) = v.parse::<u64>() {
                self.consolidation.decay_halflife_secs = s;
            }
        }

        // MCP overrides
        if let Ok(v) = std::env::var("UCOTRON_MCP_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.mcp.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_MCP_TRANSPORT") {
            self.mcp.transport = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_MCP_PORT") {
            if let Ok(port) = v.parse::<u16>() {
                self.mcp.port = port;
            }
        }

        // Namespaces overrides
        if let Ok(v) = std::env::var("UCOTRON_NAMESPACES_DEFAULT") {
            self.namespaces.default_namespace = v;
        }

        // Auth overrides
        if let Ok(v) = std::env::var("UCOTRON_AUTH_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.auth.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_AUTH_API_KEY") {
            self.auth.api_key = Some(v);
        }
        if let Ok(v) = std::env::var("UCOTRON_AUTH_JWT_SECRET") {
            self.auth.jwt_secret = Some(v);
        }

        // GDPR overrides
        if let Ok(v) = std::env::var("UCOTRON_GDPR_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.gdpr.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_GDPR_DEFAULT_RETENTION_TTL_SECS") {
            if let Ok(s) = v.parse::<u64>() {
                self.gdpr.default_retention_ttl_secs = s;
            }
        }

        // Audit overrides
        if let Ok(v) = std::env::var("UCOTRON_AUDIT_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.audit.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_AUDIT_RETENTION_SECS") {
            if let Ok(s) = v.parse::<u64>() {
                self.audit.retention_secs = s;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_AUDIT_MAX_ENTRIES") {
            if let Ok(n) = v.parse::<usize>() {
                self.audit.max_entries = n;
            }
        }

        // Instance overrides
        if let Ok(v) = std::env::var("UCOTRON_INSTANCE_ID") {
            self.instance.instance_id = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_INSTANCE_ROLE") {
            self.instance.role = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_INSTANCE_ID_RANGE_START") {
            if let Ok(n) = v.parse::<u64>() {
                self.instance.id_range_start = n;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_INSTANCE_ID_RANGE_SIZE") {
            if let Ok(n) = v.parse::<u64>() {
                self.instance.id_range_size = n;
            }
        }

        // Telemetry overrides
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.telemetry.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_OTLP_ENDPOINT") {
            self.telemetry.otlp_endpoint = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_SERVICE_NAME") {
            self.telemetry.service_name = v;
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_SAMPLE_RATE") {
            if let Ok(r) = v.parse::<f64>() {
                self.telemetry.sample_rate = r;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_EXPORT_TRACES") {
            if let Ok(b) = v.parse::<bool>() {
                self.telemetry.export_traces = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_EXPORT_METRICS") {
            if let Ok(b) = v.parse::<bool>() {
                self.telemetry.export_metrics = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_TELEMETRY_EXPORT_LOGS") {
            if let Ok(b) = v.parse::<bool>() {
                self.telemetry.export_logs = b;
            }
        }

        // Connectors overrides
        if let Ok(v) = std::env::var("UCOTRON_CONNECTORS_ENABLED") {
            if let Ok(b) = v.parse::<bool>() {
                self.connectors.enabled = b;
            }
        }
        if let Ok(v) = std::env::var("UCOTRON_CONNECTORS_CHECK_INTERVAL_SECS") {
            if let Ok(n) = v.parse::<u64>() {
                self.connectors.check_interval_secs = n;
            }
        }
    }

    // --- Telemetry accessors ---

    /// Whether OTLP telemetry export is enabled.
    pub fn telemetry_enabled(&self) -> bool {
        self.telemetry.enabled
    }

    /// OTLP gRPC collector endpoint.
    pub fn telemetry_otlp_endpoint(&self) -> String {
        self.telemetry.otlp_endpoint.clone()
    }

    /// Service name reported in OTLP traces.
    pub fn telemetry_service_name(&self) -> String {
        self.telemetry.service_name.clone()
    }

    /// Trace sampling ratio (0.0–1.0).
    pub fn telemetry_sample_rate(&self) -> f64 {
        self.telemetry.sample_rate
    }

    /// Validate configuration values with detailed error messages.
    pub fn validate(&self) -> anyhow::Result<()> {
        // --- Server validation ---
        if self.server.port == 0 {
            anyhow::bail!(
                "server.port must be > 0 (got 0). Set a valid port in ucotron.toml or via UCOTRON_SERVER_PORT env var."
            );
        }
        if self.server.workers == 0 {
            anyhow::bail!(
                "server.workers must be > 0 (got 0). Set the number of worker threads in ucotron.toml or via UCOTRON_SERVER_WORKERS env var."
            );
        }
        let valid_log_levels = ["trace", "debug", "info", "warn", "error"];
        if !valid_log_levels.contains(&self.server.log_level.as_str()) {
            anyhow::bail!(
                "server.log_level must be one of: {} (got '{}').",
                valid_log_levels.join(", "),
                self.server.log_level
            );
        }
        let valid_log_formats = ["text", "json"];
        if !valid_log_formats.contains(&self.server.log_format.as_str()) {
            anyhow::bail!(
                "server.log_format must be one of: {} (got '{}').",
                valid_log_formats.join(", "),
                self.server.log_format
            );
        }

        // --- Storage validation ---
        let valid_modes = ["embedded", "external", "shared"];
        if !valid_modes.contains(&self.storage.mode.as_str()) {
            anyhow::bail!(
                "storage.mode must be one of: {} (got '{}').",
                valid_modes.join(", "),
                self.storage.mode
            );
        }
        let valid_vector_backends = ["helix", "qdrant", "custom"];
        if !valid_vector_backends.contains(&self.storage.vector.backend.as_str()) {
            anyhow::bail!(
                "storage.vector.backend must be one of: {} (got '{}').",
                valid_vector_backends.join(", "),
                self.storage.vector.backend
            );
        }
        let valid_graph_backends = ["helix", "falkordb", "custom"];
        if !valid_graph_backends.contains(&self.storage.graph.backend.as_str()) {
            anyhow::bail!(
                "storage.graph.backend must be one of: {} (got '{}').",
                valid_graph_backends.join(", "),
                self.storage.graph.backend
            );
        }
        // Shared mode requires shared_data_dir when using helix backends
        if self.storage.mode == "shared"
            && self.storage.vector.backend == "helix"
            && self.storage.graph.backend == "helix"
            && self.storage.shared_data_dir.is_none()
        {
            anyhow::bail!(
                "storage.shared_data_dir is required when storage.mode is 'shared' with helix backends. \
                 All instances must point to the same directory. \
                 Set via ucotron.toml or UCOTRON_STORAGE_SHARED_DATA_DIR env var."
            );
        }

        // External backends require a URL
        if self.storage.mode == "external" || self.storage.mode == "shared" {
            if self.storage.vector.backend != "helix" && self.storage.vector.url.is_none() {
                anyhow::bail!(
                    "storage.vector.url is required when using external vector backend '{}' in '{}' mode.",
                    self.storage.vector.backend,
                    self.storage.mode
                );
            }
            if self.storage.graph.backend != "helix" && self.storage.graph.url.is_none() {
                anyhow::bail!(
                    "storage.graph.url is required when using external graph backend '{}' in '{}' mode.",
                    self.storage.graph.backend,
                    self.storage.mode
                );
            }
        }

        // --- HNSW validation ---
        if self.storage.vector.hnsw.ef_construction == 0 {
            anyhow::bail!("storage.vector.hnsw.ef_construction must be > 0.");
        }
        if self.storage.vector.hnsw.ef_search == 0 {
            anyhow::bail!("storage.vector.hnsw.ef_search must be > 0.");
        }

        // --- Models validation ---
        let valid_llm_backends = ["candle", "llama_cpp"];
        if !valid_llm_backends.contains(&self.models.llm_backend.as_str()) {
            anyhow::bail!(
                "models.llm_backend must be one of: {} (got '{}').",
                valid_llm_backends.join(", "),
                self.models.llm_backend
            );
        }
        if self.models.embedding_model.is_empty() {
            anyhow::bail!("models.embedding_model must not be empty.");
        }

        // --- MCP validation ---
        let valid_transports = ["stdio", "sse"];
        if !valid_transports.contains(&self.mcp.transport.as_str()) {
            anyhow::bail!(
                "mcp.transport must be one of: {} (got '{}').",
                valid_transports.join(", "),
                self.mcp.transport
            );
        }
        if self.mcp.transport == "sse" && self.mcp.port == 0 {
            anyhow::bail!("mcp.port must be > 0 when mcp.transport is 'sse'.");
        }
        // MCP and server ports must not collide
        if self.mcp.enabled && self.mcp.transport == "sse" && self.mcp.port == self.server.port {
            anyhow::bail!(
                "mcp.port ({}) must differ from server.port ({}) to avoid port collision.",
                self.mcp.port,
                self.server.port
            );
        }

        // --- Auth validation ---
        if self.auth.enabled
            && self.auth.api_key.is_none()
            && self.auth.jwt_secret.is_none()
            && self.auth.api_keys.is_empty()
        {
            anyhow::bail!(
                "auth.enabled is true but no authentication method is configured. \
                 Provide auth.api_key, auth.jwt_secret, or at least one [[auth.api_keys]] entry."
            );
        }
        let valid_auth_roles = ["admin", "writer", "reader", "viewer"];
        for (i, entry) in self.auth.api_keys.iter().enumerate() {
            if entry.name.is_empty() {
                anyhow::bail!("auth.api_keys[{}].name must not be empty.", i);
            }
            if entry.key.is_empty() {
                anyhow::bail!(
                    "auth.api_keys[{}].key must not be empty (name='{}').",
                    i,
                    entry.name
                );
            }
            if !valid_auth_roles.contains(&entry.role.as_str()) {
                anyhow::bail!(
                    "auth.api_keys[{}].role must be one of: {} (got '{}', name='{}').",
                    i,
                    valid_auth_roles.join(", "),
                    entry.role,
                    entry.name
                );
            }
        }

        // --- Telemetry validation ---
        if self.telemetry.sample_rate < 0.0 || self.telemetry.sample_rate > 1.0 {
            anyhow::bail!(
                "telemetry.sample_rate must be between 0.0 and 1.0 (got {}).",
                self.telemetry.sample_rate
            );
        }
        if self.telemetry.enabled && self.telemetry.otlp_endpoint.is_empty() {
            anyhow::bail!("telemetry.otlp_endpoint must not be empty when telemetry is enabled.");
        }
        if self.telemetry.enabled && self.telemetry.service_name.is_empty() {
            anyhow::bail!("telemetry.service_name must not be empty when telemetry is enabled.");
        }

        // --- Namespaces validation ---
        if self.namespaces.default_namespace.is_empty() {
            anyhow::bail!("namespaces.default_namespace must not be empty.");
        }

        // --- Instance validation ---
        let valid_roles = ["auto", "writer", "reader"];
        if !valid_roles.contains(&self.instance.role.as_str()) {
            anyhow::bail!(
                "instance.role must be one of: {} (got '{}').",
                valid_roles.join(", "),
                self.instance.role
            );
        }
        if self.instance.id_range_size == 0 {
            anyhow::bail!("instance.id_range_size must be > 0.");
        }
        // Shared mode requires either writer or reader role
        if self.storage.mode == "shared" && self.instance.role == "auto" {
            anyhow::bail!(
                "instance.role must be 'writer' or 'reader' when storage.mode is 'shared'. \
                 Set via ucotron.toml or UCOTRON_INSTANCE_ROLE env var."
            );
        }

        // --- Connectors validation ---
        if self.connectors.check_interval_secs == 0 {
            anyhow::bail!("connectors.check_interval_secs must be > 0.");
        }
        for (i, entry) in self.connectors.schedules.iter().enumerate() {
            if entry.connector_id.is_empty() {
                anyhow::bail!(
                    "connectors.schedules[{}].connector_id must not be empty.",
                    i
                );
            }
            if entry.timeout_secs == 0 {
                anyhow::bail!(
                    "connectors.schedules[{}].timeout_secs must be > 0 (connector_id='{}').",
                    i,
                    entry.connector_id
                );
            }
        }

        Ok(())
    }

    /// Generate an example configuration as a TOML string (plain, no comments).
    pub fn example_toml() -> String {
        let config = UcotronConfig::default();
        toml::to_string_pretty(&config)
            .unwrap_or_else(|_| "# Failed to generate example".to_string())
    }

    /// Generate a fully commented example configuration file.
    ///
    /// This is suitable for `ucotron_server --init-config` output.
    pub fn example_toml_commented() -> String {
        format!(
            r#"# =============================================================================
# Ucotron Configuration File
# =============================================================================
# This file configures the Ucotron cognitive memory server.
# All values shown below are defaults — uncomment and modify as needed.
#
# Environment variables override TOML values. Use the UCOTRON_ prefix:
#   UCOTRON_SERVER_PORT=9000 ucotron_server
#
# For full documentation, see: https://ucotron.com/docs/server/configuration

# -----------------------------------------------------------------------------
# [server] — HTTP server settings
# -----------------------------------------------------------------------------
[server]
# Bind address for the REST API.
host = "0.0.0.0"
# HTTP port for the REST API.
port = 8420
# Number of worker threads for request handling.
workers = 4
# Log level: trace, debug, info, warn, error
log_level = "info"
# Log format: "text" (human-readable) or "json" (structured with trace IDs)
log_format = "text"

# -----------------------------------------------------------------------------
# [storage] — Backend storage configuration
# -----------------------------------------------------------------------------
[storage]
# Storage mode:
#   "embedded" — All data stored locally in LMDB (default, single-instance)
#   "external" — Use external backends (Qdrant, FalkorDB, etc.)
#   "shared"   — Multiple server instances sharing the same storage directory
mode = "embedded"
# Shared data directory for multi-instance mode (required when mode = "shared").
# All instances must point to the same directory (e.g., NFS mount, shared volume).
# shared_data_dir = "/data/ucotron-shared"

# Vector backend configuration.
[storage.vector]
# Backend type: "helix" (embedded LMDB+HNSW), "qdrant" (external), "custom"
backend = "helix"
# Data directory for embedded backends (relative to working dir).
data_dir = "data"
# Maximum database size in bytes (10 GB default, for LMDB map_size).
max_db_size = {max_db_size}
# URL for external vector backend (required when backend != "helix").
# url = "http://localhost:6333"

# HNSW vector index parameters.
[storage.vector.hnsw]
# Number of bi-directional links per node during index construction.
# Higher = better recall, more memory, slower build.
ef_construction = 200
# Number of candidates evaluated during search.
# Higher = better recall, slower search.
ef_search = 200
# Enable HNSW index. When false, falls back to brute-force SIMD search.
enabled = true

# Graph backend configuration.
[storage.graph]
# Backend type: "helix" (embedded LMDB), "falkordb" (external), "custom"
backend = "helix"
# Data directory for embedded backends.
data_dir = "data"
# Maximum database size in bytes (10 GB default).
max_db_size = {max_db_size}
# Batch size for bulk operations (node/edge inserts).
batch_size = 10000
# URL for external graph backend (required when backend != "helix").
# url = "redis://localhost:6379"

# -----------------------------------------------------------------------------
# [models] — ML model configuration
# -----------------------------------------------------------------------------
[models]
# Sentence embedding model (ONNX format, 384-dim output).
embedding_model = "all-MiniLM-L6-v2"
# Named Entity Recognition model (GLiNER, ONNX format).
ner_model = "gliner-multi-v2.1"
# LLM model for relation extraction (GGUF quantized).
# Set to "none" or "" to use co-occurrence fallback (no LLM).
llm_model = "Qwen3-4B-GGUF"
# LLM backend: "candle" (Rust native) or "llama_cpp" (C++ bindings).
llm_backend = "candle"
# Directory containing model files (downloaded via scripts/download_models.sh).
models_dir = "models"
# Enable document OCR pipeline (PDF text extraction + Tesseract image OCR).
enable_ocr = true
# Language for Tesseract OCR (e.g., "eng", "spa", "deu", "eng+spa").
ocr_language = "eng"
# Path to the tesseract binary. Set to full path if not on PATH.
tesseract_path = "tesseract"

# -----------------------------------------------------------------------------
# [consolidation] — Background "dreaming" worker
# -----------------------------------------------------------------------------
[consolidation]
# Number of ingested messages between consolidation runs.
trigger_interval = 100
# Enable temporal memory decay for old, unaccessed nodes.
enable_decay = true
# Decay half-life in seconds (default: 30 days = 2592000).
decay_halflife_secs = 2592000

# -----------------------------------------------------------------------------
# [telemetry] — OpenTelemetry observability
# -----------------------------------------------------------------------------
[telemetry]
# Enable OTLP telemetry export (traces, metrics, logs).
# Requires a running OpenTelemetry collector (e.g., Jaeger, Grafana Tempo).
enabled = false
# OTLP gRPC collector endpoint.
otlp_endpoint = "http://localhost:4317"
# Service name reported in traces and metrics.
service_name = "ucotron"
# Trace sampling ratio: 0.0 (no traces) to 1.0 (all traces).
sample_rate = 1.0
# Export traces via OTLP.
export_traces = true
# Export metrics via OTLP.
export_metrics = true
# Export logs via OTLP (may be verbose, disabled by default).
export_logs = false

# -----------------------------------------------------------------------------
# [mcp] — Model Context Protocol server
# -----------------------------------------------------------------------------
[mcp]
# Enable MCP server for Claude Desktop, Cursor, etc.
enabled = true
# Transport mode: "stdio" (default, for CLI tools) or "sse" (HTTP streaming).
transport = "stdio"
# Port for SSE transport (only used when transport = "sse").
port = 8421

# -----------------------------------------------------------------------------
# [namespaces] — Multi-tenancy configuration
# -----------------------------------------------------------------------------
[namespaces]
# Default namespace when no X-Ucotron-Namespace header is provided.
default_namespace = "default"
# Restrict to specific namespaces (empty = allow any).
# allowed_namespaces = ["org1", "org2"]
# Maximum number of namespaces (0 = unlimited).
max_namespaces = 0

# -----------------------------------------------------------------------------
# [auth] — Authentication (optional)
# -----------------------------------------------------------------------------
[auth]
# Enable authentication. When false, all requests are accepted.
enabled = false
# API key for Bearer token auth. Set via UCOTRON_AUTH_API_KEY env var.
# api_key = "your-secret-api-key"
# JWT secret for token-based auth (future use).
# jwt_secret = "your-jwt-secret"
# JWT issuer (future use).
# jwt_issuer = "ucotron"

# -----------------------------------------------------------------------------
# [audit] — Immutable audit logging
# -----------------------------------------------------------------------------
[audit]
# Enable audit logging for all API operations.
enabled = true
# Retention period in seconds (0 = keep forever). Default: 90 days.
retention_secs = 7776000
# Maximum entries kept in memory. Oldest entries evicted when exceeded.
max_entries = 100000

# -----------------------------------------------------------------------------
# [instance] — Multi-instance configuration
# -----------------------------------------------------------------------------
[instance]
# Unique identifier for this server instance.
# Set to "auto" to generate from hostname + PID.
instance_id = "auto"
# Instance role:
#   "auto"   — Single-instance mode (default), acts as both reader and writer
#   "writer" — Can perform writes (ingestion, learn, update, delete)
#   "reader" — Read-only (search, augment, get operations only)
role = "auto"
# Starting node ID for this instance's ID allocation range.
# Each instance in a multi-instance deployment needs a non-overlapping range.
id_range_start = 1000000
# Size of this instance's node ID allocation range (default: 1 billion).
id_range_size = 1000000000
"#,
            max_db_size = 10u64 * 1024 * 1024 * 1024
        )
    }
}

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

    #[test]
    fn test_default_config() {
        let config = UcotronConfig::default();
        assert_eq!(config.server.host, "0.0.0.0");
        assert_eq!(config.server.port, 8420);
        assert_eq!(config.server.workers, 4);
        assert_eq!(config.server.log_level, "info");
        assert_eq!(config.server.log_format, "text");
        assert_eq!(config.storage.mode, "embedded");
        assert_eq!(config.storage.vector.backend, "helix");
        assert_eq!(config.storage.graph.backend, "helix");
        assert_eq!(config.models.embedding_model, "all-MiniLM-L6-v2");
        assert_eq!(config.namespaces.default_namespace, "default");
        assert!(config.namespaces.allowed_namespaces.is_empty());
        assert!(!config.auth.enabled);
        assert!(config.auth.api_key.is_none());
    }

    #[test]
    fn test_parse_minimal_toml() {
        let toml = "";
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.server.port, 8420);
        assert_eq!(config.namespaces.default_namespace, "default");
    }

    #[test]
    fn test_parse_custom_toml() {
        let toml = r#"
[server]
host = "127.0.0.1"
port = 9000
workers = 8

[storage]
mode = "embedded"

[storage.vector]
backend = "helix"
data_dir = "/tmp/ucotron"

[storage.graph]
backend = "helix"
batch_size = 5000

[namespaces]
default_namespace = "my-project"
allowed_namespaces = ["my-project", "staging"]

[auth]
enabled = true
api_key = "test-key-123"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.server.host, "127.0.0.1");
        assert_eq!(config.server.port, 9000);
        assert_eq!(config.server.workers, 8);
        assert_eq!(config.storage.vector.data_dir, "/tmp/ucotron");
        assert_eq!(config.storage.graph.batch_size, 5000);
        assert_eq!(config.namespaces.default_namespace, "my-project");
        assert_eq!(
            config.namespaces.allowed_namespaces,
            vec!["my-project", "staging"]
        );
        assert!(config.auth.enabled);
        assert_eq!(config.auth.api_key.as_deref(), Some("test-key-123"));
    }

    #[test]
    fn test_invalid_storage_mode() {
        let toml = r#"
[storage]
mode = "invalid"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("storage.mode"));
        assert!(err.contains("invalid"));
    }

    #[test]
    fn test_invalid_port() {
        let toml = r#"
[server]
port = 0
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("server.port"));
    }

    #[test]
    fn test_invalid_log_level() {
        let toml = r#"
[server]
log_level = "verbose"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("server.log_level"));
        assert!(err.contains("verbose"));
    }

    #[test]
    fn test_invalid_log_format() {
        let toml = r#"
[server]
log_format = "xml"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("server.log_format"));
        assert!(err.contains("xml"));
    }

    #[test]
    fn test_log_format_json_valid() {
        let toml = r#"
[server]
log_format = "json"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.server.log_format, "json");
    }

    #[test]
    fn test_env_override_log_format() {
        std::env::set_var("UCOTRON_SERVER_LOG_FORMAT", "json");
        let mut config = UcotronConfig::default();
        config.apply_env_overrides();
        assert_eq!(config.server.log_format, "json");
        std::env::remove_var("UCOTRON_SERVER_LOG_FORMAT");
    }

    #[test]
    fn test_example_toml_generation() {
        let example = UcotronConfig::example_toml();
        assert!(example.contains("port"));
        assert!(example.contains("8420"));
        assert!(example.contains("helix"));
        // Verify it round-trips
        let _config = UcotronConfig::parse_toml(&example).unwrap();
    }

    #[test]
    fn test_example_toml_commented() {
        let commented = UcotronConfig::example_toml_commented();
        // Should contain section headers
        assert!(commented.contains("[server]"));
        assert!(commented.contains("[storage]"));
        assert!(commented.contains("[models]"));
        assert!(commented.contains("[consolidation]"));
        assert!(commented.contains("[mcp]"));
        assert!(commented.contains("[namespaces]"));
        assert!(commented.contains("[auth]"));
        // Should contain inline comments
        assert!(commented.contains("# Bind address"));
        assert!(commented.contains("UCOTRON_"));
    }

    #[test]
    fn test_serialization_roundtrip() {
        let config = UcotronConfig::default();
        let toml_str = toml::to_string_pretty(&config).unwrap();
        let parsed: UcotronConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed.server.port, config.server.port);
        assert_eq!(parsed.storage.mode, config.storage.mode);
        assert_eq!(
            parsed.namespaces.default_namespace,
            config.namespaces.default_namespace
        );
    }

    #[test]
    fn test_env_override_server_port() {
        let mut config = UcotronConfig::default();
        // Simulate env override
        std::env::set_var("UCOTRON_SERVER_PORT", "9999");
        config.apply_env_overrides();
        assert_eq!(config.server.port, 9999);
        std::env::remove_var("UCOTRON_SERVER_PORT");
    }

    #[test]
    fn test_env_override_server_host() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_SERVER_HOST", "127.0.0.1");
        config.apply_env_overrides();
        assert_eq!(config.server.host, "127.0.0.1");
        std::env::remove_var("UCOTRON_SERVER_HOST");
    }

    #[test]
    fn test_env_override_storage_mode() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_STORAGE_MODE", "external");
        config.apply_env_overrides();
        assert_eq!(config.storage.mode, "external");
        std::env::remove_var("UCOTRON_STORAGE_MODE");
    }

    #[test]
    fn test_env_override_auth_api_key() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_AUTH_API_KEY", "secret-from-env");
        config.apply_env_overrides();
        assert_eq!(config.auth.api_key.as_deref(), Some("secret-from-env"));
        std::env::remove_var("UCOTRON_AUTH_API_KEY");
    }

    #[test]
    fn test_env_override_models_dir() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_MODELS_DIR", "/opt/models");
        config.apply_env_overrides();
        assert_eq!(config.models.models_dir, "/opt/models");
        std::env::remove_var("UCOTRON_MODELS_DIR");
    }

    #[test]
    fn test_env_override_invalid_port_ignored() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_SERVER_PORT", "not-a-number");
        config.apply_env_overrides();
        // Should keep the default since parse fails
        assert_eq!(config.server.port, 8420);
        std::env::remove_var("UCOTRON_SERVER_PORT");
    }

    #[test]
    fn test_auth_enabled_without_credentials() {
        let toml = r#"
[auth]
enabled = true
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("auth.enabled"));
        assert!(err.contains("api_key"));
    }

    #[test]
    fn test_auth_enabled_with_api_key() {
        let toml = r#"
[auth]
enabled = true
api_key = "my-secret"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert!(config.auth.enabled);
        assert_eq!(config.auth.api_key.as_deref(), Some("my-secret"));
    }

    #[test]
    fn test_external_mode_requires_url() {
        let toml = r#"
[storage]
mode = "external"

[storage.vector]
backend = "qdrant"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("storage.vector.url"));
        assert!(err.contains("qdrant"));
    }

    #[test]
    fn test_external_mode_helix_no_url_needed() {
        // Helix in external mode doesn't need URL (it's embedded)
        let toml = r#"
[storage]
mode = "external"

[storage.vector]
backend = "helix"

[storage.graph]
backend = "helix"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.storage.mode, "external");
    }

    #[test]
    fn test_mcp_sse_port_collision() {
        let toml = r#"
[server]
port = 8420

[mcp]
enabled = true
transport = "sse"
port = 8420
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("port collision"));
    }

    #[test]
    fn test_mcp_transport_validation() {
        let toml = r#"
[mcp]
transport = "grpc"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("mcp.transport"));
    }

    #[test]
    fn test_empty_namespace_rejected() {
        let toml = r#"
[namespaces]
default_namespace = ""
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("namespaces.default_namespace"));
    }

    #[test]
    fn test_hnsw_zero_ef_construction() {
        let toml = r#"
[storage.vector.hnsw]
ef_construction = 0
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("ef_construction"));
    }

    #[test]
    fn test_empty_embedding_model_rejected() {
        let toml = r#"
[models]
embedding_model = ""
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("models.embedding_model"));
    }

    // --- Instance config tests ---

    #[test]
    fn test_instance_config_defaults() {
        let config = UcotronConfig::default();
        assert_eq!(config.instance.instance_id, "auto");
        assert_eq!(config.instance.role, "auto");
        assert_eq!(config.instance.id_range_start, 1_000_000);
        assert_eq!(config.instance.id_range_size, 1_000_000_000);
        assert!(config.instance.can_write());
        assert!(!config.instance.is_reader_only());
    }

    #[test]
    fn test_instance_config_writer_role() {
        let toml = r#"
[instance]
instance_id = "writer-1"
role = "writer"
id_range_start = 0
id_range_size = 500000000
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.instance.instance_id, "writer-1");
        assert_eq!(config.instance.role, "writer");
        assert_eq!(config.instance.id_range_start, 0);
        assert_eq!(config.instance.id_range_size, 500_000_000);
        assert!(config.instance.can_write());
        assert!(!config.instance.is_reader_only());
    }

    #[test]
    fn test_instance_config_reader_role() {
        let toml = r#"
[instance]
instance_id = "reader-1"
role = "reader"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.instance.role, "reader");
        assert!(!config.instance.can_write());
        assert!(config.instance.is_reader_only());
    }

    #[test]
    fn test_instance_invalid_role() {
        let toml = r#"
[instance]
role = "master"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("instance.role"));
        assert!(err.contains("master"));
    }

    #[test]
    fn test_instance_zero_range_size_rejected() {
        let toml = r#"
[instance]
id_range_size = 0
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("id_range_size"));
    }

    #[test]
    fn test_shared_mode_requires_explicit_role() {
        let toml = r#"
[storage]
mode = "shared"
shared_data_dir = "/data/shared"

[instance]
role = "auto"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("instance.role"));
        assert!(err.contains("shared"));
    }

    #[test]
    fn test_shared_mode_writer_role_ok() {
        let toml = r#"
[storage]
mode = "shared"
shared_data_dir = "/data/shared"

[instance]
instance_id = "w1"
role = "writer"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.storage.mode, "shared");
        assert_eq!(config.instance.role, "writer");
        assert_eq!(
            config.storage.shared_data_dir.as_deref(),
            Some("/data/shared")
        );
    }

    #[test]
    fn test_shared_mode_reader_role_ok() {
        let toml = r#"
[storage]
mode = "shared"
shared_data_dir = "/data/shared"

[instance]
instance_id = "r1"
role = "reader"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.storage.mode, "shared");
        assert_eq!(config.instance.role, "reader");
    }

    #[test]
    fn test_shared_mode_requires_shared_data_dir() {
        let toml = r#"
[storage]
mode = "shared"

[instance]
instance_id = "w1"
role = "writer"
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("shared_data_dir"));
    }

    #[test]
    fn test_effective_data_dirs_embedded() {
        let config = UcotronConfig::default();
        assert_eq!(config.storage.effective_vector_data_dir(), "data");
        assert_eq!(config.storage.effective_graph_data_dir(), "data");
    }

    #[test]
    fn test_effective_data_dirs_shared() {
        let toml = r#"
[storage]
mode = "shared"
shared_data_dir = "/mnt/shared"

[instance]
role = "writer"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.storage.effective_vector_data_dir(), "/mnt/shared");
        assert_eq!(config.storage.effective_graph_data_dir(), "/mnt/shared");
    }

    #[test]
    fn test_instance_resolved_id_auto() {
        let config = UcotronConfig::default();
        let resolved = config.instance.resolved_instance_id();
        // Should contain hostname and PID
        assert!(resolved.contains('-'));
        assert!(!resolved.is_empty());
        assert_ne!(resolved, "auto");
    }

    #[test]
    fn test_instance_resolved_id_explicit() {
        let toml = r#"
[instance]
instance_id = "my-server-1"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.instance.resolved_instance_id(), "my-server-1");
    }

    #[test]
    fn test_env_override_instance_role() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_INSTANCE_ROLE", "reader");
        config.apply_env_overrides();
        assert_eq!(config.instance.role, "reader");
        assert!(config.instance.is_reader_only());
        std::env::remove_var("UCOTRON_INSTANCE_ROLE");
    }

    #[test]
    fn test_env_override_instance_id() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_INSTANCE_ID", "env-instance-42");
        config.apply_env_overrides();
        assert_eq!(config.instance.instance_id, "env-instance-42");
        assert_eq!(config.instance.resolved_instance_id(), "env-instance-42");
        std::env::remove_var("UCOTRON_INSTANCE_ID");
    }

    #[test]
    fn test_env_override_id_range() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_INSTANCE_ID_RANGE_START", "2000000000");
        std::env::set_var("UCOTRON_INSTANCE_ID_RANGE_SIZE", "500000000");
        config.apply_env_overrides();
        assert_eq!(config.instance.id_range_start, 2_000_000_000);
        assert_eq!(config.instance.id_range_size, 500_000_000);
        std::env::remove_var("UCOTRON_INSTANCE_ID_RANGE_START");
        std::env::remove_var("UCOTRON_INSTANCE_ID_RANGE_SIZE");
    }

    #[test]
    fn test_example_toml_contains_instance_section() {
        let commented = UcotronConfig::example_toml_commented();
        assert!(commented.contains("[instance]"));
        assert!(commented.contains("instance_id"));
        assert!(commented.contains("role"));
        assert!(commented.contains("id_range_start"));
        assert!(commented.contains("id_range_size"));
    }

    #[test]
    fn test_full_config_all_sections() {
        let toml = r#"
[server]
host = "10.0.0.1"
port = 3000
workers = 16
log_level = "debug"

[storage]
mode = "embedded"

[storage.vector]
backend = "helix"
data_dir = "/data/vectors"

[storage.vector.hnsw]
ef_construction = 100
ef_search = 100
enabled = true

[storage.graph]
backend = "helix"
data_dir = "/data/graph"
batch_size = 20000

[models]
embedding_model = "custom-model"
ner_model = "custom-ner"
llm_model = "none"
llm_backend = "candle"
models_dir = "/opt/models"

[consolidation]
trigger_interval = 50
enable_decay = false
decay_halflife_secs = 86400

[mcp]
enabled = false
transport = "stdio"
port = 9000

[namespaces]
default_namespace = "prod"
allowed_namespaces = ["prod", "staging", "dev"]
max_namespaces = 10

[auth]
enabled = true
api_key = "super-secret-key"
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert_eq!(config.server.host, "10.0.0.1");
        assert_eq!(config.server.port, 3000);
        assert_eq!(config.server.workers, 16);
        assert_eq!(config.server.log_level, "debug");
        assert_eq!(config.storage.vector.data_dir, "/data/vectors");
        assert_eq!(config.storage.vector.hnsw.ef_construction, 100);
        assert_eq!(config.storage.graph.data_dir, "/data/graph");
        assert_eq!(config.storage.graph.batch_size, 20000);
        assert_eq!(config.models.llm_model, "none");
        assert_eq!(config.consolidation.trigger_interval, 50);
        assert!(!config.consolidation.enable_decay);
        assert!(!config.mcp.enabled);
        assert_eq!(config.namespaces.default_namespace, "prod");
        assert_eq!(config.namespaces.allowed_namespaces.len(), 3);
        assert_eq!(config.namespaces.max_namespaces, 10);
        assert!(config.auth.enabled);
    }

    // --- Telemetry config tests ---

    #[test]
    fn test_telemetry_config_defaults() {
        let config = UcotronConfig::default();
        assert!(!config.telemetry.enabled);
        assert_eq!(config.telemetry.otlp_endpoint, "http://localhost:4317");
        assert_eq!(config.telemetry.service_name, "ucotron");
        assert_eq!(config.telemetry.sample_rate, 1.0);
        assert!(config.telemetry.export_traces);
        assert!(config.telemetry.export_metrics);
        assert!(!config.telemetry.export_logs);
    }

    #[test]
    fn test_telemetry_config_parse_toml() {
        let toml = r#"
[telemetry]
enabled = true
otlp_endpoint = "http://otel-collector:4317"
service_name = "ucotron-prod"
sample_rate = 0.5
export_traces = true
export_metrics = false
export_logs = true
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert!(config.telemetry.enabled);
        assert_eq!(config.telemetry.otlp_endpoint, "http://otel-collector:4317");
        assert_eq!(config.telemetry.service_name, "ucotron-prod");
        assert_eq!(config.telemetry.sample_rate, 0.5);
        assert!(config.telemetry.export_traces);
        assert!(!config.telemetry.export_metrics);
        assert!(config.telemetry.export_logs);
    }

    #[test]
    fn test_telemetry_accessors() {
        let toml = r#"
[telemetry]
enabled = true
otlp_endpoint = "http://collector:4317"
service_name = "test-svc"
sample_rate = 0.75
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert!(config.telemetry_enabled());
        assert_eq!(config.telemetry_otlp_endpoint(), "http://collector:4317");
        assert_eq!(config.telemetry_service_name(), "test-svc");
        assert_eq!(config.telemetry_sample_rate(), 0.75);
    }

    #[test]
    fn test_telemetry_sample_rate_out_of_range() {
        let toml = r#"
[telemetry]
sample_rate = 1.5
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("telemetry.sample_rate"));
        assert!(err.contains("1.5"));
    }

    #[test]
    fn test_telemetry_sample_rate_negative() {
        let toml = r#"
[telemetry]
sample_rate = -0.1
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("telemetry.sample_rate"));
    }

    #[test]
    fn test_telemetry_enabled_empty_endpoint_rejected() {
        let toml = r#"
[telemetry]
enabled = true
otlp_endpoint = ""
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("telemetry.otlp_endpoint"));
    }

    #[test]
    fn test_telemetry_enabled_empty_service_name_rejected() {
        let toml = r#"
[telemetry]
enabled = true
service_name = ""
"#;
        let result = UcotronConfig::parse_toml(toml);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("telemetry.service_name"));
    }

    #[test]
    fn test_env_override_telemetry_enabled() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_TELEMETRY_ENABLED", "true");
        config.apply_env_overrides();
        assert!(config.telemetry.enabled);
        std::env::remove_var("UCOTRON_TELEMETRY_ENABLED");
    }

    #[test]
    fn test_env_override_telemetry_endpoint() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_TELEMETRY_OTLP_ENDPOINT", "http://custom:4317");
        config.apply_env_overrides();
        assert_eq!(config.telemetry.otlp_endpoint, "http://custom:4317");
        std::env::remove_var("UCOTRON_TELEMETRY_OTLP_ENDPOINT");
    }

    #[test]
    fn test_env_override_telemetry_service_name() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_TELEMETRY_SERVICE_NAME", "my-svc");
        config.apply_env_overrides();
        assert_eq!(config.telemetry.service_name, "my-svc");
        std::env::remove_var("UCOTRON_TELEMETRY_SERVICE_NAME");
    }

    #[test]
    fn test_env_override_telemetry_sample_rate() {
        let mut config = UcotronConfig::default();
        std::env::set_var("UCOTRON_TELEMETRY_SAMPLE_RATE", "0.25");
        config.apply_env_overrides();
        assert_eq!(config.telemetry.sample_rate, 0.25);
        std::env::remove_var("UCOTRON_TELEMETRY_SAMPLE_RATE");
    }

    #[test]
    fn test_example_toml_contains_telemetry_section() {
        let commented = UcotronConfig::example_toml_commented();
        assert!(commented.contains("[telemetry]"));
        assert!(commented.contains("otlp_endpoint"));
        assert!(commented.contains("service_name"));
        assert!(commented.contains("sample_rate"));
        assert!(commented.contains("export_traces"));
        assert!(commented.contains("export_metrics"));
        assert!(commented.contains("export_logs"));
    }

    // --- Mindset config tests ---

    #[test]
    fn test_mindset_config_defaults() {
        let config = UcotronConfig::default();
        assert!(config.mindset.enabled);
        assert!(!config.mindset.algorithmic_keywords.is_empty());
        assert!(!config.mindset.divergent_keywords.is_empty());
        assert!(!config.mindset.convergent_keywords.is_empty());
        assert!(config
            .mindset
            .algorithmic_keywords
            .contains(&"verify".to_string()));
        assert!(config
            .mindset
            .divergent_keywords
            .contains(&"explore".to_string()));
        assert!(config
            .mindset
            .convergent_keywords
            .contains(&"summarize".to_string()));
    }

    #[test]
    fn test_mindset_config_parse_toml() {
        let toml = r#"
[mindset]
enabled = false
algorithmic_keywords = ["audit", "fact-check"]
divergent_keywords = ["hypothesize"]
convergent_keywords = ["wrap up"]
"#;
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert!(!config.mindset.enabled);
        assert_eq!(
            config.mindset.algorithmic_keywords,
            vec!["audit", "fact-check"]
        );
        assert_eq!(config.mindset.divergent_keywords, vec!["hypothesize"]);
        assert_eq!(config.mindset.convergent_keywords, vec!["wrap up"]);
    }

    #[test]
    fn test_mindset_config_empty_uses_defaults() {
        let toml = "";
        let config = UcotronConfig::parse_toml(toml).unwrap();
        assert!(config.mindset.enabled);
        assert_eq!(config.mindset.algorithmic_keywords.len(), 6);
        assert_eq!(config.mindset.divergent_keywords.len(), 6);
        assert_eq!(config.mindset.convergent_keywords.len(), 6);
    }
}