Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(workflow): reconcile Start button state on WS reconnect via CloudEvents snapshot
trohitgde8df87e717c
diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsxindex 510d30cb..dee0c5e4 100644--- a/client/src/contexts/WebSocketContext.tsx+++ b/client/src/contexts/WebSocketContext.tsx@@ -962,6 +962,57 @@ export const WebSocketProvider: React.FC<{ children: React.ReactNode }> = ({ chibreak;}+ case 'deployment_snapshot': {+ // CloudEvents v1.0 envelope from broadcaster._send_deployment_snapshot.+ // Pushed once per WS connect so the FE can reconcile its stale+ // `deploymentStatus.isRunning=true` after a backend restart that+ // wiped DeploymentManager._deployments. Empty list is meaningful+ // and forces a reset — the prior bug was: backend restart wiped+ // the deployment dict, FE never got a `stopped` broadcast (because+ // there was nothing to broadcast about), so the Start button+ // stayed showing "Stop" forever.+ const envelope = data as WorkflowEvent<{+ running_workflow_ids?: string[];+ }> | undefined;+ const runningIds = envelope?.data?.running_workflow_ids ?? [];+ const runningSet = new Set(runningIds);+ const store = useAppStore.getState();+ const currentId = store.currentWorkflow?.id;++ // Reconcile per-workflow execution state in workflowUIStates.+ // Anything currently flagged isExecuting=true that isn't in+ // the snapshot's running set gets cleared (the load-bearing+ // reset for stale state after backend restart). Anything in+ // the snapshot gets flagged true.+ const existingStates = store.workflowUIStates ?? {};+ for (const [wid, ui] of Object.entries(existingStates)) {+ if (ui?.isExecuting && !runningSet.has(wid)) {+ store.setWorkflowExecuting(wid, false);+ }+ }+ for (const wid of runningIds) {+ store.setWorkflowExecuting(wid, true);+ }++ // Reconcile the toolbar `deploymentStatus` for the active workflow+ setDeploymentStatus(prev => {+ const next: DeploymentStatus = { ...prev };+ if (currentId && runningSet.has(currentId)) {+ next.isRunning = true;+ next.status = 'running';+ next.workflow_id = currentId;+ } else if (currentId && !runningSet.has(currentId) && prev.workflow_id === currentId) {+ // Previously thought current workflow was deployed; backend says no.+ next.isRunning = false;+ next.status = 'stopped';+ next.workflow_id = null;+ next.activeRuns = 0;+ }+ return next;+ });+ break;+ }+case 'deployment_status':// Handle deployment status updates (event-driven, no iterations)// Per-workflow scoping (n8n pattern): Only apply updates for current workflowdiff --git a/server/services/events/envelope.py b/server/services/events/envelope.pyindex 1e769ea5..082299b7 100644--- a/server/services/events/envelope.py+++ b/server/services/events/envelope.py@@ -219,6 +219,33 @@ class WorkflowEvent(BaseModel):},)+ @classmethod+ def deployment_snapshot(+ cls,+ running_workflow_ids: list[str],+ ) -> "WorkflowEvent":+ """Push-on-connect snapshot of every currently-running deployment.++ Emitted by ``broadcaster.broadcast_deployment_snapshot()`` to a+ single WebSocket target right after ``initial_status``. Lets the+ FE reconcile its local ``deploymentStatus`` / ``runningWorkflows``+ cache against the backend's source of truth on every (re)connect,+ instead of carrying stale "isRunning=true" forward through a+ backend restart that wiped the in-memory deployment dict.++ Distinct from ``workflow_lifecycle("deployment.started")`` —+ that fires when a deployment STARTS (one-shot edge event).+ ``deployment.snapshot`` is an idempotent state dump tied to+ client connect, not to a state transition. Empty list is+ meaningful: "no deployments are running, drop your stale+ local state."+ """+ return cls(+ source="machinaos://services/workflow",+ type="workflow.deployment.snapshot",+ data={"running_workflow_ids": list(running_workflow_ids)},+ )+@classmethoddef task_completed(cls,diff --git a/server/services/status_broadcaster.py b/server/services/status_broadcaster.pyindex d8897778..48c1f661 100644--- a/server/services/status_broadcaster.py+++ b/server/services/status_broadcaster.py@@ -120,6 +120,46 @@ class StatusBroadcaster:except Exception as e:logger.error(f"[StatusBroadcaster] Failed to send initial status: {e}")+ # Reconcile snapshot — every fresh client gets the+ # currently-running-deployments truth so a stale FE+ # `deploymentStatus.isRunning=true` (carried forward through a+ # backend restart that wiped DeploymentManager._deployments)+ # gets cleared. CloudEvents-shaped envelope; empty list is+ # meaningful and triggers FE-side reset.+ try:+ await self._send_deployment_snapshot(websocket)+ except Exception as e:+ logger.error(f"[StatusBroadcaster] Failed to send deployment snapshot: {e}")++ async def _send_deployment_snapshot(self, websocket: WebSocket) -> None:+ """Build + send a CloudEvents deployment_snapshot to one client.++ Lazy container resolution because the broadcaster is constructed+ before WorkflowService and we do not want a circular dependency+ at module-load time.+ """+ try:+ from core.container import container+ from services.events import WorkflowEvent++ workflow_service = container.workflow_service()+ dm = workflow_service._get_deployment_manager()+ running_ids = dm.get_deployed_workflows()+ except Exception as e:+ # Backend startup race or DI not wired yet -- skip the snapshot+ # rather than fail the connect entirely. The empty-list path+ # would still be safer than crashing here.+ logger.debug(+ f"[StatusBroadcaster] DeploymentManager unavailable for snapshot: {e}",+ )+ return++ event = WorkflowEvent.deployment_snapshot(running_ids)+ await websocket.send_json({+ "type": "deployment_snapshot",+ "data": event.model_dump(mode="json"),+ })+async def disconnect(self, websocket: WebSocket):"""Remove a WebSocket connection."""async with self._lock: