workmux 0.1.251

An opinionated workflow tool that orchestrates git worktrees and tmux
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Agent identity classification.
//!
//! Classifies an agent pane by combining tmux's `pane_current_command` with
//! the pane title. Some agents report a version string (Claude Code: "2.1.118"),
//! a truncated binary name (Codex: "codex-aarch64-a"), or run as a generic
//! interpreter (Gemini, Pi, Vibe). Stem-based profile resolution alone misses
//! these, so the result of `classify_agent_kind` is cached on `AgentState`
//! once it becomes non-None and reused by the sidebar render path.
//!
//! The canonical string form (e.g. "claude", "kiro-cli") matches the existing
//! `AgentProfile::name` so the sidebar can look up the corresponding profile.

use ratatui::style::Color;
use std::path::Path;

const GENERIC_INTERPRETERS: &[&str] = &["node", "python", "python3", "bun", "deno"];

/// Canonical set of agents the sidebar knows how to render.
///
/// Keeping per-variant metadata (icon, label) on this enum forces a compile
/// error in every consumer when a new variant is added, instead of silently
/// falling through to a generic default.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AgentKind {
    Claude,
    Codex,
    OpenCode,
    Gemini,
    Antigravity,
    Pi,
    Omp,
    KiroCli,
    Vibe,
    Copilot,
    Grok,
}

impl AgentKind {
    /// Canonical string form. Matches `AgentProfile::name` and is the value
    /// persisted in `AgentState::agent_kind`.
    pub fn as_str(self) -> &'static str {
        match self {
            AgentKind::Claude => "claude",
            AgentKind::Codex => "codex",
            AgentKind::OpenCode => "opencode",
            AgentKind::Gemini => "gemini",
            AgentKind::Antigravity => "agy",
            AgentKind::Pi => "pi",
            AgentKind::Omp => "omp",
            AgentKind::KiroCli => "kiro-cli",
            AgentKind::Vibe => "vibe",
            AgentKind::Copilot => "copilot",
            AgentKind::Grok => "grok",
        }
    }

    /// Parse the canonical string form. Round-trips with [`AgentKind::as_str`].
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "claude" => Some(AgentKind::Claude),
            "codex" => Some(AgentKind::Codex),
            "opencode" => Some(AgentKind::OpenCode),
            "gemini" => Some(AgentKind::Gemini),
            "agy" => Some(AgentKind::Antigravity),
            "pi" => Some(AgentKind::Pi),
            "omp" => Some(AgentKind::Omp),
            "kiro-cli" => Some(AgentKind::KiroCli),
            "vibe" => Some(AgentKind::Vibe),
            "copilot" => Some(AgentKind::Copilot),
            "grok" => Some(AgentKind::Grok),
            _ => None,
        }
    }

    /// Default sidebar icon. Exhaustive match: adding a variant forces an
    /// update here.
    pub fn default_icon(self) -> &'static str {
        match self {
            AgentKind::Claude => "CC",
            AgentKind::Codex => "CX",
            AgentKind::OpenCode => "OC",
            AgentKind::Gemini => "G",
            AgentKind::Antigravity => "⋂",
            AgentKind::Pi => "π",
            AgentKind::Omp => "⌥",
            AgentKind::KiroCli => "K",
            AgentKind::Vibe => "V",
            AgentKind::Copilot => "CP",
            AgentKind::Grok => "GK",
        }
    }

    /// Default sidebar label. Exhaustive match: adding a variant forces an
    /// update here.
    pub fn default_label(self) -> &'static str {
        match self {
            AgentKind::Claude => "Claude",
            AgentKind::Codex => "Codex",
            AgentKind::OpenCode => "OpenCode",
            AgentKind::Gemini => "Gemini",
            AgentKind::Antigravity => "Antigravity",
            AgentKind::Pi => "Pi",
            AgentKind::Omp => "Oh My Pi",
            AgentKind::KiroCli => "Kiro",
            AgentKind::Vibe => "Vibe",
            AgentKind::Copilot => "Copilot",
            AgentKind::Grok => "Grok",
        }
    }

    /// Default sidebar icon foreground color. Brand-true mid-luminance hex
    /// values that read on both shipped light and dark palettes.
    ///
    /// Sources: Anthropic, OpenAI, Google, GitHub, Mistral brand sheets;
    /// Pi sampled from product UI. `None` means "fall through to
    /// `palette.text`".
    pub fn default_color(self) -> Option<Color> {
        Some(match self {
            AgentKind::Claude => Color::Rgb(0xd9, 0x77, 0x57),
            AgentKind::Codex => Color::Rgb(0x10, 0xa3, 0x7f),
            AgentKind::Gemini => Color::Rgb(0x07, 0x8e, 0xfa),
            AgentKind::Antigravity => Color::Rgb(0x89, 0x57, 0xe5),
            AgentKind::Copilot => Color::Rgb(0x89, 0x57, 0xe5),
            AgentKind::Vibe => Color::Rgb(0xff, 0x82, 0x05),
            AgentKind::Pi => Color::Rgb(0x96, 0xbb, 0xb5),
            AgentKind::Omp => Color::Rgb(0xe0, 0x57, 0x35),
            AgentKind::OpenCode => Color::Blue,
            AgentKind::KiroCli => return None,
            AgentKind::Grok => Color::Rgb(0x1d, 0x9b, 0xf0),
        })
    }
}

