macot 0.1.11

Multi Agent Control Tower - CLI for orchestrating Claude CLI instances
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use chrono::{DateTime, Utc};
use ratatui::style::Color;
use serde::{Deserialize, Serialize};

use super::message::ExpertId;

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExpertState {
    #[default]
    Idle,
    Busy,
}

impl ExpertState {
    pub fn symbol(&self) -> &'static str {
        match self {
            ExpertState::Idle => "",
            ExpertState::Busy => "",
        }
    }

    pub fn color(&self) -> Color {
        match self {
            ExpertState::Idle => Color::Gray,
            ExpertState::Busy => Color::Green,
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            ExpertState::Idle => "Waiting for input",
            ExpertState::Busy => "Working",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Role {
    Analyst,
    Developer,
    Reviewer,
    Coordinator,
    Specialist(String),
}

impl Role {
    pub fn specialist(name: impl Into<String>) -> Self {
        Self::Specialist(name.into())
    }

    pub fn matches(&self, other: &str) -> bool {
        match self {
            Role::Analyst => other.eq_ignore_ascii_case("analyst"),
            Role::Developer => other.eq_ignore_ascii_case("developer"),
            Role::Reviewer => other.eq_ignore_ascii_case("reviewer"),
            Role::Coordinator => other.eq_ignore_ascii_case("coordinator"),
            Role::Specialist(name) => name.eq_ignore_ascii_case(other),
        }
    }

    pub fn as_str(&self) -> &str {
        match self {
            Role::Analyst => "analyst",
            Role::Developer => "developer",
            Role::Reviewer => "reviewer",
            Role::Coordinator => "coordinator",
            Role::Specialist(name) => name,
        }
    }
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpertInfo {
    pub id: ExpertId,
    pub name: String,
    pub role: Role,
    pub tmux_session: String,
    #[serde(alias = "tmux_pane")]
    pub tmux_window: String,
    pub state: ExpertState,
    pub last_activity: DateTime<Utc>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub worktree_path: Option<String>,
}

impl ExpertInfo {
    pub fn new(
        id: ExpertId,
        name: String,
        role: Role,
        tmux_session: String,
        tmux_window: String,
    ) -> Self {
        Self {
            id,
            name,
            role,
            tmux_session,
            tmux_window,
            state: ExpertState::default(),
            last_activity: Utc::now(),
            worktree_path: None,
        }
    }

    pub fn set_state(&mut self, state: ExpertState) {
        self.state = state;
        self.last_activity = Utc::now();
    }

    pub fn is_idle(&self) -> bool {
        matches!(self.state, ExpertState::Idle)
    }

    #[allow(dead_code)]
    pub fn is_busy(&self) -> bool {
        matches!(self.state, ExpertState::Busy)
    }

    #[allow(dead_code)]
    pub fn matches_name(&self, name: &str) -> bool {
        self.name.eq_ignore_ascii_case(name)
    }

    #[allow(dead_code)]
    pub fn matches_role(&self, role: &str) -> bool {
        self.role.matches(role)
    }

    pub fn set_worktree_path(&mut self, path: Option<String>) {
        self.worktree_path = path;
    }

    pub fn same_worktree(&self, other: &ExpertInfo) -> bool {
        self.worktree_path == other.worktree_path
    }

    #[allow(dead_code)]
    pub fn update_activity(&mut self) {
        self.last_activity = Utc::now();
    }
}

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

    #[test]
    fn expert_info_new_creates_with_defaults() {
        let expert = ExpertInfo::new(
            0,
            "architect".to_string(),
            Role::Analyst,
            "session-1".to_string(),
            "0".to_string(),
        );

        assert_eq!(expert.id, 0);
        assert_eq!(expert.name, "architect");
        assert_eq!(expert.role, Role::Analyst);
        assert_eq!(expert.tmux_session, "session-1");
        assert_eq!(expert.tmux_window, "0");
        assert_eq!(expert.state, ExpertState::Idle);
    }

    #[test]
    fn expert_state_transitions() {
        let mut expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );

        // Start idle
        assert!(expert.is_idle());
        assert!(!expert.is_busy());

        // Set to busy
        expert.set_state(ExpertState::Busy);
        assert!(expert.is_busy());
        assert!(!expert.is_idle());

        // Set back to idle
        expert.set_state(ExpertState::Idle);
        assert!(expert.is_idle());
        assert!(!expert.is_busy());
    }

    #[test]
    fn expert_name_matching() {
        let expert = ExpertInfo::new(
            0,
            "Backend-Expert".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );

        assert!(expert.matches_name("Backend-Expert"));
        assert!(expert.matches_name("backend-expert"));
        assert!(expert.matches_name("BACKEND-EXPERT"));
        assert!(!expert.matches_name("Frontend"));
    }

    #[test]
    fn role_matching() {
        let analyst_role = Role::Analyst;
        let specialist_role = Role::specialist("backend");

        assert!(analyst_role.matches("analyst"));
        assert!(analyst_role.matches("ANALYST"));
        assert!(analyst_role.matches("Analyst"));
        assert!(!analyst_role.matches("developer"));

        assert!(specialist_role.matches("backend"));
        assert!(specialist_role.matches("BACKEND"));
        assert!(!specialist_role.matches("frontend"));
    }

    #[test]
    fn expert_role_matching() {
        let expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::specialist("backend"),
            "session".to_string(),
            "0".to_string(),
        );

        assert!(expert.matches_role("backend"));
        assert!(expert.matches_role("BACKEND"));
        assert!(!expert.matches_role("frontend"));
    }

    #[test]
    fn role_display() {
        assert_eq!(Role::Analyst.to_string(), "analyst");
        assert_eq!(Role::Developer.to_string(), "developer");
        assert_eq!(Role::specialist("custom").to_string(), "custom");
    }

    #[test]
    fn expert_activity_update() {
        let mut expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );

        let initial_activity = expert.last_activity;

        // Small delay to ensure timestamp difference
        std::thread::sleep(std::time::Duration::from_millis(1));

        expert.update_activity();
        assert!(expert.last_activity > initial_activity);
    }

    #[test]
    fn expert_serializes_to_yaml() {
        let expert = ExpertInfo::new(
            1,
            "backend-dev".to_string(),
            Role::Developer,
            "macot-session".to_string(),
            "1".to_string(),
        );

        let yaml = serde_yaml::to_string(&expert).unwrap();
        assert!(yaml.contains("id: 1"));
        assert!(yaml.contains("name: backend-dev"));
        assert!(yaml.contains("role: developer"));
        assert!(yaml.contains("state: idle"));
    }

    #[test]
    fn expert_deserializes_from_yaml() {
        let yaml = r#"
id: 2
name: "frontend-expert"
role: developer
tmux_session: "macot-session"
tmux_pane: "2"
state: idle
last_activity: "2024-01-15T10:30:00Z"
"#;

        let expert: ExpertInfo = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(expert.id, 2);
        assert_eq!(expert.name, "frontend-expert");
        assert_eq!(expert.role, Role::Developer);
        assert_eq!(expert.state, ExpertState::Idle);
    }

    #[test]
    fn worktree_path_defaults_to_none() {
        let expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );
        assert!(
            expert.worktree_path.is_none(),
            "worktree_path: should default to None"
        );
    }

    #[test]
    fn set_worktree_path_updates_field() {
        let mut expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );

        expert.set_worktree_path(Some("/path/to/worktree".to_string()));
        assert_eq!(
            expert.worktree_path,
            Some("/path/to/worktree".to_string()),
            "set_worktree_path: should set to Some"
        );

        expert.set_worktree_path(None);
        assert!(
            expert.worktree_path.is_none(),
            "set_worktree_path: should set back to None"
        );
    }

    #[test]
    fn same_worktree_both_none() {
        let a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        let b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        assert!(
            a.same_worktree(&b),
            "same_worktree: (None, None) should be true"
        );
    }

    #[test]
    fn same_worktree_both_same_path() {
        let mut a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        let mut b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        a.set_worktree_path(Some("/worktree/feature-auth".to_string()));
        b.set_worktree_path(Some("/worktree/feature-auth".to_string()));
        assert!(
            a.same_worktree(&b),
            "same_worktree: (Some(X), Some(X)) should be true"
        );
    }

    #[test]
    fn same_worktree_none_vs_some() {
        let a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        let mut b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        b.set_worktree_path(Some("/worktree/feature-auth".to_string()));
        assert!(
            !a.same_worktree(&b),
            "same_worktree: (None, Some(X)) should be false"
        );
        assert!(
            !b.same_worktree(&a),
            "same_worktree: (Some(X), None) should be false"
        );
    }

    #[test]
    fn same_worktree_different_paths() {
        let mut a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        let mut b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        a.set_worktree_path(Some("/worktree/feature-auth".to_string()));
        b.set_worktree_path(Some("/worktree/feature-payments".to_string()));
        assert!(
            !a.same_worktree(&b),
            "same_worktree: (Some(X), Some(Y)) should be false"
        );
    }

    #[test]
    fn same_worktree_is_symmetric() {
        let a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        let mut b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        b.set_worktree_path(Some("/worktree/feature-auth".to_string()));

        assert_eq!(
            a.same_worktree(&b),
            b.same_worktree(&a),
            "same_worktree: should be symmetric"
        );
    }

    #[test]
    fn same_worktree_is_reflexive() {
        let a = ExpertInfo::new(
            0,
            "a".to_string(),
            Role::Developer,
            "s".to_string(),
            "0".to_string(),
        );
        assert!(a.same_worktree(&a), "same_worktree: reflexive with None");

        let mut b = ExpertInfo::new(
            1,
            "b".to_string(),
            Role::Developer,
            "s".to_string(),
            "1".to_string(),
        );
        b.set_worktree_path(Some("/worktree/feature-auth".to_string()));
        assert!(b.same_worktree(&b), "same_worktree: reflexive with Some");
    }

    #[test]
    fn worktree_path_backward_compat_deserialization() {
        // YAML without worktree_path field should deserialize to None
        let yaml = r#"
id: 2
name: "test-expert"
role: developer
tmux_session: "session"
tmux_pane: "0"
state: idle
last_activity: "2024-01-15T10:30:00Z"
"#;
        let expert: ExpertInfo = serde_yaml::from_str(yaml).unwrap();
        assert!(
            expert.worktree_path.is_none(),
            "backward_compat: missing worktree_path should deserialize as None"
        );
    }

    #[test]
    fn worktree_path_serialization_roundtrip() {
        let mut expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );
        expert.set_worktree_path(Some("/worktree/feature".to_string()));

        let yaml = serde_yaml::to_string(&expert).unwrap();
        assert!(
            yaml.contains("worktree_path"),
            "serialization: should include worktree_path when Some"
        );

        let deserialized: ExpertInfo = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(
            deserialized.worktree_path,
            Some("/worktree/feature".to_string()),
            "serialization: roundtrip should preserve worktree_path"
        );
    }

    #[test]
    fn worktree_path_none_omitted_in_serialization() {
        let expert = ExpertInfo::new(
            0,
            "test".to_string(),
            Role::Developer,
            "session".to_string(),
            "0".to_string(),
        );

        let yaml = serde_yaml::to_string(&expert).unwrap();
        assert!(
            !yaml.contains("worktree_path"),
            "serialization: should omit worktree_path when None"
        );
    }

    #[test]
    fn expert_state_default_is_idle() {
        assert_eq!(ExpertState::default(), ExpertState::Idle);
    }

    #[test]
    fn expert_state_symbols_are_unique() {
        let states = [ExpertState::Idle, ExpertState::Busy];
        let symbols: Vec<_> = states.iter().map(|s| s.symbol()).collect();
        let unique: std::collections::HashSet<_> = symbols.iter().collect();
        assert_eq!(
            symbols.len(),
            unique.len(),
            "expert_state_symbols: all symbols should be unique"
        );
    }

    #[test]
    fn expert_state_symbol_values() {
        assert_eq!(ExpertState::Idle.symbol(), "");
        assert_eq!(ExpertState::Busy.symbol(), "");
    }

    #[test]
    fn expert_state_color_values() {
        assert_eq!(ExpertState::Idle.color(), Color::Gray);
        assert_eq!(ExpertState::Busy.color(), Color::Green);
    }

    #[test]
    fn expert_state_description_values() {
        assert_eq!(ExpertState::Idle.description(), "Waiting for input");
        assert_eq!(ExpertState::Busy.description(), "Working");
    }
}