hematite-cli 0.11.0

Senior SysAdmin, Network Admin, Data Analyst, and Software Engineer living in your terminal. A high-precision local AI agent harness for LM Studio, Ollama, and other local OpenAI-compatible runtimes that runs 100% on your own silicon. Reads repos, edits files, runs builds, inspects full network state and workstation telemetry, and runs real Python/JS for data analysis.
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
use crate::agent::config;
use crate::agent::inference::InferenceEvent;
use serde_json::Value;
use std::process::Command;
use tokio::sync::mpsc;

const BUILD_TIMEOUT_SECS: u64 = 120;

/// Streaming variant — emits live shell lines to the SPECULAR panel while buffering
/// the final combined output for the tool result returned to the model.
pub async fn execute_streaming(
    args: &Value,
    tx: mpsc::Sender<InferenceEvent>,
) -> Result<String, String> {
    let cwd =
        std::env::current_dir().map_err(|e| format!("Cannot determine working directory: {e}"))?;
    let action = args
        .get("action")
        .and_then(|v| v.as_str())
        .unwrap_or("build");
    let explicit_profile = args.get("profile").and_then(|v| v.as_str());
    let timeout_override = args.get("timeout_secs").and_then(|v| v.as_u64());

    let config = config::load_config();
    if let Some(profile_name) = explicit_profile {
        let profile = config.verify.profiles.get(profile_name).ok_or_else(|| {
            format!(
                "Unknown verify profile `{}`. Define it in `.hematite/settings.json` or omit the profile argument.",
                profile_name
            )
        })?;
        if let Some(command) = profile_command(profile, action) {
            let timeout_secs = timeout_override
                .or(profile.timeout_secs)
                .unwrap_or(BUILD_TIMEOUT_SECS);
            return run_profile_command_streaming(profile_name, action, command, timeout_secs, tx)
                .await;
        }

        return Err(format!(
            "VERIFY PROFILE MISSING [{profile_name}] action `{action}`.\n\
             Configure `.hematite/settings.json` with a `{action}` command for this profile, \
             or call `verify_build` with a different action/profile."
        ));
    }

    if let Some(default_profile) = config.verify.default_profile.as_deref() {
        let profile = config.verify.profiles.get(default_profile).ok_or_else(|| {
            format!(
                "Configured default verify profile `{}` was not found in `.hematite/settings.json`.",
                default_profile
            )
        })?;
        if let Some(command) = profile_command(profile, action) {
            let timeout_secs = timeout_override
                .or(profile.timeout_secs)
                .unwrap_or(BUILD_TIMEOUT_SECS);
            return run_profile_command_streaming(
                default_profile,
                action,
                command,
                timeout_secs,
                tx,
            )
            .await;
        }

        return Err(format!(
            "VERIFY PROFILE MISSING [{default_profile}] action `{action}`.\n\
             Configure `.hematite/settings.json` with a `{action}` command for the default profile, \
             or call `verify_build` with an explicit profile."
        ));
    }

    let (label, command, timeout_secs) = autodetect_command(&cwd, action, timeout_override)?;
    run_profile_command_streaming(label, action, &command, timeout_secs, tx).await
}

pub async fn execute(args: &Value) -> Result<String, String> {
    let cwd =
        std::env::current_dir().map_err(|e| format!("Cannot determine working directory: {e}"))?;
    let action = args
        .get("action")
        .and_then(|v| v.as_str())
        .unwrap_or("build");
    let explicit_profile = args.get("profile").and_then(|v| v.as_str());
    let timeout_override = args.get("timeout_secs").and_then(|v| v.as_u64());

    let config = config::load_config();
    if let Some(profile_name) = explicit_profile {
        let profile = config.verify.profiles.get(profile_name).ok_or_else(|| {
            format!(
                "Unknown verify profile `{}`. Define it in `.hematite/settings.json` or omit the profile argument.",
                profile_name
            )
        })?;
        if let Some(command) = profile_command(profile, action) {
            let timeout_secs = timeout_override
                .or(profile.timeout_secs)
                .unwrap_or(BUILD_TIMEOUT_SECS);
            return run_profile_command(profile_name, action, command, timeout_secs).await;
        }

        return Err(format!(
            "VERIFY PROFILE MISSING [{profile_name}] action `{action}`.\n\
             Configure `.hematite/settings.json` with a `{action}` command for this profile, \
             or call `verify_build` with a different action/profile."
        ));
    }

    if let Some(default_profile) = config.verify.default_profile.as_deref() {
        let profile = config.verify.profiles.get(default_profile).ok_or_else(|| {
            format!(
                "Configured default verify profile `{}` was not found in `.hematite/settings.json`.",
                default_profile
            )
        })?;
        if let Some(command) = profile_command(profile, action) {
            let timeout_secs = timeout_override
                .or(profile.timeout_secs)
                .unwrap_or(BUILD_TIMEOUT_SECS);
            return run_profile_command(default_profile, action, command, timeout_secs).await;
        }

        return Err(format!(
            "VERIFY PROFILE MISSING [{default_profile}] action `{action}`.\n\
             Configure `.hematite/settings.json` with a `{action}` command for the default profile, \
             or call `verify_build` with an explicit profile."
        ));
    }

    let (label, command, timeout_secs) = autodetect_command(&cwd, action, timeout_override)?;
    run_profile_command(label, action, &command, timeout_secs).await
}

