claudix 0.2.0

Local semantic search plugin for Claude Code
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
//! Drives `bin/claudix-bootstrap.js` (via node) to verify `development_mode`
//! binary resolution, the cache-hit symlink relink, the warn-not-delete cargo
//! behavior, and that download failures are recorded to install.log.
#![cfg(unix)]

use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

fn bootstrap_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("bin")
        .join("claudix-bootstrap.js")
}

fn node_available() -> bool {
    Command::new("node")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

fn temp_dir() -> tempfile::TempDir {
    let temp = tempfile::tempdir();
    assert!(temp.is_ok());
    temp.ok().unwrap_or_else(|| unreachable!())
}

fn write_config(dir: &Path, body: &str) {
    let claude_dir = dir.join(".claude");
    assert!(fs::create_dir_all(&claude_dir).is_ok());
    assert!(fs::write(claude_dir.join("claudix.toml"), body).is_ok());
}

fn write_executable(path: &Path, body: &str) {
    if let Some(parent) = path.parent() {
        assert!(fs::create_dir_all(parent).is_ok());
    }
    assert!(fs::write(path, body).is_ok());
    let metadata = fs::metadata(path);
    assert!(metadata.is_ok());
    let mut perms = metadata
        .ok()
        .unwrap_or_else(|| unreachable!())
        .permissions();
    perms.set_mode(0o755);
    assert!(fs::set_permissions(path, perms).is_ok());
}

/// Run `node claudix-bootstrap.js --check-only` with an isolated HOME, project dir, and
/// CARGO_HOME so the real environment is never touched.
fn run_check_only(home: &Path, project_dir: &Path, cargo_home: &Path) -> Output {
    let output = Command::new("node")
        .arg(bootstrap_path())
        .arg("--check-only")
        .env("HOME", home)
        .env("CLAUDE_PROJECT_DIR", project_dir)
        .env("CARGO_HOME", cargo_home)
        .env("CLAUDIX_HOME", home.join("cache"))
        .env_remove("CLAUDE_PLUGIN_DATA")
        .output();
    assert!(output.is_ok());
    output.ok().unwrap_or_else(|| unreachable!())
}

fn stderr_of(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

fn stdout_of(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).into_owned()
}

fn write_manifest(plugin_root: &Path, version: &str) {
    let dir = plugin_root.join(".claude-plugin");
    assert!(fs::create_dir_all(&dir).is_ok());
    let body = format!(
        "{{\n  \"name\": \"claudix\",\n  \"version\": \"{}\"\n}}\n",
        version
    );
    assert!(fs::write(dir.join("plugin.json"), body).is_ok());
}

/// Run `node claudix-bootstrap.js --install` with an isolated HOME, plugin root, cargo
/// home, and symlink dir so the real environment is never touched. Forces a
/// deterministic platform so the cached-binary path is stable on any unix host.
fn run_install(
    home: &Path,
    project_dir: &Path,
    cargo_home: &Path,
    plugin_root: &Path,
    local_bin: &Path,
) -> Output {
    let output = Command::new("node")
        .arg(bootstrap_path())
        .arg("--install")
        .env("HOME", home)
        .env("CLAUDE_PROJECT_DIR", project_dir)
        .env("CARGO_HOME", cargo_home)
        .env("CLAUDE_PLUGIN_ROOT", plugin_root)
        .env("CLAUDE_PLUGIN_DATA", home.join("cache"))
        .env("CLAUDIX_LOCAL_BIN", local_bin)
        .env("CLAUDIX_PLATFORM_OVERRIDE", "linux-x86_64")
        .output();
    assert!(output.is_ok());
    output.ok().unwrap_or_else(|| unreachable!())
}

#[test]
fn development_mode_resolves_cargo_binary() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    assert!(fs::create_dir_all(&project).is_ok());

    write_config(&home, "development_mode = true\n");
    let cargo_bin = cargo_home.join("bin").join("claudix");
    write_executable(&cargo_bin, "#!/bin/sh\necho stub\n");

    let output = run_check_only(&home, &project, &cargo_home);
    assert!(
        output.status.success(),
        "expected success, stderr: {}",
        stderr_of(&output)
    );
    assert_eq!(stdout_of(&output).trim(), cargo_bin.to_string_lossy());
}

