cora-code 0.16.0

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
use std::path::PathBuf;

use anyhow::{Context, Result};
use colored::Colorize;

use crate::config::loader;
use crate::config::schema::{CoraFile, HookSection, OutputSection, ProviderSection};

/// Execute `cora config show` — print the current resolved configuration.
///
/// `--global` shows only ~/.cora/config.yaml
/// `--project` shows only .cora.yaml
/// (default) shows the fully merged effective config
pub fn execute_config_show(
    global_only: bool,
    project_only: bool,
    config_path: Option<&str>,
) -> Result<()> {
    // Clap conflicts_with handles the --global + --project case,
    // but add a defensive check in case of programmatic invocation.
    if global_only {
        return show_global_config();
    }
    if project_only {
        return show_project_config();
    }
    show_effective_config(config_path)
}

/// Show the fully merged effective config (default behavior).
fn show_effective_config(config_path: Option<&str>) -> Result<()> {
    let config = loader::load_config(config_path, None, None, None, None, false)?;

    // Resolve effective values (env vars can override config file)
    let eff_provider = std::env::var("CORA_PROVIDER")
        .ok()
        .unwrap_or_else(|| config.provider.provider.clone());
    let eff_model = std::env::var("CORA_MODEL")
        .ok()
        .unwrap_or_else(|| config.provider.model.clone());
    let eff_base_url = std::env::var("CORA_BASE_URL")
        .ok()
        .unwrap_or_else(|| config.provider.base_url.clone());

    // Source annotations
    let provider_src = if std::env::var("CORA_PROVIDER").is_ok() {
        " [from: env CORA_PROVIDER]"
    } else {
        ""
    };
    let model_src = if std::env::var("CORA_MODEL").is_ok() {
        " [from: env CORA_MODEL]"
    } else {
        ""
    };
    let url_src = if std::env::var("CORA_BASE_URL").is_ok() {
        " [from: env CORA_BASE_URL]"
    } else {
        ""
    };

    println!(
        "{}",
        "╔══════════════════════════════════════════╗".cyan().bold()
    );
    println!(
        "{}",
        "║       Effective Configuration             ║"
            .cyan()
            .bold()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".cyan().bold()
    );
    println!();

    println!(
        "{} {}{}",
        "provider:".bold(),
        eff_provider.green(),
        provider_src.dimmed()
    );
    println!(
        "  {} {}{}",
        "model:".dimmed(),
        eff_model.green(),
        model_src.dimmed()
    );
    println!(
        "  {} {}{}",
        "base_url:".dimmed(),
        eff_base_url.green(),
        url_src.dimmed()
    );

    println!(
        "{} {}",
        "focus:".bold(),
        config
            .focus
            .iter()
            .map(|f| format!("{}", f.green()))
            .collect::<Vec<_>>()
            .join(", ")
    );

    if !config.rules.is_empty() {
        println!(
            "{} {}",
            "rules:".bold(),
            config
                .rules
                .iter()
                .map(|r| format!("{}", r.green()))
                .collect::<Vec<_>>()
                .join(", ")
        );
    }

    println!(
        "{} mode={} min_severity={} max_diff_size={}",
        "hook:".bold(),
        config.hook.mode.yellow(),
        config.hook.min_severity.yellow(),
        config.hook.max_diff_size
    );

    println!(
        "{} format={} color={}",
        "output:".bold(),
        config.output.format.green(),
        config.output.color
    );

    println!(
        "{} {}",
        "ignore files:".bold(),
        if config.ignore.files.is_empty() {
            "(none)".to_string().dimmed().to_string()
        } else {
            config
                .ignore
                .files
                .iter()
                .map(|f| format!("{}", f.dimmed()))
                .collect::<Vec<_>>()
                .join(", ")
        }
    );

    Ok(())
}

/// Show only the global config (~/.cora/config.yaml).
fn show_global_config() -> Result<()> {
    let dir = loader::cora_dir()?;
    let path = dir.join("config.yaml");

    println!(
        "{}",
        "╔══════════════════════════════════════════╗".cyan().bold()
    );
    println!(
        "{}",
        "║         Global Configuration             ║".cyan().bold()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".cyan().bold()
    );
    println!();

    if !path.is_file() {
        println!(
            "{} No global config found at {}",
            "⚠️".yellow(),
            path.display()
        );
        println!(
            "   Run {} or {} to create one.",
            "cora auth login".cyan(),
            "cora config set --global provider zai".cyan()
        );
        return Ok(());
    }

    let content = std::fs::read_to_string(&path)
        .with_context(|| format!("failed to read {}", path.display()))?;
    let cora = CoraFile::from_str(&content)
        .with_context(|| format!("failed to parse {}", path.display()))?;

    println!("{} {}", "File:".bold(), path.display());
    println!();
    print_cora_file(&cora);

    // Also show auth status
    println!();
    let auth = loader::auth_status()?;
    if auth.has_key {
        println!(
            "{} API key: configured ({})",
            "✅".green().bold(),
            auth.source
        );
    } else {
        println!("{} API key: not configured", "❌".red().bold());
    }

    Ok(())
}

