vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
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
//! Configuration types and defaults for Vera's pipeline.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

pub(crate) const DEFAULT_MAX_FILE_SIZE_BYTES: u64 = 1_000_000;

/// Top-level configuration for Vera.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct VeraConfig {
    /// Indexing configuration.
    pub indexing: IndexingConfig,
    /// Retrieval configuration.
    pub retrieval: RetrievalConfig,
    /// Embedding configuration.
    pub embedding: EmbeddingConfig,
}

/// Configuration for the indexing pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexingConfig {
    /// Maximum lines for a single chunk before splitting.
    pub max_chunk_lines: u32,
    /// Default path exclusion patterns (in addition to .gitignore).
    pub default_excludes: Vec<String>,
    /// Maximum file size in bytes to index (skip larger files).
    pub max_file_size_bytes: u64,
    /// Extra exclusion globs from CLI `--exclude` flags.
    #[serde(default)]
    pub extra_excludes: Vec<String>,
    /// Disable .gitignore and .veraignore parsing.
    #[serde(default)]
    pub no_ignore: bool,
    /// Disable smart default exclusions.
    #[serde(default)]
    pub no_default_excludes: bool,
    /// Maximum chunk size in bytes for embedding. Chunks exceeding this are
    /// split at line boundaries. 0 disables byte-based splitting.
    /// Default: 24576 (24KB, ~6K-7K tokens). Local embedders see only the
    /// first 512 tokens of a chunk; the size is a retrieval-quality choice
    /// (measured on the Semble suite, see issue #67), not a model limit.
    #[serde(default = "default_max_chunk_bytes")]
    pub max_chunk_bytes: usize,
}

/// Read a `usize` config override from an environment variable, falling back
/// to `default` when unset or unparseable. Invalid values are reported so a
/// typo cannot silently change runtime behavior.
fn env_usize(key: &str, default: usize) -> usize {
    match std::env::var(key) {
        Ok(value) => match value.parse() {
            Ok(parsed) => parsed,
            Err(error) => {
                tracing::warn!(
                    key,
                    value = %value,
                    default,
                    error = %error,
                    "invalid numeric environment override; using default"
                );
                default
            }
        },
        Err(std::env::VarError::NotPresent) => default,
        Err(error) => {
            tracing::warn!(
                key,
                default,
                error = %error,
                "could not read numeric environment override; using default"
            );
            default
        }
    }
}

fn default_max_chunk_bytes() -> usize {
    env_usize("VERA_MAX_CHUNK_BYTES", 24_576)
}

impl Default for IndexingConfig {
    fn default() -> Self {
        Self {
            max_chunk_lines: 200,
            default_excludes: vec![
                ".git".to_string(),
                ".vera".to_string(),
                "node_modules".to_string(),
                "target".to_string(),
                "build".to_string(),
                "dist".to_string(),
                "__pycache__".to_string(),
                ".venv".to_string(),
            ],
            max_file_size_bytes: DEFAULT_MAX_FILE_SIZE_BYTES, // 1MB
            extra_excludes: Vec::new(),
            no_ignore: false,
            no_default_excludes: false,
            max_chunk_bytes: default_max_chunk_bytes(),
        }
    }
}

/// Configuration for the retrieval pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetrievalConfig {
    /// Number of results to return by default.
    pub default_limit: usize,
    /// RRF fusion constant (k in 1/(k + rank)).
    pub rrf_k: f64,
    /// Number of candidates to pass to the reranker.
    pub rerank_candidates: usize,
    /// Whether to enable reranking (requires API credentials).
    pub reranking_enabled: bool,
    /// Maximum documents per reranker API call. Larger candidate sets are
    /// partitioned into batches and scores merged. 0 means no batching.
    #[serde(default = "default_max_rerank_batch")]
    pub max_rerank_batch: usize,
    /// Total character budget for search output. Results are progressively
    /// truncated so the combined output stays within this limit.
    /// 0 means unlimited.
    #[serde(default = "default_max_output_chars")]
    pub max_output_chars: usize,
}

fn default_max_output_chars() -> usize {
    12_000
}

fn default_max_rerank_batch() -> usize {
    env_usize("VERA_MAX_RERANK_BATCH", 20)
}

impl Default for RetrievalConfig {
    fn default() -> Self {
        Self {
            default_limit: 5,
            rrf_k: 60.0,
            rerank_candidates: 50,
            reranking_enabled: false,
            max_rerank_batch: default_max_rerank_batch(),
            max_output_chars: 12_000,
        }
    }
}

