leindex 2.1.1

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! HTTP `Asker` for Laya (`laya-serve`), TypeSafe Jev and OpenAI Decisions.
//! Single attempt per call, with one managed-service restart and one retry on
//! transport failure. The gate is latency-bounded and fails open.

use super::scrub::PathScrub;
use super::types::{Asker, Provider, Request, Response, SystemOneError};
use super::wire::{decode, encode};
use serde_json::Value;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

/// Default base URL per provider (Laya's `laya-serve` default port is 8000).
pub fn default_base_url(provider: Provider) -> &'static str {
    match provider {
        Provider::Laya => "http://127.0.0.1:8000",
        Provider::Jev => "https://api.typesafe.ai",
        Provider::OpenAi => "https://api.openai.com",
    }
}

/// Default model name per provider.
pub fn default_model(provider: Provider) -> &'static str {
    match provider {
        Provider::Laya => "laya",
        Provider::Jev => "jev-latest",
        Provider::OpenAi => "gpt-6-luna",
    }
}

fn endpoint_path(provider: Provider) -> &'static str {
    match provider {
        Provider::Laya | Provider::Jev => "/v1/systemone",
        Provider::OpenAi => "/v1/decisions",
    }
}

/// `reqwest`-backed provider client.
pub struct HttpAsker {
    client: reqwest::Client,
    provider: Provider,
    url: String,
    model: String,
    api_key: Option<String>,
    /// Managed service to (re)start when transport fails and retry once.
    /// Attached by the gate only for `[system_one] service` sections with
    /// `auto_start = true` and a model.
    managed: Option<(String, crate::config::ServiceConfig)>,
}

/// One managed-start attempt per process: a service that cannot come up
/// must not turn every gated call into a spawn attempt.
static MANAGED_START_ATTEMPTED: AtomicBool = AtomicBool::new(false);

impl HttpAsker {
    /// Build a client. `base_url` may omit the endpoint path.
    pub fn new(
        provider: Provider,
        base_url: &str,
        model: &str,
        api_key: Option<String>,
        timeout: Duration,
    ) -> Result<Self, SystemOneError> {
        let client = reqwest::Client::builder()
            .timeout(timeout)
            .build()
            .map_err(|e| SystemOneError::Transport(e.to_string()))?;
        Ok(Self {
            client,
            provider,
            url: format!(
                "{}{}",
                base_url.trim_end_matches('/'),
                endpoint_path(provider)
            ),
            model: model.to_string(),
            api_key,
            managed: None,
        })
    }

    /// Attach a managed service for transport-failure self-healing.
    pub fn with_managed_service(
        mut self,
        name: String,
        config: crate::config::ServiceConfig,
    ) -> Self {
        self.managed = Some((name, config));
        self
    }

    /// Endpoint URL (test/telemetry surface).
    pub fn endpoint(&self) -> &str {
        &self.url
    }

    /// Try to bring the managed service up (once per process). True when
    /// this call spawned it and a retry is worth attempting.
    async fn try_managed_restart(&self) -> bool {
        let Some((name, config)) = self.managed.clone() else {
            return false;
        };
        if MANAGED_START_ATTEMPTED.swap(true, Ordering::Relaxed) {
            return false;
        }
        tracing::warn!(service = %name, "transport failure; starting managed service once");
        let result =
            tokio::task::spawn_blocking(move || crate::services::ensure_running(&name, &config))
                .await;
        matches!(result, Ok(Ok(true)))
    }

    async fn ask_once(&self, request: &Request) -> Result<Response, SystemOneError> {
        let mut call =
            self.client
                .post(&self.url)
                .json(&encode(self.provider, &self.model, request));
        if let Some(key) = &self.api_key {
            call = call.bearer_auth(key);
        }
        let response = call
            .send()
            .await
            .map_err(|e| SystemOneError::Transport(e.to_string()))?;
        let status = response.status();
        if !status.is_success() {
            return Err(SystemOneError::Status(status.as_u16()));
        }
        let body: Value = response
            .json()
            .await
            .map_err(|e| SystemOneError::Decode(e.to_string()))?;
        decode(self.provider, request, &body)
    }
}

impl Asker for HttpAsker {
    async fn ask(&self, request: &Request) -> Result<Response, SystemOneError> {
        // Scrub every endpoint, including Laya: provider identity does not
        // prove a configured URL is local. The same aliases serve both attempts.
        let mut scrub = PathScrub::new(request);
        let scrubbed = scrub.scrub_request(request);
        let request = &scrubbed;
        let response = match self.ask_once(request).await {
            Err(SystemOneError::Transport(reason)) => {
                // Service-backed endpoint: one restart attempt, one retry.
                if self.try_managed_restart().await {
                    self.ask_once(request).await
                } else {
                    Err(SystemOneError::Transport(reason))
                }
            }
            other => other,
        }?;
        Ok(scrub.unscrub_response(response))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::cli::mcp::system_one::types::Question;
    use serde_json::json;
    use std::io::{Read, Write};

    /// Serve one canned HTTP response; returns the base URL and the captured
    /// request (headers + body) receiver. Pattern follows
    /// `remote_embed_test.rs::spawn_mock_embeddings_server`.
    fn mock_server(status: u16, body: String) -> (String, std::sync::mpsc::Receiver<String>) {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
        let addr = listener.local_addr().expect("addr").to_string();
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let (mut stream, _) = listener.accept().expect("accept");
            let mut buf = Vec::new();
            let mut chunk = [0u8; 4096];
            loop {
                let n = stream.read(&mut chunk).expect("read");
                buf.extend_from_slice(&chunk[..n]);
                let text = String::from_utf8_lossy(&buf);
                if let Some(split) = text.find("\r\n\r\n") {
                    let length = text[..split]
                        .lines()
                        .find_map(|l| {
                            l.to_ascii_lowercase()
                                .strip_prefix("content-length:")
                                .and_then(|v| v.trim().parse::<usize>().ok())
                        })
                        .unwrap_or(0);
                    if buf.len() >= split + 4 + length {
                        break;
                    }
                }
                if n == 0 {
                    break;
                }
            }
            tx.send(String::from_utf8_lossy(&buf).to_string()).ok();
            let reply = format!(
                "HTTP/1.1 {status} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
            stream.write_all(reply.as_bytes()).ok();
        });
        (format!("http://{addr}"), rx)
    }

    fn request() -> Request {
        Request {
            state: json!({"q": "x"}),
            questions: vec![(
                "ok".into(),
                Question::YesNo {
                    instructions: "Is it ok?".into(),
                },
            )],
        }
    }

    #[tokio::test]
    async fn test_posts_jev_shape_with_bearer_and_decodes() {
        let (base, rx) = mock_server(
            200,
            json!({"answers": {"ok": {"type": "noul", "noul": 0.9}},
                   "usage": {"input_tokens": 7, "output_tokens": 1}})
            .to_string(),
        );
        let asker = HttpAsker::new(
            Provider::Jev,
            &base,
            "jev-latest",
            Some("secret".into()),
            Duration::from_secs(2),
        )
        .unwrap();
        let response = asker.ask(&request()).await.unwrap();
        assert_eq!(response.p_yes("ok"), Some(0.9));
        assert_eq!(response.input_tokens, Some(7));
        let raw = rx.recv().unwrap();
        assert!(raw.starts_with("POST /v1/systemone "), "{raw}");
        assert!(
            raw.to_ascii_lowercase()
                .contains("authorization: bearer secret")
        );
        assert!(raw.contains("\"type\":\"noul\""));
    }

    #[tokio::test]
    async fn test_openai_uses_decisions_path() {
        let (base, rx) = mock_server(
            200,
            json!({"answers": [{"type": "predicate", "name": "ok", "probability": 0.2}]})
                .to_string(),
        );
        let asker = HttpAsker::new(
            Provider::OpenAi,
            &base,
            "gpt-6-luna",
            None,
            Duration::from_secs(2),
        )
        .unwrap();
        assert_eq!(asker.ask(&request()).await.unwrap().p_yes("ok"), Some(0.2));
        assert!(rx.recv().unwrap().starts_with("POST /v1/decisions "));
    }

