rusty-stack 0.3.1

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
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
//! Migration Wave 1 — Rust equivalents of shell logic.
//!
//! This module provides consolidated Rust implementations of the read-heavy
//! shell logic from `update_helper.sh` and `update_stack.sh`:
//!
//! - **Version lookup** — `up_get_version()` equivalent (VAL-MIGR-001, VAL-MIGR-002)
//! - **Selection plan** — `update_stack.sh` menu logic (VAL-MIGR-003, VAL-MIGR-004)
//! - **Manifest freshness** — TTL and offline tolerance (VAL-MIGR-005, VAL-MIGR-006)
//! - **Consolidated detection** — `up_detect_installed()` equivalent (VAL-MIGR-007)
//! - **Partial installation** — recognition of partially installed state (VAL-MIGR-008)
//!
//! # Shell Parity
//!
//! Each function in this module is designed to produce identical output to its
//! shell counterpart. The tests in `tests/migration_wave1.rs` verify this parity.

use crate::core::manifest::{self, Manifest, ManifestFetcher, ManifestSource, ResolvedManifest};
use crate::orchestrator::planner::{CompatibilityContext, PlannerOptions, UpdatePlanner};
use crate::platform::registry;

use std::collections::{HashMap, HashSet};
use std::path::Path;

// ===========================================================================
// Version Lookup (VAL-MIGR-001, VAL-MIGR-002)
// ===========================================================================

/// Rust equivalent of `up_get_version()` from `update_helper.sh`.
///
/// Returns the version string for a component, matching the shell behavior:
/// - ROCm: version from `/opt/rocm/.info/version` or `rocminfo --version`
/// - Python modules: `__version__` attribute or "not installed"
/// - Git-based: `git log -1 --format="%h %s"` or "installed" or "not installed"
/// - Command-based: first line of `--version` output or "not installed"
/// - permanent-env: "installed" or "not installed"
/// - Unknown: "unknown"
///
/// # Errors
///
/// Unknown component IDs return "unknown" (structured, not panic).
/// This matches the shell's `*) echo "unknown" ;;` default case.
pub fn migrated_get_version(component_id: &str) -> String {
    registry::get_version(component_id)
}

/// Rust equivalent of `up_get_version()` with explicit home directory.
///
/// Same as `migrated_get_version` but uses the provided home directory
/// for git-based and path-based components.
pub fn migrated_get_version_with_home(component_id: &str, home: &Path) -> String {
    registry::get_version_with_home(component_id, home)
}

/// Rust equivalent of `up_get_versions_batch()` from `update_helper.sh`.
///
/// Queries versions for multiple Python ML modules in a single subprocess.
/// Returns `component_id=version` pairs.
///
/// # Differences from Shell
///
/// The shell function takes the python interpreter as the first argument.
/// This Rust version auto-discovers interpreters via `python_interpreters()`.
pub fn migrated_get_versions_batch(component_ids: &[&str]) -> Vec<registry::VersionInfo> {
    registry::get_versions_batch(component_ids)
}

// ===========================================================================
// Selection Plan (VAL-MIGR-003, VAL-MIGR-004)
// ===========================================================================

/// Rust equivalent of the `update_stack.sh` menu construction logic.
///
/// Builds a selection plan from the manifest and current system state.
/// The plan includes:
/// - Ordered list of components (matching shell menu order)
/// - Preselected defaults (safe updates preselected, others not)
/// - Classification (safe/guarded/blocked/candidate/experimental)
///
/// # Channel Support
///
/// When the ROCm channel changes, calling this again produces a fresh plan
/// with no stale entries from the previous channel.
pub fn migrated_build_selection_plan(
    manifest: &Manifest,
    context: &CompatibilityContext,
    options: &PlannerOptions,
) -> Result<Vec<PlannerItem>, crate::orchestrator::planner::PlannerError> {
    let planner = UpdatePlanner::new();
    planner.build_plan(manifest, context, options)
}

// Re-export PlannerItem for convenience
pub use crate::orchestrator::planner::PlannerItem;

