krait-cli 0.1.2

Code intelligence CLI for AI agents
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
use std::collections::HashMap;
use std::fmt::Write;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use crate::detect::Language;

/// Primary config file name (inside .krait/ directory).
const CONFIG_FILE: &str = ".krait/krait.toml";

/// Legacy config location (project root, for backwards compat).
const LEGACY_CONFIG_FILE: &str = "krait.toml";

/// Parsed project configuration from `krait.toml`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectConfig {
    /// Project root override (relative to config file location).
    pub root: Option<String>,

    /// Workspace entries — each gets its own LSP server.
    #[serde(default)]
    pub workspace: Vec<WorkspaceEntry>,

    /// Per-language server overrides.
    #[serde(default)]
    pub servers: HashMap<String, ServerOverride>,

    /// Workspaces to pre-warm and exempt from LRU eviction.
    /// Paths are relative to the project root.
    #[serde(default)]
    pub primary_workspaces: Vec<String>,

    /// Maximum concurrent LSP sessions for LRU fallback (default: 10).
    pub max_active_sessions: Option<usize>,

    /// Maximum concurrent language server processes across all languages (default: unlimited).
    /// When exceeded, the least-recently-used language server is shut down.
    pub max_language_servers: Option<usize>,
}

/// One workspace scope for LSP indexing.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkspaceEntry {
    /// Relative path to workspace root (e.g., "packages/api").
    pub path: String,

    /// Language identifier (e.g., "typescript", "rust").
    pub language: String,

    /// Server binary override (e.g., "vtsls").
    pub server: Option<String>,
}

/// Override default server binary/args for a language.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerOverride {
    /// Binary name or path.
    pub binary: Option<String>,

    /// Command-line arguments.
    pub args: Option<Vec<String>>,
}

/// Where the config was loaded from.
#[derive(Debug, Clone)]
pub enum ConfigSource {
    /// Loaded from `.krait/krait.toml`.
    KraitToml,
    /// Loaded from legacy `krait.toml` at project root.
    LegacyKraitToml,
    /// No config file found — using auto-detection.
    AutoDetected,
}

impl ConfigSource {
    /// Human-readable label for status output.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Self::KraitToml => ".krait/krait.toml",
            Self::LegacyKraitToml => "krait.toml",
            Self::AutoDetected => "auto-detected",
        }
    }
}

/// Result of loading config: the config (if any) and its source.
#[derive(Debug, Clone)]
pub struct LoadedConfig {
    pub config: Option<ProjectConfig>,
    pub source: ConfigSource,
}

/// Try to load `.krait/krait.toml` (or legacy `krait.toml`) from the project root.
///
/// Returns `None` config with `AutoDetected` source if no config file exists.
#[must_use]
pub fn load(project_root: &Path) -> LoadedConfig {
    // Try .krait/krait.toml first
    let primary = project_root.join(CONFIG_FILE);
    if primary.is_file() {
        match load_from_file(&primary) {
            Ok(config) => {
                debug!("loaded config from {}", primary.display());
                return LoadedConfig {
                    config: Some(config),
                    source: ConfigSource::KraitToml,
                };
            }
            Err(e) => {
                warn!("failed to parse {}: {e}", primary.display());
            }
        }
    }

    // Try legacy krait.toml at project root
    let legacy = project_root.join(LEGACY_CONFIG_FILE);
    if legacy.is_file() {
        match load_from_file(&legacy) {
            Ok(config) => {
                debug!("loaded config from {} (legacy location)", legacy.display());
                return LoadedConfig {
                    config: Some(config),
                    source: ConfigSource::LegacyKraitToml,
                };
            }
            Err(e) => {
                warn!("failed to parse {}: {e}", legacy.display());
            }
        }
    }

    debug!("no config found, using auto-detection");
    LoadedConfig {
        config: None,
        source: ConfigSource::AutoDetected,
    }
}

fn load_from_file(path: &Path) -> anyhow::Result<ProjectConfig> {
    let content = std::fs::read_to_string(path)?;
    let config: ProjectConfig = toml::from_str(&content)?;
    Ok(config)
}

/// Convert a loaded config into `(Language, PathBuf)` pairs for the multiplexer.
///
/// Validates each entry: skips entries with unknown languages or missing directories.
#[must_use]
pub fn config_to_package_roots(
    config: &ProjectConfig,
    project_root: &Path,
) -> Vec<(Language, PathBuf)> {
    let mut roots = Vec::new();

    for entry in &config.workspace {
        let Some(lang) = parse_language(&entry.language) else {
            warn!(
                "unknown language '{}' in krait.toml, skipping workspace '{}'",
                entry.language, entry.path
            );
            continue;
        };

        let abs_path = project_root.join(&entry.path);
        if !abs_path.is_dir() {
            warn!("workspace path '{}' does not exist, skipping", entry.path);
            continue;
        }

        roots.push((lang, abs_path));
    }

    roots
}

