حلقة التصحيح اللانهائية: دع Cursor CLI يصلح حتى تمر الاختبارات
اكتب سكربت shell يجعل cursor agent يصلح الكود تلقائياً ويشغّل الاختبارات، وإذا فشلت يدع الذكاء الاصطناعي يصلح مرة أخرى — ويكرر حتى تمر كل الاختبارات. لا حاجة للجلوس والضغط Y/N. سلّم الحلقة للذكاء الاصطناعي واذهب لشغل آخر — ارجع عندما تظهر الطرفية "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
حلقة "شغّل الاختبارات → فشل → أخبر الذكاء الاصطناعي يصلح → كرر" حتى تنجح. بسيطة ومباشرة لكن فعّالة.
الخطوة 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 chancesأعطِ الذكاء الاصطناعي سياقاً أوضح (داخل أمر 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مفيد لمراجعة "ما الذي حدث في أي حلقة" لاحقاً.
اعتبارات الأمان
| الخطر | كيف تتجنّبه |
|---|---|
| الذكاء الاصطناعي يكسّر ملفات أكثر | git commit أولاً لحفظ الحالة، git checkout . للتراجع إن لزم |
| حلقات كثيرة تهدر الحصة | ضع MAX_LOOPS معقولاً (5–15 موصى به) |
| الذكاء الاصطناعي يعدّل ملفات الاختبار | قل صراحة في الـ prompt "لا تعدّل ملفات الاختبار" |
| فشل مختلف كل حلقة | احتفظ بالسجلات (النسخة المحسّنة) للتحليل لاحقاً |
ملخص
- بنية السكربت: شغّل الاختبارات → فشل → أطعِم الذكاء الاصطناعي → كرر حتى النجاح أو الوصول للحد
- المعاملات الأساسية:
TEST_CMD(أمر الاختبار)،MAX_LOOPS(الحد الأقصى للتكرارات) - كن واضحاً في الـ prompt: حدّد اللغة، حدّد المجلدات القابلة للتعديل، عدم لمس ملفات الاختبار
git commitأولاً — إن حصل خطأ يسهل الرجوع، وراحة بال
مع Worktree: شغّل حلقة تصحيح على فروع مختلفة في الوقت نفسه. راجع 06-cursor-cli-with-worktree.