Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Revert Parallel Workflow Loops (SKY-8175, SKY-8176, SKY-8177, SKY-8180) (#5445)
Aaron Perez18ab8447c24f
diff --git a/skyvern/constants.py b/skyvern/constants.pyindex 434621b8a..b95033752 100644--- a/skyvern/constants.py+++ b/skyvern/constants.py@@ -1,20 +1,6 @@from enum import StrEnumfrom pathlib import Path-# Parallel loop iteration browser key separator-LOOP_ITERATION_SEPARATOR = "__iter_"---def loop_iteration_key(workflow_run_id: str, loop_idx: int) -> str:- """Build the cache key for a parallel loop iteration's browser state."""- return f"{workflow_run_id}{LOOP_ITERATION_SEPARATOR}{loop_idx}"---def is_loop_iteration_key(key: str) -> bool:- """Check whether a browser_session_id belongs to a parallel loop iteration."""- return LOOP_ITERATION_SEPARATOR in key--# This is the attribute name used to tag interactable elementsSKYVERN_ID_ATTR: str = "unique_id"SKYVERN_DIR = Path(__file__).parentdiff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex 2a84fabc7..4b1a1b80c 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -651,19 +651,6 @@ class AgentFunction:return cleanup_element_tree_func- async def check_parallel_loop_quota(self, organization_id: str, requested_concurrency: int) -> int:- """Check per-org quota for parallel loop iterations.-- Returns the number of parallel iterations allowed. OSS base returns- requested_concurrency unchanged (no enforcement). Cloud override- enforces org-level caps via Redis.- """- return requested_concurrency-- async def release_parallel_loop_quota(self, organization_id: str, count: int) -> None:- """Release parallel loop iteration slots. OSS base is a no-op."""- return-async def validate_code_block(self, organization_id: str | None = None) -> None:if not settings.ENABLE_CODE_BLOCK:raise DisabledBlockExecutionError("CodeBlock is disabled")diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.pyindex 139c6ded6..9c2e23f99 100644--- a/skyvern/forge/sdk/core/skyvern_context.py+++ b/skyvern/forge/sdk/core/skyvern_context.py@@ -1,5 +1,3 @@-import builtins-import dataclassesfrom contextvars import ContextVarfrom dataclasses import dataclass, fieldfrom typing import Any@@ -95,34 +93,6 @@ class SkyvernContext:# preventing repeated injection loops when the captcha solver succeeds but the page doesn't changeproactive_captcha_task_ids: set[str] = field(default_factory=set)- def create_iteration_copy(self, browser_session_id: str) -> "SkyvernContext":- """Create an isolated copy for a parallel loop iteration.-- Copies scalar fields by value and creates fresh instances of mutable- containers (dicts, lists, sets). Fields that hold non-copyable objects- (Playwright Frame/Page) are reset to empty defaults.- """- kwargs: dict[str, Any] = {}- for f in dataclasses.fields(self):- val = getattr(self, f.name)- if isinstance(val, dict):- # frame_index_map and magic_link_pages hold Playwright objects- # that cannot be safely copied — start fresh per iteration.- if f.name in ("frame_index_map", "magic_link_pages"):- kwargs[f.name] = {}- else:- kwargs[f.name] = dict(val)- elif isinstance(val, list):- kwargs[f.name] = list(val)- # builtins.set required because the module-level `set()` function- # below (line ~180) shadows the built-in inside this module.- elif isinstance(val, builtins.set):- kwargs[f.name] = builtins.set(val)- else:- kwargs[f.name] = val- kwargs["browser_session_id"] = browser_session_id- return SkyvernContext(**kwargs)-def __repr__(self) -> str:return f"SkyvernContext(request_id={self.request_id}, organization_id={self.organization_id}, task_id={self.task_id}, step_id={self.step_id}, workflow_id={self.workflow_id}, workflow_run_id={self.workflow_run_id}, task_v2_id={self.task_v2_id}, max_steps_override={self.max_steps_override}, run_id={self.run_id})"diff --git a/skyvern/forge/sdk/workflow/context_manager.py b/skyvern/forge/sdk/workflow/context_manager.pyindex 48701211e..945d90178 100644--- a/skyvern/forge/sdk/workflow/context_manager.py+++ b/skyvern/forge/sdk/workflow/context_manager.py@@ -1,5 +1,4 @@import copy-from contextvars import ContextVarfrom typing import TYPE_CHECKING, Any, Awaitable, Callable, Selfimport structlog@@ -226,50 +225,6 @@ class WorkflowRunContext:label = ""return self.blocks_metadata.get(label, BlockMetadata())- def create_iteration_snapshot(self, loop_idx: int) -> "WorkflowRunContext":- """Create a deep-copy snapshot of mutable state for a parallel loop iteration.-- The snapshot shares immutable/reference fields (aws_client, workflow, secrets,- parameters, organization_id) but gets its own copies of values and blocks_metadata- so concurrent iterations don't interfere with each other.- """- snapshot = WorkflowRunContext(- workflow_title=self.workflow_title,- workflow_id=self.workflow_id,- workflow_permanent_id=self.workflow_permanent_id,- workflow_run_id=self.workflow_run_id,- aws_client=self._aws_client,- workflow=self.workflow,- )- snapshot.organization_id = self.organization_id- snapshot.browser_session_id = self.browser_session_id- snapshot.include_secrets_in_templates = self.include_secrets_in_templates- snapshot.credential_totp_identifiers = self.credential_totp_identifiers- # Parameter definitions and secrets are read-only during iteration today,- # but shallow-copy the dicts so a future code path that mutates them- # inside a loop block can't silently leak across iterations.- snapshot.parameters = dict(self.parameters)- snapshot.secrets = dict(self.secrets)- # Deep-copy mutable state so iterations don't clobber each other- snapshot.values = copy.deepcopy(self.values)- snapshot.blocks_metadata = copy.deepcopy(self.blocks_metadata)- snapshot.workflow_run_outputs = copy.deepcopy(self.workflow_run_outputs)- return snapshot-- def merge_iteration_results(self, snapshots: list[tuple[int, "WorkflowRunContext"]]) -> None:- """Merge results from parallel iteration snapshots back into this context.-- Snapshots are applied in iteration-index order so that later iterations- overwrite earlier ones when keys collide (matching sequential semantics).- """- for _loop_idx, snapshot in sorted(snapshots, key=lambda t: t[0]):- # Merge values: iteration outputs are keyed by block label- self.values.update(snapshot.values)- # Merge block metadata- self.blocks_metadata.update(snapshot.blocks_metadata)- # Merge workflow run outputs- self.workflow_run_outputs.update(snapshot.workflow_run_outputs)-async def _should_include_secrets_in_templates(self) -> bool:"""Check if secrets should be included in template formatting based on experimentation provider.@@ -1270,22 +1225,6 @@ class WorkflowRunContext:self.values[parameter.key][key] = secret_id-# Per-task override for the active WorkflowRunContext, used by parallel loop-# iterations. When set, get_workflow_run_context() returns this snapshot-# instead of looking up the shared context in workflow_run_contexts. Because-# ContextVar state is copied per asyncio.create_task, each iteration sees its-# own snapshot, so child blocks resolving context via the classmethod get-# their isolated view without any call-signature changes.-_iteration_workflow_run_context: ContextVar["WorkflowRunContext | None"] = ContextVar(- "_iteration_workflow_run_context", default=None-)---def set_iteration_workflow_run_context(context: "WorkflowRunContext | None") -> None:- """Set the per-task workflow run context override for parallel loop iterations."""- _iteration_workflow_run_context.set(context)--class WorkflowContextManager:aws_client: AsyncAWSClientworkflow_run_contexts: dict[str, WorkflowRunContext]@@ -1343,14 +1282,6 @@ class WorkflowContextManager:return workflow_run_contextdef get_workflow_run_context(self, workflow_run_id: str) -> WorkflowRunContext:- # Parallel loop iterations install a per-task snapshot via the- # _iteration_workflow_run_context ContextVar. If present and matching- # this workflow_run_id, return the snapshot so child blocks resolving- # context via the classmethod see their isolated view instead of the- # globally shared context.- override = _iteration_workflow_run_context.get()- if override is not None and override.workflow_run_id == workflow_run_id:- return overrideself._validate_workflow_run_context(workflow_run_id)return self.workflow_run_contexts[workflow_run_id]diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 68f55e817..a1fa17c86 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -38,7 +38,6 @@ from skyvern.constants import (GET_DOWNLOADED_FILES_TIMEOUT,MAX_FILE_PARSE_INPUT_TOKENS,MAX_UPLOAD_FILE_COUNT,- loop_iteration_key,)from skyvern.exceptions import (AzureConfigurationError,@@ -82,11 +81,7 @@ from skyvern.forge.sdk.settings_manager import SettingsManagerfrom skyvern.forge.sdk.trace import tracedfrom skyvern.forge.sdk.utils.pdf_parser import extract_pdf_file, render_pdf_pages_as_images, validate_pdf_filefrom skyvern.forge.sdk.utils.sanitization import sanitize_postgres_text-from skyvern.forge.sdk.workflow.context_manager import (- BlockMetadata,- WorkflowRunContext,- set_iteration_workflow_run_context,-)+from skyvern.forge.sdk.workflow.context_manager import BlockMetadata, WorkflowRunContextfrom skyvern.forge.sdk.workflow.exceptions import (CustomizedCodeException,FailedToFormatJinjaStyleParameter,@@ -1023,8 +1018,6 @@ class BaseTaskBlock(Block):)else:# if not the first task block, need to navigate manually- # get_for_workflow_run checks SkyvernContext for iteration-specific- # browser keys (__iter_N) first, falling back to bare workflow_run_id.browser_state = app.BROWSER_MANAGER.get_for_workflow_run(workflow_run_id=workflow_run_id)if browser_state is None:raise MissingBrowserState(task_id=task.task_id, workflow_run_id=workflow_run_id)@@ -1410,14 +1403,6 @@ class ForLoopBlock(Block):# Note: intentionally excludes `list` (unlike BaseTaskBlock.data_schema) because a list schema# does not describe the shape of individual loop items -- only dict schemas are meaningful here.data_schema: dict[str, Any] | str | None = None- # Parallel execution: None or 1 = sequential (existing behavior), 2-20 = parallel batch size.- max_concurrency: int | None = None-- @model_validator(mode="after")- def _clamp_max_concurrency(self) -> ForLoopBlock:- if self.max_concurrency is not None:- self.max_concurrency = max(1, min(self.max_concurrency, 20))- return selfdef get_all_parameters(self,@@ -1909,61 +1894,6 @@ class ForLoopBlock(Block):organization_id: str | None = None,browser_session_id: str | None = None,) -> LoopBlockExecutedResult:- # Dispatch to parallel path when max_concurrency > 1- effective_concurrency = self.max_concurrency or 1- if effective_concurrency > 1 and browser_session_id:- # Persistent browser sessions cannot be safely shared across- # parallel iterations — every iteration would race on the same- # live page. Fall back to sequential execution rather than- # corrupt the session.- LOG.warning(- "Persistent browser session set; forcing sequential execution for for-loop",- workflow_run_id=workflow_run_id,- browser_session_id=browser_session_id,- requested_concurrency=self.max_concurrency,- )- effective_concurrency = 1- if effective_concurrency > 1:- # Check per-org quota — may reduce concurrency or force sequential- if organization_id:- effective_concurrency = await app.AGENT_FUNCTION.check_parallel_loop_quota(- organization_id, effective_concurrency- )- if effective_concurrency <= 1:- # Release whatever was granted before falling back to sequential.- # A return value of 0 means no slots were acquired, so nothing- # to release. A return value of 1 means one slot was incremented- # in Redis and must be decremented to avoid a leak.- if effective_concurrency > 0:- await app.AGENT_FUNCTION.release_parallel_loop_quota(organization_id, effective_concurrency)- LOG.info(- "Org concurrency quota exhausted, falling back to sequential execution",- workflow_run_id=workflow_run_id,- organization_id=organization_id,- requested_concurrency=self.max_concurrency,- )- # Fall through to sequential path below-- if effective_concurrency > 1:- if self.max_concurrency and effective_concurrency < self.max_concurrency:- LOG.warning(- "ForLoopBlock parallel granted concurrency below requested",- workflow_run_id=workflow_run_id,- organization_id=organization_id,- requested_concurrency=self.max_concurrency,- granted_concurrency=effective_concurrency,- )- return await self._execute_loop_parallel(- workflow_run_id=workflow_run_id,- workflow_run_block_id=workflow_run_block_id,- workflow_run_context=workflow_run_context,- loop_over_values=loop_over_values,- organization_id=organization_id,- browser_session_id=browser_session_id,- granted_concurrency=effective_concurrency,- )-- # --- Sequential path (existing behavior, unchanged) ---outputs_with_loop_values: list[list[dict[str, Any]]] = []block_outputs: list[BlockResult] = []current_block: BlockTypeVar | None = None@@ -2271,499 +2201,6 @@ class ForLoopBlock(Block):last_block=current_block,)- async def _execute_single_iteration_parallel(- self,- loop_idx: int,- loop_over_value: Any,- workflow_run_id: str,- workflow_run_block_id: str,- workflow_run_context: WorkflowRunContext,- start_label: str,- label_to_block: dict[str, BlockTypeVar],- default_next_map: dict[str, str | None],- conditional_scopes: dict[str, str],- organization_id: str | None = None,- browser_session_id: str | None = None,- ) -> tuple[int, list[dict[str, Any]], list[BlockResult], BlockTypeVar | None, WorkflowRunContext]:- """Execute a single loop iteration with its own isolated context.-- Returns (loop_idx, iteration_outputs, block_results, last_block, context_snapshot).- """- each_loop_output_values: list[dict[str, Any]] = []- block_outputs: list[BlockResult] = []- current_block: BlockTypeVar | None = None-- # Create isolated browser context for this iteration and register it- # under workflow_run_id so existing task block browser lookups find it.- # This is safe because each coroutine in asyncio.gather holds its own- # reference and the SkyvernContext iteration key provides disambiguation.- try:- await app.BROWSER_MANAGER.get_or_create_for_loop_iteration(- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- organization_id=organization_id,- browser_session_id=browser_session_id,- )- # The iteration browser is stored under the iteration key- # (e.g. "wr_xxx__iter_0") and resolved via SkyvernContext in- # get_or_create_for_workflow_run(). We intentionally do NOT- # alias it under the bare workflow_run_id to avoid races- # between concurrent iterations.- except Exception:- # Fail this iteration immediately. Falling back to the shared- # workflow browser would cause concurrent iterations to race on- # the same page, corrupting navigation/actions and producing- # non-deterministic data. Sequential loops can safely share a- # browser; parallel loops cannot.- LOG.error(- "Failed to create isolated browser for parallel iteration",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- exc_info=True,- )- failure_block_result = await self.build_block_result(- success=False,- status=BlockStatus.failed,- failure_reason=f"Failed to create isolated browser for parallel iteration {loop_idx}",- workflow_run_block_id=None,- organization_id=organization_id,- )- return loop_idx, [], [failure_block_result], None, workflow_run_context-- # Capture baseline downloaded files for per-iteration scoping (SKY-7005)- loop_context = skyvern_context.current()- if loop_context:- downloaded_file_sigs_before: list[tuple[str | None, str | None, str | None]] = []- baseline_timed_out = False- try:- async with asyncio.timeout(GET_DOWNLOADED_FILES_TIMEOUT):- downloaded_file_sigs_before = [- to_downloaded_file_signature(fi)- for fi in await app.STORAGE.get_downloaded_files(- organization_id=organization_id or "",- run_id=loop_context.run_id if loop_context.run_id else workflow_run_id,- )- ]- except asyncio.TimeoutError:- baseline_timed_out = True- LOG.warning(- "Timeout getting baseline downloaded files for parallel loop iteration",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- )- if baseline_timed_out:- loop_context.loop_internal_state = None- else:- loop_context.loop_internal_state = {- "downloaded_file_signatures_before_iteration": downloaded_file_sigs_before,- }-- context_parameters_with_value = self.get_loop_block_context_parameters(workflow_run_id, loop_over_value)- for context_parameter in context_parameters_with_value:- workflow_run_context.set_value(context_parameter.key, context_parameter.value)-- iteration_step_count = 0- LOG.info(- "ForLoopBlock parallel: starting iteration",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- max_steps_per_iteration=DEFAULT_MAX_STEPS_PER_ITERATION,- )-- block_idx = 0- current_label: str | None = start_label- conditional_wrb_ids: dict[str, str] = {}- while current_label:- loop_block = label_to_block.get(current_label)- if not loop_block:- LOG.error(- "Unable to find loop block with label in parallel loop graph",- workflow_run_id=workflow_run_id,- loop_label=self.label,- current_label=current_label,- )- failure_block_result = await self.build_block_result(- success=False,- status=BlockStatus.failed,- failure_reason=f"Unable to find block with label {current_label} inside loop {self.label}",- workflow_run_block_id=workflow_run_block_id,- organization_id=organization_id,- )- block_outputs.append(failure_block_result)- return (loop_idx, each_loop_output_values, block_outputs, current_block, workflow_run_context)-- metadata: BlockMetadata = {- "current_index": loop_idx,- "current_value": loop_over_value,- "current_item": loop_over_value,- }- workflow_run_context.update_block_metadata(self.label, metadata)- workflow_run_context.update_block_metadata(loop_block.label, metadata)-- original_loop_block = loop_block- loop_block = loop_block.model_copy(deep=True)- current_block = loop_block-- parent_wrb_id = workflow_run_block_id- if current_label in conditional_scopes:- cond_label = conditional_scopes[current_label]- if cond_label in conditional_wrb_ids:- parent_wrb_id = conditional_wrb_ids[cond_label]-- block_output = await loop_block.execute_safe(- workflow_run_id=workflow_run_id,- parent_workflow_run_block_id=parent_wrb_id,- organization_id=organization_id,- browser_session_id=browser_session_id,- current_value=str(loop_over_value),- current_index=loop_idx,- )-- if loop_block.block_type == BlockType.CONDITIONAL and block_output.workflow_run_block_id:- conditional_wrb_ids[current_label] = block_output.workflow_run_block_id-- output_value = (- workflow_run_context.get_value(block_output.output_parameter.key)- if workflow_run_context.has_value(block_output.output_parameter.key)- else None- )-- if block_output.output_parameter.key.endswith("_output"):- LOG.debug("Block output (parallel)", block_type=loop_block.block_type, output_value=output_value)-- each_loop_output_values.append(- {- "loop_value": loop_over_value,- "output_parameter": block_output.output_parameter,- "output_value": output_value,- }- )- try:- if block_output.workflow_run_block_id:- await app.DATABASE.observer.update_workflow_run_block(- workflow_run_block_id=block_output.workflow_run_block_id,- organization_id=organization_id,- current_value=str(loop_over_value),- current_index=loop_idx,- )- except Exception:- LOG.warning(- "Failed to update workflow run block (parallel)",- workflow_run_block_id=block_output.workflow_run_block_id,- loop_over_value=loop_over_value,- loop_idx=loop_idx,- )- loop_block = original_loop_block- block_outputs.append(block_output)-- iteration_step_count += 1- if iteration_step_count >= DEFAULT_MAX_STEPS_PER_ITERATION:- LOG.info(- "ForLoopBlock parallel: reached max_steps_per_iteration limit",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- )- failure_block_result = await self.build_block_result(- success=False,- status=BlockStatus.failed,- failure_reason=f"Reached max_steps_per_iteration limit of {DEFAULT_MAX_STEPS_PER_ITERATION}",- workflow_run_block_id=workflow_run_block_id,- organization_id=organization_id,- )- block_outputs.append(failure_block_result)- break-- if block_output.status == BlockStatus.canceled:- LOG.info(- "ForLoopBlock parallel: iteration was canceled",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- )- break-- if (- not block_output.success- and not loop_block.continue_on_failure- and not loop_block.next_loop_on_failure- and not self.next_loop_on_failure- ):- LOG.info(- "ForLoopBlock parallel: iteration block failed, terminating",- loop_idx=loop_idx,- block_idx=block_idx,- failure_reason=block_output.failure_reason,- )- break-- if block_output.success or loop_block.continue_on_failure:- next_label: str | None = None- if loop_block.block_type == BlockType.CONDITIONAL:- branch_metadata = (- block_output.output_parameter_value- if isinstance(block_output.output_parameter_value, dict)- else None- )- next_label = (branch_metadata or {}).get("next_block_label")- else:- next_label = default_next_map.get(loop_block.label)-- if not next_label:- break-- if next_label not in label_to_block:- failure_block_result = await self.build_block_result(- success=False,- status=BlockStatus.failed,- failure_reason=f"Next block label {next_label} not found inside loop {self.label}",- workflow_run_block_id=workflow_run_block_id,- organization_id=organization_id,- )- block_outputs.append(failure_block_result)- break-- current_label = next_label- block_idx += 1- continue-- if loop_block.next_loop_on_failure or self.next_loop_on_failure:- LOG.info(- "ForLoopBlock parallel: iteration block failed, continuing to next",- loop_idx=loop_idx,- block_idx=block_idx,- )- break-- break-- return (loop_idx, each_loop_output_values, block_outputs, current_block, workflow_run_context)-- async def _execute_loop_parallel(- self,- workflow_run_id: str,- workflow_run_block_id: str,- workflow_run_context: WorkflowRunContext,- loop_over_values: list[Any],- organization_id: str | None = None,- browser_session_id: str | None = None,- granted_concurrency: int | None = None,- ) -> LoopBlockExecutedResult:- """Execute loop iterations in parallel batches of max_concurrency.-- Slot ownership contract:- - The CALLER (`execute_loop_helper`) acquires `granted_concurrency`- slots via `check_parallel_loop_quota` before invoking this method.- - This method takes ownership of those slots and is responsible for- releasing exactly `granted_concurrency` slots in its `finally` block.- - Callers must NOT release the slots themselves after this method- returns or raises.- """- # Caller (execute_loop_helper) only invokes this method when the- # post-quota granted_concurrency is > 1. Use an explicit check rather- # than `assert` so the contract holds when running with -O.- if not granted_concurrency or granted_concurrency <= 1:- raise ValueError(f"_execute_loop_parallel requires granted_concurrency > 1, got {granted_concurrency}")- effective_concurrency = granted_concurrency- outputs_with_loop_values: list[list[dict[str, Any]]] = []- all_block_outputs: list[BlockResult] = []- last_block: BlockTypeVar | None = None- # Release exactly what we acquired. acquire_parallel_slots already- # returns min(requested, available), so effective_concurrency is the- # true granted count. Releasing fewer (e.g. min(granted, len(values)))- # would orphan the unused slots in Redis until TTL expiry.- slots_to_release = effective_concurrency-- # Wrap the entire method body so concurrency slots are always released,- # even if _build_loop_graph or compute_conditional_scopes fails.- try:- start_label, label_to_block, default_next_map = self._build_loop_graph(self.loop_blocks)- conditional_scopes = compute_conditional_scopes(label_to_block, default_next_map)-- # Enforce max iterations- capped_values = loop_over_values[:DEFAULT_MAX_LOOP_ITERATIONS]- if len(loop_over_values) > DEFAULT_MAX_LOOP_ITERATIONS:- LOG.info(- "ForLoopBlock parallel: capping iterations",- workflow_run_id=workflow_run_id,- max_iterations=DEFAULT_MAX_LOOP_ITERATIONS,- total_values=len(loop_over_values),- )- # Process in batches- for batch_start in range(0, len(capped_values), effective_concurrency):- batch_end = min(batch_start + effective_concurrency, len(capped_values))- batch = [(idx, val) for idx, val in enumerate(capped_values[batch_start:batch_end], start=batch_start)]-- LOG.info(- "ForLoopBlock parallel: executing batch",- workflow_run_id=workflow_run_id,- batch_start=batch_start,- batch_end=batch_end,- batch_size=len(batch),- max_concurrency=effective_concurrency,- )-- # Create context snapshots for each iteration in the batch- iteration_tasks = []- batch_loop_indices = []- for loop_idx, loop_over_value in batch:- batch_loop_indices.append(loop_idx)- ctx_snapshot = workflow_run_context.create_iteration_snapshot(loop_idx)-- # Default args freeze loop variables at definition time so each- # task closes over its own loop_idx/value/snapshot rather than- # the late-binding loop locals.- async def _run_iteration(- _loop_idx: int = loop_idx,- _loop_value: Any = loop_over_value,- _ctx_snapshot: WorkflowRunContext = ctx_snapshot,- ) -> tuple[int, list[dict[str, Any]], list[BlockResult], BlockTypeVar | None, WorkflowRunContext]:- # Install the per-iteration workflow_run_context snapshot- # into the ContextVar override so child blocks that resolve- # context via Block.get_workflow_run_context(workflow_run_id)- # see the isolated snapshot instead of the shared global.- # create_task() snapshotted ContextVars at task creation,- # so this set() only mutates the current task's copy.- set_iteration_workflow_run_context(_ctx_snapshot)-- # Fork SkyvernContext for this iteration with isolated mutable fields.- # Uses create_iteration_copy() which shallow-copies scalars and creates- # fresh dicts/lists/sets, avoiding deepcopy issues with Playwright- # Frame/Page objects that are not safely copyable.- parent_skyvern_ctx = skyvern_context.current()- if parent_skyvern_ctx:- iter_ctx = parent_skyvern_ctx.create_iteration_copy(- browser_session_id=loop_iteration_key(workflow_run_id, _loop_idx),- )- skyvern_context.set(iter_ctx)- else:- LOG.warning(- "No parent SkyvernContext to fork for parallel iteration — "- "per-iteration file tracking skipped",- workflow_run_id=workflow_run_id,- loop_idx=_loop_idx,- )-- return await self._execute_single_iteration_parallel(- loop_idx=_loop_idx,- loop_over_value=_loop_value,- workflow_run_id=workflow_run_id,- workflow_run_block_id=workflow_run_block_id,- workflow_run_context=_ctx_snapshot,- start_label=start_label,- label_to_block=label_to_block,- default_next_map=default_next_map,- conditional_scopes=conditional_scopes,- organization_id=organization_id,- browser_session_id=browser_session_id,- )-- # asyncio.create_task() copies the current ContextVar state at- # creation time, giving each iteration its own isolated copy.- # Plain coroutines in asyncio.gather() share the caller's- # ContextVar scope, so skyvern_context.set() would race.- iteration_tasks.append(asyncio.create_task(_run_iteration()))-- # Execute batch concurrently; return_exceptions=True so one failure doesn't kill the batch- batch_results = await asyncio.gather(*iteration_tasks, return_exceptions=True)-- # Collect snapshots for merging- context_snapshots: list[tuple[int, WorkflowRunContext]] = []-- # Sort results by loop_idx to preserve ordering- indexed_results: list[tuple[int, Any]] = []- for i, result in enumerate(batch_results):- loop_idx_for_result = batch[i][0]- indexed_results.append((loop_idx_for_result, result))- indexed_results.sort(key=lambda t: t[0])-- # Process every result in the batch before deciding whether to- # stop. asyncio.gather(return_exceptions=True) already ran all- # iterations to completion, so successful outputs from indices- # after a failing one must still be captured — otherwise we'd- # silently discard data the browser already produced.- batch_had_failure = False- for loop_idx_sorted, result in indexed_results:- if isinstance(result, Exception):- LOG.error(- "ForLoopBlock parallel: iteration raised an exception",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx_sorted,- error=str(result),- exc_info=result,- )- failure_block_result = await self.build_block_result(- success=False,- status=BlockStatus.failed,- failure_reason=f"Parallel iteration {loop_idx_sorted} failed: {str(result)}",- workflow_run_block_id=workflow_run_block_id,- organization_id=organization_id,- )- all_block_outputs.append(failure_block_result)- outputs_with_loop_values.append([])- batch_had_failure = True- else:- iter_idx, iter_outputs, iter_block_results, iter_last_block, iter_ctx = result- outputs_with_loop_values.append(iter_outputs)- all_block_outputs.extend(iter_block_results)- if iter_last_block is not None:- last_block = iter_last_block- context_snapshots.append((iter_idx, iter_ctx))-- if batch_had_failure and not self.next_loop_on_failure:- # Merge any successful snapshots before stopping so their- # context mutations aren't lost.- workflow_run_context.merge_iteration_results(context_snapshots)- # Persist accumulated outputs before the early return so a- # subsequent Temporal timeout can recover prior-batch data.- await self._persist_partial_loop_output(- workflow_run_id, outputs_with_loop_values, batch_loop_indices[-1]- )- try:- await app.BROWSER_MANAGER.cleanup_loop_iterations(- workflow_run_id=workflow_run_id,- loop_indices=batch_loop_indices,- organization_id=organization_id,- )- except Exception:- LOG.warning("Failed to cleanup loop iteration browsers", exc_info=True)- return LoopBlockExecutedResult(- outputs_with_loop_values=outputs_with_loop_values,- block_outputs=all_block_outputs,- last_block=last_block,- )-- # Merge iteration context snapshots back into the main context- workflow_run_context.merge_iteration_results(context_snapshots)-- # Persist accumulated outputs after each batch so progress- # survives Temporal activity timeouts mid-loop. Mirrors the- # sequential path's per-iteration persistence.- await self._persist_partial_loop_output(- workflow_run_id, outputs_with_loop_values, batch_loop_indices[-1]- )-- # Clean up iteration browser contexts for this batch- try:- await app.BROWSER_MANAGER.cleanup_loop_iterations(- workflow_run_id=workflow_run_id,- loop_indices=batch_loop_indices,- organization_id=organization_id,- )- except Exception:- LOG.warning("Failed to cleanup loop iteration browsers after batch", exc_info=True)- finally:- # Always release concurrency slots back to the org quota.- # Release the full granted count — acquire incremented Redis by- # this amount, so we must decrement by the same amount even if- # the loop had fewer iterations than granted slots.- if organization_id and slots_to_release > 0:- await app.AGENT_FUNCTION.release_parallel_loop_quota(organization_id, slots_to_release)-- return LoopBlockExecutedResult(- outputs_with_loop_values=outputs_with_loop_values,- block_outputs=all_block_outputs,- last_block=last_block,- )-async def execute(self,workflow_run_id: str,diff --git a/skyvern/forge/sdk/workflow/workflow_definition_converter.py b/skyvern/forge/sdk/workflow/workflow_definition_converter.pyindex 422bdc614..213efa6f1 100644--- a/skyvern/forge/sdk/workflow/workflow_definition_converter.py+++ b/skyvern/forge/sdk/workflow/workflow_definition_converter.py@@ -439,7 +439,6 @@ def block_yaml_to_block(loop_blocks=loop_blocks,complete_if_empty=block_yaml.complete_if_empty,data_schema=block_yaml.data_schema,- max_concurrency=block_yaml.max_concurrency,)elif block_yaml.block_type == BlockType.CONDITIONAL:branch_conditions = []diff --git a/skyvern/schemas/workflows.py b/skyvern/schemas/workflows.pyindex 393ae55d0..d938a70ab 100644--- a/skyvern/schemas/workflows.py+++ b/skyvern/schemas/workflows.py@@ -641,7 +641,6 @@ class ForLoopBlockYAML(BlockYAML):loop_variable_reference: str | None = Nonecomplete_if_empty: bool = Falsedata_schema: dict[str, Any] | str | None = None- max_concurrency: int | None = Noneclass BranchCriteriaYAML(BaseModel):diff --git a/skyvern/webeye/browser_manager.py b/skyvern/webeye/browser_manager.pyindex a82480ed8..6b36e0271 100644--- a/skyvern/webeye/browser_manager.py+++ b/skyvern/webeye/browser_manager.py@@ -61,21 +61,6 @@ class BrowserManager(Protocol):def get_for_script(self, script_id: str | None = None) -> BrowserState | None: ...- async def get_or_create_for_loop_iteration(- self,- workflow_run_id: str,- loop_idx: int,- organization_id: str | None = None,- browser_session_id: str | None = None,- ) -> BrowserState: ...-- async def cleanup_loop_iterations(- self,- workflow_run_id: str,- loop_indices: list[int],- organization_id: str | None = None,- ) -> None: ...-def set_video_artifact_for_task(self, task: Task, artifacts: list[VideoArtifact]) -> None: ...async def get_video_artifacts(diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 3f21b5718..bffe99ad6 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -1,16 +1,12 @@from __future__ import annotations-import asyncioimport os-import timeimport structlogfrom playwright.async_api import async_playwright-from skyvern.constants import is_loop_iteration_key, loop_iteration_keyfrom skyvern.exceptions import MissingBrowserStatefrom skyvern.forge import app-from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.schemas.tasks import Taskfrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunfrom skyvern.schemas.runs import ProxyLocation, ProxyLocationInput@@ -23,47 +19,9 @@ from skyvern.webeye.real_browser_state import RealBrowserStateLOG = structlog.get_logger()-def _consume_future_exception(future: asyncio.Future[BrowserState]) -> None:- """Done-callback that retrieves a future's exception so asyncio does not- emit an "exception was never retrieved" warning when the owning coroutine- re-raises and no other waiter attached to the in-flight future. The- owner always re-raises the real exception on its own task, so dropping- it here is safe — this callback only exists to silence asyncio's- unhandled-exception warning when the in-flight group happened to have- zero waiters at the time of failure.-- Logs at INFO so launch failures and teardown-race exceptions are- correlatable with incidents even when the owner's re-raise lands in a- separate log entry (cleanup/close/owner-cancellation paths intentionally- surface a RuntimeError here; real Playwright failures surface the real- exception type)."""- if future.cancelled():- return- exc = future.exception()- if exc is not None:- LOG.info(- "Consumed in-flight loop iteration launch exception (re-raised on owner)",- exc_type=type(exc).__name__,- )--class RealBrowserManager(BrowserManager):def __init__(self) -> None:self.pages: dict[str, BrowserState] = {}- # Lazily initialized inside an async context to avoid binding the lock- # to the wrong event loop when RealBrowserManager is instantiated- # during module import or test fixtures that haven't started a loop.- self._loop_iteration_lock_instance: asyncio.Lock | None = None- # Single-flight registry for in-progress loop-iteration browser- # launches. The lock is held only across check-then-insert, not- # across the Playwright launch itself.- self._loop_iteration_inflight: dict[str, asyncio.Future[BrowserState]] = {}-- @property- def _loop_iteration_lock(self) -> asyncio.Lock:- if self._loop_iteration_lock_instance is None:- self._loop_iteration_lock_instance = asyncio.Lock()- return self._loop_iteration_lock_instance@staticmethodasync def _create_browser_state(@@ -202,28 +160,6 @@ class RealBrowserManager(BrowserManager):if browser_profile_id is None:browser_profile_id = workflow_run.browser_profile_id- # When running inside a parallel loop iteration, SkyvernContext holds an- # iteration-specific browser_session_id (e.g. "wr_xxx__iter_0"). The- # iteration browser was pre-created by get_or_create_for_loop_iteration()- # and stored under that key. Return it directly so child blocks (task,- # action, etc.) use the isolated browser instead of racing to create one- # under the bare workflow_run_id.- ctx = skyvern_context.current()- if ctx and ctx.browser_session_id and is_loop_iteration_key(ctx.browser_session_id):- iteration_browser = self.pages.get(ctx.browser_session_id)- if iteration_browser:- # Navigate to the task URL if page is still on about:blank- if url:- page = await iteration_browser.get_working_page()- if page and page.url == "about:blank":- await iteration_browser.navigate_to_url(page=page, url=url)- LOG.debug(- "Returning iteration-specific browser state from parallel loop context",- workflow_run_id=workflow_run_id,- iteration_key=ctx.browser_session_id,- )- return iteration_browser-# Check own cache entry first so navigate_to_url is only called on the first step.# Don't pass parent_workflow_run_id here — that lookup is deferred to the block# below so PBS runs don't accidentally inherit the parent's browser.@@ -316,15 +252,6 @@ class RealBrowserManager(BrowserManager):def get_for_workflow_run(self, workflow_run_id: str, parent_workflow_run_id: str | None = None) -> BrowserState | None:- # Check for parallel loop iteration browser via SkyvernContext first.- # This mirrors the async get_or_create_for_workflow_run() so callers- # like task block's non-first-task path get the correct iteration browser.- ctx = skyvern_context.current()- if ctx and ctx.browser_session_id and is_loop_iteration_key(ctx.browser_session_id):- iteration_browser = self.pages.get(ctx.browser_session_id)- if iteration_browser:- return iteration_browser-# Priority: parent first, then own entry.# Callers that need to avoid parent inheritance must omit parent_workflow_run_id.# See get_or_create_for_workflow_run() for the two-phase lookup pattern.@@ -337,13 +264,6 @@ class RealBrowserManager(BrowserManager):return Nonedef set_video_artifact_for_task(self, task: Task, artifacts: list[VideoArtifact]) -> None:- # Check parallel loop iteration browser first- ctx = skyvern_context.current()- if ctx and ctx.browser_session_id and is_loop_iteration_key(ctx.browser_session_id):- iter_browser = self.pages.get(ctx.browser_session_id)- if iter_browser:- iter_browser.browser_artifacts.video_artifacts = artifacts- returnif task.workflow_run_id and task.workflow_run_id in self.pages:self.pages[task.workflow_run_id].browser_artifacts.video_artifacts = artifactsreturn@@ -425,33 +345,9 @@ class RealBrowserManager(BrowserManager):async def close(self) -> None:LOG.info("Closing BrowserManager")- # Hold the loop-iteration lock across the whole shutdown so a- # concurrent Phase 1 registration cannot land a fresh in-flight- # future after we've already cancelled + cleared the dict, and- # so cleanup_loop_iterations cannot race with us for the same- # keys. cleanup_loop_iterations already takes this lock; close()- # must do the same for the invariant to hold.- async with self._loop_iteration_lock:- # Fail in-flight launches with a regular Exception (not cancel())- # so shielded same-key waiters see a RuntimeError they can route- # through _execute_single_iteration_parallel's Exception handler,- # instead of a CancelledError that would bypass `isinstance(result,- # Exception)` in _execute_loop_parallel and crash the batch unpack.- # The owner of each launch still discards its new browser state- # via the identity-guarded check below (stored_future mismatch) —- # future.cancelled() is not required for discard.- for key, future in list(self._loop_iteration_inflight.items()):- if not future.done():- future.set_exception(RuntimeError(f"loop iteration {key} was cleaned up during launch"))- self._loop_iteration_inflight.clear()- # Intentionally sequential here (unlike cleanup_loop_iterations,- # which parallelizes via asyncio.gather). close() runs under- # _loop_iteration_lock so that Phase 1 registration cannot- # race the teardown; a future refactor to gather() must- # preserve that lock-held invariant.- for browser_state in self.pages.values():- await browser_state.close()- self.pages = dict()+ for browser_state in self.pages.values():+ await browser_state.close()+ self.pages = dict()LOG.info("BrowserManger is closed")async def cleanup_for_task(@@ -629,274 +525,3 @@ class RealBrowserManager(BrowserManager):if script_id and script_id in self.pages:return self.pages[script_id]return None-- async def get_or_create_for_loop_iteration(- self,- workflow_run_id: str,- loop_idx: int,- organization_id: str | None = None,- browser_session_id: str | None = None,- ) -> BrowserState:- """Get or create an isolated BrowserContext for a parallel loop iteration.-- Each iteration gets its own browser context so cookies, auth state, and- storage are fully isolated between concurrent iterations.-- Uses _loop_iteration_lock to guard the check-then-register on- self.pages / self._loop_iteration_inflight, but releases it before- doing the Playwright launch so concurrent iterations with distinct- keys can cold-start in parallel. Single-flight semantics are- preserved via an in-flight futures map so concurrent callers for- the same key share a single launch.- """- key = loop_iteration_key(workflow_run_id, loop_idx)-- # Phase 1: short critical section — either return cached, attach to- # an in-flight launch, or register a new future and proceed to launch.- async with self._loop_iteration_lock:- if key in self.pages:- cached_state = self.pages[key]- inflight_future: asyncio.Future[BrowserState] | None = None- owns_launch = False- elif key in self._loop_iteration_inflight:- cached_state = None- inflight_future = self._loop_iteration_inflight[key]- owns_launch = False- else:- cached_state = None- inflight_future = asyncio.get_running_loop().create_future()- # Eagerly observe any exception the future may carry so that- # a launch failure with no attached waiters doesn't emit an- # "exception was never retrieved" warning from asyncio. The- # owning coroutine always re-raises separately.- inflight_future.add_done_callback(_consume_future_exception)- self._loop_iteration_inflight[key] = inflight_future- owns_launch = True-- # Persistent sessions cannot be aliased under per-iteration keys —- # multiple iterations would race on the same live page. The caller- # (execute_loop_helper) is expected to force sequential execution- # when a persistent session is in use; this branch only runs if- # that contract is bypassed, in which case we still create a fresh- # isolated context to preserve correctness over the persistence.- if browser_session_id:- LOG.warning(- "Persistent browser session not used for parallel loop iteration — "- "creating isolated context to avoid cross-iteration races",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- browser_session_id=browser_session_id,- )-- LOG.info(- "Creating isolated browser state for loop iteration",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- key=key,- )-- # Phase 2: fulfil the request outside the lock.- if cached_state is not None:- browser_state = cached_state- elif not owns_launch:- # Attached to another coroutine's in-flight launch. Shield the- # shared future from this waiter's cancellation so one waiter- # being cancelled does not cancel the shared launch and fan out- # CancelledError to the entire single-flight group.- assert inflight_future is not None- browser_state = await asyncio.shield(inflight_future)- else:- # This coroutine owns the launch — do it unlocked so other keys- # can launch in parallel. The owner also performs the first- # get_or_create_page before publishing the future so same-key- # waiters don't race inside RealBrowserState.get_or_create_page- # (which is not internally synchronized).- assert inflight_future is not None- owner_state: BrowserState | None = None- launch_start = time.perf_counter()- try:- owner_state = await self._create_browser_state(- workflow_run_id=workflow_run_id,- organization_id=organization_id,- )- await owner_state.get_or_create_page(- workflow_run_id=workflow_run_id,- organization_id=organization_id,- )- except BaseException as exc:- # The pop + future-resolution pair below must be synchronous- # (no awaits between them) so cancellation cannot split the- # pop from the future resolution and orphan the in-flight- # entry. Any attached waiters see the exception; subsequent- # callers get a fresh launch.- #- # Identity-guarded pop: a concurrent cleanup may have- # cancelled+removed our future and a replacement owner may- # have registered a new one for the same key. Only remove- # the entry if it is still ours — otherwise leave the- # replacement alone.- if self._loop_iteration_inflight.get(key) is inflight_future:- self._loop_iteration_inflight.pop(key, None)- if not inflight_future.done():- # Normalize owner-task cancellation to a RuntimeError for- # shielded waiters. A raw CancelledError on the shared- # future would bypass `isinstance(result, Exception)` in- # _execute_loop_parallel (CancelledError is a BaseException)- # and crash the batch success-unpack. The owner task- # itself still re-raises the original CancelledError via- # the `raise` below to let its own cancellation propagate.- if isinstance(exc, asyncio.CancelledError):- inflight_future.set_exception(RuntimeError(f"loop iteration {key} launch cancelled"))- else:- inflight_future.set_exception(exc)- # Teardown below runs AFTER the future is already settled,- # so the await does not affect waiters or reopen a race on- # the in-flight entry. Best effort — a failing close should- # not mask the original exception.- if owner_state is not None:- try:- await asyncio.shield(owner_state.close())- except BaseException:- LOG.warning(- "Failed to close half-initialized loop iteration browser state",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- exc_info=True,- )- raise-- assert owner_state is not None- browser_state = owner_state- # Success path is synchronous from here on — no awaits between- # page init completing and the future being resolved, so- # cancellation of this owner task cannot orphan the in-flight- # entry.- #- # cleanup_loop_iterations and close() can run concurrently with- # the launch (launch is no longer under the lock). Both teardown- # paths pop our entry from _loop_iteration_inflight and then set- # a RuntimeError on the future, so our identity check below- # detects the race via `stored_future is not inflight_future`- # alone. No `inflight_future.cancelled()` check is needed- # because neither teardown path calls `.cancel()` — both use- # set_exception so shielded waiters receive a regular Exception- # that _execute_single_iteration_parallel handles normally- # instead of a BaseException-derived CancelledError that would- # bypass the batch-result dispatch.- stored_future = self._loop_iteration_inflight.get(key)- if stored_future is inflight_future:- self._loop_iteration_inflight.pop(key, None)- if stored_future is not inflight_future:- LOG.warning(- "Loop iteration cleanup ran during launch, discarding browser state",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- key=key,- )- try:- await asyncio.shield(browser_state.close())- except BaseException:- LOG.warning(- "Failed to close loop iteration browser state after concurrent cleanup",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- exc_info=True,- )- # Use a plain Exception subclass rather than CancelledError:- # the parallel for-loop driver routes gather() results via- # isinstance(result, Exception), and BaseException-derived- # CancelledError would bypass that check and crash the- # success-unpack path with a TypeError.- raise RuntimeError(f"loop iteration {key} was cleaned up during launch")-- self.pages[key] = browser_state- if not inflight_future.done():- inflight_future.set_result(browser_state)- LOG.info(- "Loop iteration browser launch completed",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- key=key,- elapsed_s=round(time.perf_counter() - launch_start, 3),- )- return browser_state-- # Waiters/cache-hit path: the owner has already completed the- # initial get_or_create_page before publishing the future, so this- # call will cache-hit and not race with any concurrent setup.- await browser_state.get_or_create_page(- workflow_run_id=workflow_run_id,- organization_id=organization_id,- )- return browser_state-- async def cleanup_loop_iterations(- self,- workflow_run_id: str,- loop_indices: list[int],- organization_id: str | None = None,- ) -> None:- """Close and remove browser states for the given parallel loop iterations.-- Uses _loop_iteration_lock so cleanup cannot race with create or with- a concurrent cleanup call from another batch.- """- # Collect entries to close under the lock, then close outside it- # to avoid holding the lock during potentially slow browser teardown.- # Also fail any in-flight launches for these keys so a launch still- # running in its unlocked section discards its new browser state- # instead of publishing it after cleanup has already run.- to_close: list[tuple[int, BrowserState]] = []- async with self._loop_iteration_lock:- for loop_idx in loop_indices:- key = loop_iteration_key(workflow_run_id, loop_idx)- inflight_future = self._loop_iteration_inflight.pop(key, None)- if inflight_future is not None and not inflight_future.done():- # set_exception (not cancel()) so shielded same-key- # waiters receive a RuntimeError that- # _execute_single_iteration_parallel can catch via its- # `except Exception` handler, instead of a CancelledError- # that would bypass _execute_loop_parallel's- # `isinstance(result, Exception)` dispatch and crash the- # batch success-unpack path (CancelledError is a- # BaseException, not an Exception).- inflight_future.set_exception(RuntimeError(f"loop iteration {key} was cleaned up during launch"))- browser_state = self.pages.pop(key, None)- if browser_state is None:- continue- # Only close if no other entry still references the same object- shared = any(bs is browser_state for bs in self.pages.values())- if not shared:- to_close.append((loop_idx, browser_state))-- if not to_close:- return-- # Close all entries in parallel — Playwright context teardown is- # IO-bound and would otherwise dominate the batch gap.- close_start = time.perf_counter()- results = await asyncio.gather(- *(browser_state.close() for _, browser_state in to_close),- return_exceptions=True,- )- close_elapsed_s = time.perf_counter() - close_start- failure_count = sum(1 for r in results if isinstance(r, BaseException))- LOG.info(- "cleanup_loop_iterations closed entries",- workflow_run_id=workflow_run_id,- count=len(to_close),- failures=failure_count,- elapsed_s=round(close_elapsed_s, 3),- )- for (loop_idx, _), result in zip(to_close, results):- if isinstance(result, BaseException):- LOG.warning(- "Failed to close loop iteration browser state",- workflow_run_id=workflow_run_id,- loop_idx=loop_idx,- # return_exceptions=True means there is no active- # exception context, so exc_info=True won't pick- # anything up. Pass an explicit (type, value, tb)- # tuple — structlog's documented signature.- exc_info=(type(result), result, result.__traceback__),- )diff --git a/tests/unit/test_parallel_loop_execution.py b/tests/unit/test_parallel_loop_execution.pydeleted file mode 100644index a116a2628..000000000--- a/tests/unit/test_parallel_loop_execution.py+++ /dev/null@@ -1,985 +0,0 @@-"""Tests for parallel loop execution (SKY-8175 + SKY-8176 + SKY-8180).--Tests cover:-1. max_concurrency field validation and clamping-2. Sequential behavior unchanged when max_concurrency is None or 1-3. Parallel execution dispatches correctly-4. Result ordering is preserved regardless of completion order-5. Error handling: one iteration failure doesn't kill others (when next_loop_on_failure=True)-6. WorkflowRunContext snapshot/merge isolation-7. Browser isolation key format-8. YAML schema passthrough-9. Batch sizing logic-10. Quota-enforced fallback to sequential-11. Concurrency slot release on success and failure-12. YAML round-trip through workflow_definition_converter-13. Browser cleanup verification-"""--from datetime import UTC, datetime-from unittest.mock import AsyncMock, MagicMock, patch--import pytest--from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext-from skyvern.forge.sdk.workflow.models.block import (- BlockStatus,- BlockType,- ForLoopBlock,- LoopBlockExecutedResult,- TaskBlock,-)-from skyvern.forge.sdk.workflow.models.parameter import OutputParameter-from skyvern.schemas.workflows import BlockResult, ForLoopBlockYAML, TaskBlockYAML, WorkflowDefinitionYAML--_NOW = datetime.now(UTC)---# ----------------------------------------------------------------------------# Helpers-# ------------------------------------------------------------------------------def _make_failure_block_result(label: str = "failure") -> BlockResult:- """Create a real BlockResult for failed iteration testing."""- return BlockResult(- success=False,- output_parameter=_make_output_parameter(label),- status=BlockStatus.failed,- failure_reason="test failure",- )---def _make_output_parameter(label: str) -> OutputParameter:- return OutputParameter(- output_parameter_id=f"op_{label}",- key=f"{label}_output",- workflow_id="wf_test",- created_at=_NOW,- modified_at=_NOW,- )---def _make_task_block(label: str) -> TaskBlock:- return TaskBlock(- label=label,- block_type=BlockType.TASK,- output_parameter=_make_output_parameter(label),- url="https://example.com",- )---def _make_for_loop_block(- label: str = "loop_block",- loop_blocks: list | None = None,- max_concurrency: int | None = None,- next_loop_on_failure: bool = False,-) -> ForLoopBlock:- blocks = loop_blocks or [_make_task_block("inner_task")]- return ForLoopBlock(- label=label,- block_type=BlockType.FOR_LOOP,- output_parameter=_make_output_parameter(label),- loop_blocks=blocks,- max_concurrency=max_concurrency,- next_loop_on_failure=next_loop_on_failure,- )---# ----------------------------------------------------------------------------# max_concurrency field validation-# ------------------------------------------------------------------------------class TestMaxConcurrencyValidation:- def test_none_is_preserved(self) -> None:- block = _make_for_loop_block(max_concurrency=None)- assert block.max_concurrency is None-- def test_one_is_preserved(self) -> None:- block = _make_for_loop_block(max_concurrency=1)- assert block.max_concurrency == 1-- def test_valid_value_preserved(self) -> None:- block = _make_for_loop_block(max_concurrency=5)- assert block.max_concurrency == 5-- def test_clamped_to_min_one(self) -> None:- block = _make_for_loop_block(max_concurrency=0)- assert block.max_concurrency == 1-- def test_clamped_negative_to_one(self) -> None:- block = _make_for_loop_block(max_concurrency=-5)- assert block.max_concurrency == 1-- def test_clamped_to_max_twenty(self) -> None:- block = _make_for_loop_block(max_concurrency=100)- assert block.max_concurrency == 20-- def test_twenty_is_preserved(self) -> None:- block = _make_for_loop_block(max_concurrency=20)- assert block.max_concurrency == 20---# ----------------------------------------------------------------------------# YAML schema passthrough-# ------------------------------------------------------------------------------class TestForLoopBlockYAMLMaxConcurrency:- def test_yaml_accepts_max_concurrency(self) -> None:- yaml_block = ForLoopBlockYAML(- label="test_loop",- loop_blocks=[TaskBlockYAML(label="inner", url="https://example.com")],- max_concurrency=5,- )- assert yaml_block.max_concurrency == 5-- def test_yaml_defaults_to_none(self) -> None:- yaml_block = ForLoopBlockYAML(- label="test_loop",- loop_blocks=[TaskBlockYAML(label="inner", url="https://example.com")],- )- assert yaml_block.max_concurrency is None---# ----------------------------------------------------------------------------# WorkflowRunContext snapshot/merge-# ------------------------------------------------------------------------------class TestWorkflowRunContextSnapshot:- def _make_context(self) -> WorkflowRunContext:- ctx = WorkflowRunContext(- workflow_title="Test",- workflow_id="wf_1",- workflow_permanent_id="wpid_1",- workflow_run_id="wr_1",- aws_client=MagicMock(),- )- ctx.values = {"key1": "original_value", "key2": [1, 2, 3]}- ctx.blocks_metadata = {"block_a": {"current_index": 0}}- ctx.parameters = {"param1": MagicMock()}- ctx.secrets = {"secret1": "s3cr3t"}- ctx.organization_id = "org_1"- return ctx-- def test_snapshot_deep_copies_values(self) -> None:- ctx = self._make_context()- snapshot = ctx.create_iteration_snapshot(0)-- # Modifying snapshot values should not affect original- snapshot.values["key1"] = "modified"- snapshot.values["key2"].append(4)-- assert ctx.values["key1"] == "original_value"- assert ctx.values["key2"] == [1, 2, 3]-- def test_snapshot_shallow_copies_parameters_and_secrets(self) -> None:- ctx = self._make_context()- snapshot = ctx.create_iteration_snapshot(0)-- # Parameters and secrets dicts are shallow-copied so a future code- # path that mutates them inside a loop block can't leak across- # iterations. Values inside are still shared (read-only contract).- assert snapshot.parameters is not ctx.parameters- assert snapshot.parameters == ctx.parameters- assert snapshot.secrets is not ctx.secrets- assert snapshot.secrets == ctx.secrets-- def test_snapshot_deep_copies_blocks_metadata(self) -> None:- ctx = self._make_context()- snapshot = ctx.create_iteration_snapshot(0)-- snapshot.blocks_metadata["block_a"]["current_index"] = 99-- assert ctx.blocks_metadata["block_a"]["current_index"] == 0-- def test_snapshot_preserves_immutable_fields(self) -> None:- ctx = self._make_context()- snapshot = ctx.create_iteration_snapshot(0)-- assert snapshot.workflow_run_id == ctx.workflow_run_id- assert snapshot.organization_id == ctx.organization_id- assert snapshot.workflow_id == ctx.workflow_id-- def test_merge_iteration_results_preserves_order(self) -> None:- ctx = self._make_context()-- # Create two snapshots with different values- snap1 = ctx.create_iteration_snapshot(0)- snap1.values["iter_0_result"] = "result_0"- snap1.blocks_metadata["block_iter_0"] = {"done": True}-- snap2 = ctx.create_iteration_snapshot(1)- snap2.values["iter_1_result"] = "result_1"- snap2.blocks_metadata["block_iter_1"] = {"done": True}-- # Merge in reverse order — should still apply in index order- ctx.merge_iteration_results([(1, snap2), (0, snap1)])-- assert ctx.values["iter_0_result"] == "result_0"- assert ctx.values["iter_1_result"] == "result_1"- assert "block_iter_0" in ctx.blocks_metadata- assert "block_iter_1" in ctx.blocks_metadata-- def test_merge_later_iteration_overwrites_earlier_on_collision(self) -> None:- ctx = self._make_context()-- snap0 = ctx.create_iteration_snapshot(0)- snap0.values["shared_key"] = "from_iter_0"-- snap1 = ctx.create_iteration_snapshot(1)- snap1.values["shared_key"] = "from_iter_1"-- ctx.merge_iteration_results([(0, snap0), (1, snap1)])-- # Later iteration (idx=1) should win- assert ctx.values["shared_key"] == "from_iter_1"---# ----------------------------------------------------------------------------# Browser iteration key format-# ------------------------------------------------------------------------------class TestBrowserIterationKey:- def test_key_format(self) -> None:- from skyvern.constants import loop_iteration_key-- assert loop_iteration_key("wr_abc123", 5) == "wr_abc123__iter_5"-- def test_key_format_zero(self) -> None:- from skyvern.constants import loop_iteration_key-- assert loop_iteration_key("wr_xyz", 0) == "wr_xyz__iter_0"---# ----------------------------------------------------------------------------# Sequential path unchanged-# ------------------------------------------------------------------------------class TestSequentialPathUnchanged:- """Verify that max_concurrency=None and max_concurrency=1 use the sequential path."""-- @pytest.mark.asyncio- async def test_none_concurrency_does_not_call_parallel(self) -> None:- block = _make_for_loop_block(max_concurrency=None)- # Mock _execute_loop_parallel to verify it's NOT called- mock_parallel = AsyncMock()- object.__setattr__(block, "_execute_loop_parallel", mock_parallel)-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.get_value.return_value = None- mock_context.has_value.return_value = False-- # The sequential path will fail because we haven't mocked everything,- # but _execute_loop_parallel should NOT be called- try:- await block.execute_loop_helper(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b"],- )- except Exception:- pass-- mock_parallel.assert_not_called()-- @pytest.mark.asyncio- async def test_concurrency_one_does_not_call_parallel(self) -> None:- block = _make_for_loop_block(max_concurrency=1)- mock_parallel = AsyncMock()- object.__setattr__(block, "_execute_loop_parallel", mock_parallel)-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.get_value.return_value = None- mock_context.has_value.return_value = False-- try:- await block.execute_loop_helper(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b"],- )- except Exception:- pass-- mock_parallel.assert_not_called()---# ----------------------------------------------------------------------------# Parallel dispatch-# ------------------------------------------------------------------------------class TestParallelDispatch:- """Verify that max_concurrency > 1 dispatches to _execute_loop_parallel."""-- @pytest.mark.asyncio- async def test_concurrency_gt_one_calls_parallel(self) -> None:- block = _make_for_loop_block(max_concurrency=3)-- expected_result = LoopBlockExecutedResult(- outputs_with_loop_values=[],- block_outputs=[],- last_block=None,- )- mock_parallel = AsyncMock(return_value=expected_result)- object.__setattr__(block, "_execute_loop_parallel", mock_parallel)-- mock_context = MagicMock(spec=WorkflowRunContext)-- result = await block.execute_loop_helper(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c"],- )-- mock_parallel.assert_called_once()- assert result is expected_result-- @pytest.mark.asyncio- async def test_parallel_passes_granted_concurrency(self) -> None:- """Verify granted_concurrency kwarg is forwarded from execute_loop_helper."""- block = _make_for_loop_block(max_concurrency=5)-- expected_result = LoopBlockExecutedResult(- outputs_with_loop_values=[],- block_outputs=[],- last_block=None,- )- mock_parallel = AsyncMock(return_value=expected_result)- object.__setattr__(block, "_execute_loop_parallel", mock_parallel)-- mock_context = MagicMock(spec=WorkflowRunContext)-- await block.execute_loop_helper(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b"],- )-- call_kwargs = mock_parallel.call_args.kwargs- assert call_kwargs["granted_concurrency"] == 5---class TestBatchSizing:- """Verify that _execute_loop_parallel processes iterations in correct batch sizes."""-- @pytest.mark.asyncio- async def test_single_batch_when_values_lte_concurrency(self) -> None:- """3 values with concurrency=5 should produce a single batch of 3."""- block = _make_for_loop_block(max_concurrency=5)- gather_calls: list[int] = []-- async def tracking_gather(*coros_or_futures, return_exceptions=False):- gather_calls.append(len(coros_or_futures))- # Return successful mock results for each coroutine- results = []- for coro in coros_or_futures:- try:- # Cancel the coroutine since we can't actually run them- coro.close()- except Exception:- pass- # Return a successful iteration result tuple- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((len(results), [], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = tracking_gather- mock_skyvern_ctx.current.return_value = None-- await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c"],- granted_concurrency=5,- )-- assert len(gather_calls) == 1- assert gather_calls[0] == 3 # single batch of 3-- @pytest.mark.asyncio- async def test_multiple_batches_when_values_gt_concurrency(self) -> None:- """7 values with concurrency=3 should produce 3 batches: [3, 3, 1]."""- block = _make_for_loop_block(max_concurrency=3)- gather_calls: list[int] = []-- async def tracking_gather(*coros_or_futures, return_exceptions=False):- gather_calls.append(len(coros_or_futures))- results = []- for coro in coros_or_futures:- try:- coro.close()- except Exception:- pass- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((len(results), [], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = tracking_gather- mock_skyvern_ctx.current.return_value = None-- await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c", "d", "e", "f", "g"],- granted_concurrency=3,- )-- assert len(gather_calls) == 3- assert gather_calls == [3, 3, 1]---# ----------------------------------------------------------------------------# Parallel error handling-# ------------------------------------------------------------------------------class TestParallelErrorHandling:- """Verify error handling in _execute_loop_parallel."""-- @pytest.mark.asyncio- async def test_failed_iteration_with_next_loop_on_failure_continues(self) -> None:- """When next_loop_on_failure=True, one exception doesn't stop the loop."""- block = _make_for_loop_block(max_concurrency=3, next_loop_on_failure=True)-- async def mock_gather(*coros_or_futures, return_exceptions=False):- results = []- for i, coro in enumerate(coros_or_futures):- try:- coro.close()- except Exception:- pass- if i == 1:- # Second iteration raises- results.append(RuntimeError("iteration 1 failed"))- else:- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((i, [{"output": f"result_{i}"}], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- # build_block_result needs to return a mock BlockResult- object.__setattr__(block, "build_block_result", AsyncMock(return_value=_make_failure_block_result()))-- result = await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c"],- granted_concurrency=3,- )-- # All 3 iterations processed: 2 success + 1 failure- assert len(result.outputs_with_loop_values) == 3- # Failure produces empty outputs list- assert result.outputs_with_loop_values[1] == []-- @pytest.mark.asyncio- async def test_failed_iteration_without_next_loop_on_failure_stops(self) -> None:- """When next_loop_on_failure=False, first exception stops the loop early."""- block = _make_for_loop_block(max_concurrency=3, next_loop_on_failure=False)-- async def mock_gather(*coros_or_futures, return_exceptions=False):- results = []- for i, coro in enumerate(coros_or_futures):- try:- coro.close()- except Exception:- pass- if i == 0:- # First iteration (index 0) returns success- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((i, [{"output": "ok"}], [], None, mock_ctx))- elif i == 1:- # Second iteration (index 1) raises- results.append(RuntimeError("iteration 1 failed"))- else:- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((i, [{"output": "ok"}], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- object.__setattr__(block, "build_block_result", AsyncMock(return_value=_make_failure_block_result()))-- result = await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c"],- granted_concurrency=3,- )-- # Stops after the batch finishes, but all 3 iteration results are- # captured first (idx 0 success + idx 1 failure + idx 2 success).- # asyncio.gather already ran them to completion, so dropping the- # later successes would silently lose data the browser produced.- assert len(result.outputs_with_loop_values) == 3-- @pytest.mark.asyncio- async def test_all_iterations_fail_with_next_loop_on_failure(self) -> None:- """Edge case: all iterations fail but loop continues due to next_loop_on_failure."""- block = _make_for_loop_block(max_concurrency=3, next_loop_on_failure=True)-- async def mock_gather(*coros_or_futures, return_exceptions=False):- results = []- for coro in coros_or_futures:- try:- coro.close()- except Exception:- pass- results.append(RuntimeError("failed"))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- object.__setattr__(block, "build_block_result", AsyncMock(return_value=_make_failure_block_result()))-- result = await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c"],- granted_concurrency=3,- )-- # All 3 iterations produced output (all failures)- assert len(result.outputs_with_loop_values) == 3- # All outputs are empty lists (failure case)- assert all(o == [] for o in result.outputs_with_loop_values)---class TestWorkflowRunContextSnapshotIsolation:- """Additional snapshot/merge isolation tests for parallel loops."""-- def _make_context(self) -> WorkflowRunContext:- ctx = WorkflowRunContext(- workflow_title="Test",- workflow_id="wf_1",- workflow_permanent_id="wpid_1",- workflow_run_id="wr_1",- aws_client=MagicMock(),- )- ctx.values = {"shared": "original"}- ctx.blocks_metadata = {"block_a": {"idx": 0}}- ctx.workflow_run_outputs = {"output_a": "val_a"}- ctx.parameters = {"p1": MagicMock()}- ctx.secrets = {"s1": "secret"}- ctx.organization_id = "org_1"- return ctx-- def test_snapshot_deep_copies_workflow_run_outputs(self) -> None:- ctx = self._make_context()- snapshot = ctx.create_iteration_snapshot(0)-- snapshot.workflow_run_outputs["output_b"] = "val_b"-- assert "output_b" not in ctx.workflow_run_outputs-- def test_multiple_snapshots_are_independent(self) -> None:- ctx = self._make_context()- snap0 = ctx.create_iteration_snapshot(0)- snap1 = ctx.create_iteration_snapshot(1)-- snap0.values["only_in_0"] = True- snap1.values["only_in_1"] = True-- assert "only_in_1" not in snap0.values- assert "only_in_0" not in snap1.values-- def test_merge_workflow_run_outputs(self) -> None:- ctx = self._make_context()- snap0 = ctx.create_iteration_snapshot(0)- snap0.workflow_run_outputs["iter_0_out"] = "r0"-- snap1 = ctx.create_iteration_snapshot(1)- snap1.workflow_run_outputs["iter_1_out"] = "r1"-- ctx.merge_iteration_results([(0, snap0), (1, snap1)])-- assert ctx.workflow_run_outputs["iter_0_out"] == "r0"- assert ctx.workflow_run_outputs["iter_1_out"] == "r1"-- def test_merge_empty_snapshots_is_safe(self) -> None:- ctx = self._make_context()- original_values = dict(ctx.values)-- ctx.merge_iteration_results([])-- assert ctx.values == original_values---# ----------------------------------------------------------------------------# Browser cleanup verification-# ------------------------------------------------------------------------------class TestBrowserCleanup:- """Verify browser cleanup is called correctly in parallel loops."""-- @pytest.mark.asyncio- async def test_cleanup_called_for_each_batch(self) -> None:- """cleanup_loop_iterations should be called once per batch."""- block = _make_for_loop_block(max_concurrency=2)- cleanup_calls: list[list[int]] = []-- async def mock_gather(*coros_or_futures, return_exceptions=False):- results = []- for i, coro in enumerate(coros_or_futures):- try:- coro.close()- except Exception:- pass- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((i, [], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- async def track_cleanup(workflow_run_id, loop_indices, organization_id=None):- cleanup_calls.append(list(loop_indices))-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_app.BROWSER_MANAGER.cleanup_loop_iterations = track_cleanup- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["a", "b", "c", "d", "e"],- granted_concurrency=2,- )-- # 5 values / 2 concurrency = 3 batches- assert len(cleanup_calls) == 3- assert cleanup_calls[0] == [0, 1]- assert cleanup_calls[1] == [2, 3]- assert cleanup_calls[2] == [4]---# ----------------------------------------------------------------------------# YAML round-trip through workflow_definition_converter-# ------------------------------------------------------------------------------class TestYAMLRoundTrip:- """Verify max_concurrency survives YAML→block conversion."""-- def test_converter_preserves_max_concurrency(self) -> None:- from skyvern.forge.sdk.workflow.workflow_definition_converter import (- convert_workflow_definition,- )- from skyvern.schemas.workflows import WorkflowParameterYAML-- yaml_def = WorkflowDefinitionYAML(- version=2,- blocks=[- ForLoopBlockYAML(- label="parallel_loop",- loop_blocks=[TaskBlockYAML(label="inner_task", url="https://example.com")],- loop_over_parameter_key="items",- max_concurrency=5,- ),- ],- parameters=[- WorkflowParameterYAML(- key="items",- parameter_type="workflow",- workflow_parameter_type="json",- default_value=["a", "b"],- ),- ],- )-- wd = convert_workflow_definition(yaml_def, "wf_test")- assert len(wd.blocks) == 1- loop_block = wd.blocks[0]- assert isinstance(loop_block, ForLoopBlock)- assert loop_block.max_concurrency == 5-- def test_converter_preserves_none_max_concurrency(self) -> None:- from skyvern.forge.sdk.workflow.workflow_definition_converter import (- convert_workflow_definition,- )- from skyvern.schemas.workflows import WorkflowParameterYAML-- yaml_def = WorkflowDefinitionYAML(- version=2,- blocks=[- ForLoopBlockYAML(- label="sequential_loop",- loop_blocks=[TaskBlockYAML(label="inner_task", url="https://example.com")],- loop_over_parameter_key="items",- ),- ],- parameters=[- WorkflowParameterYAML(- key="items",- parameter_type="workflow",- workflow_parameter_type="json",- default_value=["a", "b"],- ),- ],- )-- wd = convert_workflow_definition(yaml_def, "wf_test")- loop_block = wd.blocks[0]- assert isinstance(loop_block, ForLoopBlock)- assert loop_block.max_concurrency is None---# ----------------------------------------------------------------------------# Single iteration edge case-# ------------------------------------------------------------------------------class TestSingleIterationEdgeCase:- """With only 1 iteration, parallel path should still work correctly."""-- @pytest.mark.asyncio- async def test_single_value_parallel(self) -> None:- block = _make_for_loop_block(max_concurrency=5)-- async def mock_gather(*coros_or_futures, return_exceptions=False):- results = []- for i, coro in enumerate(coros_or_futures):- try:- coro.close()- except Exception:- pass- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {"result": "single"}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((0, [{"output": "single_result"}], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- result = await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=["only_one"],- granted_concurrency=5,- )-- assert len(result.outputs_with_loop_values) == 1- assert result.outputs_with_loop_values[0] == [{"output": "single_result"}]---# ----------------------------------------------------------------------------# Iteration cap enforcement in parallel path-# ------------------------------------------------------------------------------class TestIterationCapParallel:- """Verify DEFAULT_MAX_LOOP_ITERATIONS is enforced in parallel path."""-- @pytest.mark.asyncio- async def test_values_capped_at_max_iterations(self) -> None:- from skyvern.forge.sdk.workflow.models.block import DEFAULT_MAX_LOOP_ITERATIONS-- block = _make_for_loop_block(max_concurrency=20)- gather_call_sizes: list[int] = []-- async def mock_gather(*coros_or_futures, return_exceptions=False):- gather_call_sizes.append(len(coros_or_futures))- results = []- for i, coro in enumerate(coros_or_futures):- try:- coro.close()- except Exception:- pass- mock_ctx = MagicMock(spec=WorkflowRunContext)- mock_ctx.values = {}- mock_ctx.blocks_metadata = {}- mock_ctx.workflow_run_outputs = {}- results.append((i, [], [], None, mock_ctx))- return results-- mock_context = MagicMock(spec=WorkflowRunContext)- mock_context.create_iteration_snapshot.return_value = MagicMock(- spec=WorkflowRunContext,- values={},- blocks_metadata={},- workflow_run_outputs={},- )-- # Create more values than the max- oversized_values = list(range(DEFAULT_MAX_LOOP_ITERATIONS + 50))-- with (- patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,- patch("skyvern.forge.sdk.workflow.models.block.asyncio") as mock_asyncio,- patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,- ):- mock_app.AGENT_FUNCTION = AsyncMock()- mock_app.AGENT_FUNCTION.release_parallel_loop_quota = AsyncMock()- mock_app.BROWSER_MANAGER = AsyncMock()- mock_asyncio.create_task = lambda coro: coro- mock_asyncio.gather = mock_gather- mock_skyvern_ctx.current.return_value = None-- await block._execute_loop_parallel(- workflow_run_id="wr_test",- workflow_run_block_id="wrb_test",- workflow_run_context=mock_context,- loop_over_values=oversized_values,- granted_concurrency=20,- )-- total_processed = sum(gather_call_sizes)- assert total_processed == DEFAULT_MAX_LOOP_ITERATIONSdiff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex 251878923..3d9ceb069 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -6,15 +6,10 @@ behind `if not browser_session_id:`, causing PBS workflow runs to skip the cacheon every call and re-invoke navigate_to_url() on every step."""-import asyncio-import timefrom unittest.mock import AsyncMock, MagicMock, patchimport pytest-from skyvern.constants import loop_iteration_key-from skyvern.forge.sdk.core import skyvern_context-from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.webeye.real_browser_manager import RealBrowserManager@@ -136,641 +131,3 @@ async def test_non_pbs_workflow_run_inherits_parent_browser() -> None:# Both entries should be syncedassert manager.pages["wfr_child"] is parent_stateassert manager.pages["wfr_parent"] is parent_state---async def test_parallel_iteration_browser_returned_from_context() -> None:- """When SkyvernContext has an iteration browser_session_id, get_or_create_for_workflow_run- must return the pre-created iteration browser instead of creating a new one."""- manager = RealBrowserManager()- iteration_state = MagicMock()- iteration_state.get_working_page = AsyncMock(return_value=MagicMock(url="https://example.com"))- manager.pages["wr_abc__iter_0"] = iteration_state-- workflow_run = make_workflow_run("wr_abc")-- # Set up SkyvernContext with iteration browser_session_id- ctx = SkyvernContext(browser_session_id="wr_abc__iter_0")- skyvern_context.set(ctx)- try:- with patch("skyvern.webeye.real_browser_manager.app"):- result = await manager.get_or_create_for_workflow_run(- workflow_run=workflow_run,- url="https://example.com",- )- assert result is iteration_state- finally:- skyvern_context.reset()---async def test_parallel_iteration_browser_not_used_without_context() -> None:- """Without a parallel iteration context, normal lookup applies even if- iteration keys exist in pages."""- manager = RealBrowserManager()- iteration_state = MagicMock()- normal_state = MagicMock()- manager.pages["wr_abc__iter_0"] = iteration_state- manager.pages["wr_abc"] = normal_state-- workflow_run = make_workflow_run("wr_abc")-- # No SkyvernContext set (or context without iteration marker)- skyvern_context.reset()- with patch("skyvern.webeye.real_browser_manager.app"):- result = await manager.get_or_create_for_workflow_run(- workflow_run=workflow_run,- url="https://example.com",- )- assert result is normal_state---async def test_parallel_iteration_different_iterations_get_different_browsers() -> None:- """Each parallel iteration should get its own isolated browser state."""- manager = RealBrowserManager()- iter0_state = MagicMock()- iter0_state.get_working_page = AsyncMock(return_value=MagicMock(url="https://example.com"))- iter1_state = MagicMock()- iter1_state.get_working_page = AsyncMock(return_value=MagicMock(url="https://example.com"))- manager.pages["wr_abc__iter_0"] = iter0_state- manager.pages["wr_abc__iter_1"] = iter1_state-- workflow_run = make_workflow_run("wr_abc")-- # Iteration 0- ctx0 = SkyvernContext(browser_session_id="wr_abc__iter_0")- skyvern_context.set(ctx0)- try:- with patch("skyvern.webeye.real_browser_manager.app"):- result0 = await manager.get_or_create_for_workflow_run(- workflow_run=workflow_run,- url="https://example.com",- )- finally:- skyvern_context.reset()-- # Iteration 1- ctx1 = SkyvernContext(browser_session_id="wr_abc__iter_1")- skyvern_context.set(ctx1)- try:- with patch("skyvern.webeye.real_browser_manager.app"):- result1 = await manager.get_or_create_for_workflow_run(- workflow_run=workflow_run,- url="https://example.com",- )- finally:- skyvern_context.reset()-- assert result0 is iter0_state- assert result1 is iter1_state- assert result0 is not result1---# ----------------------------------------------------------------------------# Parallel-loop browser launch must not be serialized on a lock.-# ------------------------------------------------------------------------------def _make_fake_browser_state() -> MagicMock:- state = MagicMock()- state.get_or_create_page = AsyncMock(return_value=None)- state.close = AsyncMock(return_value=None)- return state---async def test_loop_iteration_launches_overlap() -> None:- """Concurrent get_or_create_for_loop_iteration calls with distinct keys- must overlap in wall-clock time. If launches serialize under a lock,- N * per_launch is observed; this asserts < 2 * per_launch for 4 calls.- """- manager = RealBrowserManager()- per_launch = 0.3- call_count = 0-- async def fake_create(**_kwargs: object) -> MagicMock:- nonlocal call_count- call_count += 1- await asyncio.sleep(per_launch)- return _make_fake_browser_state()-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=fake_create):- start = time.perf_counter()- results = await asyncio.gather(- *(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_overlap",- loop_idx=i,- organization_id="org_test",- )- for i in range(4)- )- )- elapsed = time.perf_counter() - start-- assert call_count == 4, f"expected 4 launches, got {call_count}"- assert len(manager.pages) == 4- # Serialized would be ~1.2s; parallel ~0.3s. 3x gives jitter slack for- # slow CI runners (GitHub Actions can stall 500ms+ on GC). call_count- # already proves correctness; this bound is a secondary signal.- assert elapsed < per_launch * 3, (- f"concurrent launches did not overlap: elapsed={elapsed:.3f}s "- f"(per_launch={per_launch}s, expected < {per_launch * 3}s)"- )- # Every returned state must be a distinct object.- assert len({id(r) for r in results}) == 4---async def test_loop_iteration_single_flight_same_key() -> None:- """Concurrent callers for the same key must collapse to a single- underlying browser launch (single-flight)."""- manager = RealBrowserManager()- call_count = 0- created_state = _make_fake_browser_state()-- async def fake_create(**_kwargs: object) -> MagicMock:- nonlocal call_count- call_count += 1- await asyncio.sleep(0.1)- return created_state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=fake_create):- results = await asyncio.gather(- *(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_single_flight",- loop_idx=7,- organization_id="org_test",- )- for _ in range(4)- )- )-- assert call_count == 1- assert len(manager.pages) == 1- assert all(r is created_state for r in results)---async def test_loop_iteration_owner_cancellation_does_not_orphan_inflight() -> None:- """If the launch-owning task is cancelled mid-launch, subsequent callers- for the same key must not await a stale in-flight future forever — the- in-flight entry must be cleared so the next caller starts a fresh- launch."""- manager = RealBrowserManager()- launch_started = asyncio.Event()- call_count = 0-- async def hanging_create(**_kwargs: object) -> MagicMock:- nonlocal call_count- call_count += 1- launch_started.set()- # Hang until cancelled.- await asyncio.sleep(10)- return _make_fake_browser_state() # pragma: no cover-- async def fast_create(**_kwargs: object) -> MagicMock:- nonlocal call_count- call_count += 1- return _make_fake_browser_state()-- # First call: owner, gets cancelled while _create_browser_state hangs.- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=hanging_create):- owner_task = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_cancel",- loop_idx=0,- organization_id="org_test",- )- )- await launch_started.wait()- owner_task.cancel()- with pytest.raises(asyncio.CancelledError):- await owner_task-- # In-flight entry must be cleared so a retry is not stuck waiting on it.- assert "wr_cancel__iter_0" not in manager._loop_iteration_inflight- assert "wr_cancel__iter_0" not in manager.pages-- # Retry succeeds with a fresh launch.- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=fast_create):- result = await manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_cancel",- loop_idx=0,- organization_id="org_test",- )-- assert result is not None- assert call_count == 2- assert "wr_cancel__iter_0" in manager.pages---async def test_loop_iteration_waiter_cancellation_does_not_cancel_owner() -> None:- """Cancelling one waiter on a single-flight key must not cancel the- shared launch future — otherwise the owner and every other waiter- would fan out into CancelledError."""- manager = RealBrowserManager()- launch_started = asyncio.Event()- call_count = 0- created_state = _make_fake_browser_state()-- async def slow_create(**_kwargs: object) -> MagicMock:- nonlocal call_count- call_count += 1- launch_started.set()- await asyncio.sleep(0.2)- return created_state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=slow_create):- owner = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_waiter_cancel",- loop_idx=0,- organization_id="org_test",- )- )- await launch_started.wait()-- waiter_to_cancel = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_waiter_cancel",- loop_idx=0,- organization_id="org_test",- )- )- surviving_waiter = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_waiter_cancel",- loop_idx=0,- organization_id="org_test",- )- )- # Let the waiters reach their shielded await.- await asyncio.sleep(0)- waiter_to_cancel.cancel()- with pytest.raises(asyncio.CancelledError):- await waiter_to_cancel-- owner_result = await owner- surviving_result = await surviving_waiter-- assert call_count == 1- assert owner_result is created_state- assert surviving_result is created_state---async def test_cleanup_during_launch_discards_orphan_state() -> None:- """If cleanup_loop_iterations runs while a launch is still in-flight- for the same key, the owner must close the browser state it created- instead of publishing it into self.pages — otherwise an orphaned- Playwright context is leaked."""- manager = RealBrowserManager()- launch_started = asyncio.Event()- cleanup_done = asyncio.Event()- created_state = _make_fake_browser_state()-- async def gated_create(**_kwargs: object) -> MagicMock:- launch_started.set()- await cleanup_done.wait()- return created_state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=gated_create):- owner = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_race",- loop_idx=0,- organization_id="org_test",- )- )- await launch_started.wait()- # Cleanup runs mid-launch.- await manager.cleanup_loop_iterations(- workflow_run_id="wr_race",- loop_indices=[0],- organization_id="org_test",- )- cleanup_done.set()- with pytest.raises(RuntimeError, match="cleaned up during launch"):- await owner-- assert "wr_race__iter_0" not in manager.pages- assert "wr_race__iter_0" not in manager._loop_iteration_inflight- created_state.close.assert_awaited()---async def test_cleanup_during_launch_does_not_disturb_replacement_owner() -> None:- """After cleanup cancels owner A's in-flight future, a replacement- owner B may register a new future for the same key before owner A- finishes unwinding. Owner A must not pop B's future (Codex P1)."""- manager = RealBrowserManager()- owner_a_started = asyncio.Event()- owner_a_can_finish = asyncio.Event()- states: list[MagicMock] = []-- async def gated_create(**_kwargs: object) -> MagicMock:- state = _make_fake_browser_state()- states.append(state)- if len(states) == 1:- owner_a_started.set()- await owner_a_can_finish.wait()- return state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=gated_create):- owner_a = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_replace",- loop_idx=0,- organization_id="org_test",- )- )- await owner_a_started.wait()-- # Cleanup cancels owner A's in-flight future.- await manager.cleanup_loop_iterations(- workflow_run_id="wr_replace",- loop_indices=[0],- organization_id="org_test",- )-- # Owner B registers a brand-new future for the same key.- owner_b = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_replace",- loop_idx=0,- organization_id="org_test",- )- )- # Let owner B reach its await on _create_browser_state (second call).- await asyncio.sleep(0)-- # Now let owner A finish unwinding. It must not disturb owner B's- # inflight entry.- owner_a_can_finish.set()- with pytest.raises(RuntimeError, match="cleaned up during launch"):- await owner_a-- result_b = await owner_b-- assert result_b is states[1]- assert manager.pages["wr_replace__iter_0"] is states[1]- assert "wr_replace__iter_0" not in manager._loop_iteration_inflight- # Owner A's orphaned state was closed; owner B's state is intact.- states[0].close.assert_awaited()- states[1].close.assert_not_awaited()---async def test_owner_failure_does_not_disturb_replacement_inflight() -> None:- """Owner A's failure path must identity-check before popping inflight.- If cleanup cancelled A and owner B registered a replacement future,- A's failure must not disturb B's entry (Codex P1)."""- manager = RealBrowserManager()- owner_a_started = asyncio.Event()- owner_a_can_finish = asyncio.Event()- states: list[MagicMock] = []-- class LaunchFailure(RuntimeError):- pass-- async def gated_create(**_kwargs: object) -> MagicMock:- if not states:- states.append(object()) # sentinel for "A ran"- owner_a_started.set()- await owner_a_can_finish.wait()- raise LaunchFailure("A failed")- state = _make_fake_browser_state()- states.append(state)- return state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=gated_create):- owner_a = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_fail_race",- loop_idx=0,- organization_id="org_test",- )- )- await owner_a_started.wait()-- await manager.cleanup_loop_iterations(- workflow_run_id="wr_fail_race",- loop_indices=[0],- organization_id="org_test",- )-- owner_b = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_fail_race",- loop_idx=0,- organization_id="org_test",- )- )- await asyncio.sleep(0)-- owner_a_can_finish.set()- # Owner A raises — it was cancelled by cleanup, so CancelledError- # propagates first (cancellation takes precedence over the- # subsequent LaunchFailure); either way it must not touch B.- with pytest.raises(BaseException):- await owner_a-- result_b = await owner_b-- assert result_b is states[1]- assert manager.pages["wr_fail_race__iter_0"] is states[1]- assert "wr_fail_race__iter_0" not in manager._loop_iteration_inflight- states[1].close.assert_not_awaited()---async def test_close_during_launch_does_not_orphan_state() -> None:- """RealBrowserManager.close() while an owner is mid-launch must- cancel the owner's in-flight future and cause the owner to close- its own newly-created state instead of publishing it into- self.pages after close() has already reset the dict."""- manager = RealBrowserManager()- launch_started = asyncio.Event()- close_done = asyncio.Event()- created_state = _make_fake_browser_state()-- async def gated_create(**_kwargs: object) -> MagicMock:- launch_started.set()- await close_done.wait()- return created_state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=gated_create):- owner = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_close_race",- loop_idx=0,- organization_id="org_test",- )- )- await launch_started.wait()-- await manager.close()- close_done.set()- with pytest.raises(RuntimeError, match="cleaned up during launch"):- await owner-- assert "wr_close_race__iter_0" not in manager.pages- assert "wr_close_race__iter_0" not in manager._loop_iteration_inflight- created_state.close.assert_awaited()---async def test_cleanup_during_launch_surfaces_exception_to_waiters() -> None:- """Codex P2 regression: cleanup_loop_iterations must surface a regular- Exception (not CancelledError) to same-key waiters. CancelledError is a- BaseException and would bypass `_execute_single_iteration_parallel`'s- `except Exception` handler and `_execute_loop_parallel`'s- `isinstance(result, Exception)` dispatch, crashing the batch unpack."""- manager = RealBrowserManager()- owner_started = asyncio.Event()- cleanup_done = asyncio.Event()- created_state = _make_fake_browser_state()-- async def gated_create(**_kwargs: object) -> MagicMock:- owner_started.set()- await cleanup_done.wait()- return created_state-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=gated_create):- owner = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_waiter_exc",- loop_idx=0,- organization_id="org_test",- )- )- await owner_started.wait()-- # Attach waiters AFTER the in-flight future is registered but- # BEFORE cleanup runs, so they have a shielded reference that- # cleanup's set_exception must reach.- waiters = [- asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_waiter_exc",- loop_idx=0,- organization_id="org_test",- )- )- for _ in range(2)- ]- # Yield so waiters reach `await asyncio.shield(inflight_future)`.- await asyncio.sleep(0)-- await manager.cleanup_loop_iterations(- workflow_run_id="wr_waiter_exc",- loop_indices=[0],- organization_id="org_test",- )- cleanup_done.set()-- for waiter in waiters:- with pytest.raises(Exception) as excinfo:- await waiter- # Critically, not a CancelledError — that would bypass the- # batch handler and crash the success-unpack path.- assert not isinstance(excinfo.value, asyncio.CancelledError), (- "waiter must receive a regular Exception, not CancelledError"- )- assert "cleaned up during launch" in str(excinfo.value)-- with pytest.raises(RuntimeError, match="cleaned up during launch"):- await owner-- assert "wr_waiter_exc__iter_0" not in manager.pages- assert "wr_waiter_exc__iter_0" not in manager._loop_iteration_inflight---async def test_owner_cancellation_surfaces_exception_to_waiters() -> None:- """If the launch-owning task is cancelled while same-key waiters are- attached, waiters must receive a regular Exception (not CancelledError)- so the batch driver's `isinstance(result, Exception)` dispatch catches- them instead of the success-unpack path crashing on a BaseException."""- manager = RealBrowserManager()- owner_started = asyncio.Event()-- async def hanging_create(**_kwargs: object) -> MagicMock:- owner_started.set()- await asyncio.sleep(10)- return _make_fake_browser_state() # pragma: no cover-- with patch.object(RealBrowserManager, "_create_browser_state", side_effect=hanging_create):- owner = asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_owner_cancel_waiters",- loop_idx=0,- organization_id="org_test",- )- )- await owner_started.wait()-- waiters = [- asyncio.create_task(- manager.get_or_create_for_loop_iteration(- workflow_run_id="wr_owner_cancel_waiters",- loop_idx=0,- organization_id="org_test",- )- )- for _ in range(2)- ]- # Let waiters reach their shielded await.- await asyncio.sleep(0)-- owner.cancel()- with pytest.raises(asyncio.CancelledError):- await owner-- for waiter in waiters:- with pytest.raises(Exception) as excinfo:- await waiter- assert not isinstance(excinfo.value, asyncio.CancelledError), (- "waiter must receive a regular Exception when owner is cancelled"- )- assert "launch cancelled" in str(excinfo.value)-- assert "wr_owner_cancel_waiters__iter_0" not in manager._loop_iteration_inflight- assert "wr_owner_cancel_waiters__iter_0" not in manager.pages---async def test_cleanup_loop_iterations_runs_close_in_parallel() -> None:- """cleanup_loop_iterations must close browser contexts in parallel.- Sequential close of 4 entries at 0.2s each would take ~0.8s; parallel- should finish in ~0.2s. 0.4s ceiling gives CI slack."""- manager = RealBrowserManager()- per_close = 0.2-- async def slow_close() -> None:- await asyncio.sleep(per_close)-- loop_indices = [0, 1, 2, 3]- for idx in loop_indices:- state = _make_fake_browser_state()- state.close = AsyncMock(side_effect=slow_close)- manager.pages[loop_iteration_key("wr_cleanup", idx)] = state-- start = time.perf_counter()- await manager.cleanup_loop_iterations(- workflow_run_id="wr_cleanup",- loop_indices=loop_indices,- organization_id="org_test",- )- elapsed = time.perf_counter() - start-- assert len(manager.pages) == 0- # 3x for CI jitter slack; len(manager.pages) == 0 is the real correctness check.- assert elapsed < per_close * 3, (- f"cleanup did not run closes in parallel: elapsed={elapsed:.3f}s "- f"(per_close={per_close}s, expected < {per_close * 3}s)"- )