/// Build a selection plan with automatic system detection.
///
/// This is the highest-level entry point that combines:
/// 1. Hardware detection
/// 2. Component detection
/// 3. Version querying
/// 4. Plan construction
///
/// Returns the plan items ready for display to the user.
pub fn build_plan_from_system(
    manifest: &Manifest,
    rocm_channel: &str,
    include_experimental: bool,
) -> Result<Vec<PlannerItem>, crate::orchestrator::planner::PlannerError> {
    // Detect installed components
    let installed_ids = registry::detect_all_installed();
    let mut installed_versions = HashMap::new();
    let mut installed_set = HashSet::new();

    for id in &installed_ids {
        let v = registry::get_version(id);
        installed_versions.insert(id.clone(), v);
        installed_set.insert(id.clone());
    }

    // Detect ROCm version
    let rocm_version = installed_versions.get("rocm").cloned().unwrap_or_default();

    // Build context
    let context = CompatibilityContext {
        rocm_version,
        rocm_channel: rocm_channel.to_string(),
        available_executors: HashSet::from([
            crate::core::types::ExecutorKind::LegacyScript,
            crate::core::types::ExecutorKind::Rust,
        ]),
        installed_components: installed_set,
        installed_versions,
        gpu_architecture: String::new(),
        runtime_version: env!("CARGO_PKG_VERSION").to_string(),
    };

    let options = PlannerOptions {
        include_experimental,
        ..PlannerOptions::default()
    };

    migrated_build_selection_plan(manifest, &context, &options)
}

// ===========================================================================
// Manifest Freshness (VAL-MIGR-005, VAL-MIGR-006)
// ===========================================================================

/// Result of a manifest freshness check.
#[derive(Debug, Clone)]
pub struct FreshnessResult {
    /// Whether the manifest is fresh (not stale).
    pub is_fresh: bool,
    /// Source of the manifest.
    pub source: ManifestSource,
    /// Warning message if the manifest is stale but usable.
    pub warning: Option<String>,
    /// Whether a re-fetch was triggered.
    pub refetch_triggered: bool,
}

/// Check manifest freshness and resolve using the fallback chain.
///
/// This is the Rust equivalent of the shell manifest freshness logic:
/// 1. Try to fetch fresh remote manifest
/// 2. If TTL expired, trigger re-fetch
/// 3. If offline, use cached manifest with warning
/// 4. If all else fails, use bundled baseline
///
/// # VAL-MIGR-005: Cache Expiry
///
/// If the cached manifest's TTL has expired, a re-fetch is triggered.
/// The `refetch_triggered` field will be `true`.
///
/// # VAL-MIGR-006: Offline Tolerance
///
/// If the network is unavailable but a stale manifest exists, the operation
/// proceeds with a warning. The `warning` field will contain the message.
pub fn check_manifest_freshness(fetcher: &dyn ManifestFetcher) -> FreshnessResult {
    // Try to fetch fresh remote
    if let Some(remote) = fetcher.fetch_remote() {
        if remote.verify_trust().is_ok() {
            return FreshnessResult {
                is_fresh: true,
                source: ManifestSource::FreshRemote,
                warning: None,
                refetch_triggered: false,
            };
        }
        // Fresh fetch succeeded but trust check failed
        // This triggers a re-fetch attempt (which would also fail)
    }

    // Try cached manifest
    if let Some(_cached) = fetcher.load_cached() {
        return FreshnessResult {
            is_fresh: false,
            source: ManifestSource::CachedRemote,
            warning: Some(
                "Using cached manifest (remote unavailable or trust check failed)".to_string(),
            ),
            refetch_triggered: true,
        };
    }

    // Fall back to baseline
    FreshnessResult {
        is_fresh: false,
        source: ManifestSource::Baseline,
        warning: Some("Using bundled baseline manifest (no remote, no cache)".to_string()),
        refetch_triggered: true,
    }
}

/// Resolve manifest with freshness checking.
///
/// Combines freshness check with actual manifest resolution.
/// Returns the resolved manifest and freshness information.
pub fn resolve_with_freshness(
    fetcher: &dyn ManifestFetcher,
) -> (ResolvedManifest, FreshnessResult) {
    let freshness = check_manifest_freshness(fetcher);
    let resolved = manifest::resolve_manifest(fetcher);
    (resolved, freshness)
}

// ===========================================================================
// Consolidated Detection (VAL-MIGR-007, VAL-MIGR-008)
// ===========================================================================

/// Installation status of a component, with partial installation recognition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComponentInstallStatus {
    /// Fully installed and functional.
    Installed,
    /// Not installed at all.
    NotInstalled,
    /// Partially installed — needs update to complete installation.
    NeedsUpdate,
}

/// Detection result for a single component.
#[derive(Debug, Clone)]
pub struct DetectionResult {
    /// Component ID.
    pub component_id: String,
    /// Installation status.
    pub status: ComponentInstallStatus,
    /// Version string (if installed).
    pub version: String,
    /// Detection method used.
    pub detection_method: String,
}

/// Rust equivalent of `up_detect_installed()` from `update_helper.sh`.
///
/// Detects all installed components using the consolidated detection pipeline.
/// This matches the shell's `up_detect_installed` function which:
/// 1. Checks ROCm via version file / rocminfo
/// 2. Detects Python modules in a single subprocess
/// 3. Checks git-based components
/// 4. Checks system tools (rocm-smi)
/// 5. Checks permanent-env
///
/// # VAL-MIGR-007: Consolidated Detector
///
/// The detection results must match the shell for all components.
pub fn migrated_detect_all() -> Vec<DetectionResult> {
    let home = crate::platform::environment::resolve_user_home();
    migrated_detect_all_with_home(&home)
}

