sprit-mutilagent 0.3.2

A robust command-line toolkit for managing multiple AI coding agents in isolated tmux sessions with comprehensive workspace management and health monitoring
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
//! Session and tmux profile data structures for the Sprite multi-agent workflow toolkit.

use super::{ConflictResolution, LogLevel};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Represents the tmux session that hosts all agent panes and provides supervision interface.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    /// Unique session identifier
    pub name: String,

    /// Current session status
    pub status: SessionStatus,

    /// Tmux profile being used
    pub profile: String,

    /// Active profile configuration
    pub profile_config: TmuxProfile,

    /// Creation timestamp
    pub created_at: DateTime<Utc>,

    /// Last activity timestamp
    pub last_activity: Option<DateTime<Utc>>,

    /// Session-specific settings
    pub settings: SessionSettings,
}

#[allow(dead_code)]
impl Session {
    /// Create a new session with basic configuration.
    pub fn new(name: impl Into<String>, profile: TmuxProfile) -> Self {
        let name_str = name.into();
        Self {
            name: name_str.clone(),
            status: SessionStatus::Creating,
            profile: profile.name.clone(),
            profile_config: profile,
            created_at: Utc::now(),
            last_activity: None,
            settings: SessionSettings::default(),
        }
    }

    /// Update the session's last activity timestamp.
    pub fn update_activity(&mut self) {
        self.last_activity = Some(Utc::now());
    }

    /// Check if the session is currently active.
    pub fn is_active(&self) -> bool {
        matches!(self.status, SessionStatus::Active | SessionStatus::Detached)
    }

    /// Check if the session is in an error state.
    pub fn has_error(&self) -> bool {
        matches!(self.status, SessionStatus::Error(_))
    }

    /// Get the error message if the session is in an error state.
    pub fn error_message(&self) -> Option<&str> {
        match &self.status {
            SessionStatus::Error(msg) => Some(msg),
            _ => None,
        }
    }

    /// Get the duration since session creation.
    pub fn duration(&self) -> chrono::Duration {
        Utc::now() - self.created_at
    }

    /// Validate the session configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.name.is_empty() {
            return Err("Session name cannot be empty".to_string());
        }

        // Validate session name format (alphanumeric with hyphens/underscores)
        if !self
            .name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(
                "Session name can only contain alphanumeric characters, hyphens, and underscores"
                    .to_string(),
            );
        }

        // Validate profile configuration
        self.profile_config.validate()?;

        Ok(())
    }
}

/// Current status of a session.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum SessionStatus {
    /// Session is being created
    #[default]
    Creating,
    /// Session is active and running
    Active,
    /// Session is detached but still running
    Detached,
    /// Session is being terminated
    Terminating,
    /// Session encountered an error
    Error(String),
}

/// Session-specific settings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSettings {
    /// Whether to auto-sync agents
    pub auto_sync: bool,

    /// Sync interval in seconds
    pub sync_interval_secs: Option<u64>,

    /// Conflict resolution strategy
    pub conflict_resolution: ConflictResolution,

    /// Log level
    pub log_level: LogLevel,

    /// Whether to enable activity monitoring
    pub enable_monitoring: bool,

    /// Maximum session duration in seconds (None for unlimited)
    pub max_duration_secs: Option<u64>,

    /// Whether to automatically cleanup on termination
    pub auto_cleanup: bool,
}

impl Default for SessionSettings {
    fn default() -> Self {
        Self {
            auto_sync: false,
            sync_interval_secs: Some(300), // 5 minutes
            conflict_resolution: ConflictResolution::default(),
            log_level: LogLevel::default(),
            enable_monitoring: true,
            max_duration_secs: None, // Unlimited
            auto_cleanup: true,
        }
    }
}

/// Represents pre-configured tmux layout profiles for different agent arrangements.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TmuxProfile {
    /// Profile identifier (profile0 - profile5)
    pub name: String,

    /// Profile description and use case
    pub description: String,

    /// Best for number of agents
    pub best_for_agent_count: Option<u32>,

    /// Layout type and configuration
    pub layout: ProfileLayout,

    /// Shell script path for this profile
    pub script_path: PathBuf,

    /// Whether this profile includes a supervisor pane
    pub includes_supervisor: bool,

    /// Supervisor configuration (if applicable)
    pub supervisor_config: Option<SupervisorConfig>,
}