/// Parse a language name string into a `Language` enum.
#[must_use]
pub fn parse_language(name: &str) -> Option<Language> {
    match name.to_lowercase().as_str() {
        "rust" => Some(Language::Rust),
        "typescript" | "ts" => Some(Language::TypeScript),
        "javascript" | "js" => Some(Language::JavaScript),
        "go" | "golang" => Some(Language::Go),
        "cpp" | "c++" | "cxx" | "c" => Some(Language::Cpp),
        _ => None,
    }
}

/// Generate a `krait.toml` config string from detected workspace roots.
#[must_use]
pub fn generate(package_roots: &[(Language, PathBuf)], project_root: &Path) -> String {
    let mut out = String::from("# krait.toml — generated by `krait init`\n");
    out.push_str("# Edit this file to customize which workspaces to index.\n");
    out.push_str("# Remove entries you don't need. Run `krait daemon stop` after changes.\n\n");

    for (lang, abs_path) in package_roots {
        let rel = abs_path
            .strip_prefix(project_root)
            .unwrap_or(abs_path)
            .to_string_lossy();
        let path_str = if rel.is_empty() { "." } else { &rel };

        let _ = writeln!(out, "[[workspace]]");
        let _ = writeln!(out, "path = \"{path_str}\"");
        let _ = writeln!(out, "language = \"{}\"", lang.name());
        out.push('\n');
    }

    // Add commented primary_workspaces section
    out.push_str("# Priority workspaces — always warm, exempt from LRU eviction\n");
    out.push_str("# primary_workspaces = [\"packages/core\", \"packages/api\"]\n\n");

    // Add commented max_active_sessions
    out.push_str("# Maximum concurrent LSP sessions for non-multi-root servers (default: 10)\n");
    out.push_str("# max_active_sessions = 10\n\n");

    out.push_str("# Maximum concurrent language server processes across all languages (default: unlimited)\n");
    out.push_str("# When exceeded, the least-recently-used language server is shut down.\n");
    out.push_str("# max_language_servers = 10\n\n");

    // Add commented server overrides section
    out.push_str("# Server overrides (uncomment to customize)\n");
    out.push_str("# [servers.typescript]\n");
    out.push_str("# binary = \"vtsls\"\n");
    out.push_str("# args = [\"--stdio\"]\n");

    out
}

/// Write config to `.krait/krait.toml`, creating `.krait/` if needed.
///
/// # Errors
/// Returns an error if the directory or file cannot be written.
pub fn write_config(project_root: &Path, content: &str) -> anyhow::Result<()> {
    let krait_dir = project_root.join(".krait");
    std::fs::create_dir_all(&krait_dir)?;
    let path = project_root.join(CONFIG_FILE);
    std::fs::write(&path, content)?;
    Ok(())
}

/// Check if a config file already exists.
#[must_use]
pub fn config_exists(project_root: &Path) -> bool {
    project_root.join(CONFIG_FILE).is_file() || project_root.join(LEGACY_CONFIG_FILE).is_file()
}

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

    #[test]
    fn parse_language_variants() {
        assert_eq!(parse_language("rust"), Some(Language::Rust));
        assert_eq!(parse_language("typescript"), Some(Language::TypeScript));
        assert_eq!(parse_language("ts"), Some(Language::TypeScript));
        assert_eq!(parse_language("javascript"), Some(Language::JavaScript));
        assert_eq!(parse_language("js"), Some(Language::JavaScript));
        assert_eq!(parse_language("go"), Some(Language::Go));
        assert_eq!(parse_language("golang"), Some(Language::Go));
        assert_eq!(parse_language("c++"), Some(Language::Cpp));
        assert_eq!(parse_language("cpp"), Some(Language::Cpp));
        assert_eq!(parse_language("unknown"), None);
    }

    #[test]
    fn parse_language_case_insensitive() {
        assert_eq!(parse_language("Rust"), Some(Language::Rust));
        assert_eq!(parse_language("TYPESCRIPT"), Some(Language::TypeScript));
    }

    #[test]
    fn load_returns_auto_detected_when_no_config() {
        let dir = tempfile::tempdir().unwrap();
        let loaded = load(dir.path());
        assert!(loaded.config.is_none());
        assert!(matches!(loaded.source, ConfigSource::AutoDetected));
    }

    #[test]
    fn load_reads_krait_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join(".krait")).unwrap();
        let config_content = r#"
