1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Tier 3: heuristic / fuzzy classification.
//!
//! Catches commits the rule tiers missed using cheap structural signals:
//! - explicit merge commits (`is_merge` flag from git)
//! - "Merge pull request" / "Merge branch" style messages
//! - "Revert" prefix
//! - bare ticket-prefixed messages (e.g. `PROJ-123: update auth`)
//! - very short / very long messages (low-confidence default)
use crate::classify::taxonomy::TopLevelCategory;
use crate::classify::tiers::ClassificationResult;
use crate::core::models::ClassificationMethod;
/// Tier-3 heuristic classifier. Stateless.
pub struct FuzzyClassifier;
impl FuzzyClassifier {
/// Classify `message` using heuristics.
///
/// `is_merge` is the upstream git-level merge flag (parents > 1) and
/// takes precedence over text-based detection.
///
/// Returns `None` when no heuristic matched — the caller will then
/// either invoke the LLM tier or fall back to "uncategorized".
pub fn classify(&self, message: &str, is_merge: bool) -> Option<ClassificationResult> {
self.classify_traced(message, is_merge).map(|(r, _)| r)
}
/// [`Self::classify`] plus the name of the heuristic that fired (#111).
///
/// Why: the eval harness measures precision per heuristic.
/// What: returns the same verdict as [`Self::classify`] paired with one of
/// `merge`, `revert`, `ticket_prefix`, `short_message`.
/// Test: `classify::trace_tests::traced_cascade_matches_untraced_verdicts`.
pub fn classify_traced(
&self,
message: &str,
is_merge: bool,
) -> Option<(ClassificationResult, &'static str)> {
let trimmed = message.trim();
let lower = trimmed.to_lowercase();
// 1. Merge commits — strongest structural signal.
if is_merge
|| lower.starts_with("merge pull request")
|| lower.starts_with("merge branch")
|| lower.starts_with("merge remote-tracking")
{
return Some((
ClassificationResult {
category: "merge".to_string(),
subcategory: None,
top_level: Some(TopLevelCategory::Maintenance),
confidence: 0.95,
method: ClassificationMethod::FuzzyMatch,
ticket_id: None,
complexity: None,
},
"merge",
));
}
// 2. Revert commits.
if lower.starts_with("revert ") || lower.starts_with("revert:") {
return Some((
ClassificationResult {
category: "revert".to_string(),
subcategory: None,
top_level: Some(TopLevelCategory::Maintenance),
confidence: 0.9,
method: ClassificationMethod::FuzzyMatch,
ticket_id: None,
complexity: None,
},
"revert",
));
}
// 3. Bare ticket-prefixed message — likely a feature/chore.
if let Some(ticket) = bare_ticket_prefix(trimmed) {
return Some((
ClassificationResult {
category: "feature".to_string(),
subcategory: Some("ticketed".to_string()),
top_level: Some(TopLevelCategory::Feature),
confidence: 0.6,
method: ClassificationMethod::FuzzyMatch,
ticket_id: Some(ticket),
complexity: None,
},
"ticket_prefix",
));
}
// 4. Very short messages — chore by convention.
if trimmed.len() < 12 && !trimmed.is_empty() {
return Some((
ClassificationResult {
category: "chore".to_string(),
subcategory: None,
top_level: Some(TopLevelCategory::Maintenance),
confidence: 0.4,
method: ClassificationMethod::FuzzyMatch,
ticket_id: None,
complexity: None,
},
"short_message",
));
}
None
}
}
/// If `message` starts with a JIRA-style ticket identifier, return it.
fn bare_ticket_prefix(message: &str) -> Option<String> {
let first = message.split_whitespace().next()?;
// Strip a trailing colon or hyphen separator.
let candidate = first.trim_end_matches([':', '-', ',']);
let mut parts = candidate.split('-');
let project = parts.next()?;
let number = parts.next()?;
if parts.next().is_some() {
return None;
}
if project.is_empty() || number.is_empty() {
return None;
}
if !project
.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
{
return None;
}
if !project.chars().next()?.is_ascii_uppercase() {
return None;
}
if !number.chars().all(|c| c.is_ascii_digit()) {
return None;
}
Some(format!("{project}-{number}"))
}