feat: summarize confidence and severities

This commit is contained in:
Dandaedre 2026-09-29 15:49:54 +02:00
parent 0428cd8a9a
commit 04a3a0b0d7
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
4 changed files with 137 additions and 3 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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<ConfidenceBand>,
}
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<ConfidenceBand>,
/// 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());
}
}

View File

@ -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.