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
//! Quality profiles — preset and configurable rule sets per project.
//!
//! Profiles define focus areas, weights, and review behavior that modify
//! the AI review prompt. Built-in profiles are embedded at compile time.
//! Custom profiles can be loaded from `.cora.yaml` or external files.

use serde::{Deserialize, Serialize};
use tracing::debug;

// ─── Built-in profile YAML sources (embedded at compile time) ───

const SECURITY_FIRST_YAML: &str = include_str!("../profiles/security-first.yaml");
const PERFORMANCE_YAML: &str = include_str!("../profiles/performance.yaml");
const CLEAN_CODE_YAML: &str = include_str!("../profiles/clean-code.yaml");
const BEGINNER_FRIENDLY_YAML: &str = include_str!("../profiles/beginner-friendly.yaml");
const MINIMAL_YAML: &str = include_str!("../profiles/minimal.yaml");
const RUST_STRICT_YAML: &str = include_str!("../profiles/rust-strict.yaml");
const TYPESCRIPT_STRICT_YAML: &str = include_str!("../profiles/typescript-strict.yaml");
const GO_PRAGMATIC_YAML: &str = include_str!("../profiles/go-pragmatic.yaml");

/// All built-in profile names in display order.
pub const BUILTIN_PROFILES: &[&str] = &[
    "security-first",
    "performance",
    "clean-code",
    "beginner-friendly",
    "minimal",
    "rust-strict",
    "typescript-strict",
    "go-pragmatic",
];

// ─── Profile types ───

/// A complete quality profile definition.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Profile {
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub version: String,
    #[serde(default)]
    pub focus_areas: Vec<FocusArea>,
    #[serde(default)]
    pub ignore_areas: Vec<String>,
    #[serde(default)]
    pub severity_override: std::collections::HashMap<String, String>,
    #[serde(default)]
    pub review_style: ReviewStyle,
}

/// A single focus area within a profile.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FocusArea {
    pub id: String,
    /// Priority weight 1-10 (higher = more important).
    pub weight: u32,
    /// "block" = any finding fails gate, "warn" = report only.
    #[serde(default = "default_action")]
    pub action: String,
    #[serde(default)]
    pub rules: Vec<String>,
}

fn default_action() -> String {
    "warn".to_string()
}

/// Review style settings that modify LLM behavior.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ReviewStyle {
    /// "strict" | "standard" | "gentle"
    #[serde(default = "default_tone")]
    pub tone: String,
    /// "minimal" | "standard" | "high" | "exhaustive"
    #[serde(default = "default_detail")]
    pub detail_level: String,
    #[serde(default = "default_true")]
    pub suggest_fixes: bool,
    /// Max findings to report (null = no limit).
    pub max_findings: Option<usize>,
}

fn default_tone() -> String {
    "standard".to_string()
}

fn default_detail() -> String {
    "standard".to_string()
}

fn default_true() -> bool {
    true
}

/// Lightweight profile reference stored in `.cora.yaml`.
/// Can be a simple name string, or a full inline definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ProfileRef {
    /// Built-in profile name or path to custom profile file.
    Name(String),
    /// Inline profile definition with optional `extends`.
    Inline(InlineProfileRef),
}

/// Inline profile in `.cora.yaml` — can extend a built-in and override.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct InlineProfileRef {
    /// Extend a built-in profile by name.
    #[serde(default)]
    pub extends: Option<String>,
    /// Override focus areas (merged with base).
    #[serde(default)]
    pub focus_areas: Vec<FocusArea>,
    /// Areas to ignore.
    #[serde(default)]
    pub ignore_areas: Vec<String>,
    /// Override review style.
    #[serde(default)]
    pub review_style: Option<ReviewStyle>,
}

// ─── Profile operations ───

impl Profile {
    /// Validate profile values (#81): focus weights must be 1-10, actions and
    /// review-style enums must be recognized. Unknown values fail loudly so a
    /// typo (e.g. `weight: 15` or `action: blok`) does not silently change
    /// review behavior.
    pub fn validate(&self) -> Result<(), String> {
        const VALID_ACTIONS: &[&str] = &["block", "warn", "info"];
        const VALID_TONES: &[&str] = &["strict", "standard", "gentle"];
        const VALID_DETAILS: &[&str] = &["minimal", "standard", "high", "exhaustive"];
        let mut errs: Vec<String> = Vec::new();

        for area in &self.focus_areas {
            if !(1..=10).contains(&area.weight) {
                errs.push(format!(
                    "profile focus area '{}' weight must be 1-10, got: {}",
                    area.id, area.weight
                ));
            }
            if !VALID_ACTIONS.contains(&area.action.as_str()) {
                errs.push(format!(
                    "profile focus area '{}' action must be one of {:?}, got: '{}'",
                    area.id, VALID_ACTIONS, area.action
                ));
            }
        }

        let style = &self.review_style;
        if !style.tone.is_empty() && !VALID_TONES.contains(&style.tone.as_str()) {
            errs.push(format!(
                "profile review_style.tone must be one of {:?}, got: '{}'",
                VALID_TONES, style.tone
            ));
        }
        if !style.detail_level.is_empty() && !VALID_DETAILS.contains(&style.detail_level.as_str()) {
            errs.push(format!(
                "profile review_style.detail_level must be one of {:?}, got: '{}'",
                VALID_DETAILS, style.detail_level
            ));
        }

        if errs.is_empty() {
            Ok(())
        } else {
            Err(errs.join("; "))
        }
    }
}

/// Load a built-in profile by name. Returns `None` if not found.
pub fn load_builtin(name: &str) -> Option<Profile> {
    let yaml = match name {
        "security-first" => SECURITY_FIRST_YAML,
        "performance" => PERFORMANCE_YAML,
        "clean-code" => CLEAN_CODE_YAML,
        "beginner-friendly" => BEGINNER_FRIENDLY_YAML,
        "minimal" => MINIMAL_YAML,
        "rust-strict" => RUST_STRICT_YAML,
        "typescript-strict" => TYPESCRIPT_STRICT_YAML,
        "go-pragmatic" => GO_PRAGMATIC_YAML,
        _ => return None,
    };
    parse_profile_yaml(yaml).ok()
}

/// Parse a profile from YAML string.
pub fn parse_profile_yaml(yaml: &str) -> Result<Profile, String> {
    serde_yaml_ng::from_str(yaml).map_err(|e| format!("invalid profile YAML: {e}"))
}

/// Load all built-in profiles.
#[allow(dead_code)]
pub fn load_all_builtins() -> Vec<Profile> {
    BUILTIN_PROFILES
        .iter()
        .filter_map(|name| load_builtin(name))
        .collect()
}

/// Resolve a `ProfileRef` from `.cora.yaml` into a final `Profile`.
///
/// Resolution order:
/// - If `Name("builtin")` → load built-in
/// - If `Name("./path.yaml")` → load from file (resolve relative to project root)
/// - If `Inline { extends, ... }` → merge with base
/// - If `Inline` without extends → use as-is
pub fn resolve_profile(profile_ref: &ProfileRef) -> Result<Profile, String> {
    resolve_profile_with_root(profile_ref, None)
}

/// Resolve profile with optional project root for relative file path resolution.
pub fn resolve_profile_with_root(
    profile_ref: &ProfileRef,
    project_root: Option<&std::path::Path>,
) -> Result<Profile, String> {
    match profile_ref {
        ProfileRef::Name(name) => {
            // Try built-in first
            if let Some(p) = load_builtin(name) {
                debug!(profile = name, "loaded built-in profile");
                return Ok(p);
            }
            // Resolve relative to project root (not CWD) for deterministic behavior
            let path = if let Some(root) = project_root {
                root.join(name)
            } else {
                std::path::PathBuf::from(name)
            };
            if path.is_file() {
                let content = std::fs::read_to_string(&path)
                    .map_err(|e| format!("cannot read profile file '{}': {e}", path.display()))?;
                let mut profile = parse_profile_yaml(&content)?;
                // If profile has no name, derive from filename
                if profile.name.is_empty() {
                    profile.name = path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .unwrap_or("custom")
                        .to_string();
                }
                debug!(profile = %path.display(), "loaded profile from file");
                return Ok(profile);
            }
            Err(format!(
                "unknown profile '{name}'. Available built-ins: {}",
                BUILTIN_PROFILES.join(", ")
            ))
        }
        ProfileRef::Inline(inline) => {
            let mut profile = if let Some(base_name) = &inline.extends {
                load_builtin(base_name)
                    .ok_or_else(|| format!("cannot extend unknown profile '{base_name}'"))?
            } else {
                Profile::default()
            };

            // Override name if extending
            if let Some(ref base) = inline.extends {
                profile.name = format!("custom-{base}");
            } else {
                profile.name = "custom".to_string();
            }

            // Merge focus areas by id — replace existing, append new
            if !inline.focus_areas.is_empty() {
                for area in &inline.focus_areas {
                    if let Some(existing) = profile.focus_areas.iter_mut().find(|a| a.id == area.id)
                    {
                        // Replace existing focus area with override
                        *existing = area.clone();
                    } else {
                        // Append new focus area
                        profile.focus_areas.push(area.clone());
                    }
                }
            }

            // Override ignore areas
            if !inline.ignore_areas.is_empty() {
                profile.ignore_areas = inline.ignore_areas.clone();
            }

            // Override review style
            if let Some(style) = &inline.review_style {
                profile.review_style = style.clone();
            }

            Ok(profile)
        }
    }
}

