Implementing a Multi-Agent Workflow for Automated Python Bug Fixing and Testing
The problem with a single-shot request like "fix this bug" is that the AI often hallucinates a fix that looks correct but breaks a dependency elsewhere because it's too focused on the local error.
My current workflow splits this into three distinct roles: the Triage Agent, the Developer Agent, and the QA Agent.
The Triage Phase
Instead of feeding the AI a stack trace and asking for a fix, I feed it the error and the relevant files, but tell it only to identify the root cause and the specific lines of code responsible. I use a custom .cursorrules entry to enforce this.
Role: Triage Agent
Goal: Root Cause Analysis (RCA)
Constraint: Do not write any fixing code. Output only the file path, line number, and a 1-sentence explanation of why the logic is failing.The Implementation Phase
Once I have the RCA, I switch the context. I provide the Triage Agent's output to the Developer Agent. Because the scope is now narrowed to a specific line and a specific reason, the "hallucination rate" drops significantly. I typically use Claude 3.5 Sonnet for this part because its reasoning on Python's async patterns is currently superior to GPT-4o.
The Verification Loop (The Secret Sauce)
This is where most people stop, but it's the most important part. I've created a "Test-Driven Fix" prompt. I tell the AI: "Before you apply the fix, write a failing Pytest case that reproduces the bug described by the Triage Agent."
# Example of the prompt I use to force a reproduction script
"Write a standalone script `repro_bug.py` that triggers the reported IndexError in the current codebase. The script must fail currently and pass only after the fix is implemented."The Workflow in Practice
1. Triage: AI identifies that utils.py line 42 is accessing a list index that doesn't exist during empty API responses.
2. Repro: AI writes a test case passing an empty list to that function. I run it → it fails (confirmed).
3. Fix: AI modifies the code to handle the empty list.
4. Verify: I run the repro script → it passes.
Productivity Gains and Gotchas
The biggest gain is confidence. I no longer "hope" the fix works; I have a script that proves it. My velocity has increased because I spend less time in a "fix-break-fix" cycle.
A few warnings:
Context Bloat: If you keep all three agents in one long chat thread, the AI starts getting confused and merges the roles. I start a fresh "Composer" session or clear the chat between the Triage and Implementation phases.
Over-reliance on AI Tests: Sometimes the AI writes a test that passes for the wrong reason (e.g., it mocks the very thing that's broken). Always skim the repro_bug.py to ensure it's actually testing the logic, not just mocking the output.
Indexing: In Cursor, make sure your @Codebase index is up to date, otherwise the Triage agent will miss the connection between the error and a distant utility function.
This modular approach turns bug fixing from a guessing game into a deterministic process.
All Replies (0)
No replies yet — be the first!
