osvm 0.8.3

OpenSVM CLI tool for managing SVM nodes and deployments
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
//! OSVM Self-Repair System
//!
//! This module provides comprehensive self-repair functionality for OSVM CLI,
//! including automatic dependency detection, system repairs, and rollback capabilities.

use serde::{Deserialize, Serialize};
use std::error::Error as StdError;
use std::fmt;
use tokio::time::Duration;

pub mod package_managers;
pub mod repair_strategies;
// pub mod snapshots;  // REMOVED
pub mod system_deps;
pub mod user_deps;

/// Main error type for self-repair operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RepairError {
    /// System-level dependency issues
    SystemDependency(String),
    /// User-level dependency issues
    UserDependency(String),
    /// Network connectivity problems
    NetworkError(String),
    /// Permission-related errors
    PermissionError(String),
    /// Rollback operation failures
    RollbackError(String),
    /// Snapshot operation failures - REMOVED
    // SnapshotError(String),
    /// Validation failures
    ValidationError(String),
    /// Unknown or unexpected errors
    Unknown(String),
}

impl fmt::Display for RepairError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RepairError::SystemDependency(msg) => write!(f, "System dependency error: {}", msg),
            RepairError::UserDependency(msg) => write!(f, "User dependency error: {}", msg),
            RepairError::NetworkError(msg) => write!(f, "Network error: {}", msg),
            RepairError::PermissionError(msg) => write!(f, "Permission error: {}", msg),
            RepairError::RollbackError(msg) => write!(f, "Rollback error: {}", msg),
            // RepairError::SnapshotError(msg) => write!(f, "Snapshot error: {}", msg),
            RepairError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
            RepairError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
        }
    }
}

impl StdError for RepairError {}

/// Types of errors that can be automatically repaired
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum RepairableError {
    // System-level issues
    OutdatedSystemPackages,
    MissingBuildTools,
    OutdatedRustToolchain,
    MissingSystemDependencies(Vec<String>),

    // User-level issues
    MissingSolanaCli,
    OutdatedSolanaCli,
    MissingKeypair(String),
    InvalidConfig,
    MissingConfigDirectory,

    // Network-level issues
    ConnectivityIssues,
    RpcEndpointFailure(String),

    // Permission issues
    InsufficientPermissions(String),

    // System tuning issues
    SystemTuningRequired,
}

/// Result of a repair operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RepairResult {
    Success(String),
    Partial(String, Vec<RepairableError>),
    Failed(RepairError),
    RequiresManualIntervention(String),
}

/// Configuration for repair operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepairConfig {
    /// Whether to prompt user for confirmation before repairs
    pub interactive: bool,
    /// Automatically create snapshots before repairs - REMOVED
    // pub auto_snapshot: bool,
    /// Maximum time to wait for operations
    pub timeout: Duration,
    /// Whether to perform system-level repairs
    pub allow_system_repairs: bool,
    /// Whether to perform user-level repairs
    pub allow_user_repairs: bool,
    // pub backup_dir: Option<String>,  // REMOVED: Backup directory for snapshots
}

impl Default for RepairConfig {
    fn default() -> Self {
        Self {
            interactive: true,
            // auto_snapshot: true,  // REMOVED
            timeout: Duration::from_secs(300), // 5 minutes
            allow_system_repairs: true,
            allow_user_repairs: true,
            // backup_dir: None,  // REMOVED
        }
    }
}

/// Main self-repair orchestrator
pub struct SelfRepairSystem {
    config: RepairConfig,
    diagnostics: crate::utils::diagnostics::DiagnosticCoordinator,
}

impl SelfRepairSystem {
    /// Create a new self-repair system
    pub fn new(config: RepairConfig) -> Self {
        Self {
            config,
            diagnostics: crate::utils::diagnostics::DiagnosticCoordinator::new(),
        }
    }

    /// Create a new self-repair system with default configuration
    pub fn with_default_config() -> Self {
        Self::new(RepairConfig::default())
    }

