rs-guard 1.0.0

AI-powered code review CLI for GitHub PRs
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
//! Terminal output formatting and review artifact writing.
//!
//! Provides functions for writing structured review artifacts and printing
//! color-coded summaries to the terminal for local mode.

use crate::verdict::{ReviewState, Verdict};
use colored::Colorize;
use serde::Serialize;
use std::io::Write;

/// Default filename for the review result artifact.
pub const ARTIFACT_FILENAME: &str = "review-result.txt";

/// Default filename for the metrics JSON artifact.
pub const METRICS_FILENAME: &str = "rs-guard-metrics.json";

/// Per-run metrics for observability and cost tracking.
#[derive(Debug, Clone, Serialize)]
pub struct ReviewMetrics {
    /// LLM provider name.
    pub provider: String,
    /// Model identifier.
    pub model: String,
    /// Estimated input tokens sent to the LLM (character count / 4 heuristic).
    pub estimated_tokens_in: usize,
    /// Estimated output tokens received from the LLM (character count / 4 heuristic).
    pub estimated_tokens_out: usize,
    /// API latency in seconds.
    pub latency_secs: f64,
    /// Estimated cost in cents (USD).
    pub estimated_cost_cents: f64,
    /// Diff size in lines.
    pub diff_lines: usize,
    /// Parsed verdict string.
    pub verdict: String,
    /// Review state.
    pub state: String,
}

/// Metadata about the review run, used for artifact and console output.
#[derive(Debug, Clone)]
pub struct ReviewConfig {
    /// LLM provider name.
    pub provider: String,
    /// Model identifier.
    pub model: String,
    /// Sampling temperature used.
    pub temperature: f32,
    /// Pull request number (if in CI mode).
    pub pr_number: Option<u64>,
    /// Diff size in bytes.
    pub diff_size_bytes: usize,
    /// Diff line count.
    pub diff_line_count: usize,
}

/// Writes a structured review result file for downstream CI jobs.
///
/// The artifact includes provider metadata, the full LLM review text,
/// and the parsed verdict metadata. The `--- Parsed Metadata ---` section
/// renders all five verdict fields: `Verdict`, `CriticalIssues`,
/// `SecurityIssues`, `ImportantIssues`, and `Suggestions`.
///
/// # Errors
///
/// Returns [`std::io::Error`] if the file cannot be created or written.
pub fn write_artifact(
    review: &str,
    verdict: &Verdict,
    state: &ReviewState,
    config: &ReviewConfig,
    path: &str,
) -> std::io::Result<()> {
    let content = format!(
        "rs-guard Review Result
==========================
Provider: {}
Model: {}
Temperature: {}
Diff Size: {} lines ({} bytes)
Review State: {}

--- LLM Review ---
{}

--- Parsed Metadata ---
Verdict:         {}
CriticalIssues:  {}
SecurityIssues:  {}
ImportantIssues: {}
Suggestions:     {}
",
        config.provider,
        config.model,
        config.temperature,
        config.diff_line_count,
        config.diff_size_bytes,
        state,
        review,
        verdict.verdict,
        verdict.critical_issues,
        verdict.security_issues,
        verdict.important_issues,
        verdict.suggestions,
    );

    // Create parent directory if it doesn't exist
    if let Some(parent) = std::path::Path::new(path).parent() {
        if !parent.as_os_str().is_empty() && !parent.exists() {
            std::fs::create_dir_all(parent)?;
        }
    }

    let mut file = std::fs::File::create(path)?;
    file.write_all(content.as_bytes())?;
    Ok(())
}

/// Prints the full review text with a color-coded state header to a writer.
///
/// Renders all five verdict fields: `Verdict`, `Critical Issues`,
/// `Security Issues`, `Important Issues`, and `Suggestions`.
/// Accepts any [`std::io::Write`] implementation for testability.
/// The `colored` crate's ANSI codes are preserved in the output.
///
/// # Arguments
///
/// * `review` — The full LLM review text.
/// * `verdict` — Parsed verdict metadata.
/// * `state` — Determined review state.
/// * `writer` — Output destination (e.g. `std::io::stdout()`, `Vec<u8>`).
///
/// # Errors
///
/// Returns [`std::io::Error`] if writing to the output fails.
pub fn print_colored_report(
    review: &str,
    verdict: &Verdict,
    state: &ReviewState,
    writer: &mut impl Write,
) -> std::io::Result<()> {
    writeln!(writer, "{}", "rs-guard Review".bold().underline())?;
    writeln!(writer)?;

    match state {
        ReviewState::Approve => {
            writeln!(writer, "{}", "✓ State: APPROVE".green().bold())?;
        }
        ReviewState::RequestChanges => {
            writeln!(writer, "{}", "✗ State: REQUEST_CHANGES".red().bold())?;
        }
        ReviewState::Comment => {
            writeln!(writer, "{}", "→ State: COMMENT".yellow().bold())?;
        }
    }

    writeln!(writer)?;
    writeln!(writer, "Verdict:          {}", verdict.verdict)?;
    writeln!(writer, "Critical Issues:  {}", verdict.critical_issues)?;
    writeln!(writer, "Security Issues:  {}", verdict.security_issues)?;
    writeln!(writer, "Important Issues: {}", verdict.important_issues)?;
    writeln!(writer, "Suggestions:      {}", verdict.suggestions)?;
    writeln!(writer)?;
    writeln!(writer, "{}", review)?;
    Ok(())
}

