| name | debug |
| description | Systematically diagnose and fix bugs. Use when debugging errors, fixing failing tests, or investigating unexpected behavior. Use when this capability is needed. |
| metadata | {"author":"ademkao"} |
Debug Skill
Instructions
-
Reproduce the Issue
- Get exact steps to reproduce
- Identify inputs that cause the bug
- Confirm it's consistently reproducible
-
Gather Information
cat logs/error.log | tail -50
git log --oneline -10
git diff HEAD~5
-
Form Hypothesis
- What could cause this behavior?
- List 2-3 most likely causes
- Rank by probability
-
Isolate the Problem
- Binary search through code
- Add logging/breakpoints
- Create minimal reproduction
-
Identify Root Cause
- Don't stop at symptoms
- Ask "why" multiple times
- Find the actual bug, not a workaround
-
Fix the Bug
- Fix the root cause
- Add test to prevent regression
- Verify fix doesn't break other things
-
Verify Fix
pnpm test
pnpm build
Debugging Techniques
Add Strategic Logging
console.log("[DEBUG] Function called with:", { userId, options });
console.log("[DEBUG] Query result:", result);
console.log("[DEBUG] State before update:", this.state);
Binary Search
function processData(data: Data[]) {
console.log("[DEBUG] 1. Start, data length:", data.length);
const filtered = filterData(data);
console.log("[DEBUG] 2. After filter:", filtered.length);
const transformed = transformData(filtered);
console.log("[DEBUG] 3. After transform:", transformed.length);
const result = aggregateData(transformed);
console.log("[DEBUG] 4. After aggregate:", result);
return result;
}
Check Assumptions
function getUser(id: string) {
console.assert(typeof id === "string", "id should be string");
console.assert(id.length > 0, "id should not be empty");
const user = db.users.find(id);
console.assert(user !== undefined, "user should exist");
return user;
}
Minimal Reproduction
it("should reproduce the bug", () => {
const input = { userId: "123", status: "active" };
const result = problematicFunction(input);
expect(result).toBe(expectedValue);
});
Common Bug Categories
1. Off-by-One Errors
for (let i = 0; i < array.length - 1; i++)
for (let i = 0; i < array.length; i++)
for (let i = 0; i <= array.length - 1; i++)
2. Null/Undefined
const name = user.profile.name;
const name = user?.profile?.name ?? "Unknown";
3. Async Issues
function getData() {
const data = fetchData();
return data.items;
}
async function getData() {
const data = await fetchData();
return data.items;
}
4. State Mutation
function addItem(items: Item[], newItem: Item) {
items.push(newItem);
return items;
}
function addItem(items: Item[], newItem: Item) {
return [...items, newItem];
}
5. Scope/Closure Issues
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
Debug Report Format
## Bug Report: [Brief Description]
### Symptoms
- What user observed
- Error messages
### Reproduction Steps
1. Step 1
2. Step 2
3. Bug occurs
### Root Cause
[Explanation of why the bug occurred]
### Fix
[Description of the fix]
### Files Changed
- `src/module/file.ts` - [what changed]
### Tests Added
- `src/module/file.test.ts` - [test description]
### Verification
- [x] Bug no longer reproduces
- [x] All tests pass
- [x] No regression in related features
Anti-Patterns
❌ Don't Do This
- Fix symptoms without understanding cause
- Make multiple changes at once
- Skip adding regression tests
- Leave debug logging in code
- Assume you know the cause without verification
✅ Do This
- Reproduce before fixing
- One change at a time
- Verify each hypothesis
- Add test that would have caught the bug
- Clean up debug code
Converted and distributed by TomeVault — claim your Tome and manage your conversions.