Infinite Debug Loop: Let Cursor CLI Fix Until Tests Pass
Write a shell script that has cursor agent automatically fix code, run tests, and if they fail let the AI fix again — repeating until all tests pass. No need to sit there pressing Y/N. Hand the loop to the AI and go do something else — come back when the terminal shows "tests passed."
Core Concept
while tests don't pass:
run tests → collect error output
feed error output to cursor agent
cursor agent fixes the code
run tests again
It's a "run tests → fail → tell AI to fix → repeat" loop until green. Simple and direct, but effective.
Step 1: Check Prerequisites
cursoris installed and logged in (cursor --versionto verify)- The project has a test command that runs in the terminal, such as:
npm testpytestgo test ./...cargo test
- Tests return a non-zero exit code on failure (almost all test frameworks do this by default)
Step 2: The Simplest Script
Create debug-loop.sh:
#!/bin/bash
MAX_LOOPS=10 # max iterations to prevent truly infinite loops
TEST_CMD="npm test" # replace with your test command
loop=0
while [ $loop -lt $MAX_LOOPS ]; do
echo ""
echo "=== Loop $((loop + 1)) ==="
# run tests and capture stdout + stderr
TEST_OUTPUT=$(eval "$TEST_CMD" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo "✅ Tests passed! Completed in $((loop + 1)) loop(s)."
exit 0
fi
echo "❌ Tests failed, letting the AI fix..."
echo "$TEST_OUTPUT" | tail -30 # print the last 30 lines for your reference
# feed the error output to cursor agent
cursor agent "Tests failed with the following errors. Please fix the code to make the tests pass:
$TEST_OUTPUT" --model gpt-codex-5.3-high --no-interactive
loop=$((loop + 1))
done
echo "⚠️ Reached max loops ($MAX_LOOPS). Stopping. Please investigate manually."
exit 1Step 3: Run the Script
chmod +x debug-loop.sh
./debug-loop.shNow you can make coffee, reply to messages, and come back when you see ✅ Tests passed! in the terminal. If it hits MAX_LOOPS without passing, it's time for human intervention.
Step 4: Customize for Your Project
Change the test command (line 4):
TEST_CMD="pytest -x" # Python, -x stops on first failure
TEST_CMD="go test ./..." # Go
TEST_CMD="cargo test" # Rust
TEST_CMD="./vendor/bin/phpunit" # PHPAdjust the max loops:
MAX_LOOPS=5 # conservative, stop early if not improving
MAX_LOOPS=20 # more complex problems get more chancesGive the AI more context (inside the cursor agent command):
cursor agent "Tests failed. Errors below.
The test framework is Jest and the language is TypeScript.
Do not modify test files — only change files under src/.
Error output:
$TEST_OUTPUT" --model gpt-codex-5.3-high --no-interactiveEnhanced Version: Keep Logs for Each Loop
#!/bin/bash
MAX_LOOPS=10
TEST_CMD="npm test"
LOG_DIR="debug-logs"
mkdir -p $LOG_DIR
loop=0
while [ $loop -lt $MAX_LOOPS ]; do
echo ""
echo "=== Loop $((loop + 1)) ==="
LOG_FILE="$LOG_DIR/loop-$((loop + 1)).log"
TEST_OUTPUT=$(eval "$TEST_CMD" 2>&1 | tee "$LOG_FILE")
EXIT_CODE=${PIPESTATUS[0]}
if [ $EXIT_CODE -eq 0 ]; then
echo "✅ Tests passed! Completed in $((loop + 1)) loop(s)."
exit 0
fi
echo "❌ Failed. Log saved to $LOG_FILE"
cursor agent "Tests failed with the following errors. Please fix the code to make the tests pass:
$TEST_OUTPUT" --model gpt-codex-5.3-high --no-interactive
loop=$((loop + 1))
done
echo "⚠️ Reached max loops. Stopping. Check the logs in $LOG_DIR/."
exit 1Useful for reviewing "what went wrong in which loop" after the fact.
Safety Considerations
| Risk | How to Mitigate |
|---|---|
| AI breaks more files | git commit first to save state, git checkout . to revert if needed |
| Too many loops waste quota | Set a reasonable MAX_LOOPS (5–15 recommended) |
| AI modifies test files | Explicitly say "do not modify test files" in the prompt |
| Different failures each loop | Keep logs (enhanced version) for post-analysis |
Summary
- Script structure: run tests → fail → feed to AI → repeat until pass or hit limit
- Key parameters:
TEST_CMD(test command),MAX_LOOPS(maximum iterations) - Be specific in your prompt: state the language, restrict which directories to modify, no touching test files
git commitfirst — easy to revert if things go wrong, and peace of mind
Pair with Worktree: run a debug loop on different branches simultaneously. See 06-cursor-cli-with-worktree.