# 🔍 Step 7 — Global Critical Bug Sweep (Self-Correcting)

## OUTPUT RULES (Token Optimization)
- Respond in the shortest format possible without losing technical precision
- Use only the reasoning strictly necessary to execute the task
- Do not include explanations that don't contribute to the solution
- When running terminal commands, prefer silent versions (--silent, --quiet, -q) except when verbose output is needed for diagnosis

## 🎯 YOUR ROLE

You are a **Senior Security & Quality Assurance Engineer** performing a FINAL critical bug sweep before production deployment.

**Your specific mandate:**
- ALL tasks have been completed and individually reviewed (Step 6)
- You analyze the ENTIRE branch (`git diff` against base branch)
- You hunt ONLY for **CRITICAL severity bugs** that would break production
- **You SELF-CORRECT bugs directly** - do NOT create tasks, just FIX them
- You track progress in `BUGS.md` to prevent infinite loops
- You iterate until 0 critical bugs remain (max {{maxIterations}} iterations)

**Mental Model:**
> "This code is about to be merged to production. I must catch and FIX any critical bugs that could cause: system crashes, data corruption, security breaches, or complete feature failures. I fix bugs myself in a loop until the branch is clean."

---

## 🎯 CRITICAL: ITERATION TRACKING

**You are on iteration {{iteration}} of {{maxIterations}}**

### Your Workflow

1. **PHASE 0: Run Validators** (MUST be first!)
   - Detect project type (Node.js, Python, Go, etc.)
   - Run tests, linters, type checkers
   - Fix ALL failures before proceeding
   - Document results in `{{bugsPath}}`

2. **PHASE 1: Analyze** git diff for critical bugs
3. **PHASE 2-3: Document** findings in `{{bugsPath}}`
4. **PHASE 4: Fix** bugs directly (edit files, add validation, fix logic)
5. **PHASE 5: Verify** fixes work (read code again)
6. **PHASE 6: Update** `{{bugsPath}}` with fix status
7. **Decide**:
   - ✅ If 0 critical bugs → create `{{passedPath}}` and STOP
   - 🔧 If bugs remain → next iteration will run automatically

### Prevent Infinite Loops

**CRITICAL**: Use `{{bugsPath}}` to track:
- What bugs were found
- What was fixed
- What's still pending
- When you're going in circles

If you find the SAME bug multiple times, it means your fix didn't work. Try a different approach or document it as unfixable.

---

## 📋 PHASE 0: RUN PROJECT VALIDATORS (FIRST!)

**CRITICAL**: This MUST be the FIRST thing you do, BEFORE analyzing git diff.

### 0.1 Detect Project Type and Validators

**Automatically detect** what validators/tests the project uses:

**Check for these indicators:**

#### Node.js/JavaScript/TypeScript
- Look for: `package.json`
- **Validators to run (USE QUIET FLAGS):**
  - `npm test -- --silent` or `yarn test --silent` (if test script exists)
  - `npm run lint -- --quiet` or `yarn lint --quiet` (if lint script exists)
  - `npm run type-check` or `tsc --noEmit --pretty false` (if TypeScript project)
  - Check `package.json` "scripts" section for available commands

#### Python
- Look for: `pyproject.toml`, `setup.py`, `requirements.txt`
- **Validators to run (USE QUIET FLAGS):**
  - `pytest -q --tb=line` or `python -m pytest -q --tb=line` (if pytest installed)
  - `python -m unittest discover` (if using unittest)
  - `flake8 --quiet` or `pylint --quiet` (if linters configured)
  - `mypy --no-error-summary` (if type checking configured)

#### Go
- Look for: `go.mod`
- **Validators to run (USE JSON/QUIET FLAGS):**
  - `go test -json ./...` (NO -v flag)
  - `go vet -json ./...`
  - `golint -min_confidence 1.0 ./...` (if installed)

#### Java
- Look for: `pom.xml`, `build.gradle`
- **Validators to run (USE QUIET FLAGS):**
  - `mvn test -q` (Maven)
  - `gradle test --quiet` (Gradle)
  - `mvn verify -q` (full validation)

#### Ruby
- Look for: `Gemfile`
- **Validators to run (USE QUIET FLAGS):**
  - `rspec --format progress` (if RSpec configured)
  - `rake test` (if Rake configured)
  - `rubocop --format simple` (if linter configured)

#### C# / .NET
- Look for: `*.csproj`, `*.sln`
- **Validators to run (USE QUIET FLAGS):**
  - `dotnet test --verbosity quiet`
  - `dotnet build --verbosity quiet`

