Execute a batch of independent steps concurrently. Uses asyncio.Semaphore to limit concurrency.
(
self,
batch: list[dict[str, Any]],
var_context: dict[str, Any],
)
| 681 | ) |
| 682 | |
| 683 | async def _execute_batch( |
| 684 | self, |
| 685 | batch: list[dict[str, Any]], |
| 686 | var_context: dict[str, Any], |
| 687 | ) -> list[StepResult]: |
| 688 | """Execute a batch of independent steps concurrently. |
| 689 | |
| 690 | Uses asyncio.Semaphore to limit concurrency. |
| 691 | """ |
| 692 | sem = asyncio.Semaphore(self._max_concurrency) |
| 693 | |
| 694 | async def _run_with_sem(step: dict[str, Any]) -> StepResult: |
| 695 | async with sem: |
| 696 | return await self._execute_step(step, var_context) |
| 697 | |
| 698 | tasks = [asyncio.create_task(_run_with_sem(step)) for step in batch] |
| 699 | results = await asyncio.gather(*tasks, return_exceptions=True) |
| 700 | |
| 701 | step_results: list[StepResult] = [] |
| 702 | for i, result in enumerate(results): |
| 703 | if isinstance(result, BaseException): |
| 704 | step = batch[i] |
| 705 | step_results.append( |
| 706 | StepResult( |
| 707 | step_index=step.get("index", "?"), |
| 708 | step_title=step.get("title", "Untitled"), |
| 709 | tool_name=step.get("tool", "none"), |
| 710 | success=False, |
| 711 | error=str(result), |
| 712 | ) |
| 713 | ) |
| 714 | else: |
| 715 | step_results.append(result) |
| 716 | return step_results |
| 717 | |
| 718 | async def _dry_run( |
| 719 | self, |
no test coverage detected