Work through a bug systematically: reproduce it, rank likely causes, run a targeted experiment, then fix it with a regression test.
You are an experienced debugger. Help find the cause of the problem below before proposing a fix. Separate what the evidence shows from what you suspect, and give the smallest experiment that would confirm each suspicion. ## Inputs Expected behaviour versus actual behaviour: [PROBLEM] Relevant code: ```[LANGUAGE] [CODE] ``` Exact error message or stack trace: [ERROR] Steps to reproduce and how often it happens: [REPRODUCTION] What has already been tried and the result: [ATTEMPTS] Environment (language/framework versions, OS, dependency versions, recent changes): [ENVIRONMENT] ## Process 1. **Read the evidence**: what the error and stack trace prove, including the exact line and the value or state that was wrong. 2. **Rank hypotheses**: list likely causes with the evidence for and against each. Do not repeat what [ATTEMPTS] already ruled out. 3. **Confirm**: for the top hypotheses, give one small experiment each (a log line, an assertion, a minimal reproduction) and what result would confirm or reject it. 4. **Fix**: provide corrected code for the most likely cause, highlight what changed and why. 5. **Prevent**: a regression test that fails before the fix and passes after it, plus related edge cases worth checking. 6. **Other issues**: problems noticed in the code that are unrelated to this bug, marked as separate. ## Return Sections for Evidence, Ranked hypotheses, Experiments, Fix, Regression test, and Other issues. ## Acceptance checks - The fix addresses the top-ranked cause, or explains why it is safe to apply before confirmation. - The regression test targets the reported failure, not just general behaviour. - No claim that the fix was verified; verification is the user's run of the experiment and test. - If information needed to proceed is missing, ask for it specifically.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Problem: the cleanup job should remove expired sessions but crashes
Code (Python 3.12):
def purge_expired(sessions, now):
for sid, s in sessions.items():
if s["expires_at"] < now:
del sessions[sid]
return sessions
Error: RuntimeError: dictionary changed size during iteration (at the for line)
Reproduction: happens whenever at least one session is expired
Tried: wrapping in try/except, which hides the error but leaves expired sessions behind
Environment: Python 3.12, no third-party code involvedEvidence: the RuntimeError is raised because the dict is mutated while iterating over its live view. Top hypothesis confirmed by the error text itself; the experiment is a two-entry dict with one expired item. Fix: iterate over list(sessions.items()) or rebuild the dict with a comprehension, keeping the function's in-place contract if callers rely on it. Regression test: a dict with expired and active sessions where the expired ones are removed and active ones kept. Other issue: the try/except attempt should be removed because it masks failures. Illustrative expectation; the original error and the comprehension fix were reproduced locally on a synthetic fixture (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models