@jjdenhertog/ai-driven-development
Version:
AI-driven development workflow with learning capabilities for Claude
542 lines (473 loc) • 14.6 kB
Markdown
---
description: "Phase 4B: TEST FIXER - Automatically fixes failing tests and re-validates"
allowed-tools: ["Read", "Edit", "MultiEdit", "Bash", "Grep", "Glob", "LS", "Write", "TodoWrite","mcp__*"]
disallowed-tools: ["git", "WebFetch", "WebSearch", "Task", "NotebookRead", "NotebookEdit"]
---
# Command: aidev-code-phase4B
# 🔧 CRITICAL: PHASE 4B = AUTOMATIC TEST FIXING 🔧
**YOU ARE IN PHASE 4B OF 7:**
- **Phase 0 (DONE)**: Inventory completed
- **Phase 1 (DONE)**: Architecture designed
- **Phase 2 (DONE)**: Tests created
- **Phase 3 (DONE)**: Implementation completed
- **Phase 4A (DONE)**: Validation executed
- **Phase 4B (NOW)**: Fix failing tests automatically
- **Phase 5 (LATER)**: Final review
**PHASE 4B RESPONSIBILITIES:**
✅ Read Phase 4 validation results
✅ Identify failing tests
✅ Fix implementation to make tests pass
✅ Re-run tests to verify fixes
✅ Update implementation files only
✅ Track fixing progress with TodoWrite
❌ DO NOT modify test files
❌ DO NOT change test expectations
❌ DO NOT skip failing tests
<role-context>
You are a test fixing specialist in the multi-agent system. Your role is to automatically fix failing tests by updating the implementation code to meet test expectations.
**CRITICAL**: You fix implementation to match tests, not the other way around. Tests define the specification.
</role-context>
## Purpose
Phase 4B of the multi-agent pipeline. Automatically fixes failing tests by updating implementation code to meet test specifications.
## Process
### 0. Pre-Flight Check (Bash for Critical Validation Only)
```bash
echo "===================================="
echo "🔧 PHASE 4B: TEST FIXER"
echo "===================================="
echo "✅ Will: Fix implementation for failing tests"
echo "✅ Will: Re-run tests to verify fixes"
echo "❌ Will NOT: Modify test files"
echo "❌ Will NOT: Change test expectations"
echo "===================================="
# Parse parameters
PARAMETERS_JSON='<extracted-json-from-prompt>'
TASK_FILENAME=$(echo "$PARAMETERS_JSON" | jq -r '.task_filename')
TASK_OUTPUT_FOLDER=$(echo "$PARAMETERS_JSON" | jq -r '.task_output_folder // empty')
if [ -z "$TASK_FILENAME" ] || [ "$TASK_FILENAME" = "null" ]; then
echo "ERROR: task_filename not found in parameters"
exit 1
fi
if [ -z "$TASK_OUTPUT_FOLDER" ] || [ "$TASK_OUTPUT_FOLDER" = "null" ]; then
echo "ERROR: task_output_folder not found in parameters"
exit 1
fi
# Verify Phase 4A validation results exist
if [ ! -f "$TASK_OUTPUT_FOLDER/phase_outputs/validate/validation_summary.json" ]; then
echo "❌ ERROR: Phase 4A validation summary not found"
exit 1
fi
echo "✅ Pre-flight checks passed"
```
### 1. Load Context and Determine Fix Strategy
<load-fix-context>
Use the Read tool to load:
1. `$TASK_OUTPUT_FOLDER/phase_outputs/validate/validation_summary.json`
2. `$TASK_OUTPUT_FOLDER/phase_outputs/validate/test_results.txt`
3. `$TASK_OUTPUT_FOLDER/context.json`
4. `.aidev-storage/tasks/$TASK_FILENAME.json`
Determine fix requirements:
```json
{
"task_type": "feature|pattern",
"test_status": {
"passed": boolean,
"failing_count": 0,
"skip_fixing": boolean
},
"fix_strategy": {
"approach": "progressive|targeted|skip",
"max_iterations": 5,
"stop_conditions": [
"all_tests_pass",
"no_progress",
"max_iterations_reached"
]
}
}
```
</load-fix-context>
<early-exit-conditions>
Skip Phase 4B if:
1. Task type is "pattern" - no test fixing needed
2. All tests already passing - no fixes required
3. No test framework available - nothing to fix
Update context and exit gracefully in these cases
</early-exit-conditions>
<todo-initialization>
**CRITICAL TODO INITIALIZATION FOR FIX TRACKING**:
1. **For tasks requiring fixes, MUST use TodoWrite tool to create 5 todos**:
1. Analyze test failures and identify root causes
2. Understand test specifications and expected behavior
3. Apply fixes to implementation code
4. Verify fixes by re-running tests
5. Document fix results and any remaining issues
2. **For tasks with no failing tests (early exit)**:
- Still create 1 todo: "Document validation passed - no fixes needed"
- Mark it as completed immediately
3. **Track progress meticulously**:
- Mark todo as "in_progress" when starting each step
- Mark as "completed" immediately after finishing
- Document outcome of each step
4. **REMEMBER: Phase cannot succeed with incomplete todos**
</todo-initialization>
### 2. Intelligent Test Failure Analysis
<failure-analysis>
Analyze test failures systematically:
1. **Failure Categorization**:
```json
{
"failure_patterns": {
"assertion_failures": [
{
"test": "test_name",
"expected": "value",
"actual": "value",
"type": "value|type|behavior"
}
],
"runtime_errors": [
{
"test": "test_name",
"error": "error_message",
"type": "undefined|null|type_error"
}
],
"missing_implementations": [
{
"test": "test_name",
"missing": "function|component|property"
}
]
}
}
```
2. **Fix Priority Matrix**:
- Critical: Core functionality failures
- High: User-facing feature failures
- Medium: Edge case failures
- Low: Nice-to-have features
3. **Implementation Mapping**:
- Map test files to implementation files
- Identify exact locations needing fixes
- Plan minimal changes for maximum impact
</failure-analysis>
### 2. Apply Fixes Iteratively
For each failing test:
```bash
# Mark fixing todo as in progress
# Use TodoWrite tool to mark todo as in progress
# Update todo ID 3 status to "in_progress"
# Track current iteration
ITERATION=$(jq -r '.iterations' "$FIX_OUTPUT_PATH/fix_tracking.json")
ITERATION=$((ITERATION + 1))
echo "🔧 Starting fix iteration $ITERATION..."
```
Use MultiEdit to fix implementation files based on test expectations:
- Update function logic to match test assertions
- Fix return values to match expected outputs
- Add missing error handling
- Correct data transformations
After each fix:
```bash
# Document the fix
FIX_RECORD='{
"iteration": '$ITERATION',
"timestamp": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'",
"test_name": "<test-name>",
"file_fixed": "<file-path>",
"fix_description": "<what-was-fixed>"
}'
# Add to tracking
jq --argjson fix "$FIX_RECORD" '.fixes += [$fix] | .iterations = '$ITERATION "$FIX_OUTPUT_PATH/fix_tracking.json" > "$FIX_OUTPUT_PATH/fix_tracking.json.tmp"
mv "$FIX_OUTPUT_PATH/fix_tracking.json.tmp" "$FIX_OUTPUT_PATH/fix_tracking.json"
```
### 3. Re-run Tests After Fixes
<test-verification-strategy>
After applying fixes, verify the implementation systematically:
1. **Test Execution Management**:
```json
{
"execution_plan": {
"test_command": "detected_from_package_json",
"output_capture": "structured_results",
"failure_analysis": "progressive",
"iteration_tracking": "automated"
}
}
```
2. **Progress Tracking**:
```json
{
"iteration_metrics": {
"current_iteration": 0,
"initial_failures": 0,
"current_failures": 0,
"tests_fixed": 0,
"progress_rate": "percentage"
},
"stop_conditions": {
"all_passing": "success",
"no_progress": "stop_with_partial",
"max_iterations": 5,
"critical_failure": "stop_immediately"
}
}
```
3. **Intelligent Continuation Logic**:
- Analyze failure patterns between iterations
- Identify tests that consistently fail
- Group related failures for batch fixing
- Prioritize high-impact fixes
4. **Failure Pattern Recognition**:
```json
{
"patterns": {
"consistent_failures": "same tests failing repeatedly",
"cascading_failures": "one fix causes new failures",
"flaky_tests": "intermittent failures",
"environment_issues": "setup/teardown problems"
},
"adaptive_strategy": {
"consistent": "deeper investigation needed",
"cascading": "fix order adjustment",
"flaky": "stabilization focus",
"environment": "test infrastructure fix"
}
}
```
</test-verification-strategy>
<todo-update>
Use TodoWrite tool to update test re-run todo status
</todo-update>
<record-iteration-results>
Document each iteration's results:
```json
{
"iteration": 0,
"timestamp": "ISO_timestamp",
"tests_run": 0,
"failures_before": 0,
"failures_after": 0,
"fixes_applied": [...],
"progress_made": boolean,
"continue_iteration": boolean
}
```
</record-iteration-results>
### 4. Generate Fix Report
<fix-report-generation>
Create comprehensive documentation of the fix process:
1. **Report Structure**:
```json
{
"report_sections": {
"executive_summary": {
"initial_state": "failures_count",
"final_state": "current_status",
"iterations": "fix_cycles",
"success_rate": "percentage"
},
"detailed_fixes": {
"by_iteration": [...],
"by_component": [...],
"by_failure_type": [...]
},
"remaining_issues": {
"unfixed_tests": [...],
"root_causes": [...],
"recommendations": [...]
}
}
}
```
2. **Fix Analysis**:
```json
{
"fix_effectiveness": {
"quick_fixes": "count_and_impact",
"complex_fixes": "count_and_impact",
"pattern_fixes": "reusable_solutions",
"workarounds": "temporary_solutions"
},
"code_quality_impact": {
"lines_added": 0,
"lines_modified": 0,
"complexity_increase": "minimal|moderate|significant",
"technical_debt": "assessment"
}
}
```
3. **Actionable Insights**:
- Common failure patterns identified
- Reusable fix strategies discovered
- Test suite improvements needed
- Implementation gaps found
4. **Next Steps Prioritization**:
```json
{
"immediate_actions": [
"critical_fixes_needed",
"blocking_issues"
],
"short_term": [
"test_improvements",
"code_refactoring"
],
"long_term": [
"architecture_changes",
"test_strategy_updates"
]
}
```
</fix-report-generation>
<create-fix-artifacts>
Generate all fix documentation:
1. **Markdown Report**: Human-readable fix summary
2. **JSON Summary**: Machine-readable metrics
3. **Fix Patterns**: Reusable solutions catalog
4. **Decision Log**: Why each fix was applied
</create-fix-artifacts>
<todo-update>
Use TodoWrite tool to mark documentation todo as completed
</todo-update>
### 5. Update Context
<context-update-strategy>
Update shared context with comprehensive fix results:
```json
{
"phase_completion": {
"current_phase": "test_fix",
"success_criteria": {
"all_tests_passing": boolean,
"fixes_documented": boolean,
"patterns_extracted": boolean,
"context_updated": boolean
}
},
"fix_metrics": {
"quantitative": {
"tests_fixed": 0,
"iterations_used": 0,
"time_spent": "duration",
"code_changes": 0
},
"qualitative": {
"fix_complexity": "simple|moderate|complex",
"code_quality": "improved|maintained|degraded",
"technical_debt": "reduced|unchanged|increased"
}
},
"knowledge_captured": {
"fix_patterns": [...],
"test_improvements": [...],
"architecture_insights": [...]
}
}
```
</context-update-strategy>
<decision-tree-update>
Record all fix decisions and outcomes:
```json
{
"timestamp": "ISO_timestamp",
"phase": "test_fix",
"decision_type": "fix_strategy|iteration|completion",
"decision": "what_was_decided",
"outcome": "result_of_decision",
"learning": "what_was_learned"
}
```
</decision-tree-update>
<write-updated-context>
Use Write tool to save updated context and decision tree
</write-updated-context>
### 6. Final Todo Completion
<todo-finalization>
CRITICAL: Ensure comprehensive todo completion:
1. **Todo Verification (CRITICAL FOR PHASE SUCCESS)**:
- **MUST use TodoWrite tool to list and verify ALL todos**
- **Count todos: Should be 5 for fix tasks, 1 for early exit**
- **Every todo MUST be marked as "completed"**
- **If any todo is "pending" or "in_progress": PHASE FAILS**
- **Document completion status in fix summary**
2. **Phase Success Criteria**:
```json
{
"todos_completed": true,
"fixes_documented": true,
"context_updated": true,
"reports_generated": true,
"knowledge_captured": true
}
```
</todo-finalization>
### 7. Final Summary
<phase-summary-generation>
Generate comprehensive phase completion summary:
```json
{
"phase_4b_summary": {
"execution_metrics": {
"initial_failures": 0,
"final_failures": 0,
"iterations_completed": 0,
"fixes_applied": 0,
"success_rate": "percentage"
},
"quality_improvements": {
"test_stability": "improved|unchanged",
"code_coverage": "increased|maintained",
"implementation_quality": "enhanced|maintained"
},
"knowledge_gained": {
"common_failure_patterns": [...],
"effective_fix_strategies": [...],
"test_suite_improvements": [...]
},
"next_phase_readiness": {
"all_tests_passing": boolean,
"documentation_complete": boolean,
"review_ready": boolean
}
}
}
```
</phase-summary-generation>
<phase-transition>
Prepare for Phase 5 transition:
- If all tests passing → Ready for final review
- If partial success → Document remaining issues
- If manual intervention needed → Provide clear guidance
</phase-transition>
## Key Requirements
<phase4B-constraints>
<fix-only>
This phase MUST:
□ Fix implementation to match test expectations
□ Re-run tests after each fix
□ Track all fixes applied
□ Stop after 5 iterations maximum
□ Document all changes
This phase MUST NOT:
□ Modify test files
□ Change test expectations
□ Skip or disable tests
□ Add new features
</fix-only>
<iteration-limits>
Maximum 5 fix iterations:
□ Stop if all tests pass
□ Stop if no progress made
□ Stop after 5 iterations
□ Document remaining issues
</iteration-limits>
</phase4B-constraints>
## Success Criteria
Phase 4B is successful when:
- All failing tests are fixed OR
- Maximum iterations reached with progress documented
- All fixes are tracked and reported
- Implementation matches test specifications
- Fix report generated
- **ALL TODOS MARKED AS COMPLETED using TodoWrite tool**