magellan 4.9.1

Deterministic codebase mapping tool for local development
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
use anyhow::Result;

pub trait TextEmbedder: Send + Sync {
    fn embed(&self, text: &str) -> Result<Vec<f32>>;
    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
        texts.iter().map(|t| self.embed(t)).collect()
    }
    fn dimension(&self) -> usize;
    fn name(&self) -> &str;
}

pub struct HashEmbedder {
    dim: usize,
}

impl HashEmbedder {
    pub fn new(dim: usize) -> Self {
        Self { dim }
    }
}

impl TextEmbedder for HashEmbedder {
    fn embed(&self, text: &str) -> Result<Vec<f32>> {
        let mut vec = vec![0.0f32; self.dim];
        let lower = text.to_ascii_lowercase();
        for (i, token) in lower
            .split(|c: char| !c.is_alphanumeric())
            .filter(|t| !t.is_empty())
            .enumerate()
        {
            let hash = xxhash_rust::xxh3::xxh3_64(token.as_bytes());
            let slot = (hash as usize) % self.dim;
            let idx = (i + slot) % self.dim;
            vec[idx] += 1.0;
        }
        let norm: f32 = vec.iter().map(|v| v * v).sum::<f32>().sqrt();
        if norm > 0.0 {
            for v in &mut vec {
                *v /= norm;
            }
        }
        Ok(vec)
    }

    fn dimension(&self) -> usize {
        self.dim
    }

    fn name(&self) -> &str {
        "hash"
    }
}

pub struct OllamaEmbedder {
    base_url: String,
    model: String,
    dim: std::sync::atomic::AtomicUsize,
    num_ctx: usize,
}

impl OllamaEmbedder {
    pub fn new(base_url: &str, model: &str, num_ctx: usize) -> Self {
        Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            model: model.to_string(),
            dim: std::sync::atomic::AtomicUsize::new(0),
            num_ctx,
        }
    }
}

impl TextEmbedder for OllamaEmbedder {
    fn embed(&self, text: &str) -> Result<Vec<f32>> {
        let results = self.embed_batch(&[text])?;
        results
            .into_iter()
            .next()
            .ok_or_else(|| anyhow::anyhow!("ollama embed: no result returned"))
    }

    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
        let url = format!("{}/api/embed", self.base_url);
        let mut body = serde_json::json!({
            "model": self.model,
            "input": texts,
        });
        if self.num_ctx > 0 {
            body["options"] = serde_json::json!({ "num_ctx": self.num_ctx });
        }
        let response_body = http_post_json(&url, &body, "")?;
        // Ollama format: {"embeddings": [[...], ...]}
        let embedding_arrays = response_body
            .get("embeddings")
            .and_then(|v| v.as_array())
            .ok_or_else(|| anyhow::anyhow!("ollama embed: no embeddings array in response"))?;
        let refs: Vec<&serde_json::Value> = embedding_arrays.iter().collect();
        parse_vector_arrays(&refs, "ollama")
    }

    fn dimension(&self) -> usize {
        auto_detect_dim(&self.dim, || self.embed_batch(&["x"]).ok())
    }

    fn name(&self) -> &str {
        "ollama"
    }
}

/// OpenAI-compatible embedding endpoint.
///
/// Works with: llama.cpp server (`/v1/embeddings`), vLLM, any OpenAI-compatible API.
/// Request: `POST /v1/embeddings` with `{"model": "...", "input": [...]}`
/// Response: `{"data": [{"embedding": [...]}, ...]}`
pub struct OpenAICompatEmbedder {
    base_url: String,
    model: String,
    api_key: String,
    dim: std::sync::atomic::AtomicUsize,
}

impl OpenAICompatEmbedder {
    pub fn new(base_url: &str, model: &str, api_key: &str) -> Self {
        Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            model: model.to_string(),
            api_key: api_key.to_string(),
            dim: std::sync::atomic::AtomicUsize::new(0),
        }
    }
}

impl TextEmbedder for OpenAICompatEmbedder {
    fn embed(&self, text: &str) -> Result<Vec<f32>> {
        let results = self.embed_batch(&[text])?;
        results
            .into_iter()
            .next()
            .ok_or_else(|| anyhow::anyhow!("openai-compat embed: no result returned"))
    }

    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
        let url = format!("{}/v1/embeddings", self.base_url);
        let body = serde_json::json!({
            "model": self.model,
            "input": texts,
        });
        let response_body = http_post_json(&url, &body, &self.api_key)?;
        // OpenAI format: {"data": [{"embedding": [...]}, ...]}
        let data_array = response_body
            .get("data")
            .and_then(|v| v.as_array())
            .ok_or_else(|| anyhow::anyhow!("openai-compat embed: no data array in response"))?;
        let embedding_arrays: Vec<&serde_json::Value> = data_array
            .iter()
            .filter_map(|item| item.get("embedding"))
            .collect();
        if embedding_arrays.len() != texts.len() {
            return Err(anyhow::anyhow!(
                "openai-compat embed: expected {} embeddings, got {}",
                texts.len(),
                embedding_arrays.len()
            ));
        }
        parse_vector_arrays(&embedding_arrays, "openai-compat")
    }

    fn dimension(&self) -> usize {
        auto_detect_dim(&self.dim, || self.embed_batch(&["x"]).ok())
    }

    fn name(&self) -> &str {
        "openai-compat"
    }
}

// ── Shared helpers ──

fn http_post_json(url: &str, body: &serde_json::Value, api_key: &str) -> Result<serde_json::Value> {
    let mut req = ureq::post(url)
        .config()
        .timeout_global(Some(std::time::Duration::from_secs(120)))
        .build()
        .header("Content-Type", "application/json");
    if !api_key.is_empty() {
        req = req.header("Authorization", &format!("Bearer {}", api_key));
    }
    let mut response = req
        .send_json(body)
        .map_err(|e| anyhow::anyhow!("embed request failed: {}", e))?;
    let body_text = response
        .body_mut()
        .read_to_string()
        .map_err(|e| anyhow::anyhow!("embed response read failed: {}", e))?;
    serde_json::from_str(&body_text)
        .map_err(|e| anyhow::anyhow!("embed response parse failed: {}", e))
}

fn parse_vector_arrays(
    arrays: &[&serde_json::Value],
    provider_name: &str,
) -> Result<Vec<Vec<f32>>> {
    let mut results = Vec::with_capacity(arrays.len());
    for arr in arrays {
        let vec: Vec<f32> = arr
            .as_array()
            .map(|a| {
                a.iter()
                    .filter_map(|v| v.as_f64().map(|f| f as f32))
                    .collect()
            })
            .unwrap_or_default();
        if vec.is_empty() {
            return Err(anyhow::anyhow!(
                "{}: empty embedding vector in batch",
                provider_name
            ));
        }
        results.push(vec);
    }
    Ok(results)
}

fn auto_detect_dim<F>(dim: &std::sync::atomic::AtomicUsize, probe: F) -> usize
where
    F: FnOnce() -> Option<Vec<Vec<f32>>>,
{
    use std::sync::atomic::Ordering;

    let d = dim.load(Ordering::Relaxed);
    if d > 0 {
        return d;
    }
    if let Some(vectors) = probe() {
        if let Some(first) = vectors.first() {
            let detected = first.len();
            dim.store(detected, Ordering::Relaxed);
            return detected;
        }
    }
    // Fallback: nomic-embed-text default
    let fallback = 768;
    dim.store(fallback, Ordering::Relaxed);
    fallback
}

