Skip to content

Conversation

@SeasonPilot
Copy link

@SeasonPilot SeasonPilot commented Nov 26, 2025

…) #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 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

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

  • I have prepared the pull request title according to the requirements.
  • I have successfully run all unit tests and integration tests.
  • I have followed the code style guidelines of this project.
  • I have already rebased the latest master branch.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.

…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
@SeasonPilot
Copy link
Author

#40

2 similar comments
@SeasonPilot
Copy link
Author

#40

@SeasonPilot
Copy link
Author

#40

Copy link

Copilot AI left a 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
@SeasonPilot SeasonPilot force-pushed the feature/issue-40-agent-persistence branch from 67f55a9 to 6fed780 Compare December 19, 2025 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant