Skip to main content

cortex_router/
classifier.rs

1use cortex_domain::RiskLevel;
2
3use crate::fold::fold_words;
4use crate::lexicon::{
5    ADVISORY, AUTH, COMPRESSION, CONCURRENCY, DEPLOYMENT, DETERMINISTIC, EXTRACTION, INJECTION,
6    MIGRATION, MUTATION, PUBLICATION, RELEASE, REPOSITORY, SECURITY, negated,
7};
8use crate::{Classification, TaskClass};
9
10/// Classify work using stable lexical rules, without calling a model.
11#[must_use]
12pub fn classify(task: &str) -> Classification {
13    let normalized = fold_words(task);
14    let mutation_likely = mutation_likely(&normalized);
15    let class = high_risk_class(&normalized)
16        .unwrap_or_else(|| ordinary_class(&normalized, mutation_likely));
17    let risk = if class.is_high_risk() {
18        RiskLevel::High
19    } else if matches!(
20        class,
21        TaskClass::Implementation | TaskClass::RepositoryAnalysis
22    ) {
23        RiskLevel::Medium
24    } else {
25        RiskLevel::Low
26    };
27    Classification {
28        class,
29        risk,
30        mutation_likely,
31    }
32}
33
34fn mutation_likely(normalized: &str) -> bool {
35    let tokens: Vec<&str> = normalized.split_whitespace().collect();
36    tokens.iter().enumerate().any(|(index, token)| {
37        MUTATION
38            .iter()
39            .any(|cue| *token == *cue || token.starts_with(cue) && cue.chars().count() >= 4)
40            && !negated(&tokens, index)
41    })
42}
43
44fn high_risk_class(normalized: &str) -> Option<TaskClass> {
45    if has_phrase(normalized, INJECTION) {
46        Some(TaskClass::Ambiguous)
47    } else if has_phrase(normalized, AUTH) || contains_word_from(normalized, &["auth"]) {
48        Some(TaskClass::Authentication)
49    } else if has_phrase(normalized, SECURITY) {
50        Some(TaskClass::Security)
51    } else if has_phrase(normalized, CONCURRENCY) {
52        Some(TaskClass::Concurrency)
53    } else if has_phrase(normalized, MIGRATION)
54        || contains_word_from(normalized, &["migration", "migrations"])
55    {
56        Some(TaskClass::Migration)
57    } else if has_phrase(normalized, RELEASE) {
58        Some(TaskClass::Release)
59    } else if has_phrase(normalized, DEPLOYMENT) {
60        Some(TaskClass::Deployment)
61    } else if has_phrase(normalized, PUBLICATION) {
62        Some(TaskClass::Publication)
63    } else {
64        None
65    }
66}
67
68fn ordinary_class(normalized: &str, mutation_likely: bool) -> TaskClass {
69    if is_ambiguous(normalized) {
70        TaskClass::Ambiguous
71    } else if has_phrase(normalized, REPOSITORY) {
72        TaskClass::RepositoryAnalysis
73    } else if has_phrase(normalized, DETERMINISTIC) && !mutation_likely {
74        TaskClass::Deterministic
75    } else if has_phrase(normalized, EXTRACTION) && !mutation_likely {
76        TaskClass::StructuredExtraction
77    } else if has_phrase(normalized, COMPRESSION) && !mutation_likely {
78        TaskClass::ContextCompression
79    } else if has_phrase(normalized, ADVISORY) && !mutation_likely {
80        TaskClass::AdvisoryDraft
81    } else if mutation_likely {
82        TaskClass::Implementation
83    } else {
84        TaskClass::Ambiguous
85    }
86}
87
88fn has_phrase(normalized: &str, phrases: &[&str]) -> bool {
89    let padded = format!(" {normalized} ");
90    phrases
91        .iter()
92        .any(|phrase| padded.contains(&format!(" {phrase} ")))
93}
94
95fn contains_word_from(normalized: &str, words: &[&str]) -> bool {
96    normalized
97        .split_whitespace()
98        .any(|word| words.contains(&word))
99}
100
101fn is_ambiguous(normalized: &str) -> bool {
102    normalized.is_empty()
103        || matches!(
104            normalized,
105            "fix it" | "do it" | "handle this" | "make it better" | "update this" | "change this"
106        )
107}
108
109/// True when two or more deterministic lexicon families fire on the same task.
110#[must_use]
111pub fn detector_disagreement(task: &str) -> bool {
112    let normalized = fold_words(task);
113    let families: &[&[&str]] = &[
114        REPOSITORY,
115        DETERMINISTIC,
116        EXTRACTION,
117        COMPRESSION,
118        ADVISORY,
119        AUTH,
120        SECURITY,
121        CONCURRENCY,
122        MIGRATION,
123        RELEASE,
124        DEPLOYMENT,
125        PUBLICATION,
126    ];
127    families
128        .iter()
129        .filter(|phrases| has_phrase(&normalized, phrases))
130        .count()
131        >= 2
132}