ai-rsk 0.7.2

Security gate for AI-generated code - blocks the build until vulnerabilities are fixed
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
use crate::types::{Ecosystem, ExternalTool, ToolStatus};
use colored::Colorize;
use std::path::PathBuf;
use std::process::Command;

/// Get the list of required tools based on detected ecosystems.
/// These 3 are ALWAYS required regardless of ecosystem.
/// Ecosystem-specific tools are added on top.
pub fn get_required_tools(ecosystems: &[Ecosystem]) -> Vec<ExternalTool> {
    let mut tools = vec![
        ExternalTool {
            name: "semgrep".to_string(),
            binary: "semgrep".to_string(),
            required: true,
            install_hint: "pipx install semgrep  OR  pip install semgrep  OR  brew install semgrep".to_string(),
            install_commands: vec![
                vec!["pipx".into(), "install".into(), "semgrep".into()],
                vec!["pip".into(), "install".into(), "--user".into(), "semgrep".into()],
                vec!["pip".into(), "install".into(), "--system".into(), "semgrep".into()],
                vec!["pip3".into(), "install".into(), "--user".into(), "semgrep".into()],
                vec!["brew".into(), "install".into(), "semgrep".into()],
            ],
            update_commands: vec![
                vec!["pipx".into(), "upgrade".into(), "semgrep".into()],
                vec!["pip".into(), "install".into(), "--upgrade".into(), "semgrep".into()],
                vec!["pip3".into(), "install".into(), "--upgrade".into(), "semgrep".into()],
                vec!["brew".into(), "upgrade".into(), "semgrep".into()],
            ],
            github_release: None, // Semgrep is Python-based, no single binary release
        },
        ExternalTool {
            name: "gitleaks".to_string(),
            binary: "gitleaks".to_string(),
            required: true,
            install_hint: "brew install gitleaks  OR  go install github.com/gitleaks/gitleaks/v8@latest".to_string(),
            install_commands: vec![
                vec!["brew".into(), "install".into(), "gitleaks".into()],
                vec!["go".into(), "install".into(), "github.com/gitleaks/gitleaks/v8@latest".into()],
            ],
            update_commands: vec![
                vec!["brew".into(), "upgrade".into(), "gitleaks".into()],
                vec!["go".into(), "install".into(), "github.com/gitleaks/gitleaks/v8@latest".into()],
            ],
            github_release: Some((
                "gitleaks/gitleaks".into(),
                "gitleaks_{version}_{os}_{arch}.tar.gz".into(),
            )),
        },
        ExternalTool {
            name: "osv-scanner".to_string(),
            binary: "osv-scanner".to_string(),
            required: true,
            install_hint: "go install github.com/google/osv-scanner/v2/cmd/osv-scanner@latest  OR  brew install osv-scanner".to_string(),
            install_commands: vec![
                vec!["go".into(), "install".into(), "github.com/google/osv-scanner/v2/cmd/osv-scanner@latest".into()],
                vec!["brew".into(), "install".into(), "osv-scanner".into()],
            ],
            update_commands: vec![
                vec!["go".into(), "install".into(), "github.com/google/osv-scanner/v2/cmd/osv-scanner@latest".into()],
                vec!["brew".into(), "upgrade".into(), "osv-scanner".into()],
            ],
            github_release: Some((
                "google/osv-scanner".into(),
                "osv-scanner_{os}_{arch}".into(),
            )),
        },
    ];

    // Ecosystem-specific required tools
    if ecosystems.contains(&Ecosystem::JavaScript) {
        tools.push(ExternalTool {
            name: "knip".to_string(),
            binary: "knip".to_string(),
            required: true,
            install_hint: "npm install -g knip  OR  npm install -D knip (in your project)"
                .to_string(),
            install_commands: vec![
                vec!["npm".into(), "install".into(), "-g".into(), "knip".into()],
                vec!["pnpm".into(), "add".into(), "-g".into(), "knip".into()],
                vec!["bun".into(), "add".into(), "-g".into(), "knip".into()],
            ],
            update_commands: vec![vec![
                "npm".into(),
                "update".into(),
                "-g".into(),
                "knip".into(),
            ]],
            github_release: None,
        });
    }

    tools
}