/// Classify an agent pane using its foreground command and pane title.
///
/// Returns the canonical profile name (e.g. "claude") or `None` if no rule
/// matches. Callers cache the first non-None result to avoid re-classifying
/// on every tick.
pub fn classify_agent_kind(command: Option<&str>, pane_title: Option<&str>) -> Option<String> {
    classify_agent_kind_enum(command, pane_title).map(|k| k.as_str().to_string())
}

fn classify_agent_kind_enum(command: Option<&str>, pane_title: Option<&str>) -> Option<AgentKind> {
    let raw = command.unwrap_or("").trim();
    let stem = command_stem(raw);

    if let Some(kind) = classify_by_command(raw, &stem) {
        return Some(kind);
    }

    if is_generic_interpreter(&stem)
        && let Some(kind) = classify_by_title(pane_title.unwrap_or(""))
    {
        return Some(kind);
    }

    None
}

fn classify_by_command(raw: &str, stem: &str) -> Option<AgentKind> {
    if stem.is_empty() {
        return None;
    }

    if is_version_string(stem) || is_version_string(raw) {
        return Some(AgentKind::Claude);
    }

    if stem == "codex" || stem.starts_with("codex-") {
        return Some(AgentKind::Codex);
    }

    AgentKind::from_str(stem)
}

fn classify_by_title(title: &str) -> Option<AgentKind> {
    if title.contains("Claude Code") {
        return Some(AgentKind::Claude);
    }
    if title.contains("opencode") {
        return Some(AgentKind::OpenCode);
    }
    if title.contains("Gemini") || title.contains('\u{25C7}') {
        return Some(AgentKind::Gemini);
    }
    if title.contains('\u{03C0}') {
        return Some(AgentKind::Pi);
    }
    if title.contains("Oh My Pi") || contains_omp_ascii_case_insensitive(title) {
        return Some(AgentKind::Omp);
    }
    if title.contains("Vibe") {
        return Some(AgentKind::Vibe);
    }
    None
}

fn contains_omp_ascii_case_insensitive(title: &str) -> bool {
    title
        .split(|c: char| !c.is_ascii_alphanumeric())
        .any(|token| token.eq_ignore_ascii_case("omp"))
}

fn is_generic_interpreter(stem: &str) -> bool {
    let lower = stem.to_ascii_lowercase();
    GENERIC_INTERPRETERS.iter().any(|i| *i == lower)
}

fn is_version_string(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    let mut has_dot = false;
    let mut prev_dot = true;
    for c in s.chars() {
        if c == '.' {
            if prev_dot {
                return false;
            }
            has_dot = true;
            prev_dot = true;
        } else if c.is_ascii_digit() {
            prev_dot = false;
        } else {
            return false;
        }
    }
    has_dot && !prev_dot
}