    #[tokio::test]
    async fn test_non_success_status_is_an_error_not_a_panic() {
        let (base, _rx) = mock_server(429, "{}".into());
        let asker = HttpAsker::new(
            Provider::Jev,
            &base,
            "jev-latest",
            None,
            Duration::from_secs(2),
        )
        .unwrap();
        assert!(matches!(
            asker.ask(&request()).await,
            Err(SystemOneError::Status(429))
        ));
    }

    #[tokio::test]
    async fn test_unreachable_endpoint_is_a_transport_error() {
        let asker = HttpAsker::new(
            Provider::Laya,
            "http://127.0.0.1:1",
            "laya",
            None,
            Duration::from_millis(300),
        )
        .unwrap();
        assert!(matches!(
            asker.ask(&request()).await,
            Err(SystemOneError::Transport(_))
        ));
    }

    fn pick_request(state: Value, instructions: &str) -> Request {
        Request {
            state,
            questions: vec![(
                "dept".into(),
                Question::Pick {
                    instructions: instructions.into(),
                    options: vec![
                        (
                            "/home/alice/proj".into(),
                            "the project at /home/alice/proj".into(),
                        ),
                        ("keep".into(), "other project".into()),
                    ],
                },
            )],
        }
    }

    #[tokio::test]
    async fn test_remote_payload_excludes_absolute_paths() {
        let request = pick_request(
            json!({
                "unix": "/home/alice/proj/src/a.rs",
                "win": "C:\\Users\\bob\\app\\b.rs",
                "home": "~/code/c.rs",
                "outside": "/srv/data/d.rs"
            }),
            "Which project is /home/alice/proj?",
        );
        let (base, rx) = mock_server(200, json!({"answers": {}}).to_string());
        let asker = HttpAsker::new(
            Provider::Jev,
            &base,
            "jev-latest",
            None,
            Duration::from_secs(2),
        )
        .unwrap();
        asker.ask(&request).await.unwrap();
        let raw = rx.recv().unwrap();
        for leak in ["alice", "bob", "C:\\", "~/code", "/srv/data", "/home/"] {
            assert!(!raw.contains(leak), "leaked {leak}: {raw}");
        }
        assert!(raw.contains("pathref-"), "nothing was aliased: {raw}");
    }

    #[tokio::test]
    async fn test_pick_choice_alias_roundtrips_to_real_path() {
        let request = pick_request(json!({"note": "no paths"}), "Which project?");
        // The first path span in this request is the option value, so it is `pathref-1`.
        let (base, _rx) = mock_server(
            200,
            json!({"answers": {"dept": {
                "choice": "pathref-1",
                "probabilities": {"pathref-1": 0.9, "keep": 0.1},
                "confidence": 0.9
            }}})
            .to_string(),
        );
        let asker = HttpAsker::new(
            Provider::Jev,
            &base,
            "jev-latest",
            None,
            Duration::from_secs(2),
        )
        .unwrap();
        let response = asker.ask(&request).await.unwrap();
        assert_eq!(response.pick("dept"), Some(("/home/alice/proj", 0.9)));
    }

    #[tokio::test]
    async fn test_unknown_pick_choice_is_still_rejected() {
        let request = pick_request(json!({"note": "no paths"}), "Which project?");
        let (base, _rx) = mock_server(
            200,
            json!({"answers": {"dept": {
                "choice": "/home/alice/invented",
                "probabilities": {},
                "confidence": 0.9
            }}})
            .to_string(),
        );
        let asker = HttpAsker::new(
            Provider::Jev,
            &base,
            "jev-latest",
            None,
            Duration::from_secs(2),
        )
        .unwrap();
        assert_eq!(asker.ask(&request).await.unwrap().pick("dept"), None);
    }

    #[tokio::test]
    async fn test_laya_payload_also_excludes_absolute_paths() {
        let request = pick_request(json!({"path": "/home/alice/proj"}), "Which project?");
        let (base, rx) = mock_server(200, json!({"answers": {}}).to_string());
        let asker =
            HttpAsker::new(Provider::Laya, &base, "laya", None, Duration::from_secs(2)).unwrap();
        asker.ask(&request).await.unwrap();
        let raw = rx.recv().unwrap();
        assert!(
            !raw.contains("/home/alice/proj"),
            "Laya leaked a host path: {raw}"
        );
        assert!(raw.contains("pathref-"));
    }
}