/// Get the list of recommended tools based on detected ecosystems.
/// RTK is always recommended regardless of ecosystem.
pub fn get_recommended_tools(ecosystems: &[Ecosystem]) -> Vec<ExternalTool> {
    let mut tools = vec![ExternalTool {
        name: "rtk".to_string(),
        binary: "rtk".to_string(),
        required: false,
        install_hint: "cargo install --git https://github.com/rtk-ai/rtk".to_string(),
        install_commands: vec![],
        update_commands: vec![],
        github_release: None,
    }];

    for eco in ecosystems {
        match eco {
            Ecosystem::JavaScript => {
                // knip is now required (moved to get_required_tools)
            }
            Ecosystem::Rust => {
                tools.push(ExternalTool {
                    name: "cargo-audit".to_string(),
                    binary: "cargo-audit".to_string(),
                    required: false,
                    install_hint: "cargo install cargo-audit".to_string(),
                    install_commands: vec![],
                    update_commands: vec![],
                    github_release: None,
                });
            }
            _ => {}
        }
    }

    tools
}

/// Check if a tool is installed by looking for its binary in PATH
/// and extracting its version.
pub fn check_tool(tool: &ExternalTool) -> ToolStatus {
    // First check if the binary exists in PATH
    if which::which(&tool.binary).is_err() {
        return ToolStatus::Missing;
    }

    // Try to get version
    let version = get_tool_version(&tool.binary);

    ToolStatus::Installed {
        version: version.unwrap_or_else(|| "unknown".to_string()),
    }
}

/// Extract version string from a tool by running `tool --version`.
fn get_tool_version(binary: &str) -> Option<String> {
    let output = Command::new(binary).arg("--version").output().ok()?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Some tools output version to stdout, some to stderr
    let version_text = if stdout.trim().is_empty() {
        stderr.to_string()
    } else {
        stdout.to_string()
    };

    // Extract first line, trim whitespace
    let first_line = version_text.lines().next()?.trim().to_string();

    if first_line.is_empty() {
        None
    } else {
        Some(first_line)
    }
}

/// Compare two semver-like version strings.
/// Returns true if `current` >= `required`.
/// Handles formats like "1.50.0", "v1.50.0", "semgrep 1.50.0", etc.
/// Extracts the first version-like pattern (digits.digits.digits) from the string.
pub fn version_satisfies(current_version_str: &str, required: &str) -> bool {
    let extract_version = |s: &str| -> Option<Vec<u64>> {
        // Find a pattern that looks like a version number (digits.digits or digits.digits.digits)
        let re = regex::Regex::new(r"(\d+)\.(\d+)(?:\.(\d+))?").ok()?;
        let caps = re.captures(s)?;
        let major: u64 = caps.get(1)?.as_str().parse().ok()?;
        let minor: u64 = caps.get(2)?.as_str().parse().ok()?;
        let patch: u64 = caps
            .get(3)
            .and_then(|m| m.as_str().parse().ok())
            .unwrap_or(0);
        Some(vec![major, minor, patch])
    };

    let current = match extract_version(current_version_str) {
        Some(v) => v,
        None => return false, // Can't parse current → assume not satisfying
    };
    let required = match extract_version(required) {
        Some(v) => v,
        None => return true, // Can't parse required → no constraint
    };

    current >= required
}

/// RAII guard that removes a temporary directory when dropped.
struct TmpDirGuard<'a>(&'a std::path::Path);
impl Drop for TmpDirGuard<'_> {
    fn drop(&mut self) {
        std::fs::remove_dir_all(self.0).ok();
    }
}

/// Detect the current OS as used in GitHub release asset names.
/// Returns None if the OS is not supported for binary downloads.
fn detect_os() -> Option<&'static str> {
    if cfg!(target_os = "linux") {
        Some("linux")
    } else if cfg!(target_os = "macos") {
        Some("darwin")
    } else if cfg!(target_os = "windows") {
        Some("windows")
    } else {
        None
    }
}

/// Detect the current CPU architecture as used in GitHub release asset names.
/// Returns a tuple: (primary name, alternative name) because some projects
/// use "x64" (gitleaks) while others use "amd64" (osv-scanner).
fn detect_arch() -> Option<(&'static str, &'static str)> {
    if cfg!(target_arch = "x86_64") {
        Some(("amd64", "x64"))
    } else if cfg!(target_arch = "aarch64") {
        Some(("arm64", "arm64"))
    } else {
        None
    }
}

