Skip to content

[BUGF] validate autoswarm max_loops before generating Python#1538

Open
shaun0927 wants to merge 2 commits into
kyegomez:masterfrom
shaun0927:fix/autoswarm-generated-literal
Open

[BUGF] validate autoswarm max_loops before generating Python#1538
shaun0927 wants to merge 2 commits into
kyegomez:masterfrom
shaun0927:fix/autoswarm-generated-literal

Conversation

@shaun0927
Copy link
Copy Markdown

@shaun0927 shaun0927 commented Apr 16, 2026

Description

This PR fixes a code-generation sink in write_autoswarm_file(). The autoswarm writer previously interpolated swarm_architecture.max_loops directly into generated Python source. If that field contained a non-integer string expression, the generated module embedded executable code instead of a validated literal.

This change keeps the PR intentionally narrow:

  • validate/coerce swarm_architecture.max_loops to an integer-compatible value before emission
  • raise a clear ValueError for unsafe / invalid values
  • add a regression test covering the failure mode

Files Changed

  • swarms/agents/auto_generate_swarm_config.py
  • tests/agents/test_autoswarm_writer.py

Issue

Closes #1535

Dependencies

No extra dependencies required.

Maintainer

@kyegomez

Test plan

  • PYTHONPATH=. python -m pytest tests/agents/test_autoswarm_writer.py -k 'swarm_max_loops' -q

Why this scope

Recent merged PRs in this repo tend to land when they stay tightly scoped, include a direct regression test, and avoid bundling adjacent cleanups. This PR only fixes the confirmed max_loops generation path and leaves the rest of autoswarm codegen unchanged.


📚 Documentation preview 📚: https://swarms--1538.org.readthedocs.build/en/1538/

… expressions

The autoswarm writer interpolated swarm_architecture.max_loops directly into generated Python source. This change validates/coerces the field before emission and adds a regression test that rejects non-integer expressions instead of writing executable code into the generated module.

Constraint: Keep autoswarm writer behavior unchanged for valid integer inputs
Rejected: Route max_loops through _format_value only | would silently pass invalid string values into SwarmRouter instead of failing fast
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Any future code-generated SwarmRouter field should be validated or literal-formatted before emission; do not interpolate raw config values into Python source
Tested: PYTHONPATH=. python -m pytest tests/agents/test_autoswarm_writer.py -k 'swarm_max_loops' -q
Not-tested: End-to-end autoswarm CLI execution against live model providers
…c YAML

The initial fix blocked executable expressions, but it still skipped validation for falsey values and did not prove quoted numeric strings remained compatible. This follow-up validates any explicit max_loops field and adds regression coverage for both accepted quoted integers and rejected boolean values.

Constraint: Preserve compatibility with common YAML/LLM output that serializes integers as strings
Rejected: Leave validation gated on truthiness | would silently ignore explicit False/empty values instead of validating them
Confidence: high
Scope-risk: narrow
Reversibility: clean
Directive: Presence-based validation is safer than truthiness checks for generated config fields; keep this pattern for future autoswarm writer hardening
Tested: PYTHONPATH=. python -m pytest tests/agents/test_autoswarm_writer.py -k 'swarm_max_loops' -q
Not-tested: End-to-end autoswarm CLI execution against live model providers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] autoswarm writer emits raw swarm_architecture.max_loops into generated Python

1 participant