    /// Analyze an error and determine if it can be automatically repaired
    pub async fn analyze_error(&self, error: &str) -> Result<Vec<RepairableError>, RepairError> {
        let mut repairable_errors = Vec::new();

        // Check for system tuning errors
        if error.contains("net.core.rmem_max") && error.contains("too small") {
            repairable_errors.push(RepairableError::SystemTuningRequired);
        }

        // Check for keypair file errors
        if error.contains("Error reading keypair file")
            && error.contains("No such file or directory")
        {
            // Extract the keypair path from the error message
            if let Some(path) = extract_keypair_path(error) {
                repairable_errors.push(RepairableError::MissingKeypair(path));
            }

            // Check if this is due to missing Solana CLI
            if !self.is_solana_cli_installed().await? {
                repairable_errors.push(RepairableError::MissingSolanaCli);
            }

            // Check if config directory exists
            if !self.config_directory_exists().await? {
                repairable_errors.push(RepairableError::MissingConfigDirectory);
            }
        }

        // Check for other system-level issues
        let system_health = self
            .diagnostics
            .check_system_health()
            .await
            .map_err(|e| RepairError::ValidationError(e.to_string()))?;

        // Check if system tuning is needed based on health check
        if system_health.issues.iter().any(|issue| {
            issue.title.contains("System tuning") || issue.description.contains("kernel parameter")
        }) {
            repairable_errors.push(RepairableError::SystemTuningRequired);
        }

        if system_health.has_package_updates() {
            repairable_errors.push(RepairableError::OutdatedSystemPackages);
        }

        if system_health.has_rust_updates() {
            repairable_errors.push(RepairableError::OutdatedRustToolchain);
        }

        if let Some(missing_deps) = system_health.missing_build_tools() {
            if !missing_deps.is_empty() {
                repairable_errors.push(RepairableError::MissingSystemDependencies(missing_deps));
            }
        }

        Ok(repairable_errors)
    }

    /// Attempt to repair the detected issues automatically
    pub async fn repair_automatically(
        &self,
        errors: Vec<RepairableError>,
    ) -> Result<RepairResult, RepairError> {
        if errors.is_empty() {
            return Ok(RepairResult::Success("No issues detected".to_string()));
        }

        // Create snapshot if configured
        // DISABLED: Commenting out snapshot creation as requested
        // let snapshot_id = if self.config.auto_snapshot {
        //     Some(self.create_system_snapshot().await?)
        // } else {
        //     None
        // };
        let snapshot_id: Option<String> = None;

        let mut repair_transaction = repair_strategies::RepairTransaction::new()
            .map_err(|e| RepairError::Unknown(e.to_string()))?;

        // Add operations based on detected errors
        for error in &errors {
            self.add_repair_operation(&mut repair_transaction, error)
                .await?;
        }

        // Execute the repair transaction
        match repair_transaction.execute().await {
            Ok(_) => {
                // Validate the repairs
                let validation_result = self.validate_repairs(&errors).await?;
                match validation_result {
                    true => Ok(RepairResult::Success(
                        "All repairs completed successfully".to_string(),
                    )),
                    false => {
                        // Rollback if validation fails
                        // DISABLED: No rollback since no snapshot was created
                        // if let Some(snap_id) = snapshot_id {
                        //     self.rollback_to_snapshot(&snap_id).await?;
                        // }
                        Ok(RepairResult::Failed(RepairError::ValidationError(
                            "Repairs failed validation (no rollback available - snapshots disabled)".to_string()
                        )))
                    }
                }
            }
            Err(e) => {
                // Rollback on error
                // DISABLED: No rollback since no snapshot was created
                // if let Some(snap_id) = snapshot_id {
                //     self.rollback_to_snapshot(&snap_id).await?;
                // }
                Ok(RepairResult::Failed(e))
            }
        }
    }

    /// Check if Solana CLI is installed
    async fn is_solana_cli_installed(&self) -> Result<bool, RepairError> {
        system_deps::check_solana_cli()
            .await
            .map_err(|e| RepairError::SystemDependency(e.to_string()))
    }

    /// Check if config directory exists
    async fn config_directory_exists(&self) -> Result<bool, RepairError> {
        user_deps::check_config_directory()
            .await
            .map_err(|e| RepairError::UserDependency(e.to_string()))
    }

    /// Add repair operation based on error type
    async fn add_repair_operation(
        &self,
        transaction: &mut repair_strategies::RepairTransaction,
        error: &RepairableError,
    ) -> Result<(), RepairError> {
        match error {
            RepairableError::MissingSolanaCli => {
                transaction.add_operation(repair_strategies::RepairOperation::InstallSolanaCli);
            }
            RepairableError::MissingKeypair(path) => {
                transaction.add_operation(repair_strategies::RepairOperation::GenerateKeypair(
                    path.clone(),
                ));
            }
            RepairableError::MissingConfigDirectory => {
                transaction
                    .add_operation(repair_strategies::RepairOperation::CreateConfigDirectory);
            }
            RepairableError::SystemTuningRequired => {
                if self.config.allow_system_repairs {
                    transaction
                        .add_operation(repair_strategies::RepairOperation::TuneSystemParameters);
                }
            }
            RepairableError::OutdatedSystemPackages => {
                if self.config.allow_system_repairs {
                    transaction
                        .add_operation(repair_strategies::RepairOperation::UpdateSystemPackages);
                }
            }
            RepairableError::OutdatedRustToolchain => {
                transaction.add_operation(repair_strategies::RepairOperation::UpdateRustToolchain);
            }
            RepairableError::MissingSystemDependencies(deps) => {
                if self.config.allow_system_repairs {
                    transaction.add_operation(
                        repair_strategies::RepairOperation::InstallSystemDependencies(deps.clone()),
                    );
                }
            }
            _ => {
                return Err(RepairError::Unknown(format!(
                    "Unsupported repair type: {:?}",
                    error
                )));
            }
        }
        Ok(())
    }

