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
// Model path resolution for the worker process
//
// VAL-CPHASE-010: Worker model resolution uses the documented precedence:
// 1. Explicit env override (LEINDEX_MODEL_PATH)
// 2. Bundled models near the binary
// 3. User cache fallback (~/.leindex/models/ or $LEINDEX_HOME/models/)
//
// The resolver is used by the worker runtime to locate model and tokenizer
// files without requiring the main daemon to pass paths explicitly.
use std::path::{Path, PathBuf};
/// Error during model path resolution.
#[derive(Debug, Clone)]
pub struct ModelResolutionError {
pub message: String,
}
impl std::fmt::Display for ModelResolutionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "model resolution error: {}", self.message)
}
}
impl std::error::Error for ModelResolutionError {}
/// Resolves model file paths using the documented precedence chain.
pub struct ModelResolver;
impl ModelResolver {
fn bundled_model_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
if let Ok(exe_path) = std::env::current_exe() {
if let Some(parent) = exe_path.parent() {
// 1. exe_parent/models (e.g., target/release/models)
dirs.push(parent.join("models"));
if let Some(grandparent) = parent.parent() {
// 2. exe_parent/../models (e.g., target/models)
dirs.push(grandparent.join("models"));
// VAL-ONNX-004: 3. exe_parent/../../models (e.g., workspace root models)
// When running from target/release/, models are at ../../models/
if let Some(great_grandparent) = grandparent.parent() {
dirs.push(great_grandparent.join("models"));
}
}
}
}
dirs
}
fn user_model_dirs() -> Vec<PathBuf> {
let mut dirs = Vec::new();
if let Some(configured) = crate::config::model_dir_path() {
dirs.push(configured);
}
if let Some(home) = dirs::home_dir() {
let default_home = home.join(".leindex").join("models");
if !dirs.iter().any(|dir| dir == &default_home) {
dirs.push(default_home);
}
}
dirs
}
/// Resolve the ONNX model file path for the given model name.
///
/// VAL-CPHASE-010: Uses the precedence chain:
/// 1. LEINDEX_MODEL_PATH env override
/// 2. Bundled models directory (relative to the worker binary)
/// 3. User cache directory (~/.leindex/models/ or $LEINDEX_HOME/models/)
pub fn resolve(model_name: &str) -> Result<PathBuf, ModelResolutionError> {
let model_filename = format!("{}.onnx", model_name);
// 1. Explicit env override
if let Ok(path) = std::env::var("LEINDEX_MODEL_PATH") {
let model_path = PathBuf::from(path).join(&model_filename);
if model_path.exists() {
tracing::debug!("model resolved via env override: {}", model_path.display());
return Ok(model_path);
}
}
// 2. Bundled models (relative to the running binary)
for bundled_dir in Self::bundled_model_dirs() {
let model_path = bundled_dir.join(&model_filename);
if model_path.exists() {
tracing::debug!("model resolved via bundled path: {}", model_path.display());
return Ok(model_path);
}
}
// 3. User cache fallback
for user_models in Self::user_model_dirs() {
let model_path = user_models.join(&model_filename);
if model_path.exists() {
tracing::debug!("model resolved via user cache: {}", model_path.display());
return Ok(model_path);
}
}
Err(ModelResolutionError {
message: format!(
"model '{}' not found in any standard location (env, bundled, user cache)",
model_name
),
})
}
/// Resolve the tokenizer file path for the given model name.
///
/// Uses the same precedence chain as model resolution.
pub fn resolve_tokenizer(model_name: &str) -> Result<PathBuf, ModelResolutionError> {
// Tokenizer is typically shared across model variants
let _ = model_name; // Model name may be used for variant-specific tokenizers in future
// 1. Explicit env override
if let Ok(path) = std::env::var("LEINDEX_MODEL_PATH") {
let tokenizer_path = PathBuf::from(path).join("tokenizer.json");
if tokenizer_path.exists() {
return Ok(tokenizer_path);
}
}
// 2. Bundled models
for bundled_dir in Self::bundled_model_dirs() {
let tokenizer_path = bundled_dir.join("tokenizer.json");
if tokenizer_path.exists() {
return Ok(tokenizer_path);
}
}
// 3. User cache fallback
for user_models in Self::user_model_dirs() {
let tokenizer_path = user_models.join("tokenizer.json");
if tokenizer_path.exists() {
return Ok(tokenizer_path);
}
}
Err(ModelResolutionError {
message: "tokenizer not found in any standard location".to_string(),
})
}
/// Determine the source of a resolved path for reporting.
///
/// Returns one of: "env_override", "bundled", "user_cache".
pub fn source_for_path(path: &Path) -> &'static str {
// Check env override first — if the env var is set and the path is
// rooted under it, report as env_override regardless of whether the
// file also happens to live near the binary.
if let Ok(env_path) = std::env::var("LEINDEX_MODEL_PATH") {
let env_dir = PathBuf::from(env_path);
if path.starts_with(&env_dir) {
return "env_override";
}
}
// Check if it's near the binary
if let Ok(exe_path) = std::env::current_exe() {
if let Some(parent) = exe_path.parent() {
if path.starts_with(parent) {
return "bundled";
}
}
}
"user_cache"
}
}
/// All tests in this module mutate the `LEINDEX_MODEL_PATH` env var and must
/// not run concurrently. We use a single test-serialising attribute so that
/// `cargo test -- --test-threads=N` still works correctly.
#[cfg(test)]
mod tests {
use super::*;
// Use the crate-level shared lock for env-var-mutating test serialization.
use crate::embed::test_util::ENV_TEST_LOCK;
#[test]
fn test_resolve_model_not_found() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Clear any env override
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var(crate::config::LEINDEX_HOME_ENV) };
let result = ModelResolver::resolve("nonexistent-model-xyz");
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.message.contains("nonexistent-model-xyz"));
assert!(err.message.contains("not found"));
}
#[test]
fn test_resolve_tokenizer_not_found() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var(crate::config::LEINDEX_HOME_ENV) };
// Use a model name guaranteed not to correspond to a real model. The
// user-cache fallback only triggers when `tokenizer.json` actually
// exists in `~/.leindex/models/`, so this test correctly fails on dev
// machines where that file is present. We therefore only assert the
// error path when the user cache does NOT have a tokenizer.
let user_has_tokenizer = dirs::home_dir()
.map(|h| {
h.join(".leindex")
.join("models")
.join("tokenizer.json")
.exists()
})
.unwrap_or(false);
if !user_has_tokenizer {
let result = ModelResolver::resolve_tokenizer("nonexistent");
assert!(result.is_err());
assert!(result.unwrap_err().message.contains("tokenizer not found"));
} else {
// On dev machines with `~/.leindex/models/tokenizer.json`, the
// user-cache fallback legitimately resolves, so skip the assertion.
// This is a pre-existing environment coupling, not a regression.
}
}
#[test]
fn test_resolve_with_env_override_missing_file() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Set env to a temp dir that doesn't have the model
let temp_dir = tempfile::tempdir().unwrap();
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::set_var("LEINDEX_MODEL_PATH", temp_dir.path()) };
let result = ModelResolver::resolve("test-model");
// Should still fail because the file doesn't exist in the temp dir
assert!(result.is_err());
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
}
#[test]
fn test_resolve_with_env_override_existing_file() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let temp_dir = tempfile::tempdir().unwrap();
let model_file = temp_dir.path().join("test-model.onnx");
std::fs::write(&model_file, b"fake model").unwrap();
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::set_var("LEINDEX_MODEL_PATH", temp_dir.path()) };
let result = ModelResolver::resolve("test-model");
assert!(result.is_ok());
assert_eq!(result.unwrap(), model_file);
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
}
#[test]
fn test_resolve_with_leindex_home_models() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
let temp_dir = tempfile::tempdir().unwrap();
let models_dir = temp_dir.path().join("models");
std::fs::create_dir_all(&models_dir).unwrap();
let model_file = models_dir.join("home-model.onnx");
std::fs::write(&model_file, b"fake model").unwrap();
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::set_var(crate::config::LEINDEX_HOME_ENV, temp_dir.path()) };
let result = ModelResolver::resolve("home-model");
assert_eq!(result.unwrap(), model_file);
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var(crate::config::LEINDEX_HOME_ENV) };
}
#[test]
fn test_resolve_tokenizer_with_env_override() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let temp_dir = tempfile::tempdir().unwrap();
let tokenizer_file = temp_dir.path().join("tokenizer.json");
std::fs::write(&tokenizer_file, b"{}").unwrap();
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::set_var("LEINDEX_MODEL_PATH", temp_dir.path()) };
let result = ModelResolver::resolve_tokenizer("test");
assert!(result.is_ok());
assert_eq!(result.unwrap(), tokenizer_file);
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var(crate::config::LEINDEX_HOME_ENV) };
}
#[test]
fn test_source_for_path_env_override() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let temp_dir = tempfile::tempdir().unwrap();
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::set_var("LEINDEX_MODEL_PATH", temp_dir.path()) };
let path = temp_dir.path().join("model.onnx");
assert_eq!(ModelResolver::source_for_path(&path), "env_override");
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
}
#[test]
fn test_source_for_path_user_cache() {
let _guard = ENV_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// FIXME: Audit that the environment access only happens in single-threaded code.
unsafe { std::env::remove_var("LEINDEX_MODEL_PATH") };
let path = PathBuf::from("/some/random/path/model.onnx");
assert_eq!(ModelResolver::source_for_path(&path), "user_cache");
}
#[test]
fn test_model_resolution_error_display() {
let err = ModelResolutionError {
message: "test error".to_string(),
};
assert!(err.to_string().contains("test error"));
}
}