The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
feat: add ecc2 dashboard worktree cleanup control
Affaan Mustafacbdced99797b
diff --git a/ecc2/src/session/manager.rs b/ecc2/src/session/manager.rsindex 612965ece..ef7b5faf9 100644--- a/ecc2/src/session/manager.rs+++ b/ecc2/src/session/manager.rs@@ -90,6 +90,23 @@ pub async fn resume_session(db: &StateStore, id: &str) -> Result<String> {Ok(session.id)}+pub async fn cleanup_session_worktree(db: &StateStore, id: &str) -> Result<()> {+ let session = resolve_session(db, id)?;++ if session.state == SessionState::Running {+ stop_session_with_options(db, &session.id, true).await?;+ db.clear_worktree(&session.id)?;+ return Ok(());+ }++ if let Some(worktree) = session.worktree.as_ref() {+ crate::worktree::remove(&worktree.path)?;+ db.clear_worktree(&session.id)?;+ }++ Ok(())+}+fn agent_program(agent_type: &str) -> Result<PathBuf> {match agent_type {"claude" => Ok(PathBuf::from("claude")),@@ -661,6 +678,49 @@ mod tests {Ok(())}+ #[tokio::test(flavor = "current_thread")]+ async fn cleanup_session_worktree_removes_path_and_clears_metadata() -> Result<()> {+ let tempdir = TestDir::new("manager-cleanup-worktree")?;+ let repo_root = tempdir.path().join("repo");+ init_git_repo(&repo_root)?;++ let cfg = build_config(tempdir.path());+ let db = StateStore::open(&cfg.db_path)?;+ let (fake_claude, _) = write_fake_claude(tempdir.path())?;++ let session_id = create_session_in_dir(+ &db,+ &cfg,+ "cleanup later",+ "claude",+ true,+ &repo_root,+ &fake_claude,+ )+ .await?;++ stop_session_with_options(&db, &session_id, false).await?;+ let stopped = db+ .get_session(&session_id)?+ .context("stopped session should exist")?;+ let worktree_path = stopped+ .worktree+ .clone()+ .context("stopped session worktree missing")?+ .path;+ assert!(worktree_path.exists(), "worktree should still exist before cleanup");++ cleanup_session_worktree(&db, &session_id).await?;++ let cleaned = db+ .get_session(&session_id)?+ .context("cleaned session should still exist")?;+ assert!(cleaned.worktree.is_none(), "worktree metadata should be cleared");+ assert!(!worktree_path.exists(), "worktree path should be removed");++ Ok(())+ }+#[test]fn get_status_supports_latest_alias() -> Result<()> {let tempdir = TestDir::new("manager-latest-status")?;diff --git a/ecc2/src/session/store.rs b/ecc2/src/session/store.rsindex a547ac2ae..a01dfb51b 100644--- a/ecc2/src/session/store.rs+++ b/ecc2/src/session/store.rs@@ -202,6 +202,21 @@ impl StateStore {Ok(())}+ pub fn clear_worktree(&self, session_id: &str) -> Result<()> {+ let updated = self.conn.execute(+ "UPDATE sessions+ SET worktree_path = NULL, worktree_branch = NULL, worktree_base = NULL, updated_at = ?1+ WHERE id = ?2",+ rusqlite::params![chrono::Utc::now().to_rfc3339(), session_id],+ )?;++ if updated == 0 {+ anyhow::bail!("Session not found: {session_id}");+ }++ Ok(())+ }+pub fn update_metrics(&self, session_id: &str, metrics: &SessionMetrics) -> Result<()> {self.conn.execute("UPDATE sessions SET tokens_used = ?1, tool_calls = ?2, files_changed = ?3, duration_secs = ?4, cost_usd = ?5, updated_at = ?6 WHERE id = ?7",diff --git a/ecc2/src/tui/app.rs b/ecc2/src/tui/app.rsindex 7155b43ad..92dc36d0f 100644--- a/ecc2/src/tui/app.rs+++ b/ecc2/src/tui/app.rs@@ -41,6 +41,7 @@ pub async fn run(db: StateStore, cfg: Config) -> Result<()> {(_, KeyCode::Char('n')) => dashboard.new_session(),(_, KeyCode::Char('s')) => dashboard.stop_selected().await,(_, KeyCode::Char('u')) => dashboard.resume_selected().await,+ (_, KeyCode::Char('x')) => dashboard.cleanup_selected_worktree().await,(_, KeyCode::Char('r')) => dashboard.refresh(),(_, KeyCode::Char('?')) => dashboard.toggle_help(),_ => {}diff --git a/ecc2/src/tui/dashboard.rs b/ecc2/src/tui/dashboard.rsindex c87c03d6b..0bc6c5cd4 100644--- a/ecc2/src/tui/dashboard.rs+++ b/ecc2/src/tui/dashboard.rs@@ -345,7 +345,7 @@ impl Dashboard {fn render_status_bar(&self, frame: &mut Frame, area: Rect) {let text = format!(- " [n]ew session [s]top [u]resume [r]efresh [Tab] switch pane [j/k] scroll [+/-] resize [{}] layout [?] help [q]uit ",+ " [n]ew session [s]top [u]resume [x]cleanup [r]efresh [Tab] switch pane [j/k] scroll [+/-] resize [{}] layout [?] help [q]uit ",self.layout_label());let aggregate = self.aggregate_usage();@@ -387,6 +387,7 @@ impl Dashboard {" n New session"," s Stop selected session"," u Resume selected session",+ " x Cleanup selected worktree"," Tab Next pane"," S-Tab Previous pane"," j/↓ Scroll down",@@ -527,6 +528,23 @@ impl Dashboard {self.refresh();}+ pub async fn cleanup_selected_worktree(&mut self) {+ let Some(session) = self.sessions.get(self.selected_session) else {+ return;+ };++ if session.worktree.is_none() {+ return;+ }++ if let Err(error) = manager::cleanup_session_worktree(&self.db, &session.id).await {+ tracing::warn!("Failed to cleanup session {} worktree: {error}", session.id);+ return;+ }++ self.refresh();+ }+pub fn refresh(&mut self) {self.sync_from_store();}@@ -1341,6 +1359,44 @@ mod tests {Ok(())}+ #[tokio::test]+ async fn cleanup_selected_worktree_clears_session_metadata() -> Result<()> {+ let db_path = std::env::temp_dir().join(format!("ecc2-dashboard-{}.db", Uuid::new_v4()));+ let db = StateStore::open(&db_path)?;+ let now = Utc::now();+ let worktree_path = std::env::temp_dir().join(format!("ecc2-cleanup-{}", Uuid::new_v4()));+ std::fs::create_dir_all(&worktree_path)?;++ db.insert_session(&Session {+ id: "stopped-1".to_string(),+ task: "cleanup me".to_string(),+ agent_type: "claude".to_string(),+ state: SessionState::Stopped,+ pid: None,+ worktree: Some(WorktreeInfo {+ path: worktree_path.clone(),+ branch: "ecc/stopped-1".to_string(),+ base_branch: "main".to_string(),+ }),+ created_at: now,+ updated_at: now,+ metrics: SessionMetrics::default(),+ })?;++ let dashboard_store = StateStore::open(&db_path)?;+ let mut dashboard = Dashboard::new(dashboard_store, Config::default());+ dashboard.cleanup_selected_worktree().await;++ let session = db+ .get_session("stopped-1")?+ .expect("session should exist after cleanup");+ assert!(session.worktree.is_none(), "worktree metadata should be cleared");++ let _ = std::fs::remove_dir_all(worktree_path);+ let _ = std::fs::remove_file(db_path);+ Ok(())+ }+#[test]fn grid_layout_renders_four_panes() {let mut dashboard = test_dashboard(vec![sample_session("grid-1", "claude", SessionState::Running, None, 1, 1)], 0);