MCPcopy
hub / github.com/github/spec-kit / WhileStep

Class WhileStep

src/specify_cli/workflows/steps/while_loop/__init__.py:11–71  ·  view source on GitHub ↗

Repeat nested steps while condition is truthy. Evaluates condition *before* each iteration. If falsy on first check, the body never runs. ``max_iterations`` is an optional safety cap (defaults to 10 if omitted).

Source from the content-addressed store, hash-verified

9
10
11class WhileStep(StepBase):
12 """Repeat nested steps while condition is truthy.
13
14 Evaluates condition *before* each iteration. If falsy on first
15 check, the body never runs. ``max_iterations`` is an optional
16 safety cap (defaults to 10 if omitted).
17 """
18
19 type_key = "while"
20
21 def execute(self, config: dict[str, Any], context: StepContext) -> StepResult:
22 condition = config.get("condition", False)
23 max_iterations = config.get("max_iterations")
24 if max_iterations is None:
25 max_iterations = 10
26 nested_steps = config.get("steps", [])
27
28 result = evaluate_condition(condition, context)
29 if result:
30 return StepResult(
31 status=StepStatus.COMPLETED,
32 output={
33 "condition_result": True,
34 "max_iterations": max_iterations,
35 "loop_type": "while",
36 },
37 next_steps=nested_steps,
38 )
39
40 return StepResult(
41 status=StepStatus.COMPLETED,
42 output={
43 "condition_result": False,
44 "max_iterations": max_iterations,
45 "loop_type": "while",
46 },
47 )
48
49 def validate(self, config: dict[str, Any]) -> list[str]:
50 errors = super().validate(config)
51 if "condition" not in config:
52 errors.append(
53 f"While step {config.get('id', '?')!r} is missing "
54 f"'condition' field."
55 )
56 max_iter = config.get("max_iterations")
57 if max_iter is not None:
58 # bool is a subclass of int, so isinstance(True, int) is True and
59 # True < 1 is False; reject bools explicitly so `max_iterations: true`
60 # is a type error rather than a silent single iteration.
61 if isinstance(max_iter, bool) or not isinstance(max_iter, int) or max_iter < 1:
62 errors.append(
63 f"While step {config.get('id', '?')!r}: "
64 f"'max_iterations' must be an integer >= 1."
65 )
66 nested = config.get("steps", [])
67 if not isinstance(nested, list):
68 errors.append(

Calls

no outgoing calls