/// Configuration for the embedding provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingConfig {
    /// Batch size for embedding API calls.
    pub batch_size: usize,
    /// Maximum number of concurrent embedding API requests.
    pub max_concurrent_requests: usize,
    /// Hard limit on the total number of embedding inputs that may be active
    /// across concurrent API requests. This bounds abandoned backend work when
    /// an indexing client disconnects.
    #[serde(default = "default_max_in_flight_inputs")]
    pub max_in_flight_inputs: usize,
    /// Request timeout in seconds.
    pub timeout_secs: u64,
    /// Maximum retries on transient errors.
    pub max_retries: u32,
    /// Maximum stored vector dimensionality.
    ///
    /// If the embedding model produces vectors larger than this, they
    /// are truncated to this dimensionality before storage. Qwen3 models
    /// support Matryoshka-style truncation, so lower dimensions still
    /// yield good retrieval quality while dramatically reducing index size.
    /// Set to 0 to store full-dimensionality vectors.
    pub max_stored_dim: usize,
    /// GPU memory limit in MB for ONNX CUDA sessions.
    /// 0 means no limit (ORT default: use all available VRAM).
    #[serde(default)]
    pub gpu_mem_limit_mb: u64,
    /// When true, forces conservative GPU settings (batch_size=1, low mem limit).
    #[serde(default)]
    pub low_vram: bool,
    /// Optional API query prefix override.
    #[serde(default)]
    pub query_prefix: Option<String>,
    /// Optional API document prefix override.
    #[serde(default)]
    pub document_prefix: Option<String>,
    /// Equivalent embedding model names.
    ///
    /// OpenAI-compatible providers sometimes expose a deployment alias while the
    /// embedding response, stored index metadata, or another compatible gateway
    /// reports the canonical upstream model name. Each inner list is one
    /// equivalence class. When two model names normalize into the same list, Vera
    /// treats them as index-compatible after the existing dimension check passes.
    /// Only alias models you have verified produce compatible embeddings.
    ///
    /// Can also be supplied with `VERA_EMBEDDING_MODEL_ALIASES`, using
    /// semicolon-separated groups of comma-separated aliases:
    /// `text-embedding-3-large,text-embedding-3-large-2;model-a,model-a-prod`
    #[serde(default)]
    pub model_aliases: Vec<Vec<String>>,
}

impl Default for EmbeddingConfig {
    fn default() -> Self {
        let is_local = is_local_mode();
        Self {
            batch_size: if is_local { 4 } else { 128 },
            max_concurrent_requests: if is_local { 1 } else { 8 },
            max_in_flight_inputs: default_max_in_flight_inputs(),
            timeout_secs: 60,
            max_retries: 3,
            max_stored_dim: 1024,
            gpu_mem_limit_mb: 0,
            low_vram: false,
            query_prefix: None,
            document_prefix: None,
            model_aliases: Vec::new(),
        }
    }
}

fn default_max_in_flight_inputs() -> usize {
    env_usize("VERA_MAX_IN_FLIGHT_INPUTS", 16).max(1)
}

impl EmbeddingConfig {
    /// Clamp configured batching so the product of batch size and concurrency
    /// never exceeds `max_in_flight_inputs`.
    pub fn bounded_parallelism(&self) -> (usize, usize) {
        let max_in_flight = self.max_in_flight_inputs.max(1);
        let batch_size = self.batch_size.max(1).min(max_in_flight);
        let max_concurrent_requests = self
            .max_concurrent_requests
            .max(1)
            .min((max_in_flight / batch_size).max(1));
        (batch_size, max_concurrent_requests)
    }
}

/// ONNX execution provider for local inference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum OnnxExecutionProvider {
    Cpu,
    Cuda,
    Rocm,
    DirectMl,
    CoreMl,
    OpenVino,
}

impl fmt::Display for OnnxExecutionProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cpu => write!(f, "cpu"),
            Self::Cuda => write!(f, "cuda"),
            Self::Rocm => write!(f, "rocm"),
            Self::DirectMl => write!(f, "directml"),
            Self::CoreMl => write!(f, "coreml"),
            Self::OpenVino => write!(f, "openvino"),
        }
    }
}

/// Inference backend selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum InferenceBackend {
    /// Use external OpenAI-compatible API for embeddings/reranking.
    Api,
    /// Use local ONNX models with the specified execution provider.
    OnnxJina(OnnxExecutionProvider),
    /// Use the CPU-first Potion Code static embedding model (the default local backend).
    PotionCode,
}

impl InferenceBackend {
    /// True if this backend uses local inference.
    pub fn is_local(self) -> bool {
        matches!(self, Self::OnnxJina(_) | Self::PotionCode)
    }

    /// True if this backend uses local ONNX inference.
    pub fn is_onnx(self) -> bool {
        matches!(self, Self::OnnxJina(_))
    }

    /// Get the execution provider (only for local backends).
    pub fn execution_provider(self) -> Option<OnnxExecutionProvider> {
        match self {
            Self::OnnxJina(ep) => Some(ep),
            Self::Api | Self::PotionCode => None,
        }
    }
}