#[test]
fn development_mode_enabled_only_in_project_config() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    assert!(fs::create_dir_all(&project).is_ok());

    // No global config at all; dev mode comes solely from the project file.
    write_config(&project, "development_mode = true\n");
    let cargo_bin = cargo_home.join("bin").join("claudix");
    write_executable(&cargo_bin, "#!/bin/sh\necho stub\n");

    let output = run_check_only(&home, &project, &cargo_home);
    assert!(
        output.status.success(),
        "expected success, stderr: {}",
        stderr_of(&output)
    );
    assert_eq!(stdout_of(&output).trim(), cargo_bin.to_string_lossy());
}

#[test]
fn development_mode_missing_cargo_binary_exits_3() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    assert!(fs::create_dir_all(&project).is_ok());

    write_config(&home, "development_mode = true\n");

    let output = run_check_only(&home, &project, &cargo_home);
    assert_eq!(
        output.status.code(),
        Some(3),
        "expected exit 3 for missing cargo binary, stderr: {}",
        stderr_of(&output)
    );
    assert!(
        stderr_of(&output).contains("cargo install --path ."),
        "expected install hint in stderr"
    );
    assert!(stdout_of(&output).trim().is_empty());
}

#[test]
fn project_config_disables_global_development_mode() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    assert!(fs::create_dir_all(&project).is_ok());

    // Global turns it on, project turns it off: the dev branch must be skipped.
    write_config(&home, "development_mode = true\n");
    write_config(&project, "development_mode = false\n");
    write_executable(
        &cargo_home.join("bin").join("claudix"),
        "#!/bin/sh\necho stub\n",
    );

    let output = run_check_only(&home, &project, &cargo_home);
    // Dev branch skipped -> normal resolution finds no cached release -> exit 1,
    // never exit 3 (the dev-missing signal) and never the cargo path.
    assert_eq!(
        output.status.code(),
        Some(1),
        "expected normal cache-miss exit 1, stderr: {}",
        stderr_of(&output)
    );
    assert!(
        !stderr_of(&output).contains("cargo install --path ."),
        "dev branch leaked despite project override"
    );
}

#[test]
fn commented_development_mode_key_is_ignored() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    assert!(fs::create_dir_all(&project).is_ok());

    write_config(&home, "# development_mode = true\n");
    write_executable(
        &cargo_home.join("bin").join("claudix"),
        "#!/bin/sh\necho stub\n",
    );

    let output = run_check_only(&home, &project, &cargo_home);
    assert_eq!(
        output.status.code(),
        Some(1),
        "commented key should leave dev mode off -> normal cache-miss exit 1, stderr: {}",
        stderr_of(&output)
    );
    assert!(
        !stderr_of(&output).contains("cargo install --path ."),
        "commented key wrongly triggered the dev branch"
    );
}

#[test]
fn install_relinks_cached_binary_on_fast_path() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    let plugin_root = temp.path().join("plugin");
    let local_bin = temp.path().join("bin");
    assert!(fs::create_dir_all(&project).is_ok());

    write_manifest(&plugin_root, "0.1.6");

    // a cached release binary for the wanted version + platform — the fast-path
    // hit. `--install` must repoint the symlink even when nothing is downloaded.
    let cache_bin = home
        .join("cache")
        .join("bin")
        .join("claudix-v0.1.6-linux-x86_64");
    write_executable(&cache_bin, "#!/bin/sh\nexit 0\n");

    let output = run_install(&home, &project, &cargo_home, &plugin_root, &local_bin);
    assert!(
        output.status.success(),
        "expected success, stderr: {}",
        stderr_of(&output)
    );

    let target = fs::read_link(local_bin.join("claudix"));
    assert!(
        target.is_ok(),
        "expected ~/.local/bin/claudix symlink on cache-hit, stderr: {}",
        stderr_of(&output)
    );
    assert_eq!(
        target
            .ok()
            .unwrap_or_else(|| unreachable!())
            .to_string_lossy(),
        cache_bin.to_string_lossy()
    );
}

