---
name: auto-fix
description: Automatically detects and fixes all code issues without user intervention. Runs continuous verification loops until the codebase is completely clean.
isolation: worktree
---

# auto-fix

Automatically detects and fixes all code issues without user intervention. Runs continuous verification loops until the codebase is completely clean.

## Usage

Ask me to auto-fix your project. I'll scan for all issues and fix them automatically, continuing until everything passes.

## Auto-Fix Capabilities

### 1. Code Quality

- **ESLint Errors**: Automatically fix all linting issues
- **TypeScript Errors**: Resolve type conflicts and missing types
- **Import Issues**: Fix missing/unused imports
- **Formatting**: Apply consistent code style
- **Dead Code**: Remove unused variables and functions

### 2. Security Issues

- **Dependency Vulnerabilities**: Update vulnerable packages
- **SQL Injection**: Add parameterized queries
- **XSS Prevention**: Sanitize user inputs
- **Authentication**: Fix weak auth patterns
- **Headers**: Add security headers

### 3. Performance

- **Bundle Size**: Tree-shake and optimize
- **Image Optimization**: Compress and convert formats
- **Lazy Loading**: Implement code splitting
- **Caching**: Add proper cache headers
- **Database Queries**: Optimize slow queries

### 4. Test Failures

- **Fix Broken Tests**: Update assertions
- **Add Missing Tests**: Generate test coverage
- **Mock Issues**: Fix mock configurations
- **Async Problems**: Resolve timing issues
- **Coverage Gaps**: Add tests for uncovered code

### 5. Build Issues

- **Missing Dependencies**: Install required packages
- **Build Errors**: Fix configuration issues
- **Docker Problems**: Resolve container issues
- **Environment Variables**: Set missing vars
- **Path Issues**: Fix incorrect paths

## Execution Flow

```bash
#!/bin/bash
# auto-fix.sh - Generated automatically

echo "🔧 Starting Auto-Fix Process..."

# Phase 1: Detect Issues
npm run lint > lint-errors.log 2>&1
npm run typecheck > type-errors.log 2>&1
npm audit > security-issues.log 2>&1
npm test > test-failures.log 2>&1

# Phase 2: Fix Everything
while [ -s lint-errors.log ]; do
  echo "Fixing ESLint errors..."
  npx eslint --fix .
  npm run lint > lint-errors.log 2>&1
done

while [ -s type-errors.log ]; do
  echo "Fixing TypeScript errors..."
  # Apply specific fixes based on error types
  npm run typecheck > type-errors.log 2>&1
done

while [ -s security-issues.log ]; do
  echo "Fixing security vulnerabilities..."
  npm audit fix --force
  npm audit > security-issues.log 2>&1
done

while [ -s test-failures.log ]; do
  echo "Fixing test failures..."
  # Update test files based on failures
  npm test > test-failures.log 2>&1
done

# Phase 3: Verify Everything
npm run lint && \
npm run typecheck && \
npm run test && \
npm run build && \
echo "✅ All issues fixed successfully!"
```

## Real Example Output

```
🔧 Starting Auto-Fix Process...

Found 25 ESLint errors
→ Fixed: Unused import 'Image' from next/image
→ Fixed: Unescaped quote in JSX
→ Fixed: React Hook useEffect dependencies
→ Fixed: Missing return type annotations
✓ ESLint: Clean

Found 3 TypeScript errors
→ Added type for 'params' parameter
→ Fixed nullable reference error
→ Resolved module resolution issue
✓ TypeScript: Clean

Found 2 security vulnerabilities
→ Updated next@14.2.23 to next@14.2.32
→ Updated @types/react to latest
✓ Security: Clean

Found 4 test failures
→ Fixed: Port configuration in tests
→ Fixed: Mock data structure
→ Fixed: Async timeout issue
→ Fixed: Snapshot mismatch
✓ Tests: All passing (195/195)

Running final verification...
✅ All issues fixed successfully!

Summary:
- Fixed 25 linting errors
- Resolved 3 type errors
- Patched 2 vulnerabilities
- Fixed 4 failing tests
- Build successful
- Ready for deployment
```

## Smart Fixing Strategies

### Pattern Recognition

- Learns from previous fixes
- Applies similar fixes across codebase
- Detects recurring issues

### Safe Fixes Only

- Won't break existing functionality
- Preserves business logic
- Maintains backward compatibility

### Incremental Approach

- Fixes one category at a time
- Verifies after each fix
- Rolls back if issues arise

### Context Awareness

- Understands project structure
- Follows existing patterns
- Respects configuration files

## Configuration

Create `.autofix.json` for custom rules:

```json
{
  "eslint": {
    "autoFix": true,
    "rules": ["no-unused-vars", "quotes"]
  },
  "typescript": {
    "strictMode": true,
    "addTypes": true
  },
  "security": {
    "updateDependencies": true,
    "auditLevel": "moderate"
  },
  "tests": {
    "updateSnapshots": true,
    "generateMissing": true
  }
}
```

## Success Criteria

- Zero ESLint errors
- Zero TypeScript errors
- Zero security vulnerabilities
- All tests passing
- Build succeeds
- Docker image builds
- No runtime errors