#[allow(dead_code)]
impl TmuxProfile {
    /// Validate the profile configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.name.is_empty() {
            return Err("Profile name cannot be empty".to_string());
        }

        if self.script_path.as_os_str().is_empty() {
            return Err("Profile script path cannot be empty".to_string());
        }

        self.layout.validate()?;

        if let Some(ref supervisor) = self.supervisor_config {
            supervisor.validate()?;
        }

        Ok(())
    }

    /// Check if this profile is suitable for the given number of agents.
    pub fn is_suitable_for(&self, agent_count: u32) -> bool {
        self.best_for_agent_count
            .map(|best| agent_count <= best)
            .unwrap_or(true)
    }

    /// Get the total number of panes this profile creates.
    pub fn total_panes(&self) -> u32 {
        let mut count = self.layout.pane_sizes.len() as u32;
        if self.includes_supervisor {
            count += 1;
        }
        count
    }
}

/// Layout configuration for tmux profiles.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileLayout {
    /// Layout pattern: main-horizontal, main-vertical, even-horizontal, tiled
    pub layout_type: String,

    /// Pane size specifications
    pub pane_sizes: Vec<PaneSize>,

    /// Root pane configuration (main repository)
    pub root_pane: Option<RootPaneConfig>,
}

#[allow(dead_code)]
impl ProfileLayout {
    /// Validate the layout configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.layout_type.is_empty() {
            return Err("Layout type cannot be empty".to_string());
        }

        if self.pane_sizes.is_empty() {
            return Err("Layout must have at least one pane size specification".to_string());
        }

        // Validate layout type
        let valid_layouts = [
            "main-horizontal",
            "main-vertical",
            "even-horizontal",
            "tiled",
        ];
        if !valid_layouts.contains(&self.layout_type.as_str()) {
            return Err(format!(
                "Invalid layout type '{}'. Valid options: {}",
                self.layout_type,
                valid_layouts.join(", ")
            ));
        }

        for (i, pane_size) in self.pane_sizes.iter().enumerate() {
            if let Err(e) = pane_size.validate() {
                return Err(format!("Pane size {} validation failed: {}", i + 1, e));
            }
        }

        Ok(())
    }
}

/// Size and position specification for a pane.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaneSize {
    /// Agent identifier
    pub agent_id: String,

    /// Percentage or size specification
    pub size_spec: String,

    /// Position in layout
    pub position: LayoutPosition,
}

#[allow(dead_code)]
impl PaneSize {
    /// Validate the pane size specification.
    pub fn validate(&self) -> Result<(), String> {
        if self.agent_id.is_empty() {
            return Err("Agent ID cannot be empty".to_string());
        }

        if self.size_spec.is_empty() {
            return Err("Size specification cannot be empty".to_string());
        }

        // Validate size spec format (e.g., "50%", "80px", etc.)
        if !self.size_spec.ends_with('%') && !self.size_spec.ends_with("px") {
            return Err("Size specification must end with '%' or 'px'".to_string());
        }

        Ok(())
    }
}

/// Position of a pane in the layout.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum LayoutPosition {
    Top,
    Bottom,
    Left,
    Right,
    Center,
}

/// Configuration for the root pane (main repository).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootPaneConfig {
    /// Whether root pane is enabled
    pub enabled: bool,

    /// Root pane position
    pub position: LayoutPosition,

    /// Root pane size
    pub size: String,

    /// Default working directory
    pub work_dir: PathBuf,
}

/// Supervisor configuration for session management.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupervisorConfig {
    /// Supervisor pane position
    pub position: SupervisorPosition,

    /// Supervisor pane size
    pub size: SupervisorSize,

    /// Default working directory for supervisor
    pub work_dir: PathBuf,

    /// Commands to run in supervisor pane on startup
    pub startup_commands: Vec<String>,

    /// Whether supervisor pane should be focused by default
    pub focus_by_default: bool,
}

#[allow(dead_code)]
impl SupervisorConfig {
    /// Validate the supervisor configuration.
    pub fn validate(&self) -> Result<(), String> {
        if self.work_dir.as_os_str().is_empty() {
            return Err("Supervisor work directory cannot be empty".to_string());
        }

        Ok(())
    }
}

/// Position of the supervisor pane.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SupervisorPosition {
    Top,
    Bottom,
    Left,
    Right,
    Hidden,
}

/// Size specification for supervisor pane.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SupervisorSize {
    /// Fixed size in pixels
    Fixed(u32),
    /// Percentage of available space
    Percentage(u8),
    /// Auto-size based on content
    Auto,
}

/// Session layout configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct SessionLayout {
    /// Layout type: tiled, main-vertical, main-horizontal, custom
    pub layout_type: LayoutType,

    /// Pane arrangement specifications
    pub pane_config: Vec<PaneConfig>,

    /// Session dimensions
    pub dimensions: Option<SessionDimensions>,
}

