opencodesearch 0.1.4

Largescale MCP server for codebase search with background indexing and automatic updating to git commits in rust
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
use opencodesearch::config::AppConfig;
use opencodesearch::indexing::IndexingRuntime;
use opencodesearch::mcp::{OpenCodeSearchMcpServer, SearchRequest};
use rmcp::handler::server::wrapper::Parameters;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use std::time::{Duration, Instant};

fn repo_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

fn marker_dir() -> PathBuf {
    repo_root().join(".opencodesearch").join("test-markers")
}

fn init_marker_dir_once() -> anyhow::Result<()> {
    static INIT: OnceLock<()> = OnceLock::new();
    if INIT.get().is_none() {
        let dir = marker_dir();
        if dir.exists() {
            std::fs::remove_dir_all(&dir)?;
        }
        std::fs::create_dir_all(&dir)?;
        let _ = INIT.set(());
    }
    Ok(())
}

fn mark_ignored_test_done(test_name: &str) -> anyhow::Result<()> {
    let marker = marker_dir().join(format!("{test_name}.done"));
    let now = format!("{:?}", std::time::SystemTime::now());
    std::fs::write(marker, now)?;
    Ok(())
}

fn wait_for_ignored_tests_done() -> anyhow::Result<()> {
    // zzzz cleanup test must wait until all other ignored tests have completed.
    const REQUIRED_MARKERS: &[&str] = &[
        "a_connect_to_docker_ollama_and_run_cargo_test_flow",
        "b_connect_to_docker_quickwit_and_qdrant",
        "c_to_f_index_python_project_and_query_via_mcp_logic",
        "g_and_h_watchdog_handles_100_commit_refactor_updates",
        "index_moss_kernel_and_retrieve_code",
    ];

    let start = Instant::now();
    let timeout = Duration::from_secs(30 * 60);
    loop {
        let all_done = REQUIRED_MARKERS
            .iter()
            .all(|name| marker_dir().join(format!("{name}.done")).exists());
        if all_done {
            return Ok(());
        }
        if start.elapsed() > timeout {
            anyhow::bail!("timed out waiting for all ignored tests to complete");
        }
        std::thread::sleep(Duration::from_millis(300));
    }
}

fn docker_compose_up() -> anyhow::Result<()> {
    let output = match Command::new("docker")
        .args(["compose", "up", "-d"])
        .current_dir(repo_root())
        .output()
    {
        Ok(out) => out,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            anyhow::bail!("docker command not found")
        }
        Err(err) => return Err(err.into()),
    };

    if output.status.success() {
        return Ok(());
    }

    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    anyhow::bail!(
        "docker compose up failed\nstdout:\n{}\nstderr:\n{}",
        stdout,
        stderr
    )
}

fn should_skip_for_docker_error(err: &anyhow::Error) -> bool {
    let msg = err.to_string().to_ascii_lowercase();
    msg.contains("docker command not found")
        || msg.contains("permission denied")
        || msg.contains("cannot connect to the docker daemon")
        || msg.contains("while trying to connect to the docker api")
}

fn docker_compose_up_or_skip(test_name: &str) -> anyhow::Result<bool> {
    match docker_compose_up() {
        Ok(()) => Ok(true),
        Err(err) if should_skip_for_docker_error(&err) => {
            eprintln!("skipping {test_name}: {}", err);
            mark_ignored_test_done(test_name)?;
            Ok(false)
        }
        Err(err) => Err(err),
    }
}

async fn ensure_ollama_model_available_or_skip(
    test_name: &str,
    model_name: &str,
) -> anyhow::Result<bool> {
    let response = match reqwest::Client::new()
        .get("http://localhost:11434/api/tags")
        .send()
        .await
    {
        Ok(resp) => resp,
        Err(err) => {
            eprintln!("skipping {test_name}: failed to query ollama tags endpoint: {err}");
            mark_ignored_test_done(test_name)?;
            return Ok(false);
        }
    };

    if !response.status().is_success() {
        eprintln!(
            "skipping {test_name}: ollama tags endpoint unhealthy (status {})",
            response.status()
        );
        mark_ignored_test_done(test_name)?;
        return Ok(false);
    }

    let json: serde_json::Value = response.json().await?;
    let found = json
        .get("models")
        .and_then(|models| models.as_array())
        .map(|models| {
            models.iter().any(|m| {
                m.get("name")
                    .and_then(|name| name.as_str())
                    .map(|name| {
                        name == model_name || name.starts_with(&(model_name.to_string() + ":"))
                    })
                    .unwrap_or(false)
            })
        })
        .unwrap_or(false);

    if !found {
        eprintln!("skipping {test_name}: required ollama model '{model_name}' not present");
        mark_ignored_test_done(test_name)?;
        return Ok(false);
    }

    Ok(true)
}