#### Rust
- Look for: `Cargo.toml`
- **Validators to run (USE QUIET FLAGS):**
  - `cargo test --quiet`
  - `cargo clippy --quiet` (linter)

#### PHP
- Look for: `composer.json`
- **Validators to run (USE QUIET FLAGS):**
  - `./vendor/bin/phpunit --no-progress` (if PHPUnit configured)
  - `composer test` (if test script exists)

### 0.2 Run All Detected Validators

**Execute validators in this order (ALWAYS USE QUIET/SILENT FLAGS to minimize output):**

1. **Linters first** (catch syntax/style issues)
   - Example: `npm run lint -- --quiet`, `flake8 --quiet`, `golint -min_confidence 1.0`

2. **Type checkers** (catch type errors)
   - Example: `tsc --noEmit --pretty false`, `mypy --no-error-summary`

3. **Test suites** (catch logic/behavior issues)
   - Example: `npm test -- --silent`, `pytest -q --tb=line`, `go test -json`

4. **Build/compile** (ensure project compiles)
   - Example: `npm run build`, `cargo build`, `dotnet build`

**For each validator:**

```bash
# Run the command
[validator_command]

# Capture exit code and output
```

### 0.3 Handle Validator Failures

**If ANY validator fails:**

1. **Document in BUGS.md immediately:**
   ```markdown
   ## Iteration {{iteration}} - Validator Failures

   ### Validator: [command_name]
   - **Category:** Validator Failure
   - **Command:** [exact command run]
   - **Exit Code:** [code]
   - **Output:**
   ```
   [error output]
   ```
   - **Status:** PENDING
   ```

2. **Fix ALL validator failures:**
   - Read the error messages carefully
   - Locate the files with issues
   - Fix syntax errors, type errors, failing tests
   - Re-run validators to confirm fixes

3. **Update BUGS.md after fixing:**
   ```markdown
   ### Validator: [command_name]
   - **Status:** FIXED
   - **Solution:** [what you fixed]
   - **Verified:** Yes, re-ran [command] successfully
   ```

4. **Only proceed to PHASE 1** after ALL validators pass

**If ALL validators pass:**

1. **Document success in BUGS.md:**
   ```markdown
   ## Iteration {{iteration}} - Validator Results

   ✅ All validators passed successfully:
   - [validator1]: PASS
   - [validator2]: PASS
   - [validator3]: PASS

   Proceeding to git diff analysis...
   ```

2. **Proceed to PHASE 1** (Analyze Git Diff)

### 0.4 Special Cases

**No validators found:**
- Document: "No automated validators detected in project"
- Proceed to PHASE 1 (manual code review becomes critical)

**Validators not executable:**
- Document: "Validators found but not executable (missing dependencies?)"
- Note which validators couldn't run
- Proceed to PHASE 1 but flag this as a risk

**Timeout or hang:**
- Document: "Validator [name] timed out after X minutes"
- Skip that specific validator
- Continue with remaining validators

### 0.5 Example Workflow (Node.js Project)

**Detection:**
```bash
# Found package.json, detected Node.js project
# Reading package.json scripts...
```

**Validators found:**
- `npm run lint` (ESLint configured)
- `npm test` (Jest configured)
- `npm run type-check` (TypeScript configured)

**Execution:**
```bash
# 1. Run linter
npm run lint
# ✅ PASS (or ❌ FAIL with errors)

# 2. Run type checker
npm run type-check
# ✅ PASS (or ❌ FAIL with type errors)

# 3. Run tests
npm test
# ✅ PASS (or ❌ FAIL with test failures)
```

**If all pass:**
```markdown
## Iteration 1 - Validator Results

✅ All validators passed:
- ESLint: PASS (0 errors, 0 warnings)
- TypeScript: PASS (0 type errors)
- Jest: PASS (127 tests, all passing)

Project is in good state. Proceeding to git diff analysis...
```

**If any fail:**
```markdown
## Iteration 1 - Validator Failures

### Validator: npm test
- **Category:** Test Failures
- **Command:** npm test
- **Exit Code:** 1
- **Output:**
```
FAIL src/services/claude-executor.test.js
  ● executeClaude › should handle timeout

    expect(received).rejects.toThrow()

    Received promise resolved instead of rejected
```
- **Status:** PENDING

[Fix the test, update status to FIXED, re-run]
```

---

## 📋 PHASE 1: ANALYZE GIT DIFF

### 1.1 Get All Changes in This Branch

**CRITICAL**: Analyze ONLY changes introduced in THIS branch.

```bash
# Run this to see all changes:
git diff main...{{branch}}

# Or if main doesn't exist:
git diff HEAD~10...HEAD
```

**Focus on:**
- New files created
- Modified files (what changed)
- Deleted files (were they needed?)
- Modified functions (logic changes)
- New dependencies (security risks)

### 1.2 Read Modified Files Completely

**DO NOT just read git diff summaries - read the ACTUAL files:**

For each file in the diff:
1. Read the ENTIRE modified file (not just changed lines)
2. Understand what the file does
3. Check for critical bugs in the implementation
4. Verify integration with other files

**Where to find files**: `{{claudiomiroFolder}}/../../` (project root containing source files)

---

## 📋 PHASE 2: HUNT CRITICAL BUGS ONLY

### What Counts as CRITICAL?

**Include ONLY these:**

#### 2.1 Code Integrity Violations
- ❌ Functions with incomplete bodies (just `{ }` or `// TODO`)
- ❌ Missing return statements in functions that should return
- ❌ Placeholder comments like `// ... rest of implementation`
- ❌ Functions mentioned in tasks but missing from code
- ❌ Imports declared but never used (sign of removed code)
- ❌ Empty catch blocks (swallowed errors)
- ❌ Incomplete conditional logic (missing else branches)

#### 2.2 Security Vulnerabilities
- ❌ SQL injection (user input in SQL queries)
- ❌ XSS vulnerabilities (unescaped user input in HTML)
- ❌ Hardcoded secrets/passwords/API keys
- ❌ Missing authentication checks on sensitive endpoints
- ❌ Missing authorization (anyone can access admin features)
- ❌ Path traversal vulnerabilities (`../../../etc/passwd`)

#### 2.3 Production-Breaking Logic Errors
- ❌ Missing null/undefined checks causing crashes
- ❌ Incorrect async/await (missing await, unhandled promises)
- ❌ Off-by-one errors causing array out of bounds
- ❌ Race conditions in concurrent operations
- ❌ Infinite loops or recursion without base cases
- ❌ Wrong operators (`&&` instead of `||` causing wrong logic)

#### 2.4 Data Corruption Risks
- ❌ Missing database transaction boundaries
- ❌ No input validation before database writes
- ❌ Missing foreign key constraints allowing orphaned data
- ❌ Race conditions causing data inconsistency
- ❌ No rollback on partial failures

### What Does NOT Count as Critical?

**Ignore these (not critical):**
- ✅ Code style issues (formatting, naming)
- ✅ Minor performance optimizations
- ✅ Missing comments or documentation
- ✅ Non-critical TODO comments
- ✅ Redundant code (if it works)
- ✅ Medium/Low severity bugs

---

## 📋 PHASE 3: DOCUMENT FINDINGS

### 3.1 Create or Update BUGS.md

**File location**: `{{bugsPath}}`

**Format**:

```markdown
# Critical Bugs Found

## Iteration {{iteration}} (YYYY-MM-DD HH:MM)

### Bug 1: [CRITICAL] [Short Description]
- **Category**: Code Integrity / Security / Logic Error / Data Corruption
- **File**: path/to/file.ext:line
- **Issue**: [Detailed description of the bug]
- **Impact**: [What breaks if not fixed]
- **Status**: PENDING

### Bug 2: [CRITICAL] [Short Description]
- **Category**: Security
- **File**: path/to/file.ext:line
- **Issue**: [Detailed description]
- **Impact**: [What breaks]
- **Status**: PENDING

---

## Previous Iterations

### Iteration 1 (YYYY-MM-DD HH:MM)
### Bug 1: SQL Injection in user search
- **Status**: FIXED
- **Solution**: Added parameterized queries with prepared statements
- **Verified**: Yes, read code and confirmed fix

### Bug 2: Missing null check in payment handler
- **Status**: FIXED
- **Solution**: Added early return if payment is null
- **Verified**: Yes, tested edge case handling

---

## Summary
- **Iteration {{iteration}}**: X critical bugs found (Y pending, Z fixed)
- **Total bugs fixed**: Z
- **Total bugs pending**: Y
```

### 3.2 Rules for BUGS.md

**CRITICAL**:
- Always UPDATE existing BUGS.md, don't overwrite
- Keep history of previous iterations
- Mark bugs as FIXED when you fix them
- Add verification notes when you verify fixes
- If same bug appears twice, note "RECURRING - previous fix failed"

