Bucle infinito de depuración: deja que Cursor CLI corrija hasta pasar tests
3 min read
Puedes crear un script donde cursor agent corrija código automáticamente, ejecute tests y, si fallan, vuelva a intentar.
El ciclo se repite hasta que los tests pasen o se alcance un máximo de iteraciones.
Idea central
while los tests no pasen:
ejecutar tests -> capturar errores
enviar errores a cursor agent
cursor agent corrige código
ejecutar tests otra vez
Es un loop simple, directo y muy efectivo: test -> fallo -> IA corrige -> repetir hasta verde.
Paso 1: Prerrequisitos
cursorinstalado y con login (cursor --versionpara verificar)- El proyecto tiene un comando de tests ejecutable en terminal, por ejemplo:
npm testpytestgo test ./...cargo test
- Los tests devuelven código de salida no-cero al fallar
Paso 2: Script mínimo
Crea debug-loop.sh:
#!/bin/bash
MAX_LOOPS=10 # límite para evitar loops realmente infinitos
TEST_CMD="npm test" # reemplaza por tu comando
loop=0
while [ $loop -lt $MAX_LOOPS ]; do
echo ""
echo "=== Loop $((loop + 1)) ==="
# ejecuta tests y captura 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
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 1Paso 3: Ejecutar script
chmod +x debug-loop.sh
./debug-loop.shSi llega al máximo de loops sin pasar, toca revisión humana.
Paso 4: Personalizar para tu stack
Cambiar comando de tests:
TEST_CMD="pytest -x" # Python
TEST_CMD="go test ./..." # Go
TEST_CMD="cargo test" # Rust
TEST_CMD="./vendor/bin/phpunit" # PHPAjustar límite de loops:
MAX_LOOPS=5 # conservador
MAX_LOOPS=20 # problemas complejosDar más contexto a la IA:
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-interactiveVersión mejorada: guardar logs por 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 1Consideraciones de seguridad
| Riesgo | Mitigación |
|---|---|
| La IA rompe más archivos | Haz git commit antes; revierte si hace falta |
| Demasiados loops consumen cuota | Define MAX_LOOPS razonable (5-15) |
| La IA modifica tests | Indica explícitamente "no modificar tests" |
| Cada loop falla distinto | Guarda logs para analizar después |
Resumen
- Estructura: ejecutar tests -> fallo -> pasar errores a IA -> repetir
- Parámetros clave:
TEST_CMDyMAX_LOOPS - Prompt específico: lenguaje, límites de carpetas, no tocar tests
- Haz
git commitantes para volver atrás rápido si algo sale mal
Combínalo con Worktree para correr bucles de depuración en ramas distintas en paralelo: 06-cursor-cli-with-worktree.