Token System Simplification - Implementation Roadmap
Historical note: This page is preserved as an archival implementation roadmap. It may mention older subsystem names or assumptions that no longer match the current codebase. Start with Historical Internal Docs, Hybrid Execution Overview, and Migration Guide for current guidance.
Overview
This roadmap provides a detailed, step-by-step guide for simplifying the Token system while maintaining backward compatibility and system stability. Based on analysis of ~50+ files using tokens across the codebase.
Phase 1: Protocol Foundation (Days 1-3)
Step 1.1: Create Protocol Infrastructure
Files to create:
libs/netherframe.core/netherframe/core/tokens_v2/
├── __init__.py
├── protocols.py # Core protocols (DeferredValue, ResourceReference)
├── adapters.py # Adapters for existing Token compatibility
└── types.py # Type aliases and helpers
Key deliverables:
- DeferredValue[T] protocol with resolve() and get_dependencies()
- ResourceReference protocol extending DeferredValue
- Adapters to make existing Token work with new protocols
- Type stubs for better IDE support
Step 1.2: Add Protocol Tests
Files to create:
libs/netherframe.core/tests/tokens_v2/
├── test_protocols.py
├── test_adapters.py
└── test_compatibility.py
Test coverage: - Protocol compliance checking - Adapter functionality - Backward compatibility verification
Phase 2: Dependency Analysis Migration (Days 4-7)
Step 2.1: Update Core Analyzer
Files to modify:
- libs/netherframe.core/netherframe/core/phases/analyzer.py
- libs/netherframe.core/netherframe/core/dependency_analyzer.py
Changes:
# Before: Fragile introspection
if hasattr(token._resolvable, "__class__") and token._resolvable.__class__.__name__ == "ResourceAttribute":
pass
# After: Protocol-based
if isinstance(value, DeferredValue):
deps = value.get_dependencies()
Step 2.2: Update Phase Utilities
Files to modify:
- libs/netherframe.core/netherframe/core/phases/utils.py
- libs/netherframe.core/netherframe/core/resource_introspection.py
Changes: - Replace token type checks with protocol checks - Simplify token detection logic - Remove regex-based token parsing where possible
Step 2.3: Test Phase System
Files to update:
- libs/netherframe.core/tests/test_token_dependency_detection.py
- libs/netherframe.core/tests/test_native_circular_deps.py
Phase 3: Simplified Token Implementation (Days 8-12)
Step 3.1: Create New Token Classes
Files to create:
- libs/netherframe.core/netherframe/core/tokens_v2/simplified.py
- libs/netherframe.core/netherframe/core/tokens_v2/references.py
Implementation:
class SimplifiedToken(Generic[T]):
"""Token without operator overloading"""
# No __add__, __sub__, etc.
# Natural Python errors instead of custom messages
class ResourceReferenceImpl(SimplifiedToken[T]):
"""Concrete implementation of ResourceReference protocol"""
Step 3.2: Dual Protocol Support
Files to modify:
- libs/netherframe.core/netherframe/core/tokens.py
Changes:
- Make existing Token implement DeferredValue protocol
- Add get_dependencies() method to Token
- Ensure both old and new code can work together
Step 3.3: Update Resource Attributes
Files to modify:
- libs/netherframe.core/netherframe/core/constructs/attributes.py
- libs/netherframe.core/netherframe/core/constructs/resources.py
Changes:
- Update ResourceAttribute to implement new protocols
- Modify ref_attribute() to optionally return new token types
- Add feature flag support
Phase 4: Function System Update (Days 13-16)
Step 4.1: Update Core Functions
Files to modify:
- libs/netherframe.core/netherframe/core/functions.py
- libs/netherframe.core/netherframe/core/function_base.py
Changes: - Update function return types to use protocol types - Ensure functions work with both old and new tokens - Update type hints for better IDE support
Step 4.2: Update Function Generation
Files to modify:
- libs/netherframe.generation/netherframe/generation/functions/generator.py
- libs/netherframe.generation/netherframe/generation/functions/generator.py
Changes: - Generate code that imports from both old and new modules - Update templates to support protocol-based approach - Ensure generated code is backward compatible
Step 4.3: Test Function System
Files to update:
- libs/netherframe.core/tests/test_functions.py
- Function-specific test files
Phase 5: Native Execution Support (Days 17-20)
Step 5.1: Update Orchestrator
Files to modify:
- libs/netherframe.core/netherframe/core/orchestrator.py
- libs/netherframe.core/netherframe/core/state_engine.py
Changes: - Update token resolution to use protocol methods - Ensure native execution works with new tokens - Maintain performance characteristics
Step 5.2: Update Runtime
Files to modify:
- libs/netherframe.core/netherframe/core/runtime.py
- libs/netherframe.core/netherframe/core/native_circular_deps.py
Changes: - Protocol-based token handling - Update circular dependency detection - Ensure state consistency
Step 5.3: Native Execution Tests
Files to update:
- libs/netherframe.core/tests/test_native_runtime_tokens.py
- Native execution test files
Phase 6: Feature Flags & Migration (Days 21-24)
Step 6.1: Implement Feature Flags
Files to create/modify:
- libs/netherframe.core/netherframe/core/config.py
- Settings support (flags and [tool.nf.config] keys)
Feature flags:
[tool.nf.config]
use_simplified_tokens = false # Use new token implementation
disable_token_operators = false # Disable operator overloading
token_protocol_checks = true # Enable protocol-based checks
Step 6.2: Migration Tooling
Files to create:
- tools/migrate-tokens.py
- Migration documentation
Tools: - AST-based code transformer - Token usage analyzer - Migration validator
Step 6.3: Update Examples
Files to update:
- All files in examples/ directory
- Documentation examples
Phase 7: Performance & Optimization (Days 25-28)
Step 7.1: Performance Testing
Files to update:
- libs/netherframe.core/tests/performance/test_optimization_6_memoized_tokens.py
- libs/netherframe.core/tests/performance/test_optimization_8_batch_tokens.py
- libs/netherframe.core/tests/performance/analyze_synthesis_bottlenecks.py
Benchmarks: - Token creation performance - Resolution performance - Memory usage - Dependency analysis speed
Step 7.2: Optimization
Focus areas: - Protocol check performance - Caching effectiveness - Memory footprint - Hot path optimization
Phase 8: Documentation & Rollout (Days 29-30)
Step 8.1: Update Documentation
Files to update:
- docs/core/core/GENERIC_TOKENS_GUIDE.md
- docs/core/core/README.md
- API documentation
Step 8.2: Migration Guide
Create: - Migration cookbook - Common patterns guide - Troubleshooting guide
Step 8.3: Release Planning
- Beta testing with feature flags
- Gradual rollout strategy
- Deprecation timeline
Testing Strategy
Unit Tests
- Test each new protocol and class
- Ensure backward compatibility
- Cover edge cases
Integration Tests
- Full synthesis tests
- Native execution tests
- Cross-stack reference tests
- Circular dependency tests
Performance Tests
- Benchmark against baseline
- Memory profiling
- Load testing
Migration Tests
- Test migration tooling
- Validate transformed code
- Ensure semantic equivalence
Risk Management
Technical Risks
- Breaking Changes: Mitigated by dual protocol support
- Performance Regression: Continuous benchmarking
- Missed Edge Cases: Comprehensive test coverage
Process Risks
- Long Timeline: Phased approach allows partial delivery
- User Disruption: Feature flags for gradual adoption
- Documentation Lag: Update docs with each phase
Success Criteria
- Code Metrics:
- Remove 200+ lines of operator overloading
- Reduce token-related complexity by 40%
-
Maintain 100% test coverage
-
Performance Metrics:
- No regression in synthesis time
- No increase in memory usage
-
Faster dependency analysis (10%+ improvement)
-
User Metrics:
- Zero breaking changes for existing code
- Positive feedback from early adopters
- Successful migration of example code
Next Steps
- Immediate Actions:
- Create feature branch
token-simplification-v2 - Set up CI/CD for new tests
-
Begin Phase 1 implementation
-
Communication:
- Announce plan to team
- Set up weekly progress updates
-
Create feedback channels
-
Monitoring:
- Track progress against roadmap
- Monitor performance metrics
- Gather user feedback
This roadmap provides a clear path forward while minimizing risk and ensuring system stability throughout the simplification process.