Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Fix infinite script regeneration loop for adaptive caching workflows (#4956)
pedrohsdb99d0141116ff
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex f33f50278..f183c81bb 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -4327,11 +4327,23 @@ class WorkflowService:if cached_block_labels != should_cache_block_labels:missing_labels = should_cache_block_labels - cached_block_labelsif missing_labels and not has_conditionals:- # Only add missing labels for workflows WITHOUT conditionals.- # For workflows WITH conditionals, missing labels are expected (unexecuted branches).- blocks_to_update.update(missing_labels)- # Always rebuild the orchestrator if the definition changed- blocks_to_update.add(settings.WORKFLOW_START_BLOCK_LABEL)+ # Only add missing labels that actually executed in this run.+ # Unexecuted missing blocks have no action data and can't be generated —+ # adding them causes an infinite regeneration loop when runs terminate early.+ executable_missing = missing_labels & blocks_to_update+ if executable_missing:+ blocks_to_update.add(settings.WORKFLOW_START_BLOCK_LABEL)+ else:+ # All missing blocks are unexecuted — don't regenerate+ blocks_to_update -= missing_labels # no-op but defensive+ if missing_labels - executable_missing:+ LOG.info(+ "Skipping unexecuted missing labels to avoid regeneration loop",+ workflow_id=workflow.workflow_id,+ workflow_run_id=workflow_run.workflow_run_id,+ skipped_labels=list(missing_labels - executable_missing),+ executed_labels=list(executable_missing),+ )elif missing_labels and has_conditionals:LOG.debug("Skipping regeneration for missing labels in workflow with conditionals",diff --git a/tests/unit/test_conditional_script_caching.py b/tests/unit/test_conditional_script_caching.pyindex 005742b96..7922ab5f5 100644--- a/tests/unit/test_conditional_script_caching.py+++ b/tests/unit/test_conditional_script_caching.py@@ -809,3 +809,125 @@ class TestRegenerationLoopPrevention:# (execution tracking only adds blocks that DON'T have cached code)should_regenerate_run3 = bool(blocks_to_update_run3)assert should_regenerate_run3 is False, "No regeneration needed on Run 3 - the loop is broken"+++class TestAdaptiveCachingRegenerationLoopPrevention:+ """+ Tests for regeneration loop prevention in NON-conditional workflows.++ The loop occurs when a workflow consistently terminates early (e.g., first+ navigation block hits a 404 or Cloudflare block):+ 1. Run terminates early → only first block executes → script generated with only that block+ 2. Next run: missing_labels check detects blocks from the workflow definition that aren't cached+ 3. Those missing labels are added to blocks_to_update, triggering regeneration+ 4. But transform_workflow_run_to_code_gen_input skips blocks that didn't execute (no run_block data)+ 5. generate_workflow_script_python_code can only generate code for blocks with action data+ 6. Result: regeneration produces the same incomplete script → infinite loop++ Fix: Only add missing labels that also appear in blocks_to_update (i.e., blocks that+ actually executed in the current run). Missing blocks that didn't execute can't be+ generated anyway.+ """++ def test_missing_labels_only_added_if_executed(self) -> None:+ """+ Missing labels NOT in blocks_to_update (unexecuted) should be skipped.++ Simulates a 4-block workflow where only block_1 executes — the other 3+ missing blocks should NOT trigger regeneration.+ """+ should_cache_block_labels = {"block_1", "block_2", "block_3", "block_4", "__start_block__"}+ cached_block_labels = {"block_1", "__start_block__"}++ missing_labels = should_cache_block_labels - cached_block_labels+ assert missing_labels == {"block_2", "block_3", "block_4"}++ has_conditionals = False+ # blocks_to_update comes from execution tracking — only block_1 executed+ blocks_to_update: set[str] = {"block_1"}++ if missing_labels and not has_conditionals:+ executable_missing = missing_labels & blocks_to_update+ if executable_missing:+ blocks_to_update.add("__start_block__")+ else:+ blocks_to_update -= missing_labels++ # None of the missing blocks executed, so they should NOT be in blocks_to_update+ assert "block_2" not in blocks_to_update+ assert "block_3" not in blocks_to_update+ assert "block_4" not in blocks_to_update+ # block_1 should still be there (it executed)+ assert "block_1" in blocks_to_update+ # __start_block__ should NOT be added (no executable missing blocks)+ assert "__start_block__" not in blocks_to_update++ def test_executed_missing_labels_still_trigger_regeneration(self) -> None:+ """+ Missing labels that DID execute should still be added to blocks_to_update+ and trigger regeneration with the start block.+ """+ should_cache_block_labels = {"block_1", "block_2", "block_3", "__start_block__"}+ cached_block_labels = {"block_1", "__start_block__"}++ missing_labels = should_cache_block_labels - cached_block_labels+ assert missing_labels == {"block_2", "block_3"}++ has_conditionals = False+ # This run executed block_1 and block_2, but not block_3+ blocks_to_update: set[str] = {"block_1", "block_2"}++ if missing_labels and not has_conditionals:+ executable_missing = missing_labels & blocks_to_update+ if executable_missing:+ blocks_to_update.add("__start_block__")+ else:+ blocks_to_update -= missing_labels++ # block_2 is both missing AND executed — it should stay in blocks_to_update+ assert "block_2" in blocks_to_update+ # __start_block__ should be added because there ARE executable missing blocks+ assert "__start_block__" in blocks_to_update+ # block_3 is missing but didn't execute — it should NOT be added+ assert "block_3" not in blocks_to_update++ def test_early_termination_no_regeneration(self) -> None:+ """+ Simulates an early-termination scenario: a 4-block workflow where runs consistently+ terminate after the first block (e.g., 404 or Cloudflare block).++ Without the fix: every run detects 3 missing blocks → triggers regeneration+ → produces same incomplete script → infinite loop (88 regenerations in 2 hours).++ With the fix: unexecuted missing blocks are skipped → no regeneration → loop broken.+ """+ # Workflow has 4 blocks: nav_1, nav_2, extract_1, extract_2+ should_cache_block_labels = {"nav_1", "nav_2", "extract_1", "extract_2", "__start_block__"}++ # After first generation: only nav_1 was cached (workflow terminated early)+ cached_block_labels = {"nav_1", "__start_block__"}++ # Simulate multiple consecutive runs that all terminate after nav_1+ for run_number in range(1, 6):+ missing_labels = should_cache_block_labels - cached_block_labels+ assert missing_labels == {"nav_2", "extract_1", "extract_2"}, f"Run {run_number}: wrong missing labels"++ has_conditionals = False+ # Only nav_1 executed this run (early termination)+ blocks_to_update: set[str] = set() # nav_1 already cached, so not in blocks_to_update++ if missing_labels and not has_conditionals:+ executable_missing = missing_labels & blocks_to_update+ if executable_missing:+ blocks_to_update.add("__start_block__")+ else:+ blocks_to_update -= missing_labels++ should_regenerate = bool(blocks_to_update)+ assert should_regenerate is False, (+ f"Run {run_number}: should NOT trigger regeneration — "+ f"missing blocks didn't execute and can't be generated"+ )++ # cached_block_labels stays the same — no new blocks were cached+ assert cached_block_labels == {"nav_1", "__start_block__"}