nap-core 0.4.3

Core library for the Narrative Addressing Protocol
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
// SPDX-FileCopyrightText: 2026 Digital Creations
// SPDX-License-Identifier: MIT
//! NAP doctor diagnostics and repair
//!
//! Performs comprehensive diagnostics on the NAP SDK environment and
//! provides repair capabilities for common issues.

use anyhow::Result;
use std::path::Path;
use tracing::{error, info};

use super::{
    cert::generate_certificates, config::generate_local_config, install::LoreInstaller,
    manager::ServerManager, version::verify_lore_installation,
};

/// NAP doctor for diagnostics and repair
pub struct NapDoctor {
    nap_home: std::path::PathBuf,
}

impl NapDoctor {
    /// Create a new NAP doctor
    pub fn new(nap_home: &Path) -> Self {
        Self {
            nap_home: nap_home.to_path_buf(),
        }
    }

    /// Run comprehensive diagnostics
    pub async fn diagnose(&self) -> Result<DoctorReport> {
        info!("Running NAP doctor diagnostics");

        let mut checks = vec![];

        // Check NAP configuration
        checks.push(self.check_nap_configuration());

        // Check Lore installation
        checks.push(self.check_lore_installation());

        // Check Lore configuration
        checks.push(self.check_lore_configuration());

        // Check Lore certificates
        checks.push(self.check_lore_certificates());

        // Check Lore server status
        checks.push(self.check_lore_server_status().await);

        // Check store directories
        checks.push(self.check_store_directories());

        // Check provider connectivity
        checks.push(self.check_provider_connectivity().await);

        let report = DoctorReport {
            checks,
            nap_home: self.nap_home.clone(),
        };

        info!("Diagnostics complete: {} checks", report.checks.len());
        Ok(report)
    }

    /// Repair detected issues
    pub async fn repair(&self, report: &DoctorReport) -> Result<RepairReport> {
        info!("Starting repair based on diagnostics");

        let mut repairs = vec![];

        for check in &report.checks {
            if !check.passed {
                match self.repair_check(check).await {
                    Ok(repair_result) => {
                        repairs.push(repair_result);
                    }
                    Err(e) => {
                        error!("Failed to repair {}: {}", check.name, e);
                        repairs.push(RepairResult {
                            check_name: check.name.clone(),
                            success: false,
                            message: format!("Repair failed: {}", e),
                        });
                    }
                }
            }
        }

        let repair_report = RepairReport { repairs };
        info!(
            "Repair complete: {} repairs attempted",
            repair_report.repairs.len()
        );
        Ok(repair_report)
    }

    /// Check NAP configuration
    fn check_nap_configuration(&self) -> CheckResult {
        let name = "NAP Configuration";

        let config_exists = self.nap_home.exists();
        let config_dir = self.nap_home.join("lore").join("config");
        let lore_config_exists = config_dir.exists();

        if config_exists && lore_config_exists {
            CheckResult {
                name: name.to_string(),
                passed: true,
                message: "NAP configuration exists".to_string(),
                severity: CheckSeverity::Info,
            }
        } else {
            CheckResult {
                name: name.to_string(),
                passed: false,
                message: "NAP configuration missing".to_string(),
                severity: CheckSeverity::Error,
            }
        }
    }

    /// Check Lore installation
    fn check_lore_installation(&self) -> CheckResult {
        let name = "Lore Installation";

        match verify_lore_installation() {
            Ok(status) => {
                if status.is_fully_compatible() {
                    CheckResult {
                        name: name.to_string(),
                        passed: true,
                        message: format!("Lore {} installed and compatible", status.pinned_version),
                        severity: CheckSeverity::Info,
                    }
                } else {
                    CheckResult {
                        name: name.to_string(),
                        passed: false,
                        message: status.status_message(),
                        severity: CheckSeverity::Error,
                    }
                }
            }
            Err(e) => CheckResult {
                name: name.to_string(),
                passed: false,
                message: format!("Failed to check Lore installation: {}", e),
                severity: CheckSeverity::Error,
            },
        }
    }