fn command_stem(command: &str) -> String {
    let token = command.split_whitespace().next().unwrap_or("");
    Path::new(token)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(token)
        .to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn classify(cmd: &str, title: &str) -> Option<String> {
        classify_agent_kind(Some(cmd), Some(title))
    }

    #[test]
    fn version_string_matches_claude() {
        assert_eq!(classify("2.1.118", ""), Some("claude".into()));
        assert_eq!(classify("2.1.111", "✳ task"), Some("claude".into()));
        assert_eq!(classify("3.0.0.1", ""), Some("claude".into()));
    }

    #[test]
    fn codex_truncated_binary_matches() {
        assert_eq!(classify("codex-aarch64-a", ""), Some("codex".into()));
        assert_eq!(classify("codex", ""), Some("codex".into()));
    }

    #[test]
    fn opencode_exact_command() {
        assert_eq!(classify("opencode", "⠹ opencode"), Some("opencode".into()));
    }

    #[test]
    fn kiro_and_copilot_match() {
        assert_eq!(classify("kiro-cli", ""), Some("kiro-cli".into()));
        assert_eq!(classify("copilot", ""), Some("copilot".into()));
    }

    #[test]
    fn grok_command_matches() {
        assert_eq!(classify("grok", ""), Some("grok".into()));
        assert_eq!(classify("/usr/local/bin/grok", ""), Some("grok".into()));
        assert_eq!(classify("grok", "anything"), Some("grok".into()));
        assert_eq!(classify("grok-build", ""), None);
        assert_eq!(classify("grokster", ""), None);
    }

    #[test]
    fn direct_stem_matches_for_known_binaries() {
        assert_eq!(classify("claude", ""), Some("claude".into()));
        assert_eq!(classify("gemini", ""), Some("gemini".into()));
        assert_eq!(classify("agy", ""), Some("agy".into()));
        assert_eq!(classify("pi", ""), Some("pi".into()));
        assert_eq!(classify("omp", ""), Some("omp".into()));
        assert_eq!(classify("vibe", ""), Some("vibe".into()));
        assert_eq!(classify("grok", ""), Some("grok".into()));
    }

    #[test]
    fn absolute_path_is_normalized() {
        assert_eq!(classify("/usr/local/bin/claude", ""), Some("claude".into()));
        assert_eq!(classify("/usr/bin/agy", ""), Some("agy".into()));
        assert_eq!(classify("/opt/codex-aarch64-a", ""), Some("codex".into()));
    }

    #[test]
    fn node_with_claude_title() {
        assert_eq!(
            classify("node", "Claude Code 2.1.0 - foo"),
            Some("claude".into())
        );
    }

    #[test]
    fn node_with_gemini_title() {
        assert_eq!(
            classify("node", "\u{25C7}  Ready (sidebar-templates)"),
            Some("gemini".into())
        );
        assert_eq!(classify("node", "Gemini - working"), Some("gemini".into()));
    }

    #[test]
    fn node_with_pi_title() {
        assert_eq!(
            classify("node", "\u{03C0} - sidebar-templates"),
            Some("pi".into())
        );
    }

    #[test]
    fn generic_interpreter_with_omp_title() {
        assert_eq!(classify("bun", "omp"), Some("omp".into()));
        assert_eq!(classify("bun", "OMP"), Some("omp".into()));
        assert_eq!(classify("bun", "agent: omp"), Some("omp".into()));
        assert_eq!(classify("python3", "Oh My Pi"), Some("omp".into()));
    }

    #[test]
    fn generic_interpreter_with_omp_substring_title_does_not_match() {
        assert_eq!(classify("bun", "component-tests"), None);
        assert_eq!(classify("node", "compile server"), None);
        assert_eq!(classify("python3", "company dashboard"), None);
        assert_eq!(classify("python3", "completion worker"), None);
    }

    #[test]
    fn python_with_vibe_title() {
        assert_eq!(classify("Python", "Vibe"), Some("vibe".into()));
        assert_eq!(classify("python3", "Vibe agent"), Some("vibe".into()));
    }

    #[test]
    fn opencode_via_node_title() {
        assert_eq!(
            classify("node", "⠹ opencode session"),
            Some("opencode".into())
        );
    }

    #[test]
    fn empty_command_returns_none() {
        assert_eq!(classify_agent_kind(None, None), None);
        assert_eq!(classify("", ""), None);
        assert_eq!(classify("", "Vibe"), None);
    }

    #[test]
    fn unknown_command_returns_none() {
        assert_eq!(classify("zsh", ""), None);
        assert_eq!(classify("vim", "some title"), None);
        // Bare prefix collisions with "codex" must not match.
        assert_eq!(classify("codexploitation", ""), None);
        assert_eq!(classify("codex2", ""), None);
    }

    #[test]
    fn generic_interpreter_no_matching_title_returns_none() {
        assert_eq!(classify("node", "random title"), None);
        assert_eq!(classify("Python", "no match"), None);
    }

    #[test]
    fn version_string_negative_cases() {
        assert!(!is_version_string(""));
        assert!(!is_version_string("2"));
        assert!(!is_version_string("2."));
        assert!(!is_version_string(".2"));
        assert!(!is_version_string("2..1"));
        assert!(!is_version_string("2.1a"));
        assert!(is_version_string("2.1"));
        assert!(is_version_string("2.1.118"));
    }

    /// Every variant has a non-empty icon and label; the classifier produces
    /// a string that round-trips back to the same variant. Catches forgetting
    /// to fill in metadata or string form when adding a variant.
    #[test]
    fn every_variant_has_metadata_and_round_trips() {
        let all = [
            AgentKind::Claude,
            AgentKind::Codex,
            AgentKind::OpenCode,
            AgentKind::Gemini,
            AgentKind::Pi,
            AgentKind::Omp,
            AgentKind::KiroCli,
            AgentKind::Vibe,
            AgentKind::Copilot,
            AgentKind::Grok,
        ];
        for kind in all {
            assert!(!kind.default_icon().is_empty(), "{:?} icon empty", kind);
            assert!(!kind.default_label().is_empty(), "{:?} label empty", kind);
            assert_eq!(AgentKind::from_str(kind.as_str()), Some(kind));
        }
    }

    #[test]
    fn from_str_rejects_unknown() {
        assert_eq!(AgentKind::from_str(""), None);
        assert_eq!(AgentKind::from_str("not-a-profile"), None);
        assert_eq!(AgentKind::from_str("Claude"), None); // case-sensitive
    }
}