/// Auto-detect a profile based on project language.
/// Returns the best-matching built-in profile name.
#[allow(dead_code)]
pub fn auto_detect_profile(language: &str) -> &'static str {
    match language {
        "rs" => "rust-strict",
        "ts" | "js" => "typescript-strict",
        "go" => "go-pragmatic",
        _ => "clean-code",
    }
}

/// Build the profile instruction text that gets injected into the LLM prompt.
///
/// This replaces the generic focus area list with profile-specific instructions.
pub fn build_profile_prompt(profile: &Profile) -> String {
    let mut parts = Vec::new();

    parts.push(format!("Reviewing using the '{}' profile.", profile.name));
    if !profile.description.is_empty() {
        parts.push(profile.description.clone());
    }

    // Focus areas with rules
    if !profile.focus_areas.is_empty() {
        parts.push("\nFOCUSED REVIEW PRIORITIES:".to_string());
        // Sort by weight descending
        let mut areas = profile.focus_areas.clone();
        areas.sort_by_key(|b| std::cmp::Reverse(b.weight));

        for area in &areas {
            let action_label = if area.action == "block" {
                "BLOCK"
            } else {
                "WARN"
            };
            parts.push(format!(
                "\n[{}] {} (weight: {}/10, action: {})",
                area.id.to_uppercase(),
                area.id,
                area.weight,
                action_label
            ));
            for rule in &area.rules {
                parts.push(format!("  - {rule}"));
            }
        }
    }

    // Ignore areas
    if !profile.ignore_areas.is_empty() {
        parts.push(format!(
            "\nIGNORE these areas (do NOT report): {}",
            profile.ignore_areas.join(", ")
        ));
    }

    // Review style
    let style = &profile.review_style;
    if !style.tone.is_empty() && style.tone != "standard" {
        parts.push(format!("\nReview tone: {}.", style.tone));
    }
    if !style.detail_level.is_empty() && style.detail_level != "standard" {
        parts.push(format!("Detail level: {}.", style.detail_level));
    }
    if !style.suggest_fixes {
        parts.push("Do NOT suggest fixes — just report issues.".to_string());
    }
    if let Some(max) = style.max_findings {
        parts.push(format!("Report at most {max} findings."));
    }

    parts.join("\n")
}

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

    #[test]
    fn load_all_builtins_succeeds() {
        let profiles = load_all_builtins();
        assert_eq!(profiles.len(), 8, "should have 8 built-in profiles");
        for p in &profiles {
            assert!(!p.name.is_empty(), "profile name should not be empty");
            assert!(!p.description.is_empty(), "profile should have description");
        }
    }

    #[test]
    fn load_builtin_security_first() {
        let p = load_builtin("security-first").unwrap();
        assert_eq!(p.name, "security-first");
        assert!(!p.focus_areas.is_empty());
        assert!(p.ignore_areas.contains(&"style".to_string()));
        assert_eq!(p.review_style.tone, "strict");
    }

    #[test]
    fn load_builtin_rust_strict() {
        let p = load_builtin("rust-strict").unwrap();
        assert_eq!(p.name, "rust-strict");
        assert!(p.focus_areas.iter().any(|a| a.id == "unsafe_usage"));
    }

    #[test]
    fn load_builtin_unknown_returns_none() {
        assert!(load_builtin("nonexistent").is_none());
    }

    #[test]
    fn resolve_builtin_name() {
        let r = ProfileRef::Name("minimal".to_string());
        let p = resolve_profile(&r).unwrap();
        assert_eq!(p.name, "minimal");
    }

    #[test]
    fn resolve_unknown_name_fails() {
        let r = ProfileRef::Name("nope".to_string());
        assert!(resolve_profile(&r).is_err());
    }

    #[test]
    fn resolve_inline_without_extends() {
        let r = ProfileRef::Inline(InlineProfileRef {
            extends: None,
            focus_areas: vec![FocusArea {
                id: "test".to_string(),
                weight: 5,
                action: "warn".to_string(),
                rules: vec!["No test rule".to_string()],
            }],
            ignore_areas: vec!["docs".to_string()],
            review_style: None,
        });
        let p = resolve_profile(&r).unwrap();
        assert_eq!(p.name, "custom");
        assert_eq!(p.focus_areas.len(), 1);
        assert_eq!(p.ignore_areas, vec!["docs"]);
    }

    #[test]
    fn resolve_inline_extends_builtin() {
        let r = ProfileRef::Inline(InlineProfileRef {
            extends: Some("minimal".to_string()),
            focus_areas: vec![FocusArea {
                id: "compliance".to_string(),
                weight: 8,
                action: "warn".to_string(),
                rules: vec!["Check data processing consent".to_string()],
            }],
            ignore_areas: vec![],
            review_style: Some(ReviewStyle {
                tone: "strict".to_string(),
                detail_level: "high".to_string(),
                suggest_fixes: true,
                max_findings: Some(15),
            }),
        });
        let p = resolve_profile(&r).unwrap();
        assert_eq!(p.name, "custom-minimal");
        // Should have base focus areas + custom
        assert!(p.focus_areas.len() > 2);
        assert!(p.focus_areas.iter().any(|a| a.id == "compliance"));
        assert_eq!(p.review_style.tone, "strict");
        assert_eq!(p.review_style.max_findings, Some(15));
    }

    #[test]
    fn auto_detect_profiles() {
        assert_eq!(auto_detect_profile("rs"), "rust-strict");
        assert_eq!(auto_detect_profile("ts"), "typescript-strict");
        assert_eq!(auto_detect_profile("js"), "typescript-strict");
        assert_eq!(auto_detect_profile("go"), "go-pragmatic");
        assert_eq!(auto_detect_profile("py"), "clean-code");
        assert_eq!(auto_detect_profile("unknown"), "clean-code");
    }

    #[test]
    fn build_profile_prompt_not_empty() {
        let p = load_builtin("security-first").unwrap();
        let prompt = build_profile_prompt(&p);
        assert!(prompt.contains("security-first"));
        assert!(prompt.contains("FOCUSED REVIEW PRIORITIES"));
        assert!(prompt.contains("IGNORE these areas"));
        assert!(prompt.contains("strict"));
    }

    #[test]
    fn build_profile_prompt_minimal() {
        let p = Profile {
            name: "custom".to_string(),
            ..Default::default()
        };
        let prompt = build_profile_prompt(&p);
        assert!(prompt.contains("custom"));
    }

    #[test]
    fn parse_profile_yaml_invalid() {
        let result = parse_profile_yaml("not: valid: yaml:::");
        assert!(result.is_err());
    }

    #[test]
    fn profile_roundtrip_yaml() {
        let p = load_builtin("performance").unwrap();
        let yaml = serde_yaml_ng::to_string(&p).unwrap();
        let back: Profile = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(back.name, p.name);
        assert_eq!(back.focus_areas.len(), p.focus_areas.len());
    }

    // ─── #81: Profile::validate ───

    #[test]
    fn validate_accepts_all_builtins() {
        for name in BUILTIN_PROFILES {
            let p = load_builtin(name).unwrap_or_else(|| panic!("missing builtin {name}"));
            assert!(p.validate().is_ok(), "builtin '{name}' failed validation");
        }
    }

    #[test]
    fn validate_rejects_weight_out_of_range() {
        let p = Profile {
            name: "test".into(),
            focus_areas: vec![FocusArea {
                id: "x".into(),
                weight: 15,
                action: "warn".into(),
                rules: vec![],
            }],
            ..Default::default()
        };
        let err = p.validate().unwrap_err();
        assert!(err.contains("weight"), "err: {err}");
    }

    #[test]
    fn validate_rejects_unknown_action() {
        let p = Profile {
            name: "test".into(),
            focus_areas: vec![FocusArea {
                id: "x".into(),
                weight: 5,
                action: "blok".into(), // typo
                rules: vec![],
            }],
            ..Default::default()
        };
        let err = p.validate().unwrap_err();
        assert!(err.contains("action"), "err: {err}");
    }

    #[test]
    fn validate_rejects_unknown_tone() {
        let p = Profile {
            name: "test".into(),
            review_style: ReviewStyle {
                tone: "angry".into(),
                detail_level: "standard".into(),
                suggest_fixes: true,
                max_findings: None,
            },
            ..Default::default()
        };
        assert!(p.validate().is_err());
    }
}