Skip to main content

tj_core/classifier/
mod.rs

1//! Event classifier: takes a chat chunk + recent task context,
2//! returns suggested event_type + task_id + confidence.
3
4use crate::event::{EventType, EvidenceStrength};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize)]
8pub struct ClassifyInput {
9    pub text: String,
10    pub author_hint: String,
11    pub recent_tasks: Vec<TaskContext>,
12}
13
14#[derive(Debug, Clone, Serialize)]
15pub struct TaskContext {
16    pub task_id: String,
17    pub title: String,
18    pub last_events: Vec<String>,
19    /// The task's most-recent `constraint` events (≤ N). Empty when the
20    /// task has no constraints — the prompt is then unchanged.
21    pub constraints: Vec<String>,
22}
23
24#[derive(Debug, Clone, Deserialize, Serialize)]
25pub struct ClassifyOutput {
26    pub event_type: EventType,
27    pub task_id_guess: Option<String>,
28    pub confidence: f64,
29    pub evidence_strength: Option<EvidenceStrength>,
30    pub suggested_text: String,
31    /// v0.6.0: optional structured artifacts the classifier extracted
32    /// directly. When absent (old protocol or model didn't bother),
33    /// the journal falls back to regex extraction in
34    /// `db::ingest_new_events`. When present, the two sets are merged
35    /// at ingest time so the model can surface artifacts the regex
36    /// would miss (e.g. ticket ids in non-ASCII brackets).
37    #[serde(default)]
38    pub artifacts: Option<crate::artifacts::Artifacts>,
39}
40
41pub trait Classifier: Send + Sync {
42    fn classify(&self, input: &ClassifyInput) -> anyhow::Result<ClassifyOutput>;
43}
44
45use crate::event::EventStatus;
46
47pub const CONFIDENCE_THRESHOLD: f64 = 0.85;
48
49pub fn decide_status(confidence: f64) -> EventStatus {
50    if confidence >= CONFIDENCE_THRESHOLD {
51        EventStatus::Confirmed
52    } else {
53        EventStatus::Suggested
54    }
55}
56
57pub mod heuristic;
58pub mod http;
59pub mod hybrid;
60pub mod mock;
61pub mod prompt;
62pub mod telemetry;
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    /// The HTTP backend must honour `TJ_CLASSIFIER_MODEL`. Wraps the
69    /// read-set-restore steps in one test to avoid env-var races with
70    /// other tests in this crate.
71    #[test]
72    fn tj_classifier_model_env_var_overrides_http_default() {
73        let prev_model = std::env::var("TJ_CLASSIFIER_MODEL").ok();
74        let prev_key = std::env::var("ANTHROPIC_API_KEY").ok();
75
76        // SAFETY: tests in this crate do not concurrently read these env vars.
77        unsafe {
78            std::env::remove_var("TJ_CLASSIFIER_MODEL");
79            std::env::set_var("ANTHROPIC_API_KEY", "test-key-do-not-use");
80        }
81        let http_default = http::AnthropicClassifier::from_env().unwrap();
82        assert_eq!(http_default.model, http::DEFAULT_MODEL);
83
84        unsafe {
85            std::env::set_var("TJ_CLASSIFIER_MODEL", "sonnet-override");
86        }
87        let http_override = http::AnthropicClassifier::from_env().unwrap();
88        assert_eq!(http_override.model, "sonnet-override");
89
90        // Restore.
91        unsafe {
92            match prev_model {
93                Some(v) => std::env::set_var("TJ_CLASSIFIER_MODEL", v),
94                None => std::env::remove_var("TJ_CLASSIFIER_MODEL"),
95            }
96            match prev_key {
97                Some(v) => std::env::set_var("ANTHROPIC_API_KEY", v),
98                None => std::env::remove_var("ANTHROPIC_API_KEY"),
99            }
100        }
101    }
102
103    #[test]
104    fn task_context_has_constraints_field() {
105        let c = TaskContext {
106            task_id: "tj-1".into(),
107            title: "t".into(),
108            last_events: vec![],
109            constraints: vec!["must support PHP 7.4".into()],
110        };
111        assert_eq!(c.constraints, vec!["must support PHP 7.4".to_string()]);
112    }
113
114    #[test]
115    fn classify_input_serializes() {
116        let i = ClassifyInput {
117            text: "Adopted Rust for the journal".into(),
118            author_hint: "assistant".into(),
119            recent_tasks: vec![],
120        };
121        let s = serde_json::to_string(&i).unwrap();
122        assert!(s.contains("Adopted Rust"));
123    }
124
125    #[test]
126    fn decide_status_high_confidence_is_confirmed() {
127        assert_eq!(decide_status(0.95), EventStatus::Confirmed);
128        assert_eq!(decide_status(0.85), EventStatus::Confirmed);
129    }
130
131    #[test]
132    fn decide_status_low_confidence_is_suggested() {
133        assert_eq!(decide_status(0.84), EventStatus::Suggested);
134        assert_eq!(decide_status(0.0), EventStatus::Suggested);
135    }
136}