/// Writes a structured metrics JSON file for downstream analysis.
///
/// # Errors
///
/// Returns [`std::io::Error`] if the file cannot be created or written.
pub fn write_metrics(metrics: &ReviewMetrics, path: &str) -> std::io::Result<()> {
    // Create parent directory if it doesn't exist
    if let Some(parent) = std::path::Path::new(path).parent() {
        if !parent.as_os_str().is_empty() && !parent.exists() {
            std::fs::create_dir_all(parent)?;
        }
    }
    let json = serde_json::to_string_pretty(metrics).map_err(std::io::Error::other)?;
    std::fs::write(path, json)
}

/// Prints a colored summary of the review with full metadata to a writer.
///
/// Convenience wrapper around [`print_colored_report`] that adds a metadata
/// section. Accepts any [`std::io::Write`] implementation for testability.
///
/// # Arguments
///
/// * `review` — The full LLM review text.
/// * `verdict` — Parsed verdict metadata.
/// * `state` — Determined review state.
/// * `config` — Review configuration (provider, model, etc.).
/// * `writer` — Output destination.
///
/// # Errors
///
/// Returns [`std::io::Error`] if writing to the output fails.
pub fn print_colored_summary(
    review: &str,
    verdict: &Verdict,
    state: &ReviewState,
    config: &ReviewConfig,
    writer: &mut impl Write,
) -> std::io::Result<()> {
    print_colored_report(review, verdict, state, writer)?;

    writeln!(writer)?;
    writeln!(writer, "{}", "--- Metadata ---".dimmed())?;
    writeln!(writer, "Provider:    {}", config.provider)?;
    writeln!(writer, "Model:       {}", config.model)?;
    writeln!(writer, "Temperature: {}", config.temperature)?;
    writeln!(writer, "Diff Lines:  {}", config.diff_line_count)?;
    Ok(())
}

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

    #[test]
    fn test_write_metrics_creates_json_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("metrics.json");
        let path_str = path.to_str().unwrap();

        let metrics = ReviewMetrics {
            provider: "deepseek".to_string(),
            model: "deepseek-v4-flash".to_string(),
            estimated_tokens_in: 4230,
            estimated_tokens_out: 892,
            latency_secs: 8.4,
            estimated_cost_cents: 3.0,
            diff_lines: 150,
            verdict: "POSITIVE".to_string(),
            state: "APPROVE".to_string(),
        };

        write_metrics(&metrics, path_str).unwrap();

        let content = std::fs::read_to_string(path_str).unwrap();
        assert!(content.contains("deepseek"));
        assert!(content.contains("4230"));
        assert!(content.contains("APPROVE"));
        assert!(content.contains("3"));
    }

    #[test]
    fn test_write_artifact_creates_file_with_correct_content() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("review.txt");
        let path_str = path.to_str().unwrap();

        let verdict = Verdict {
            verdict: "POSITIVE".to_string(),
            critical_issues: 0,
            security_issues: 0,
            important_issues: 0,
            suggestions: 0,
        };
        let state = ReviewState::Approve;
        let config = ReviewConfig {
            provider: "deepseek".to_string(),
            model: "deepseek-v4-flash".to_string(),
            temperature: 0.1,
            pr_number: Some(42),
            diff_size_bytes: 1024,
            diff_line_count: 50,
        };

        write_artifact("looks good", &verdict, &state, &config, path_str).unwrap();

        let content = std::fs::read_to_string(path_str).unwrap();
        assert!(content.contains("APPROVE"));
        assert!(content.contains("POSITIVE"));
        assert!(content.contains("deepseek"));
        assert!(content.contains("deepseek-v4-flash"));
        assert!(content.contains("looks good"));
        assert!(content.contains("CriticalIssues:  0"));
        assert!(content.contains("SecurityIssues:  0"));
        assert!(content.contains("ImportantIssues: 0"));
        assert!(content.contains("Suggestions:     0"));
    }

    #[test]
    fn test_write_artifact_propagates_io_error() {
        let result = write_artifact(
            "test",
            &Verdict {
                verdict: "POSITIVE".to_string(),
                critical_issues: 0,
                security_issues: 0,
                important_issues: 0,
                suggestions: 0,
            },
            &ReviewState::Comment,
            &ReviewConfig {
                provider: "test".to_string(),
                model: "test".to_string(),
                temperature: 0.0,
                pr_number: None,
                diff_size_bytes: 0,
                diff_line_count: 0,
            },
            "/nonexistent/dir/artifact.txt",
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_print_colored_report_approve() {
        let verdict = Verdict {
            verdict: "POSITIVE".to_string(),
            critical_issues: 0,
            security_issues: 0,
            important_issues: 0,
            suggestions: 0,
        };
        let mut buf = Vec::new();
        print_colored_report("all good", &verdict, &ReviewState::Approve, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(output.contains("APPROVE"));
        assert!(output.contains("all good"));
    }

    #[test]
    fn test_print_colored_report_request_changes() {
        let verdict = Verdict {
            verdict: "NEGATIVE".to_string(),
            critical_issues: 3,
            security_issues: 1,
            important_issues: 0,
            suggestions: 0,
        };
        let mut buf = Vec::new();
        print_colored_report(
            "fix these issues",
            &verdict,
            &ReviewState::RequestChanges,
            &mut buf,
        )
        .unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(output.contains("REQUEST_CHANGES"));
        assert!(output.contains("fix these issues"));
        assert!(output.contains("Critical Issues:  3"));
        assert!(output.contains("Security Issues:  1"));
    }

    #[test]
    fn test_print_colored_summary_includes_metadata() {
        let verdict = Verdict {
            verdict: "POSITIVE".to_string(),
            critical_issues: 0,
            security_issues: 0,
            important_issues: 0,
            suggestions: 0,
        };
        let config = ReviewConfig {
            provider: "openai".to_string(),
            model: "gpt-4o".to_string(),
            temperature: 0.5,
            pr_number: None,
            diff_size_bytes: 512,
            diff_line_count: 25,
        };
        let mut buf = Vec::new();
        print_colored_summary(
            "review text",
            &verdict,
            &ReviewState::Comment,
            &config,
            &mut buf,
        )
        .unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(output.contains("openai"));
        assert!(output.contains("gpt-4o"));
        assert!(output.contains("0.5"));
        assert!(output.contains("25"));
        assert!(output.contains("COMMENT"));
    }

    #[test]
    fn test_write_artifact_includes_important_and_suggestions() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("review.txt");
        let path_str = path.to_str().unwrap();

        let verdict = Verdict {
            verdict: "POSITIVE".to_string(),
            critical_issues: 0,
            security_issues: 0,
            important_issues: 2,
            suggestions: 5,
        };
        let config = ReviewConfig {
            provider: "openai".to_string(),
            model: "gpt-4o".to_string(),
            temperature: 0.1,
            pr_number: None,
            diff_size_bytes: 512,
            diff_line_count: 20,
        };

        write_artifact("review", &verdict, &ReviewState::Comment, &config, path_str).unwrap();

        let content = std::fs::read_to_string(path_str).unwrap();
        assert!(
            content.contains("ImportantIssues: 2"),
            "ImportantIssues missing"
        );
        assert!(
            content.contains("Suggestions:     5"),
            "Suggestions missing"
        );
    }

    #[test]
    fn test_print_colored_report_shows_important_and_suggestions() {
        let verdict = Verdict {
            verdict: "POSITIVE".to_string(),
            critical_issues: 0,
            security_issues: 0,
            important_issues: 2,
            suggestions: 4,
        };
        let mut buf = Vec::new();
        print_colored_report("ok", &verdict, &ReviewState::Comment, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(
            output.contains("Important Issues: 2"),
            "important issues missing"
        );
        assert!(
            output.contains("Suggestions:      4"),
            "suggestions missing"
        );
    }

    #[test]
    fn test_review_config_display_format() {
        let config = ReviewConfig {
            provider: "test-provider".to_string(),
            model: "test-model".to_string(),
            temperature: 0.3,
            pr_number: Some(99),
            diff_size_bytes: 2048,
            diff_line_count: 100,
        };
        assert_eq!(config.provider, "test-provider");
        assert_eq!(config.model, "test-model");
        assert_eq!(config.pr_number, Some(99));
    }
}