Native Mode Refactoring Summary
What Was Done
1. Created ExecutionMode Enum
- Added
ExecutionModeenum innf/core/execution_mode.pywithTERRAFORMandNATIVEvalues - Provides type-safe execution mode checking
2. Updated App Class
- Added
execution_modeparameter to App constructor with defaultExecutionMode.TERRAFORM - Added
execution_modeto App's__slots__ - Import ExecutionMode in app_stack.py
3. Added Execution Mode Properties
- Added
appproperty toBaseConstructto traverse up and find the App instance - Added
execution_modeproperty toBaseConstructto get execution mode from app - Added
execution_modeproperty toResource(which uses SimpleNamespace)
4. Updated HybridExecutionMode
- Now sets
app.execution_mode = ExecutionMode.NATIVEin constructor - Removed all thread-local state setting (
_nf_native_mode)
5. Replaced Thread-Local Checks
- Updated
unified_orchestrator.py: Changed from checking thread-local toresource.execution_mode - Updated
runtime_impl.py: Changedis_native_mode()toresource.execution_mode == ExecutionMode.NATIVE - Updated
attributes.py: Changed both cross-stack reference checks to use execution_mode
6. Deprecated is_native_mode()
- Kept function for backward compatibility but added deprecation warning
- Function now only checks thread-local for legacy code
- New code should use
app.execution_modeorconstruct.execution_mode
7. Updated Tests
- All tests now use
App(execution_mode=ExecutionMode.TERRAFORM)instead of thread-local manipulation - Removed all try/finally blocks for thread-local cleanup
- Tests are much cleaner and more explicit about execution mode
Benefits Achieved
- Explicit Configuration: Execution mode is clearly visible in app initialization
- Type Safety: Using enum provides better IDE support and type checking
- No Thread-Local State: Eliminated fragile thread-local storage
- Better Testing: Tests are cleaner without manual state management
- Single Source of Truth: Mode is stored in App and propagated through the tree
Migration Path for Users
Before:
# Old way - thread-local state set implicitly
enable_hybrid_execution(app)
After:
# New way - explicit execution mode
app = App(nf_id="myapp", execution_mode=ExecutionMode.NATIVE)
# Or
app = App(nf_id="myapp") # Defaults to TERRAFORM
enable_hybrid_execution(app) # Sets to NATIVE
Next Steps
- Remove deprecated
is_native_mode()function in future release - Consider adding execution mode validation in App constructor
- Document the new execution_mode parameter in user guides
- Update any remaining code that uses thread-local checks