/// Detect all installed components with explicit home directory (testable).
pub fn migrated_detect_all_with_home(home: &Path) -> Vec<DetectionResult> {
    let installed_ids = registry::detect_all_installed_with_home(home);
    let mut results = Vec::new();

    for info in registry::known_components() {
        let version = registry::get_version_with_home(&info.id, home);
        let is_installed = installed_ids.contains(&info.id);

        let status = if is_installed {
            ComponentInstallStatus::Installed
        } else {
            check_partial_installation(&info.id, home)
        };

        results.push(DetectionResult {
            component_id: info.id.clone(),
            status,
            version,
            detection_method: format!("{:?}", info.detection_method),
        });
    }

    results
}

/// Detect a single component's installation status.
///
/// # VAL-MIGR-008: Partial Installation Recognition
///
/// Partially installed components are reported as `NeedsUpdate`,
/// not `Installed` or `NotInstalled`.
pub fn detect_component_status(component_id: &str) -> DetectionResult {
    let home = crate::platform::environment::resolve_user_home();
    detect_component_status_with_home(component_id, &home)
}

/// Detect a single component's status with explicit home directory.
pub fn detect_component_status_with_home(component_id: &str, home: &Path) -> DetectionResult {
    let info = registry::get_component(component_id);
    let is_installed = registry::is_component_installed_with_home(component_id, home);
    let version = registry::get_version_with_home(component_id, home);

    let status = if is_installed {
        ComponentInstallStatus::Installed
    } else {
        check_partial_installation(component_id, home)
    };

    DetectionResult {
        component_id: component_id.to_string(),
        status,
        version,
        detection_method: info
            .map(|i| format!("{:?}", i.detection_method))
            .unwrap_or_else(|| "Unknown".to_string()),
    }
}

/// Check if a component is partially installed.
///
/// A component is "partially installed" when some indicators of installation
/// are present but the component is not fully functional. Examples:
/// - ROCm: version file exists but rocminfo doesn't work
/// - Git-based: directory exists but no `.git` directory
/// - permanent-env: file exists but missing required markers
///
/// Returns `NeedsUpdate` if partially installed, `NotInstalled` otherwise.
fn check_partial_installation(component_id: &str, home: &Path) -> ComponentInstallStatus {
    match component_id {
        "rocm" => {
            // Partial: version file exists but rocminfo doesn't work
            let version_file = Path::new("/opt/rocm/.info/version").exists();
            let rocminfo_works = std::process::Command::new("rocminfo")
                .arg("--version")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            if version_file && !rocminfo_works {
                ComponentInstallStatus::NeedsUpdate
            } else {
                ComponentInstallStatus::NotInstalled
            }
        }
        "comfyui" | "vllm-studio" | "textgen" => {
            // Partial: directory exists but no .git
            let clone_dir = match component_id {
                "comfyui" => "ComfyUI",
                "vllm-studio" => "vllm-studio",
                "textgen" => "text-generation-webui",
                _ => return ComponentInstallStatus::NotInstalled,
            };
            let dir = home.join(clone_dir);
            if dir.exists() && !dir.join(".git").exists() {
                ComponentInstallStatus::NeedsUpdate
            } else {
                ComponentInstallStatus::NotInstalled
            }
        }
        "permanent-env" => {
            // Partial: file exists but missing marker
            let env_file = home.join(".mlstack_env");
            if env_file.exists() {
                let content = std::fs::read_to_string(&env_file).unwrap_or_default();
                if !content.contains("MLSTACK_PYTHON_BIN") {
                    ComponentInstallStatus::NeedsUpdate
                } else {
                    ComponentInstallStatus::NotInstalled
                }
            } else {
                ComponentInstallStatus::NotInstalled
            }
        }
        _ => ComponentInstallStatus::NotInstalled,
    }
}

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

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

    // -----------------------------------------------------------------------
    // VAL-MIGR-001: Version Lookup — Shell Parity
    // -----------------------------------------------------------------------

    #[test]
    fn test_migrated_get_version_all_components_no_panic() {
        let ids = [
            "rocm",
            "pytorch",
            "triton",
            "deepspeed",
            "vllm",
            "aiter",
            "onnx",
            "bitsandbytes",
            "migraphx",
            "flash-attn",
            "mpi4py",
            "wandb",
            "comfyui",
            "vllm-studio",
            "textgen",
            "rocm-smi",
            "permanent-env",
        ];

        for id in &ids {
            let v = migrated_get_version(id);
            assert!(!v.is_empty(), "version for '{id}' must not be empty");
        }
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-002: Unknown Component Graceful Handling
    // -----------------------------------------------------------------------

    #[test]
    fn test_migrated_get_version_unknown_returns_unknown() {
        assert_eq!(migrated_get_version("nonexistent"), "unknown");
        assert_eq!(migrated_get_version("fake-component"), "unknown");
        assert_eq!(migrated_get_version(""), "unknown");
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-007: Consolidated Detection
    // -----------------------------------------------------------------------

    #[test]
    fn test_migrated_detect_all_returns_valid_results() {
        let results = migrated_detect_all();

        // All results must have valid component IDs
        for result in &results {
            assert!(
                registry::is_known_component(&result.component_id),
                "result component must be known: {}",
                result.component_id
            );
        }
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-008: Partial Installation Recognition
    // -----------------------------------------------------------------------

    #[test]
    fn test_partial_installation_detection_with_temp_dir() {
        let temp_dir = tempfile::tempdir().unwrap();
        let home = temp_dir.path();

        // Test git-based partial: directory exists, no .git
        let comfyui_dir = home.join("ComfyUI");
        std::fs::create_dir_all(&comfyui_dir).unwrap();

        let result = detect_component_status_with_home("comfyui", home);
        assert_eq!(
            result.status,
            ComponentInstallStatus::NeedsUpdate,
            "ComfyUI dir without .git must be NeedsUpdate"
        );

        // Complete the installation
        std::fs::create_dir_all(comfyui_dir.join(".git")).unwrap();
        let result = detect_component_status_with_home("comfyui", home);
        assert_eq!(
            result.status,
            ComponentInstallStatus::Installed,
            "ComfyUI with .git must be Installed"
        );
    }

    #[test]
    fn test_partial_installation_permanent_env() {
        let temp_dir = tempfile::tempdir().unwrap();
        let home = temp_dir.path();

        // No file at all
        let result = detect_component_status_with_home("permanent-env", home);
        assert_eq!(result.status, ComponentInstallStatus::NotInstalled);

        // File exists but no marker
        let env_file = home.join(".mlstack_env");
        std::fs::write(&env_file, "# empty").unwrap();
        let result = detect_component_status_with_home("permanent-env", home);
        assert_eq!(
            result.status,
            ComponentInstallStatus::NeedsUpdate,
            "env file without marker must be NeedsUpdate"
        );

        // File with marker
        std::fs::write(&env_file, "export MLSTACK_PYTHON_BIN=/usr/bin/python3").unwrap();
        let result = detect_component_status_with_home("permanent-env", home);
        assert_eq!(
            result.status,
            ComponentInstallStatus::Installed,
            "env file with marker must be Installed"
        );
    }

    // -----------------------------------------------------------------------
    // VAL-MIGR-005/006: Manifest Freshness
    // -----------------------------------------------------------------------

    struct MockFetcher {
        remote: Option<Manifest>,
        cached: Option<Manifest>,
    }

    impl ManifestFetcher for MockFetcher {
        fn fetch_remote(&self) -> Option<Manifest> {
            self.remote.clone()
        }
        fn load_cached(&self) -> Option<Manifest> {
            self.cached.clone()
        }
    }

    fn make_signed_manifest() -> Manifest {
        let mut m = Manifest {
            schema_version: manifest::CURRENT_SCHEMA_VERSION,
            sequence: 1,
            generated_at: chrono::Utc::now().to_rfc3339(),
            expires_at: None,
            min_runtime_version: String::new(),
            components: vec![],
            signature: None,
        };
        m.signature = Some(m.compute_signature());
        m
    }

    #[test]
    fn test_freshness_check_fresh_remote() {
        let fetcher = MockFetcher {
            remote: Some(make_signed_manifest()),
            cached: None,
        };
        let result = check_manifest_freshness(&fetcher);
        assert!(result.is_fresh);
        assert_eq!(result.source, ManifestSource::FreshRemote);
        assert!(result.warning.is_none());
    }

    #[test]
    fn test_freshness_check_offline_with_cache() {
        let fetcher = MockFetcher {
            remote: None,
            cached: Some(make_signed_manifest()),
        };
        let result = check_manifest_freshness(&fetcher);
        assert!(!result.is_fresh);
        assert_eq!(result.source, ManifestSource::CachedRemote);
        assert!(result.warning.is_some());
        assert!(result.refetch_triggered);
    }

    #[test]
    fn test_freshness_check_offline_no_cache() {
        let fetcher = MockFetcher {
            remote: None,
            cached: None,
        };
        let result = check_manifest_freshness(&fetcher);
        assert!(!result.is_fresh);
        assert_eq!(result.source, ManifestSource::Baseline);
        assert!(result.warning.is_some());
    }
}