impl fmt::Display for InferenceBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Api => write!(f, "api"),
            Self::OnnxJina(ep) => write!(f, "onnx-jina-{ep}"),
            Self::PotionCode => write!(f, "potion-code-cpu"),
        }
    }
}

impl FromStr for InferenceBackend {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "api" => Ok(Self::Api),
            "onnx-jina-cpu" => Ok(Self::OnnxJina(OnnxExecutionProvider::Cpu)),
            "onnx-jina-cuda" => Ok(Self::OnnxJina(OnnxExecutionProvider::Cuda)),
            "onnx-jina-rocm" => Ok(Self::OnnxJina(OnnxExecutionProvider::Rocm)),
            "onnx-jina-directml" => Ok(Self::OnnxJina(OnnxExecutionProvider::DirectMl)),
            "onnx-jina-coreml" => Ok(Self::OnnxJina(OnnxExecutionProvider::CoreMl)),
            "onnx-jina-openvino" => Ok(Self::OnnxJina(OnnxExecutionProvider::OpenVino)),
            "potion-code-cpu" | "potion-code" | "potion-cpu" => Ok(Self::PotionCode),
            other => Err(format!("unknown backend: {other}")),
        }
    }
}

/// Check if the local inference mode is active (legacy env var support).
pub fn is_local_mode() -> bool {
    std::env::var("VERA_LOCAL")
        .map(|v| v == "1" || v == "true")
        .unwrap_or(false)
}

/// Whether experimental structural graph augmentation is enabled.
///
/// This is intentionally an environment-only ablation switch rather than a
/// persisted retrieval setting. Accepted truthy values are `1`, `true`, and
/// `yes`, case-insensitively.
pub fn graph_augmentation_enabled() -> bool {
    std::env::var("VERA_GRAPH_AUGMENT")
        .map(|value| {
            value.eq_ignore_ascii_case("1")
                || value.eq_ignore_ascii_case("true")
                || value.eq_ignore_ascii_case("yes")
        })
        .unwrap_or(false)
}

fn backend_from_env() -> Option<InferenceBackend> {
    std::env::var("VERA_BACKEND")
        .ok()
        .and_then(|value| InferenceBackend::from_str(&value).ok())
}

impl VeraConfig {
    /// Adjust embedding parameters to match the actual backend.
    ///
    /// Saved configs may have API-mode defaults (batch 128, concurrency 8)
    /// even when the user switches to local mode. CPU inference needs small
    /// batches; GPU can handle larger ones. For GPU backends, this picks a
    /// coarse outer batch ceiling from available VRAM. The local ONNX provider
    /// still shapes the actual micro-batches from sequence length at runtime.
    pub fn adjust_for_backend(&mut self, backend: InferenceBackend) {
        match backend {
            InferenceBackend::PotionCode => {
                self.embedding.batch_size = 1024;
                self.embedding.max_concurrent_requests = 1;
                self.embedding.max_stored_dim = self.embedding.max_stored_dim.min(256);
            }
            InferenceBackend::OnnxJina(OnnxExecutionProvider::Cpu) => {
                self.embedding.batch_size = 4;
                self.embedding.max_concurrent_requests = 1;
            }
            InferenceBackend::OnnxJina(ep) => {
                self.embedding.max_concurrent_requests = 1;

                if self.embedding.low_vram {
                    self.embedding.batch_size = 1;
                    if self.embedding.gpu_mem_limit_mb == 0 {
                        self.embedding.gpu_mem_limit_mb = 1024;
                    }
                    tracing::info!(
                        "low-vram mode: batch_size=1, gpu_mem_limit={}MB",
                        self.embedding.gpu_mem_limit_mb
                    );
                    return;
                }

                let gpu_info = detect_gpu_info(ep);
                if let Some(vram) = gpu_info.vram_free_mb {
                    tracing::info!("detected GPU VRAM: {vram}MB");
                    // Auto-scale batch_size based on VRAM.
                    // Prioritize speed: use large batches when VRAM allows.
                    // A GPU reporting ~0 free MB is full or shared, so run
                    // the most conservative shape rather than trusting the
                    // reading as headroom.
                    let auto_batch = if vram < 512 {
                        1
                    } else if vram < 3072 {
                        4
                    } else if vram < 5120 {
                        16
                    } else if vram < 8192 {
                        32
                    } else if vram < 12288 {
                        64
                    } else {
                        128
                    };
                    // Unified memory is shared with macOS and apps; cap the
                    // CoreML batch so large-RAM Macs don't starve the system.
                    if ep == OnnxExecutionProvider::CoreMl {
                        self.embedding.batch_size = auto_batch.min(64);
                    } else {
                        self.embedding.batch_size = auto_batch;
                    }

                    // Set a conservative memory limit only for low-VRAM GPUs
                    // to prevent ORT from grabbing all VRAM. For >=8GB, no limit.
                    if self.embedding.gpu_mem_limit_mb == 0 && vram < 8192 {
                        // Use 80% of available VRAM, floored so a near-zero
                        // reading cannot decay into 0, which means "no ORT
                        // memory cap" everywhere else.
                        self.embedding.gpu_mem_limit_mb = ((vram as f64 * 0.8) as u64).max(128);
                        tracing::info!(
                            "auto-set gpu_mem_limit={}MB (80% of {vram}MB)",
                            self.embedding.gpu_mem_limit_mb
                        );
                    }
                } else {
                    // Could not detect VRAM; use conservative defaults.
                    // DirectML/CoreML/OpenVINO lack CLI VRAM detection,
                    // so pick a safe batch size that won't OOM on small GPUs.
                    self.embedding.batch_size = 16;
                }
            }
            InferenceBackend::Api => {}
        }
    }
}

