feat: add ecc2 dashboard worktree cleanup control

This commit is contained in:
Affaan Mustafa
2026-04-07 11:40:32 -07:00
parent bdbed70436
commit cbdced9979
4 changed files with 133 additions and 1 deletions

View File

@@ -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")?;

View File

@@ -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",