/// Get the user's home directory, cross-platform.
/// Linux/macOS: $HOME
/// Windows: %USERPROFILE% (fallback: %HOMEDRIVE%%HOMEPATH%)
fn home_dir() -> Option<PathBuf> {
    if let Ok(home) = std::env::var("HOME") {
        return Some(PathBuf::from(home));
    }
    if let Ok(profile) = std::env::var("USERPROFILE") {
        return Some(PathBuf::from(profile));
    }
    if let (Ok(drive), Ok(path)) = (std::env::var("HOMEDRIVE"), std::env::var("HOMEPATH")) {
        return Some(PathBuf::from(format!("{}{}", drive, path)));
    }
    None
}

/// Ensure the user-local binary directory exists and return its path.
/// Linux/macOS: ~/.local/bin/
/// Windows: %LOCALAPPDATA%\ai-rsk\bin\
fn ensure_local_bin() -> Option<PathBuf> {
    let local_bin = if cfg!(target_os = "windows") {
        let local_app_data = std::env::var("LOCALAPPDATA").ok()?;
        PathBuf::from(local_app_data).join("ai-rsk").join("bin")
    } else {
        let home = home_dir()?;
        home.join(".local").join("bin")
    };
    if !local_bin.exists() {
        std::fs::create_dir_all(&local_bin).ok()?;
    }
    Some(local_bin)
}

/// Download and install a tool from its GitHub Releases page.
/// This is the last-resort fallback when no package manager is available.
///
/// Strategy:
/// 1. Query GitHub API for latest release tag
/// 2. Build the asset URL from the pattern in ExternalTool.github_release
/// 3. Download with curl
/// 4. Extract if archive (tar.gz), or chmod +x if bare binary
/// 5. Move to ~/.local/bin/
/// 6. Verify the tool is now callable
///
/// Requires: curl in PATH (available on Linux, macOS, and Windows 10+).
fn install_from_github_release(tool: &ExternalTool) -> bool {
    let (repo, asset_pattern) = match &tool.github_release {
        Some(r) => r,
        None => return false,
    };

    // curl is required for download.
    // On Windows 10+, curl.exe is bundled at C:\Windows\System32\curl.exe.
    // On older Windows, this will fail gracefully.
    if which::which("curl").is_err() {
        eprintln!(
            "  {} curl not found - cannot download from GitHub Releases.",
            "✗".red()
        );
        if cfg!(target_os = "windows") {
            eprintln!(
                "  {} curl.exe is included in Windows 10 1803+. If you're on an older version, install curl manually.",
                "→".cyan()
            );
        }
        return false;
    }

    let os = match detect_os() {
        Some(o) => o,
        None => {
            eprintln!(
                "  {} Unsupported OS for GitHub Release download.",
                "✗".red()
            );
            return false;
        }
    };

    let (arch_primary, arch_alt) = match detect_arch() {
        Some(a) => a,
        None => {
            eprintln!(
                "  {} Unsupported architecture for GitHub Release download.",
                "✗".red()
            );
            return false;
        }
    };

    let local_bin = match ensure_local_bin() {
        Some(p) => p,
        None => {
            eprintln!("  {} Cannot create ~/.local/bin/ directory.", "✗".red());
            return false;
        }
    };

    eprintln!(
        "  {} Downloading {} from GitHub Releases (last resort)...",
        "→".cyan(),
        tool.name.bold()
    );

    // Step 1: Get latest release tag via GitHub API
    let tag_output = Command::new("curl")
        .args([
            "-sfL",
            &format!("https://api.github.com/repos/{}/releases/latest", repo),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output();

    let tag_output = match tag_output {
        Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
        _ => {
            eprintln!("  {} Failed to query GitHub API for {}.", "✗".red(), repo);
            return false;
        }
    };

    // Parse tag_name from the GitHub API JSON response.
    // serde_json is already a dependency of this project.
    let tag = serde_json::from_str::<serde_json::Value>(&tag_output)
        .ok()
        .and_then(|v| v["tag_name"].as_str().map(String::from));

    let tag = match tag {
        Some(t) => t,
        None => {
            eprintln!(
                "  {} Could not parse release tag from GitHub API response.",
                "✗".red()
            );
            return false;
        }
    };

    // Version without 'v' prefix (e.g., "8.30.0" from "v8.30.0")
    let version = tag.strip_prefix('v').unwrap_or(&tag);

    // Step 2: Build asset name by trying both arch variants
    // Try primary arch first (amd64), then alternative (x64)
    let asset_primary = asset_pattern
        .replace("{version}", version)
        .replace("{os}", os)
        .replace("{arch}", arch_primary);

    let asset_alt = asset_pattern
        .replace("{version}", version)
        .replace("{os}", os)
        .replace("{arch}", arch_alt);

    let is_archive = asset_primary.ends_with(".tar.gz") || asset_primary.ends_with(".zip");

    // Step 3: Download - try primary arch name first, then alternative
    // Use a unique subdirectory in the system temp dir to avoid collisions.
    // We clean it up at the end of the function.
    let tmp_dir = std::env::temp_dir().join(format!("ai-rsk-install-{}", std::process::id()));
    if std::fs::create_dir_all(&tmp_dir).is_err() {
        eprintln!("  {} Cannot create temp directory.", "✗".red());
        return false;
    }

    // Ensure cleanup on all exit paths
    let _cleanup = TmpDirGuard(&tmp_dir);

    let download_url_primary = format!(
        "https://github.com/{}/releases/download/{}/{}",
        repo, tag, asset_primary
    );
    let download_url_alt = format!(
        "https://github.com/{}/releases/download/{}/{}",
        repo, tag, asset_alt
    );

    let mut downloaded = false;
    let mut actual_asset = asset_primary.clone();

    for (url, asset_name) in [
        (&download_url_primary, &asset_primary),
        (&download_url_alt, &asset_alt),
    ] {
        let dl_path = tmp_dir.join(asset_name);
        let status = Command::new("curl")
            .args(["-sfL", "-o", &dl_path.to_string_lossy(), url])
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .status();

        if let Ok(s) = status
            && s.success()
            && dl_path.exists()
            && dl_path.metadata().map(|m| m.len() > 0).unwrap_or(false)
        {
            downloaded = true;
            actual_asset = asset_name.clone();
            break;
        }
    }

    if !downloaded {
        eprintln!(
            "  {} Download failed for {} (tried both arch variants).",
            "✗".red(),
            tool.name
        );
        return false;
    }

    let actual_download_path = tmp_dir.join(&actual_asset);

    // Step 4: Extract archive or set executable bit
    let binary_path = if is_archive && actual_asset.ends_with(".tar.gz") {
        // Extract tar.gz (tar is available on Linux, macOS, and Windows 10+)
        let status = Command::new("tar")
            .args([
                "-xzf",
                &actual_download_path.to_string_lossy(),
                "-C",
                &tmp_dir.to_string_lossy(),
            ])
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .status();

        match status {
            Ok(s) if s.success() => {}
            _ => {
                eprintln!("  {} Failed to extract tar.gz archive.", "✗".red());
                return false;
            }
        }

        // Find the binary in the extracted files
        let extracted_binary = tmp_dir.join(&tool.binary);
        if extracted_binary.exists() {
            extracted_binary
        } else {
            eprintln!(
                "  {} Binary '{}' not found in extracted archive.",
                "✗".red(),
                tool.binary
            );
            return false;
        }
    } else if is_archive && actual_asset.ends_with(".zip") {
        // Extract .zip - common format for Windows GitHub Releases.
        // Use tar on Windows 10+ (bsdtar supports .zip) or unzip on Unix.
        let extract_status = if cfg!(target_os = "windows") {
            Command::new("tar")
                .args([
                    "-xf",
                    &actual_download_path.to_string_lossy(),
                    "-C",
                    &tmp_dir.to_string_lossy(),
                ])
                .stdout(std::process::Stdio::piped())
                .stderr(std::process::Stdio::piped())
                .status()
        } else {
            Command::new("unzip")
                .args([
                    "-o",
                    actual_download_path.to_string_lossy().as_ref(),
                    "-d",
                    tmp_dir.to_string_lossy().as_ref(),
                ])
                .stdout(std::process::Stdio::piped())
                .stderr(std::process::Stdio::piped())
                .status()
        };

        match extract_status {
            Ok(s) if s.success() => {}
            _ => {
                eprintln!("  {} Failed to extract zip archive.", "✗".red());
                return false;
            }
        }

        // On Windows, the binary name has .exe extension
        let binary_name = if cfg!(target_os = "windows") {
            format!("{}.exe", tool.binary)
        } else {
            tool.binary.clone()
        };
        let extracted_binary = tmp_dir.join(&binary_name);
        if extracted_binary.exists() {
            extracted_binary
        } else {
            eprintln!(
                "  {} Binary '{}' not found in extracted zip archive.",
                "✗".red(),
                binary_name
            );
            return false;
        }
    } else {
        // Bare binary - just chmod +x
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if let Ok(metadata) = actual_download_path.metadata() {
                let mut perms = metadata.permissions();
                perms.set_mode(0o755);
                std::fs::set_permissions(&actual_download_path, perms).ok();
            }
        }
        actual_download_path
    };

    // Step 5: Move to local bin directory
    let dest_name = if cfg!(target_os = "windows") && !tool.binary.ends_with(".exe") {
        format!("{}.exe", tool.binary)
    } else {
        tool.binary.clone()
    };
    let dest = local_bin.join(&dest_name);
    if std::fs::copy(&binary_path, &dest).is_err() {
        eprintln!("  {} Failed to copy binary to {:?}.", "✗".red(), dest);
        return false;
    }

    // Ensure executable bit on final location
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(metadata) = dest.metadata() {
            let mut perms = metadata.permissions();
            perms.set_mode(0o755);
            std::fs::set_permissions(&dest, perms).ok();
        }
    }

    // Step 6: Verify - check if tool is now in PATH
    // ~/.local/bin might not be in PATH, so also check directly
    if which::which(&tool.binary).is_ok() {
        eprintln!(
            "  {} {} installed via GitHub Release ({}).",
            "✓".green(),
            tool.name,
            tag
        );
        return true;
    }

    // If not in PATH, check if the binary exists and works at dest
    let verify = Command::new(&dest)
        .arg("--version")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .status();

    if let Ok(s) = verify
        && s.success()
    {
        eprintln!(
            "  {} {} installed to {} ({}). Add ~/.local/bin to your PATH if not already done.",
            "✓".green(),
            tool.name,
            dest.display(),
            tag
        );
        return true;
    }

    eprintln!(
        "  {} {} downloaded but binary verification failed.",
        "✗".red(),
        tool.name
    );
    false
}

