Implemented
The external_backends.py module uses global module-level dictionaries to manage backend registration:
_KNOWLEDGE_BACKENDS: dict[str, KnowledgeSearchBackend] = {}
_CODE_PARSE_BACKENDS: dict[str, CodeParseBackend] = {}This creates hidden global state that makes testing difficult and creates implicit dependencies. There's no abstraction layer for backend discovery or lifecycle management.
We will create a BackendRegistry class to encapsulate backend management with proper lifecycle support.
- Create
BackendRegistryclass to encapsulate backend management - Move global dictionaries to instance variables
- Add methods for registration, retrieval, and lifecycle management
- Add unit tests for the registry
- Create
BackendFactoryprotocol/interface for creating backend instances - Implement factory methods for each backend type
- Add unit tests for each factory
- Update registration to use factories
- Create
BackendLifecycleinterface for initialization/shutdown - Implement lifecycle hooks for each backend
- Add integration tests for lifecycle management
- Update registry to manage backend lifecycles
- Create
BackendHealthCheckprotocol for monitoring backend status - Implement health checks for each backend
- Add unit tests for health checks
- Update registry to support health monitoring
- Update all components to accept
BackendRegistryas a parameter - Create a singleton registry instance for backward compatibility
- Add integration tests for the new pattern
- Update all callers to use dependency injection
- Maintain backward compatibility during transition
- Add comprehensive tests before refactoring
- Use feature flags to enable new implementation gradually
- Create migration guide for breaking changes
- Positive: Better testability, explicit dependencies, lifecycle management
- Negative: Breaking changes to backend registration API
- Risk: Medium - affects backend system initialization
- Keep global dictionaries (rejected - hidden state, hard to test)
- Use service locator pattern (rejected - hidden dependencies)
- Use framework-level dependency injection (rejected - over-engineering)
- Original issue: Medium-severity architecture issue #4
- Related files:
external_backends.py