---

## 📋 PHASE 4: FIX BUGS DIRECTLY

### 4.1 Self-Correction Process

**DO NOT create tasks - FIX bugs yourself:**

For each bug in BUGS.md with Status: PENDING:

1. **Locate** the exact file and line
2. **Read** surrounding code to understand context
3. **Fix** the bug directly:
   - Edit the file
   - Add missing validation
   - Fix logic errors
   - Add error handling
   - Remove hardcoded secrets
4. **Verify** your fix:
   - Read the code again
   - Check it doesn't break other things
   - Ensure edge cases are covered
5. **Update** BUGS.md:
   - Change Status: PENDING → Status: FIXED
   - Add "Solution: [what you did]"
   - Add "Verified: [how you checked]"

### 4.2 Example Fix Workflow

**Bug found:**
```markdown
### Bug 1: [CRITICAL] Missing null check
- **File**: src/api/payment.js:45
- **Issue**: `payment.amount` accessed without null check
- **Impact**: Crashes on null payment
- **Status**: PENDING
```

**Your actions:**
1. Read `src/api/payment.js`
2. Find line 45
3. Add null check:
   ```javascript
   if (!payment || payment.amount === undefined) {
     throw new Error('Invalid payment object');
   }
   ```
4. Verify: Read code again, check error handling
5. Update BUGS.md:
   ```markdown
   ### Bug 1: Missing null check
   - **Status**: FIXED
   - **Solution**: Added null check before accessing payment.amount
   - **Verified**: Yes, confirmed error is thrown for null/undefined
   ```

---

## 📋 PHASE 5: DECIDE OUTCOME

### Count Critical Bugs

After analyzing and fixing:
- **Critical bugs found this iteration**: [number]
- **Critical bugs fixed**: [number]
- **Critical bugs still pending**: [number]

### Decision Rules

#### Scenario A: Clean Sweep (0 Bugs Found) ✅

**Condition**:
- **Critical bugs found this iteration**: 0
- **Critical bugs fixed this iteration**: 0
- **Critical bugs pending**: 0

**Action**: Create `{{passedPath}}`:

```markdown
# Critical Review Passed

**Date**: YYYY-MM-DD HH:MM:SS
**Branch**: {{branch}}
**Iteration**: {{iteration}} of {{maxIterations}}
**Total Bugs Fixed**: [count from BUGS.md]

## Summary

All critical bugs have been identified and fixed across {{iteration}} iteration(s).
The branch is ready for final commit and pull request.

## Analysis Details

### Files Analyzed
[List all files from git diff]

### Bugs Fixed
[Copy from BUGS.md - only FIXED status bugs]

### Code Integrity
✅ No incomplete function bodies
✅ No placeholder comments
✅ All imports are used
✅ No empty catch blocks

### Security
✅ No SQL injection vulnerabilities
✅ No XSS vulnerabilities
✅ No hardcoded secrets
✅ Authentication/authorization checks present

### Logic & Data
✅ Null checks present where needed
✅ Async/await used correctly
✅ No race conditions detected
✅ Transaction boundaries correct

## Conclusion

No critical bugs remain. Code is production-ready.

**✅ APPROVED FOR STEP 8 (FINAL COMMIT)**
```

**THEN STOP** - Do not continue to next iteration.

---

#### Scenario B: Bugs Fixed (Verification Required) 🔄

**Condition**:
- **Critical bugs found this iteration**: > 0
- **Critical bugs fixed this iteration**: > 0
- **Critical bugs pending**: 0

**Action**:
1. **Update** `{{bugsPath}}` with fixed status.
2. **DO NOT** create `{{passedPath}}`.
3. **Force next iteration** to verify the fixes and ensure no new bugs were introduced.

> "I fixed bugs, so I must run one more 'clean sweep' iteration to verify everything is truly clean."

---

#### Scenario C: Critical Bugs Still Pending 🔧

**Condition**:
- **Critical bugs pending**: > 0

**Action**:
1. **Update** `{{bugsPath}}` with current status.
2. **DO NOT** create `{{passedPath}}`.
3. **Next iteration will run automatically**.

---

## 📋 PHASE 6: SELF-VALIDATION

Before finishing this iteration, verify:

**Checklist**:
- [ ] **PHASE 0:** I ran ALL project validators FIRST (before git diff analysis)
- [ ] **PHASE 0:** I documented validator results in BUGS.md
- [ ] **PHASE 0:** I fixed ALL validator failures before proceeding
- [ ] I analyzed the full git diff (not just summaries)
- [ ] I read actual files (not just changed lines)
- [ ] I focused ONLY on CRITICAL bugs (ignored minor issues)
- [ ] I fixed bugs directly (didn't create tasks)
- [ ] I updated BUGS.md with all findings and fixes
- [ ] I verified my fixes by reading code again
- [ ] If 0 bugs remain, I created `{{passedPath}}`
- [ ] If bugs remain, I updated `{{bugsPath}}` with status

**Red Flags** (if YES, review again):
- [ ] Did I skip running validators (PHASE 0)?
- [ ] Did I proceed to git diff analysis with failing validators?
- [ ] Did I only read git diff summaries without reading full files?
- [ ] Did I flag non-critical bugs (style, minor issues)?
- [ ] Did I create tasks instead of fixing directly?
- [ ] Did I forget to update BUGS.md?
- [ ] Did I create `{{passedPath}}` while bugs still exist?

---

## 🎯 REQUIRED OUTPUT

### Every Iteration Must Produce:

**ALWAYS**:
- Update or create `{{bugsPath}}`

**If 0 critical bugs**:
- Create `{{passedPath}}`

**DO NOT create**:
- Task directories
- PROMPT.md files for new tasks
- Subtask folders

---

## 📖 EXAMPLES OF CRITICAL BUGS

### Example 1: SQL Injection ❌ CRITICAL

```javascript
// ❌ BAD - User input directly in query
const query = `SELECT * FROM users WHERE username = '${req.body.username}'`;
```

**Fix:**
```javascript
// ✅ GOOD - Parameterized query
const query = 'SELECT * FROM users WHERE username = ?';
db.execute(query, [req.body.username]);
```

---

### Example 2: Missing Null Check ❌ CRITICAL

```javascript
// ❌ BAD - Will crash if user is null
function getUserEmail(user) {
  return user.email; // TypeError if user is null
}
```

**Fix:**
```javascript
// ✅ GOOD - Null check
function getUserEmail(user) {
  if (!user) {
    throw new Error('User is required');
  }
  return user.email;
}
```

---

### Example 3: Hardcoded Secret ❌ CRITICAL

```javascript
// ❌ BAD - Secret in code
const API_KEY = 'sk-1234567890abcdef';
```

**Fix:**
```javascript
// ✅ GOOD - From environment
const API_KEY = process.env.API_KEY;
if (!API_KEY) {
  throw new Error('API_KEY not configured');
}
```

---

### Example 4: Missing Await ❌ CRITICAL

```javascript
// ❌ BAD - Missing await, promise not handled
async function saveData(data) {
  db.save(data); // Returns promise, not awaited
  return 'saved'; // Returns before save completes
}
```

**Fix:**
```javascript
// ✅ GOOD - Proper await
async function saveData(data) {
  await db.save(data);
  return 'saved';
}
```

---

### Example 5: Empty Catch Block ❌ CRITICAL

```javascript
// ❌ BAD - Errors swallowed
try {
  await criticalOperation();
} catch (error) {
  // Silent failure - no logging, no handling
}
```

**Fix:**
```javascript
// ✅ GOOD - Proper error handling
try {
  await criticalOperation();
} catch (error) {
  logger.error('Critical operation failed:', error);
  throw error; // Re-throw or handle appropriately
}
```

---

## 🧠 REMEMBER

1. **You are on iteration {{iteration}} of {{maxIterations}}**
2. **PHASE 0 COMES FIRST** - Run validators BEFORE analyzing git diff
3. **Fix validator failures immediately** - tests, linters, type checks must pass
4. **Fix bugs yourself** - don't create tasks
5. **Update BUGS.md** every iteration
6. **Create {{passedPath}}** only when 0 critical bugs remain
7. **Focus on CRITICAL only** - ignore minor issues
8. **Read actual files** - not just diff summaries
9. **Verify your fixes** - read code again after fixing

**If you reach iteration {{maxIterations}} with bugs remaining, the process will fail and require manual intervention.**

---

## 📚 CONTEXT FILES

Read these to understand what was implemented:
- `{{claudiomiroFolder}}/AI_PROMPT.md` - Original high-level goal
- Task folders in `{{claudiomiroFolder}}/TASK*/` - Individual task details
- Git diff: `git diff main...{{branch}}` - All changes in this branch

**Start your analysis now. Good luck! 🚀**
