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_graph_diagram" => {
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_wrapped" | "ctx_heatmap" => {
58                TaskCategory::Review
59            }
60            "ctx_shell" | "ctx_execute" => TaskCategory::Debugging,
61            _ => TaskCategory::General,
62        }
63    }
64
65    pub fn classify_command_key(cmd_key: &str) -> TaskCategory {
66        let k = normalize(cmd_key);
67        if k.is_empty() {
68            return TaskCategory::General;
69        }
70        if k.starts_with("git ") || k == "git" {
71            return TaskCategory::Git;
72        }
73
74        if k.starts_with("cargo ") {
75            let sub = k.trim_start_matches("cargo ").trim();
76            if matches!(sub, "test" | "nextest" | "llvm-cov" | "tarpaulin") {
77                return TaskCategory::Testing;
78            }
79            if matches!(sub, "build" | "check" | "clippy" | "fmt" | "run" | "doc") {
80                return TaskCategory::BuildDeploy;
81            }
82            return TaskCategory::BuildDeploy;
83        }
84
85        if k.contains("test") || k.contains("pytest") || k.contains("jest") || k.contains("vitest")
86        {
87            return TaskCategory::Testing;
88        }
89        if k.contains("build")
90            || k.contains("deploy")
91            || k.contains("docker")
92            || k.contains("compose")
93            || k.contains("kubectl")
94            || k.contains("helm")
95            || k.contains("terraform")
96        {
97            return TaskCategory::BuildDeploy;
98        }
99        if k.contains("lint") || k.contains("clippy") || k.contains("fmt") || k.contains("format") {
100            return TaskCategory::BuildDeploy;
101        }
102        if k.contains("grep") || k.contains("rg") || k.contains("ripgrep") {
103            return TaskCategory::Exploration;
104        }
105
106        TaskCategory::General
107    }
108}
109
110fn normalize(s: &str) -> String {
111    s.trim().to_lowercase()
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn classify_git() {
120        assert_eq!(
121            TaskClassifier::classify_command_key("git status"),
122            TaskCategory::Git
123        );
124    }
125
126    #[test]
127    fn classify_tools() {
128        assert_eq!(
129            TaskClassifier::classify_tool("ctx_semantic_search"),
130            TaskCategory::Architecture
131        );
132    }
133}