Circular Dependency Detection
Overview
The porch configuration system now includes robust circular dependency detection to prevent infinite loops in command group references. This feature helps catch configuration errors early and provides clear error messages to help debug issues.
Features
1. Circular Dependency Detection
The system detects several types of circular dependencies:
- Simple circular dependencies: Group A references Group B, which references Group A
- Multi-way circular dependencies: Group A → Group B → Group C → Group A
- Self-referencing groups: A group that references itself
- Deep nested cycles: Circular dependencies buried deep in nested command structures
2. Maximum Recursion Depth Protection
To prevent stack overflow and excessive processing, the system enforces a maximum recursion depth of 100 levels when resolving command groups.
3. Enhanced Error Messages
When a circular dependency is detected, the system provides clear error messages showing:
- The exact circular path (e.g., “group_a → group_b → group_a”)
- Which command within a group caused the issue
- The command index for easier debugging
4. Context Cancellation Support
Configuration building now respects context cancellation, allowing for:
- Graceful interruption with Ctrl+C during config parsing
- Timeout protection (30-second default for config building)
- Responsive signal handling during long configuration processes
Examples
Detecting Circular Dependencies
Example 1: Simple Circular Dependency
Error Output:
Example 2: Self-Referencing Group
Error Output:
Valid Nested Groups
This configuration is valid and will work correctly:
Signal Handling Improvements
Configuration Building Timeout
Configuration building now has a 30-second timeout to prevent hanging on malformed configurations:
Context Cancellation Checks
The system now checks for context cancellation at key points:
- Before starting configuration parsing
- After command group validation
- During individual command processing
Graceful Interruption
When you press Ctrl+C during configuration building, you’ll see:
Implementation Details
Registry-Level Detection
The commandregistry.Registry now includes:
resolveCommandGroupWithDepth(): Tracks recursion depth and visiting statevalidateCommandForCircularDeps(): Validates individual commands for group referencesformatCircularDependencyPath(): Creates human-readable circular dependency paths
Configuration-Level Validation
The config.BuildFromYAML() function:
- Validates all command groups before proceeding with command creation
- Includes context cancellation checks throughout the process
- Provides detailed error messages with command indices
Error Types
New error types have been added:
ErrCircularDependency: For circular dependency detectionErrConfigurationTimeout: For configuration timeout handlingErrMaxRecursionDepth: For recursion depth protection
Testing
Comprehensive tests have been added covering:
- Simple circular dependencies
- Multi-way circular dependencies
- Self-referencing groups
- Maximum recursion depth
- Context cancellation
- Configuration timeouts
Run tests with: