Skip to main content

ag_session/
review.rs

1//! Pure focused-review parsing helpers shared across session frontends.
2
3use std::fmt;
4use std::str::FromStr;
5
6use ag_protocol::TurnPrompt;
7
8/// Durable state of one focused-review generation attempt.
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum FocusedReviewStatus {
11    /// Review generation is still running.
12    Pending,
13    /// Review generation completed and persisted its markdown.
14    Ready,
15    /// Review generation completed without usable markdown.
16    Failed,
17}
18
19impl fmt::Display for FocusedReviewStatus {
20    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
21        formatter.write_str(match self {
22            Self::Pending => "Pending",
23            Self::Ready => "Ready",
24            Self::Failed => "Failed",
25        })
26    }
27}
28
29impl FromStr for FocusedReviewStatus {
30    type Err = String;
31
32    fn from_str(value: &str) -> Result<Self, Self::Err> {
33        match value {
34            "Pending" => Ok(Self::Pending),
35            "Ready" => Ok(Self::Ready),
36            "Failed" => Ok(Self::Failed),
37            _ => Err(format!("Unknown focused review status: {value}")),
38        }
39    }
40}
41
42/// Builds the agent-facing `/apply` prompt from focused-review suggestions.
43///
44/// The prompt explicitly asks the agent to verify each suggestion against the
45/// current code before making changes, then apply only suggestions that remain
46/// correct and relevant.
47pub fn build_apply_review_prompt(suggestions: &str) -> TurnPrompt {
48    let suggestions = suggestions.trim();
49    let fence = ag_agent::diff_fence(suggestions);
50    let fenced_suggestions = format!("{fence}text\n{suggestions}\n{fence}");
51    let prompt = APPLY_REVIEW_PROMPT_TEMPLATE
52        .trim_end()
53        .replace("{{ fenced_suggestions }}", &fenced_suggestions);
54
55    TurnPrompt::from_text(prompt)
56}
57
58/// Extracts actionable suggestion content from focused-review markdown.
59///
60/// Returns `None` when the `### Suggestions` section is missing, empty, or
61/// reports `- None` with optional trailing punctuation.
62#[must_use]
63pub fn review_suggestions(review_text: &str) -> Option<String> {
64    let suggestions_header = "### Suggestions";
65    let header_start = review_text.find(suggestions_header)?;
66    let content_start = header_start + suggestions_header.len();
67    let content = &review_text[content_start..];
68    let section_end = content.find("\n### ").unwrap_or(content.len());
69    let suggestions = content[..section_end].trim();
70
71    if suggestions.is_empty() || is_no_suggestions_sentinel(suggestions) {
72        return None;
73    }
74
75    Some(suggestions.to_string())
76}
77
78/// Returns whether focused-review markdown contains suggestions that `/apply`
79/// can act on.
80#[must_use]
81pub fn has_actionable_review_suggestions(review_text: Option<&str>) -> bool {
82    review_text.and_then(review_suggestions).is_some()
83}
84
85const APPLY_REVIEW_PROMPT_TEMPLATE: &str = include_str!("template/apply_review_prompt.md");
86
87/// Returns whether a suggestions section contains only the required `None`
88/// sentinel plus optional trailing punctuation.
89fn is_no_suggestions_sentinel(suggestions: &str) -> bool {
90    suggestions.strip_prefix("- None").is_some_and(|suffix| {
91        suffix
92            .chars()
93            .all(|character| character.is_ascii_punctuation())
94    })
95}
96
97#[cfg(test)]
98#[path = "review_test.rs"]
99mod tests;