Skip to main content

lean_ctx/core/gain/
task_classifier.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
4pub enum TaskCategory {
5    Coding,
6    Debugging,
7    Refactoring,
8    Testing,
9    Exploration,
10    Planning,
11    Delegation,
12    Git,
13    BuildDeploy,
14    Knowledge,
15    Architecture,
16    Review,
17    General,
18}
19
20impl TaskCategory {
21    pub fn label(&self) -> &'static str {
22        match self {
23            TaskCategory::Coding => "Coding",
24            TaskCategory::Debugging => "Debugging",
25            TaskCategory::Refactoring => "Refactoring",
26            TaskCategory::Testing => "Testing",
27            TaskCategory::Exploration => "Exploration",
28            TaskCategory::Planning => "Planning",
29            TaskCategory::Delegation => "Delegation",
30            TaskCategory::Git => "Git",
31            TaskCategory::BuildDeploy => "Build/Deploy",
32            TaskCategory::Knowledge => "Knowledge",
33            TaskCategory::Architecture => "Architecture",
34            TaskCategory::Review => "Review",
35            TaskCategory::General => "General",
36        }
37    }
38}
39
40pub struct TaskClassifier;
41
42impl TaskClassifier {
43    pub fn classify_tool(tool_name: &str) -> TaskCategory {
44        let t = normalize(tool_name);
45        match t.as_str() {
46            "ctx_edit" | "ctx_fill" => TaskCategory::Refactoring,
47            "ctx_read" | "ctx_multi_read" | "ctx_smart_read" | "ctx_delta" | "ctx_tree"
48            | "ctx_search" | "ctx_outline" | "ctx_graph" | "ctx_callgraph" => {
49                TaskCategory::Exploration
50            }
51            "ctx_semantic_search" | "ctx_architecture" | "ctx_impact" => TaskCategory::Architecture,
52            "ctx_overview" | "ctx_preload" | "ctx_task" | "ctx_intent" | "ctx_workflow" => {
53                TaskCategory::Planning
54            }
55            "ctx_handoff" | "ctx_agent" | "ctx_share" => TaskCategory::Delegation,
56            "ctx_session" | "ctx_knowledge" | "ctx_compress_memory" => TaskCategory::Knowledge,
57            "ctx_cost" | "ctx_gain" | "ctx_metrics" | "ctx_heatmap" => TaskCategory::Review,
58            "ctx_shell" | "ctx_execute" => TaskCategory::Debugging,
59            _ => TaskCategory::General,
60        }
61    }
62
63    pub fn classify_command_key(cmd_key: &str) -> TaskCategory {
64        let k = normalize(cmd_key);
65        if k.is_empty() {
66            return TaskCategory::General;
67        }
68        if k.starts_with("git ") || k == "git" {
69            return TaskCategory::Git;
70        }
71
72        if k.starts_with("cargo ") {
73            let sub = k.trim_start_matches("cargo ").trim();
74            if matches!(sub, "test" | "nextest" | "llvm-cov" | "tarpaulin") {
75                return TaskCategory::Testing;
76            }
77            if matches!(sub, "build" | "check" | "clippy" | "fmt" | "run" | "doc") {
78                return TaskCategory::BuildDeploy;
79            }
80            return TaskCategory::BuildDeploy;
81        }
82
83        if k.contains("test") || k.contains("pytest") || k.contains("jest") || k.contains("vitest")
84        {
85            return TaskCategory::Testing;
86        }
87        if k.contains("build")
88            || k.contains("deploy")
89            || k.contains("docker")
90            || k.contains("compose")
91            || k.contains("kubectl")
92            || k.contains("helm")
93            || k.contains("terraform")
94        {
95            return TaskCategory::BuildDeploy;
96        }
97        if k.contains("lint") || k.contains("clippy") || k.contains("fmt") || k.contains("format") {
98            return TaskCategory::BuildDeploy;
99        }
100        if k.contains("grep") || k.contains("rg") || k.contains("ripgrep") {
101            return TaskCategory::Exploration;
102        }
103
104        TaskCategory::General
105    }
106}
107
108fn normalize(s: &str) -> String {
109    s.trim().to_lowercase()
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn classify_git() {
118        assert_eq!(
119            TaskClassifier::classify_command_key("git status"),
120            TaskCategory::Git
121        );
122    }
123
124    #[test]
125    fn classify_tools() {
126        assert_eq!(
127            TaskClassifier::classify_tool("ctx_semantic_search"),
128            TaskCategory::Architecture
129        );
130    }
131}