/// Layout type enumeration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[allow(dead_code)]
pub enum LayoutType {
    /// All panes arranged in a grid
    Tiled,
    /// One main vertical pane with smaller panes on the side
    MainVertical,
    /// One main horizontal pane with smaller panes below
    MainHorizontal,
    /// Custom layout configuration
    Custom(String),
}

/// Configuration for individual panes in a session.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct PaneConfig {
    /// Agent name for this pane
    pub agent_name: String,

    /// Pane size/position specifications
    pub size: PaneSize,

    /// Pane index in layout
    pub index: u32,
}

/// Session dimensions for layout calculations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct SessionDimensions {
    /// Width in characters
    pub width: u32,

    /// Height in characters
    pub height: u32,
}

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

    #[test]
    fn test_session_creation() {
        let profile = TmuxProfile {
            name: "test-profile".to_string(),
            description: "Test profile".to_string(),
            best_for_agent_count: Some(3),
            layout: ProfileLayout {
                layout_type: "tiled".to_string(),
                pane_sizes: vec![PaneSize {
                    agent_id: "agent-1".to_string(),
                    size_spec: "50%".to_string(),
                    position: LayoutPosition::Top,
                }],
                root_pane: None,
            },
            script_path: PathBuf::from("test.sh"),
            includes_supervisor: false,
            supervisor_config: None,
        };

        let session = Session::new("test-session", profile);
        assert_eq!(session.name, "test-session");
        assert_eq!(session.status, SessionStatus::Creating);
        assert_eq!(session.profile, "test-profile");
    }

    #[test]
    fn test_session_validation() {
        let profile = TmuxProfile {
            name: "test-profile".to_string(),
            description: "Test profile".to_string(),
            best_for_agent_count: Some(3),
            layout: ProfileLayout {
                layout_type: "tiled".to_string(),
                pane_sizes: vec![PaneSize {
                    agent_id: "agent-1".to_string(),
                    size_spec: "50%".to_string(),
                    position: LayoutPosition::Top,
                }],
                root_pane: None,
            },
            script_path: PathBuf::from("test.sh"),
            includes_supervisor: false,
            supervisor_config: None,
        };

        let session = Session::new("valid-session", profile.clone());
        assert!(session.validate().is_ok());

        // Test empty name
        let mut invalid_session = Session::new("", profile.clone());
        assert!(invalid_session.validate().is_err());

        // Test invalid name format
        invalid_session.name = "invalid session!".to_string();
        assert!(invalid_session.validate().is_err());
    }

    #[test]
    fn test_profile_validation() {
        let valid_profile = TmuxProfile {
            name: "profile0".to_string(),
            description: "Valid profile".to_string(),
            best_for_agent_count: Some(3),
            layout: ProfileLayout {
                layout_type: "tiled".to_string(),
                pane_sizes: vec![PaneSize {
                    agent_id: "agent-1".to_string(),
                    size_spec: "50%".to_string(),
                    position: LayoutPosition::Top,
                }],
                root_pane: None,
            },
            script_path: PathBuf::from("profile0.sh"),
            includes_supervisor: false,
            supervisor_config: None,
        };

        assert!(valid_profile.validate().is_ok());

        // Test empty layout type
        let mut invalid_profile = valid_profile.clone();
        invalid_profile.layout.layout_type = "".to_string();
        assert!(invalid_profile.validate().is_err());

        // Test invalid layout type
        invalid_profile.layout.layout_type = "invalid".to_string();
        assert!(invalid_profile.validate().is_err());
    }

    #[test]
    fn test_pane_size_validation() {
        let valid_pane = PaneSize {
            agent_id: "agent-1".to_string(),
            size_spec: "50%".to_string(),
            position: LayoutPosition::Top,
        };

        assert!(valid_pane.validate().is_ok());

        // Test empty agent ID
        let mut invalid_pane = valid_pane.clone();
        invalid_pane.agent_id = "".to_string();
        assert!(invalid_pane.validate().is_err());

        // Test invalid size spec
        invalid_pane.agent_id = "agent-1".to_string();
        invalid_pane.size_spec = "50".to_string(); // Missing % or px
        assert!(invalid_pane.validate().is_err());
    }

    #[test]
    fn test_session_settings() {
        let settings = SessionSettings::default();
        assert!(!settings.auto_sync);
        assert_eq!(settings.sync_interval_secs, Some(300));
        assert_eq!(settings.conflict_resolution, ConflictResolution::Manual);
        assert!(settings.enable_monitoring);
        assert!(settings.auto_cleanup);
    }
}