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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Cascade orchestrator combining the classification tiers.
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use rayon::prelude::*;
use rusqlite::Connection;
use crate::classify::errors::Result;
use crate::classify::rules::RuleSet;
use crate::classify::taxonomy::{SubcategoryDef, TaxonomyRegistry};
use crate::classify::tiers::exact::ExactMatcher;
use crate::classify::tiers::fuzzy::FuzzyClassifier;
use crate::classify::tiers::issue_type_tier::IssueTypeTier;
use crate::classify::tiers::jira_project_tier::JiraProjectTier;
use crate::classify::tiers::llm::LlmClassifier;
use crate::classify::tiers::override_tier::OverrideTier;
use crate::classify::tiers::regex_tier::RegexMatcher;
use crate::classify::tiers::ClassificationResult;
use crate::core::models::ClassificationMethod;
/// Runtime configuration for the [`ClassificationEngine`].
#[derive(Debug, Clone)]
pub struct ClassificationEngineConfig {
/// Whether to engage the LLM tier when tiers 1–3 fail.
pub use_llm: bool,
/// LLM model identifier (provider-specific).
pub llm_model: String,
/// LLM provider: `"openrouter"`, `"openai"`, or `"auto"`.
pub llm_provider: String,
/// Optional OpenRouter API key. If `None`, the env var
/// `OPENROUTER_API_KEY` is consulted at engine-build time.
pub openrouter_api_key: Option<String>,
/// Minimum confidence required to accept a verdict.
///
/// Verdicts below this threshold are returned as-is (so the caller
/// can still inspect them), but their `confidence` informs filtering
/// in downstream reports.
pub confidence_threshold: f64,
}
impl Default for ClassificationEngineConfig {
fn default() -> Self {
Self {
use_llm: false,
llm_model: "gpt-4o-mini".to_string(),
llm_provider: "auto".to_string(),
openrouter_api_key: None,
confidence_threshold: 0.7,
}
}
}
/// Combined classification cascade.
pub struct ClassificationEngine {
override_tier: Option<OverrideTier>,
exact: ExactMatcher,
issue_type: IssueTypeTier,
regex: RegexMatcher,
jira_project: JiraProjectTier,
fuzzy: FuzzyClassifier,
llm: Option<LlmClassifier>,
taxonomy: TaxonomyRegistry,
config: ClassificationEngineConfig,
}
impl ClassificationEngine {
/// Build a new engine from a [`RuleSet`] and configuration.
///
/// The LLM tier is constructed (but only invoked) if `config.use_llm`
/// is true. The API key is read from the `OPENAI_API_KEY` environment
/// variable; if unset, the LLM tier silently returns `None`.
///
/// Uses the built-in taxonomy registry only. To extend it with
/// user-defined subcategories, use [`Self::with_taxonomy`].
///
/// # Errors
///
/// Returns an error if the rules fail to compile (e.g. invalid regex).
pub fn new(ruleset: RuleSet, config: ClassificationEngineConfig) -> Result<Self> {
Self::with_taxonomy(ruleset, config, Vec::new())
}
/// Build an engine with user-defined subcategory definitions merged into
/// the built-in taxonomy registry.
///
/// # Errors
///
/// Returns an error if the rules fail to compile.
pub fn with_taxonomy(
ruleset: RuleSet,
config: ClassificationEngineConfig,
custom_taxonomy: Vec<SubcategoryDef>,
) -> Result<Self> {
Self::with_taxonomy_and_mappings(ruleset, config, custom_taxonomy, HashMap::new(), None)
}
/// Full builder allowing JIRA project-key mappings and an optional DB
/// connection for the manual-override tier.
///
/// # Errors
///
/// Returns an error if the rules fail to compile.
pub fn with_taxonomy_and_mappings(
ruleset: RuleSet,
config: ClassificationEngineConfig,
custom_taxonomy: Vec<SubcategoryDef>,
jira_project_mappings: HashMap<String, String>,
override_conn: Option<Arc<Mutex<Connection>>>,
) -> Result<Self> {
let exact = ExactMatcher::new(&ruleset.rules)?;
let regex = RegexMatcher::new(&ruleset.rules)?;
let fuzzy = FuzzyClassifier;
let llm = if config.use_llm {
match LlmClassifier::from_provider(
&config.llm_provider,
&config.llm_model,
config.openrouter_api_key.clone(),
) {
Ok(c) => Some(c),
Err(e) => {
return Err(crate::classify::errors::ClassifyError::Config(format!(
"LLM provider init failed: {e}"
)))
}
}
} else {
None
};
let taxonomy = TaxonomyRegistry::new(custom_taxonomy);
let issue_type = IssueTypeTier::with_taxonomy(taxonomy.clone());
let jira_project = JiraProjectTier::with_taxonomy(jira_project_mappings, taxonomy.clone());
let override_tier = override_conn.map(|c| OverrideTier::with_taxonomy(c, taxonomy.clone()));
Ok(Self {
override_tier,
exact,
issue_type,
regex,
jira_project,
fuzzy,
llm,
taxonomy,
config,
})
}
/// Borrow the engine's taxonomy registry.
pub fn taxonomy(&self) -> &TaxonomyRegistry {
&self.taxonomy
}
/// Borrow the engine's effective configuration.
pub fn config(&self) -> &ClassificationEngineConfig {
&self.config
}
/// Run the synchronous tiers (0, 1, 1.5, 2, 3, 3.5) for a single message.
///
/// Returns `None` if no tier matched; callers may then invoke the
/// async [`ClassificationEngine::classify`] for the LLM fallback.
///
/// `commit_sha` and `repo_path` are optional; when supplied, the
/// manual-override tier (Tier 0) is consulted first. When `issue_type`
/// is supplied, the issue-type tier (Tier 1.5) is consulted between
/// the exact and regex tiers.
pub fn classify_sync(&self, message: &str, is_merge: bool) -> Option<ClassificationResult> {
self.classify_sync_with_context(message, is_merge, None, None, None)
}
/// Context-aware variant of [`Self::classify_sync`] that supplies
/// optional commit identity (for Tier 0) and PM-system issue type
/// (for Tier 1.5).
pub fn classify_sync_with_context(
&self,
message: &str,
is_merge: bool,
commit_sha: Option<&str>,
repo_path: Option<&str>,
issue_type: Option<&str>,
) -> Option<ClassificationResult> {
// Tier 0: manual override (DB lookup, short-circuits everything).
if let (Some(tier), Some(sha), Some(repo)) =
(self.override_tier.as_ref(), commit_sha, repo_path)
{
if let Some(r) = tier.lookup(sha, repo) {
return Some(r);
}
}
// Tier 1: exact keywords
if let Some(rule) = self.exact.classify(message) {
return Some(ClassificationResult {
top_level: self.taxonomy.resolve(&rule.category),
category: rule.category.clone(),
subcategory: rule.subcategory.clone(),
confidence: rule.confidence,
method: ClassificationMethod::ExactRule,
ticket_id: RegexMatcher::extract_ticket_id(message),
});
}
// Tier 1.5: PM issue-type mapping.
if let Some(it) = issue_type {
if let Some(mut r) = self.issue_type.classify(it) {
r.ticket_id = RegexMatcher::extract_ticket_id(message);
return Some(r);
}
}
// Tier 2: regex
if let Some(rule) = self.regex.classify(message) {
return Some(ClassificationResult {
top_level: self.taxonomy.resolve(&rule.category),
category: rule.category.clone(),
subcategory: rule.subcategory.clone(),
confidence: rule.confidence,
method: ClassificationMethod::RegexRule,
ticket_id: RegexMatcher::extract_ticket_id(message),
});
}
// Tier 3: JIRA project-key mapping (if configured).
if !self.jira_project.is_empty() {
if let Some(r) = self.jira_project.classify(message) {
return Some(r);
}
}
// Tier 3.5: fuzzy heuristics
if let Some(mut result) = self.fuzzy.classify(message, is_merge) {
if result.ticket_id.is_none() {
result.ticket_id = RegexMatcher::extract_ticket_id(message);
}
// Re-resolve top_level via the engine's registry in case user
// overrides changed the parent for the fuzzy verdict's category.
if let Some(top) = self.taxonomy.resolve(&result.category) {
result.top_level = Some(top);
}
return Some(result);
}
None
}
/// Run the full four-tier cascade including the optional LLM fallback.
pub async fn classify(&self, message: &str, is_merge: bool) -> ClassificationResult {
if let Some(r) = self.classify_sync(message, is_merge) {
return r;
}
if let Some(r) = self.llm_classify_only(message).await {
return r;
}
let mut fallback = ClassificationResult::unclassified();
fallback.ticket_id = RegexMatcher::extract_ticket_id(message);
fallback
}
/// Invoke the LLM tier directly, bypassing tiers 0–3.5.
///
/// Returns `None` when the LLM tier is not configured, no API key is
/// reachable, or the underlying request fails. The pipeline-level LLM
/// fallback uses this to route low-confidence catch-all verdicts to the
/// LLM without re-running `classify_sync` (which would short-circuit on
/// the same low-confidence verdict that triggered the fallback).
///
/// Backfills `ticket_id` from the message text — the LLM verdict
/// itself does not surface ticket IDs, and without this the pipeline's
/// overwrite-guard would otherwise drop a ticket reference carried by
/// the original tier-1-3 verdict when the LLM result wins.
pub async fn llm_classify_only(&self, message: &str) -> Option<ClassificationResult> {
let llm = self.llm.as_ref()?;
let mut r = llm.classify(message).await?;
r.top_level = self.taxonomy.resolve(&r.category);
if r.ticket_id.is_none() {
r.ticket_id = RegexMatcher::extract_ticket_id(message);
}
Some(r)
}
/// `Some(true)` when the LLM tier is enabled and has a reachable API
/// key, `Some(false)` when it is enabled but unconfigured, `None` when
/// the tier is disabled entirely. Callers can warn at startup when the
/// middle case occurs to avoid silent misconfiguration.
pub fn llm_has_api_key(&self) -> Option<bool> {
self.llm.as_ref().map(LlmClassifier::has_api_key)
}
/// Classify a batch of `(message, is_merge)` pairs in parallel using
/// Rayon (tiers 1–3 only). Entries where no tier matched are returned
/// as [`ClassificationResult::unclassified`].
pub fn classify_batch(&self, messages: &[(&str, bool)]) -> Vec<ClassificationResult> {
messages
.par_iter()
.map(|(msg, is_merge)| {
self.classify_sync(msg, *is_merge)
.unwrap_or_else(ClassificationResult::unclassified)
})
.collect()
}
}