-
Notifications
You must be signed in to change notification settings - Fork 47
feat(dal): implement persistence for Agent instances in database (#40… #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(dal): implement persistence for Agent instances in database (#40… #234
Conversation
…raph-family#40) TuGraph-family#232 ## Summary Implement database persistence for Agent instances (both Leader and Expert) to resolve issue TuGraph-family#40. ## Changes ### New Files - `app/core/dal/do/agent_do.py`: Agent configuration data model with UUID-based identification - `app/core/dal/dao/agent_dao.py`: Agent data access layer with CRUD operations - `app/core/reasoner/reasoner_factory.py`: Factory for creating Reasoner instances from configuration - `app/core/workflow/workflow_factory.py`: Factory for creating Workflow instances from configuration ### Modified Files - `app/core/agent/agent.py`: Replace mock ID generation with UUID - `app/core/agent/leader_state.py`: Add DAO support and database loading interface - `app/core/agent/builtin_leader_state.py`: Implement persistence with lazy loading and three-tier caching - `app/core/service/agent_service.py`: Add load_leader() and save_leader() methods - `app/core/dal/init_db.py`: Register AgentDo table in database initialization ## Key Features - **UUID-based identification**: Replace `name + "_id"` with proper UUID generation - **Configuration persistence**: Store agent configurations rather than serializing instances - **Lazy loading**: Three-tier caching strategy (memory → config → database) - **Cross-restart recovery**: Agents maintain state between system restarts - **Query support**: SQL-based agent queries with indexed fields ## Technical Details - Database: SQLite with SQLAlchemy ORM - Pattern: DO/DAO architecture following project conventions - Caching: Memory instances + configuration cache + database persistence - Indexes: agent_type, name (unique), is_active, composite (type+active) ## Benefits - ✅ Persistence across restarts - ✅ Improved agent tracking and monitoring - ✅ Configuration management and auditability - ✅ Lazy loading for performance optimization - ✅ Queryable agent information via SQL
2 similar comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements comprehensive database persistence for Agent instances (both Leader and Expert) to enable cross-restart recovery and configuration management. The implementation replaces mock ID generation with proper UUID-based identification and introduces a three-tier caching strategy (memory → config → database) with lazy loading for performance optimization.
Key changes include:
- New data access layer with AgentDo model and AgentDao for CRUD operations
- Factory classes for Reasoner and Workflow instantiation from stored configurations
- Enhanced LeaderState with database integration and lazy loading
- UUID-based agent identification replacing name-based mock IDs
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 16 comments.
Show a summary per file
| File | Description |
|---|---|
app/core/workflow/workflow_factory.py |
New factory class for creating Workflow instances from configuration with fallback handling |
app/core/service/agent_service.py |
Added persistence support with load_leader() and save_leader() methods, DAO integration |
app/core/reasoner/reasoner_factory.py |
New factory class for creating Reasoner instances from type and configuration |
app/core/dal/init_db.py |
Registered AgentDo table in database initialization |
app/core/dal/do/agent_do.py |
New data model for agent configuration persistence with UUID, indexes, and metadata |
app/core/dal/dao/agent_dao.py |
New DAO providing CRUD operations, configuration extraction/reconstruction, and query methods |
app/core/agent/leader_state.py |
Added DAO setter and load_experts_from_db interface for database integration |
app/core/agent/builtin_leader_state.py |
Implemented three-tier caching with lazy loading and persistence for expert management |
app/core/agent/agent.py |
Replaced mock ID generation (name + "_id") with proper UUID-based identification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix import path: builtin_workflow -> workflow.BuiltinWorkflow - Fix reasoner type names: DualReasoner -> DualModelReasoner - Fix return type mismatch in deactivate_agent/activate_agent - Add TYPE_CHECKING imports to avoid circular imports - Fix docstring formatting (remove trailing spaces)
- Fix agent_do.py comments: DualReasoner -> DualModelReasoner - Fix agent_dao.py: extract _actor_name and _thinker_name in config - Fix builtin_leader_state.py: complete remove_expert cleanup - Fix builtin_leader_state.py: thread-safe lazy loading with double-checked locking - Fix builtin_leader_state.py: remove unused Optional import - Fix agent_service.py: use get_cached_expert_config_count() method - Fix agent_service.py: add leader config consistency check - Fix workflow_factory.py: make config optional, remove commented code - Fix reasoner_factory.py: use config in all fallback paths
67f55a9 to
6fed780
Compare
…) #232
Summary
Implement database persistence for Agent instances (both Leader and Expert) to resolve issue #40.
Changes
New Files
app/core/dal/do/agent_do.py: Agent configuration data model with UUID-based identificationapp/core/dal/dao/agent_dao.py: Agent data access layer with CRUD operationsapp/core/reasoner/reasoner_factory.py: Factory for creating Reasoner instances from configurationapp/core/workflow/workflow_factory.py: Factory for creating Workflow instances from configurationModified Files
app/core/agent/agent.py: Replace mock ID generation with UUIDapp/core/agent/leader_state.py: Add DAO support and database loading interfaceapp/core/agent/builtin_leader_state.py: Implement persistence with lazy loading and three-tier cachingapp/core/service/agent_service.py: Add load_leader() and save_leader() methodsapp/core/dal/init_db.py: Register AgentDo table in database initializationKey Features
name + "_id"with proper UUID generationTechnical Details
Benefits
Title
Type
feat: (new feature)fix: (bug fix)docs: (doc update)style: (update format)refactor: (refactor code)test: (test code)chore: (other updates)Scope
app: (Application Layer)web: (web front-end module)server: (web server module)dal: (data access layer)sdk: (sdk module)agent: (Agent Layer)reasoner: (reasoner module)planner: (planner module)workflow: (workflow module)memory: (memory module)knowledge: (knowledge module)env: (env module)toolkit: (toolkit module)system: (System Layer)plugin: (plugin module)tracer: (tracer module)resource: (resource module)none: (N/A)Description
Issue: #
Checklist
masterbranch.