Skip to content

Native Mode Refactoring Summary

What Was Done

1. Created ExecutionMode Enum

  • Added ExecutionMode enum in nf/core/execution_mode.py with TERRAFORM and NATIVE values
  • Provides type-safe execution mode checking

2. Updated App Class

  • Added execution_mode parameter to App constructor with default ExecutionMode.TERRAFORM
  • Added execution_mode to App's __slots__
  • Import ExecutionMode in app_stack.py

3. Added Execution Mode Properties

  • Added app property to BaseConstruct to traverse up and find the App instance
  • Added execution_mode property to BaseConstruct to get execution mode from app
  • Added execution_mode property to Resource (which uses SimpleNamespace)

4. Updated HybridExecutionMode

  • Now sets app.execution_mode = ExecutionMode.NATIVE in constructor
  • Removed all thread-local state setting (_nf_native_mode)

5. Replaced Thread-Local Checks

  • Updated unified_orchestrator.py: Changed from checking thread-local to resource.execution_mode
  • Updated runtime_impl.py: Changed is_native_mode() to resource.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_mode or construct.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

  1. Explicit Configuration: Execution mode is clearly visible in app initialization
  2. Type Safety: Using enum provides better IDE support and type checking
  3. No Thread-Local State: Eliminated fragile thread-local storage
  4. Better Testing: Tests are cleaner without manual state management
  5. 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

  1. Remove deprecated is_native_mode() function in future release
  2. Consider adding execution mode validation in App constructor
  3. Document the new execution_mode parameter in user guides
  4. Update any remaining code that uses thread-local checks