/// GPU information collected from a single detection pass.
#[derive(Debug, Clone)]
pub struct GpuInfo {
    /// Free VRAM in MB, if detectable.
    pub vram_free_mb: Option<u64>,
    /// Device fingerprint string for profile keying.
    pub fingerprint: String,
}

/// Detect GPU information (VRAM and device fingerprint) for the given
/// execution provider. Runs the vendor CLI tool once and extracts both
/// pieces of data, avoiding duplicate subprocess calls.
pub fn detect_gpu_info(ep: OnnxExecutionProvider) -> GpuInfo {
    match ep {
        OnnxExecutionProvider::Cuda => detect_nvidia_gpu_info(),
        OnnxExecutionProvider::Rocm => detect_rocm_gpu_info(),
        OnnxExecutionProvider::CoreMl => detect_apple_silicon_mem_info(),
        _ => GpuInfo {
            vram_free_mb: None,
            fingerprint: host_fingerprint(ep),
        },
    }
}

/// Apple Silicon uses unified memory: the GPU shares system RAM. Report half
/// of total RAM (via `sysctl hw.memsize`) as the available pool so batch
/// auto-scaling works; returns None for VRAM off-macOS or on parse failure.
fn detect_apple_silicon_mem_info() -> GpuInfo {
    let output = std::process::Command::new("sysctl")
        .args(["-n", "hw.memsize"])
        .output()
        .ok();
    let total_mb = output
        .filter(|o| o.status.success())
        .and_then(|o| {
            String::from_utf8_lossy(&o.stdout)
                .trim()
                .parse::<u64>()
                .ok()
        })
        .map(|bytes| bytes / (1024 * 1024));
    // Half of system RAM is a conservative proxy for what the GPU can use
    // while macOS and other apps stay responsive.
    let vram_free_mb = total_mb.map(|mb| mb / 2);
    let brand = std::process::Command::new("sysctl")
        .args(["-n", "machdep.cpu.brand_string"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .filter(|s| !s.is_empty());
    let fingerprint = match (brand, total_mb) {
        (Some(brand), Some(mb)) => format!("{brand}|{mb}MB-unified"),
        _ => host_fingerprint(OnnxExecutionProvider::CoreMl),
    };
    GpuInfo {
        vram_free_mb,
        fingerprint,
    }
}

fn detect_nvidia_gpu_info() -> GpuInfo {
    // Single nvidia-smi call that returns free VRAM, device name, total VRAM, and driver.
    let output = std::process::Command::new("nvidia-smi")
        .args([
            "--query-gpu=memory.free,name,memory.total,driver_version",
            "--format=csv,noheader,nounits",
        ])
        .output()
        .ok();
    let Some(output) = output.filter(|o| o.status.success()) else {
        return GpuInfo {
            vram_free_mb: None,
            fingerprint: host_fingerprint(OnnxExecutionProvider::Cuda),
        };
    };
    let stdout = String::from_utf8_lossy(&output.stdout);
    let first_line = stdout.lines().find_map(|line| {
        let trimmed = line.trim();
        (!trimmed.is_empty()).then(|| trimmed.to_string())
    });
    let Some(line) = first_line else {
        return GpuInfo {
            vram_free_mb: None,
            fingerprint: host_fingerprint(OnnxExecutionProvider::Cuda),
        };
    };
    // CSV columns: memory.free, name, memory.total, driver_version
    let parts: Vec<&str> = line.split(',').map(str::trim).collect();
    let vram_free_mb = parts.first().and_then(|s| s.parse::<u64>().ok());
    // Fingerprint from name, total VRAM, driver (columns 1-3).
    let fingerprint = if parts.len() >= 4 {
        format!("{}|{}|{}", parts[1], parts[2], parts[3])
    } else {
        line.replace(", ", "|").replace(',', "|")
    };
    GpuInfo {
        vram_free_mb,
        fingerprint,
    }
}

fn detect_rocm_gpu_info() -> GpuInfo {
    // rocm-smi: get VRAM info and product name in one call.
    let output = std::process::Command::new("rocm-smi")
        .args(["--showproductname", "--showmeminfo", "vram", "--csv"])
        .output()
        .ok();
    let Some(output) = output.filter(|o| o.status.success()) else {
        return GpuInfo {
            vram_free_mb: None,
            fingerprint: host_fingerprint(OnnxExecutionProvider::Rocm),
        };
    };
    let stdout = String::from_utf8_lossy(&output.stdout);
    parse_rocm_gpu_csv(&stdout)
}

fn parse_rocm_gpu_csv(stdout: &str) -> GpuInfo {
    let mut vram_free_mb = None;
    let mut fingerprint_fields: Vec<&str> = Vec::new();
    let mut free_column = None;
    let mut total_column = None;
    let mut used_column = None;
    for line in stdout.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let parts: Vec<&str> = trimmed.split(',').map(str::trim).collect();
        let is_data_row = parts.first().is_some_and(|first| {
            let first = first.to_ascii_lowercase();
            first.starts_with("gpu[") || first.starts_with("card")
        });
        if !is_data_row {
            // Header row: remember which columns hold the memory figures.
            for (index, part) in parts.iter().enumerate() {
                let label = part.to_ascii_lowercase();
                if label.contains("free memory") {
                    free_column.get_or_insert(index);
                } else if label.contains("used memory") {
                    used_column.get_or_insert(index);
                } else if label.contains("total memory") {
                    total_column.get_or_insert(index);
                }
            }
            continue;
        }
        if vram_free_mb.is_none() {
            vram_free_mb = rocm_free_memory_mb(&parts, free_column, total_column, used_column);
        }
        if fingerprint_fields.is_empty() {
            // Keep only stable fields. VRAM totals and live usage numbers
            // change between runs, and a fingerprint that drifts defeats the
            // batch-scaler profile keying it feeds.
            fingerprint_fields = parts
                .iter()
                .copied()
                .filter(|part| is_stable_fingerprint_field(part))
                .collect();
        }
    }
    GpuInfo {
        vram_free_mb,
        fingerprint: if fingerprint_fields.is_empty() {
            host_fingerprint(OnnxExecutionProvider::Rocm)
        } else {
            fingerprint_fields.join("|")
        },
    }
}

fn is_stable_fingerprint_field(part: &str) -> bool {
    let lower = part.to_ascii_lowercase();
    !part.is_empty()
        && part.parse::<u64>().is_err()
        && !lower.contains("memory")
        && !lower.contains("vram")
}

/// Free VRAM in MB from one `rocm-smi` data row. Some versions repeat the
/// labels inside each row, others put them only in the header, and some
/// report no free column at all; derive free = total - used then.
fn rocm_free_memory_mb(
    parts: &[&str],
    free_column: Option<usize>,
    total_column: Option<usize>,
    used_column: Option<usize>,
) -> Option<u64> {
    let row_label_value = |needle: &str| {
        parts
            .iter()
            .position(|part| part.to_ascii_lowercase().contains(needle))
            .and_then(|label_index| parts.get(label_index + 1))
            .and_then(|value| value.parse::<u64>().ok())
    };
    let column_value = |column: Option<usize>| {
        column
            .and_then(|index| parts.get(index))
            .and_then(|value| value.parse::<u64>().ok())
    };

    let free_bytes = row_label_value("free memory")
        .or_else(|| column_value(free_column))
        .or_else(|| {
            // u64 parsing rejects negatives; saturating_sub keeps a used >
            // total reading from wrapping.
            let total = row_label_value("total memory").or_else(|| column_value(total_column))?;
            let used = row_label_value("used memory").or_else(|| column_value(used_column))?;
            Some(total.saturating_sub(used))
        })?;
    Some(free_bytes / (1024 * 1024))
}

fn host_fingerprint(ep: OnnxExecutionProvider) -> String {
    let host = std::env::var("HOSTNAME")
        .ok()
        .filter(|value| !value.trim().is_empty())
        .or_else(|| {
            std::process::Command::new("hostname")
                .output()
                .ok()
                .filter(|o| o.status.success())
                .and_then(|o| {
                    let s = String::from_utf8_lossy(&o.stdout).trim().to_string();
                    (!s.is_empty()).then_some(s)
                })
        })
        .unwrap_or_else(|| "unknown-host".to_string());
    format!(
        "{host}|os={}|arch={}|backend={ep}",
        std::env::consts::OS,
        std::env::consts::ARCH
    )
}

/// Resolve the effective inference backend from a CLI flag or environment.
pub fn resolve_backend(backend: Option<InferenceBackend>) -> InferenceBackend {
    if let Some(b) = backend {
        return b;
    }
    if let Some(b) = backend_from_env() {
        return b;
    }
    // Legacy: VERA_LOCAL=1 maps to onnx-jina-cpu
    if is_local_mode() {
        return InferenceBackend::OnnxJina(OnnxExecutionProvider::Cpu);
    }
    InferenceBackend::Api
}

/// Check whether two model names refer to the same model, using configured
/// alias groups plus aliases supplied by `VERA_EMBEDDING_MODEL_ALIASES`.
///
/// Model names may differ only by an org/repo prefix (e.g.
/// `"jinaai/jina-embeddings-v5-text-nano-retrieval"` vs
/// `"jina-embeddings-v5-text-nano-retrieval"`). Both names are normalised by
/// stripping everything up to and including the last `/` and then compared
/// case-insensitively.
pub fn model_names_match_with_aliases(a: &str, b: &str, aliases: &[Vec<String>]) -> bool {
    let a = normalize_model_name(a);
    let b = normalize_model_name(b);
    a == b || aliases_match(&a, &b, aliases) || aliases_match_env(&a, &b)
}

fn normalize_model_name(s: &str) -> String {
    s.rsplit('/')
        .next()
        .unwrap_or(s)
        .trim()
        .to_ascii_lowercase()
}

fn aliases_match(a: &str, b: &str, aliases: &[Vec<String>]) -> bool {
    aliases.iter().any(|group| {
        let mut has_a = false;
        let mut has_b = false;
        for alias in group {
            let normalized = normalize_model_name(alias);
            has_a |= normalized == a;
            has_b |= normalized == b;
        }
        has_a && has_b
    })
}

fn aliases_match_env(a: &str, b: &str) -> bool {
    std::env::var("VERA_EMBEDDING_MODEL_ALIASES")
        .ok()
        .map(|value| aliases_match(a, b, &parse_model_alias_groups(&value)))
        .unwrap_or(false)
}

fn parse_model_alias_groups(value: &str) -> Vec<Vec<String>> {
    value
        .split(';')
        .filter_map(|group| {
            let aliases = group
                .split(',')
                .map(str::trim)
                .filter(|alias| !alias.is_empty())
                .map(ToOwned::to_owned)
                .collect::<Vec<_>>();
            (aliases.len() >= 2).then_some(aliases)
        })
        .collect()
}

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

    #[test]
    fn default_config_is_valid() {
        let config = VeraConfig::default();
        assert!(config.indexing.max_chunk_lines > 0);
        assert!(config.retrieval.default_limit > 0);
        assert!(config.retrieval.rrf_k > 0.0);
        assert!(config.embedding.batch_size > 0);
        assert!(config.embedding.max_in_flight_inputs > 0);
        let (batch_size, concurrency) = config.embedding.bounded_parallelism();
        assert!(
            batch_size * concurrency <= config.embedding.max_in_flight_inputs,
            "default embedding parallelism must respect its in-flight bound"
        );
    }

    #[test]
    fn graph_augmentation_env_accepts_only_truthy_values() {
        for value in ["1", "true", "TRUE", "yes", "YeS"] {
            run_env_test(
                "config::tests::graph_augmentation_truthy_probe",
                &[("VERA_GRAPH_AUGMENT", Some(value))],
            );
        }

        for value in ["0", "false", "no", "", "on"] {
            run_env_test(
                "config::tests::graph_augmentation_falsey_probe",
                &[("VERA_GRAPH_AUGMENT", Some(value))],
            );
        }
    }

    #[test]
    #[ignore = "driven by graph_augmentation_env_accepts_only_truthy_values"]
    fn graph_augmentation_truthy_probe() {
        let value = std::env::var("VERA_GRAPH_AUGMENT").unwrap();
        assert!(
            graph_augmentation_enabled(),
            "{value} should enable the flag"
        );
    }

    #[test]
    #[ignore = "driven by graph_augmentation_env_accepts_only_truthy_values"]
    fn graph_augmentation_falsey_probe() {
        let value = std::env::var("VERA_GRAPH_AUGMENT").unwrap();
        assert!(
            !graph_augmentation_enabled(),
            "{value} should disable the flag"
        );
    }

    #[test]
    fn embedding_parallelism_clamps_batch_and_concurrency() {
        let config = EmbeddingConfig {
            batch_size: 128,
            max_concurrent_requests: 8,
            max_in_flight_inputs: 16,
            ..EmbeddingConfig::default()
        };

        assert_eq!(config.bounded_parallelism(), (16, 1));
    }

    #[test]
    fn embedding_parallelism_normalizes_zero_values_to_one() {
        let config = EmbeddingConfig {
            batch_size: 0,
            max_concurrent_requests: 0,
            max_in_flight_inputs: 0,
            ..EmbeddingConfig::default()
        };

        assert_eq!(config.bounded_parallelism(), (1, 1));
    }

    #[test]
    fn max_in_flight_environment_value_normalizes_zero_to_one() {
        run_env_test(
            "config::tests::max_in_flight_environment_value_normalizes_zero_to_one_probe",
            &[("VERA_MAX_IN_FLIGHT_INPUTS", Some("0"))],
        );
    }

    #[test]
    #[ignore = "driven by max_in_flight_environment_value_normalizes_zero_to_one"]
    fn max_in_flight_environment_value_normalizes_zero_to_one_probe() {
        assert_eq!(default_max_in_flight_inputs(), 1);
    }

    #[test]
    fn invalid_numeric_environment_value_falls_back_to_default() {
        run_env_test(
            "config::tests::invalid_numeric_environment_value_falls_back_to_default_probe",
            &[("VERA_MAX_CHUNK_BYTES", Some("24_576"))],
        );
    }

    #[test]
    #[ignore = "driven by invalid_numeric_environment_value_falls_back_to_default"]
    fn invalid_numeric_environment_value_falls_back_to_default_probe() {
        assert_eq!(default_max_chunk_bytes(), 24_576);
    }

    #[test]
    fn rocm_csv_parses_gpu_rows_and_free_memory_column() {
        let csv = "GPU, vram total used memory (bytes), vram total free memory (bytes)\n\
GPU[0], vram total used memory (bytes), 4294967296, vram total free memory (bytes), 12884901888\n\
GPU[1], vram total used memory (bytes), 1073741824, vram total free memory (bytes), 3221225472\n";

        let info = parse_rocm_gpu_csv(csv);

        assert_eq!(info.vram_free_mb, Some(12_288));
        assert!(info.fingerprint.contains("GPU[0]"));
    }

    #[test]
    fn rocm_csv_uses_free_memory_header_for_numeric_rows() {
        let csv = "GPU, vram total used memory (bytes), vram total free memory (bytes)\n\
GPU[0], 4294967296, 12884901888\n\
GPU[1], 1073741824, 3221225472\n";

        let info = parse_rocm_gpu_csv(csv);

        assert_eq!(info.vram_free_mb, Some(12_288));
        assert!(info.fingerprint.contains("GPU[0]"));
    }

    #[test]
    fn rocm_csv_derives_free_from_total_minus_used_for_card_rows() {
        // Newer rocm-smi CSV layout: card-prefixed rows, no free column, and
        // product-name columns from --showproductname in the same table.
        let csv = "device, VRAM Total Memory (B), VRAM Total Used Memory (B), Card series\n\
card0, 17179869184, 4294967296, AMD Radeon RX 7900 XTX\n";

        let info = parse_rocm_gpu_csv(csv);

        assert_eq!(info.vram_free_mb, Some(12_288));
        assert!(info.fingerprint.contains("card0"));
        assert!(info.fingerprint.contains("AMD Radeon RX 7900 XTX"));
    }

    #[test]
    fn rocm_fingerprint_ignores_live_memory_values() {
        let header = "device, VRAM Total Memory (B), VRAM Total Used Memory (B), Card series\n";
        let idle = format!("{header}card0, 17179869184, 1073741824, AMD Radeon\n");
        let busy = format!("{header}card0, 17179869184, 16106127360, AMD Radeon\n");

        assert_eq!(
            parse_rocm_gpu_csv(&idle).fingerprint,
            parse_rocm_gpu_csv(&busy).fingerprint,
            "live VRAM usage must not leak into the device fingerprint"
        );
        assert_eq!(parse_rocm_gpu_csv(&idle).vram_free_mb, Some(15_360));
        assert_eq!(parse_rocm_gpu_csv(&busy).vram_free_mb, Some(1_024));
    }

    #[test]
    fn rocm_csv_saturates_when_used_exceeds_total() {
        let csv = "device, VRAM Total Memory (B), VRAM Total Used Memory (B)\n\
card0, 1073741824, 4294967296\n";

        assert_eq!(parse_rocm_gpu_csv(csv).vram_free_mb, Some(0));
    }

    #[test]
    fn rocm_csv_without_memory_data_reports_no_vram() {
        let csv = "device, Card series\ncard0, AMD Radeon\n";

        assert_eq!(parse_rocm_gpu_csv(csv).vram_free_mb, None);
    }

    #[test]
    fn config_serialization_round_trip() {
        let config = VeraConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: VeraConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(
            deserialized.indexing.max_chunk_lines,
            config.indexing.max_chunk_lines
        );
        assert_eq!(
            deserialized.retrieval.default_limit,
            config.retrieval.default_limit
        );
    }

    #[test]
    fn openvino_backend_round_trip() {
        let backend = InferenceBackend::from_str("onnx-jina-openvino").unwrap();
        assert_eq!(
            backend,
            InferenceBackend::OnnxJina(OnnxExecutionProvider::OpenVino)
        );
        assert_eq!(backend.to_string(), "onnx-jina-openvino");
        assert!(backend.is_local());
    }

    #[test]
    fn potion_code_backend_round_trip() {
        let backend = InferenceBackend::from_str("potion-code-cpu").unwrap();
        assert_eq!(backend, InferenceBackend::PotionCode);
        assert_eq!(backend.to_string(), "potion-code-cpu");
        assert!(backend.is_local());
        assert!(!backend.is_onnx());
        assert_eq!(backend.execution_provider(), None);
    }

    #[test]
    fn default_excludes_contains_common_dirs() {
        let config = IndexingConfig::default();
        assert!(config.default_excludes.contains(&".git".to_string()));
        assert!(
            config
                .default_excludes
                .contains(&"node_modules".to_string())
        );
        assert!(config.default_excludes.contains(&"target".to_string()));
    }

    #[test]
    fn resolve_backend_prefers_saved_backend_env() {
        run_env_test(
            "config::tests::resolve_backend_prefers_saved_backend_env_probe",
            &[
                ("VERA_BACKEND", Some("onnx-jina-cuda")),
                ("VERA_LOCAL", Some("1")),
            ],
        );
    }

    #[test]
    #[ignore = "driven by resolve_backend_prefers_saved_backend_env"]
    fn resolve_backend_prefers_saved_backend_env_probe() {
        assert_eq!(
            resolve_backend(None),
            InferenceBackend::OnnxJina(OnnxExecutionProvider::Cuda)
        );
    }

    #[test]
    fn resolve_backend_falls_back_to_legacy_local_env() {
        run_env_test(
            "config::tests::resolve_backend_falls_back_to_legacy_local_env_probe",
            &[("VERA_BACKEND", None), ("VERA_LOCAL", Some("1"))],
        );
    }

    #[test]
    #[ignore = "driven by resolve_backend_falls_back_to_legacy_local_env"]
    fn resolve_backend_falls_back_to_legacy_local_env_probe() {
        assert_eq!(
            resolve_backend(None),
            InferenceBackend::OnnxJina(OnnxExecutionProvider::Cpu)
        );
    }

    /// Shorthand for matching without configured alias groups.
    fn model_names_match(a: &str, b: &str) -> bool {
        model_names_match_with_aliases(a, b, &[])
    }

    #[test]
    fn model_names_match_exact() {
        assert!(model_names_match(
            "jina-embeddings-v5",
            "jina-embeddings-v5"
        ));
    }

    #[test]
    fn model_names_match_with_org_prefix() {
        assert!(model_names_match(
            "jinaai/jina-embeddings-v5-text-nano-retrieval",
            "jina-embeddings-v5-text-nano-retrieval"
        ));
    }

    #[test]
    fn model_names_match_case_insensitive() {
        assert!(model_names_match(
            "Jina-Embeddings-V5",
            "jina-embeddings-v5"
        ));
    }

    #[test]
    fn model_names_match_different_models() {
        assert!(!model_names_match("jina-embeddings-v5", "jina-reranker-v2"));
    }

    #[test]
    fn model_names_match_configured_alias_group() {
        let aliases = vec![vec![
            "text-embedding-3-large".to_string(),
            "text-embedding-3-large-2".to_string(),
        ]];

        assert!(model_names_match_with_aliases(
            "text-embedding-3-large",
            "text-embedding-3-large-2",
            &aliases
        ));
        assert!(!model_names_match_with_aliases(
            "text-embedding-3-large",
            "text-embedding-3-small",
            &aliases
        ));
    }

    #[test]
    fn model_names_match_env_alias_group() {
        run_env_test(
            "config::tests::model_names_match_env_alias_group_probe",
            &[(
                "VERA_EMBEDDING_MODEL_ALIASES",
                Some("text-embedding-3-large,text-embedding-3-large-2;other,other-prod"),
            )],
        );
    }

    #[test]
    #[ignore = "driven by model_names_match_env_alias_group"]
    fn model_names_match_env_alias_group_probe() {
        assert!(model_names_match(
            "text-embedding-3-large",
            "text-embedding-3-large-2"
        ));
        assert!(model_names_match("other", "other-prod"));
        assert!(!model_names_match(
            "text-embedding-3-large",
            "text-embedding-3-small"
        ));
    }

    #[test]
    fn model_alias_groups_ignore_single_entry_groups() {
        assert!(parse_model_alias_groups("solo;").is_empty());
        assert_eq!(
            parse_model_alias_groups("a,b; c , d").len(),
            2,
            "whitespace-tolerant groups parse"
        );
    }
}