Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Fix for-loop adaptive caching: output aggregation, cached function bodies, and template resolution (#4931)
pedrohsdbfab33477587e
diff --git a/skyvern/core/script_generations/generate_script.py b/skyvern/core/script_generations/generate_script.pyindex 9c40d1191..0a7e4b4d4 100644--- a/skyvern/core/script_generations/generate_script.py+++ b/skyvern/core/script_generations/generate_script.py@@ -2786,6 +2786,75 @@ async def generate_workflow_script_python_code(# at module level ("async for outside async function"). The for-loop code is# already correctly inlined inside run_workflow() via _build_block_statement().+ # Generate cached function bodies for for_loop inner blocks.+ # Inner blocks (e.g. extraction inside a loop) are nested in loop_blocks and+ # are NOT in the top-level blocks list, so they need separate processing here.+ # This follows the same pattern as task_v2 child block handling (lines 2704-2714).+ for loop_block in for_loop_block.get("loop_blocks", []):+ if loop_block.get("block_type") not in SCRIPT_TASK_BLOCKS:+ continue++ inner_label = (+ loop_block.get("label") or loop_block.get("title") or f"block_{loop_block.get('workflow_run_block_id')}"+ )++ # Check if already cached (for progressive caching)+ cached_inner = cached_blocks.get(inner_label)+ use_inner_cached = cached_inner is not None and inner_label not in updated_block_labels++ if use_inner_cached:+ assert cached_inner is not None+ inner_block_code = cached_inner.code+ inner_run_signature = cached_inner.run_signature+ inner_wrbi = cached_inner.workflow_run_block_id+ inner_wri = cached_inner.workflow_run_id+ else:+ inner_actions = actions_by_task.get(loop_block.get("task_id", ""), [])+ if not inner_actions:+ continue # No actions from agent run = can't generate cached function++ inner_fn_def = _build_block_fn(+ loop_block,+ inner_actions,+ value_to_param=value_to_param,+ use_semantic_selectors=use_semantic_selectors,+ )+ inner_block_code = cst.Module(body=[inner_fn_def]).code++ inner_stmt = _build_block_statement(loop_block, value_to_param=None)+ inner_run_signature = cst.Module(body=[inner_stmt]).code.strip()++ inner_wrbi = loop_block.get("workflow_run_block_id")+ inner_wri = loop_block.get("workflow_run_id") or run_id++ # Create script_block entry for preservation across regenerations+ if script_id and script_revision_id and organization_id:+ try:+ inner_input_fields = _collect_block_input_fields(loop_block, actions_by_task)+ if not inner_input_fields and cached_inner and cached_inner.input_fields:+ inner_input_fields = cached_inner.input_fields+ await create_or_update_script_block(+ block_code=inner_block_code,+ script_revision_id=script_revision_id,+ script_id=script_id,+ organization_id=organization_id,+ block_label=inner_label,+ update=pending,+ run_signature=inner_run_signature,+ workflow_run_id=inner_wri,+ workflow_run_block_id=inner_wrbi,+ input_fields=inner_input_fields,+ )+ except Exception as e:+ LOG.error(+ "Failed to create for_loop inner block script block",+ error=str(e),+ block_label=inner_label,+ exc_info=True,+ )++ append_block_code(inner_block_code)+# --- agent-required blocks (adaptive caching) -----------------------# Structural blocks (conditional, text_prompt, wait) can't be code-generated# initially. Create script_block entries with requires_agent=True so the runtime@@ -2852,6 +2921,12 @@ async def generate_workflow_script_python_code(for flb in for_loop_blocks:label = flb.get("label") or f"for_loop_{flb.get('workflow_run_block_id')}"processed_labels.add(label)+ # Also track inner block labels to prevent duplication in the+ # "preserve unexecuted branch" section below+ for lb in flb.get("loop_blocks", []):+ inner_lbl = lb.get("label") or lb.get("title")+ if inner_lbl:+ processed_labels.add(inner_lbl)if adaptive_caching:for arb in [b for b in blocks if b["block_type"] in _AGENT_REQUIRED_BLOCK_TYPES]:arb_label = arb.get("label") or f"{arb['block_type']}_{arb.get('workflow_run_block_id')}"diff --git a/skyvern/core/script_generations/real_skyvern_page_ai.py b/skyvern/core/script_generations/real_skyvern_page_ai.pyindex 1a1663ce5..de2a41bb8 100644--- a/skyvern/core/script_generations/real_skyvern_page_ai.py+++ b/skyvern/core/script_generations/real_skyvern_page_ai.py@@ -129,6 +129,14 @@ def render_template(template: str, data: dict[str, Any] | None = None) -> str:if template in template_data:return template_data[template]+ # Inject for_loop metadata (current_value, current_index, current_item) so+ # that cached function bodies inside for_loops can resolve {{ current_value }}+ # in page.goto() and other template-rendered calls.+ if context and context.loop_metadata:+ for key in ("current_value", "current_index", "current_item"):+ if key in context.loop_metadata:+ template_data[key] = context.loop_metadata[key]+return jinja_template.render(template_data)diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.pyindex 5b624fd64..c8d554833 100644--- a/skyvern/services/script_service.py+++ b/skyvern/services/script_service.py@@ -699,6 +699,15 @@ async def _update_workflow_block(label,)+ # If executing inside a for_loop, collect this block's output for loop aggregation.+ # Guard: skip if this is the loop block's own _update_workflow_block call.+ if (+ context.loop_output_values is not None+ and context.parent_workflow_run_block_id+ and workflow_run_block_id != context.parent_workflow_run_block_id+ ):+ _append_to_loop_output(final_output, label)+except Exception as e:LOG.warning("Failed to update workflow block status",@@ -714,6 +723,20 @@ async def _run_cached_function(cached_fn: Callable) -> Any:return await cached_fn(page=run_context.page, context=run_context)+def _append_to_loop_output(output: Any, label: str | None = None) -> None:+ """If executing inside a for_loop, collect this block's output for loop aggregation."""+ context = skyvern_context.current()+ if not context or context.loop_output_values is None or context.loop_metadata is None:+ return+ context.loop_output_values.append(+ {+ "loop_value": context.loop_metadata.get("current_value"),+ "output_value": output,+ "label": label,+ }+ )++def _determine_action_ai_mode(action: Action,merged_value: str | None,@@ -1605,6 +1628,7 @@ async def run_task(organization_id=block_validation_output.organization_id,browser_session_id=block_validation_output.browser_session_id,)+ _append_to_loop_output(block_output.output_parameter_value, label)return block_output.output_parameter_value@@ -1936,6 +1960,7 @@ async def extract(organization_id=block_validation_output.organization_id,browser_session_id=block_validation_output.browser_session_id,)+ _append_to_loop_output(block_result.output_parameter_value, label)return block_result.output_parameter_value@@ -2039,6 +2064,7 @@ async def execute_validation(organization_id=block_validation_output.organization_id,browser_session_id=block_validation_output.browser_session_id,)+ _append_to_loop_output(result.output_parameter_value, label)return result@@ -2161,6 +2187,13 @@ def render_template(template: str, data: dict[str, Any] | None = None) -> str:template_data.update(workflow_run_context.values)if template in template_data:return template_data[template]+ # Inject for_loop metadata (current_value, current_index, current_item) so+ # that cached function bodies inside for_loops can resolve {{ current_value }}+ # in page.goto() and other template-rendered calls.+ if context.loop_metadata:+ for key in ("current_value", "current_index", "current_item"):+ if key in context.loop_metadata:+ template_data[key] = context.loop_metadata[key]return jinja_template.render(template_data)@@ -2258,6 +2291,7 @@ async def run_code(organization_id=block_validation_output.organization_id,browser_session_id=block_validation_output.browser_session_id,)+ _append_to_loop_output(block_result.output_parameter_value, label)return cast(dict[str, Any], block_result.output_parameter_value)@@ -2487,6 +2521,7 @@ async def prompt(organization_id=block_validation_output.organization_id,browser_session_id=block_validation_output.browser_session_id,)+ _append_to_loop_output(result.output_parameter_value, label)return result.output_parameter_valuediff --git a/tests/unit/test_forloop_script_generation.py b/tests/unit/test_forloop_script_generation.pyindex 8e248e60b..f22dc0a5c 100644--- a/tests/unit/test_forloop_script_generation.py+++ b/tests/unit/test_forloop_script_generation.py@@ -483,3 +483,318 @@ class TestForLoopScriptCompilation:# Verify the for-loop is present inside run_workflow (not at module level)assert "async for current_value in skyvern.loop" in resultassert "def run_workflow" in result+++class TestForLoopInnerBlockCachedFunctions:+ """Test that inner blocks inside for_loop get @skyvern.cached function bodies generated.++ This covers the bug where for_loop inner blocks (e.g. extraction inside a loop) didn't+ get cached function bodies because they were nested in loop_blocks and not in the+ top-level blocks list. Without cached function bodies, code mode falls back to agent.+ """++ @pytest.mark.asyncio+ async def test_inner_extraction_block_gets_cached_function(self) -> None:+ """Verify that an extraction block inside a for_loop generates a @skyvern.cached function."""+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code++ blocks = [+ {+ "block_type": "for_loop",+ "label": "scrape_each_page",+ "loop_variable_reference": "{{ urls }}",+ "workflow_run_block_id": "wfrb_loop_1",+ "loop_blocks": [+ {+ "block_type": "extraction",+ "label": "extract_data",+ "data_extraction_goal": "Get page content",+ "task_id": "task_inner_1",+ "workflow_run_block_id": "wfrb_inner_1",+ "url": "https://example.com",+ },+ ],+ },+ ]++ actions_by_task = {+ "task_inner_1": [+ {+ "action_type": "extract",+ "action_id": "action_1",+ "xpath": "//div[@id='content']",+ "element_id": "elem_1",+ "text": None,+ "data_extraction_goal": "Get page content",+ },+ ],+ }++ workflow = {+ "workflow_id": "wf_test",+ "title": "Test ForLoop Inner Block",+ "workflow_definition": {"parameters": []},+ }++ mock_create_script_block = AsyncMock()++ with (+ patch(+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",+ new_callable=AsyncMock,+ return_value=("", {}),+ ),+ patch(+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",+ mock_create_script_block,+ ),+ ):+ result = await generate_workflow_script_python_code(+ file_name="test_forloop_inner.py",+ workflow_run_request={"workflow_id": "wpid_test"},+ workflow=workflow,+ blocks=blocks,+ actions_by_task=actions_by_task,+ script_id="script_123",+ script_revision_id="rev_123",+ organization_id="org_123",+ )++ # The generated code must compile without errors+ try:+ ast.parse(result)+ except SyntaxError as e:+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")++ # Verify the inner block's @skyvern.cached function is present+ assert "@skyvern.cached" in result+ assert "extract_data" in result++ # Verify create_or_update_script_block was called for both the for_loop+ # and the inner extraction block+ call_labels = [+ call.kwargs.get("block_label") or call.args[4] for call in mock_create_script_block.call_args_list+ ]+ assert "scrape_each_page" in call_labels, (+ f"for_loop block should have a script_block entry. Labels: {call_labels}"+ )+ assert "extract_data" in call_labels, (+ f"Inner extraction block should have a script_block entry. Labels: {call_labels}"+ )++ @pytest.mark.asyncio+ async def test_inner_block_skipped_without_actions(self) -> None:+ """Inner blocks with no actions should be skipped (no cached function generated)."""+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code++ blocks = [+ {+ "block_type": "for_loop",+ "label": "scrape_each_page",+ "loop_variable_reference": "{{ urls }}",+ "loop_blocks": [+ {+ "block_type": "extraction",+ "label": "extract_data",+ "data_extraction_goal": "Get page content",+ "task_id": "task_no_actions",+ },+ ],+ },+ ]++ workflow = {+ "workflow_id": "wf_test",+ "title": "Test ForLoop No Actions",+ "workflow_definition": {"parameters": []},+ }++ mock_create_script_block = AsyncMock()++ with (+ patch(+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",+ new_callable=AsyncMock,+ return_value=("", {}),+ ),+ patch(+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",+ mock_create_script_block,+ ),+ ):+ result = await generate_workflow_script_python_code(+ file_name="test_forloop_no_actions.py",+ workflow_run_request={"workflow_id": "wpid_test"},+ workflow=workflow,+ blocks=blocks,+ actions_by_task={}, # No actions+ script_id="script_123",+ script_revision_id="rev_123",+ organization_id="org_123",+ )++ # Should compile fine+ try:+ ast.parse(result)+ except SyntaxError as e:+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")++ # No @skyvern.cached should be present since inner block had no actions+ assert "@skyvern.cached" not in result++ # create_or_update_script_block should be called for the for_loop only,+ # NOT for the inner block+ call_labels = [+ call.kwargs.get("block_label") or call.args[4] for call in mock_create_script_block.call_args_list+ ]+ assert "scrape_each_page" in call_labels+ assert "extract_data" not in call_labels++ @pytest.mark.asyncio+ async def test_non_task_inner_blocks_are_skipped(self) -> None:+ """Inner blocks with block_type not in SCRIPT_TASK_BLOCKS should be skipped."""+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code++ blocks = [+ {+ "block_type": "for_loop",+ "label": "process_items",+ "loop_variable_reference": "{{ items }}",+ "loop_blocks": [+ {+ "block_type": "goto_url",+ "label": "go_to_page",+ "url": "https://example.com",+ },+ ],+ },+ ]++ workflow = {+ "workflow_id": "wf_test",+ "title": "Test ForLoop Non-Task Inner",+ "workflow_definition": {"parameters": []},+ }++ mock_create_script_block = AsyncMock()++ with (+ patch(+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",+ new_callable=AsyncMock,+ return_value=("", {}),+ ),+ patch(+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",+ mock_create_script_block,+ ),+ ):+ result = await generate_workflow_script_python_code(+ file_name="test_forloop_non_task.py",+ workflow_run_request={"workflow_id": "wpid_test"},+ workflow=workflow,+ blocks=blocks,+ actions_by_task={},+ script_id="script_123",+ script_revision_id="rev_123",+ organization_id="org_123",+ )++ try:+ ast.parse(result)+ except SyntaxError as e:+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")++ # No @skyvern.cached should be generated for non-task blocks+ assert "@skyvern.cached" not in result++ @pytest.mark.asyncio+ async def test_inner_block_labels_in_processed_labels(self) -> None:+ """Inner block labels should be tracked to avoid duplication in unexecuted-branch preservation."""+ from skyvern.core.script_generations.generate_script import generate_workflow_script_python_code++ blocks = [+ {+ "block_type": "for_loop",+ "label": "scrape_each_page",+ "loop_variable_reference": "{{ urls }}",+ "workflow_run_block_id": "wfrb_loop_1",+ "loop_blocks": [+ {+ "block_type": "extraction",+ "label": "extract_data",+ "data_extraction_goal": "Get page content",+ "task_id": "task_inner_1",+ "workflow_run_block_id": "wfrb_inner_1",+ },+ ],+ },+ ]++ actions_by_task = {+ "task_inner_1": [+ {+ "action_type": "extract",+ "action_id": "action_1",+ "xpath": "//div[@id='content']",+ "element_id": "elem_1",+ "text": None,+ "data_extraction_goal": "Get page content",+ },+ ],+ }++ workflow = {+ "workflow_id": "wf_test",+ "title": "Test ForLoop Labels",+ "workflow_definition": {"parameters": []},+ }++ # Mock a cached_block with the same label as the inner block to verify+ # it doesn't get duplicated in the "preserve unexecuted branch" section+ mock_cached_source = MagicMock()+ mock_cached_source.code = "@skyvern.cached(cache_key='extract_data')\nasync def extract_data_fn(): pass"+ mock_cached_source.run_signature = "await skyvern.extract(prompt='Get page content', label='extract_data')"+ mock_cached_source.workflow_run_id = "wr_old"+ mock_cached_source.workflow_run_block_id = "wfrb_old"+ mock_cached_source.input_fields = None++ mock_create_script_block = AsyncMock()++ with (+ patch(+ "skyvern.core.script_generations.generate_script.generate_workflow_parameters_schema",+ new_callable=AsyncMock,+ return_value=("", {}),+ ),+ patch(+ "skyvern.core.script_generations.generate_script.create_or_update_script_block",+ mock_create_script_block,+ ),+ ):+ result = await generate_workflow_script_python_code(+ file_name="test_forloop_labels.py",+ workflow_run_request={"workflow_id": "wpid_test"},+ workflow=workflow,+ blocks=blocks,+ actions_by_task=actions_by_task,+ script_id="script_123",+ script_revision_id="rev_123",+ organization_id="org_123",+ cached_blocks={"extract_data": mock_cached_source},+ )++ # Should compile fine+ try:+ ast.parse(result)+ except SyntaxError as e:+ pytest.fail(f"Generated script has SyntaxError: {e}\n\nGenerated code:\n{result}")++ # The inner block should appear only once in the generated code (not duplicated+ # by the "preserve unexecuted branch" section)+ occurrences = result.count("@skyvern.cached")+ # There should be exactly 1 cached function (the inner block), not 2+ assert occurrences == 1, (+ f"Expected 1 @skyvern.cached occurrence but found {occurrences}. "+ f"Inner block may have been duplicated.\n\nGenerated code:\n{result}"+ )