# Phase 01.9: Detect Previous QA Failures [MANDATORY]

**Check if story has failed QA due to deferral or other issues:**

```
# Search for QA reports for this story
Glob(pattern="devforgeai/qa/reports/${STORY_ID}-qa-report*.md")

IF reports found:
    # Read most recent report
    reports_sorted = sort_by_timestamp(reports)
    latest_report = reports_sorted[0]

    Read(file_path=latest_report)

    # Parse QA status
    IF report contains "Status: FAILED":
        # Extract failure type
        IF report contains "Deferral Validation FAILED":
            Display: "⚠ Previous QA failed due to deferral issues"
            Display: "  QA Report: {latest_report}"
            $QA_DEFERRAL_FAILURE = true
            $QA_FAILURE_REPORT = latest_report

        ELIF report contains "Coverage Below Threshold":
            Display: "⚠ Previous QA failed due to coverage issues"
            Display: "  Focus: Increase test coverage"
            $QA_COVERAGE_FAILURE = true

        ELIF report contains "Anti-Pattern Violations":
            Display: "⚠ Previous QA failed due to anti-patterns"
            Display: "  Focus: Refactor to remove violations"
            $QA_ANTIPATTERN_FAILURE = true

        ELSE:
            Display: "⚠ Previous QA failed (review report for details)"
            Display: "  Report: {latest_report}"
            $QA_GENERIC_FAILURE = true

    ELIF report contains "Status: PASSED":
        Display: "Note: QA already passed for this story"
        Display: "  Proceeding with development (may be enhancement or bug fix)"
        $QA_PASSED = true

ELSE:
    # No QA reports found - first development iteration
    Display: "✓ First development iteration (no previous QA attempts)"
    $QA_FIRST_ITERATION = true
```

**Token cost:** ~1,500 tokens

**Use in subsequent phases:**
- If `$QA_DEFERRAL_FAILURE == true` → Invoke "Handling QA Deferral Failures" workflow
- If `$QA_COVERAGE_FAILURE == true` → Focus on test coverage in Phase 02
- If `$QA_ANTIPATTERN_FAILURE == true` → Extra validation in Phase 04 (Refactor)

---

# Phase 01.9.5: Load Structured Gap Data (gaps.json) [IF QA FAILED]

**Purpose:** Parse machine-readable gap data for targeted remediation workflow.

**When to execute:** After Phase 01.9 detects `$QA_COVERAGE_FAILURE`, `$QA_ANTIPATTERN_FAILURE`, or `$QA_DEFERRAL_FAILURE`

```
# Check if structured gap export exists
gaps_file = "devforgeai/qa/reports/${STORY_ID}-gaps.json"

Glob(pattern=gaps_file)

IF gaps_file EXISTS:
    Display: ""
    Display: "╔═══════════════════════════════════════════════════════════════╗"
    Display: "║  📊 STRUCTURED GAP DATA DETECTED                              ║"
    Display: "╠═══════════════════════════════════════════════════════════════╣"
    Display: "║                                                               ║"
    Display: "║  QA-Dev Integration: ACTIVE                                   ║"
    Display: "║  Gap file: {gaps_file}                                        ║"
    Display: "║                                                               ║"
    Display: "║  Development will enter REMEDIATION MODE:                     ║"
    Display: "║  • Targeted test generation for specific gaps                 ║"
    Display: "║  • Focus on coverage failures by file                         ║"
    Display: "║  • Suggested tests provided                                   ║"
    Display: "║                                                               ║"
    Display: "╚═══════════════════════════════════════════════════════════════╝"
    Display: ""

    # Read and parse gaps.json
    Read(file_path=gaps_file)
    gaps_data = parse_json(file_content)

    # Build arrays for Phase 02-06 consumption
    $QA_COVERAGE_GAPS = gaps_data.coverage_gaps
    $QA_ANTIPATTERN_GAPS = gaps_data.anti_pattern_violations
    $QA_DEFERRAL_GAPS = gaps_data.deferral_issues

    # Display summary
    IF $QA_COVERAGE_GAPS.count > 0:
        Display: "📉 Coverage Gaps: {count} files below threshold"

    IF $QA_ANTIPATTERN_GAPS.count > 0:
        Display: "⚠️  Anti-Pattern Violations: {count} issues to resolve"

    IF $QA_DEFERRAL_GAPS.count > 0:
        Display: "📋 Deferral Issues: {count} items need resolution"

    $REMEDIATION_MODE = true
    Display: "✅ Remediation mode enabled"

ELSE:
    Display: "ℹ️  No structured gap data (gaps.json) found"
    Display: "   Using legacy QA report parsing for failure context"
    $REMEDIATION_MODE = false
```

**Token cost:** ~800 tokens

**Variables set for Phases 02-06:**
- `$QA_COVERAGE_GAPS` - Array of coverage gap objects
- `$QA_ANTIPATTERN_GAPS` - Array of anti-pattern violations
- `$QA_DEFERRAL_GAPS` - Array of deferral issues
- `$REMEDIATION_MODE` - Boolean flag for targeted workflow
