Execute body at least once, then check condition. Continues while condition is truthy. ``max_iterations`` is an optional safety cap (defaults to 10 if omitted). The first invocation always returns the nested steps for execution. The engine re-evaluates ``step_config['condition']``
| 8 | |
| 9 | |
| 10 | class DoWhileStep(StepBase): |
| 11 | """Execute body at least once, then check condition. |
| 12 | |
| 13 | Continues while condition is truthy. ``max_iterations`` is an |
| 14 | optional safety cap (defaults to 10 if omitted). |
| 15 | |
| 16 | The first invocation always returns the nested steps for execution. |
| 17 | The engine re-evaluates ``step_config['condition']`` after each |
| 18 | iteration to decide whether to loop again. |
| 19 | """ |
| 20 | |
| 21 | type_key = "do-while" |
| 22 | |
| 23 | def execute(self, config: dict[str, Any], context: StepContext) -> StepResult: |
| 24 | max_iterations = config.get("max_iterations") |
| 25 | if max_iterations is None: |
| 26 | max_iterations = 10 |
| 27 | nested_steps = config.get("steps", []) |
| 28 | condition = config.get("condition", "false") |
| 29 | |
| 30 | # Always execute body at least once; the engine layer evaluates |
| 31 | # `condition` after each iteration to decide whether to loop. |
| 32 | return StepResult( |
| 33 | status=StepStatus.COMPLETED, |
| 34 | output={ |
| 35 | "condition": condition, |
| 36 | "max_iterations": max_iterations, |
| 37 | "loop_type": "do-while", |
| 38 | }, |
| 39 | next_steps=nested_steps, |
| 40 | ) |
| 41 | |
| 42 | def validate(self, config: dict[str, Any]) -> list[str]: |
| 43 | errors = super().validate(config) |
| 44 | if "condition" not in config: |
| 45 | errors.append( |
| 46 | f"Do-while step {config.get('id', '?')!r} is missing " |
| 47 | f"'condition' field." |
| 48 | ) |
| 49 | max_iter = config.get("max_iterations") |
| 50 | if max_iter is not None: |
| 51 | # bool is a subclass of int, so isinstance(True, int) is True and |
| 52 | # True < 1 is False; reject bools explicitly so `max_iterations: true` |
| 53 | # is a type error rather than a silent single iteration. |
| 54 | if isinstance(max_iter, bool) or not isinstance(max_iter, int) or max_iter < 1: |
| 55 | errors.append( |
| 56 | f"Do-while step {config.get('id', '?')!r}: " |
| 57 | f"'max_iterations' must be an integer >= 1." |
| 58 | ) |
| 59 | nested = config.get("steps", []) |
| 60 | if not isinstance(nested, list): |
| 61 | errors.append( |
| 62 | f"Do-while step {config.get('id', '?')!r}: 'steps' must be a list." |
| 63 | ) |
| 64 | return errors |
no outgoing calls