    /// Validate that repairs were successful
    async fn validate_repairs(&self, _errors: &[RepairableError]) -> Result<bool, RepairError> {
        // Perform comprehensive health check after repairs
        let health_check = self
            .diagnostics
            .check_system_health()
            .await
            .map_err(|e| RepairError::ValidationError(e.to_string()))?;

        Ok(health_check.is_healthy())
    }
}

/// Extract keypair path from error message
fn extract_keypair_path(error: &str) -> Option<String> {
    // Look for pattern: "Error reading keypair file <path>:"
    if let Some(start) = error.find("Error reading keypair file ") {
        let start = start + "Error reading keypair file ".len();
        if let Some(end) = error[start..].find(':') {
            return Some(error[start..start + end].to_string());
        }
    }
    None
}

/// Convenience function to handle keypair reading with automatic repair
pub async fn read_keypair_with_repair(
    keypair_path: &str,
) -> Result<solana_sdk::signature::Keypair, Box<dyn StdError>> {
    // First try normal keypair reading
    match solana_sdk::signature::read_keypair_file(keypair_path) {
        Ok(keypair) => Ok(keypair),
        Err(err) => {
            // Error detected, attempt self-repair
            let repair_system = SelfRepairSystem::with_default_config();
            let error_message = format!("Error reading keypair file {}: {}", keypair_path, err);

            println!("๐Ÿ”ง OSVM detected a configuration issue. Attempting automatic repair...");

            match repair_system.analyze_error(&error_message).await {
                Ok(repairable_errors) => {
                    if !repairable_errors.is_empty() {
                        // Show repair plan to user
                        println!("๐Ÿ“‹ Issues detected:");
                        for error in &repairable_errors {
                            println!("  - {:?}", error);
                        }

                        // Check if running in test environment
                        let in_test = std::env::var("CARGO_TEST").is_ok()
                            || std::env::var("OSVM_TEST_MODE").is_ok()
                            || std::thread::current()
                                .name()
                                .map_or(false, |name| name.contains("test"));

                        let should_repair = if in_test {
                            println!("๐Ÿงช Test environment detected - skipping repair");
                            false
                        } else {
                            println!(
                                "\n๐Ÿ› ๏ธ  Would you like OSVM to fix these issues automatically? (Y/n):"
                            );
                            let mut input = String::new();
                            std::io::stdin().read_line(&mut input).unwrap();
                            input.trim().to_lowercase() != "n"
                        };

                        if should_repair {
                            match repair_system.repair_automatically(repairable_errors).await {
                                Ok(RepairResult::Success(msg)) => {
                                    println!("โœ… {}", msg);
                                    println!("๐Ÿ”„ Retrying keypair read...");

                                    // Retry reading the keypair
                                    match solana_sdk::signature::read_keypair_file(keypair_path) {
                                        Ok(keypair) => return Ok(keypair),
                                        Err(retry_err) => {
                                            return Err(format!("Repair succeeded but keypair still not readable: {}", retry_err).into());
                                        }
                                    }
                                }
                                Ok(result) => {
                                    return Err(format!("Repair incomplete: {:?}", result).into());
                                }
                                Err(repair_err) => {
                                    return Err(format!("Repair failed: {}", repair_err).into());
                                }
                            }
                        }
                    }
                }
                Err(analysis_err) => {
                    return Err(format!("Failed to analyze error: {}", analysis_err).into());
                }
            }

            // If we get here, repair was declined or failed
            Err(err)
        }
    }
}

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

    #[test]
    fn test_extract_keypair_path() {
        let error = "Error reading keypair file /home/user/.config/solana/id.json: No such file or directory";
        let path = extract_keypair_path(error);
        assert_eq!(path, Some("/home/user/.config/solana/id.json".to_string()));
    }

    #[test]
    fn test_extract_keypair_path_no_match() {
        let error = "Some other error message";
        let path = extract_keypair_path(error);
        assert_eq!(path, None);
    }

    #[tokio::test]
    async fn test_repair_system_creation() {
        let repair_system = SelfRepairSystem::with_default_config();
        assert!(repair_system.config.interactive);
        // assert!(repair_system.config.auto_snapshot);  // REMOVED
    }
}