NF-Core Refactoring and API Improvement Analysis
Historical note: This page is preserved as an archival analysis snapshot. It may discuss older module names, temporary APIs, or proposed changes that were later revised. Start with Historical Internal Docs, Hybrid Execution Overview, and Migration Guide for current guidance.
Executive Summary
After analyzing the nf.core codebase, I've identified significant opportunities for refactoring and API improvements. Since backward compatibility is not required, we can make aggressive changes to simplify the codebase and improve developer experience.
1. Backward Compatibility Code to Remove
1.1 Module Aliases and Re-exports
- resolver.py: Entire file is just backward compatibility imports from base_resolver
- core.py: Re-exports everything from constructs module
- init.py: Line 185 -
import_hookalias for backward compatibility - circular_dependency_handler.py: Lines 513-514 - NativeCircularDependencyHandler alias
- optimized_resolver.py: Lines 259-264 - Module-level functions for backward compatibility
- base_resolver.py: Similar module-level functions for compatibility
1.2 Compatibility Methods
- circular_dependency_handler.py: Lines 516-520 - Compatibility method additions
- benchmarks/benchmark_suite.py: Backward compatibility aliases
Recommendation: Remove all backward compatibility code and consolidate imports.
2. Duplicated Orchestrator Implementations
Current State (4 separate files):
orchestrator.py- Base sequential executionparallel_orchestrator.py- Parallel execution (doesn't inherit from base!)circular_orchestrator.py- Circular dependency handlingcircular_orchestrator_enhanced.py- Enhanced circular deps
Issues:
- ExecutionNode defined differently in each file
- Duplicate dependency resolution logic (~150 lines each)
- No proper inheritance hierarchy
- Conditional selection in native_execution.py
Proposed Solution:
# unified_orchestrator.py already exists but needs to be fully adopted
class UnifiedOrchestrator:
def __init__(self, strategy: OrchestrationStrategy):
self.strategy = strategy
# Remove the 4 old orchestrator files
# Update all imports to use UnifiedOrchestrator
3. State Engine Duplication
Current State:
state_engine.py- Base implementationenhanced_state_engine.py- Adds featurestransactional_state_engine.py- Adds transactions
Proposed Solution:
# Single state engine with feature flags
class StateEngine:
def __init__(self, features: StateFeatures):
self.enhanced_mode = features.enhanced
self.transactional = features.transactional
4. Native Execution Variants
Current State:
native_execution.py- Main implementationnative_execution_enhanced.py- Adds planningnative_circular_deps.py- Sequential circular handlingnative_circular_deps_parallel.py- Parallel circular handling
Issues:
- CircularExecutionNode duplicated in both circular files
- ~40% code duplication between circular handlers
Proposed Solution:
- Already partially done with
circular_dependency_handler.py - Need to remove old files and update imports
5. Resolver Architecture
Current State:
base_resolver.py- Base implementationresolver.py- Just backward compatibility importsoptimized_resolver.py- Adds caching/batching
Proposed Solution:
# Single resolver with optimization flag
class Resolver:
def __init__(self, optimized: bool = True):
self.cache_enabled = optimized
self.batch_enabled = optimized
6. API Design Improvements
6.1 Inconsistent Resource Creation
# Current - multiple ways to create resources
Resource(scope, id, nf_type="aws_instance", ami="ami-1234", instance_type="t3.micro")
Resource(scope, nf_type="aws_instance", ami="ami-1234", instance_type="t3.micro") # id auto-generated
aws_instance(scope, id, ami="ami-1234", instance_type="t3.micro") # Generated provider method
Recommendation: Standardize on generated provider methods only.
6.2 Complex Import System
# Current - confusing imports
from nf.core import Resource, Stack, App
from nf.core.runtime import runtime_function
from nf.core.runtime_impl import ResourceOutput
Recommendation: Flatten namespace:
from nf.core import App, Stack, Resource, runtime_function, ResourceOutput
6.3 Deferred Value System Complexity
Multiple overlapping concepts: - RuntimeValue - ResourceOutput - VariableReference - LocalReference - MappedValue - CombinedValue
Recommendation: Simplify to 2-3 core types.
7. Performance Optimizations
7.1 Excessive Performance Test Files
- 44 performance test files, many one-off benchmarks
- Old optimization results (optimization_1 through optimization_7)
- Duplicate benchmark utilities
Recommendation: - Archive old results - Consolidate to single benchmark suite - Move to tools/performance/
7.2 Import Time Optimization
- Heavy imports in init.py
- Optional native execution imports already conditional
Recommendation: Use lazy imports for heavy modules.
8. Code Organization
8.1 Misplaced Files
- Debug scripts in main codebase
- Test utilities mixed with tests
- Performance analysis scripts in tests/
8.2 Module Structure
Current:
nf/core/
├── 70+ files in flat structure
├── constructs/
├── phases/
└── benchmarks/
Proposed:
nf/core/
├── api/ # Public API
├── execution/ # Orchestrators, executors
├── state/ # State management
├── providers/ # Provider protocol
├── runtime/ # Runtime values, resolution
└── internal/ # Implementation details
9. Error Handling Improvements
Current Issues:
- Inconsistent exception types
- Generic Exception raising in many places
- No clear error hierarchy
Proposed Solution:
# nf/core/exceptions.py
class NetherframeError(Exception): ...
class ResourceError(NetherframeError): ...
class StateError(NetherframeError): ...
class ProviderError(NetherframeError): ...
class CircularDependencyError(NetherframeError): ...
10. Implementation Plan
Phase 1: Remove Backward Compatibility (1 day)
- Delete resolver.py (pure compatibility)
- Remove all compatibility aliases
- Update imports throughout codebase
- Consolidate core.py with constructs/
Phase 2: Consolidate Duplicate Implementations (3 days)
- Adopt unified_orchestrator.py fully
- Delete old orchestrator files
- Merge state engine variants
- Remove duplicate circular handlers
Phase 3: API Simplification (2 days)
- Flatten import namespace
- Simplify deferred value types
- Standardize resource creation
- Add proper exception hierarchy
Phase 4: Code Organization (1 day)
- Reorganize into logical subdirectories
- Move misplaced files
- Archive old performance results
- Consolidate test utilities
Estimated Impact
- Code Reduction: ~8,000 lines (30-35% of core)
- Import Simplification: From 20+ imports to ~10
- Performance: Faster import times, cleaner execution paths
- Maintainability: Dramatic improvement with single implementations
- Developer Experience: Much clearer API surface
Conclusion
The nf.core codebase has significant technical debt from maintaining backward compatibility and having multiple implementations of core functionality. By removing compatibility requirements and consolidating duplicate code, we can reduce the codebase by approximately 30% while significantly improving clarity and maintainability. The unified orchestrator pattern already exists but needs to be fully adopted throughout the codebase.