diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py index 46379d833b..62298e1519 100644 --- a/skyvern/forge/sdk/workflow/service.py +++ b/skyvern/forge/sdk/workflow/service.py @@ -2472,6 +2472,14 @@ async def _execute_workflow_script( # Mark workflow run as completed workflow_run = await self.mark_workflow_run_as_completed(workflow_run_id=workflow_run.workflow_run_id) + # Clean up workflow to persist video data and other artifacts + await self.clean_up_workflow( + workflow=workflow, + workflow_run=workflow_run, + api_key=api_key, + browser_session_id=browser_session_id, + ) + LOG.info( "Successfully executed workflow script", workflow_run_id=workflow_run.workflow_run_id, @@ -2495,6 +2503,22 @@ async def _execute_workflow_script( workflow_run_id=workflow_run.workflow_run_id, failure_reason=failure_reason ) + # Clean up workflow to persist video data and other artifacts even on failure + try: + await self.clean_up_workflow( + workflow=workflow, + workflow_run=workflow_run, + api_key=api_key, + browser_session_id=browser_session_id, + ) + except Exception as cleanup_error: + LOG.error( + "Failed to clean up workflow after script execution failure", + workflow_run_id=workflow_run.workflow_run_id, + cleanup_error=str(cleanup_error), + exc_info=True, + ) + return workflow_run async def generate_script_if_needed(