From 124fe1544fd0bf3eb599d6da5a0e0a215a18c612 Mon Sep 17 00:00:00 2001 From: Mufeed VH Date: Wed, 2 Jul 2025 18:34:13 +0530 Subject: [PATCH] fix(agents): improve session tracking accuracy with process registry cross-check - Cross-check database running sessions with actual process registry - Filter out stale database entries for crashed processes without cleanup - Add comprehensive comments explaining the reliability improvements - Fix sticky header z-index layering issue in AgentExecution component This addresses cases where agent processes crash without properly updating the database status, ensuring the UI shows only truly active sessions. --- src-tauri/src/commands/agents.rs | 29 +++++++++++++++++++++++++++-- src/components/AgentExecution.tsx | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/commands/agents.rs b/src-tauri/src/commands/agents.rs index b903f40..10b0fd8 100644 --- a/src-tauri/src/commands/agents.rs +++ b/src-tauri/src/commands/agents.rs @@ -1466,15 +1466,19 @@ pub async fn execute_agent( /// List all currently running agent sessions #[tauri::command] -pub async fn list_running_sessions(db: State<'_, AgentDb>) -> Result, String> { +pub async fn list_running_sessions( + db: State<'_, AgentDb>, + registry: State<'_, crate::process::ProcessRegistryState>, +) -> Result, String> { let conn = db.0.lock().map_err(|e| e.to_string())?; + // First get all running sessions from the database let mut stmt = conn.prepare( "SELECT id, agent_id, agent_name, agent_icon, task, model, project_path, session_id, status, pid, process_started_at, created_at, completed_at FROM agent_runs WHERE status = 'running' ORDER BY process_started_at DESC" ).map_err(|e| e.to_string())?; - let runs = stmt + let mut runs = stmt .query_map([], |row| { Ok(AgentRun { id: Some(row.get(0)?), @@ -1502,6 +1506,27 @@ pub async fn list_running_sessions(db: State<'_, AgentDb>) -> Result, _>>() .map_err(|e| e.to_string())?; + drop(stmt); + drop(conn); + + // Cross-check with the process registry to ensure accuracy + // Get actually running processes from the registry + let registry_processes = registry.0.get_running_agent_processes()?; + let registry_run_ids: std::collections::HashSet = registry_processes + .iter() + .map(|p| p.run_id) + .collect(); + + // Filter out any database entries that aren't actually running in the registry + // This handles cases where processes crashed without updating the database + runs.retain(|run| { + if let Some(run_id) = run.id { + registry_run_ids.contains(&run_id) + } else { + false + } + }); + Ok(runs) } diff --git a/src/components/AgentExecution.tsx b/src/components/AgentExecution.tsx index 8a515c6..f929560 100644 --- a/src/components/AgentExecution.tsx +++ b/src/components/AgentExecution.tsx @@ -495,7 +495,7 @@ export const AgentExecution: React.FC = ({ {/* Fixed container that takes full height */}
{/* Sticky Header */} -
+