๐Ÿšจ CRITICAL: Migration Hangs Due to Circular Dependency Resolution in BookLoader

Resolved ๐Ÿ’ฌ 3 comments Opened Sep 1, 2025 by EaCognitive Closed Nov 29, 2025

๐Ÿšจ CRITICAL BLOCKING ISSUE

The MongoDB โ†’ PostgreSQL migration system is experiencing critical hanging issues during the book loading phase due to circular dependency resolution failures. This prevents completion of the full migration process.

๐Ÿ“Š Impact Assessment

  • Current Status: Only 3 books successfully migrated out of 6,565 expected
  • Migration Completion: 0% (hanging prevents completion)
  • Affected Component: BookLoader dependency resolution system
  • Business Impact: Complete migration pipeline blockage

๐Ÿ” Root Cause Analysis

Primary Issue: Infinite Loop in Dependency Resolution

Location: pkg/migration/dependencies/dependency_resolver.go:163-212

The ExpandTitles() method contains a hardcoded iteration limit that gets exceeded during dependency graph traversal:

// Current problematic code
maxIterations := 100 // Much smaller limit for performance  
iterations := 0

for len(queue) > 0 && iterations < maxIterations {
    iterations++
    // ... processing logic
    if limit > 0 && len(needed) >= limit {
        break
    }
}

if iterations >= maxIterations {
    logger.LogWarn(fmt.Sprintf("ExpandTitles hit max iterations limit (%d) - possible circular dependency", iterations), "MIGRATION")
}

Observable Symptoms

  • Migration hangs with warning: "ExpandTitles hit max iterations limit"
  • Books loading stops after processing only 3 books
  • System appears frozen during Phase 1 book processing
  • No graceful error handling or recovery

๐Ÿ› ๏ธ DEBUGGING METHODOLOGY โš ๏ธ

CRITICAL: Always use the standardized debugging approach:

โœ… REQUIRED Commands

# ALWAYS use this command for debugging
make migrate-strict

# For incremental testing  
make migrate-strict ARGS="--limit 1"    # Single book test
make migrate-strict ARGS="--limit 100"  # Moderate test
make migrate-strict ARGS="--limit 0"    # Full migration (currently fails)

โœ… REQUIRED Investigation Tools

  • MCP MongoDB Tools: Investigate Sefaria database through MCP tool interface
  • MCP PostgreSQL Tools: Validate target database state
  • Log Analysis: Always inspect most recent logs first
  • Database Verification: Check for hanging processes and data integrity

โŒ NEVER Use

  • Direct script execution
  • Other migration commands besides make migrate-strict
  • Manual database operations outside MCP tools

๐Ÿ“ˆ Migration Status Summary

โœ… Issues Fixed

  1. --limit Flag Handling: Fixed --limit 0 (unlimited) flag recognition
  2. Log References: Corrected "index collection" โ†’ "indexmap collection"
  3. Metrics Tracking: Fixed "books" โ†’ "books_loaded" metric collection
  4. Code Cleanup: Removed deprecated schema synchronization functions

๐Ÿšจ Critical Outstanding Issues

1. Circular Dependency Hanging (BLOCKING)
  • Symptom: Migration hangs during Phase 1 with "ExpandTitles hit max iterations limit"
  • Root Cause: Circular references in book dependency graph cause infinite recursion
  • Impact: Cannot complete migration with any moderate limit (>= 100 books)
  • Solution Needed: Implement proper cycle detection in dependency resolver
2. Missing Cycle Detection Algorithm
  • Current State: Queue-based traversal without visited node tracking
  • Problem: No detection mechanism for dependency cycles
  • Required Fix: Add visited := make(map[string]bool) pattern with cycle detection

๐Ÿ’ก Immediate Technical Solutions

1. Implement Cycle Detection

Add proper cycle detection to ExpandTitles() method:

// Required fix pattern
func (dr *DependencyResolver) ExpandTitles(ctx context.Context, titles []string, limit int) ([]string, error) {
    visited := make(map[string]bool)
    processing := make(map[string]bool) // Track currently processing nodes
    
    for len(queue) > 0 {
        current := queue[0]
        queue = queue[1:]
        
        // Cycle detection
        if processing[current] {
            logger.LogWarn(fmt.Sprintf("Circular dependency detected for: %s", current), "MIGRATION")
            continue // Skip circular reference
        }
        
        if visited[current] {
            continue // Already processed
        }
        
        processing[current] = true
        // ... process dependencies
        processing[current] = false
        visited[current] = true
    }
}

2. Add Context Timeout

Implement timeout for dependency resolution:

func (dr *DependencyResolver) ExpandTitles(ctx context.Context, titles []string, limit int) ([]string, error) {
    timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
    defer cancel()
    
    // Process with timeout context
}

3. Graceful Error Recovery

Replace hanging behavior with controlled failure:

  • Log circular dependency chains
  • Continue processing non-circular dependencies
  • Provide actionable error messages

๐Ÿ“‹ Testing Requirements

Validation Steps

  1. Unit Test: make migrate-strict ARGS="--limit 1" (should complete quickly)
  2. Integration Test: make migrate-strict ARGS="--limit 100" (should not hang)
  3. Full Migration: make migrate-strict ARGS="--limit 0" (ultimate goal)

Success Criteria

  • [ ] No "ExpandTitles hit max iterations limit" warnings
  • [ ] All 6,565 books successfully processed
  • [ ] Migration completes within reasonable time bounds (<2 hours)
  • [ ] Proper handling of circular dependencies with logging

๐Ÿ”ง Additional Context

LLM Schema Generation Status: โœ… AVAILABLE

Correction to debugging summary: LLM schema generation system is fully functional

  • Location: llm_api/src/schema_generator_production.py
  • Capabilities: Can generate schemas for all 6,000+ books
  • Integration: Ready for production use with OpenAI API

Architecture Notes

  • Migration Type: MongoDB (source) โ†’ PostgreSQL (target)
  • Processing: Parallel worker pools with circuit breaker patterns
  • Configuration: Extensive Makefile with 100+ specialized targets
  • Monitoring: Real-time progress tracking and dashboard capabilities

๐ŸŽฏ Next Steps

  1. Immediate: Implement cycle detection in ExpandTitles() method
  2. Validation: Test with make migrate-strict using incremental limits
  3. Monitoring: Use MCP tools to validate database states during testing
  4. Documentation: Update migration guides with proper debugging workflow

โš ๏ธ Important Notes

  • Schema Issues Are Bugs: Not cosmetic issues - investigate through MCP MongoDB tool
  • Incremental Debugging: Always start with make migrate-strict and investigate failures step-by-step
  • Database Inspection: Use MCP tools to examine both MongoDB and PostgreSQL states
  • No Silent Errors: Address root causes, don't skip or silence errors

---
Priority: ๐Ÿ”ด CRITICAL - BLOCKING PRODUCTION MIGRATION
Assignment: Immediate attention required for dependency resolution expert
Timeline: Fix required before full migration deployment

View original on GitHub โ†—

This issue has 3 comments on GitHub. Read the full discussion on GitHub โ†—