Bugfix tdd

From divad12/dotfiles · View interactive version

Bugfix tdd is an AI agent skill from the GitHub repository divad12/dotfiles. Use when the user reports a bug or says 'fix', 'broken', 'doesn't work', 'regression', or describes unexpected behavior.

How to use this skill

Skill file

Bugfix TDD

Every bug fix starts with a failing test. No exceptions.

Steps

1. Write a failing test FIRST - before ANY investigation

Do NOT read the implementation code yet. Do not investigate the root cause. Do not look at what function is responsible. Write the test purely from the user's description of what's broken - the symptom, not the cause.

Why: If you investigate first, you'll write a test that targets the specific bug you found (e.g., "off-by-one in line 42"). That test is too specific - it only guards against that exact mistake. Instead, write the test from the user's perspective: "when I do X, I expect Y but get Z." This produces a behavioral regression test that catches ANY cause of the same symptom, including future bugs you can't predict.

Write a test that:

Run the test and confirm it fails. If it passes, your test doesn't reproduce the bug - rewrite it.

# Run just the new test to confirm it fails
npm test -- --run -t "your test name"

2. NOW investigate and find the root cause

With the failing test in hand, read the relevant code and find the root cause. Use these techniques:

Trace backward, not forward. Don't fix where the error appears - trace up the call chain. Ask "what called this with bad data?" repeatedly until you reach the original source. Example: git init ran in the wrong directory because projectDir was empty, because context.tempDir was accessed before beforeEach. Fix at source, not symptom.

For cross-layer bugs, instrument each boundary. If the bug spans multiple components (form -> API route -> service -> database), add temporary logging at each boundary to see where data goes wrong. Run once, read the logs, identify the failing layer. Don't guess which layer - observe it.

Find duplicated logic. If similar code exists elsewhere that works correctly, that's both a clue to the bug AND a problem in itself - it should have been consolidated. Note it for step 3.

3. Fix the bug and consolidate

Now fix the implementation. Change what's necessary to make the failing test pass. If you found duplicated logic in step 2, consolidate it as part of the fix - duplication is a bug factory and deduplication prevents the same class of bug from recurring elsewhere.

Defense-in-depth: After fixing the root cause, consider adding validation at other layers the bad data passed through. A single fix at the source is correct but fragile - validation at multiple layers makes the bug structurally impossible to recur from new code paths.

4. Verify

Run the full test suite to confirm:

npm test -- --run

5. Route the learning

After the fix passes verification, invoke /learn capture for any reusable bug

class, missing guardrail, or project-specific workflow lesson. The canonical

record is docs/learnings/; legacy files such as BUG_PATTERNS.md and

LESSONS_LEARNED.md may be candidate artifacts when a project still uses them.

6. Summarize

Tell the user:

Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.