## Overview Complete migration to standardized lazy singleton initialization pattern to eliminate 140-210 lines of duplicate code. **Document:** `docs/developer/LAZY_SINGLETON_MIGRATION.md` **Status:** ✅ Utility Ready - Migration In Progress ## Impact - **Utility Created:** `lazy_singleton.py` - **Lines Saved:** 10-15 lines per implementation - **Files Affected:** 10+ API files - **Total Code Reduction:** ~140-210 lines ## Common Pattern Being Replaced ```python def get_chat_history_manager(request): manager = getattr(request.app.state, "chat_history_manager", None) if manager is None: try: from src.chat_history_manager import ChatHistoryManager manager = ChatHistoryManager() request.app.state.chat_history_manager = manager logger.info("✅ Lazy-initialized chat_history_manager") except Exception as e: logger.error(f"Failed to lazy-initialize: {e}") return manager ``` ## Tasks - [ ] Migrate `backend/api/chat.py` lazy initializers - [ ] Migrate `backend/api/knowledge.py` lazy initializers - [ ] Migrate remaining API files - [ ] Update documentation - [ ] Testing and validation
Activity
docs: Add GitHub issue links to 8 task documentation files
mrveiss commentedon Nov 29, 2025
Migration Progress - Session 1
Files Migrated
backend/utils/chat_utils.py-get_chat_history_manager()backend/api/chat.py-get_llm_service(),get_chat_workflow_manager()Commit
b1b0c1e1- refactor(api): Migrate to lazy_singleton pattern (Lazy Singleton Migration - Standardize Initialization Pattern #253)Lines Saved
Remaining
Migration utility:
src/utils/lazy_singleton.pyprovides:lazy_init_singleton()- synchronous lazy initlazy_init_singleton_async()- async lazy init for services requiring async initializationmrveiss commentedon Nov 29, 2025
Issue Complete ✅
Migration Summary
The lazy singleton pattern has been standardized using
src/utils/lazy_singleton.py.Files Migrated
backend/utils/chat_utils.py-get_chat_history_manager()backend/api/chat.py-get_llm_service(),get_chat_workflow_manager()Commit
b1b0c1e1- refactor(api): Migrate to lazy_singleton pattern (Lazy Singleton Migration - Standardize Initialization Pattern #253)Result
Notes
Other files using
getattr(request.app.state, ...)were reviewed but most use a retrieval pattern (not lazy initialization), so no migration needed.refactor(api): Migrate to lazy_singleton pattern (#253)
docs: Add GitHub issue links to 8 task documentation files
refactor(api): Migrate to lazy_singleton pattern (#253)