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
//! # Lens Core - High-Performance Code Search Engine
//!
//! A production-ready code search engine with:
//! - LSP-first architecture with real language server integration
//! - Zero-copy fused pipeline for ≤150ms p95 latency
//! - Bounded BFS for def/ref/type/impl traversal
//! - SLA-bounded metrics: Success@10, nDCG@10, SLA-Recall@50
//! - Built-in benchmarking and fraud-resistant attestation
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
//! │ gRPC API │────│ Query Router │────│ Search Engine │
//! └─────────────────┘ └─────────────────┘ └─────────────────┘
//! │ │ │
//! │ ┌─────────────────┐ │
//! │ │ LSP Manager │ │
//! │ └─────────────────┘ │
//! │ │ │
//! │ ┌─────────────────┐ │
//! │ │ Hint Cache │ │
//! │ └─────────────────┘ │
//! │ │
//! └─────────────────┬─────────────────────────────┘
//! │
//! ┌─────────────────┐
//! │ Attestation │
//! └─────────────────┘
//! ```
pub mod adversarial;
pub mod attestation;
pub mod baseline;
pub mod benchmark;
pub mod cache;
// Temporarily disabled calibration module due to complex dependencies
// pub mod calibration;
pub mod config;
pub mod grpc;
pub mod lang;
pub mod lsp;
pub mod metrics;
pub mod pipeline;
pub mod proto;
pub mod query;
pub mod search;
pub mod semantic;
pub mod server;
use anyhow::Result;
// Build-time information for anti-fraud
pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
/// Initialize the lens core with configuration and anti-fraud checks
pub async fn initialize() -> Result<()> {
// Initialize logging
tracing_subscriber::fmt()
.with_env_filter(
std::env::var("RUST_LOG")
.unwrap_or_else(|_| "lens_core=info,tower_lsp=debug".into()),
)
.init();
// Verify we're in real mode, not mock
if built_info::CFG_OS == "test" {
tracing::warn!("Running in test mode");
} else {
attestation::verify_real_mode()?;
}
// Initialize metrics system
if let Err(e) = metrics::init_prometheus_metrics() {
tracing::warn!("Failed to initialize Prometheus metrics: {}", e);
tracing::info!("Continuing without Prometheus metrics - basic metrics still available");
}
tracing::info!("Lens Core initialized");
tracing::info!("Git SHA: {}", built_info::GIT_VERSION.unwrap_or("unknown"));
tracing::info!("Build timestamp: {}", env!("BUILD_TIMESTAMP", "unknown"));
tracing::info!("Target: {} {}", built_info::CFG_TARGET_ARCH, built_info::CFG_OS);
Ok(())
}
/// Core application configuration
#[derive(Debug, Clone)]
pub struct LensConfig {
pub server_port: u16,
pub index_path: String,
pub lsp_enabled: bool,
pub cache_ttl_hours: u64,
pub max_results: usize,
pub performance_target_ms: u64,
pub attestation_enabled: bool,
// Dataset configuration for pinned golden datasets
pub dataset_path: String,
pub enable_pinned_datasets: bool,
pub default_dataset_version: Option<String>,
pub enable_corpus_validation: bool,
}
impl Default for LensConfig {
fn default() -> Self {
Self {
server_port: 50051,
index_path: "./index".to_string(),
lsp_enabled: true,
cache_ttl_hours: 24,
max_results: 50,
performance_target_ms: 150,
attestation_enabled: true,
// Default dataset configuration
dataset_path: "./pinned-datasets".to_string(),
enable_pinned_datasets: true,
default_dataset_version: Some(crate::benchmark::DEFAULT_PINNED_VERSION.to_string()),
enable_corpus_validation: true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
#[tokio::test]
async fn test_lens_config_default() {
let config = LensConfig::default();
assert_eq!(config.server_port, 50051);
assert_eq!(config.index_path, "./index");
assert!(config.lsp_enabled);
assert_eq!(config.cache_ttl_hours, 24);
assert_eq!(config.max_results, 50);
assert_eq!(config.performance_target_ms, 150);
assert!(config.attestation_enabled);
assert!(config.enable_pinned_datasets);
}
#[test]
fn test_lens_config_clone() {
let config1 = LensConfig::default();
let config2 = config1.clone();
assert_eq!(config1.server_port, config2.server_port);
assert_eq!(config1.index_path, config2.index_path);
}
#[tokio::test]
async fn test_initialize_in_test_mode() {
// Set test environment to avoid real mode verification
env::set_var("RUST_LOG", "debug");
// Initialize should not fail in test environment
let result = initialize().await;
// Should succeed or fail gracefully
match result {
Ok(_) => println!("Initialization successful"),
Err(e) => println!("Initialization failed as expected in test mode: {}", e),
}
}
#[test]
fn test_built_info_available() {
// Verify build info is accessible
assert!(built_info::PKG_NAME.len() > 0);
assert!(built_info::PKG_VERSION.len() > 0);
}
// Additional simple configuration and data structure tests
#[test]
fn test_lens_config_debug_format() {
let config = LensConfig::default();
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("LensConfig"));
assert!(debug_str.contains("50051"));
}
#[test]
fn test_lens_config_modification() {
let mut config = LensConfig::default();
config.server_port = 8080;
config.max_results = 100;
assert_eq!(config.server_port, 8080);
assert_eq!(config.max_results, 100);
// Other values should remain default
assert_eq!(config.cache_ttl_hours, 24);
assert!(config.lsp_enabled);
}
}