rusty-stack 0.4.0

A comprehensive ML environment installer and platform toolkit optimized for AMD GPUs with ROCm support
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
//! Windows-native platform support.
//!
//! Provides cfg-gated Windows implementations that compile cleanly on the
//! `x86_64-pc-windows-msvc` target without any Unix-only dependencies.
//!
//! # Platform Detection
//!
//! - `detect_backend_mode()` — classifies the current platform as
//!   `LinuxNative`, `WslBackedLinux`, or `WindowsNative`
//!
//! # Backend Routing
//!
//! - [`BackendRouter`] — selects the correct execution backend per component
//!   based on OS and WSL2 state (VAL-WIN-007)
//! - Unsupported components produce clear messages without attempting install
//!   (VAL-WIN-008)
//!
//! # Component Routing Matrix
//!
//! | Component     | Linux Native      | WSL-backed Linux  | Windows Native    |
//! |---------------|-------------------|--------------------|-------------------|
//! | rocm          | LegacyScript      | LegacyScript       | Unsupported       |
//! | pytorch       | LegacyScript      | LegacyScript       | Unsupported       |
//! | triton        | LegacyScript      | LegacyScript       | Unsupported       |
//! | deepspeed     | LegacyScript      | LegacyScript       | Unsupported       |
//! | vllm          | LegacyScript      | LegacyScript       | Unsupported       |
//! | aiter         | LegacyScript      | LegacyScript       | Unsupported       |
//! | onnx          | LegacyScript      | LegacyScript       | Unsupported       |
//! | bitsandbytes  | LegacyScript      | LegacyScript       | Unsupported       |
//! | migraphx      | LegacyScript      | LegacyScript       | Unsupported       |
//! | flash-attn    | LegacyScript      | LegacyScript       | Unsupported       |
//! | mpi4py        | LegacyScript      | LegacyScript       | Unsupported       |
//! | wandb         | ExternalPackageManager | ExternalPackageManager | ExternalPackageManager |
//! | comfyui       | LegacyScript      | LegacyScript       | Unsupported       |
//! | vllm-studio   | LegacyScript      | LegacyScript       | Unsupported       |
//! | textgen       | LegacyScript      | LegacyScript       | Unsupported       |
//! | rocm-smi      | LegacyScript      | LegacyScript       | Unsupported       |
//! | permanent-env | LegacyScript      | LegacyScript       | Unsupported       |

use crate::core::types::{BackendMode, ExecutorKind};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ===========================================================================
// Backend Router
// ===========================================================================

/// Routes components to appropriate execution backends based on platform.
///
/// The router considers the [`BackendMode`] (Linux native, WSL-backed, or
/// Windows native) and selects the best [`ExecutorKind`] for each component.
///
/// # Examples
///
/// ```
/// use rusty_stack::platform::BackendRouter;
/// use rusty_stack::core::types::{BackendMode, ExecutorKind};
///
/// let router = BackendRouter::new(BackendMode::LinuxNative);
/// assert_ne!(router.executor_for("rocm"), ExecutorKind::Unsupported);
///
/// let win_router = BackendRouter::new(BackendMode::WindowsNative);
/// assert_eq!(win_router.executor_for("rocm"), ExecutorKind::Unsupported);
/// ```
#[derive(Debug, Clone)]
pub struct BackendRouter {
    mode: BackendMode,
    /// Component-specific overrides (component_id -> ExecutorKind).
    overrides: HashMap<String, ExecutorKind>,
}

impl BackendRouter {
    /// Create a new router for the given backend mode.
    pub fn new(mode: BackendMode) -> Self {
        Self {
            mode,
            overrides: HashMap::new(),
        }
    }

    /// Set an executor override for a specific component.
    pub fn set_override(&mut self, component_id: &str, kind: ExecutorKind) {
        self.overrides.insert(component_id.to_string(), kind);
    }

    /// Get the appropriate executor kind for the given component.
    ///
    /// Returns [`ExecutorKind::Unsupported`] for unknown components.
    pub fn executor_for(&self, component_id: &str) -> ExecutorKind {
        // Check overrides first
        if let Some(kind) = self.overrides.get(component_id) {
            return *kind;
        }

        // Check if the component is known
        if !is_known_component(component_id) {
            return ExecutorKind::Unsupported;
        }

        // Route based on backend mode and component
        match self.mode {
            BackendMode::LinuxNative => self.route_linux_native(component_id),
            BackendMode::WslBackedLinux => self.route_wsl_backed(component_id),
            BackendMode::WindowsNative => self.route_windows_native(component_id),
        }
    }

    /// Get the current backend mode.
    pub fn mode(&self) -> BackendMode {
        self.mode
    }

    /// Get a human-readable message explaining why a component is unsupported.
    ///
    /// Returns an empty string if the component is not unsupported.
    pub fn unsupported_message(&self, component_id: &str) -> String {
        let kind = self.executor_for(component_id);
        if kind != ExecutorKind::Unsupported {
            return String::new();
        }

        if !is_known_component(component_id) {
            return format!(
                "Component '{}' is not recognized by Rusty Stack.",
                component_id
            );
        }

        match self.mode {
            BackendMode::WindowsNative => format!(
                "Component '{}' requires a Linux environment with ROCm support. \
                 It cannot be installed natively on Windows. \
                 Consider using WSL2 with ROCm to enable Linux GPU computing on Windows.",
                display_name(component_id)
            ),
            BackendMode::WslBackedLinux => format!(
                "Component '{}' is not supported in the current WSL2 configuration.",
                display_name(component_id)
            ),
            BackendMode::LinuxNative => format!(
                "Component '{}' is not supported on this Linux configuration.",
                display_name(component_id)
            ),
        }
    }

    /// Route component on Linux native.
    fn route_linux_native(&self, component_id: &str) -> ExecutorKind {
        // On Linux native, all known ML components use legacy scripts
        // Python-only tools could use ExternalPackageManager
        match component_id {
            "wandb" => ExecutorKind::ExternalPackageManager,
            _ => ExecutorKind::LegacyScript,
        }
    }

    /// Route component on WSL-backed Linux.
    fn route_wsl_backed(&self, component_id: &str) -> ExecutorKind {
        // WSL-backed Linux can run the same scripts as native Linux
        match component_id {
            "wandb" => ExecutorKind::ExternalPackageManager,
            _ => ExecutorKind::LegacyScript,
        }
    }

    /// Route component on Windows native.
    fn route_windows_native(&self, component_id: &str) -> ExecutorKind {
        // On Windows native, only pure Python packages work
        match component_id {
            // Pure Python packages that work on Windows
            "wandb" => ExecutorKind::ExternalPackageManager,
            // Everything else requires Linux/ROCm
            _ => ExecutorKind::Unsupported,
        }
    }
}

// ===========================================================================
// Platform Detection
// ===========================================================================

/// Detect the current platform's backend mode.
///
/// Returns:
/// - `LinuxNative` on Linux without WSL indicators
/// - `WslBackedLinux` on Linux with WSL indicators (running inside WSL)
/// - `WindowsNative` on Windows
pub fn detect_backend_mode() -> BackendMode {
    #[cfg(target_os = "windows")]
    {
        BackendMode::WindowsNative
    }

    #[cfg(not(target_os = "windows"))]
    {
        if is_wsl_environment() {
            BackendMode::WslBackedLinux
        } else {
            BackendMode::LinuxNative
        }
    }
}

/// Check if the current Linux environment is running under WSL.
#[cfg(not(target_os = "windows"))]
fn is_wsl_environment() -> bool {
    if let Ok(version) = std::fs::read_to_string("/proc/version") {
        let version_lower = version.to_lowercase();
        return version_lower.contains("microsoft") || version_lower.contains("wsl");
    }
    false
}

// ===========================================================================
// Component Registry Helpers
// ===========================================================================

