ggen-core 26.7.3

Core graph-aware code generation engine
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
//! System diagnostics - domain layer
//!
//! Pure business logic for system health checks.

use crate::utils::error::Result;
use serde::{Deserialize, Serialize};

/// System check status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CheckStatus {
    Ok,
    Warning,
    Error,
}

/// System check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckResult {
    pub name: String,
    pub status: CheckStatus,
    pub message: String,
    pub recovery: Option<String>,
}

/// Doctor command input (pure domain type)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DoctorInput {
    /// Show detailed output with fix instructions
    pub verbose: bool,

    /// Run specific check (e.g., "rust", "cargo", "git")
    pub check: Option<String>,

    /// Show environment information
    pub env: bool,

    /// Also run the expensive checks (SLO microbenchmarks, observability
    /// stack network probes). Off by default so `doctor` stays a fast,
    /// no-network local health check.
    pub all: bool,
}

/// Doctor command result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DoctorResult {
    pub checks: Vec<CheckResult>,
    pub environment: Option<EnvironmentInfo>,
}

/// Environment information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnvironmentInfo {
    pub rust_version: Option<String>,
    pub cargo_version: Option<String>,
    pub git_version: Option<String>,
    pub os: String,
    pub architecture: String,
    pub home_dir: Option<String>,
}

/// Execute doctor checks (pure domain function)
pub async fn execute_doctor(input: DoctorInput) -> Result<DoctorResult> {
    let mut checks = Vec::new();

    // Check Rust
    if input.check.is_none() || input.check.as_deref() == Some("rust") {
        let rust_check = check_rust().await?;
        checks.push(rust_check);
    }

    // Check Cargo
    if input.check.is_none() || input.check.as_deref() == Some("cargo") {
        let cargo_check = check_cargo().await?;
        checks.push(cargo_check);
    }

    // Check Git
    if input.check.is_none() || input.check.as_deref() == Some("git") {
        let git_check = check_git().await?;
        checks.push(git_check);
    }

    // Check Marketplace
    if input.check.is_none() || input.check.as_deref() == Some("marketplace") {
        let marketplace_check = check_marketplace().await?;
        checks.push(marketplace_check);
    }

    // Check User Cache
    if input.check.is_none() || input.check.as_deref() == Some("cache") {
        let cache_check = check_cache().await?;
        checks.push(cache_check);
    }

    // Observability stack (network probes) and SLO microbenchmarks are
    // expensive and not local-only — opt in explicitly via `all` or by
    // naming the check directly, don't run them on a bare `doctor` call.
    if input.all || input.check.as_deref() == Some("observability") {
        let observability_check = check_observability().await?;
        checks.push(observability_check);
    }

    if input.all || input.check.as_deref() == Some("slo") {
        let slo_check = check_slo().await?;
        checks.push(slo_check);
    }

    // Collect environment info if requested
    let environment = if input.env {
        Some(collect_environment().await?)
    } else {
        None
    };

    Ok(DoctorResult {
        checks,
        environment,
    })
}

/// Check Marketplace health
async fn check_marketplace() -> Result<CheckResult> {
    use std::path::PathBuf;

    let cache_dir = if let Some(dir) = dirs::cache_dir() {
        dir.join("ggen").join("packs")
    } else {
        PathBuf::from(".cache").join("ggen").join("packs")
    };

    let db_path = cache_dir.join("marketplace.db");

    if !db_path.exists() {
        return Ok(CheckResult {
            name: "Marketplace DB".to_string(),
            status: CheckStatus::Warning,
            message: "RDF store not found. Run 'ggen marketplace sync' to initialize.".to_string(),
            recovery: Some("ggen marketplace sync".to_string()),
        });
    }

    // Try to open the store (check for corruption/locks)
    match oxigraph::store::Store::open(&db_path) {
        Ok(_) => Ok(CheckResult {
            name: "Marketplace DB".to_string(),
            status: CheckStatus::Ok,
            message: format!("RDF store healthy: {}", db_path.display()),
            recovery: None,
        }),
        Err(e) => Ok(CheckResult {
            name: "Marketplace DB".to_string(),
            status: CheckStatus::Error,
            message: format!("RDF store error (possibly locked or corrupt): {}", e),
            recovery: Some(
                "rm -rf ~/.cache/ggen/packs/marketplace.db && ggen marketplace sync".to_string(),
            ),
        }),
    }
}

