Test-driven autonomous bug fixing

Give the agent a failing test as ground truth

Asking an agent to 'fix the bug' is vague; giving it a failing pytest command gives it an objective exit condition: write the failing test, patch the code, re-run pytest, and stop only when green.

Example

# Terminal TDD bugfix prompt
praisonai code "1) Add a failing pytest test for empty cart discounts in tests/test_cart.py. 2) Run pytest to confirm failure. 3) Fix cart.py until pytest passes."

Example explained

  • 1) Add a failing pytest test… — Locks down the exact bug before any production code changes.
  • 3) Fix cart.py until pytest passes — Gives the agent a verifiable completion signal instead of guessing.

Cap iterations and verify test output in Python

When automating a test-fix loop via the SDK, cap max_iter with ExecutionConfig so a stubborn bug cannot spin up unlimited LLM calls.

Example

from praisonai import Agent, ExecutionConfig

def run_unit_test() -> str:
    """Run pytest on the target module."""
    return '1 passed in 0.04s'

fixer = Agent(
    name='TDDFixer',
    instructions='Always call run_unit_test and report whether the suite is green in one sentence.',
    tools=[run_unit_test],
    execution=ExecutionConfig(max_iter=5, context_compaction=True),
    output='silent',
)
print(fixer.start('Verify the cart discount fix.'))

Try it Yourself

Example explained

  • ExecutionConfig(max_iter=5, context_compaction=True) — Prevents runaway repair loops by capping iterations to 5.
  • ScriptedModel.tool_call('run_unit_test', {}) — Proves the agent invokes the test runner tool before claiming success.

Common pitfall: letting the agent weaken assertions

Always inspect git diff tests/ after an autonomous fix. Never accept a patch that passed tests by deleting or loosening an assertion instead of fixing the source bug.

Reference: documentation