/// Check if a component ID is known.
fn is_known_component(id: &str) -> bool {
    matches!(
        id,
        "rocm"
            | "pytorch"
            | "triton"
            | "deepspeed"
            | "vllm"
            | "aiter"
            | "onnx"
            | "bitsandbytes"
            | "migraphx"
            | "flash-attn"
            | "mpi4py"
            | "wandb"
            | "comfyui"
            | "vllm-studio"
            | "textgen"
            | "rocm-smi"
            | "permanent-env"
    )
}

/// Get display name for a component.
fn display_name(id: &str) -> &str {
    match id {
        "rocm" => "ROCm",
        "pytorch" => "PyTorch",
        "triton" => "Triton",
        "deepspeed" => "DeepSpeed",
        "vllm" => "vLLM",
        "aiter" => "AITER",
        "onnx" => "ONNX Runtime",
        "bitsandbytes" => "bitsandbytes",
        "migraphx" => "MIGraphX",
        "flash-attn" => "Flash Attention",
        "mpi4py" => "MPI4Py",
        "wandb" => "Weights & Biases",
        "comfyui" => "ComfyUI",
        "vllm-studio" => "vLLM Studio",
        "textgen" => "Text Generation WebUI",
        "rocm-smi" => "ROCm SMI",
        "permanent-env" => "Permanent Environment",
        _ => id,
    }
}

// ===========================================================================
// Windows-Native Platform Info
// ===========================================================================