#[test]
fn install_leaves_intentional_cargo_binary_in_place() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo");
    let plugin_root = temp.path().join("plugin");
    let local_bin = temp.path().join("bin");
    assert!(fs::create_dir_all(&project).is_ok());

    write_manifest(&plugin_root, "0.1.6");

    // cached release binary for the wanted version — release wins on the fast path.
    let cache_bin = home
        .join("cache")
        .join("bin")
        .join("claudix-v0.1.6-linux-x86_64");
    write_executable(&cache_bin, "#!/bin/sh\nexit 0\n");

    // a cargo-installed claudix at a different version — intentionally installed,
    // must be left in place (warned, not deleted).
    let cargo_bin = cargo_home.join("bin").join("claudix");
    write_executable(
        &cargo_bin,
        "#!/bin/sh\n[ \"$1\" = \"-V\" ] && echo \"claudix 0.1.5\" || true\n",
    );

    let output = run_install(&home, &project, &cargo_home, &plugin_root, &local_bin);
    assert!(
        output.status.success(),
        "expected success, stderr: {}",
        stderr_of(&output)
    );

    assert!(
        cargo_bin.exists(),
        "cargo-installed claudix was deleted; stderr: {}",
        stderr_of(&output)
    );
    assert!(
        stderr_of(&output).contains("left in place"),
        "expected left-in-place warning, stderr: {}",
        stderr_of(&output)
    );
}

#[test]
fn install_log_records_error_on_failed_download() {
    if !node_available() {
        return;
    }
    let temp = temp_dir();
    let home = temp.path().join("home");
    let project = temp.path().join("project");
    let cargo_home = temp.path().join("cargo"); // empty -> no cargo fallback
    let plugin_root = temp.path().join("plugin");
    let local_bin = temp.path().join("bin");
    assert!(fs::create_dir_all(&project).is_ok());
    write_manifest(&plugin_root, "0.1.6");

    // point the release URL at a path nothing serves -> download fails. AU-1: the
    // failure must be recorded to install.log so /doctor can surface it instead of
    // leaving MCP silently dead across sessions.
    let output = Command::new("node")
        .arg(bootstrap_path())
        .arg("--install")
        .env("HOME", &home)
        .env("CLAUDE_PROJECT_DIR", &project)
        .env("CARGO_HOME", &cargo_home)
        .env("CLAUDE_PLUGIN_ROOT", &plugin_root)
        .env("CLAUDE_PLUGIN_DATA", home.join("cache"))
        .env("CLAUDIX_LOCAL_BIN", &local_bin)
        .env("CLAUDIX_PLATFORM_OVERRIDE", "linux-x86_64")
        .env(
            "CLAUDIX_RELEASE_BASE_URL",
            "file:///nonexistent/claudix-release",
        )
        .output();
    let output = output.expect("node spawn");
    assert!(
        !output.status.success(),
        "expected install to fail on dead URL, stderr: {}",
        stderr_of(&output)
    );

    let log = fs::read_to_string(home.join("cache").join("install.log"))
        .expect("install.log should exist after a failed download");
    assert!(
        log.lines().any(|l| l.starts_with("error:")),
        "expected an `error:` line in install.log, got: {}",
        log
    );
}

#[test]
fn bootstrap_source_never_corrupts_mcp_stdio() {
    // Static guard: the bootstrap spawns the native binary with stdio:'inherit' for MCP,
    // so it must never write to stdout (console.log) or read process.stdin itself —
    // either would corrupt the JSON-RPC handshake.
    let src = fs::read_to_string(bootstrap_path()).expect("bootstrap source readable");
    assert!(
        !src.contains("console.log"),
        "bootstrap writes to stdout via console.log (MCP stdio corruption risk)"
    );
    assert!(
        !src.contains("process.stdin."),
        "bootstrap reads process.stdin (MCP handshake corruption risk)"
    );
}