/// Attempt to auto-install a missing tool.
/// Tries each install_command in order until one succeeds.
/// Falls back to GitHub Release download as last resort.
/// Returns true if installation succeeded (tool now in PATH), false otherwise.
pub fn auto_install_tool(tool: &ExternalTool) -> bool {
    eprintln!("  {} Auto-installing {}...", "→".cyan(), tool.name.bold());

    for cmd in &tool.install_commands {
        if cmd.is_empty() {
            continue;
        }

        let binary = &cmd[0];
        let args: Vec<&str> = cmd[1..].iter().map(|s| s.as_str()).collect();

        // Check if the installer binary exists
        if which::which(binary).is_err() {
            continue;
        }

        let result = Command::new(binary)
            .args(&args)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .status();

        match result {
            Ok(status) if status.success() => {
                // Verify the tool is now in PATH
                if which::which(&tool.binary).is_ok() {
                    eprintln!("  {} {} installed successfully.", "✓".green(), tool.name);
                    return true;
                }
            }
            _ => continue,
        }
    }

    // Last resort: download from GitHub Releases
    if tool.github_release.is_some() && install_from_github_release(tool) {
        return true;
    }

    eprintln!(
        "  {} Failed to auto-install {}. Manual install required: {}",
        "✗".red(),
        tool.name,
        tool.install_hint
    );
    false
}