/// Windows-specific platform information.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct WindowsInfo {
    /// Windows version string (e.g., "10.0.19045").
    pub version: String,
    /// Windows build number.
    pub build: u32,
    /// Whether WSL2 is available.
    pub wsl2_available: bool,
    /// Whether the system has a supported AMD GPU.
    pub has_amd_gpu: bool,
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // ---- Backend Router Tests ----

    #[test]
    fn test_router_linux_native_rocm() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        assert_eq!(router.executor_for("rocm"), ExecutorKind::LegacyScript);
    }

    #[test]
    fn test_router_linux_native_all_components() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        let components = [
            "rocm",
            "pytorch",
            "triton",
            "deepspeed",
            "vllm",
            "aiter",
            "onnx",
            "bitsandbytes",
            "migraphx",
            "flash-attn",
            "mpi4py",
            "comfyui",
            "vllm-studio",
            "textgen",
            "rocm-smi",
            "permanent-env",
        ];
        for c in &components {
            assert_ne!(
                router.executor_for(c),
                ExecutorKind::Unsupported,
                "{} should not be unsupported on Linux",
                c
            );
        }
    }

    #[test]
    fn test_router_linux_native_wandb() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        assert_eq!(
            router.executor_for("wandb"),
            ExecutorKind::ExternalPackageManager
        );
    }

    #[test]
    fn test_router_windows_native_rocm_unsupported() {
        let router = BackendRouter::new(BackendMode::WindowsNative);
        assert_eq!(router.executor_for("rocm"), ExecutorKind::Unsupported);
    }

    #[test]
    fn test_router_windows_native_all_gpu_unsupported() {
        let router = BackendRouter::new(BackendMode::WindowsNative);
        let gpu_components = [
            "rocm",
            "pytorch",
            "triton",
            "deepspeed",
            "vllm",
            "aiter",
            "onnx",
            "bitsandbytes",
            "migraphx",
            "flash-attn",
            "mpi4py",
            "comfyui",
            "vllm-studio",
            "textgen",
            "rocm-smi",
            "permanent-env",
        ];
        for c in &gpu_components {
            assert_eq!(
                router.executor_for(c),
                ExecutorKind::Unsupported,
                "{} should be unsupported on Windows native",
                c
            );
        }
    }

    #[test]
    fn test_router_windows_native_wandb() {
        let router = BackendRouter::new(BackendMode::WindowsNative);
        assert_eq!(
            router.executor_for("wandb"),
            ExecutorKind::ExternalPackageManager
        );
    }

    #[test]
    fn test_router_wsl_backed_rocm() {
        let router = BackendRouter::new(BackendMode::WslBackedLinux);
        assert_eq!(router.executor_for("rocm"), ExecutorKind::LegacyScript);
    }

    #[test]
    fn test_router_unknown_component() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        assert_eq!(
            router.executor_for("nonexistent"),
            ExecutorKind::Unsupported
        );
    }

    #[test]
    fn test_router_override() {
        let mut router = BackendRouter::new(BackendMode::LinuxNative);
        router.set_override("rocm", ExecutorKind::Rust);
        assert_eq!(router.executor_for("rocm"), ExecutorKind::Rust);
    }

    // ---- Unsupported Message Tests ----

    #[test]
    fn test_unsupported_message_windows_rocm() {
        let router = BackendRouter::new(BackendMode::WindowsNative);
        let msg = router.unsupported_message("rocm");
        assert!(!msg.is_empty());
        assert!(msg.contains("ROCm"));
        assert!(msg.contains("Windows") || msg.contains("WSL"));
    }

    #[test]
    fn test_unsupported_message_unknown_component() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        let msg = router.unsupported_message("nonexistent");
        assert!(msg.contains("not recognized"));
    }

    #[test]
    fn test_unsupported_message_supported_component_empty() {
        let router = BackendRouter::new(BackendMode::LinuxNative);
        let msg = router.unsupported_message("rocm");
        assert!(
            msg.is_empty(),
            "Supported component should return empty message"
        );
    }

    // ---- Platform Detection Tests ----

    #[test]
    fn test_detect_backend_mode() {
        let mode = detect_backend_mode();
        // On this Linux system, should be LinuxNative or WslBackedLinux
        match mode {
            BackendMode::LinuxNative | BackendMode::WslBackedLinux => {}
            BackendMode::WindowsNative => {
                panic!("Should not detect WindowsNative on Linux")
            }
        }
    }

    // ---- Component Registry Tests ----

    #[test]
    fn test_is_known_component_all_17() {
        let known = [
            "rocm",
            "pytorch",
            "triton",
            "deepspeed",
            "vllm",
            "aiter",
            "onnx",
            "bitsandbytes",
            "migraphx",
            "flash-attn",
            "mpi4py",
            "wandb",
            "comfyui",
            "vllm-studio",
            "textgen",
            "rocm-smi",
            "permanent-env",
        ];
        for c in &known {
            assert!(is_known_component(c), "{} should be known", c);
        }
    }

    #[test]
    fn test_is_known_component_unknowns() {
        let unknowns = ["foo", "bar", "cuda", "nvidia", ""];
        for c in &unknowns {
            assert!(!is_known_component(c), "{} should be unknown", c);
        }
    }

    #[test]
    fn test_display_name_known() {
        assert_eq!(display_name("rocm"), "ROCm");
        assert_eq!(display_name("pytorch"), "PyTorch");
        assert_eq!(display_name("wandb"), "Weights & Biases");
    }

    #[test]
    fn test_display_name_unknown_passthrough() {
        assert_eq!(display_name("custom-component"), "custom-component");
    }

    // ---- WindowsInfo Serde Tests ----

    #[test]
    fn test_windows_info_serde_roundtrip() {
        let info = WindowsInfo {
            version: "10.0.19045".to_string(),
            build: 19045,
            wsl2_available: true,
            has_amd_gpu: true,
        };
        let json = serde_json::to_string(&info).unwrap();
        let back: WindowsInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(info, back);
    }

    #[test]
    fn test_windows_info_default() {
        let info = WindowsInfo::default();
        assert!(info.version.is_empty());
        assert_eq!(info.build, 0);
        assert!(!info.wsl2_available);
        assert!(!info.has_amd_gpu);
    }

    // ---- Router Mode Accessor ----

    #[test]
    fn test_router_mode() {
        let router = BackendRouter::new(BackendMode::WindowsNative);
        assert_eq!(router.mode(), BackendMode::WindowsNative);
    }
}