Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: skip block description LLM call on subsequent for-loop iterations (#5428)
pedrohsdb7bd7598c0f80
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 93513ea76..f0168143c 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -544,7 +544,13 @@ class Block(BaseModel, abc.ABC):workflow_run_block_id = workflow_run_block.workflow_run_block_id# generate the description for the workflow run block asynchronously- asyncio.create_task(self._generate_workflow_run_block_description(workflow_run_block_id, organization_id))+ # Skip for subsequent for-loop iterations (current_index > 0) — the block+ # definition is identical across iterations and each iteration gets a fresh+ # model_copy(deep=True), so instance-level caching doesn't survive.+ if current_index is None or current_index == 0:+ asyncio.create_task(+ self._generate_workflow_run_block_description(workflow_run_block_id, organization_id)+ )# create a screenshotbrowser_state = app.BROWSER_MANAGER.get_for_workflow_run(workflow_run_id)diff --git a/tests/unit/test_block_description_caching.py b/tests/unit/test_block_description_caching.pynew file mode 100644index 000000000..53b17318b--- /dev/null+++ b/tests/unit/test_block_description_caching.py@@ -0,0 +1,124 @@+"""Tests for skipping block description LLM calls on for-loop iterations.++Validates that execute_safe only dispatches description generation when+current_index is None (not in a loop) or 0 (first iteration), and skips+it for current_index > 0 (subsequent iterations).+"""++from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from skyvern.forge.sdk.workflow.models.block import Block, TaskBlock+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter+from skyvern.schemas.workflows import BlockResult, BlockStatus+++def _make_block() -> TaskBlock:+ now = datetime.now(UTC)+ return TaskBlock(+ label="test_block",+ output_parameter=OutputParameter(+ output_parameter_id="op_test",+ key="test_output",+ workflow_id="wf_test",+ created_at=now,+ modified_at=now,+ ),+ )+++def _mock_workflow_run_block() -> MagicMock:+ wrb = MagicMock()+ wrb.workflow_run_block_id = "wrb_test"+ return wrb+++def _block_result() -> BlockResult:+ now = datetime.now(UTC)+ return BlockResult(+ success=True,+ output_parameter=OutputParameter(+ output_parameter_id="op_result",+ key="result",+ workflow_id="wf_test",+ created_at=now,+ modified_at=now,+ ),+ status=BlockStatus.completed,+ )+++def _setup_mocks(mock_app: MagicMock) -> None:+ """Set up the common mocks needed by execute_safe."""+ mock_app.DATABASE.observer.create_workflow_run_block = AsyncMock(return_value=_mock_workflow_run_block())+ mock_app.DATABASE.observer.update_workflow_run_block = AsyncMock()+ mock_app.BROWSER_MANAGER.get_for_workflow_run.return_value = None+++class TestDescriptionSkippedOnLoopIterations:+ @pytest.mark.asyncio+ async def test_generates_description_when_not_in_loop(self) -> None:+ """current_index=None means the block is not inside a for-loop."""+ block = _make_block()++ with (+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,+ patch.object(Block, "execute", new_callable=AsyncMock, return_value=_block_result()),+ patch.object(Block, "_generate_workflow_run_block_description", new_callable=AsyncMock) as mock_gen_desc,+ ):+ _setup_mocks(mock_app)++ await block.execute_safe(workflow_run_id="wr_1", current_index=None)++ mock_gen_desc.assert_called_once()++ @pytest.mark.asyncio+ async def test_generates_description_on_first_iteration(self) -> None:+ """current_index=0 is the first loop iteration — should still generate."""+ block = _make_block()++ with (+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,+ patch.object(Block, "execute", new_callable=AsyncMock, return_value=_block_result()),+ patch.object(Block, "_generate_workflow_run_block_description", new_callable=AsyncMock) as mock_gen_desc,+ ):+ _setup_mocks(mock_app)++ await block.execute_safe(workflow_run_id="wr_1", current_index=0)++ mock_gen_desc.assert_called_once()++ @pytest.mark.asyncio+ async def test_skips_description_on_subsequent_iterations(self) -> None:+ """current_index>0 should skip description generation entirely."""+ block = _make_block()++ with (+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,+ patch.object(Block, "execute", new_callable=AsyncMock, return_value=_block_result()),+ patch.object(Block, "_generate_workflow_run_block_description", new_callable=AsyncMock) as mock_gen_desc,+ ):+ _setup_mocks(mock_app)++ await block.execute_safe(workflow_run_id="wr_1", current_index=5)++ mock_gen_desc.assert_not_called()++ @pytest.mark.asyncio+ async def test_description_called_once_across_multiple_iterations(self) -> None:+ """Simulates a 5-iteration loop — description generated only for index 0."""+ block = _make_block()++ with (+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,+ patch.object(Block, "execute", new_callable=AsyncMock, return_value=_block_result()),+ patch.object(Block, "_generate_workflow_run_block_description", new_callable=AsyncMock) as mock_gen_desc,+ ):+ _setup_mocks(mock_app)++ for i in range(5):+ await block.execute_safe(workflow_run_id="wr_1", current_index=i)++ assert mock_gen_desc.call_count == 1