pub fn create_embedder(
    provider: &crate::config::EmbedProvider,
    enabled: bool,
    base_url: &str,
    model: &str,
    api_key: &str,
    num_ctx: usize,
) -> Box<dyn TextEmbedder> {
    if !enabled {
        return Box::new(HashEmbedder::new(128));
    }
    match provider {
        crate::config::EmbedProvider::Ollama => {
            Box::new(OllamaEmbedder::new(base_url, model, num_ctx))
        }
        crate::config::EmbedProvider::OpenAi => {
            Box::new(OpenAICompatEmbedder::new(base_url, model, api_key))
        }
        crate::config::EmbedProvider::Hash => Box::new(HashEmbedder::new(128)),
    }
}

pub fn symbol_embed_text(entity: &sqlitegraph::GraphEntity, body: Option<&str>) -> String {
    let mut parts = vec![entity.kind.clone(), entity.name.clone()];
    for key in &[
        "fqn",
        "canonical_fqn",
        "display_fqn",
        "file_path",
        "kind_normalized",
    ] {
        if let Some(value) = entity.data.get(key).and_then(|v| v.as_str()) {
            parts.push(value.to_string());
        }
    }
    if let Some(lang) = entity.data.get("language").and_then(|v| v.as_str()) {
        parts.push(lang.to_string());
    }
    if let Some(body) = body {
        let truncated = if body.len() > EMBED_BODY_MAX_CHARS {
            let mut end = EMBED_BODY_MAX_CHARS;
            while !body.is_char_boundary(end) && end > 0 {
                end -= 1;
            }
            &body[..end]
        } else {
            body
        };
        parts.push(truncated.to_string());
    }
    parts.join(" ")
}

/// Maximum source body length included in embedding text.
/// Prevents extremely large function bodies from dominating the embedding.
const EMBED_BODY_MAX_CHARS: usize = 1024;

pub fn symbol_fact_embed_text(
    name: &Option<String>,
    file_path: &str,
    kind_normalized: &str,
    body: Option<&str>,
) -> String {
    let mut parts = vec!["Symbol".to_string()];
    if let Some(name) = name {
        parts.push(name.clone());
    }
    parts.push(file_path.to_string());
    parts.push(kind_normalized.to_string());
    if let Some(body) = body {
        let truncated = if body.len() > EMBED_BODY_MAX_CHARS {
            // Find the last char boundary at or before the limit
            let mut end = EMBED_BODY_MAX_CHARS;
            while !body.is_char_boundary(end) && end > 0 {
                end -= 1;
            }
            &body[..end]
        } else {
            body
        };
        parts.push(truncated.to_string());
    }
    parts.join(" ")
}

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

    #[test]
    fn test_hash_embedder_dimension() {
        let embedder = HashEmbedder::new(128);
        assert_eq!(embedder.dimension(), 128);
    }

    #[test]
    fn test_hash_embedder_basic() {
        let embedder = HashEmbedder::new(128);
        let vec = embedder.embed("fn parse_rust").unwrap();
        assert_eq!(vec.len(), 128);
        let norm: f32 = vec.iter().map(|v| v * v).sum::<f32>().sqrt();
        assert!((norm - 1.0).abs() < 0.01, "should be unit vector");
    }

    #[test]
    fn test_hash_embedder_shared_tokens() {
        let embedder = HashEmbedder::new(128);
        let a = embedder.embed("fn parse_rust").unwrap();
        let b = embedder.embed("fn parse_python").unwrap();
        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        assert!(
            dot > 0.3,
            "shared 'fn' 'parse' tokens should give positive cosine, got {}",
            dot
        );
    }

    #[test]
    fn test_hash_embedder_no_shared_tokens() {
        let embedder = HashEmbedder::new(128);
        let a = embedder.embed("sync_claude_transcript").unwrap();
        let b = embedder.embed("process_file_operations").unwrap();
        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        assert!(
            dot < 0.1,
            "no shared tokens should give near-zero cosine, got {}",
            dot
        );
    }

    #[test]
    fn test_create_embedder_hash() {
        let embedder = create_embedder(&crate::config::EmbedProvider::Hash, false, "", "", "", 0);
        assert_eq!(embedder.name(), "hash");
        assert_eq!(embedder.dimension(), 128);
    }

    #[test]
    fn test_create_embedder_ollama() {
        let embedder = create_embedder(
            &crate::config::EmbedProvider::Ollama,
            true,
            "http://localhost:11434",
            "nomic-embed-text",
            "",
            0,
        );
        assert_eq!(embedder.name(), "ollama");
        // dimension is auto-detected on first call; without ollama running it
        // falls back to 768. Just verify it returns a positive value.
        assert!(embedder.dimension() > 0);
    }

    #[test]
    fn test_symbol_embed_text() {
        let entity = sqlitegraph::GraphEntity {
            id: 1,
            kind: "Symbol".to_string(),
            name: "parse_rust".to_string(),
            file_path: Some("src/lib.rs".to_string()),
            data: serde_json::json!({
                "fqn": "magellan::parse_rust",
                "kind_normalized": "function",
                "language": "rust",
            }),
        };
        let text = symbol_embed_text(&entity, None);
        assert!(text.contains("Symbol"));
        assert!(text.contains("parse_rust"));
        assert!(text.contains("magellan::parse_rust"));
        assert!(text.contains("function"));
        assert!(text.contains("rust"));
    }

    #[test]
    fn test_symbol_embed_text_with_body() {
        let entity = sqlitegraph::GraphEntity {
            id: 2,
            kind: "Symbol".to_string(),
            name: "my_function".to_string(),
            file_path: Some("src/lib.rs".to_string()),
            data: serde_json::json!({
                "fqn": "my_crate::my_function",
                "kind_normalized": "function",
                "language": "rust",
            }),
        };
        let text_no_body = symbol_embed_text(&entity, None);
        let text_with_body = symbol_embed_text(&entity, Some("fn my_function() -> i32 { 42 }"));

        assert!(
            !text_no_body.contains("fn my_function()"),
            "no-body should not contain source: {:?}",
            text_no_body
        );
        assert!(
            text_with_body.contains("fn my_function()"),
            "with-body should contain source: {:?}",
            text_with_body
        );
        assert!(
            text_with_body.contains("42"),
            "with-body should contain function body"
        );
        assert!(
            text_with_body.contains("my_crate::my_function"),
            "with-body should still have fqn"
        );
    }

    #[test]
    fn test_symbol_fact_embed_text_with_body() {
        let name = Some("compute_value".to_string());
        let text_no_body = symbol_fact_embed_text(&name, "src/calc.rs", "fn", None);
        let text_with_body = symbol_fact_embed_text(
            &name,
            "src/calc.rs",
            "fn",
            Some("fn compute_value(x: i32) -> i32 { x * 2 + 1 }"),
        );

        assert!(
            text_no_body.contains("Symbol"),
            "should start with Symbol marker"
        );
        assert!(
            text_no_body.contains("compute_value"),
            "should contain name"
        );
        assert!(
            !text_no_body.contains("x * 2"),
            "no-body should not contain source body"
        );

        assert!(
            text_with_body.contains("compute_value"),
            "with-body should contain name"
        );
        assert!(
            text_with_body.contains("fn compute_value"),
            "with-body should contain function signature"
        );
        assert!(
            text_with_body.contains("x * 2 + 1"),
            "with-body should contain function body expression"
        );
    }

    #[test]
    fn test_symbol_fact_embed_text_body_truncation() {
        let long_body = "fn big_fn() { ".to_string() + &"let x = 1; ".repeat(200) + "}";
        let name = Some("big_fn".to_string());
        let text = symbol_fact_embed_text(&name, "src/big.rs", "fn", Some(&long_body));

        assert!(
            text.contains("big_fn"),
            "should contain name even with truncated body"
        );
        // Body should be truncated to ~1024 chars, not the full ~1800 chars
        assert!(
            text.len() < 1200,
            "text should be under ~1200 chars with truncation, got {}",
            text.len()
        );
    }
}