{"record":{"id":"de97b9cfd61fc0ac","repo":"affaan-m/ECC","slug":"cannot-delete-active-session-while-it-is","errorCode":null,"errorMessage":"Cannot delete active session {} while it is {}","messagePattern":"Cannot delete active session (.+?) while it is (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"ecc2/src/session/manager.rs","lineNumber":2314,"sourceCode":"            dirty.push(entry.session_id.clone());\n        } else if entry.worktree_health == worktree::WorktreeHealth::Conflicted {\n            conflicted.push(entry.session_id.clone());\n        } else {\n            queue_blocked.push(entry.session_id.clone());\n        }\n    }\n\n    (active, conflicted, dirty, queue_blocked)\n}\n\npub async fn delete_session(db: &StateStore, id: &str) -> Result<()> {\n    let session = resolve_session(db, id)?;\n\n    if matches!(\n        session.state,\n        SessionState::Pending | SessionState::Running | SessionState::Idle\n    ) {\n        anyhow::bail!(\n            \"Cannot delete active session {} while it is {}\",\n            session.id,\n            session.state\n        );\n    }\n\n    if let Some(worktree) = session.worktree.as_ref() {\n        let _ = crate::worktree::remove(worktree);\n    }\n\n    db.delete_session(&session.id)?;\n    Ok(())\n}\n\nfn agent_program(cfg: &Config, agent_type: &str) -> Result<PathBuf> {\n    let harness = HarnessKind::from_agent_type(agent_type);\n    let runner_key = SessionHarnessInfo::runner_key(agent_type);\n    if let Some(runner) = cfg.harness_runner(&runner_key) {","sourceCodeStart":2296,"sourceCodeEnd":2332,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/ecc2/src/session/manager.rs#L2296-L2332","documentation":"The delete operation refuses to delete a session that is active (Pending, Running, or Idle). Deleting an active session would destroy its state/pid records and potentially its worktree while a process may still be using them. Note that unlike merge/rebase, Stale sessions are still deletable here.","triggerScenarios":"Calling the delete-session API (manager.rs:2314) on a session whose state matches Pending | Running | Idle.","commonSituations":"User deleting a session from the UI while its agent is running; cleanup scripts deleting queued sessions; trying to purge a session that is Idle but hasn't been finalized; a runner crashed leaving the session Idle and blocking deletion.","solutions":["Stop/cancel the running session first, wait for terminal state, then delete","If the runner is dead, reset or finalize the session state (e.g., mark Failed) and then delete","Skip deletion of active sessions in cleanup scripts and retry later","Check resolve_session(db, id).state before calling delete"],"exampleFix":"// before\ndelete_session(db, id)?;\n// after\nlet session = resolve_session(db, id)?;\nif matches!(session.state, SessionState::Pending | SessionState::Running | SessionState::Idle) {\n    eprintln!(\"session {} is active ({:?}); not deleting\", id, session.state);\n} else {\n    delete_session(db, id)?;\n}","handlingStrategy":"validation","validationCode":"let session = resolve_session(db, id)?;\nif matches!(session.state, SessionState::Pending | SessionState::Running | SessionState::Idle) {\n    return Err(format!(\"session {} not deletable in state {:?}\", id, session.state));\n}","typeGuard":"fn is_deletable(s: &Session) -> bool { !matches!(s.state, SessionState::Pending | SessionState::Running | SessionState::Idle) }","tryCatchPattern":"match delete_session(db, id) {\n    Err(e) if e.to_string().starts_with(\"Cannot delete active session\") => { /* stop session then retry */ }\n    other => other,\n}","preventionTips":["Cancel sessions before deleting them in cleanup scripts","Finalize crashed sessions (mark Failed) so they become deletable","Show delete actions only for terminal-state sessions in UIs"],"tags":["session","deletion","state-machine"],"backgroundTag":"invalid-state-transition","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}