    /// Check Lore configuration
    fn check_lore_configuration(&self) -> CheckResult {
        let name = "Lore Configuration";

        let config_path = self.nap_home.join("lore").join("config").join("local.toml");

        if config_path.exists() {
            CheckResult {
                name: name.to_string(),
                passed: true,
                message: "Lore configuration exists".to_string(),
                severity: CheckSeverity::Info,
            }
        } else {
            CheckResult {
                name: name.to_string(),
                passed: false,
                message: "Lore configuration missing".to_string(),
                severity: CheckSeverity::Warning,
            }
        }
    }

    /// Check Lore certificates
    fn check_lore_certificates(&self) -> CheckResult {
        let name = "Lore Certificates";

        let cert_dir = self.nap_home.join("lore").join("certs");
        let cert_path = cert_dir.join("cert.pem");
        let key_path = cert_dir.join("key.pem");

        if cert_path.exists() && key_path.exists() {
            CheckResult {
                name: name.to_string(),
                passed: true,
                message: "Lore certificates exist".to_string(),
                severity: CheckSeverity::Info,
            }
        } else {
            CheckResult {
                name: name.to_string(),
                passed: false,
                message: "Lore certificates missing".to_string(),
                severity: CheckSeverity::Warning,
            }
        }
    }

    /// Check Lore server status
    async fn check_lore_server_status(&self) -> CheckResult {
        let name = "Lore Server Status";

        let server_manager = ServerManager::new(&self.nap_home);

        match server_manager.status().await {
            Ok(status) => {
                if status.is_ready() {
                    CheckResult {
                        name: name.to_string(),
                        passed: true,
                        message: format!("Lore server running on port {}", status.http_port),
                        severity: CheckSeverity::Info,
                    }
                } else {
                    CheckResult {
                        name: name.to_string(),
                        passed: false,
                        message: status.status_message(),
                        severity: CheckSeverity::Warning,
                    }
                }
            }
            Err(e) => CheckResult {
                name: name.to_string(),
                passed: false,
                message: format!("Failed to check server status: {}", e),
                severity: CheckSeverity::Error,
            },
        }
    }

    /// Check store directories
    fn check_store_directories(&self) -> CheckResult {
        let name = "Store Directories";

        let immutable_dir = self.nap_home.join("lore").join("store").join("immutable");
        let mutable_dir = self.nap_home.join("lore").join("store").join("mutable");

        if immutable_dir.exists() && mutable_dir.exists() {
            CheckResult {
                name: name.to_string(),
                passed: true,
                message: "Store directories exist".to_string(),
                severity: CheckSeverity::Info,
            }
        } else {
            CheckResult {
                name: name.to_string(),
                passed: false,
                message: "Store directories missing".to_string(),
                severity: CheckSeverity::Warning,
            }
        }
    }

    /// Check provider connectivity
    async fn check_provider_connectivity(&self) -> CheckResult {
        let name = "Provider Connectivity";

        // For now, just check if we can reach localhost
        match reqwest::get("http://127.0.0.1:41339/health_check").await {
            Ok(resp) => {
                if resp.status().is_success() {
                    CheckResult {
                        name: name.to_string(),
                        passed: true,
                        message: "Provider connectivity OK".to_string(),
                        severity: CheckSeverity::Info,
                    }
                } else {
                    CheckResult {
                        name: name.to_string(),
                        passed: false,
                        message: format!("Provider returned status: {}", resp.status()),
                        severity: CheckSeverity::Warning,
                    }
                }
            }
            Err(e) => CheckResult {
                name: name.to_string(),
                passed: false,
                message: format!("Provider connectivity failed: {}", e),
                severity: CheckSeverity::Warning,
            },
        }
    }

