Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(workflow-status): toolbar Start/Stop tracks ad-hoc + deployed runs
trohitgd49894de2646
diff --git a/client/src/Dashboard.tsx b/client/src/Dashboard.tsxindex 109416c3..1c8a4286 100644--- a/client/src/Dashboard.tsx+++ b/client/src/Dashboard.tsx@@ -189,7 +189,7 @@ const DashboardContent: React.FC = () => {} = useWorkflowManagement();const { collapsedSections, searchQuery, setSearchQuery, toggleSection } = useComponentPalette();- const { saveNodeParameters, getAllNodeParameters, executeWorkflow, deployWorkflow, cancelDeployment, nodeStatuses, deploymentStatus, workflowLock, isConnected, isReady, sendRequest } = useWebSocket();+ const { saveNodeParameters, getAllNodeParameters, executeWorkflow, deployWorkflow, cancelDeployment, cancelExecution, getWorkflowStatus, nodeStatuses, deploymentStatus, workflowLock, isConnected, isReady, sendRequest } = useWebSocket();const applyUIDefaults = useAppStore((state) => state.applyUIDefaults);// Workflows list: server-owned data, cached by TanStack Query.@@ -200,6 +200,12 @@ const DashboardContent: React.FC = () => {// Only show as "running" or "locked" if it applies to the currently viewed workflowconst isCurrentWorkflowDeployed = deploymentStatus.isRunning &&deploymentStatus.workflow_id === currentWorkflow?.id;+ // Backend is the source of truth: per-workflow `isExecuting` is set by+ // the `workflow_status` handler whenever ANY active run exists for the+ // current workflow (ad-hoc node, whole-workflow run, deployed trigger+ // run, or deployment registration). The toolbar Start/Stop button+ // tracks this unified signal so it stays Stop while nodes glow.+ const isCurrentWorkflowActive = isExecuting || isCurrentWorkflowDeployed;const isCurrentWorkflowLocked = workflowLock.locked &&workflowLock.workflow_id === currentWorkflow?.id;const [globalModelDefaults, setGlobalModelDefaults] = React.useState<{ provider: string; model: string } | null>(null);@@ -275,6 +281,26 @@ const DashboardContent: React.FC = () => {localStorage.setItem('workflow_settings', JSON.stringify(settings));}, [settings]);+ // Resync per-workflow execution status whenever the WS connection becomes+ // ready or the current workflow changes. Closes the gap where a mid-run+ // reconnect or a workflow switch left the toolbar Start/Stop button stale+ // because broadcasts only fire on transitions, not on join.+ React.useEffect(() => {+ if (!isReady || !currentWorkflow?.id) return;+ const wfId = currentWorkflow.id;+ let cancelled = false;+ (async () => {+ try {+ const { executing } = await getWorkflowStatus(wfId);+ if (cancelled) return;+ useAppStore.getState().setWorkflowExecuting(wfId, executing);+ } catch {+ // Silent: the next broadcast will resync; this is opportunistic.+ }+ })();+ return () => { cancelled = true; };+ }, [isReady, currentWorkflow?.id, getWorkflowStatus]);+// Load UI defaults from database on initial WebSocket connectionconst hasLoadedUIDefaults = React.useRef(false);React.useEffect(() => {@@ -637,20 +663,28 @@ const DashboardContent: React.FC = () => {}};- // Cancel running deployment for current workflow+ // Stop click handler — routes to the right cancel based on what's actually+ // running. If the workflow is deployed, cancel the deployment (existing+ // path). Otherwise cancel any in-flight ad-hoc execution(s). Falls+ // through silently if neither is true.const handleCancelDeployment = async () => {+ const workflowId = currentWorkflow?.id;+ if (!workflowId) return;try {- const workflowId = currentWorkflow?.id;- console.log('[Dashboard] Cancelling deployment for workflow:', workflowId);- const result = await cancelDeployment(workflowId);-- if (result.success) {- console.log('[Dashboard] Deployment cancelled:', result);- } else {- console.error('[Dashboard] Failed to cancel deployment:', result.message);+ if (isCurrentWorkflowDeployed) {+ console.log('[Dashboard] Cancelling deployment for workflow:', workflowId);+ const result = await cancelDeployment(workflowId);+ if (!result?.success) {+ console.error('[Dashboard] Failed to cancel deployment:', result?.message);+ }+ return;+ }+ if (isExecuting) {+ console.log('[Dashboard] Cancelling ad-hoc execution for workflow:', workflowId);+ await cancelExecution(workflowId);}} catch (error: any) {- console.error('[Dashboard] Cancel deployment error:', error);+ console.error('[Dashboard] Cancel error:', error);}};@@ -1066,7 +1100,7 @@ const DashboardContent: React.FC = () => {isRunning={isExecuting}onDeploy={handleDeploy}onCancelDeployment={handleCancelDeployment}- isDeploying={isCurrentWorkflowDeployed}+ isDeploying={isCurrentWorkflowActive}hasUnsavedChanges={hasUnsavedChanges}sidebarVisible={sidebarVisible}onToggleSidebar={toggleSidebar}diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsxindex a7eb6cff..4fe761b6 100644--- a/client/src/contexts/WebSocketContext.tsx+++ b/client/src/contexts/WebSocketContext.tsx@@ -329,6 +329,8 @@ interface WebSocketContextValue {deployWorkflow: (workflowId: string, nodes: any[], edges: any[], sessionId?: string) => Promise<any>;cancelDeployment: (workflowId?: string) => Promise<any>;getDeploymentStatus: (workflowId?: string) => Promise<{ isRunning: boolean; activeRuns: number; settings?: any; workflow_id?: string }>;+ cancelExecution: (workflowId: string, nodeId?: string) => Promise<any>;+ getWorkflowStatus: (workflowId: string) => Promise<{ executing: boolean }>;// AI OperationsexecuteAiNode: (nodeId: string, nodeType: string, parameters: Record<string, any>, model: string, workflowId: string, nodes: any[], edges: any[]) => Promise<any>;@@ -871,9 +873,21 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi}break;- case 'workflow_status':+ case 'workflow_status': {+ // Backend broadcasts this for both ad-hoc executions (execute_node /+ // execute_workflow) and explicit cancels. The active-run counter+ // logic in StatusBroadcaster guarantees that we receive an+ // `executing=true` once when work starts and an `executing=false`+ // once when the last concurrent run finishes -- safe to fan out+ // directly to per-workflow Zustand state without dedup.setWorkflowStatus(data || defaultWorkflowStatus);+ const wfId = message.workflow_id || data?.workflow_id;+ if (wfId && data && typeof data.executing === 'boolean') {+ const { setWorkflowExecuting } = useAppStore.getState();+ setWorkflowExecuting(wfId, data.executing);+ }break;+ }case 'deployment_status':// Handle deployment status updates (event-driven, no iterations)@@ -1916,6 +1930,40 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chi}}, [sendRequest]);+ // Cancel any active ad-hoc execution(s) for a workflow. The backend+ // resets every node currently glowing for this workflow_id and clears+ // its active-run counter. Distinct from cancelDeployment, which only+ // touches deployed workflows.+ const cancelExecutionAsync = useCallback(async (workflowId: string, nodeId?: string): Promise<any> => {+ try {+ const response = await sendRequest<any>('cancel_execution', {+ workflow_id: workflowId,+ node_id: nodeId,+ });+ // Optimistic local clear so the UI reflects the action immediately.+ const { setWorkflowExecuting } = useAppStore.getState();+ setWorkflowExecuting(workflowId, false);+ return response;+ } catch (error) {+ console.error('[WebSocket] Failed to cancel execution:', error);+ throw error;+ }+ }, [sendRequest]);++ // Snapshot per-workflow execution status from the backend's active-run+ // counter cache. Called on reconnect / workflow switch so the toolbar+ // button reflects current truth even if we missed broadcasts.+ const getWorkflowStatusAsync = useCallback(async (workflowId: string): Promise<{ executing: boolean }> => {+ try {+ const response = await sendRequest<any>('get_workflow_status', { workflow_id: workflowId });+ const data = response?.data || {};+ return { executing: !!data.executing };+ } catch (error) {+ console.error('[WebSocket] Failed to get workflow status:', error);+ return { executing: false };+ }+ }, [sendRequest]);+// =========================================================================// AI Operations// =========================================================================@@ -2511,6 +2559,8 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chideployWorkflow: deployWorkflowAsync,cancelDeployment: cancelDeploymentAsync,getDeploymentStatus: getDeploymentStatusAsync,+ cancelExecution: cancelExecutionAsync,+ getWorkflowStatus: getWorkflowStatusAsync,// AI OperationsexecuteAiNode: executeAiNodeAsync,@@ -2567,6 +2617,7 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chiexecuteNodeAsync, executeWorkflowAsync, getNodeOutputAsync,cancelEventWaitAsync,deployWorkflowAsync, cancelDeploymentAsync, getDeploymentStatusAsync,+ cancelExecutionAsync, getWorkflowStatusAsync,executeAiNodeAsync, getAiModelsAsync,validateApiKeyAsync, getStoredApiKeyAsync, saveApiKeyAsync, deleteApiKeyAsync,getAndroidDevicesAsync, executeAndroidActionAsync,diff --git a/server/routers/websocket.py b/server/routers/websocket.pyindex a8598e70..59f0e6c8 100644--- a/server/routers/websocket.py+++ b/server/routers/websocket.py@@ -400,28 +400,43 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dicawait broadcaster.update_node_status(node_id, "executing", {"execution_id": execution_id}, workflow_id=workflow_id,)- result = await workflow_service.execute_node(- node_id=node_id, node_type=node_type,- parameters=data.get("parameters", {}),- nodes=data.get("nodes", []), edges=data.get("edges", []),- session_id=data.get("session_id", "default"),- workflow_id=workflow_id,- outputs=data.get("outputs", {}), # Upstream node outputs for data flow- )-- if result.get("success"):- success_payload = {**(result.get("result") or {}), "execution_id": execution_id}- await broadcaster.update_node_status(node_id, "success", success_payload, workflow_id=workflow_id)- await broadcaster.update_node_output(node_id, success_payload, workflow_id=workflow_id)- elif result.get("error") == "Cancelled by user":- # Cancelled trigger nodes go back to idle, not error- await broadcaster.update_node_status(- node_id, "idle", {"message": "Cancelled", "execution_id": execution_id}, workflow_id=workflow_id,+ # Mark this workflow active so the toolbar Start->Stop reflects ad-hoc runs.+ # finally: ensures the counter rolls back even on crash.+ await broadcaster.workflow_run_started(workflow_id)+ result: Dict[str, Any]+ try:+ result = await workflow_service.execute_node(+ node_id=node_id, node_type=node_type,+ parameters=data.get("parameters", {}),+ nodes=data.get("nodes", []), edges=data.get("edges", []),+ session_id=data.get("session_id", "default"),+ workflow_id=workflow_id,+ outputs=data.get("outputs", {}), # Upstream node outputs for data flow)- else:++ if result.get("success"):+ success_payload = {**(result.get("result") or {}), "execution_id": execution_id}+ await broadcaster.update_node_status(node_id, "success", success_payload, workflow_id=workflow_id)+ await broadcaster.update_node_output(node_id, success_payload, workflow_id=workflow_id)+ elif result.get("error") == "Cancelled by user":+ # Cancelled trigger nodes go back to idle, not error+ await broadcaster.update_node_status(+ node_id, "idle", {"message": "Cancelled", "execution_id": execution_id}, workflow_id=workflow_id,+ )+ else:+ await broadcaster.update_node_status(+ node_id, "error", {"error": result.get("error"), "execution_id": execution_id}, workflow_id=workflow_id,+ )+ except Exception:+ # Mark the node as errored so the UI doesn't keep glowing on crashawait broadcaster.update_node_status(- node_id, "error", {"error": result.get("error"), "execution_id": execution_id}, workflow_id=workflow_id,+ node_id, "error",+ {"error": "execution crashed", "execution_id": execution_id},+ workflow_id=workflow_id,)+ raise+ finally:+ await broadcaster.workflow_run_ended(workflow_id)# Explicitly pass through success status (don't let decorator default to True)ws_result = {@@ -439,12 +454,50 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dicreturn ws_result-@ws_handler("node_id")+@ws_handler()async def handle_cancel_execution(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:- """Cancel node execution."""+ """Cancel ad-hoc node execution(s).++ Two modes:+ * `node_id` — reset that single node to idle (legacy single-node cancel).+ * `workflow_id` only — reset every node in this workflow that's+ currently `executing` or `waiting`, and reset the active-run counter.+ Used by the toolbar Stop button when no deployment is active.++ Either argument is sufficient; if both are present the node-specific+ reset still happens and the workflow-level cleanup runs as well.+ """+ broadcaster = get_status_broadcaster()+ workflow_id = data.get("workflow_id")+ node_id = data.get("node_id")++ if node_id:+ await broadcaster.update_node_status(node_id, "idle", workflow_id=workflow_id)++ cleared = 0+ if workflow_id:+ # Reset run counter to zero (broadcasts executing=false + clears stuck nodes)+ async with broadcaster._workflow_active_lock:+ broadcaster._workflow_active_runs.pop(workflow_id, None)+ await broadcaster.update_workflow_status(executing=False, workflow_id=workflow_id)+ cleared = await broadcaster._clear_stuck_node_statuses(workflow_id)++ return {+ "node_id": node_id,+ "workflow_id": workflow_id,+ "cleared_nodes": cleared,+ "message": "Execution cancelled",+ }+++@ws_handler()+async def handle_get_workflow_status(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Return cached per-workflow execution status for resync (reconnect / workflow switch)."""broadcaster = get_status_broadcaster()- await broadcaster.update_node_status(data["node_id"], "idle")- return {"node_id": data["node_id"], "message": "Execution cancelled"}+ workflow_id = data.get("workflow_id")+ if not workflow_id:+ return {"success": False, "error": "workflow_id required"}+ return {"workflow_id": workflow_id, "data": broadcaster.get_workflow_status(workflow_id)}@ws_handler()@@ -726,8 +779,8 @@ async def handle_execute_workflow(data: Dict[str, Any], websocket: WebSocket) ->if not nodes:return {"success": False, "error": "No nodes provided"}- # Broadcast workflow starting status- await broadcaster.update_workflow_status(executing=True, current_node=None, progress=0)+ # Mark this workflow active so the toolbar Start->Stop reflects whole-workflow runs+ await broadcaster.workflow_run_started(workflow_id)# Create status callback with workflow_id for per-workflow scoping (n8n pattern)async def status_callback(node_id: str, status: str, node_data: Optional[Dict] = None):@@ -736,23 +789,25 @@ async def handle_execute_workflow(data: Dict[str, Any], websocket: WebSocket) ->position = node_data.get("position", 0) if node_data else 0total = node_data.get("total", 1) if node_data else 1progress = int((position / total) * 100) if total > 0 else 0- await broadcaster.update_workflow_status(executing=True, current_node=node_id, progress=progress)-- # Execute the workflow with workflow_id for per-workflow status scoping- result = await workflow_service.execute_workflow(- nodes=nodes,- edges=edges,- session_id=session_id,- status_callback=status_callback,- workflow_id=workflow_id,- )+ await broadcaster.update_workflow_status(+ executing=True,+ current_node=node_id,+ progress=progress,+ workflow_id=workflow_id,+ )- # Broadcast workflow completed status- await broadcaster.update_workflow_status(- executing=False,- current_node=None,- progress=100 if result.get("success") else 0- )+ result: Dict[str, Any]+ try:+ result = await workflow_service.execute_workflow(+ nodes=nodes,+ edges=edges,+ session_id=session_id,+ status_callback=status_callback,+ workflow_id=workflow_id,+ )+ finally:+ # Always release the active-run counter so the button never gets stuck+ await broadcaster.workflow_run_ended(workflow_id)return {"success": result.get("success", False),@@ -1116,19 +1171,27 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->workflow_id = data.get("workflow_id") # Per-workflow isolation for tool node glowingawait broadcaster.update_node_status(node_id, "executing", workflow_id=workflow_id)- result = await workflow_service.execute_node(- node_id=node_id, node_type=node_type,- parameters=data.get("parameters", {}),- nodes=data.get("nodes", []), edges=data.get("edges", []),- session_id=data.get("session_id", "default"),- workflow_id=workflow_id,- )+ await broadcaster.workflow_run_started(workflow_id)+ result: Dict[str, Any]+ try:+ result = await workflow_service.execute_node(+ node_id=node_id, node_type=node_type,+ parameters=data.get("parameters", {}),+ nodes=data.get("nodes", []), edges=data.get("edges", []),+ session_id=data.get("session_id", "default"),+ workflow_id=workflow_id,+ )- if result.get("success"):- await broadcaster.update_node_status(node_id, "success", result.get("result"), workflow_id=workflow_id)- await broadcaster.update_node_output(node_id, result.get("result"), workflow_id=workflow_id)- else:- await broadcaster.update_node_status(node_id, "error", {"error": result.get("error")}, workflow_id=workflow_id)+ if result.get("success"):+ await broadcaster.update_node_status(node_id, "success", result.get("result"), workflow_id=workflow_id)+ await broadcaster.update_node_output(node_id, result.get("result"), workflow_id=workflow_id)+ else:+ await broadcaster.update_node_status(node_id, "error", {"error": result.get("error")}, workflow_id=workflow_id)+ except Exception:+ await broadcaster.update_node_status(node_id, "error", {"error": "execution crashed"}, workflow_id=workflow_id)+ raise+ finally:+ await broadcaster.workflow_run_ended(workflow_id)return {"success": result.get("success", False), "node_id": node_id, "result": result.get("result"), "error": result.get("error"),"execution_time": result.get("execution_time"), "timestamp": time.time()}@@ -3297,6 +3360,7 @@ MESSAGE_HANDLERS: Dict[str, MessageHandler] = {"execute_node": handle_execute_node,"execute_workflow": handle_execute_workflow,"cancel_execution": handle_cancel_execution,+ "get_workflow_status": handle_get_workflow_status,"get_node_output": handle_get_node_output,"clear_node_output": handle_clear_node_output,diff --git a/server/services/status_broadcaster.py b/server/services/status_broadcaster.pyindex 5ebd6daf..3972e20a 100644--- a/server/services/status_broadcaster.py+++ b/server/services/status_broadcaster.py@@ -20,6 +20,17 @@ class StatusBroadcaster:self._connections: Set[WebSocket] = set()self._lock = asyncio.Lock()+ # Per-workflow active-run counter. Counts every concurrent+ # execution path (ad-hoc node run, whole-workflow run, deployed+ # trigger spawn, ...). The counter going 0->1 emits a+ # `workflow_status` broadcast with executing=true; 1->0 emits+ # executing=false. Prevents flapping while multiple runs overlap.+ self._workflow_active_runs: Dict[str, int] = {}+ self._workflow_active_lock = asyncio.Lock()++ # Per-workflow status cache for snapshot/resync+ self._workflow_statuses: Dict[str, Dict[str, Any]] = {}+# Current state for all status typesself._status: Dict[str, Any] = {"android": {@@ -599,20 +610,121 @@ class StatusBroadcaster:self,executing: bool,current_node: Optional[str] = None,- progress: Optional[float] = None+ progress: Optional[float] = None,+ workflow_id: Optional[str] = None,):- """Update workflow execution status and broadcast."""+ """Update workflow execution status and broadcast.++ When `workflow_id` is provided the broadcast is scoped per-workflow+ so the frontend can drive its per-workflow Start/Stop button. The+ legacy global slot is kept up to date for backward compatibility.+ """+ payload = {+ "executing": executing,+ "current_node": current_node,+ "progress": progress,+ "workflow_id": workflow_id,+ }+ # Update legacy global slotself._status["workflow"] = {"executing": executing,"current_node": current_node,- "progress": progress+ "progress": progress,}+ # Update per-workflow cache for snapshot/resync+ if workflow_id:+ self._workflow_statuses[workflow_id] = {+ "executing": executing,+ "current_node": current_node,+ "progress": progress,+ }await self.broadcast({"type": "workflow_status",- "data": self._status["workflow"]+ "workflow_id": workflow_id,+ "data": payload,})+ async def workflow_run_started(self, workflow_id: Optional[str]) -> bool:+ """Mark a new active run for `workflow_id`.++ Returns True if this transition broadcast `executing=True` (i.e.+ the counter went 0->1). Subsequent overlapping runs return False+ but still increment the counter so the matching `workflow_run_ended`+ decrements properly.+ """+ if not workflow_id:+ return False+ async with self._workflow_active_lock:+ prev = self._workflow_active_runs.get(workflow_id, 0)+ self._workflow_active_runs[workflow_id] = prev + 1+ went_active = prev == 0+ if went_active:+ await self.update_workflow_status(+ executing=True, workflow_id=workflow_id,+ )+ return went_active++ async def workflow_run_ended(+ self,+ workflow_id: Optional[str],+ clear_stuck_nodes: bool = True,+ ) -> bool:+ """Mark an active run finished for `workflow_id`.++ Returns True if this transition broadcast `executing=False` (i.e.+ the counter went 1->0). When the counter reaches zero and+ `clear_stuck_nodes` is set, any node currently marked+ `executing`/`waiting` for this workflow is reset to `idle` --+ protects the UI from glow leaks on crash paths.+ """+ if not workflow_id:+ return False+ async with self._workflow_active_lock:+ prev = self._workflow_active_runs.get(workflow_id, 0)+ new_count = max(0, prev - 1)+ if new_count == 0:+ self._workflow_active_runs.pop(workflow_id, None)+ else:+ self._workflow_active_runs[workflow_id] = new_count+ went_idle = prev > 0 and new_count == 0+ if went_idle:+ await self.update_workflow_status(+ executing=False, workflow_id=workflow_id,+ )+ if clear_stuck_nodes:+ await self._clear_stuck_node_statuses(workflow_id)+ return went_idle++ async def _clear_stuck_node_statuses(self, workflow_id: str) -> int:+ """Reset any nodes still marked executing/waiting for this workflow."""+ stuck = [+ (nid, info) for nid, info in self._status["nodes"].items()+ if info.get("workflow_id") == workflow_id+ and info.get("status") in ("executing", "waiting")+ ]+ for node_id, _info in stuck:+ try:+ await self.clear_node_status(node_id)+ except Exception as e:+ logger.warning(+ "[StatusBroadcaster] Failed to clear stuck node %s: %s",+ node_id, e,+ )+ return len(stuck)++ def get_workflow_status(self, workflow_id: str) -> Dict[str, Any]:+ """Return cached per-workflow execution status (for resync)."""+ cached = self._workflow_statuses.get(workflow_id)+ if cached:+ return {**cached, "workflow_id": workflow_id}+ return {+ "executing": False,+ "current_node": None,+ "progress": None,+ "workflow_id": workflow_id,+ }+async def update_deployment_status(self,is_running: bool,