repotoire 0.3.47

Graph-powered code analysis CLI. 81 detectors for security, architecture, and code quality.
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
//! Fix proposal generation from findings
//!
//! Uses LLM to generate code fixes based on analysis findings.

use crate::ai::{AiClient, AiError, AiResult, FixPromptBuilder, Message, PromptTemplate};
use crate::models::{Finding, Severity};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

/// Type of fix being proposed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FixType {
    Refactor,
    Simplify,
    Extract,
    Rename,
    Remove,
    Security,
    TypeHint,
    Documentation,
}

/// Confidence level of the fix
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FixConfidence {
    High,
    Medium,
    Low,
}

/// A single code change within a fix
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeChange {
    pub file_path: PathBuf,
    pub original_code: String,
    pub fixed_code: String,
    pub start_line: u32,
    pub end_line: u32,
    pub description: String,
}

/// Evidence supporting a fix
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Evidence {
    pub similar_patterns: Vec<String>,
    pub documentation_refs: Vec<String>,
    pub best_practices: Vec<String>,
}

/// A proposed fix for a finding
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FixProposal {
    pub id: String,
    pub finding_id: String,
    pub fix_type: FixType,
    pub confidence: FixConfidence,
    pub title: String,
    pub description: String,
    pub rationale: String,
    pub changes: Vec<CodeChange>,
    pub evidence: Evidence,
    pub syntax_valid: bool,
}

impl FixProposal {
    /// Generate a unified diff for the changes
    pub fn diff(&self, repo_path: &Path) -> String {
        let mut diff = String::new();

        for change in &self.changes {
            // Simple line-based diff
            diff.push_str(&format!("--- a/{}\n", change.file_path.display()));
            diff.push_str(&format!("+++ b/{}\n", change.file_path.display()));
            diff.push_str(&format!(
                "@@ -{},{} +{},{} @@\n",
                change.start_line,
                change.end_line - change.start_line + 1,
                change.start_line,
                change.fixed_code.lines().count()
            ));

            // Show removed lines
            for line in change.original_code.lines() {
                diff.push_str(&format!("-{}\n", line));
            }

            // Show added lines
            for line in change.fixed_code.lines() {
                diff.push_str(&format!("+{}\n", line));
            }

            diff.push('\n');
        }

        diff
    }

    /// Apply the fix to files
    pub fn apply(&self, repo_path: &Path) -> AiResult<()> {
        for change in &self.changes {
            let file_path = repo_path.join(&change.file_path);

            let content = fs::read_to_string(&file_path)?;
            let new_content = content.replace(&change.original_code, &change.fixed_code);

            if new_content == content {
                return Err(AiError::ParseError(format!(
                    "Original code not found in {}",
                    change.file_path.display()
                )));
            }

            fs::write(&file_path, new_content)?;
        }

        Ok(())
    }
}

/// Generator for AI-powered code fixes
pub struct FixGenerator {
    client: AiClient,
}

impl FixGenerator {
    pub fn new(client: AiClient) -> Self {
        Self { client }
    }

    /// Generate a fix for a finding
    pub async fn generate_fix(
        &self,
        finding: &Finding,
        repo_path: &Path,
    ) -> AiResult<FixProposal> {
        // Determine language from file extension
        let language = finding
            .affected_files
            .first()
            .and_then(|p| p.extension())
            .and_then(|e| e.to_str())
            .map(extension_to_language)
            .unwrap_or("python");

        // Determine fix type from finding
        let fix_type = determine_fix_type(finding);

        // Read the affected code section
        let code_section = self.read_code_section(finding, repo_path)?;

        // Build the prompt
        let prompt = FixPromptBuilder::new(finding.clone(), fix_type, language)
            .code_section(&code_section)
            .build();

        // Get system prompt for language
        let system_prompt = PromptTemplate::system_prompt(language);

        // Call LLM
        let response = self
            .client
            .generate(vec![Message::user(prompt)], Some(system_prompt))
            .await?;

        // Parse response
        let mut fix = self.parse_response(&response, finding, fix_type)?;

        // Validate syntax
        fix.syntax_valid = self.validate_syntax(&fix, language);

        // Validate original code exists
        if !self.validate_original_code(&fix, repo_path) {
            fix.confidence = FixConfidence::Low;
        }

        Ok(fix)
    }

    /// Generate a fix with retry on validation failure
    pub async fn generate_fix_with_retry(
        &self,
        finding: &Finding,
        repo_path: &Path,
        max_retries: u32,
    ) -> AiResult<FixProposal> {
        let mut last_errors: Vec<String> = Vec::new();

        for attempt in 0..=max_retries {
            let language = finding
                .affected_files
                .first()
                .and_then(|p| p.extension())
                .and_then(|e| e.to_str())
                .map(extension_to_language)
                .unwrap_or("python");

            let fix_type = determine_fix_type(finding);
            let code_section = self.read_code_section(finding, repo_path)?;

            let mut builder = FixPromptBuilder::new(finding.clone(), fix_type, language)
                .code_section(&code_section);

            if attempt > 0 && !last_errors.is_empty() {
                builder = builder.previous_errors(last_errors.clone());
            }

            let prompt = builder.build();
            let system_prompt = PromptTemplate::system_prompt(language);

            let response = self
                .client
                .generate(vec![Message::user(prompt)], Some(system_prompt))
                .await?;

            let mut fix = self.parse_response(&response, finding, fix_type)?;
            fix.syntax_valid = self.validate_syntax(&fix, language);

            // Check validation
            let mut errors = Vec::new();

            if !fix.syntax_valid {
                errors.push("SyntaxError: generated code has syntax errors".to_string());
            }

            if !self.validate_original_code(&fix, repo_path) {
                errors.push("MatchError: Original code not found in file".to_string());
            }

            if errors.is_empty() {
                return Ok(fix);
            }

            last_errors = errors;
        }

        // Return last attempt even if invalid
        self.generate_fix(finding, repo_path).await
    }

    fn read_code_section(&self, finding: &Finding, repo_path: &Path) -> AiResult<String> {
        let file_path = finding
            .affected_files
            .first()
            .ok_or_else(|| AiError::ParseError("No affected files".to_string()))?;

        let full_path = repo_path.join(file_path);
        let content = fs::read_to_string(&full_path)?;
        let lines: Vec<&str> = content.lines().collect();

        // Extract relevant section with context
        let start = finding.line_start.unwrap_or(1).saturating_sub(10) as usize;
        let end = finding
            .line_end
            .or(finding.line_start)
            .unwrap_or(1)
            .saturating_add(20) as usize;

        let start = start.min(lines.len());
        let end = end.min(lines.len());

        Ok(lines[start..end].join("\n"))
    }