    /// Repair a specific check
    async fn repair_check(&self, check: &CheckResult) -> Result<RepairResult> {
        match check.name.as_str() {
            "NAP Configuration" => {
                std::fs::create_dir_all(&self.nap_home)?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Created NAP home directory".to_string(),
                })
            }
            "Lore Installation" => {
                let install_dir = dirs::home_dir().unwrap().join(".local").join("bin");
                let installer = LoreInstaller::new(&install_dir);
                installer.install_all()?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Installed Lore CLI and server".to_string(),
                })
            }
            "Lore Configuration" => {
                generate_local_config(&self.nap_home)?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Generated Lore configuration".to_string(),
                })
            }
            "Lore Certificates" => {
                let cert_dir = self.nap_home.join("lore").join("certs");
                generate_certificates(&cert_dir)?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Generated Lore certificates".to_string(),
                })
            }
            "Lore Server Status" => {
                let server_manager = ServerManager::new(&self.nap_home);
                server_manager.ensure_running().await?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Started Lore server".to_string(),
                })
            }
            "Store Directories" => {
                let immutable_dir = self.nap_home.join("lore").join("store").join("immutable");
                let mutable_dir = self.nap_home.join("lore").join("store").join("mutable");
                std::fs::create_dir_all(&immutable_dir)?;
                std::fs::create_dir_all(&mutable_dir)?;
                Ok(RepairResult {
                    check_name: check.name.clone(),
                    success: true,
                    message: "Created store directories".to_string(),
                })
            }
            _ => Ok(RepairResult {
                check_name: check.name.clone(),
                success: false,
                message: "No repair available for this check".to_string(),
            }),
        }
    }
}

/// Result of a diagnostic check
#[derive(Debug, Clone)]
pub struct CheckResult {
    pub name: String,
    pub passed: bool,
    pub message: String,
    pub severity: CheckSeverity,
}

/// Severity level of a check
#[derive(Debug, Clone, PartialEq)]
pub enum CheckSeverity {
    Info,
    Warning,
    Error,
}

/// Complete diagnostic report
#[derive(Debug, Clone)]
pub struct DoctorReport {
    pub checks: Vec<CheckResult>,
    pub nap_home: std::path::PathBuf,
}

impl DoctorReport {
    /// Get overall health status
    pub fn overall_health(&self) -> HealthStatus {
        let has_errors = self
            .checks
            .iter()
            .any(|c| c.severity == CheckSeverity::Error && !c.passed);
        let has_warnings = self
            .checks
            .iter()
            .any(|c| c.severity == CheckSeverity::Warning && !c.passed);

        if has_errors {
            HealthStatus::Unhealthy
        } else if has_warnings {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }

    /// Get a summary message
    pub fn summary(&self) -> String {
        let passed = self.checks.iter().filter(|c| c.passed).count();
        let total = self.checks.len();
        format!("{} / {} checks passed", passed, total)
    }
}

/// Overall health status
#[derive(Debug, Clone, PartialEq)]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
}

/// Result of a repair operation
#[derive(Debug, Clone)]
pub struct RepairResult {
    pub check_name: String,
    pub success: bool,
    pub message: String,
}

/// Complete repair report
#[derive(Debug, Clone)]
pub struct RepairReport {
    pub repairs: Vec<RepairResult>,
}

impl RepairReport {
    /// Get number of successful repairs
    pub fn successful_count(&self) -> usize {
        self.repairs.iter().filter(|r| r.success).count()
    }

    /// Get number of failed repairs
    pub fn failed_count(&self) -> usize {
        self.repairs.iter().filter(|r| !r.success).count()
    }

    /// Get summary message
    pub fn summary(&self) -> String {
        format!(
            "{} successful, {} failed",
            self.successful_count(),
            self.failed_count()
        )
    }
}

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

    #[test]
    fn test_doctor_creation() {
        let temp_dir = TempDir::new().unwrap();
        let doctor = NapDoctor::new(temp_dir.path());
        assert_eq!(doctor.nap_home, temp_dir.path());
    }

    #[test]
    fn test_check_result() {
        let check = CheckResult {
            name: "Test Check".to_string(),
            passed: true,
            message: "Test passed".to_string(),
            severity: CheckSeverity::Info,
        };
        assert!(check.passed);
        assert_eq!(check.severity, CheckSeverity::Info);
    }

    #[test]
    fn test_health_status() {
        let report = DoctorReport {
            checks: vec![
                CheckResult {
                    name: "Check 1".to_string(),
                    passed: true,
                    message: "OK".to_string(),
                    severity: CheckSeverity::Info,
                },
                CheckResult {
                    name: "Check 2".to_string(),
                    passed: true,
                    message: "OK".to_string(),
                    severity: CheckSeverity::Info,
                },
            ],
            nap_home: std::path::PathBuf::from("/tmp"),
        };
        assert_eq!(report.overall_health(), HealthStatus::Healthy);
    }
}