Skip to main content

ag_protocol/
review.rs

1//! Structured focused-review response contract and display formatting.
2
3use std::fmt;
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Structured result returned by a focused-review utility prompt.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
10#[serde(deny_unknown_fields)]
11#[schemars(
12    title = "FocusedReview",
13    description = "Structured focused-review result. Project impact describes the overall effect \
14                   of the diff, while suggestions contains only actionable high- or \
15                   medium-severity findings."
16)]
17pub struct FocusedReview {
18    /// Concise statements describing the overall effect of the reviewed diff.
19    #[schemars(
20        title = "project_impact",
21        description = "Concise statements about the diff's effects on behavior, reliability, \
22                       maintainability, performance, security, or developer workflow. Use an \
23                       empty array when there is no notable impact."
24    )]
25    pub project_impact: Vec<String>,
26    /// Actionable high- or medium-severity findings from the reviewed diff.
27    #[schemars(
28        title = "suggestions",
29        description = "Actionable findings scoped to the reviewed diff, ordered by severity with \
30                       high severity first. Use an empty array when there are no suggestions."
31    )]
32    pub suggestions: Vec<FocusedReviewSuggestion>,
33}
34
35impl FocusedReview {
36    /// Formats the structured review for the terminal session transcript.
37    #[must_use]
38    pub fn to_markdown(&self) -> String {
39        let project_impact = markdown_bullets(&self.project_impact);
40        let suggestions = if self.suggestions.is_empty() {
41            "- None".to_string()
42        } else {
43            self.suggestions
44                .iter()
45                .map(|suggestion| {
46                    format!("- [{}]: {}", suggestion.severity, suggestion.details.trim())
47                })
48                .collect::<Vec<_>>()
49                .join("\n")
50        };
51
52        format!(
53            "## Review\n\n### Project Impact\n\n{project_impact}\n\n### \
54             Suggestions\n\n{suggestions}"
55        )
56    }
57}
58
59/// One actionable focused-review finding.
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
61#[serde(deny_unknown_fields)]
62#[schemars(
63    title = "FocusedReviewSuggestion",
64    description = "One actionable high- or medium-severity finding scoped to the reviewed diff."
65)]
66pub struct FocusedReviewSuggestion {
67    /// Concise explanation of the issue and its practical impact.
68    #[schemars(
69        title = "details",
70        description = "Concise issue details, including relevant repository-root-relative file \
71                       and line references when available, plus the practical impact."
72    )]
73    pub details: String,
74    /// Severity assigned from the focused-review severity policy.
75    #[schemars(
76        title = "severity",
77        description = "Finding severity: high for correctness, security, data-loss, or \
78                       build-breaking risks; medium for concrete reliability, maintainability, \
79                       performance, or workflow risks."
80    )]
81    pub severity: FocusedReviewSeverity,
82}
83
84/// Supported severity levels for actionable focused-review findings.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
86#[serde(rename_all = "lowercase")]
87#[schemars(
88    title = "FocusedReviewSeverity",
89    description = "Supported severity for an actionable focused-review finding."
90)]
91pub enum FocusedReviewSeverity {
92    /// Correctness, security, data-loss, or build-breaking risk.
93    High,
94    /// Concrete reliability, maintainability, performance, or workflow risk.
95    Medium,
96}
97
98impl fmt::Display for FocusedReviewSeverity {
99    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
100        formatter.write_str(match self {
101            Self::High => "High",
102            Self::Medium => "Medium",
103        })
104    }
105}
106
107/// Formats one project-impact list, using the stable empty sentinel when no
108/// impact was reported.
109fn markdown_bullets(items: &[String]) -> String {
110    if items.is_empty() {
111        return "- None".to_string();
112    }
113
114    items
115        .iter()
116        .map(|item| format!("- {}", item.trim()))
117        .collect::<Vec<_>>()
118        .join("\n")
119}
120
121#[cfg(test)]
122#[path = "review_test.rs"]
123mod tests;