/// Attempt to auto-update an already installed tool to the latest version.
/// Tries each update command in order until one succeeds.
/// This handles the case where the tool was installed via different package managers
/// (e.g., semgrep via pip vs pipx vs brew).
/// Returns true if any update command ran successfully.
pub fn auto_update_tool(tool: &ExternalTool) -> bool {
    if tool.update_commands.is_empty() {
        return false;
    }

    eprintln!("  {} Updating {}...", "→".cyan(), tool.name);

    for cmd in &tool.update_commands {
        if cmd.is_empty() {
            continue;
        }

        let binary = &cmd[0];
        let args: Vec<&str> = cmd[1..].iter().map(|s| s.as_str()).collect();

        // Check if the updater binary exists
        if which::which(binary).is_err() {
            continue;
        }

        let result = Command::new(binary)
            .args(&args)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .status();

        if let Ok(status) = result
            && status.success()
        {
            let new_version = get_tool_version(&tool.binary).unwrap_or_else(|| "unknown".into());
            eprintln!("  {} {} updated ({})", "✓".green(), tool.name, new_version);
            return true;
        }
    }

    // All package manager commands failed — try GitHub Release as last resort
    // This handles the case where the tool was installed via direct binary download
    if tool.github_release.is_some() && install_from_github_release(tool) {
        return true;
    }

    // All update methods failed — not a critical error, continue with current version
    eprintln!(
        "  {} {} update failed - continuing with current version.",
        "!".yellow(),
        tool.name
    );
    false
}

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

    #[test]
    fn test_universal_tools_always_present() {
        let tools = get_required_tools(&[]);
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"semgrep"));
        assert!(names.contains(&"gitleaks"));
        assert!(names.contains(&"osv-scanner"));
        assert_eq!(tools.len(), 3);
    }

    #[test]
    fn test_ecosystem_specific_required() {
        // JS/TS: 3 universal + knip = 4
        let js_tools = get_required_tools(&[Ecosystem::JavaScript]);
        assert_eq!(js_tools.len(), 4);
        assert!(js_tools.iter().any(|t| t.name == "knip"));

        // Non-JS: 3 universal only
        for eco in &[Ecosystem::Python, Ecosystem::Go, Ecosystem::Rust] {
            let tools = get_required_tools(&[*eco]);
            assert_eq!(
                tools.len(),
                3,
                "Ecosystem {:?} should have 3 required tools",
                eco
            );
        }
    }

    #[test]
    fn test_multi_ecosystem_with_js() {
        let tools = get_required_tools(&[Ecosystem::JavaScript, Ecosystem::Python, Ecosystem::Go]);
        assert_eq!(tools.len(), 4); // 3 universal + knip (JS detected)
        assert!(tools.iter().any(|t| t.name == "knip"));
    }

    #[test]
    fn test_rtk_always_recommended() {
        let tools = get_recommended_tools(&[]);
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"rtk"));
    }

    #[test]
    fn test_recommended_javascript_no_knip() {
        // knip is now required, not recommended
        let tools = get_recommended_tools(&[Ecosystem::JavaScript]);
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(!names.contains(&"knip"));
    }

    #[test]
    fn test_recommended_rust_has_cargo_audit() {
        let tools = get_recommended_tools(&[Ecosystem::Rust]);
        let names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(names.contains(&"cargo-audit"));
    }

    #[test]
    fn test_all_tools_have_install_hint() {
        let required = get_required_tools(&[
            Ecosystem::JavaScript,
            Ecosystem::Python,
            Ecosystem::Go,
            Ecosystem::Rust,
        ]);
        let recommended = get_recommended_tools(&[Ecosystem::JavaScript, Ecosystem::Rust]);

        for tool in required.iter().chain(recommended.iter()) {
            assert!(
                !tool.install_hint.is_empty(),
                "Tool {} has empty install_hint",
                tool.name
            );
        }
    }

    #[test]
    fn test_version_satisfies_exact() {
        assert!(version_satisfies("1.50.0", "1.50.0"));
    }

    #[test]
    fn test_version_satisfies_newer() {
        assert!(version_satisfies("1.51.0", "1.50.0"));
        assert!(version_satisfies("2.0.0", "1.50.0"));
    }

    #[test]
    fn test_version_satisfies_older() {
        assert!(!version_satisfies("1.49.0", "1.50.0"));
        assert!(!version_satisfies("0.99.0", "1.0.0"));
    }

    #[test]
    fn test_version_satisfies_with_prefix() {
        assert!(version_satisfies("semgrep 1.50.0", "1.50.0"));
        assert!(version_satisfies("v1.50.0", "1.50.0"));
        assert!(version_satisfies("gitleaks v8.18.2", "8.18.0"));
    }

    #[test]
    fn test_version_satisfies_two_parts() {
        assert!(version_satisfies("1.50", "1.50.0"));
        assert!(version_satisfies("2.0", "1.50.0"));
    }

    #[test]
    fn test_version_satisfies_unparseable() {
        assert!(!version_satisfies("unknown", "1.50.0"));
        assert!(version_satisfies("1.50.0", "unparseable"));
    }

    #[test]
    fn test_check_nonexistent_tool() {
        let tool = ExternalTool {
            name: "nonexistent-tool-xyz".to_string(),
            binary: "nonexistent-tool-xyz".to_string(),
            required: true,
            install_hint: "impossible".to_string(),
            install_commands: vec![],
            update_commands: vec![],
            github_release: None,
        };
        assert_eq!(check_tool(&tool), ToolStatus::Missing);
    }
}