cartog 0.25.1

Code graph indexer for LLM coding agents. Map your codebase, navigate by graph.
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
use serde::Deserialize;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

/// Top-level cartog configuration, loaded from `.cartog.toml`.
///
/// Priority (highest to lowest):
/// 1. `--db` CLI flag / `CARTOG_DB` env var       (handled in main)
/// 2. `.cartog.toml` at git root or cwd           (`database.path`)
/// 3. Auto git-root detection: prefer `.cartog/db.sqlite`,
///    fall back to legacy `.cartog.db` if only it exists
/// 4. cwd fallback to `.cartog/db.sqlite`
#[derive(Debug, Default, Deserialize)]
pub struct CartogConfig {
    pub database: Option<DatabaseConfig>,
    pub embedding: Option<EmbeddingConfig>,
    pub reranker: Option<RerankerConfig>,
    pub rag: Option<RagConfig>,
    pub remote: Option<RemoteConfig>,
    pub security: Option<SecurityConfig>,
}

/// Secret-redaction settings.
#[derive(Debug, Default, Clone, Deserialize)]
pub struct SecurityConfig {
    /// Redact known secret patterns from stored symbol text. Default: true.
    /// The sensitive-file deny-list is always enforced regardless of this flag.
    pub redact_secrets: Option<bool>,
}

impl SecurityConfig {
    /// Whether secret redaction is enabled (default: true).
    #[must_use]
    pub fn redact_secrets(&self) -> bool {
        self.redact_secrets.unwrap_or(true)
    }
}

/// Optional S3-compatible remote for `cartog push` / `cartog pull`.
///
/// Credentials are resolved exclusively from the AWS environment chain (env
/// vars, profile, IMDS). Storing any credential-shaped key here (`access_key`,
/// `secret_key`, `credentials`, `token`, `aws_*`) is rejected at parse time —
/// see [`RemoteConfig::validate_no_credentials`].
///
/// ```toml
/// [remote]
/// url        = "s3://my-team-bucket/cartog/main"
/// region     = "us-east-1"
/// endpoint   = "https://minio.example.com"   # MinIO / R2 / floci
/// path_style = true                          # required for MinIO / floci
/// ```
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
// `region`, `endpoint`, and `path_style` are unused in minimal builds (no
// `remote-s3` feature). They still must be parsed and rejected on unknown
// keys, so the struct itself stays defined — but the fields would warn as
// unused. Silence that warning for the minimal build only.
#[cfg_attr(not(feature = "remote-s3"), allow(dead_code))]
pub struct RemoteConfig {
    /// Default `s3://bucket/key` target. `--remote` on push/pull overrides.
    pub url: Option<String>,
    /// AWS region (e.g. `us-east-1`). Optional when `endpoint` is set.
    pub region: Option<String>,
    /// Custom endpoint URL for S3-compatible stores (MinIO, R2, floci).
    pub endpoint: Option<String>,
    /// Force path-style addressing (required for most non-AWS endpoints).
    pub path_style: Option<bool>,
}

const CREDENTIAL_KEY_PREFIXES: &[&str] = &["aws_", "access_", "secret_"];
const CREDENTIAL_KEYS: &[&str] = &[
    "access_key",
    "secret_key",
    "credentials",
    "token",
    "session_token",
    "password",
];

/// Recursively inspect the raw `[remote]` table for credential-shaped keys.
///
/// `RemoteConfig` already has `deny_unknown_fields`, but that's coincidental
/// coverage that would silently regress the moment we accept a sub-table
/// (e.g. a future `[remote.headers]`). This walk is the real security
/// boundary: it traverses nested tables and arrays so `[remote.aws]
/// access_key = "..."` is rejected with the same actionable error as a
/// flat `[remote] access_key = "..."`.
///
/// Returned error contains a dotted path (e.g. `[remote].aws.access_key`)
/// so the user knows which line to delete.
fn validate_remote_no_credentials(table: &toml::value::Table) -> Result<(), String> {
    fn walk(prefix: &str, val: &toml::Value) -> Result<(), String> {
        match val {
            toml::Value::Table(t) => {
                for (k, v) in t {
                    let lower = k.to_lowercase();
                    if CREDENTIAL_KEYS.iter().any(|ck| lower == *ck)
                        || CREDENTIAL_KEY_PREFIXES.iter().any(|p| lower.starts_with(p))
                    {
                        return Err(format!(
                            "{prefix}.{k} looks like a credential — cartog does not read \
                             credentials from .cartog.toml. Use the AWS environment chain \
                             instead (AWS_ACCESS_KEY_ID / AWS_PROFILE / IMDS)."
                        ));
                    }
                    walk(&format!("{prefix}.{k}"), v)?;
                }
            }
            toml::Value::Array(arr) => {
                for (i, v) in arr.iter().enumerate() {
                    walk(&format!("{prefix}[{i}]"), v)?;
                }
            }
            _ => {}
        }
        Ok(())
    }
    walk("[remote]", &toml::Value::Table(table.clone()))
}

/// Tuning knobs for the hybrid search pipeline.
///
/// ```toml
/// [rag]
/// retrieval_multiplier = 3
/// retrieval_floor      = 20
/// rerank_max           = 50
/// rerank_min           = 8
/// ```
#[derive(Debug, Default, Clone, Deserialize)]
pub struct RagConfig {
    /// Over-retrieval multiplier for FTS5 + vector candidate pools.
    pub retrieval_multiplier: Option<u32>,
    /// Lower bound on candidate retrieval, independent of `limit`.
    pub retrieval_floor: Option<u32>,
    /// Cap on candidates passed to the cross-encoder.
    pub rerank_max: Option<u32>,
    /// Skip the cross-encoder entirely below this many candidates.
    pub rerank_min: Option<u32>,
}

impl RagConfig {
    /// Build a `SearchTuning` with caller-provided overrides applied on top
    /// of defaults, clamping invalid combinations so search never degrades
    /// to zero candidates or a silently-disabled reranker.
    pub fn to_search_tuning(&self) -> cartog_rag::search::SearchTuning {
        let d = cartog_rag::search::SearchTuning::default();
        // Multipliers and floors of 0 would collapse retrieval to nothing —
        // clamp to 1.
        let retrieval_multiplier = self
            .retrieval_multiplier
            .unwrap_or(d.retrieval_multiplier)
            .max(1);
        let retrieval_floor = self.retrieval_floor.unwrap_or(d.retrieval_floor).max(1);
        let rerank_max = self.rerank_max.unwrap_or(d.rerank_max);
        let rerank_min = self.rerank_min.unwrap_or(d.rerank_min);
        // If a user wrote `rerank_min > rerank_max`, the reranker would
        // silently never fire. Cap rerank_min at rerank_max.
        let rerank_min = rerank_min.min(rerank_max);
        cartog_rag::search::SearchTuning {
            retrieval_multiplier,
            retrieval_floor,
            rerank_max,
            rerank_min,
        }
    }
}