[[workspace]]
path = "."
language = "rust"
"#;
        std::fs::write(dir.path().join(".krait/krait.toml"), config_content).unwrap();

        let loaded = load(dir.path());
        assert!(loaded.config.is_some());
        assert!(matches!(loaded.source, ConfigSource::KraitToml));

        let config = loaded.config.unwrap();
        assert_eq!(config.workspace.len(), 1);
        assert_eq!(config.workspace[0].path, ".");
        assert_eq!(config.workspace[0].language, "rust");
    }

    #[test]
    fn load_reads_legacy_config() {
        let dir = tempfile::tempdir().unwrap();
        let config_content = r#"
[[workspace]]
path = "."
language = "go"
"#;
        std::fs::write(dir.path().join("krait.toml"), config_content).unwrap();

        let loaded = load(dir.path());
        assert!(loaded.config.is_some());
        assert!(matches!(loaded.source, ConfigSource::LegacyKraitToml));
    }

    #[test]
    fn krait_toml_takes_priority_over_legacy() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join(".krait")).unwrap();
        std::fs::write(
            dir.path().join(".krait/krait.toml"),
            "[[workspace]]\npath = \".\"\nlanguage = \"rust\"\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("krait.toml"),
            "[[workspace]]\npath = \".\"\nlanguage = \"go\"\n",
        )
        .unwrap();

        let loaded = load(dir.path());
        let config = loaded.config.unwrap();
        assert_eq!(config.workspace[0].language, "rust");
        assert!(matches!(loaded.source, ConfigSource::KraitToml));
    }

    #[test]
    fn config_to_package_roots_validates() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("src")).unwrap();

        let config = ProjectConfig {
            root: None,
            workspace: vec![
                WorkspaceEntry {
                    path: "src".to_string(),
                    language: "rust".to_string(),
                    server: None,
                },
                WorkspaceEntry {
                    path: "nonexistent".to_string(),
                    language: "rust".to_string(),
                    server: None,
                },
                WorkspaceEntry {
                    path: "src".to_string(),
                    language: "fakeLang".to_string(),
                    server: None,
                },
            ],
            servers: HashMap::new(),
            primary_workspaces: vec![],
            max_active_sessions: None,
            max_language_servers: None,
        };

        let roots = config_to_package_roots(&config, dir.path());
        assert_eq!(roots.len(), 1, "only valid entries should be returned");
        assert_eq!(roots[0].0, Language::Rust);
    }

    #[test]
    fn generate_produces_valid_toml() {
        let dir = tempfile::tempdir().unwrap();
        let roots = vec![
            (Language::TypeScript, dir.path().join("packages/api")),
            (Language::TypeScript, dir.path().join("packages/web")),
        ];

        let content = generate(&roots, dir.path());
        assert!(content.contains("[[workspace]]"));
        assert!(content.contains("packages/api"));
        assert!(content.contains("packages/web"));
        assert!(content.contains("language = \"typescript\""));

        // Verify it parses back
        let parsed: ProjectConfig = toml::from_str(&content).unwrap();
        assert_eq!(parsed.workspace.len(), 2);
    }

    #[test]
    fn generate_dot_for_root_workspace() {
        let dir = tempfile::tempdir().unwrap();
        let roots = vec![(Language::Rust, dir.path().to_path_buf())];

        let content = generate(&roots, dir.path());
        assert!(content.contains("path = \".\""));
    }

    #[test]
    fn config_exists_detects_files() {
        let dir = tempfile::tempdir().unwrap();
        assert!(!config_exists(dir.path()));

        std::fs::create_dir(dir.path().join(".krait")).unwrap();
        std::fs::write(dir.path().join(".krait/krait.toml"), "").unwrap();
        assert!(config_exists(dir.path()));
    }

    #[test]
    fn config_with_primary_workspaces() {
        let content = r#"
primary_workspaces = ["packages/core", "packages/api"]
max_active_sessions = 5

[[workspace]]
path = "."
language = "typescript"
"#;
        let config: ProjectConfig = toml::from_str(content).unwrap();
        assert_eq!(
            config.primary_workspaces,
            vec!["packages/core", "packages/api"]
        );
        assert_eq!(config.max_active_sessions, Some(5));
    }

    #[test]
    fn config_defaults_for_optional_fields() {
        let content = r#"
[[workspace]]
path = "."
language = "rust"
"#;
        let config: ProjectConfig = toml::from_str(content).unwrap();
        assert!(config.primary_workspaces.is_empty());
        assert!(config.max_active_sessions.is_none());
    }

    #[test]
    fn generate_includes_priority_and_sessions_comments() {
        let dir = tempfile::tempdir().unwrap();
        let roots = vec![(Language::Rust, dir.path().to_path_buf())];
        let content = generate(&roots, dir.path());
        assert!(content.contains("primary_workspaces"));
        assert!(content.contains("max_active_sessions"));
    }

    #[test]
    fn config_with_server_overrides() {
        let content = r#"
[[workspace]]
path = "."
language = "typescript"

[servers.typescript]
binary = "vtsls"
args = ["--stdio"]
"#;
        let config: ProjectConfig = toml::from_str(content).unwrap();
        assert_eq!(config.workspace.len(), 1);
        let ts_server = config.servers.get("typescript").unwrap();
        assert_eq!(ts_server.binary.as_deref(), Some("vtsls"));
        assert_eq!(
            ts_server.args.as_deref(),
            Some(&["--stdio".to_string()][..])
        );
    }
}