/// Check User Cache health
async fn check_cache() -> Result<CheckResult> {
    let mut pack_count = 0;

    if let Some(home) = dirs::home_dir() {
        let user_packs = home.join(".ggen").join("packs");
        if user_packs.exists() {
            if let Ok(entries) = std::fs::read_dir(user_packs) {
                pack_count = entries
                    .filter_map(|e| e.ok())
                    .filter(|e| e.path().is_dir())
                    .count();
            }
        }
    }

    Ok(CheckResult {
        name: "User Cache".to_string(),
        status: CheckStatus::Ok,
        message: format!(
            "Found {} packs in global user cache (~/.ggen/packs)",
            pack_count
        ),
        recovery: None,
    })
}

/// Check Observability Stack health (Anti-Cheating Gate)
async fn check_observability() -> Result<CheckResult> {
    use reqwest::Client;
    use std::time::Duration;

    let client = Client::builder()
        .timeout(Duration::from_millis(500))
        .build()
        .map_err(|e| {
            crate::utils::error::Error::new(&format!("Failed to build HTTP client: {}", e))
        })?;

    // 1. Check Tempo Health (Deep Check)
    let tempo_url = "http://127.0.0.1:3200/ready";
    let tempo_status = client.get(tempo_url).send().await;

    // 2. Check OTel Health Check extension (standard port 13133)
    let otel_url = "http://127.0.0.1:13133/";
    let otel_status = client.get(otel_url).send().await;

    let mut reachable = Vec::new();
    let mut failures = Vec::new();

    match tempo_status {
        Ok(resp) if resp.status().is_success() => {
            reachable.push("Tempo (API)");
        }
        Ok(resp) => {
            failures.push(format!("Tempo returned {}", resp.status()));
        }
        Err(_) => {
            failures.push("Tempo unreachable (port 3200)".to_string());
        }
    }

    match otel_status {
        Ok(resp) if resp.status().is_success() => {
            reachable.push("OTel Collector (Health)");
        }
        Ok(resp) => {
            failures.push(format!("OTel Collector returned {}", resp.status()));
        }
        Err(_) => {
            failures.push("OTel Collector unreachable (port 13133)".to_string());
        }
    }

    // 3. Check Jaeger health and ggen service registration
    let jaeger_url = "http://127.0.0.1:16686/api/services";
    match client.get(jaeger_url).send().await {
        Ok(resp) if resp.status().is_success() => {
            reachable.push("Jaeger (API)");
        }
        Ok(resp) => {
            failures.push(format!("Jaeger returned {}", resp.status()));
        }
        Err(_) => {
            failures.push("Jaeger unreachable (port 16686)".to_string());
        }
    }

    // 4. Evidence Check: verify ggen service has emitted traces to Jaeger
    let mut trace_evidence = false;
    if reachable.contains(&"Jaeger (API)") {
        if let Ok(resp) = client.get(jaeger_url).send().await {
            if let Ok(text) = resp.text().await {
                if text.contains("\"ggen\"") {
                    trace_evidence = true;
                }
            }
        }
    }

    if reachable.is_empty() {
        Ok(CheckResult {
            name: "Observability Stack".to_string(),
            status: CheckStatus::Warning,
            message: format!(
                "No observability services found (tried: Tempo, OTel, Jaeger). Errors: {}",
                failures.join(", ")
            ),
            recovery: Some("docker compose -f docker-compose.otel.yml up -d".to_string()),
        })
    } else if failures.is_empty() {
        let (msg, recovery) = if trace_evidence {
            (
                format!(
                    "All services healthy: {}. ggen service confirmed in Jaeger.",
                    reachable.join(", ")
                ),
                None,
            )
        } else {
            (
                format!(
                    "All services healthy: {}. No ggen traces in Jaeger yet (run a command to emit traces).",
                    reachable.join(", ")
                ),
                None,
            )
        };
        Ok(CheckResult {
            name: "Observability Stack".to_string(),
            status: CheckStatus::Ok,
            message: msg,
            recovery,
        })
    } else {
        Ok(CheckResult {
            name: "Observability Stack".to_string(),
            status: CheckStatus::Warning,
            message: format!(
                "Partial health: {}. Down: {}",
                reachable.join(", "),
                failures.join(", ")
            ),
            recovery: Some("docker compose -f docker-compose.otel.yml restart".to_string()),
        })
    }
}

