أوامر تصحيح أخطاء تحدد الموقع فعلاً
Prompts تحديد مكان الـ Bug
الطريقة الغلط في الـ debug مع LLM إنك تلصق الـ function المكسورة وتقول "ده غلط، صلّحه." الموديل بيعيد كتابة الحاجة كلها، بتخسر البنية اللي موجودة، وبتراجع implementation جديد بالكامل بدل fix سطر واحد.
النمط الصح هو اللي المهندسين الشغّالين بيعملوه فعلاً: حدّد المكان الأول، صلّح بعد كده. Prompt الـ localization بيطلب من الموديل يلاقي الـ bug — مش يعيد كتابة الـ function.
تدفق الـ module ده:
حلقة localize-then-fix الكاملة:
ده prompt localization على second_largest مكسورة:
The following Python function is supposed to return the second-largest distinct number, but it sometimes returns the largest. Identify the bug, give the exact line, and the one-line fix. Do not rewrite the whole function.
def second_largest(nums): if len(nums) < 2: return None largest = float("-inf") second = float("-inf") for n in nums: if n > largest: second = largest largest = n elif n > second: second = n return secondTest that fails:
second_largest([5, 5])returns-inf, expectedNone.
Claude Sonnet 4.5 رجّع:
## Bug Identification
Line with bug: Line 10 (the `elif n > second:` condition)
Problem: When all numbers are the same (like `[5, 5]`), the function doesn't
check if `n` is distinct from `largest` before updating `second`. This allows
duplicate values to become the "second largest."
One-line fix:
elif n > second and n != largest:
This ensures that `second` only gets updated if the number is both greater than
the current second AND different from the largest, guaranteeing we get the
second-largest distinct number.
Captured from Claude Sonnet 4.5 (claude-sonnet-4-5) on 2026-04-27. Re-runs may differ slightly.
3 حاجات بتخلي الـ prompt ده شغّال:
- السلوك المتوقع متقال في جملة — "supposed to return the second-largest distinct number." الموديل دلوقتي عارف يعني إيه صح.
- حالة test فاشلة متضمّنة —
second_largest([5, 5])بترجع-inf، المتوقعNone. ده بيربط الـ bug بـ input تقدر تعيده. - شكل المخرج مقيّد — "exact line, one-line fix, do not rewrite." الموديل مش هيقدر يهرب لـ refactor.
تعليمة "do not rewrite" هي أهم كلمة في الـ prompt ده. من غيرها، الموديل بيرجع للسلوك المفيد إنه يرجع rewrite نظيف — اللي بيخلي الـ review أصعب، والـ PR الصغيرة أصعب، والتعلم منها أصعب.
لما مش هتقدر تضمّن failing test (الـ bug متقطّع، أو مش عندك repro)، اوصف الأعراض بدقة. "Returns 0 when input is empty" أحسن من "doesn't work for empty inputs." Inputs و outputs محددة بتدّي الموديل هدف. Prose غامض بيدّيه مساحة يخترع.
التالي: تخلّي الموديل يفسّر traceback حقيقي بلغة سهلة. :::
سجّل الدخول للتقييم