/// Show only the project config (.cora.yaml).
fn show_project_config() -> Result<()> {
    println!(
        "{}",
        "╔══════════════════════════════════════════╗".cyan().bold()
    );
    println!(
        "{}",
        "║        Project Configuration             ║".cyan().bold()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".cyan().bold()
    );
    println!();

    let found = loader::find_cora_file(&std::env::current_dir()?)?;
    match found {
        Some((path, cora)) => {
            println!("{} {}", "File:".bold(), path.display());
            println!();
            print_cora_file(&cora);
        }
        None => {
            println!("{} No .cora.yaml found in this project.", "⚠️".yellow());
            println!("   Run {} to create one.", "cora init".cyan());
        }
    }

    Ok(())
}

/// Print a CoraFile's non-empty fields.
fn print_cora_file(cora: &CoraFile) {
    if let Some(ps) = &cora.provider {
        if let Some(v) = &ps.provider {
            println!("{} {}", "provider:".bold(), v.green());
        }
        if let Some(v) = &ps.model {
            println!("  {} {}", "model:".dimmed(), v.green());
        }
        if let Some(v) = &ps.base_url {
            println!("  {} {}", "base_url:".dimmed(), v.dimmed());
        }
    }
    if let Some(v) = &cora.model {
        println!("{} {}", "model:".bold(), v.green());
    }
    if let Some(v) = &cora.base_url {
        println!("{} {}", "base_url:".bold(), v.dimmed());
    }
    if let Some(v) = &cora.focus {
        println!(
            "{} {}",
            "focus:".bold(),
            v.iter()
                .map(|f| format!("{}", f.green()))
                .collect::<Vec<_>>()
                .join(", ")
        );
    }
    if let Some(v) = &cora.rules {
        if !v.is_empty() {
            println!(
                "{} {}",
                "rules:".bold(),
                v.iter()
                    .map(|r| format!("{}", r.green()))
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
    }
    if let Some(ig) = &cora.ignore {
        if let Some(v) = &ig.files {
            println!(
                "{} {}",
                "ignore files:".bold(),
                v.iter()
                    .map(|f| format!("{}", f.dimmed()))
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
    }
    if let Some(h) = &cora.hook {
        let mut parts = vec![];
        if let Some(v) = &h.mode {
            parts.push(format!("mode={}", v));
        }
        if let Some(v) = &h.min_severity {
            parts.push(format!("min_severity={}", v));
        }
        if let Some(v) = h.max_diff_size {
            parts.push(format!("max_diff_size={}", v));
        }
        if !parts.is_empty() {
            println!("{} {}", "hook:".bold(), parts.join(" ").yellow());
        }
    }
    if let Some(o) = &cora.output {
        let mut parts = vec![];
        if let Some(v) = &o.format {
            parts.push(format!("format={}", v));
        }
        if let Some(v) = o.color {
            parts.push(format!("color={}", v));
        }
        if !parts.is_empty() {
            println!("{} {}", "output:".bold(), parts.join(" ").green());
        }
    }
    if let Some(llm) = &cora.llm {
        let mut parts = vec![];
        if let Some(v) = llm.temperature {
            parts.push(format!("temperature={}", v));
        }
        if let Some(v) = llm.max_tokens {
            parts.push(format!("max_tokens={}", v));
        }
        if let Some(ref v) = llm.max_tokens_param {
            parts.push(format!("max_tokens_param={}", v));
        }
        if let Some(v) = llm.timeout {
            parts.push(format!("timeout={}", v));
        }
        if let Some(v) = llm.cache_ttl {
            parts.push(format!("cache_ttl={}", v));
        }
        if !parts.is_empty() {
            println!("{} {}", "llm:".bold(), parts.join(" ").yellow());
        }
    }
}
/// to a YAML config file.
///
/// Supported keys: model, provider, `base_url`, format, severity
pub fn execute_config_set(key: &str, value: &str, global: bool) -> Result<()> {
    // Validate the key
    match key {
        "model" | "provider" | "base_url" | "format" | "severity" => {}
        _ => {
            anyhow::bail!(
                "unsupported key: {key}\nSupported keys: model, provider, base_url, format, severity"
            );
        }
    }

    // Determine target path
    let path = if global {
        let dir = loader::cora_dir()?;
        std::fs::create_dir_all(&dir)
            .with_context(|| format!("failed to create {}", dir.display()))?;
        dir.join("config.yaml")
    } else {
        // Warn if not inside a git project (could create orphan file)
        if std::process::Command::new("git")
            .args(["rev-parse", "--git-dir"])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .is_ok_and(|s| s.success())
        {
            PathBuf::from(".cora.yaml")
        } else {
            anyhow::bail!(
                "Not in a git project. Use --global to set config in ~/.cora/config.yaml, or cd into a git repo first."
            );
        }
    };

    // Load existing file or start fresh
    let mut cora = if path.is_file() {
        let content = std::fs::read_to_string(&path)
            .with_context(|| format!("failed to read {}", path.display()))?;
        CoraFile::from_str(&content).unwrap_or_default()
    } else {
        CoraFile::default()
    };

    // Apply the key update
    match key {
        "model" => {
            let provider = cora.provider.get_or_insert_with(ProviderSection::default);
            provider.model = Some(value.to_string());
        }
        "provider" => {
            let provider = cora.provider.get_or_insert_with(ProviderSection::default);
            provider.provider = Some(value.to_string());
        }
        "base_url" => {
            let provider = cora.provider.get_or_insert_with(ProviderSection::default);
            provider.base_url = Some(value.to_string());
        }
        "format" => {
            let output = cora.output.get_or_insert_with(OutputSection::default);
            output.format = Some(value.to_string());
        }
        "severity" => {
            let hook = cora.hook.get_or_insert_with(HookSection::default);
            hook.min_severity = Some(value.to_string());
        }
        _ => unreachable!(),
    }

    // Write back as YAML
    let yaml = serde_yaml_ng::to_string(&cora).context("failed to serialize config to YAML")?;
    std::fs::write(&path, &yaml).with_context(|| format!("failed to write {}", path.display()))?;

    // Restrict permissions for global config (defense-in-depth)
    #[cfg(unix)]
    if global {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
    }

    let scope = if global { "global" } else { "project" };
    println!(
        "{} Set {} = {} in {} ({})",
        "✓".green().bold(),
        key.bold(),
        value.green(),
        path.display(),
        scope.dimmed()
    );

    Ok(())
}

/// Execute `cora config validate` — load config and report validity.
///
/// Returns exit code: 0 if valid, 2 if issues found.
pub fn execute_config_validate(config_path: Option<&str>) -> Result<i32> {
    // 1. Load resolved config (same as other commands)
    let config = loader::load_config(config_path, None, None, None, None, false)?;

    // 2. Find the raw config file to check which fields were explicitly set.
    //    Prefer --config path, then fall back to auto-discovery.
    let cora_file = if let Some(path) = config_path {
        std::fs::read_to_string(path).ok().and_then(|content| {
            CoraFile::from_str(&content)
                .ok()
                .map(|cf| (std::path::PathBuf::from(path), cf))
        })
    } else {
        loader::find_cora_file(&std::env::current_dir().unwrap_or_default()).unwrap_or_else(|e| {
            eprintln!(
                "{} Warning: could not search for config file: {}",
                "⚠️".yellow(),
                e
            );
            None
        })
    };

    // Also check global config for fields set there
    let global_cora = load_global_cora_file();

    // Track how many fields were explicitly set
    let mut explicitly_set = 0u32;
    let total_fields = 8u32;

    // ── Check config file ──
    match &cora_file {
        Some((path, _)) => {
            println!("{} Config file: {}", "✅".green().bold(), path.display());
            explicitly_set += 1;
        }
        None => {
            println!(
                "{} Config file: not found (using defaults)",
                "⚠️".yellow().bold()
            );
        }
    }

    // ── Check provider ──
    let provider_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.provider.as_ref(),
        |g| g.provider.as_ref(),
    );
    if provider_set {
        println!(
            "{} Provider: {}",
            "✅".green().bold(),
            config.provider.provider
        );
        explicitly_set += 1;
    } else {
        println!(
            "{} Provider: not set (default: {})",
            "⚠️".yellow().bold(),
            config.provider.provider
        );
    }

    // ── Check model ──
    let model_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.provider.as_ref().and_then(|p| p.model.as_ref()),
        |g| g.provider.as_ref().and_then(|p| p.model.as_ref()),
    );
    if model_set {
        println!("{} Model: {}", "✅".green().bold(), config.provider.model);
        explicitly_set += 1;
    } else {
        println!(
            "{} Model: not set (default: {})",
            "⚠️".yellow().bold(),
            config.provider.model
        );
    }

    // ── Check API key ──
    let auth = loader::auth_status().context("failed to check auth status")?;
    if auth.has_key {
        let source = if auth.source.contains("env") {
            "env var".to_string()
        } else {
            format!("auth file ({})", auth.source)
        };
        println!("{} API key: configured ({})", "✅".green().bold(), source);
        explicitly_set += 1;
    } else {
        println!("{} API key: not configured", "❌".red().bold());
    }

    // ── Check severity ──
    let severity_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.hook.as_ref().and_then(|h| h.min_severity.as_ref()),
        |g| g.hook.as_ref().and_then(|h| h.min_severity.as_ref()),
    );
    if severity_set {
        println!(
            "{} Severity: {}",
            "✅".green().bold(),
            config.hook.min_severity
        );
        explicitly_set += 1;
    } else {
        println!(
            "{} Severity: not set (default: {})",
            "⚠️".yellow().bold(),
            config.hook.min_severity
        );
    }

    // ── Check output format ──
    let format_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.output.as_ref().and_then(|o| o.format.as_ref()),
        |g| g.output.as_ref().and_then(|o| o.format.as_ref()),
    );
    if format_set {
        println!(
            "{} Output format: {}",
            "✅".green().bold(),
            config.output.format
        );
        explicitly_set += 1;
    } else {
        println!(
            "{} Output format: not set (default: {})",
            "⚠️".yellow().bold(),
            config.output.format
        );
    }

    // ── Check temperature ──
    let temp_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.llm.as_ref().and_then(|l| l.temperature.as_ref()),
        |g| g.llm.as_ref().and_then(|l| l.temperature.as_ref()),
    );
    if temp_set {
        println!(
            "{} Temperature: {}",
            "✅".green().bold(),
            config.temperature
        );
        explicitly_set += 1;
    } else {
        println!(
            "{} Temperature: not set (default: {})",
            "⚠️".yellow().bold(),
            config.temperature
        );
    }

    // ── Check cache TTL ──
    let cache_set = is_explicitly_set(
        &cora_file,
        &global_cora,
        |c| c.llm.as_ref().and_then(|l| l.cache_ttl.as_ref()),
        |g| g.llm.as_ref().and_then(|l| l.cache_ttl.as_ref()),
    );
    if cache_set {
        println!("{} Cache TTL: {}", "✅".green().bold(), config.cache_ttl);
        explicitly_set += 1;
    } else {
        println!(
            "{} Cache TTL: not set (default: {})",
            "⚠️".yellow().bold(),
            config.cache_ttl
        );
    }

    println!();

    // Determine overall validity
    let has_issues = !auth.has_key;
    if has_issues {
        println!(
            "{}",
            "Configuration issues found. Fix the ❌ items above."
                .red()
                .bold()
        );
        println!(
            "{}",
            format!("{}/{} fields explicitly set.", explicitly_set, total_fields).dimmed()
        );
        Ok(2)
    } else {
        println!("{}", "Configuration valid.".green().bold());
        println!(
            "{}",
            format!("{}/{} fields explicitly set.", explicitly_set, total_fields).dimmed()
        );
        Ok(0)
    }
}

/// Check if a field was explicitly set in either the project config or global config.
/// Uses closure extractors to check both config sources.
fn is_explicitly_set<T>(
    project: &Option<(PathBuf, CoraFile)>,
    global: &Option<CoraFile>,
    project_extractor: impl Fn(&CoraFile) -> Option<&T>,
    global_extractor: impl Fn(&CoraFile) -> Option<&T>,
) -> bool {
    let project_has = project
        .as_ref()
        .and_then(|(_, c)| project_extractor(c))
        .is_some();
    let global_has = global.as_ref().and_then(global_extractor).is_some();
    project_has || global_has
}

/// Load the global config file (same logic as loader::load_global_config but returns Option directly).
fn load_global_cora_file() -> Option<CoraFile> {
    let dir = loader::cora_dir().ok()?;
    let path = dir.join("config.yaml");
    if !path.is_file() {
        return None;
    }
    let content = std::fs::read_to_string(&path).ok()?;
    CoraFile::from_str(&content).ok()
}