diff --git a/docs/heuristics.md b/docs/heuristics.md index 83a25c6..aa8c1dc 100644 --- a/docs/heuristics.md +++ b/docs/heuristics.md @@ -31,5 +31,10 @@ findings produce score zero and no confidence verdict. Binary, invalid UTF-8, oversized, and unsupported inputs remain typed input errors; they are never reported as clean analyses. +Every `Analysis` retains its complete finding list and can produce a compact +summary containing the numeric score, optional named band, and separate advice, +warning, and error counts. Summary generation never filters the evidence used +to explain the score. + Weights and thresholds are review policy, not calibrated probabilities. A score of 75 does not mean a 75% chance that AI wrote the input. diff --git a/src/lib.rs b/src/lib.rs index 15e07ee..a3c7578 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,8 +24,8 @@ pub use diagnostic::FindingDiagnostic; pub use error::{InputError, InputResult}; pub use input::{Document, Documents}; pub use model::{ - Analysis, Category, Confidence, ConfidenceBand, Finding, FindingSeverity, RuleId, SourceId, - TextEdit, + Analysis, AnalysisSummary, Category, Confidence, ConfidenceBand, Finding, FindingSeverity, + RuleId, SeverityCounts, SourceId, TextEdit, }; /// Load embedded defaults plus optional platform user configuration. diff --git a/src/model.rs b/src/model.rs index 78bd172..b0ec056 100644 --- a/src/model.rs +++ b/src/model.rs @@ -57,6 +57,23 @@ pub enum FindingSeverity { Error, } +impl FindingSeverity { + /// Return the stable lowercase presentation name. + pub const fn as_str(self) -> &'static str { + match self { + Self::Advice => "advice", + Self::Warning => "warning", + Self::Error => "error", + } + } +} + +impl fmt::Display for FindingSeverity { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(self.as_str()) + } +} + /// Confidence that configured low-care signals are present. /// /// This is deliberately not an AI-authorship probability. @@ -88,6 +105,24 @@ pub enum ConfidenceBand { High, } +impl ConfidenceBand { + /// Return the stable kebab-case presentation name. + pub const fn as_str(self) -> &'static str { + match self { + Self::BelowThreshold => "below-threshold", + Self::Low => "low", + Self::Medium => "medium", + Self::High => "high", + } + } +} + +impl fmt::Display for ConfidenceBand { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str(self.as_str()) + } +} + /// Identity of the exact source snapshot being analyzed. #[derive(Debug, Clone, PartialEq, Eq)] pub enum SourceId { @@ -159,6 +194,58 @@ pub struct Analysis { pub confidence_band: Option, } +impl Analysis { + /// Summarize score, named band, and severity counts without consuming findings. + pub fn summary(&self) -> AnalysisSummary { + AnalysisSummary { + confidence_score: self.confidence.score(), + confidence_band: self.confidence_band, + findings: SeverityCounts::from_findings(&self.findings), + } + } +} + +/// Compact deterministic summary of one completed analysis. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct AnalysisSummary { + /// Normalized evidence score in `0..=100`. + pub confidence_score: u8, + /// Named band, or no verdict when the source has no evidence. + pub confidence_band: Option, + /// Finding totals grouped by configured severity. + pub findings: SeverityCounts, +} + +/// Finding counts grouped by configured presentation severity. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct SeverityCounts { + /// Informational findings. + pub advice: usize, + /// Warning findings. + pub warnings: usize, + /// Error findings. + pub errors: usize, +} + +impl SeverityCounts { + /// Return the total number of findings across severities. + pub const fn total(self) -> usize { + self.advice + self.warnings + self.errors + } + + fn from_findings(findings: &[Finding]) -> Self { + let mut counts = Self::default(); + for finding in findings { + match finding.severity { + FindingSeverity::Advice => counts.advice += 1, + FindingSeverity::Warning => counts.warnings += 1, + FindingSeverity::Error => counts.errors += 1, + } + } + counts + } +} + #[cfg(test)] mod tests { use super::*; @@ -196,4 +283,46 @@ mod tests { assert_eq!(first.to_string(), "alpha-rule"); assert!(first < second); } + + #[test] + fn analysis_summary_counts_severity_without_removing_findings() { + let analysis = Analysis { + findings: vec![ + Finding { + rule_id: RuleId::from_static("advice-rule"), + category: Category::Style, + severity: FindingSeverity::Advice, + message: "advice".to_owned(), + help: None, + byte_span: 0..1, + matched_text: "a".to_owned(), + weight: 1.0, + fix: None, + }, + Finding { + rule_id: RuleId::from_static("warning-rule"), + category: Category::PromptLeakage, + severity: FindingSeverity::Warning, + message: "warning".to_owned(), + help: None, + byte_span: 2..3, + matched_text: "b".to_owned(), + weight: 10.0, + fix: None, + }, + ], + confidence: Confidence::from_score(32), + confidence_band: Some(ConfidenceBand::Low), + }; + + let summary = analysis.summary(); + + assert_eq!(analysis.findings.len(), 2); + assert_eq!(summary.confidence_score, 32); + assert_eq!(summary.confidence_band, Some(ConfidenceBand::Low)); + assert_eq!(summary.findings.advice, 1); + assert_eq!(summary.findings.warnings, 1); + assert_eq!(summary.findings.errors, 0); + assert_eq!(summary.findings.total(), analysis.findings.len()); + } } diff --git a/tasks.md b/tasks.md index a6dfe26..9d9e2f3 100644 --- a/tasks.md +++ b/tasks.md @@ -140,7 +140,7 @@ formula are pinned by tests. - [x] Convert each `Finding` into a typed `miette::Diagnostic` with named source, exact `SourceSpan`, severity, rule ID as diagnostic code, message, and help. -- [ ] Add a summary that reports confidence band/score and counts by severity, +- [x] Add a summary that reports confidence band/score and counts by severity, while retaining every contributing diagnostic. - [ ] Build detector CLI inputs for file, directory, and stdin (`--stdin-filename` for language/path scoping); add ignore and maximum-size controls.