/// Check SLO Performance (Vision 2030 Gate)
async fn check_slo() -> Result<CheckResult> {
    use crate::pipeline_engine::vocabulary::VocabularyRegistry;
    use oxigraph::model::*;
    use oxigraph::store::Store;
    use rayon::prelude::*;
    use std::collections::BTreeSet;
    use std::time::{Duration, Instant};

    let total_start = Instant::now();

    // 1. Vocabulary Registry Contention Test
    let vocab_start = Instant::now();
    let registry = VocabularyRegistry::with_standard_vocabularies();
    let mut test_ns = BTreeSet::new();
    test_ns.insert("http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string());
    test_ns.insert("http://www.w3.org/2000/01/rdf-schema#".to_string());
    test_ns.insert("http://www.w3.org/2002/07/owl#".to_string());
    test_ns.insert("http://ggen.dev/v26_5_19#".to_string());

    // Stress test: 1000 parallel lookups to measure lock contention
    (0..1000).into_par_iter().for_each(|_| {
        let _ = registry.validate_namespaces(&test_ns);
    });
    let vocab_duration = vocab_start.elapsed();

    // 2. RDF Graph Synthesis Throughput Test (Oxigraph)
    let graph_start = Instant::now();
    let store = Store::new()
        .map_err(|e| crate::utils::error::Error::new(&format!("Graph error: {}", e)))?;
    for i in 0..2000 {
        let s = NamedNode::new(format!("http://ggen.io/s{}", i))
            .map_err(|e| crate::utils::error::Error::new(&format!("NamedNode error: {}", e)))?;
        let p = NamedNode::new("http://ggen.io/p")
            .map_err(|e| crate::utils::error::Error::new(&format!("NamedNode error: {}", e)))?;
        let o = Literal::from(i);
        store
            .insert(&Quad::new(s, p, o, GraphName::DefaultGraph))
            .map_err(|e| crate::utils::error::Error::new(&format!("Insert error: {}", e)))?;
    }
    let graph_duration = graph_start.elapsed();

    // 3. Template Rendering Bottleneck Test (Tera)
    let template_start = Instant::now();
    let mut tera = tera::Tera::default();
    let mut context = tera::Context::new();
    context.insert("name", "GGen Doctor");
    context.insert("items", &(0..100).collect::<Vec<i32>>());
    let template = "Hello {{ name }}! Count: {% for i in items %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %}";
    for _ in 0..500 {
        let _ = tera
            .render_str(template, &context)
            .map_err(|e| crate::utils::error::Error::new(&format!("Tera error: {}", e)))?;
    }
    let template_duration = template_start.elapsed();

    let total_duration = total_start.elapsed();

    // SLO Thresholds (Vision 2030 Standards)
    let vocab_limit = Duration::from_millis(50);
    let graph_limit = Duration::from_millis(100);
    let template_limit = Duration::from_millis(150);

    let mut violations = Vec::new();
    if vocab_duration > vocab_limit {
        violations.push(format!("Vocab Registry slow ({:?})", vocab_duration));
    }
    if graph_duration > graph_limit {
        violations.push(format!("Graph Synthesis slow ({:?})", graph_duration));
    }
    if template_duration > template_limit {
        violations.push(format!("Template Rendering slow ({:?})", template_duration));
    }

    if violations.is_empty() {
        Ok(CheckResult {
            name: "SLO Performance".to_string(),
            status: CheckStatus::Ok,
            message: format!(
                "Architectural benchmarks passed. Total: {:?}. (Vocab: {:?}, Graph: {:?}, Template: {:?})",
                total_duration, vocab_duration, graph_duration, template_duration
            ),
            recovery: None,
        })
    } else {
        Ok(CheckResult {
            name: "SLO Performance".to_string(),
            status: CheckStatus::Warning,
            message: format!(
                "SLO Violations: {}. Total: {:?}.",
                violations.join(", "),
                total_duration
            ),
            recovery: Some("cargo build --release; reduce background CPU load".to_string()),
        })
    }
}

/// Check Rust installation
async fn check_rust() -> Result<CheckResult> {
    use std::process::Command;

    let output = Command::new("rustc").arg("--version").output();
    match output {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
            Ok(CheckResult {
                name: "Rust".to_string(),
                status: CheckStatus::Ok,
                message: format!("Installed: {}", version),
                recovery: None,
            })
        }
        _ => Ok(CheckResult {
            name: "Rust".to_string(),
            status: CheckStatus::Error,
            message: "Not installed. Install Rust from https://rustup.rs".to_string(),
            recovery: Some(
                "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh".to_string(),
            ),
        }),
    }
}

