//! The pieces of a review round both loops read the same way: whether an //! announced page is a round's session, the round's `roundOutcome` in a session //! state, the report text it names, the proofs the app checks for itself, and //! the reviewer's findings as the fixer's brief. The Review Loop and the //! Implementation Loop's extra review rounds (a phase's hook) both call them. use serde_json::Value; use crate::round_logic::{Finding, Role, RoundOutcome, RoundProofs}; /// The word a role goes by in a console state and a prompt. pub fn role_word(role: Role) -> &'static str { match role { Role::Both => "both", Role::Reviewer => "reviewer", Role::Fixer => "fixer", } } /// Whether an announced page is this round's session: the agent locked it with /// `open --loop` for this round and role, and it holds no report yet. pub fn page_is_this_round(state: &Value, round: u32, role: Role) -> bool { state["loop"]["driving"].as_bool().unwrap_or(false) && state["loop"]["round"].as_u64() == Some(u64::from(round)) && state["loop"]["role"].as_str() == Some(role_word(role)) && state.get("roundOutcome").is_none_or(Value::is_null) } /// The round's outcome in `state`, when it is this round's and this role's. pub fn round_outcome_in( state: &Value, round: u32, role: Role, ) -> Option> { let raw = state .get("roundOutcome") .filter(|value| value.is_object())?; match serde_json::from_value::(raw.clone()) { Ok(outcome) => (outcome.round == round && outcome.role == role).then_some(Ok(outcome)), Err(err) => Some(Err(format!("the round's report could not be read ({err})"))), } } /// The text of the report doc the outcome names, from the session's `docs`. pub fn report_text(state: &Value, id: &str) -> Option { let doc = state["docs"] .as_array()? .iter() .find(|doc| doc["id"].as_str() == Some(id))?; let mut text = String::new(); if let Some(blocks) = doc["blocks"].as_array() { for block in blocks { if let Some(md) = block["md"].as_str() { text.push_str(md); text.push_str("\n\n"); } } } Some(text) } pub fn proofs_for(state: &Value, outcome: &RoundOutcome, role: Role) -> RoundProofs { let report_doc_present = match role { Role::Fixer => true, _ => outcome .report .as_deref() .is_some_and(|id| report_text(state, id).is_some()), }; let (tests_ran, tests_passed) = match role { // A reviewer changes nothing and is not asked for tests. Role::Reviewer => (true, true), _ => outcome .tests .as_ref() .map_or((false, false), |t| (t.ran, t.passed)), }; RoundProofs { report_doc_present, tests_ran, tests_passed, } } pub fn finding_line(finding: &Finding) -> String { match finding.line { Some(line) => format!("- {} ({}:{line})", finding.title, finding.file), None => format!("- {} ({})", finding.title, finding.file), } } /// The reviewer's findings, as the fixer's brief: the report's text and the /// Critical and Warning findings the reviewer left unfixed. pub fn reviewer_brief(text: &str, outcome: &RoundOutcome) -> String { let mut brief = format!( "# Reviewer report\n\n{}\n\nFound: {} Critical, {} Warning, {} Suggestion.\n\n", text.trim(), outcome.review.critical, outcome.review.warning, outcome.review.suggestion ); if !outcome.review.unfixed.is_empty() { brief.push_str("## Fix these\n\n"); for finding in &outcome.review.unfixed { brief.push_str(&finding_line(finding)); brief.push('\n'); } } brief }