#[derive(Debug, Default, Deserialize)]
pub struct DatabaseConfig {
    /// Filesystem path to the cartog SQLite database. Supports `~` expansion.
    pub path: Option<String>,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct EmbeddingConfig {
    /// Provider type: "local" (default), "ollama", or "openai".
    pub provider: Option<String>,
    /// Model name. For "local": fastembed built-in name or HuggingFace repo ID.
    /// For "ollama"/"openai": model name on the server.
    pub model: Option<String>,
    /// Embedding dimension. Auto-detected for built-in models, required for custom HF models.
    pub dimension: Option<usize>,
    /// Local provider settings (ONNX via fastembed).
    pub local: Option<LocalEmbeddingConfig>,
    /// Ollama provider settings.
    pub ollama: Option<OllamaConfig>,
    /// OpenAI-compatible provider settings.
    pub openai: Option<OpenAiConfig>,
    /// Auto-embed under `serve --watch` / `watch`. `None` = auto-detect (embed
    /// only if the repo already has embeddings); `Some(false)` = never;
    /// `Some(true)` = always. Precedence: `CARTOG_WATCH_RAG` env > this key >
    /// `--rag` flag (so this key overrides `--rag`, not the other way around).
    pub auto_embed: Option<bool>,
}

pub const DEFAULT_EMBEDDING_PROVIDER: &str = "local";

impl EmbeddingConfig {
    pub fn provider(&self) -> &str {
        self.provider
            .as_deref()
            .unwrap_or(DEFAULT_EMBEDDING_PROVIDER)
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct LocalEmbeddingConfig {
    /// Prefix prepended to text during search (e.g. "search_query: ").
    pub query_prefix: Option<String>,
    /// Prefix prepended to text during indexing (e.g. "search_document: ").
    pub document_prefix: Option<String>,
    /// Optional cap on ONNX intra-op threads for indexing/reranking. None =
    /// all cores (fastembed default). `CARTOG_ONNX_THREADS` overrides; read at
    /// provider load (restart `serve` to change it).
    pub intra_threads: Option<usize>,
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct OllamaConfig {
    /// Ollama server URL (default: "http://localhost:11434").
    pub base_url: Option<String>,
    /// Model name (default: "nomic-embed-text").
    pub model: Option<String>,
}

pub const DEFAULT_OLLAMA_BASE_URL: &str = cartog_rag::providers::DEFAULT_OLLAMA_BASE_URL;
pub const DEFAULT_OLLAMA_MODEL: &str = cartog_rag::providers::DEFAULT_OLLAMA_MODEL;

impl OllamaConfig {
    pub fn base_url(&self) -> &str {
        self.base_url.as_deref().unwrap_or(DEFAULT_OLLAMA_BASE_URL)
    }

    pub fn model(&self) -> &str {
        self.model.as_deref().unwrap_or(DEFAULT_OLLAMA_MODEL)
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct OpenAiConfig {
    /// OpenAI-compatible base URL (default: "https://api.openai.com/v1").
    pub base_url: Option<String>,
    /// Model name (default: "text-embedding-3-small").
    pub model: Option<String>,
    /// Env var NAME holding the API key (default: "OPENAI_API_KEY"). The key
    /// value is never stored in config; unset means a keyless local endpoint.
    pub api_key_env: Option<String>,
}

pub const DEFAULT_OPENAI_BASE_URL: &str = cartog_rag::providers::DEFAULT_OPENAI_BASE_URL;
pub const DEFAULT_OPENAI_MODEL: &str = cartog_rag::providers::DEFAULT_OPENAI_MODEL;
pub const DEFAULT_OPENAI_API_KEY_ENV: &str = cartog_rag::providers::DEFAULT_OPENAI_API_KEY_ENV;

impl OpenAiConfig {
    /// Endpoint base URL, or [`DEFAULT_OPENAI_BASE_URL`] when unset.
    pub fn base_url(&self) -> &str {
        self.base_url.as_deref().unwrap_or(DEFAULT_OPENAI_BASE_URL)
    }

    /// Embedding model name, or [`DEFAULT_OPENAI_MODEL`] when unset.
    pub fn model(&self) -> &str {
        self.model.as_deref().unwrap_or(DEFAULT_OPENAI_MODEL)
    }

    /// Env var name holding the API key, or [`DEFAULT_OPENAI_API_KEY_ENV`] when unset.
    pub fn api_key_env(&self) -> &str {
        self.api_key_env
            .as_deref()
            .unwrap_or(DEFAULT_OPENAI_API_KEY_ENV)
    }
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct RerankerConfig {
    /// Provider type: "local" (default) or "none".
    pub provider: Option<String>,
    /// Reranker model as a fastembed HF repo path (e.g. `BAAI/bge-reranker-base`).
    /// None = [`cartog_rag::DEFAULT_RERANKER_MODEL`]. Mirrors `[embedding] model`.
    pub model: Option<String>,
}

pub const DEFAULT_RERANKER_PROVIDER: &str = "local";

impl RerankerConfig {
    pub fn provider(&self) -> &str {
        self.provider
            .as_deref()
            .unwrap_or(DEFAULT_RERANKER_PROVIDER)
    }
}

/// Resolve whether the watcher should auto-embed, as an explicit override.
///
/// Precedence: `CARTOG_WATCH_RAG` env (`0`/`false`/`1`/`true`) > `[embedding]
/// auto_embed` > `--rag` flag. Returns `None` when no explicit signal is set,
/// leaving the watcher to decide from the live `embedding_count` (auto-detect).
#[must_use]
pub fn resolve_auto_embed(cli_rag: bool, config: &CartogConfig) -> Option<bool> {
    resolve_auto_embed_with(
        std::env::var("CARTOG_WATCH_RAG").ok().as_deref(),
        config.embedding.as_ref().and_then(|e| e.auto_embed),
        cli_rag,
    )
}

/// Pure resolver for [`resolve_auto_embed`], split out so tests don't mutate the
/// process environment. An unparseable env value falls through to the next tier.
fn resolve_auto_embed_with(env: Option<&str>, cfg: Option<bool>, cli_rag: bool) -> Option<bool> {
    if let Some(v) = env.and_then(parse_bool_flag) {
        return Some(v);
    }
    cfg.or_else(|| cli_rag.then_some(true))
}

/// Parse a permissive boolean env value; `None` if unrecognized.
fn parse_bool_flag(s: &str) -> Option<bool> {
    match s.trim().to_ascii_lowercase().as_str() {
        "1" | "true" | "yes" | "on" => Some(true),
        "0" | "false" | "no" | "off" => Some(false),
        _ => None,
    }
}

/// Build the indexer redaction policy from the `[security]` section.
pub fn to_redaction_config(config: &CartogConfig) -> cartog_indexer::RedactionConfig {
    let enabled = config
        .security
        .as_ref()
        .map_or(true, SecurityConfig::redact_secrets);
    cartog_indexer::RedactionConfig { enabled }
}

/// Convert the embedding config section into an `EmbeddingProviderConfig` for cartog-rag.
pub fn to_provider_config(config: &CartogConfig) -> cartog_rag::EmbeddingProviderConfig {
    // The reranker section is independent of the embedding section — honor it even
    // when `[embedding]` is absent (otherwise a lone `[reranker]` block is dropped).
    let reranker_provider = config
        .reranker
        .as_ref()
        .map(|r| r.provider().to_string())
        .unwrap_or_else(|| DEFAULT_RERANKER_PROVIDER.to_string());
    let reranker_model = config.reranker.as_ref().and_then(|r| r.model.clone());

    match &config.embedding {
        Some(embed) => {
            let (query_prefix, document_prefix, intra_threads) = match &embed.local {
                Some(local) => (
                    local.query_prefix.clone(),
                    local.document_prefix.clone(),
                    local.intra_threads,
                ),
                None => (None, None, None),
            };
            // Resolve base_url/model/api_key_env from the sub-table matching the
            // active provider — not whichever sub-table happens to be present, or a
            // lingering [embedding.ollama] block would override an openai config.
            let (sub_base_url, sub_model, api_key_env) = match embed.provider() {
                "ollama" => (
                    embed.ollama.as_ref().map(|o| o.base_url().to_string()),
                    embed.ollama.as_ref().map(|o| o.model().to_string()),
                    None,
                ),
                "openai" => (
                    embed.openai.as_ref().map(|o| o.base_url().to_string()),
                    embed.openai.as_ref().map(|o| o.model().to_string()),
                    embed.openai.as_ref().map(|o| o.api_key_env().to_string()),
                ),
                _ => (None, None, None),
            };
            cartog_rag::EmbeddingProviderConfig {
                provider: embed.provider().to_string(),
                model: embed.model.clone().or(sub_model),
                dimension: embed.dimension,
                query_prefix,
                document_prefix,
                base_url: sub_base_url,
                api_key_env,
                reranker_provider,
                reranker_model,
                intra_threads,
            }
        }
        None => cartog_rag::EmbeddingProviderConfig {
            reranker_provider,
            reranker_model,
            ..Default::default()
        },
    }
}

/// Outcome of [`load_config`]. Distinguishes three states the caller may need
/// to react to differently:
///
/// - **`Loaded { config, path }`** — `.cartog.toml` parsed successfully.
/// - **`Missing`** — no config file was found anywhere on the walk-up to
///   git root; caller proceeds with defaults silently.
/// - **`Rejected { path }`** — a config file was found but rejected (parse
///   error, security pre-check, or `deny_unknown_fields` violation).
///   `read_config` already printed the underlying reason to stderr. Callers
///   that read `[remote]` (push/pull/doctor) must NOT silently fall back to
///   defaults here, or the user's security-error message would be drowned
///   by a misleading downstream "no remote configured" error.
// `Loaded` carries the full `CartogConfig` (~344 B) while the other variants
// are tiny. We accept the size disparity rather than box the payload: every
// real call site moves the config back onto the stack immediately, so a
// `Box` would just add one heap alloc + memcpy per invocation for no
// benefit. The lint is correct in general; not correct here.
#[allow(clippy::large_enum_variant)]
pub enum ConfigLoad {
    Loaded { config: CartogConfig, path: PathBuf },
    Missing,
    Rejected { path: PathBuf },
}

impl ConfigLoad {
    /// Convenience: the parsed config when present, or a fresh default
    /// otherwise. Use for commands that don't care about distinguishing
    /// missing-vs-rejected (most read-only commands).
    pub fn config_or_default(self) -> CartogConfig {
        match self {
            ConfigLoad::Loaded { config, .. } => config,
            _ => CartogConfig::default(),
        }
    }

    /// The path the config was loaded from (or attempted to load from when
    /// `Rejected`). Used by `cartog doctor` and `cartog config` to display
    /// the file under inspection.
    pub fn path(&self) -> Option<&Path> {
        match self {
            ConfigLoad::Loaded { path, .. } | ConfigLoad::Rejected { path } => Some(path),
            ConfigLoad::Missing => None,
        }
    }

    /// True when a `.cartog.toml` was found but failed validation. Callers
    /// that depend on `[remote]` (push, pull, doctor, config) use this to
    /// distinguish "no config" from "broken config" and surface a clear
    /// rejection rather than silently falling back to defaults.
    pub fn is_rejected(&self) -> bool {
        matches!(self, ConfigLoad::Rejected { .. })
    }
}

/// Load the local project config from `.cartog.toml`. See [`ConfigLoad`]
/// for the three possible outcomes; existing commands that don't care
/// about the rejected-vs-missing distinction can wrap this with
/// [`ConfigLoad::config_or_default`].
pub fn load_config() -> ConfigLoad {
    match local_config_path() {
        Some(p) => match read_config(&p) {
            Some(config) => ConfigLoad::Loaded { config, path: p },
            None => ConfigLoad::Rejected { path: p },
        },
        None => ConfigLoad::Missing,
    }
}

/// Path to the local project config: `.cartog.toml` found by walking up from
/// cwd to the git root. Returns `None` if no such file exists.
fn local_config_path() -> Option<PathBuf> {
    let mut dir = std::env::current_dir().ok()?;
    loop {
        let candidate = dir.join(".cartog.toml");
        if candidate.exists() {
            return Some(candidate);
        }
        // Stop searching once we reach the git root without finding a config.
        if dir.join(".git").exists() {
            return None;
        }
        if !dir.pop() {
            break;
        }
    }
    None
}

/// Known top-level sections of `.cartog.toml`. Kept in sync with the fields of
/// [`CartogConfig`]. Unknown keys are warned about (non-fatal) so a typo like
/// `[embeddings]` is visible instead of silently ignored.
const KNOWN_CONFIG_SECTIONS: &[&str] = &[
    "database",
    "embedding",
    "reranker",
    "rag",
    "remote",
    "security",
];

/// Collect top-level keys that are not a recognized config section.
fn unknown_sections(raw: &toml::value::Table) -> Vec<&str> {
    raw.keys()
        .map(String::as_str)
        .filter(|k| !KNOWN_CONFIG_SECTIONS.contains(k))
        .collect()
}

/// Config-load diagnostics run before the tracing subscriber is initialised
/// (db-path resolution happens early in `main`), so they use `eprintln!`
/// rather than `tracing`. To avoid polluting the stderr of non-interactive
/// consumers — the MCP `serve` child, `--json` queries, CI pipes — they are
/// emitted only when stderr is a terminal (an interactive human is watching).
fn config_diagnostics_visible() -> bool {
    std::io::stderr().is_terminal()
}

/// Emit a one-line stderr warning for each unrecognized top-level config key.
fn warn_unknown_sections(raw: &toml::value::Table, path: &Path) {
    if !config_diagnostics_visible() {
        return;
    }
    for key in unknown_sections(raw) {
        eprintln!(
            "cartog: warning: unknown config key '{key}' in {} (ignored)",
            path.display()
        );
    }
}

fn read_config(path: &Path) -> Option<CartogConfig> {
    let text = match std::fs::read_to_string(path) {
        Ok(t) => t,
        // NotFound is the only IO error we treat as "no config", silently.
        // The normal caller (`local_config_path`) only hands us paths that
        // exist; this branch covers races where the file disappears between
        // `exists()` and `read_to_string`, and unit tests that probe missing
        // paths directly. Permission denied, EIO, EACCES, etc. should be
        // loud — silently swallowing them turned into a "no remote
        // configured" downstream error with no hint that the user's file
        // was just unreadable.
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return None,
        Err(e) => {
            eprintln!("cartog: error reading {}: {e}", path.display());
            return None;
        }
    };

    // Security pre-check: scan the raw `[remote]` table for credential-shaped
    // keys before they have a chance to be deserialised or logged anywhere.
    // Also warn (non-fatal) about unknown top-level sections so a typo like
    // `[embeddings]` doesn't silently leave the user on defaults.
    if let Ok(raw) = toml::from_str::<toml::value::Table>(&text) {
        if let Some(toml::Value::Table(remote)) = raw.get("remote") {
            if let Err(msg) = validate_remote_no_credentials(remote) {
                eprintln!("cartog: error in {}: {msg}", path.display());
                return None;
            }
        }
        warn_unknown_sections(&raw, path);
    }

    let parsed = match toml::from_str::<CartogConfig>(&text) {
        Ok(cfg) => cfg,
        Err(e) => {
            // Use eprintln rather than tracing — tracing may not be initialised yet.
            eprintln!("cartog: warning: failed to parse {}: {e}", path.display());
            return None;
        }
    };

    // Post-parse security check on `[remote].endpoint`. `parse_s3_url` already
    // refuses `s3://user:pass@bucket/key`, but `endpoint` accepts an arbitrary
    // URL — a value like `http://AKIA:secret@minio.local` would silently leak
    // credentials into the underlying S3 client's URL builder, bypassing the
    // "credentials only via AWS env chain" guarantee. Refuse explicitly.
    if let Some(remote) = parsed.remote.as_ref() {
        if let Err(msg) = validate_endpoint(remote.endpoint.as_deref()) {
            eprintln!("cartog: error in {}: {msg}", path.display());
            return None;
        }
    }

    // Reject an unknown `provider` value at parse time. Without this a typo
    // (`provider = "ollma"`) only surfaces later, when the provider is actually
    // loaded — and the reranker typo never surfaces at all. Fail fast here.
    if let Err(msg) = validate_providers(&parsed) {
        eprintln!("cartog: error in {}: {msg}", path.display());
        return None;
    }

    Some(parsed)
}

/// Reject an unknown embedding/reranker `provider` value. Unknown values are a
/// user typo: surface them at config load rather than at first use. Absent
/// (`None`) means "use the default" and is always accepted.
fn validate_providers(config: &CartogConfig) -> Result<(), String> {
    const EMBEDDING_PROVIDERS: &[&str] = &["local", "ollama", "openai"];
    const RERANKER_PROVIDERS: &[&str] = &["local", "none"];

    if let Some(p) = config
        .embedding
        .as_ref()
        .and_then(|e| e.provider.as_deref())
    {
        if !EMBEDDING_PROVIDERS.contains(&p) {
            return Err(format!(
                "unknown embedding provider '{p}'; supported: {}",
                EMBEDDING_PROVIDERS.join(", ")
            ));
        }
    }
    if let Some(p) = config.reranker.as_ref().and_then(|r| r.provider.as_deref()) {
        if !RERANKER_PROVIDERS.contains(&p) {
            return Err(format!(
                "unknown reranker provider '{p}'; supported: {}",
                RERANKER_PROVIDERS.join(", ")
            ));
        }
    }
    Ok(())
}

/// Reject a `[remote].endpoint` value that embeds credentials via the
/// `user:pass@host` URL form. None / empty endpoint is fine — both mean
/// "fall back to the default AWS host".
fn validate_endpoint(endpoint: Option<&str>) -> Result<(), String> {
    let ep = match endpoint {
        Some(s) if !s.is_empty() => s,
        _ => return Ok(()),
    };

    // Trim the scheme so `s3://user@host` is detected too.
    let after_scheme = ep.split_once("://").map(|x| x.1).unwrap_or(ep);
    // Userinfo lives before the first `/` of the path and before any `?` or `#`.
    let authority = after_scheme
        .split('/')
        .next()
        .unwrap_or(after_scheme)
        .split('?')
        .next()
        .unwrap_or(after_scheme)
        .split('#')
        .next()
        .unwrap_or(after_scheme);
    if authority.contains('@') {
        return Err(format!(
            "[remote].endpoint embeds credentials in its URL ({ep:?}) — cartog \
             does not accept credentials in config. Move them to the AWS \
             environment chain (AWS_ACCESS_KEY_ID / AWS_PROFILE / IMDS) and \
             use a plain endpoint URL."
        ));
    }
    Ok(())
}

/// Resolve the database path using the following priority:
///
/// 1. `explicit` — from `--db` flag or `CARTOG_DB` env var (already merged by clap)
/// 2. `config.database.path` — from `.cartog.toml` at git root / cwd
/// 3. Auto git-root detection: prefer `<root>/.cartog/db.sqlite`, fall back to
///    legacy `<root>/.cartog.db` if only it exists (warns once, points at
///    `cartog self migrate-db`)
/// 4. cwd fallback — `.cartog/db.sqlite` in the current directory
pub fn resolve_db_path(explicit: Option<PathBuf>, config: &CartogConfig) -> PathBuf {
    // 1. Explicit override (--db / CARTOG_DB)
    if let Some(p) = explicit {
        return expand_tilde(p);
    }

    // 2. Local project config
    if let Some(path_str) = config.database.as_ref().and_then(|d| d.path.as_deref()) {
        return expand_tilde(PathBuf::from(path_str));
    }

    // 3. Walk up to git root
    if let Ok(mut dir) = std::env::current_dir() {
        loop {
            if dir.join(".git").exists() {
                return resolve_root_db_path(&dir);
            }
            if !dir.pop() {
                break;
            }
        }
    }

    // 4. Fallback relative to cwd
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    resolve_root_db_path(&cwd)
}

/// Prefer `.cartog/db.sqlite`; fall back to legacy `.cartog.db` with a warning.
fn resolve_root_db_path(root: &Path) -> PathBuf {
    let new_path = root.join(cartog_db::DB_DIR).join(cartog_db::DB_FILENAME);
    let legacy = root.join(cartog_db::LEGACY_DB_FILE);
    if new_path.exists() {
        if legacy.exists() {
            warn_orphan_legacy_once(&legacy);
        }
        return new_path;
    }
    if legacy.exists() {
        warn_legacy_db_once(&legacy);
        return legacy;
    }
    new_path
}

fn warn_legacy_db_once(path: &Path) {
    use std::sync::atomic::{AtomicBool, Ordering};
    static WARNED: AtomicBool = AtomicBool::new(false);
    if WARNED.swap(true, Ordering::Relaxed) {
        return;
    }
    // eprintln, not tracing: db-path resolution runs before the tracing
    // subscriber is initialised in main, so a `tracing::warn!` here is dropped.
    // TTY-gated so it doesn't pollute MCP serve / --json / piped stderr.
    if !config_diagnostics_visible() {
        return;
    }
    eprintln!(
        "cartog: using legacy database at {}; run `cartog self migrate-db` to move it into .cartog/",
        path.display()
    );
}

fn warn_orphan_legacy_once(path: &Path) {
    use std::sync::atomic::{AtomicBool, Ordering};
    static WARNED: AtomicBool = AtomicBool::new(false);
    if WARNED.swap(true, Ordering::Relaxed) {
        return;
    }
    if !config_diagnostics_visible() {
        return;
    }
    eprintln!(
        "cartog: found legacy database at {} alongside the new layout; the legacy file is ignored",
        path.display()
    );
}

/// Expand a leading `~/` to the user's home directory.
pub fn expand_tilde(p: PathBuf) -> PathBuf {
    let s = p.to_string_lossy();
    if let Some(rest) = s.strip_prefix("~/") {
        if let Ok(home) = std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) {
            return PathBuf::from(home).join(rest);
        }
    }
    p
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::fs;

    #[test]
    fn test_expand_tilde_with_home() {
        let home = std::env::var("HOME")
            .or_else(|_| std::env::var("USERPROFILE"))
            .unwrap_or_else(|_| "/tmp".into());
        let expanded = expand_tilde(PathBuf::from("~/foo/bar"));
        assert_eq!(expanded, PathBuf::from(home).join("foo/bar"));
    }

    #[test]
    fn unknown_sections_flags_typos_but_not_known_keys() {
        let raw: toml::value::Table =
            toml::from_str("[embeddings]\nprovider = \"ollama\"\n[database]\npath = \"x\"\n")
                .unwrap();
        let unknown = unknown_sections(&raw);
        assert_eq!(unknown, vec!["embeddings"]);
    }

    #[test]
    fn unknown_sections_empty_for_all_known() {
        let raw: toml::value::Table =
            toml::from_str("[database]\npath = \"x\"\n[embedding]\nprovider = \"local\"\n")
                .unwrap();
        assert!(unknown_sections(&raw).is_empty());
    }

    #[test]
    fn validate_providers_accepts_known_values() {
        let config: CartogConfig =
            toml::from_str("[embedding]\nprovider = \"ollama\"\n[reranker]\nprovider = \"none\"\n")
                .unwrap();
        assert!(validate_providers(&config).is_ok());
    }

    #[test]
    fn validate_providers_accepts_absent_provider() {
        let config = CartogConfig::default();
        assert!(validate_providers(&config).is_ok());
    }

    #[test]
    fn validate_providers_rejects_unknown_embedding_provider() {
        let config: CartogConfig = toml::from_str("[embedding]\nprovider = \"ollma\"\n").unwrap();
        let err = validate_providers(&config).unwrap_err();
        assert!(
            err.contains("ollma"),
            "error should name the bad value: {err}"
        );
    }

    #[test]
    fn validate_providers_rejects_unknown_reranker_provider() {
        let config: CartogConfig = toml::from_str("[reranker]\nprovider = \"bogus\"\n").unwrap();
        let err = validate_providers(&config).unwrap_err();
        assert!(
            err.contains("bogus"),
            "error should name the bad value: {err}"
        );
    }

    #[test]
    fn read_config_rejects_unknown_provider() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join("config.toml");
        fs::write(&cfg_path, "[embedding]\nprovider = \"ollma\"\n").unwrap();
        assert!(read_config(&cfg_path).is_none());
    }

    #[test]
    fn test_expand_tilde_no_tilde() {
        let p = PathBuf::from("/absolute/path");
        assert_eq!(expand_tilde(p.clone()), p);
    }

    #[test]
    fn test_read_config_valid_toml() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join("config.toml");
        fs::write(&cfg_path, "[database]\npath = \"/tmp/test.db\"\n").unwrap();
        let cfg = read_config(&cfg_path).expect("should parse");
        assert_eq!(
            cfg.database.as_ref().unwrap().path.as_deref(),
            Some("/tmp/test.db")
        );
    }

    #[test]
    fn redact_secrets_defaults_true_when_absent() {
        let cfg = CartogConfig::default();
        assert!(to_redaction_config(&cfg).enabled);
    }

    #[test]
    fn redact_secrets_can_be_disabled_via_config() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join("config.toml");
        fs::write(&cfg_path, "[security]\nredact_secrets = false\n").unwrap();
        let cfg = read_config(&cfg_path).expect("should parse");
        assert!(!to_redaction_config(&cfg).enabled);
    }

    #[test]
    fn test_read_config_missing_file_returns_none() {
        let result = read_config(Path::new("/nonexistent/path/config.toml"));
        assert!(result.is_none());
    }

    #[test]
    fn test_read_config_invalid_toml_returns_none() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join("config.toml");
        fs::write(&cfg_path, "this is {{ not valid toml").unwrap();
        assert!(read_config(&cfg_path).is_none());
    }

    #[test]
    fn test_read_config_empty_toml_returns_default() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join("config.toml");
        fs::write(&cfg_path, "").unwrap();
        let cfg = read_config(&cfg_path).expect("empty toml is valid");
        assert!(cfg.database.is_none());
    }

