無限デバッグループ:Cursor CLI にテストが通るまで修正させる
5 min read
cursor agent にコードを自動修正させ、テストを実行し、失敗したら AI にまた修正させる — 全テストが通るまで繰り返す shell スクリプトを書きます。Y/N を押し続ける必要はありません。ループを AI に任せて別の用を済ませ、ターミナルに「tests passed」と出たら戻ってくればよいです。
考え方
while tests don't pass:
run tests → collect error output
feed error output to cursor agent
cursor agent fixes the code
run tests again
「テスト実行 → 失敗 → AI に修正依頼 → 繰り返し」のループで、緑になるまで回します。シンプルですが効果的です。
ステップ 1: 前提の確認
cursorがインストール済みでログイン済み(cursor --versionで確認)- プロジェクトに ターミナルで実行できるテストコマンド があること。例:
npm testpytestgo test ./...cargo test
- テスト失敗時に非ゼロの終了コードを返すこと(多くのテストフレームワークはデフォルトでそうなっています)
ステップ 2: 最小のスクリプト
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 1ステップ 3: スクリプトの実行
chmod +x debug-loop.sh
./debug-loop.shあとはコーヒーを飲んだりメッセージに返信したりして、ターミナルに ✅ Tests passed! が出たら戻ってくればよいです。MAX_LOOPS に達しても通らなければ、手動で調査する番です。
ステップ 4: プロジェクトに合わせてカスタマイズ
テストコマンドの変更(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" # PHP最大ループ数の調整:
MAX_LOOPS=5 # conservative, stop early if not improving
MAX_LOOPS=20 # more complex problems get more chancesAI にコンテキストを追加(cursor agent の指示内で):
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-interactive改良版:ループごとにログを残す
#!/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 1「どのループで何が起きたか」を後から確認するのに便利です。
安全上の注意
| リスク | 対策 |
|---|---|
| AI が余計なファイルを壊す | 事前に git commit で状態を保存、必要なら git checkout . で戻す |
| ループしすぎでクォータ消費 | MAX_LOOPS をほどほどに(5–15 推奨) |
| AI がテストファイルをいじる | プロンプトで「テストファイルは変更しない」と明示する |
| ループごとに別の失敗 | ログ(改良版)を残して事後分析する |
まとめ
- スクリプトの流れ: テスト実行 → 失敗 → AI に渡す → 通るか上限まで繰り返し
- 主なパラメータ:
TEST_CMD(テストコマンド)、MAX_LOOPS(最大ループ数) - プロンプトは具体的に: 言語・修正していいディレクトリ・テストファイルは触らない を書く
- 事前に
git commit— 問題があっても戻しやすく、安心して試せる
Worktree と組み合わせると、別ブランチで同時にデバッグループを回せる。 06-cursor-cli-with-worktree を参照。