/// Check Cargo installation
async fn check_cargo() -> Result<CheckResult> {
    use std::process::Command;

    let output = Command::new("cargo").arg("--version").output();
    match output {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
            Ok(CheckResult {
                name: "Cargo".to_string(),
                status: CheckStatus::Ok,
                message: format!("Installed: {}", version),
                recovery: None,
            })
        }
        _ => Ok(CheckResult {
            name: "Cargo".to_string(),
            status: CheckStatus::Error,
            message: "Not installed. Install Rust from https://rustup.rs".to_string(),
            recovery: Some(
                "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh".to_string(),
            ),
        }),
    }
}

/// Check Git installation
async fn check_git() -> Result<CheckResult> {
    use std::process::Command;

    let output = Command::new("git").arg("--version").output();

    match output {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
            Ok(CheckResult {
                name: "Git".to_string(),
                status: CheckStatus::Ok,
                message: format!("Installed: {}", version),
                recovery: None,
            })
        }
        _ => Ok(CheckResult {
            name: "Git".to_string(),
            status: CheckStatus::Warning,
            message: "Not installed. Optional but recommended".to_string(),
            recovery: Some("brew install git".to_string()),
        }),
    }
}

/// Collect environment information
async fn collect_environment() -> Result<EnvironmentInfo> {
    use std::process::Command;

    let rust_version = Command::new("rustc")
        .arg("--version")
        .output()
        .ok()
        .and_then(|o| {
            o.status
                .success()
                .then(|| String::from_utf8_lossy(&o.stdout).trim().to_string())
        });

    let cargo_version = Command::new("cargo")
        .arg("--version")
        .output()
        .ok()
        .and_then(|o| {
            o.status
                .success()
                .then(|| String::from_utf8_lossy(&o.stdout).trim().to_string())
        });

    let git_version = Command::new("git")
        .arg("--version")
        .output()
        .ok()
        .and_then(|o| {
            o.status
                .success()
                .then(|| String::from_utf8_lossy(&o.stdout).trim().to_string())
        });

    Ok(EnvironmentInfo {
        rust_version,
        cargo_version,
        git_version,
        os: std::env::consts::OS.to_string(),
        architecture: std::env::consts::ARCH.to_string(),
        home_dir: dirs::home_dir().map(|p| p.to_string_lossy().to_string()),
    })
}