use crate::utils::session_recovery::{
analyze_session_health, cleanup_old_sessions, cleanup_temp_files, generate_health_report,
RecoveryConfig,
};
use crate::utils::tmux;
use anyhow::{Context, Result};
pub fn execute(session_name: Option<String>, cleanup: bool, detailed: bool) -> Result<()> {
tmux::is_tmux_available()
.context("tmux is not available. Please install tmux to use sprite status.")?;
if cleanup {
return cleanup_resources();
}
let config = RecoveryConfig::default();
let health_reports = match analyze_session_health(&config) {
Ok(reports) => reports,
Err(_) => {
println!("📭 No tmux sessions found.");
println!();
println!("💡 Use 'sprite start' to create a new session.");
return Ok(());
}
};
if health_reports.is_empty() {
println!("📭 No tmux sessions found.");
println!();
println!("💡 Use 'sprite start' to create a new session.");
return Ok(());
}
let filtered_reports: Vec<_> = if let Some(ref target_session) = session_name {
health_reports
.into_iter()
.filter(|h| h.name == *target_session)
.collect()
} else {
health_reports
};
if filtered_reports.is_empty() {
if let Some(ref target_session) = session_name {
println!("📭 Session '{}' not found.", target_session);
println!("💡 Use 'sprite attach --list' to see available sessions.");
} else {
println!("📭 No tmux sessions found.");
}
return Ok(());
}
let report = generate_health_report(&filtered_reports);
println!("{}", report);
if detailed {
show_detailed_status(&filtered_reports)?;
}
show_recommendations(&filtered_reports)?;
Ok(())
}
fn cleanup_resources() -> Result<()> {
println!("🧹 Starting cleanup of old resources...");
let config = RecoveryConfig {
auto_cleanup: true,
..Default::default()
};
let cleaned_sessions = cleanup_old_sessions(&config)?;
if !cleaned_sessions.is_empty() {
println!(
"✅ Cleaned up {} old session(s): {}",
cleaned_sessions.len(),
cleaned_sessions.join(", ")
);
} else {
println!("✅ No old sessions needed cleanup.");
}
cleanup_temp_files(&config)?;
println!("✅ Cleaned up temporary files.");
println!();
println!("🎉 Cleanup completed successfully!");
Ok(())
}
fn show_detailed_status(
health_reports: &[crate::utils::session_recovery::SessionHealth],
) -> Result<()> {
println!("\n📊 Detailed Session Information");
println!("=============================");
for health in health_reports {
println!("\n🏷️ Session: {}", health.name);
println!(" Windows: {}", health.windows);
println!(" Active Panes: {}", health.active_panes);
println!(" Age: {} seconds", health.age_seconds);
match tmux::get_session_panes(&health.name) {
Ok(panes) => {
println!(" Panes:");
for (i, pane) in panes.iter().enumerate() {
let command = pane.current_command.as_deref().unwrap_or("idle");
let path = pane.current_path.as_deref().unwrap_or("unknown");
println!(" {}. {} ({})", i + 1, command, path);
}
}
Err(_) => {
println!(" ⚠️ Could not retrieve pane information");
}
}
if !health.issues.is_empty() {
println!(" Issues:");
for issue in &health.issues {
let issue_desc = match issue {
crate::utils::session_recovery::SessionIssue::NoActivePanes => {
"• No active panes found".to_string()
}
crate::utils::session_recovery::SessionIssue::IdleTooLong(duration) => {
format!("• Session has been idle for {:?}", duration)
}
crate::utils::session_recovery::SessionIssue::ZombieProcesses(count) => {
format!("• {} zombie processes detected", count)
}
crate::utils::session_recovery::SessionIssue::WorkspaceMissing(path) => {
format!("• Workspace directory missing: {}", path)
}
crate::utils::session_recovery::SessionIssue::GitIssues(msg) => {
format!("• Git repository issues: {}", msg)
}
crate::utils::session_recovery::SessionIssue::TmuxSocketIssues(msg) => {
format!("• Tmux socket issues: {}", msg)
}
crate::utils::session_recovery::SessionIssue::HighMemoryUsage(bytes) => {
format!("• High memory usage: {} MB", bytes / 1024 / 1024)
}
};
println!(" {}", issue_desc);
}
}
}
Ok(())
}
fn show_recommendations(
health_reports: &[crate::utils::session_recovery::SessionHealth],
) -> Result<()> {
println!("\n💡 Recommendations");
println!("=================");
let mut has_issues = false;
let mut needs_cleanup = false;
for health in health_reports {
match health.status {
crate::utils::session_recovery::SessionStatus::Healthy => {
println!(
"✅ Session '{}' is healthy and running normally.",
health.name
);
}
crate::utils::session_recovery::SessionStatus::Degraded => {
has_issues = true;
println!(
"⚠️ Session '{}' has issues that may affect performance:",
health.name
);
for issue in &health.issues {
let recommendation = match issue {
crate::utils::session_recovery::SessionIssue::NoActivePanes =>
format!(" • Consider restarting the session with 'sprite kill --force {} && sprite start'", health.name),
crate::utils::session_recovery::SessionIssue::IdleTooLong(_) =>
" • Session has been idle for a while, consider cleanup if no longer needed".to_string(),
crate::utils::session_recovery::SessionIssue::ZombieProcesses(_) =>
" • Kill and restart the session to clean up zombie processes".to_string(),
crate::utils::session_recovery::SessionIssue::WorkspaceMissing(_) =>
" • This appears to be an orphaned session, consider cleanup".to_string(),
crate::utils::session_recovery::SessionIssue::GitIssues(_) =>
" • Check git repository status in the agent workspaces".to_string(),
crate::utils::session_recovery::SessionIssue::TmuxSocketIssues(_) =>
" • Tmux communication issues detected, try restarting tmux server".to_string(),
crate::utils::session_recovery::SessionIssue::HighMemoryUsage(_) =>
" • High memory usage, consider restarting the session".to_string(),
};
println!("{}", recommendation);
}
}
crate::utils::session_recovery::SessionStatus::Dead => {
has_issues = true;
needs_cleanup = true;
println!(
"🔴 Session '{}' is unresponsive and should be cleaned up:",
health.name
);
println!(
" • Use 'sprite kill --force {}' to remove the dead session",
health.name
);
}
crate::utils::session_recovery::SessionStatus::Orphaned => {
has_issues = true;
needs_cleanup = true;
println!("⚪ Session '{}' appears to be orphaned:", health.name);
println!(
" • Use 'sprite kill --force {}' to remove the orphaned session",
health.name
);
}
}
}
if needs_cleanup {
println!();
println!("🧹 General cleanup recommendations:");
println!(" • Use 'sprite status --cleanup' to automatically clean up old sessions");
println!(" • Use 'sprite kill --all --force' to remove all sessions (use with caution)");
}
if !has_issues {
println!();
println!("🎉 All sessions are running normally!");
println!(" • Use 'sprite status --detailed' for more information");
println!(" • Use 'sprite status --cleanup' to clean up old resources");
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn test_status_execution() {
}
}