协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
test-guided-bug-detector【开发与工程】Analyze failing tests to detect functional bugs in code. Takes repository and failing test output as input, analyzes execution behavior, assertions, and stack traces to identify suspicious code regions and root causes. Use when debugging test failures, investigating regression bugs, or understanding why tests fail. Explains the bug mechanism, identif…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Analyze failing tests to detect and explain functional bugs in code.
When tests fail, they provide valuable clues about bugs in the code. This skill analyzes:
The goal is to identify the root cause bug and explain why the test exposes it.
Failing Test Output
↓
Parse Failure Information
↓
Identify Test Expectations
↓
Trace Execution Path
↓
Analyze Discrepancy
↓
Identify Suspicious Code
↓
Explain Bug Mechanism
↓
Suggest Fix
Extract key information from test output:
What to extract:
Example:
FAILED tests/test_calculator.py::test_divide - AssertionError: assert 0 == 5
Expected: 5
Actual: 0
Stack trace:
File "tests/test_calculator.py", line 15, in test_divide
assert divide(10, 2) == 5
File "src/calculator.py", line 8, in divide
return a // b
Determine what the test is trying to verify:
Questions to answer:
Example:
def test_divide():
# Intent: Verify division returns correct result
result = divide(10, 2)
assert result == 5 # Expects 10 / 2 = 5
Follow the code path from test to failure:
Trace elements:
Example trace:
test_divide()
→ divide(10, 2)
→ return a // b (integer division)
→ returns 5
→ assert 5 == 5 ✓ Should pass!
Find where expected and actual diverge:
Common discrepancies:
Example:
# Expected: 10 / 2 = 5.0
# Actual: 10 // 2 = 5 (but test got 0?)
# Discrepancy: Something else is wrong!
Examine code for bug patterns:
Bug patterns to check:
Example analysis:
def divide(a, b):
result = 0 # BUG: Initialized but never updated!
return a // b # This line is unreachable? No, wait...
# Actually, this returns correctly, but...
Describe how the bug causes the failure:
Explanation structure:
Propose concrete fix with explanation:
Fix components:
For detailed bug patterns and detection strategies, see references/bug_patterns.md.
Categories include:
For analyzing different types of test failures, see references/failure_types.md.
Failure types:
Input: Failing test
# Test file: tests/test_list_utils.py
def test_remove_duplicates():
input_list = [1, 2, 2, 3, 3, 3, 4]
result = remove_duplicates(input_list)
assert result == [1, 2, 3, 4]
assert input_list == [1, 2, 2, 3, 3, 3, 4] # Original unchanged
# Test output:
# FAILED - AssertionError: assert [1, 2, 3, 4] == [1, 2, 2, 3, 3, 3, 4]
# The second assertion failed!
# Implementation: src/list_utils.py
def remove_duplicates(lst):
seen = set()
i = 0
while i < len(lst):
if lst[i] in seen:
lst.pop(i) # BUG: Modifies input list!
else:
seen.add(lst[i])
i += 1
return lst
Output: Bug analysis
# Bug Analysis Report
## Test Failure Summary
**Test:** test_remove_duplicates
**Location:** tests/test_list_utils.py:2
**Failure Type:** Assertion failure
**Failed Assertion:** `assert input_list == [1, 2, 2, 3, 3, 3, 4]`
## Expected vs Actual
**Expected:** Original list unchanged: `[1, 2, 2, 3, 3, 3, 4]`
**Actual:** Original list modified: `[1, 2, 3, 4]`
## Root Cause
**Bug Location:** src/list_utils.py:7
**Bug Type:** Unintended side effect (input mutation)
**Problematic Code:**
```python
lst.pop(i) # Modifies the input list directly
lst.pop(i)lst
affect the original input_listtest_remove_duplicates()
input_list = [1, 2, 2, 3, 3, 3, 4]
↓
remove_duplicates(input_list) # lst points to same list as input_list
i=0: lst[0]=1, not in seen, add to seen, i=1
i=1: lst[1]=2, not in seen, add to seen, i=2
i=2: lst[2]=2, in seen, lst.pop(2) # Removes from input_list!
# Now lst = input_list = [1, 2, 3, 3, 3, 4]
i=2: lst[2]=3, not in seen, add to seen, i=3
i=3: lst[3]=3, in seen, lst.pop(3) # Removes from input_list!
# Now lst = input_list = [1, 2, 3, 3, 4]
i=3: lst[3]=3, in seen, lst.pop(3) # Removes from input_list!
# Now lst = input_list = [1, 2, 3, 4]
i=3: lst[3]=4, not in seen, add to seen, i=4
return lst # Returns [1, 2, 3, 4]
↓
result = [1, 2, 3, 4] ✓ First assertion passes
input_list = [1, 2, 3, 4] ✗ Second assertion fails!
lst.pop(i) # Direct mutation of input
Suspicion Level: HIGH Reason: Modifies input list, violating immutability expectation
return lst # Returns reference to modified input
Suspicion Level: MEDIUM Reason: Returns same object as input, not a new list
Option 1: Create a copy (Recommended)
def remove_duplicates(lst):
result = [] # Create new list
seen = set()
for item in lst:
if item not in seen:
seen.add(item)
result.append(item)
return result
Why this fixes it:
Option 2: Explicit copy
def remove_duplicates(lst):
lst = lst.copy() # Work on a copy
seen = set()
i = 0
while i < len(lst):
if lst[i] in seen:
lst.pop(i)
else:
seen.add(lst[i])
i += 1
return lst
Why this fixes it:
lst.copy() creates a shallow copyTo verify the fix:
pytest tests/test_list_utils.py::test_remove_duplicatesdef test_remove_duplicates_immutable():
original = [1, 2, 2, 3]
original_copy = original.copy()
result = remove_duplicates(original)
assert original == original_copy # Verify no mutation
This bug could affect:
remove_duplicates doesn't modify inputremove_duplicates
## Analysis Strategies
For detailed analysis strategies by language and framework, see [references/analysis_strategies.md](references/analysis_strategies.md).
Strategies include:
- Python (pytest, unittest)
- JavaScript (Jest, Mocha)
- Java (JUnit)
- C/C++ (Google Test)
- Go (testing package)
## Best Practices
1. **Start with the failure message** - It often points directly to the bug
2. **Understand test intent** - Know what should happen
3. **Trace execution carefully** - Follow the actual code path
4. **Look for common patterns** - Many bugs follow known patterns
5. **Consider edge cases** - Bugs often hide at boundaries
6. **Check assumptions** - Verify what the code assumes
7. **Explain clearly** - Make the bug mechanism understandable
## Red Flags
Watch for these suspicious patterns:
**High-priority red flags:**
- Uninitialized variables
- Missing return statements
- Wrong operators (== vs =, // vs /)
- Off-by-one errors (< vs <=)
- Null/None without checks
- Mutable default arguments
- Side effects in pure functions
**Medium-priority warnings:**
- Complex conditionals
- Nested loops with breaks
- Exception swallowing
- Type conversions
- Global state access
## Report Template
```markdown
# Bug Analysis Report
## Test Failure Summary
- Test name and location
- Failure type
- Failed assertion/error
## Expected vs Actual
- What should happen
- What actually happened
## Root Cause
- Bug location (file:line)
- Bug type
- Problematic code snippet
## Bug Mechanism
- Step-by-step explanation
- Why it's wrong
- How test exposes it
## Execution Trace
- Detailed trace from test to failure
- Variable values at key points
## Suspicious Code Regions
- Primary suspects with evidence
- Secondary suspects
## Recommended Fix
- Proposed code change
- Explanation of why it fixes the bug
- How to verify
## Related Issues
- Other code that might be affected
For detailed guidance: