chat2graph icon indicating copy to clipboard operation
chat2graph copied to clipboard

feat(dal): implement persistence for Agent instances in database (#40…

Open SeasonPilot opened this issue 1 month ago • 3 comments

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

  • [x] feat: (new feature)
  • [ ] fix: (bug fix)
  • [ ] docs: (doc update)
  • [ ] style: (update format)
  • [ ] refactor: (refactor code)
  • [ ] test: (test code)
  • [ ] chore: (other updates)

Scope

  • [x] app: (Application Layer)
    • [ ] web: (web front-end module)
    • [ ] server: (web server module)
    • [x] dal: (data access layer)
    • [ ] sdk: (sdk module)
  • [x] agent: (Agent Layer)
    • [x] 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.

SeasonPilot avatar Nov 26 '25 07:11 SeasonPilot

#40

SeasonPilot avatar Nov 26 '25 07:11 SeasonPilot

#40

SeasonPilot avatar Nov 26 '25 07:11 SeasonPilot

#40

SeasonPilot avatar Nov 26 '25 07:11 SeasonPilot