fn profile_command<'a>(profile: &'a config::VerifyProfile, action: &str) -> Option<&'a str> {
    match action {
        "build" => profile.build.as_deref(),
        "test" => profile.test.as_deref(),
        "lint" => profile.lint.as_deref(),
        "fix" => profile.fix.as_deref(),
        _ => None,
    }
}

fn autodetect_command(
    cwd: &std::path::Path,
    action: &str,
    timeout_override: Option<u64>,
) -> Result<(&'static str, String, u64), String> {
    let timeout_secs = timeout_override.unwrap_or(BUILD_TIMEOUT_SECS);
    let command = if cwd.join("Cargo.toml").exists() {
        match action {
            "build" => ("Rust/Cargo", "cargo build --color never".to_string()),
            "test" => ("Rust/Cargo", "cargo test --color never".to_string()),
            "lint" => (
                "Rust/Cargo",
                "cargo clippy --all-targets --all-features -- -D warnings".to_string(),
            ),
            "fix" => ("Rust/Cargo", "cargo fmt".to_string()),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("go.mod").exists() {
        match action {
            "build" => ("Go", "go build ./...".to_string()),
            "test" => ("Go", "go test ./...".to_string()),
            "lint" => ("Go", "go vet ./...".to_string()),
            "fix" => ("Go", "gofmt -w .".to_string()),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("CMakeLists.txt").exists() {
        // C / C++ (CMake) — create build dir if missing, configure + build
        let build_dir = "build";
        match action {
            "build" => (
                "C++/CMake",
                format!("cmake -B {build_dir} -DCMAKE_BUILD_TYPE=Release && cmake --build {build_dir} --parallel"),
            ),
            "test" => (
                "C++/CMake",
                format!("ctest --test-dir {build_dir} --output-on-failure"),
            ),
            "lint" => return Err(missing_profile_msg("C++/CMake", action)),
            "fix"  => return Err(missing_profile_msg("C++/CMake", action)),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("package.json").exists() {
        // Detect package manager: pnpm > yarn > bun > npm
        let pm = if cwd.join("pnpm-lock.yaml").exists()
            || cwd.join(".npmrc").exists() && {
                let rc = std::fs::read_to_string(cwd.join(".npmrc")).unwrap_or_default();
                rc.contains("pnpm")
            } {
            "pnpm"
        } else if cwd.join("yarn.lock").exists() {
            "yarn"
        } else if cwd.join("bun.lockb").exists() {
            "bun"
        } else {
            "npm"
        };
        // Detect TypeScript project for better label
        let label: &'static str = if cwd.join("tsconfig.json").exists() {
            match pm {
                "pnpm" => "TypeScript/pnpm",
                "yarn" => "TypeScript/yarn",
                "bun" => "TypeScript/bun",
                _ => "TypeScript/npm",
            }
        } else {
            match pm {
                "pnpm" => "Node/pnpm",
                "yarn" => "Node/yarn",
                "bun" => "Node/bun",
                _ => "Node/npm",
            }
        };
        match action {
            "build" => (label, format!("{pm} run build")),
            "test" => (label, format!("{pm} test")),
            "lint" => (label, format!("{pm} run lint")),
            "fix" => (label, format!("{pm} run format")),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("pyproject.toml").exists()
        || cwd.join("setup.py").exists()
        || cwd.join("requirements.txt").exists()
        || cwd.join(".venv").is_dir()
        || cwd.join("venv").is_dir()
        || cwd.join("env").is_dir()
    {
        // Python — prefer ruff when available, fall back to flake8/black/pytest
        // Prioritize local environment (Poetry, Pipenv, .venv)
        let py = resolve_python_cmd(cwd);
        match action {
            "build" => ("Python", format!("{py} -m compileall -q .")),
            "test" => ("Python", format!("{py} -m pytest -q")),
            "lint" => (
                "Python",
                format!("{py} -m ruff check . || {py} -m flake8 ."),
            ),
            "fix" => (
                "Python",
                format!("{py} -m ruff format . || {py} -m black ."),
            ),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("tsconfig.json").exists() {
        // TypeScript without package.json — bare tsc check
        match action {
            "build" => ("TypeScript/tsc", "tsc --noEmit".to_string()),
            "test" => return Err(missing_profile_msg("TypeScript/tsc", action)),
            "lint" => return Err(missing_profile_msg("TypeScript/tsc", action)),
            "fix" => return Err(missing_profile_msg("TypeScript/tsc", action)),
            _ => return Err(unknown_action(action)),
        }
    } else if cwd.join("index.html").exists() {
        match action {
            "build" => ("Static Web", "echo \"BUILD OK (Static assets ready)\"".to_string()),
            "test" => (
                "Static Web",
                "echo \"TEST OK (No test runner found; manual visual check and link verification suggested)\"".to_string(),
            ),
            "lint" => ("Static Web", "echo \"LINT OK (Basic structure verified)\"".to_string()),
            "fix" => ("Static Web", "echo \"FIX OK (No auto-formatter found for static assets)\"".to_string()),
            _ => return Err(unknown_action(action)),
        }
    } else {
        return Err(format!(
            "No recognized project root (Cargo.toml, package.json, go.mod, CMakeLists.txt, pyproject.toml, etc.) \
             found in {}.\nUse an explicit profile or configure a default verify profile in `.hematite/settings.json`.",
            cwd.display()
        ));
    };

    Ok((command.0, command.1, timeout_secs))
}

fn resolve_python_cmd(cwd: &std::path::Path) -> String {
    let config = config::load_config();

    if let Some(path) = config.python_path {
        if std::path::Path::new(&path).exists() {
            return path;
        }
    }

    if cwd.join("poetry.lock").exists() {
        return "poetry run python".to_string();
    }
    if cwd.join("Pipfile.lock").exists() || cwd.join("Pipfile").exists() {
        return "pipenv run python".to_string();
    }
    let venv_folders = [".venv", "venv", "env"];
    for folder in venv_folders {
        if cwd.join(folder).is_dir() {
            let rel_path = if cfg!(windows) {
                format!("{}\\Scripts\\python.exe", folder)
            } else {
                format!("{}/bin/python", folder)
            };
            if cwd.join(&rel_path).exists() {
                return format!(".{}{}", if cfg!(windows) { "\\" } else { "/" }, rel_path);
            }
        }
    }

    if cfg!(windows) {
        let check = Command::new("where").arg("py").output();
        if check.map(|o| o.status.success()).unwrap_or(false) {
            return "py -3".to_string();
        }
    }
    "python".to_string()
}

fn missing_profile_msg(stack: &str, action: &str) -> String {
    format!(
        "No auto-detected `{action}` command for [{stack}].\n\
         Add a verify profile in `.hematite/settings.json` if you want Hematite to run `{action}` for this project."
    )
}

fn unknown_action(action: &str) -> String {
    format!(
        "Unknown verify_build action `{}`. Use one of: build, test, lint, fix.",
        action
    )
}

async fn run_profile_command(
    profile_name: &str,
    action: &str,
    command: &str,
    timeout_secs: u64,
) -> Result<String, String> {
    let output = crate::tools::shell::execute(
        &serde_json::json!({
            "command": command,
            "timeout_secs": timeout_secs,
            "reason": format!("verify_build:{}:{}", profile_name, action),
        }),
        16384,
    )
    .await?;

    if output.contains("[exit code: 0]") || !output.contains("[exit code:") {
        Ok(format!(
            "BUILD OK [{}:{}]\ncommand: {}\n{}",
            profile_name,
            action,
            command,
            output.trim()
        ))
    } else if should_fallback_to_cargo_check(action, command, &output) {
        run_windows_self_hosted_check_fallback(profile_name, action, command, timeout_secs, &output)
            .await
    } else {
        Err(format!(
            "BUILD FAILED [{}:{}]\ncommand: {}\n{}",
            profile_name,
            action,
            command,
            output.trim()
        ))
    }
}

async fn run_profile_command_streaming(
    profile_name: &str,
    action: &str,
    command: &str,
    timeout_secs: u64,
    tx: mpsc::Sender<InferenceEvent>,
) -> Result<String, String> {
    let output = crate::tools::shell::execute_streaming(
        &serde_json::json!({
            "command": command,
            "timeout_secs": timeout_secs,
            "reason": format!("verify_build:{}:{}", profile_name, action),
        }),
        tx.clone(),
        16384,
    )
    .await?;

    if output.contains("[exit code: 0]") || !output.contains("[exit code:") {
        Ok(format!(
            "BUILD OK [{}:{}]\ncommand: {}\n{}",
            profile_name,
            action,
            command,
            output.trim()
        ))
    } else if should_fallback_to_cargo_check(action, command, &output) {
        run_windows_self_hosted_check_fallback_streaming(
            profile_name,
            action,
            command,
            timeout_secs,
            &output,
            tx,
        )
        .await
    } else {
        Err(format!(
            "BUILD FAILED [{}:{}]\ncommand: {}\n{}",
            profile_name,
            action,
            command,
            output.trim()
        ))
    }
}

async fn run_windows_self_hosted_check_fallback_streaming(
    profile_name: &str,
    action: &str,
    original_command: &str,
    timeout_secs: u64,
    original_output: &str,
    tx: mpsc::Sender<InferenceEvent>,
) -> Result<String, String> {
    let fallback_command = "cargo check --color never";
    let fallback_output = crate::tools::shell::execute_streaming(
        &serde_json::json!({
            "command": fallback_command,
            "timeout_secs": timeout_secs,
            "reason": format!("verify_build:{}:{}:self_hosted_windows_fallback", profile_name, action),
        }),
        tx,
        16384,
    )
    .await?;

    if fallback_output.contains("[exit code: 0]") || !fallback_output.contains("[exit code:") {
        Ok(format!(
            "BUILD OK [{}:{}]\ncommand: {}\n\
             Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, so Hematite fell back to `cargo check` to verify code health without deleting the live binary.\n\
             original build output:\n{}\n\
             fallback command: {}\n{}",
            profile_name,
            action,
            original_command,
            original_output.trim(),
            fallback_command,
            fallback_output.trim()
        ))
    } else {
        Err(format!(
            "BUILD FAILED [{}:{}]\ncommand: {}\n\
             Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, and the fallback `cargo check` also failed.\n\
             original build output:\n{}\n\
             fallback command: {}\n{}",
            profile_name,
            action,
            original_command,
            original_output.trim(),
            fallback_command,
            fallback_output.trim()
        ))
    }
}

fn should_fallback_to_cargo_check(action: &str, command: &str, output: &str) -> bool {
    if action != "build" || command.trim() != "cargo build --color never" {
        return false;
    }

    if cfg!(windows) {
        looks_like_windows_self_hosted_build_lock(output)
    } else {
        false
    }
}

fn looks_like_windows_self_hosted_build_lock(output: &str) -> bool {
    let lower = output.to_ascii_lowercase();
    lower.contains("failed to remove file")
        && lower.contains("target\\debug\\hematite.exe")
        && (lower.contains("access is denied")
            || lower.contains("being used by another process")
            || lower.contains("permission denied"))
}

async fn run_windows_self_hosted_check_fallback(
    profile_name: &str,
    action: &str,
    original_command: &str,
    timeout_secs: u64,
    original_output: &str,
) -> Result<String, String> {
    let fallback_command = "cargo check --color never";
    let fallback_output = crate::tools::shell::execute(&serde_json::json!({
        "command": fallback_command,
        "timeout_secs": timeout_secs,
        "reason": format!("verify_build:{}:{}:self_hosted_windows_fallback", profile_name, action),
    }), 16384)
    .await?;

    if fallback_output.contains("[exit code: 0]") || !fallback_output.contains("[exit code:") {
        Ok(format!(
            "BUILD OK [{}:{}]\ncommand: {}\n\
             Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, so Hematite fell back to `cargo check` to verify code health without deleting the live binary.\n\
             original build output:\n{}\n\
             fallback command: {}\n{}",
            profile_name,
            action,
            original_command,
            original_output.trim(),
            fallback_command,
            fallback_output.trim()
        ))
    } else {
        Err(format!(
            "BUILD FAILED [{}:{}]\ncommand: {}\n\
             Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, and the fallback `cargo check` also failed.\n\
             original build output:\n{}\n\
             fallback command: {}\n{}",
            profile_name,
            action,
            original_command,
            original_output.trim(),
            fallback_command,
            fallback_output.trim()
        ))
    }
}

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

    #[test]
    fn detects_windows_self_hosted_build_lock_pattern() {
        let sample = "[stderr] error: failed to remove file `C:\\Users\\ocean\\AntigravityProjects\\Hematite-CLI\\target\\debug\\hematite.exe`\r\nAccess is denied. (os error 5)";
        assert!(looks_like_windows_self_hosted_build_lock(sample));
    }

    #[test]
    fn ignores_unrelated_build_failures() {
        let sample = "[stderr] error[E0425]: cannot find value `foo` in this scope";
        assert!(!looks_like_windows_self_hosted_build_lock(sample));
        assert!(!should_fallback_to_cargo_check(
            "build",
            "cargo build --color never",
            sample
        ));
    }

    #[test]
    fn autodetect_rust_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert_eq!(label, "Rust/Cargo");
        assert!(cmd.contains("cargo build"));
        let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
        assert!(test_cmd.contains("cargo test"));
        let (_, lint_cmd, _) = autodetect_command(dir.path(), "lint", None).unwrap();
        assert!(lint_cmd.contains("clippy"));
    }

    #[test]
    fn autodetect_go_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("go.mod"),
            "module example.com/foo\ngo 1.21\n",
        )
        .unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert_eq!(label, "Go");
        assert!(cmd.contains("go build"));
        let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
        assert!(test_cmd.contains("go test"));
        let (_, lint_cmd, _) = autodetect_command(dir.path(), "lint", None).unwrap();
        assert!(lint_cmd.contains("go vet"));
    }

    #[test]
    fn autodetect_cmake_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("CMakeLists.txt"),
            "cmake_minimum_required(VERSION 3.20)\n",
        )
        .unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert_eq!(label, "C++/CMake");
        assert!(cmd.contains("cmake"));
        assert!(cmd.contains("--build"));
    }

    #[test]
    fn autodetect_node_npm_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("package.json"), "{}").unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert!(label.contains("Node") || label.contains("TypeScript"));
        assert!(cmd.contains("npm run build"));
    }

    #[test]
    fn autodetect_node_yarn_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("package.json"), "{}").unwrap();
        std::fs::write(dir.path().join("yarn.lock"), "").unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert!(label.contains("yarn"));
        assert!(cmd.contains("yarn run build"));
    }

    #[test]
    fn autodetect_node_pnpm_stack() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("package.json"), "{}").unwrap();
        std::fs::write(dir.path().join("pnpm-lock.yaml"), "").unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert!(label.contains("pnpm"));
        assert!(cmd.contains("pnpm run build"));
    }

    #[test]
    fn autodetect_python_stack_pyproject() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("pyproject.toml"), "[build-system]\n").unwrap();
        let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert_eq!(label, "Python");
        assert!(cmd.contains("compileall"));
        let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
        assert!(test_cmd.contains("pytest"));
    }

    #[test]
    fn autodetect_python_stack_requirements() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("requirements.txt"), "fastapi\n").unwrap();
        let (label, _, _) = autodetect_command(dir.path(), "build", None).unwrap();
        assert_eq!(label, "Python");
    }

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

        // Mock the python executable
        let bin_sub = if cfg!(windows) { "Scripts" } else { "bin" };
        let exe_name = if cfg!(windows) {
            "python.exe"
        } else {
            "python"
        };
        let bin_dir = venv.join(bin_sub);
        std::fs::create_dir(&bin_dir).unwrap();
        std::fs::write(bin_dir.join(exe_name), "").unwrap();

        let cmd = resolve_python_cmd(dir.path());
        assert!(cmd.contains(".venv"));
        assert!(cmd.contains(bin_sub));
    }

    #[test]
    fn resolves_poetry_run() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("poetry.lock"), "").unwrap();
        let cmd = resolve_python_cmd(dir.path());
        assert_eq!(cmd, "poetry run python");
    }

    #[test]
    fn autodetect_no_project_returns_err() {
        let dir = tempfile::tempdir().unwrap();
        let result = autodetect_command(dir.path(), "build", None);
        assert!(result.is_err());
        let msg = result.unwrap_err();
        assert!(msg.contains("No recognized project root"));
        assert!(msg.contains("Cargo.toml"));
        assert!(msg.contains("CMakeLists.txt"));
    }
}