    fn parse_response(
        &self,
        response: &str,
        finding: &Finding,
        fix_type: FixType,
    ) -> AiResult<FixProposal> {
        // Extract JSON from response (may be wrapped in markdown)
        let json_regex = Regex::new(r"```json\s*(\{.*?\})\s*```").unwrap();
        let json_str = json_regex
            .captures(response)
            .and_then(|c| c.get(1))
            .map(|m| m.as_str())
            .unwrap_or(response);

        let data: serde_json::Value = serde_json::from_str(json_str).map_err(|e| {
            AiError::ParseError(format!("Failed to parse JSON response: {}", e))
        })?;

        // Extract changes
        let changes: Vec<CodeChange> = data
            .get("changes")
            .and_then(|c| c.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|change| {
                        Some(CodeChange {
                            file_path: PathBuf::from(
                                change.get("file_path")?.as_str()?,
                            ),
                            original_code: change.get("original_code")?.as_str()?.to_string(),
                            fixed_code: change.get("fixed_code")?.as_str()?.to_string(),
                            start_line: change.get("start_line")?.as_u64()? as u32,
                            end_line: change.get("end_line")?.as_u64()? as u32,
                            description: change
                                .get("description")
                                .and_then(|d| d.as_str())
                                .unwrap_or("")
                                .to_string(),
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();

        // Extract evidence
        let evidence = data.get("evidence").map(|e| Evidence {
            similar_patterns: extract_string_array(e, "similar_patterns"),
            documentation_refs: extract_string_array(e, "documentation_refs"),
            best_practices: extract_string_array(e, "best_practices"),
        }).unwrap_or_default();

        // Calculate confidence
        let confidence = calculate_confidence(&data, finding, &changes);

        // Generate fix ID
        let fix_id = format!(
            "{:x}",
            md5::compute(format!(
                "{}:{}:{}",
                finding.id,
                finding.line_start.unwrap_or(0),
                chrono::Utc::now().timestamp()
            ))
        )[..12]
            .to_string();

        Ok(FixProposal {
            id: fix_id,
            finding_id: finding.id.clone(),
            fix_type,
            confidence,
            title: data
                .get("title")
                .and_then(|t| t.as_str())
                .unwrap_or("Auto-generated fix")
                .to_string(),
            description: data
                .get("description")
                .and_then(|d| d.as_str())
                .unwrap_or("")
                .to_string(),
            rationale: data
                .get("rationale")
                .and_then(|r| r.as_str())
                .unwrap_or("")
                .to_string(),
            changes,
            evidence,
            syntax_valid: false, // Set by validate_syntax
        })
    }

    fn validate_syntax(&self, fix: &FixProposal, language: &str) -> bool {
        for change in &fix.changes {
            // Basic syntax validation - just check for obvious issues
            let code = &change.fixed_code;

            match language {
                "python" => {
                    // Check for incomplete function definitions
                    if code.contains("def ") && !code.contains(':') {
                        return false;
                    }
                    // Check for unbalanced parentheses
                    if code.matches('(').count() != code.matches(')').count() {
                        return false;
                    }
                    // Check for unbalanced brackets
                    if code.matches('[').count() != code.matches(']').count() {
                        return false;
                    }
                }
                "javascript" | "typescript" => {
                    // Check for unbalanced braces
                    if code.matches('{').count() != code.matches('}').count() {
                        return false;
                    }
                }
                "rust" | "go" | "java" => {
                    // Check for unbalanced braces
                    if code.matches('{').count() != code.matches('}').count() {
                        return false;
                    }
                }
                _ => {}
            }
        }

        true
    }

    fn validate_original_code(&self, fix: &FixProposal, repo_path: &Path) -> bool {
        for change in &fix.changes {
            let file_path = repo_path.join(&change.file_path);
            if let Ok(content) = fs::read_to_string(&file_path) {
                // Try exact match first
                if content.contains(&change.original_code) {
                    continue;
                }

                // Try normalized match (ignore leading/trailing whitespace on lines)
                let normalized_original: String = change
                    .original_code
                    .lines()
                    .map(|l| l.trim())
                    .filter(|l| !l.is_empty())
                    .collect::<Vec<_>>()
                    .join("\n");

                let normalized_content: String = content
                    .lines()
                    .map(|l| l.trim())
                    .filter(|l| !l.is_empty())
                    .collect::<Vec<_>>()
                    .join("\n");

                if !normalized_content.contains(&normalized_original) {
                    return false;
                }
            } else {
                return false;
            }
        }

        true
    }
}

fn extract_string_array(value: &serde_json::Value, key: &str) -> Vec<String> {
    value
        .get(key)
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default()
}

fn determine_fix_type(finding: &Finding) -> FixType {
    let title = finding.title.to_lowercase();
    let description = finding.description.to_lowercase();

    // Security issues
    if finding.severity == Severity::Critical || title.contains("security") {
        return FixType::Security;
    }

    // Complexity issues
    if title.contains("complex") || description.contains("cyclomatic") {
        return FixType::Simplify;
    }

    // Dead code
    if title.contains("unused") || title.contains("dead code") {
        return FixType::Remove;
    }

    // Documentation
    if title.contains("docstring") || title.contains("documentation") {
        return FixType::Documentation;
    }

    // Type hints
    if title.contains("type") && description.contains("hint") {
        return FixType::TypeHint;
    }

    // Long methods
    if title.contains("long") || title.contains("too many") {
        return FixType::Extract;
    }

    FixType::Refactor
}

fn calculate_confidence(
    data: &serde_json::Value,
    finding: &Finding,
    changes: &[CodeChange],
) -> FixConfidence {
    let mut score = 0.5;

    // Boost if changes are small
    if changes.len() == 1 {
        score += 0.1;
    }

    // Boost if rationale is detailed
    if let Some(rationale) = data.get("rationale").and_then(|r| r.as_str()) {
        if rationale.len() > 100 {
            score += 0.1;
        }
    }

    // Reduce for critical findings (need careful review)
    if finding.severity == Severity::Critical {
        score -= 0.2;
    }

    // Boost for having evidence
    if let Some(evidence) = data.get("evidence") {
        if evidence.get("best_practices").and_then(|b| b.as_array()).map(|a| !a.is_empty()).unwrap_or(false) {
            score += 0.1;
        }
    }

    if score >= 0.9 {
        FixConfidence::High
    } else if score >= 0.7 {
        FixConfidence::Medium
    } else {
        FixConfidence::Low
    }
}

fn extension_to_language(ext: &str) -> &'static str {
    match ext {
        "py" => "python",
        "js" => "javascript",
        "ts" | "tsx" => "typescript",
        "rs" => "rust",
        "go" => "go",
        "java" => "java",
        _ => "python",
    }
}

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

    #[test]
    fn test_determine_fix_type() {
        let mut finding = Finding {
            id: "test".to_string(),
            detector: "test".to_string(),
            severity: Severity::Medium,
            title: "High cyclomatic complexity".to_string(),
            description: "Function has high complexity".to_string(),
            affected_files: vec![],
            line_start: None,
            line_end: None,
            suggested_fix: None,
            estimated_effort: None,
            category: None,
            cwe_id: None,
            why_it_matters: None,
            ..Default::default()
        };

        assert_eq!(determine_fix_type(&finding), FixType::Simplify);

        finding.title = "Unused variable".to_string();
        assert_eq!(determine_fix_type(&finding), FixType::Remove);

        finding.title = "Missing docstring".to_string();
        assert_eq!(determine_fix_type(&finding), FixType::Documentation);

        finding.severity = Severity::Critical;
        finding.title = "SQL injection vulnerability".to_string();
        assert_eq!(determine_fix_type(&finding), FixType::Security);
    }

    #[test]
    fn test_extension_to_language() {
        assert_eq!(extension_to_language("py"), "python");
        assert_eq!(extension_to_language("js"), "javascript");
        assert_eq!(extension_to_language("ts"), "typescript");
        assert_eq!(extension_to_language("rs"), "rust");
        assert_eq!(extension_to_language("go"), "go");
    }
}