# Phase 01.2.5: Dependency Graph Validation [MANDATORY]

**Purpose:** Validate story dependencies before TDD workflow begins (STORY-093).

**When to execute:** After git-worktree-manager (Phase 01.2), before workflow adaptation (Phase 01.3).

**Pre-check: Empty depends_on optimization:**

```
# Check if story has any dependencies
IF depends_on is empty OR depends_on == []:
    Display: "✓ No dependencies declared - skipping dependency validation"
    SKIP to Phase 01.3
    RETURN
```

**Invoke dependency-graph-analyzer subagent:**

```
Task(
  subagent_type="dependency-graph-analyzer",
  description="Validate dependencies for ${STORY_ID}",
  prompt="Analyze dependencies for story ${STORY_ID}.

    Story path: devforgeai/specs/Stories/

    Tasks:
    1. Extract depends_on from story frontmatter
    2. Build dependency graph with transitive resolution
    3. Detect circular dependencies
    4. Validate all dependency statuses
    5. Generate chain visualization

    Return JSON with validation results.
    BLOCKING: Return blocking=true if any dependency is invalid."
)
```

**Parse subagent response:**

```javascript
result = parse_json(subagent_output)

IF result.status == "PASS":
    Display: "✓ Dependency validation passed"
    Display: "  Dependencies: {result.dependencies.total_count}"
    Display: result.chain_visualization
    // Continue to Phase 01.3

ELIF result.status == "BLOCKED":
    // Check for --force flag
    IF $FORCE_FLAG == true:
        // Log bypass to audit file
        timestamp = current_datetime_iso()
        log_path = "devforgeai/logs/dependency-bypass-{timestamp}.log"
        Write(file_path=log_path, content=bypass_log)
        Display: "⚠️  DEPENDENCY CHECK BYPASSED (--force flag)"
        // Continue to Phase 01.3

    ELSE:
        // Block execution
        Display: "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        Display: "❌ DEPENDENCY VALIDATION FAILED"
        Display: "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

        IF result.validation.cycle_detected:
            Display: "🔄 CIRCULAR DEPENDENCY DETECTED"
            Display: "Cycle: {' → '.join(result.validation.cycle_path)}"
            Display: "Resolution: Remove circular reference in one of the story files."

        ELIF result.validation.missing.length > 0:
            Display: "❓ MISSING DEPENDENCIES"
            FOR dep in result.validation.missing:
                Display: "  • {dep} - Story file not found"
            Display: "Resolution: Create missing story files or remove dependency."

        ELSE:
            Display: "⏳ DEPENDENCIES NOT READY"
            FOR failure in result.validation.failures:
                Display: "  • {failure.message}"
                IF failure.suggestion:
                    Display: "    → {failure.suggestion}"

        Display: ""
        Display: "Dependency chain:"
        Display: result.chain_visualization
        Display: ""
        Display: "Options:"
        Display: "  1. Complete dependent stories first"
        Display: "  2. Run with --force flag to bypass:"
        Display: "     /dev {STORY_ID} --force"

        HALT workflow

ELIF result.status == "ERROR":
    Display: "❌ Dependency analysis error: {result.error}"
    Display: "Proceeding with caution..."
    // Continue to Phase 01.3 (graceful degradation)
```

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

**References:**
- Subagent: `.claude/agents/dependency-graph-analyzer.md`
- Story: STORY-093 - Dependency Graph Enforcement
