diff --git a/compliance-agent/src/controls/scan_triage.rs b/compliance-agent/src/controls/scan_triage.rs index f7bf44a..5e08789 100644 --- a/compliance-agent/src/controls/scan_triage.rs +++ b/compliance-agent/src/controls/scan_triage.rs @@ -98,7 +98,9 @@ pub async fn triage_repo_findings( // to decide. TriageOutcome::NeedsReview => { finding.status = FindingStatus::NeedsReview; - finding.triage_action = Some("control_not_confirmed".to_string()); + finding.triage_action = Some( + compliance_core::models::finding::TRIAGE_CONTROL_NOT_CONFIRMED.to_string(), + ); finding.triage_rationale.get_or_insert_with(|| { "The control check did not confirm the CRA requirements this \ finding maps to. Please review whether it is a real issue." diff --git a/compliance-agent/src/llm/triage.rs b/compliance-agent/src/llm/triage.rs index 61c1996..f626afc 100644 --- a/compliance-agent/src/llm/triage.rs +++ b/compliance-agent/src/llm/triage.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fmt; -use compliance_core::models::{Exploitation, Finding, FindingStatus, Severity}; +use compliance_core::models::{Exploitation, Finding, FindingStatus, ReviewReason, Severity}; use serde::{Deserialize, Deserializer}; use serde_json::Value; @@ -72,7 +72,7 @@ const RESPONSE_SAMPLE_CHARS: usize = 500; /// `triage_action` of a finding the LLM triage gave no verdict; the reason is /// in `triage_rationale`, which starts with `skipped: `. -pub const SKIPPED_ACTION: &str = "skipped"; +pub const SKIPPED_ACTION: &str = compliance_core::models::finding::TRIAGE_SKIPPED; /// The model the triage asks. A trait so tests can drive the triage with a /// fake model. @@ -823,12 +823,12 @@ fn drop_trailing_comma(json: &mut String) { /// A dismissed or low-confidence finding is kept as /// [`FindingStatus::NeedsReview`], never deleted: the model's rationale goes /// with it, and a person decides. +/// +/// Which of the two it was is read back from the stored verdict +/// ([`Finding::review_reason`]), so the decision lives in one place +/// ([`ReviewReason::of_verdict`]). fn apply_action(finding: &mut Finding, action: &str, adjusted_confidence: f64) -> Outcome { match action { - "dismiss" => { - finding.status = FindingStatus::NeedsReview; - Outcome::Dismissed - } "downgrade" => { finding.severity = downgrade_severity(&finding.severity); finding.status = FindingStatus::Triaged; @@ -839,22 +839,24 @@ fn apply_action(finding: &mut Finding, action: &str, adjusted_confidence: f64) - finding.status = FindingStatus::Triaged; Outcome::Upgraded } - // "confirm" or unknown: kept as reported when the model is confident. - _ if adjusted_confidence >= CONFIDENCE_THRESHOLD => { - finding.status = FindingStatus::Triaged; - Outcome::Confirmed - } - _ => { - finding.status = FindingStatus::NeedsReview; - Outcome::LowConfidence - } + // "dismiss", "confirm" or unknown: triaged when the model is sure it + // is real, else left to a person. + _ => match ReviewReason::of_verdict(action, adjusted_confidence) { + Some(reason) => { + finding.status = FindingStatus::NeedsReview; + match reason { + ReviewReason::LikelyFalseAlarm => Outcome::Dismissed, + ReviewReason::Unsure => Outcome::LowConfidence, + } + } + None => { + finding.status = FindingStatus::Triaged; + Outcome::Confirmed + } + }, } } -/// Least confidence (after [`adjust_confidence`]) at which a confirmed -/// finding counts as triaged; below it the finding needs review. -const CONFIDENCE_THRESHOLD: f64 = 3.0; - /// Read ~50 lines of surrounding code from the file at the finding's location fn read_surrounding_context(finding: &Finding) -> Option { let file_path = finding.file_path.as_deref()?; @@ -1104,6 +1106,51 @@ mod tests { assert_eq!(unsure.status, FindingStatus::NeedsReview); } + /// A verdict of the model on finding 1. + fn model_verdict(action: &str, confidence: f64) -> TriageResult { + TriageResult { + id: Some("1".into()), + action: action.into(), + confidence, + rationale: "why".into(), + remediation: None, + } + } + + #[test] + fn a_finding_kept_for_review_says_why() { + let mut dismissed = finding(); + assert_eq!( + apply_verdict(&mut dismissed, &model_verdict("dismiss", 9.0)), + Outcome::Dismissed + ); + assert_eq!( + dismissed.review_reason(), + Some(ReviewReason::LikelyFalseAlarm) + ); + assert_eq!(dismissed.triage_rationale.as_deref(), Some("why")); + + let mut unsure = finding(); + assert_eq!( + apply_verdict(&mut unsure, &model_verdict("confirm", 2.0)), + Outcome::LowConfidence + ); + assert_eq!(unsure.review_reason(), Some(ReviewReason::Unsure)); + + // Test code halves the confidence: a 5 there is unsure. + let mut in_tests = finding(); + in_tests.file_path = Some("src/tests/buffer.c".into()); + assert_eq!( + apply_verdict(&mut in_tests, &model_verdict("confirm", 5.0)), + Outcome::LowConfidence + ); + assert_eq!(in_tests.review_reason(), Some(ReviewReason::Unsure)); + + let mut confirmed = finding(); + apply_verdict(&mut confirmed, &model_verdict("confirm", 7.0)); + assert_eq!(confirmed.review_reason(), None); + } + #[test] fn confirmed_and_rerated_findings_pass() { let mut f = finding(); diff --git a/compliance-core/src/models/finding.rs b/compliance-core/src/models/finding.rs index a8fe55c..c1f5214 100644 --- a/compliance-core/src/models/finding.rs +++ b/compliance-core/src/models/finding.rs @@ -305,10 +305,135 @@ impl Finding { } } +/// `triage_action` of a finding the LLM triage judged a false alarm. +pub const TRIAGE_DISMISS: &str = "dismiss"; +/// `triage_action` of a finding the LLM triage gave no verdict; the reason is +/// in `triage_rationale`, which starts with `skipped: `. +pub const TRIAGE_SKIPPED: &str = "skipped"; +/// `triage_action` of a finding the control check did not confirm against the +/// CRA requirements it maps to. +pub const TRIAGE_CONTROL_NOT_CONFIRMED: &str = "control_not_confirmed"; +/// Least confidence (0-10, as stored in `confidence`) at which the LLM triage +/// takes a confirmed finding as triaged; below it the finding needs review. +pub const TRIAGE_CONFIDENCE_THRESHOLD: f64 = 3.0; + +/// Why the LLM triage left a finding to a person ([`FindingStatus::NeedsReview`]). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ReviewReason { + /// The model dismissed the finding as a likely false alarm. + LikelyFalseAlarm, + /// The model confirmed the finding, but with too little confidence. + Unsure, +} + +impl ReviewReason { + /// Why a verdict of the model leaves a finding to a person; `None` when + /// the verdict decides it (confirmed, downgraded or upgraded). + /// + /// # Arguments + /// + /// * `action` - the model's action (`confirm`, `dismiss`, ...); an unknown + /// one counts as `confirm` + /// * `confidence` - the model's confidence, as stored in `confidence` + pub fn of_verdict(action: &str, confidence: f64) -> Option { + match action { + TRIAGE_DISMISS => Some(Self::LikelyFalseAlarm), + "downgrade" | "upgrade" => None, + _ if confidence < TRIAGE_CONFIDENCE_THRESHOLD => Some(Self::Unsure), + _ => None, + } + } +} + +impl Finding { + /// Why the LLM triage left this finding to a person, read from its stored + /// verdict (`triage_action`, `confidence`); `None` unless it needs review + /// because of that verdict (also when the triage skipped it or the control + /// check put it there). + pub fn review_reason(&self) -> Option { + if self.status != FindingStatus::NeedsReview { + return None; + } + match self.triage_action.as_deref()? { + TRIAGE_SKIPPED | TRIAGE_CONTROL_NOT_CONFIRMED => None, + action => ReviewReason::of_verdict(action, self.confidence?), + } + } +} + #[cfg(test)] mod tests { use super::*; + /// A finding as the LLM triage leaves it. + fn reviewed(status: FindingStatus, action: &str, confidence: Option) -> Finding { + let mut finding = Finding::new( + "repo".to_string(), + "fp".to_string(), + "semgrep".to_string(), + ScanType::Sast, + "title".to_string(), + "description".to_string(), + Severity::High, + ); + finding.status = status; + finding.triage_action = Some(action.to_string()); + finding.confidence = confidence; + finding + } + + #[test] + fn a_dismissed_finding_needs_review_as_a_likely_false_alarm() { + let finding = reviewed(FindingStatus::NeedsReview, TRIAGE_DISMISS, Some(9.0)); + assert_eq!( + finding.review_reason(), + Some(ReviewReason::LikelyFalseAlarm) + ); + } + + #[test] + fn a_confirmation_below_the_threshold_is_unsure() { + let unsure = reviewed(FindingStatus::NeedsReview, "confirm", Some(2.9)); + assert_eq!(unsure.review_reason(), Some(ReviewReason::Unsure)); + // An unknown action counts as a confirmation, as in the triage. + let unknown = reviewed(FindingStatus::NeedsReview, "keep", Some(1.0)); + assert_eq!(unknown.review_reason(), Some(ReviewReason::Unsure)); + let sure = reviewed(FindingStatus::NeedsReview, "confirm", Some(3.0)); + assert_eq!(sure.review_reason(), None); + } + + #[test] + fn only_a_finding_that_needs_review_has_a_reason() { + let decided = reviewed(FindingStatus::FalsePositive, TRIAGE_DISMISS, Some(9.0)); + assert_eq!(decided.review_reason(), None); + let triaged = reviewed(FindingStatus::Triaged, "confirm", Some(1.0)); + assert_eq!(triaged.review_reason(), None); + } + + #[test] + fn a_finding_the_model_did_not_put_up_for_review_has_no_reason() { + let mut untriaged = reviewed(FindingStatus::NeedsReview, "confirm", Some(1.0)); + untriaged.triage_action = None; + assert_eq!(untriaged.review_reason(), None); + // The control check keeps the model's confidence: it is not the reason. + let control = reviewed( + FindingStatus::NeedsReview, + TRIAGE_CONTROL_NOT_CONFIRMED, + Some(1.0), + ); + assert_eq!(control.review_reason(), None); + let skipped = reviewed(FindingStatus::NeedsReview, TRIAGE_SKIPPED, Some(0.5)); + assert_eq!(skipped.review_reason(), None); + let no_confidence = reviewed(FindingStatus::NeedsReview, "confirm", None); + assert_eq!(no_confidence.review_reason(), None); + } + + #[test] + fn a_change_of_severity_decides_the_finding() { + assert_eq!(ReviewReason::of_verdict("downgrade", 0.0), None); + assert_eq!(ReviewReason::of_verdict("upgrade", 0.0), None); + } + #[test] fn needs_review_reads_and_writes_as_needs_review() { let wire = serde_json::to_string(&FindingStatus::NeedsReview).unwrap_or_default(); diff --git a/compliance-core/src/models/mod.rs b/compliance-core/src/models/mod.rs index d80b6a4..6229fd4 100644 --- a/compliance-core/src/models/mod.rs +++ b/compliance-core/src/models/mod.rs @@ -45,7 +45,8 @@ pub use exclude_paths::{ normalise_exclude_paths, ExcludePathError, MAX_EXCLUDE_PATHS, MAX_EXCLUDE_PATH_LEN, }; pub use finding::{ - Finding, FindingStatus, Occurrence, ScanChangeKind, ScanStatusChange, Severity, StatusChange, + Finding, FindingStatus, Occurrence, ReviewReason, ScanChangeKind, ScanStatusChange, Severity, + StatusChange, }; pub use git_source::{GitSource, GitSourceView, SourceRepo, GIT_SOURCE_ID}; pub use graph::{ diff --git a/compliance-dashboard/src/i18n/texts.rs b/compliance-dashboard/src/i18n/texts.rs index 95cc78b..d6450d4 100644 --- a/compliance-dashboard/src/i18n/texts.rs +++ b/compliance-dashboard/src/i18n/texts.rs @@ -1879,7 +1879,7 @@ pub mod onboarding { /// The findings list and detail pages. pub mod findings { - use compliance_core::models::{FindingStatus, ScanType}; + use compliance_core::models::{Finding, FindingStatus, ReviewReason, ScanType}; use super::Text; @@ -2117,6 +2117,40 @@ pub mod findings { } } + /// Why the LLM triage left a finding to a person, in words. Exhaustive: a + /// new reason needs both. + pub fn review_reason(reason: ReviewReason) -> Text { + match reason { + ReviewReason::LikelyFalseAlarm => Text { + de: "KI: wohl Fehlalarm", + en: "AI: likely false alarm", + }, + ReviewReason::Unsure => Text { + de: "KI: unsicher", + en: "AI: unsure", + }, + } + } + + /// A finding's status in words: for one the LLM triage left to a person, + /// why it did. + pub fn finding_status(finding: &Finding) -> Text { + finding + .review_reason() + .map_or_else(|| status(&finding.status), review_reason) + } + + /// Mark a finding the AI took for a false alarm as one. + pub const CONFIRM_FALSE_ALARM: Text = Text { + de: "Fehlalarm bestätigen", + en: "Confirm false alarm", + }; + /// Keep a finding the AI took for a false alarm as a real one. + pub const REVIEW_ANYWAY: Text = Text { + de: "Doch prüfen", + en: "Not a false alarm", + }; + /// A scan type in words. Exhaustive: a new scan type needs both. pub fn scan_type(scan_type: &ScanType) -> Text { let (de, en) = match scan_type { @@ -4951,3 +4985,54 @@ pub mod graph { } } } + +#[cfg(test)] +mod finding_status_tests { + use super::super::Lang; + use super::findings::finding_status; + use compliance_core::models::{Finding, FindingStatus, ScanType, Severity}; + + /// A finding with the LLM triage's verdict `action` at `confidence`. + fn finding(status: FindingStatus, action: &str, confidence: f64) -> Finding { + let mut finding = Finding::new( + "repo".into(), + "fp".into(), + "semgrep".into(), + ScanType::Sast, + "title".into(), + "description".into(), + Severity::High, + ); + finding.status = status; + finding.triage_action = Some(action.into()); + finding.confidence = Some(confidence); + finding + } + + #[test] + fn a_finding_to_review_says_why() { + let dismissed = finding(FindingStatus::NeedsReview, "dismiss", 8.0); + assert_eq!( + finding_status(&dismissed).get(Lang::De), + "KI: wohl Fehlalarm" + ); + assert_eq!( + finding_status(&dismissed).get(Lang::En), + "AI: likely false alarm" + ); + let unsure = finding(FindingStatus::NeedsReview, "confirm", 1.0); + assert_eq!(finding_status(&unsure).get(Lang::De), "KI: unsicher"); + assert_eq!(finding_status(&unsure).get(Lang::En), "AI: unsure"); + } + + #[test] + fn without_a_reason_the_status_shows() { + let by_control = finding(FindingStatus::NeedsReview, "control_not_confirmed", 1.0); + assert_eq!(finding_status(&by_control).get(Lang::De), "Zu prüfen"); + let decided = finding(FindingStatus::FalsePositive, "dismiss", 8.0); + assert_eq!( + finding_status(&decided).get(Lang::En), + "False positive – rule" + ); + } +} diff --git a/compliance-dashboard/src/pages/finding_detail.rs b/compliance-dashboard/src/pages/finding_detail.rs index 0e1e7d8..91dbbd5 100644 --- a/compliance-dashboard/src/pages/finding_detail.rs +++ b/compliance-dashboard/src/pages/finding_detail.rs @@ -14,7 +14,7 @@ use crate::infrastructure::findings::{location, mask_secrets, wire_name}; use crate::infrastructure::graph::fetch_impact; use crate::pages::findings::status_icon; use crate::pages::graph_view::impact_has_content; -use compliance_core::models::{FindingStatus, ScanType}; +use compliance_core::models::{FindingStatus, ReviewReason, ScanType}; /// What happened to the last save on this page, shown next to what was saved. #[derive(Debug, Clone, PartialEq)] @@ -32,6 +32,7 @@ pub fn FindingDetailPage(id: String) -> Element { let finding_id = id.clone(); let mut status_save = use_signal(|| SaveState::Idle); let mut feedback_save = use_signal(|| SaveState::Idle); + let mut decision_save = use_signal(|| SaveState::Idle); // Accepting a risk asks for a reason before it is saved. // A status that needs a reason (accepted risk, wrong mapping) asks for it // before it is saved. @@ -84,6 +85,29 @@ pub fn FindingDetailPage(id: String) -> Element { let finding_id_for_status = id.clone(); let finding_id_for_risk = id.clone(); let finding_id_for_feedback = id.clone(); + let finding_id_for_decision = id.clone(); + let review_reason = f.review_reason(); + // A click that sets `status` (with `reason`) and shows the result + // beside the AI's rationale. + let decide = move |status: FindingStatus, reason: Option| { + let id = finding_id_for_decision.clone(); + move |_: MouseEvent| { + let id = id.clone(); + let wire = wire_name(&status); + let reason = reason.clone(); + spawn(async move { + match crate::infrastructure::findings::update_finding_status( + id, wire, reason, + ) + .await + { + Ok(()) => decision_save.set(SaveState::Saved), + Err(e) => decision_save.set(SaveState::Failed(e.to_string())), + } + finding.restart(); + }); + } + }; let existing_feedback = f.developer_feedback.clone().unwrap_or_default(); let where_found = location(f.file_path.as_deref(), f.line_number); // A committed secret is shown shortened; the evidence keeps it. @@ -129,7 +153,7 @@ pub fn FindingDetailPage(id: String) -> Element { "{} | {} | {}", tx::tool_name(&f.scanner, lang), tx::scan_type(&f.scan_type).get(lang), - tx::status(&f.status).get(lang) + tx::finding_status(&f).get(lang) ), } @@ -234,11 +258,36 @@ pub fn FindingDetailPage(id: String) -> Element { div { class: "card-header", {tx::TRIAGE.get(lang)} } div { style: "display: flex; align-items: center; gap: 8px; margin-bottom: 8px;", - if let Some(action) = &f.triage_action { + if let Some(reason) = review_reason { + span { class: "badge badge-info", + {status_icon(&f.status)} + " {tx::review_reason(reason).get(lang)}" + } + } else if let Some(action) = &f.triage_action { span { class: "badge badge-info", "{action}" } } } p { style: "color: var(--text-secondary); font-size: 14px;", "{rationale}" } + // Confirm or reject the AI's guess in one click. + if can_write && review_reason == Some(ReviewReason::LikelyFalseAlarm) { + div { class: "flex gap-2", style: "margin-top: 10px;", + button { + class: "btn btn-primary", + title: "{tx::MARK_AS.get(lang)} {tx::status(&FindingStatus::FalsePositive).get(lang)}", + onclick: decide(FindingStatus::FalsePositive, Some(rationale.clone())), + {status_icon(&FindingStatus::FalsePositive)} + " {tx::CONFIRM_FALSE_ALARM.get(lang)}" + } + button { + class: "btn btn-ghost", + title: "{tx::MARK_AS.get(lang)} {tx::status(&FindingStatus::Triaged).get(lang)}", + onclick: decide(FindingStatus::Triaged, None), + {status_icon(&FindingStatus::Triaged)} + " {tx::REVIEW_ANYWAY.get(lang)}" + } + } + } + SaveNote { state: decision_save() } } } diff --git a/compliance-dashboard/src/pages/findings.rs b/compliance-dashboard/src/pages/findings.rs index c8c397e..b0d0b19 100644 --- a/compliance-dashboard/src/pages/findings.rs +++ b/compliance-dashboard/src/pages/findings.rs @@ -394,9 +394,13 @@ pub fn FindingsPage(target: String) -> Element { } } td { - span { class: "badge badge-info", + // Why the AI left it to a person, its + // rationale on hover. + span { + class: "badge badge-info", + title: finding.review_reason().and(finding.triage_rationale.clone()), {status_icon(&finding.status)} - " {tx::status(&finding.status).get(lang)}" + " {tx::finding_status(finding).get(lang)}" } } }