    #[test]
    fn test_remote_config_valid_minimal() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join(".cartog.toml");
        fs::write(
            &cfg_path,
            r#"[remote]
url = "s3://team-bucket/cartog/main"
region = "us-east-1"
"#,
        )
        .unwrap();
        let cfg = read_config(&cfg_path).expect("should parse");
        let remote = cfg.remote.expect("remote section parsed");
        assert_eq!(remote.url.as_deref(), Some("s3://team-bucket/cartog/main"));
        assert_eq!(remote.region.as_deref(), Some("us-east-1"));
        assert_eq!(remote.path_style, None);
    }

    #[test]
    fn test_remote_config_full_minio_shape() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join(".cartog.toml");
        fs::write(
            &cfg_path,
            r#"[remote]
url = "s3://b/k"
region = "us-east-1"
endpoint = "https://minio.local"
path_style = true
"#,
        )
        .unwrap();
        let cfg = read_config(&cfg_path).expect("should parse");
        let r = cfg.remote.unwrap();
        assert_eq!(r.endpoint.as_deref(), Some("https://minio.local"));
        assert_eq!(r.path_style, Some(true));
    }

    /// `deny_unknown_fields` plus the credential pre-check together guarantee
    /// no rogue key sneaks through. This test exercises each named credential
    /// key and the `aws_*` prefix.
    #[test]
    fn test_remote_config_rejects_credential_keys() {
        for bad in [
            "access_key = \"AKIA...\"",
            "secret_key = \"...\"",
            "credentials = \"...\"",
            "token = \"...\"",
            "session_token = \"...\"",
            "password = \"...\"",
            "aws_access_key_id = \"...\"",
            "AWS_SECRET = \"...\"",
        ] {
            let dir = tempfile::TempDir::new().unwrap();
            let cfg_path = dir.path().join(".cartog.toml");
            fs::write(&cfg_path, format!("[remote]\nurl = \"s3://b/k\"\n{bad}\n")).unwrap();
            assert!(
                read_config(&cfg_path).is_none(),
                "should reject credential key: {bad}"
            );
        }
    }

    /// Credentials hidden one level deeper in `[remote.aws]` / `[remote.creds]`
    /// must not slip past the security pre-check. `deny_unknown_fields` would
    /// already reject the nested table itself, but the user-visible error
    /// would say "unknown field `aws`" rather than the security-specific
    /// message — which is the whole point of having this pre-check.
    #[test]
    fn test_remote_config_rejects_nested_credential_keys() {
        for bad_section in [
            "[remote.aws]\naccess_key = \"AKIA...\"\n",
            "[remote.creds]\nsecret_key = \"...\"\n",
            "[remote.minio]\naws_session_token = \"...\"\n",
        ] {
            let dir = tempfile::TempDir::new().unwrap();
            let cfg_path = dir.path().join(".cartog.toml");
            fs::write(
                &cfg_path,
                format!("[remote]\nurl = \"s3://b/k\"\n{bad_section}"),
            )
            .unwrap();
            assert!(
                read_config(&cfg_path).is_none(),
                "should reject nested credential: {bad_section}"
            );
        }
    }

    /// The prefix list (`aws_`, `access_`, `secret_`) is the second arm of the
    /// detector and only had implicit coverage before this test (the named
    /// keys mostly happen to match prefixes too). Exercise it directly so a
    /// future refactor that loses the prefix arm fails loudly.
    #[test]
    fn test_remote_config_rejects_credential_prefixes() {
        for bad in [
            // Names not in CREDENTIAL_KEYS but caught by prefix.
            "access_token_v2 = \"...\"",
            "secret_value = \"...\"",
            "aws_role_arn = \"...\"",
        ] {
            let dir = tempfile::TempDir::new().unwrap();
            let cfg_path = dir.path().join(".cartog.toml");
            fs::write(&cfg_path, format!("[remote]\nurl = \"s3://b/k\"\n{bad}\n")).unwrap();
            assert!(
                read_config(&cfg_path).is_none(),
                "should reject prefix-matched credential key: {bad}"
            );
        }
    }

    /// `deny_unknown_fields` should also reject anything else that's not a
    /// known field — this is a forward-compatibility guard so typos like
    /// `pathstyle` fail loudly instead of silently doing nothing.
    #[test]
    fn test_remote_config_rejects_unknown_field() {
        let dir = tempfile::TempDir::new().unwrap();
        let cfg_path = dir.path().join(".cartog.toml");
        fs::write(
            &cfg_path,
            "[remote]\nurl = \"s3://b/k\"\npathstyle = true\n",
        )
        .unwrap();
        assert!(read_config(&cfg_path).is_none());
    }

    /// `[remote].endpoint` must not embed credentials via the `user:pass@host`
    /// URL form. `parse_s3_url` rejects this for `url`; the symmetric check
    /// on `endpoint` closes the matching gap.
    #[test]
    fn test_remote_config_rejects_endpoint_with_userinfo() {
        for bad in [
            "http://AKIA:secret@minio.local",
            "https://user@s3.example.com",
            // No scheme — still parseable as userinfo + host.
            "AKIA:secret@host:9000",
            "https://user:pass@host/path",
        ] {
            let dir = tempfile::TempDir::new().unwrap();
            let cfg_path = dir.path().join(".cartog.toml");
            fs::write(
                &cfg_path,
                format!("[remote]\nurl = \"s3://b/k\"\nendpoint = \"{bad}\"\n"),
            )
            .unwrap();
            assert!(
                read_config(&cfg_path).is_none(),
                "should reject endpoint with userinfo: {bad}"
            );
        }
    }

    /// Common legitimate endpoint shapes must still parse. Belt-and-braces
    /// guard against an overly-aggressive `validate_endpoint` regex.
    #[test]
    fn test_remote_config_accepts_clean_endpoints() {
        for ok in [
            "https://s3.us-east-1.amazonaws.com",
            "https://minio.example.com:9000",
            "https://r2.cloudflarestorage.com/path",
            "http://localhost:4566",
        ] {
            let dir = tempfile::TempDir::new().unwrap();
            let cfg_path = dir.path().join(".cartog.toml");
            fs::write(
                &cfg_path,
                format!("[remote]\nurl = \"s3://b/k\"\nendpoint = \"{ok}\"\n"),
            )
            .unwrap();
            assert!(
                read_config(&cfg_path).is_some(),
                "should accept clean endpoint: {ok}"
            );
        }
    }

    #[test]
    fn test_resolve_explicit_wins_over_config() {
        let cfg = CartogConfig {
            database: Some(DatabaseConfig {
                path: Some("/config/path.db".to_string()),
            }),
            ..Default::default()
        };
        let result = resolve_db_path(Some(PathBuf::from("/explicit/path.db")), &cfg);
        assert_eq!(result, PathBuf::from("/explicit/path.db"));
    }

    #[test]
    fn test_resolve_config_path_used_when_no_explicit() {
        let cfg = CartogConfig {
            database: Some(DatabaseConfig {
                path: Some("/config/proj.db".to_string()),
            }),
            ..Default::default()
        };
        let result = resolve_db_path(None, &cfg);
        assert_eq!(result, PathBuf::from("/config/proj.db"));
    }

    #[test]
    #[serial]
    fn test_resolve_fallback_when_no_config_and_no_git() {
        let dir = tempfile::TempDir::new().unwrap();
        let canonical = dir.path().canonicalize().unwrap();
        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir.path()).unwrap();

        let result = resolve_db_path(None, &CartogConfig::default());
        std::env::set_current_dir(original).unwrap();

        assert_eq!(
            result,
            canonical
                .join(cartog_db::DB_DIR)
                .join(cartog_db::DB_FILENAME)
        );
    }

    #[test]
    #[serial]
    fn test_resolve_git_root_detection() {
        let dir = tempfile::TempDir::new().unwrap();
        let canonical_root = dir.path().canonicalize().unwrap();
        let git_dir = dir.path().join(".git");
        std::fs::create_dir(&git_dir).unwrap();
        let subdir = dir.path().join("subdir");
        std::fs::create_dir(&subdir).unwrap();

        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(&subdir).unwrap();

        let result = resolve_db_path(None, &CartogConfig::default());
        std::env::set_current_dir(original).unwrap();

        assert_eq!(
            result,
            canonical_root
                .join(cartog_db::DB_DIR)
                .join(cartog_db::DB_FILENAME)
        );
    }

    #[test]
    #[serial]
    fn test_resolve_prefers_new_layout_over_legacy() {
        let dir = tempfile::TempDir::new().unwrap();
        let canonical_root = dir.path().canonicalize().unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        // Both files exist — new layout wins.
        std::fs::create_dir(dir.path().join(cartog_db::DB_DIR)).unwrap();
        std::fs::write(
            dir.path()
                .join(cartog_db::DB_DIR)
                .join(cartog_db::DB_FILENAME),
            b"",
        )
        .unwrap();
        std::fs::write(dir.path().join(cartog_db::LEGACY_DB_FILE), b"").unwrap();

        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir.path()).unwrap();
        let result = resolve_db_path(None, &CartogConfig::default());
        std::env::set_current_dir(original).unwrap();

        assert_eq!(
            result,
            canonical_root
                .join(cartog_db::DB_DIR)
                .join(cartog_db::DB_FILENAME)
        );
    }

    #[test]
    #[serial]
    fn test_resolve_falls_back_to_legacy_db_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let canonical_root = dir.path().canonicalize().unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        // Only legacy file exists — picks it up (and warns once).
        std::fs::write(dir.path().join(cartog_db::LEGACY_DB_FILE), b"").unwrap();

        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir.path()).unwrap();
        let result = resolve_db_path(None, &CartogConfig::default());
        std::env::set_current_dir(original).unwrap();

        assert_eq!(result, canonical_root.join(cartog_db::LEGACY_DB_FILE));
    }

    // ── Embedding config tests ──

    #[test]
    fn test_embedding_config_defaults() {
        let cfg = EmbeddingConfig::default();
        assert_eq!(cfg.provider(), "local");
        assert!(cfg.dimension.is_none());
        assert!(cfg.model.is_none());
        assert!(cfg.local.is_none());
        assert!(cfg.ollama.is_none());
    }

    #[test]
    fn test_embedding_config_from_toml() {
        let toml_str = r#"
[embedding]
provider = "ollama"
model = "nomic-embed-text"
dimension = 768
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let embed = cfg.embedding.unwrap();
        assert_eq!(embed.provider(), "ollama");
        assert_eq!(embed.model.as_deref(), Some("nomic-embed-text"));
        assert_eq!(embed.dimension, Some(768));
    }

    #[test]
    fn test_embedding_config_local_with_prefixes() {
        let toml_str = r#"
[embedding]
provider = "local"
model = "BAAI/bge-small-en-v1.5"

[embedding.local]
query_prefix = "search_query: "
document_prefix = "search_document: "
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let embed = cfg.embedding.unwrap();
        assert_eq!(embed.provider(), "local");
        let local = embed.local.unwrap();
        assert_eq!(local.query_prefix.as_deref(), Some("search_query: "));
        assert_eq!(local.document_prefix.as_deref(), Some("search_document: "));
    }

    #[test]
    fn test_ollama_config_defaults() {
        let cfg = OllamaConfig::default();
        assert_eq!(cfg.base_url(), "http://localhost:11434");
        assert_eq!(cfg.model(), "nomic-embed-text");
    }

    #[test]
    fn test_ollama_config_from_toml() {
        let toml_str = r#"
[embedding.ollama]
base_url = "http://gpu-server:11434"
model = "mxbai-embed-large"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let ollama = cfg.embedding.unwrap().ollama.unwrap();
        assert_eq!(ollama.base_url(), "http://gpu-server:11434");
        assert_eq!(ollama.model(), "mxbai-embed-large");
    }

    #[test]
    fn test_openai_config_defaults() {
        let cfg = OpenAiConfig::default();
        assert_eq!(cfg.base_url(), "https://api.openai.com/v1");
        assert_eq!(cfg.model(), "text-embedding-3-small");
        assert_eq!(cfg.api_key_env(), "OPENAI_API_KEY");
    }

    #[test]
    fn test_openai_config_from_toml() {
        let toml_str = r#"
[embedding.openai]
base_url = "http://localhost:11434/v1"
model = "nomic-embed-text"
api_key_env = "MY_OPENAI_KEY"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let openai = cfg.embedding.unwrap().openai.unwrap();
        assert_eq!(openai.base_url(), "http://localhost:11434/v1");
        assert_eq!(openai.model(), "nomic-embed-text");
        assert_eq!(openai.api_key_env(), "MY_OPENAI_KEY");
    }

    #[test]
    fn validate_providers_accepts_openai() {
        let config: CartogConfig = toml::from_str("[embedding]\nprovider = \"openai\"\n").unwrap();
        assert!(validate_providers(&config).is_ok());
    }

    #[test]
    fn test_to_provider_config_threads_openai_settings() {
        let toml_str = r#"
[embedding]
provider = "openai"

[embedding.openai]
base_url = "https://api.mistral.ai/v1"
model = "mistral-embed"
api_key_env = "MISTRAL_API_KEY"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "openai");
        assert_eq!(pc.model.as_deref(), Some("mistral-embed"));
        assert_eq!(pc.base_url.as_deref(), Some("https://api.mistral.ai/v1"));
        assert_eq!(pc.api_key_env.as_deref(), Some("MISTRAL_API_KEY"));
    }

    #[test]
    fn test_to_provider_config_openai_ignores_lingering_ollama_block() {
        // A leftover [embedding.ollama] block must not override an openai config:
        // resolution keys off the active provider, not sub-table presence.
        let toml_str = r#"
[embedding]
provider = "openai"

[embedding.openai]
base_url = "https://api.openai.com/v1"
model = "text-embedding-3-small"

[embedding.ollama]
base_url = "http://localhost:11434"
model = "nomic-embed-text"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "openai");
        assert_eq!(pc.base_url.as_deref(), Some("https://api.openai.com/v1"));
        assert_eq!(pc.model.as_deref(), Some("text-embedding-3-small"));
    }

    #[test]
    fn test_to_provider_config_ollama_ignores_lingering_openai_block() {
        let toml_str = r#"
[embedding]
provider = "ollama"

[embedding.ollama]
base_url = "http://gpu:11434"

[embedding.openai]
base_url = "https://api.openai.com/v1"
api_key_env = "OPENAI_API_KEY"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "ollama");
        assert_eq!(pc.base_url.as_deref(), Some("http://gpu:11434"));
        // api_key_env belongs to openai only — never leaks into an ollama config.
        assert!(pc.api_key_env.is_none());
    }

    #[test]
    fn test_reranker_config_defaults() {
        let cfg = RerankerConfig::default();
        assert_eq!(cfg.provider(), "local");
    }

    #[test]
    fn test_reranker_config_none() {
        let toml_str = r#"
[reranker]
provider = "none"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(cfg.reranker.unwrap().provider(), "none");
    }

    #[test]
    fn test_reranker_model_defaults_to_none_in_config() {
        let cfg = RerankerConfig::default();
        assert!(cfg.model.is_none());
    }

    #[test]
    fn test_to_provider_config_reranker_model_from_toml() {
        let toml_str = r#"
[reranker]
model = "BAAI/bge-reranker-base"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.reranker_model.as_deref(), Some("BAAI/bge-reranker-base"));
    }

    #[test]
    fn test_to_provider_config_reranker_model_default_is_none() {
        // Unset model maps to None; cartog-rag resolves None to DEFAULT_RERANKER_MODEL.
        let cfg = CartogConfig::default();
        let pc = to_provider_config(&cfg);
        assert!(pc.reranker_model.is_none());
    }

    #[test]
    fn test_reranker_provider_none_keeps_model_inert() {
        // A model alongside provider="none" parses fine; the model is simply never loaded.
        let toml_str = r#"
[reranker]
provider = "none"
model = "BAAI/bge-reranker-base"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.reranker_provider, "none");
        assert_eq!(pc.reranker_model.as_deref(), Some("BAAI/bge-reranker-base"));
    }

    #[test]
    fn test_full_config_backward_compat() {
        let toml_str = r#"
[database]
path = "/tmp/test.db"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        assert!(cfg.embedding.is_none());
        assert!(cfg.reranker.is_none());
        assert_eq!(cfg.database.unwrap().path.as_deref(), Some("/tmp/test.db"));
    }

    #[test]
    fn test_config_unknown_fields_ignored() {
        let toml_str = r#"
[embedding]
provider = "local"
unknown_field = "should be ignored"
"#;
        // serde default: unknown fields are silently ignored
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(cfg.embedding.unwrap().provider(), "local");
    }

    // ── to_provider_config tests ──

    #[test]
    fn test_to_provider_config_defaults() {
        let cfg = CartogConfig::default();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "local");
        assert!(pc.model.is_none());
        assert_eq!(pc.resolved_dimension(), 384);
        assert!(pc.query_prefix.is_none());
        assert!(pc.document_prefix.is_none());
    }

    #[test]
    fn test_to_provider_config_from_toml() {
        let toml_str = r#"
[embedding]
provider = "ollama"
model = "nomic-embed-text"
dimension = 768

[embedding.local]
query_prefix = "search_query: "
document_prefix = "search_document: "
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "ollama");
        assert_eq!(pc.model.as_deref(), Some("nomic-embed-text"));
        assert_eq!(pc.resolved_dimension(), 768);
        assert_eq!(pc.query_prefix.as_deref(), Some("search_query: "));
        assert_eq!(pc.document_prefix.as_deref(), Some("search_document: "));
    }

    #[test]
    fn test_provider_config_dimension_override() {
        let pc = cartog_rag::EmbeddingProviderConfig {
            dimension: Some(1536),
            ..Default::default()
        };
        assert_eq!(pc.resolved_dimension(), 1536);
    }

    #[test]
    fn test_provider_config_dimension_default_fallback() {
        let pc = cartog_rag::EmbeddingProviderConfig::default();
        assert_eq!(pc.resolved_dimension(), 384);
        assert!(pc.dimension.is_none());
    }

    #[test]
    fn test_to_provider_config_ollama_model_fallback() {
        let toml_str = r#"
[embedding]
provider = "ollama"

[embedding.ollama]
model = "mxbai-embed-large"
base_url = "http://gpu:11434"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.provider, "ollama");
        assert_eq!(pc.model.as_deref(), Some("mxbai-embed-large"));
        assert_eq!(pc.base_url.as_deref(), Some("http://gpu:11434"));
    }

    #[test]
    fn test_to_provider_config_top_level_model_wins() {
        let toml_str = r#"
[embedding]
provider = "ollama"
model = "top-level-model"

[embedding.ollama]
model = "ollama-model"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.model.as_deref(), Some("top-level-model"),);
    }

    #[test]
    fn test_to_provider_config_base_url_threaded() {
        let toml_str = r#"
[embedding]
provider = "ollama"

[embedding.ollama]
base_url = "http://custom:11434"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert_eq!(pc.base_url.as_deref(), Some("http://custom:11434"));
    }

    #[test]
    fn test_to_provider_config_no_base_url_when_local() {
        let toml_str = r#"
[embedding]
provider = "local"
"#;
        let cfg: CartogConfig = toml::from_str(toml_str).unwrap();
        let pc = to_provider_config(&cfg);
        assert!(pc.base_url.is_none());
    }

    // ── RagConfig ──────────────────────────────────────────────────────

    #[test]
    fn to_search_tuning_clamps_zero_retrieval() {
        let cfg = RagConfig {
            retrieval_multiplier: Some(0),
            retrieval_floor: Some(0),
            rerank_max: None,
            rerank_min: None,
        };
        let t = cfg.to_search_tuning();
        assert_eq!(t.retrieval_multiplier, 1);
        assert_eq!(t.retrieval_floor, 1);
    }

    #[test]
    fn to_search_tuning_caps_rerank_min_at_max() {
        let cfg = RagConfig {
            retrieval_multiplier: None,
            retrieval_floor: None,
            rerank_max: Some(10),
            rerank_min: Some(50),
        };
        let t = cfg.to_search_tuning();
        assert_eq!(t.rerank_max, 10);
        assert_eq!(t.rerank_min, 10);
    }

    #[test]
    fn to_search_tuning_passes_valid_values() {
        let cfg = RagConfig {
            retrieval_multiplier: Some(5),
            retrieval_floor: Some(40),
            rerank_max: Some(100),
            rerank_min: Some(10),
        };
        let t = cfg.to_search_tuning();
        assert_eq!(t.retrieval_multiplier, 5);
        assert_eq!(t.retrieval_floor, 40);
        assert_eq!(t.rerank_max, 100);
        assert_eq!(t.rerank_min, 10);
    }

    #[test]
    fn auto_embed_env_beats_config_and_flag() {
        assert_eq!(
            resolve_auto_embed_with(Some("0"), Some(true), true),
            Some(false)
        );
        assert_eq!(
            resolve_auto_embed_with(Some("on"), Some(false), false),
            Some(true)
        );
    }

    #[test]
    fn auto_embed_config_beats_flag() {
        assert_eq!(
            resolve_auto_embed_with(None, Some(false), true),
            Some(false)
        );
        assert_eq!(resolve_auto_embed_with(None, Some(true), false), Some(true));
    }

    #[test]
    fn auto_embed_flag_only_when_no_other_signal() {
        assert_eq!(resolve_auto_embed_with(None, None, true), Some(true));
    }

    #[test]
    fn auto_embed_none_when_no_signal_defers_to_watcher() {
        assert_eq!(resolve_auto_embed_with(None, None, false), None);
        // Unparseable env falls through to the next tier.
        assert_eq!(resolve_auto_embed_with(Some("maybe"), None, false), None);
    }
}