N-Phase Circular Dependency Implementation Summary
Overview
We have successfully implemented N-phase support (more than 2 phases) for circular dependency resolution in hybrid execution mode. The system can now handle complex circular dependency scenarios that require multiple phases to resolve.
Scope
- Supported: Circular dependencies within a single App (both intra-stack and cross-stack)
- Not Supported: Circular dependencies between different App instances
- Reason: Apps are designed as independent deployment units
Key Implementation Details
1. Native Circular Dependency Handler
- Enhanced
CircularExecutionNodewithphase_configsdictionary to store configurations for each phase - Modified
execute_with_circular_deps()to dynamically determine the maximum phase number and execute all phases - Resources can now appear in multiple phases with different configurations
2. Phase Execution Flow
The system now supports arbitrary numbers of phases: - Phase 1: Create resources without circular references - Phase 2: Update some resources with partial references - Phase 3: Update remaining resources to complete cycles - Phase N: Continue updating as needed for complex scenarios
3. Test Coverage
Created comprehensive test suite (test_n_phase_circular_deps.py) with 7 tests:
- Linear circular chain scenarios (A → B → C → A)
- Complex multi-level dependencies
- Hybrid execution with 3+ phases
- Multiple independent circular loops
- Self-referential resources with chains
4. Example 3-Phase Execution
For a scenario with resources A, B, and C where: - A depends on C - B depends on A - C depends on B
The execution proceeds as: 1. Phase 1: Create A, B, C without circular references 2. Phase 2: Update B (add ref to A), Update C (add ref to B) 3. Phase 3: Update A (add ref to C), Update C (add ref to A if needed)
Verification
The test test_hybrid_execution_handles_n_phases demonstrates a successful 3-phase execution:
- 7 total operations (3 creates + 4 updates)
- Resources properly updated across 3 distinct phases
- All circular references resolved correctly
Architecture Benefits
- Scalable: Not limited to 2 phases - handles N phases as needed
- Dynamic: Number of phases determined by dependency complexity
- Efficient: Only creates as many phases as necessary
- Compatible: Works with existing phase planning strategies
Usage
The N-phase support is automatically available when using circular dependency resolution:
app = App(enable_circular_dep_resolution=True)
# ... define resources with complex circular dependencies ...
app.synth()
The system will automatically determine the required number of phases and execute them in order.