Skip to content

Commit db64869

Browse files
jawwad-aliclaude
andauthored
fix(workflows): require a 'steps' body on while and do-while loops (#4149)
Both loop steps type-check `steps` ("must be a list") but never require it to be present, so an absent body silently becomes `[]`. `if` already requires `then`, and `fan-out` already requires both `items` and `step`. The mistype is unusually easy here because the fan-out step's own payload key is the singular `step:` while the loops use `steps:`. Writing `step:` on a `while` passed `specify workflow validate` with zero errors: A. while, body key typo'd as singular step: validate: [] execute : StepStatus.COMPLETED | next_steps = [] B. do-while, no steps at all: validate: [] execute : StepStatus.COMPLETED | next_steps = [] At run time the step reports COMPLETED while returning no `next_steps`, so the engine's `if result.next_steps:` block never fires and the loop the workflow is built around never runs even once. `DoWhileStep`'s own docstring promises "The first invocation always returns the nested steps for execution". Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent c256cea commit db64869

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎src/specify_cli/workflows/steps/do_while/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ def validate(self, config: dict[str, Any]) -> list[str]:
147147
f"Do-while step {config.get('id', '?')!r}: "
148148
f"'max_iterations' must be an integer >= 1."
149149
)
150+
if "steps" not in config:
151+
# This step's own docstring promises "The first invocation always
152+
# returns the nested steps for execution" -- with no body it
153+
# validated clean and then returned none, so the loop never ran even
154+
# once. See the matching guard in the ``while`` step.
155+
errors.append(
156+
f"Do-while step {config.get('id', '?')!r} is missing 'steps' field."
157+
)
150158
nested = config.get("steps", [])
151159
if not isinstance(nested, list):
152160
errors.append(

‎src/specify_cli/workflows/steps/while_loop/__init__.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ def validate(self, config: dict[str, Any]) -> list[str]:
156156
f"While step {config.get('id', '?')!r}: "
157157
f"'max_iterations' must be an integer >= 1."
158158
)
159+
if "steps" not in config:
160+
# A loop with no body is never what the author meant, but it used to
161+
# validate clean and then report COMPLETED at run time while
162+
# returning no next_steps -- so the engine's ``if result.next_steps:``
163+
# block never fired and the loop the workflow is built around never
164+
# ran once. The mistype is easy: fan-out's payload key is the
165+
# singular ``step:``, so writing ``step:`` on a ``while`` produced a
166+
# silent no-op. ``if`` already requires ``then`` and fan-out already
167+
# requires both ``items`` and ``step``; require a body here too.
168+
errors.append(
169+
f"While step {config.get('id', '?')!r} is missing 'steps' field."
170+
)
159171
nested = config.get("steps", [])
160172
if not isinstance(nested, list):
161173
errors.append(

‎tests/test_workflows.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,33 @@ def test_validate_missing_fields(self):
35603560
assert any("missing 'condition'" in e for e in errors)
35613561
# max_iterations is optional (defaults to 10)
35623562

3563+
def test_validate_requires_steps_body(self):
3564+
"""A while loop with no body must be rejected, not silently a no-op.
3565+
3566+
Without this, ``step:`` written instead of ``steps:`` -- an easy slip,
3567+
since fan-out's payload key really is the singular ``step:`` -- passed
3568+
``specify workflow validate`` with zero errors, and then reported
3569+
COMPLETED at run time while returning no ``next_steps``, so the loop
3570+
never ran even once.
3571+
"""
3572+
from specify_cli.workflows.base import StepContext, StepStatus
3573+
from specify_cli.workflows.steps.while_loop import WhileStep
3574+
3575+
step = WhileStep()
3576+
config = {
3577+
"id": "retry",
3578+
"condition": "true",
3579+
# The mistype: singular 'step' instead of 'steps'.
3580+
"step": {"id": "x", "type": "command", "command": "echo"},
3581+
}
3582+
errors = step.validate(config)
3583+
assert errors == ["While step 'retry' is missing 'steps' field."], errors
3584+
3585+
# Demonstrates why it matters: execution is a silent no-op.
3586+
result = step.execute(config, StepContext())
3587+
assert result.status == StepStatus.COMPLETED
3588+
assert result.next_steps == []
3589+
35633590
@pytest.mark.parametrize("bad", [["a", "b"], {"k": "v"}, 5, 1.5])
35643591
def test_validate_rejects_non_string_non_bool_condition(self, bad):
35653592
from specify_cli.workflows.steps.while_loop import WhileStep
@@ -3692,6 +3719,25 @@ def test_validate_missing_fields(self):
36923719
assert any("missing 'condition'" in e for e in errors)
36933720
# max_iterations is optional (defaults to 10)
36943721

3722+
def test_validate_requires_steps_body(self):
3723+
"""A do-while with no body must be rejected, not silently a no-op.
3724+
3725+
The step's own docstring promises "The first invocation always returns
3726+
the nested steps for execution" -- with no body it validated clean and
3727+
then returned none, so the loop never ran even once.
3728+
"""
3729+
from specify_cli.workflows.base import StepContext, StepStatus
3730+
from specify_cli.workflows.steps.do_while import DoWhileStep
3731+
3732+
step = DoWhileStep()
3733+
config = {"id": "refine", "condition": "true", "max_iterations": 3}
3734+
errors = step.validate(config)
3735+
assert errors == ["Do-while step 'refine' is missing 'steps' field."], errors
3736+
3737+
result = step.execute(config, StepContext())
3738+
assert result.status == StepStatus.COMPLETED
3739+
assert result.next_steps == []
3740+
36953741
@pytest.mark.parametrize("bad", [["a", "b"], {"k": "v"}, 5, 1.5])
36963742
def test_validate_rejects_non_string_non_bool_condition(self, bad):
36973743
from specify_cli.workflows.steps.do_while import DoWhileStep

0 commit comments

Comments
 (0)