fn write_test_config(codebase_dir: &Path) -> anyhow::Result<PathBuf> {
    let config_path = repo_root().join("config.test.json");
    let json = serde_json::json!({
        "codebase": {
            "directory_path": codebase_dir.display().to_string(),
            "git_branch": "main",
            "commit_threshold": 50,
            "mcp_server_name": "OpenCodeSearch Test Codebase",
            "mcp_server_url": "http://localhost:9443",
            "background_indexing_threads": 2
        },
        "ollama": {
            "server_url": "http://localhost:11434",
            "embedding_model": "qwen3-embedding:0.6b",
            "context_size": 2000
        },
        "qdrant": {
            "server_url": "http://localhost:6334",
            "collection_name": "opencodesearch-code-chunks",
            "api_key": null
        },
        "quickwit": {
            "quickwit_url": "http://localhost:7280",
            "quickwit_index_id": "opencodesearch-code-chunks"
        }
    });

    std::fs::write(&config_path, serde_json::to_vec_pretty(&json)?)?;
    Ok(config_path)
}

fn create_python_project_with_10_files(root: &Path) -> anyhow::Result<()> {
    std::fs::create_dir_all(root)?;

    for idx in 0..10 {
        let path = root.join(format!("module_{}.py", idx));
        let body = format!(
            "def transform_obj_{idx}(value):\n    obj = value\n    obj = obj + {idx}\n    return obj\n"
        );
        std::fs::write(path, body)?;
    }

    Ok(())
}

fn init_git_repo(path: &Path) -> anyhow::Result<()> {
    let run = |args: &[&str]| -> anyhow::Result<()> {
        let status = Command::new("git").args(args).current_dir(path).status()?;
        if !status.success() {
            anyhow::bail!("git {:?} failed", args);
        }
        Ok(())
    };

    run(&["init"])?;
    run(&["config", "user.email", "test@example.com"])?;
    run(&["config", "user.name", "test"])?;
    run(&["add", "."])?;
    run(&["commit", "-m", "initial"])?;
    Ok(())
}

#[tokio::test]
async fn parses_config_for_tests() {
    let tmp = tempfile::tempdir().expect("temp dir");
    let cfg_path = write_test_config(tmp.path()).expect("write config");
    let cfg = AppConfig::from_path(cfg_path).expect("parse config");
    assert_eq!(cfg.codebase.commit_threshold, 50);
}

#[tokio::test]
#[ignore = "requires docker compose + ollama model"]
async fn a_connect_to_docker_ollama_and_run_cargo_test_flow() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("a_connect_to_docker_ollama_and_run_cargo_test_flow")? {
        return Ok(());
    }

    let client = reqwest::Client::new();
    let response = client.get("http://localhost:11434/api/tags").send().await?;

    anyhow::ensure!(response.status().is_success(), "ollama endpoint unhealthy");
    mark_ignored_test_done("a_connect_to_docker_ollama_and_run_cargo_test_flow")?;
    Ok(())
}

#[tokio::test]
#[ignore = "requires docker compose"]
async fn b_connect_to_docker_quickwit_and_qdrant() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("b_connect_to_docker_quickwit_and_qdrant")? {
        return Ok(());
    }

    let mut quickwit_ok = false;
    for _ in 0..10 {
        let response = reqwest::get("http://localhost:7280/health/livez").await;
        if let Ok(resp) = response {
            if resp.status().is_success() {
                quickwit_ok = true;
                break;
            }
        }
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    }
    anyhow::ensure!(quickwit_ok, "quickwit health check failed");

    let qdrant_ok = reqwest::get("http://localhost:6333/healthz")
        .await?
        .status()
        .is_success();
    anyhow::ensure!(qdrant_ok, "qdrant health check failed");
    mark_ignored_test_done("b_connect_to_docker_quickwit_and_qdrant")?;

    Ok(())
}

#[tokio::test]
#[ignore = "requires docker compose, ollama model, and local git"]
async fn c_to_f_index_python_project_and_query_via_mcp_logic() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("c_to_f_index_python_project_and_query_via_mcp_logic")? {
        return Ok(());
    }
    if !ensure_ollama_model_available_or_skip(
        "c_to_f_index_python_project_and_query_via_mcp_logic",
        "qwen3-embedding:0.6b",
    )
    .await?
    {
        return Ok(());
    }

    let temp = tempfile::tempdir()?;
    create_python_project_with_10_files(temp.path())?;
    init_git_repo(temp.path())?;

    let cfg_path = write_test_config(temp.path())?;
    let config = AppConfig::from_path(cfg_path)?;
    let runtime = IndexingRuntime::from_config(config)?;

    // d) Index complete codebase using background indexing logic.
    runtime.index_entire_codebase().await?;

    // e) Query with non-exact phrasing through MCP server tool implementation.
    let mcp = OpenCodeSearchMcpServer::new(runtime);
    let payload = SearchRequest {
        query: "which method mutates the object value".to_string(),
        limit: Some(5),
    };

    let result = mcp.search_code(Parameters(payload)).await?;
    println!("c_to_f retrieved results:\n{:#?}", result.0.hits);
    anyhow::ensure!(
        result.0.hits.iter().any(|hit| hit.path.contains("module_")),
        "expected code retrieval results"
    );
    mark_ignored_test_done("c_to_f_index_python_project_and_query_via_mcp_logic")?;

    Ok(())
}

#[tokio::test]
#[ignore = "requires docker compose, ollama model, and git remotes"]
async fn g_and_h_watchdog_handles_100_commit_refactor_updates() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("g_and_h_watchdog_handles_100_commit_refactor_updates")? {
        return Ok(());
    }

    let temp = tempfile::tempdir()?;
    create_python_project_with_10_files(temp.path())?;
    init_git_repo(temp.path())?;

    // Produce 100 commits that refactor function names.
    for idx in 0..100 {
        let file = temp.path().join(format!("module_{}.py", idx % 10));
        let content = format!(
            "def refactor_obj_{idx}(value):\n    obj = value\n    obj = obj * 2\n    return obj\n"
        );
        std::fs::write(&file, content)?;

        let status_add = Command::new("git")
            .args(["add", "."])
            .current_dir(temp.path())
            .status()?;
        anyhow::ensure!(status_add.success(), "git add failed");

        let status_commit = Command::new("git")
            .args(["commit", "-m", &format!("refactor {}", idx)])
            .current_dir(temp.path())
            .status()?;
        anyhow::ensure!(status_commit.success(), "git commit failed");
    }

    // This test verifies commit creation and intended watchdog trigger condition.
    let count_out = Command::new("git")
        .args(["rev-list", "--count", "HEAD"])
        .current_dir(temp.path())
        .output()?;
    let count = String::from_utf8(count_out.stdout)?
        .trim()
        .parse::<usize>()?;

    anyhow::ensure!(count >= 101, "expected initial + 100 commits");
    mark_ignored_test_done("g_and_h_watchdog_handles_100_commit_refactor_updates")?;
    Ok(())
}

#[tokio::test]
#[ignore = "requires docker compose, ollama model, and cloned moss-kernel repository"]
async fn index_moss_kernel_and_retrieve_code() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("index_moss_kernel_and_retrieve_code")? {
        return Ok(());
    }
    if !ensure_ollama_model_available_or_skip(
        "index_moss_kernel_and_retrieve_code",
        "qwen3-embedding:0.6b",
    )
    .await?
    {
        return Ok(());
    }

    let moss_root = repo_root().join("examples").join("moss-kernel");
    if !moss_root.exists() {
        eprintln!("skipping index_moss_kernel_and_retrieve_code: examples/moss-kernel is missing");
        mark_ignored_test_done("index_moss_kernel_and_retrieve_code")?;
        return Ok(());
    }

    let cfg_path = write_test_config(&moss_root)?;
    let config = AppConfig::from_path(cfg_path)?;
    let runtime = IndexingRuntime::from_config(config)?;

    // Index the full cloned project.
    runtime.index_entire_codebase().await?;

    // Retrieve code through MCP search path.
    let mcp = OpenCodeSearchMcpServer::new(runtime);
    let payload = SearchRequest {
        query: "where is scheduler or task management implemented".to_string(),
        limit: Some(8),
    };
    let result = mcp.search_code(Parameters(payload)).await?;
    println!("index_moss_kernel retrieved results:\n{:#?}", result.0.hits);
    anyhow::ensure!(!result.0.hits.is_empty(), "no code results returned");
    anyhow::ensure!(
        result.0.hits.iter().all(|hit| !hit.path.is_empty()),
        "mcp retrieval returned malformed entries"
    );
    mark_ignored_test_done("index_moss_kernel_and_retrieve_code")?;
    Ok(())
}

#[tokio::test]
#[ignore = "requires docker compose; intended final cleanup test"]
async fn zzzz_delete_all_stored_code_from_qdrant_and_quickwit() -> anyhow::Result<()> {
    init_marker_dir_once()?;
    if !docker_compose_up_or_skip("zzzz_delete_all_stored_code_from_qdrant_and_quickwit")? {
        return Ok(());
    }

    // Force cleanup test to wait for all other ignored tests to complete.
    wait_for_ignored_tests_done()?;

    // Do not index in this test; only invoke delete-all API.
    let cfg_path = write_test_config(&repo_root())?;
    let config = AppConfig::from_path(cfg_path)?;
    let runtime = IndexingRuntime::from_config(config)?;

    // Run the new cleanup API.
    runtime.delete_all_stored_code().await?;
    Ok(())
}