LearnNewsExamplesServices
Frontmatter
id7836
titleRefactor: Robust Hybrid Database Lifecycle for AI Services
stateClosed
labels
enhancementairefactoring
assigneestobiu
createdAtNov 21, 2025, 2:31 AM
updatedAtNov 21, 2025, 3:14 AM
githubUrlhttps://github.com/neomjs/neo/issues/7836
authortobiu
commentsCount1
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtNov 21, 2025, 3:14 AM

Refactor: Robust Hybrid Database Lifecycle for AI Services

Closed v11.8.0 enhancementairefactoring
tobiu
tobiu commented on Nov 21, 2025, 2:31 AM

Objective Refactor the DatabaseLifecycleService and ChromaManager to robustly support a hybrid database model (External, Managed, Shared) without race conditions or deadlocks.

Context The current lifecycle implementation (ai/mcp/server/knowledge-base/services/DatabaseLifecycleService.mjs) has a potential circular dependency and timing issue:

  1. DatabaseLifecycleService waits for ChromaManager to be ready.
  2. ChromaManager attempts to connect immediately.
  3. If the DB is not running, ChromaManager fails to connect, but DatabaseLifecycleService hasn't started it yet.

We need to support:

  • External Mode: DB started manually in a terminal.
  • Managed Mode: Service starts the DB process.
  • Shared Mode: Multiple agents/processes sharing one DB instance (first one starts it, others detect it).

Tasks

  1. Decouple Initialization: DatabaseLifecycleService should check for an existing DB before waiting for ChromaManager.
  2. Robust Start: If no DB is detected, start the process and wait for the port to be responsive before declaring readiness.
  3. Service Base Class: Implement Neo.ai.ServiceBase to standardize the ready() contract across all services (KB, Memory, GitHub), ensuring consumers (Agents) can just await Service.ready().

Deliverables

  • Refactored DatabaseLifecycleService.mjs (KB & Memory).
  • Refactored ChromaManager.mjs (KB & Memory).
  • New ai/core/ServiceBase.mjs.
tobiu added the enhancement label on Nov 21, 2025, 2:31 AM
tobiu added the ai label on Nov 21, 2025, 2:31 AM
tobiu added the refactoring label on Nov 21, 2025, 2:31 AM
tobiu assigned to @tobiu on Nov 21, 2025, 3:13 AM
tobiu
tobiu Nov 21, 2025, 3:13 AM

Input from Gemini 3.0 Preview:

✦ I have successfully refactored the AI Service lifecycle to support a robust hybrid database model.

Key Changes:

  1. Eliminated Circular Dependencies: DatabaseLifecycleService no longer statically imports ChromaManager. It uses dynamic imports to access the client for heartbeat checks, preventing startup crashes.
  2. Enforced Initialization Order:
    • DatabaseLifecycleService: Starts the process and waits for a heartbeat (via http or dynamic client).
    • ChromaManager: Awaits DatabaseLifecycleService.ready() before attempting to connect.
    • SessionService / DatabaseService: Await ChromaManager.ready() before performing operations.
  3. Simplified Agent SDK: The self-healing.mjs demo now simply awaits KB_LifecycleService.ready() and KB_ChromaManager.ready() (or just the manager) without manual retry loops.

This resolves the race conditions and supports external/shared database instances.

Closing ticket.

tobiu closed this issue on Nov 21, 2025, 3:14 AM
tobiu referenced in commit 74edfca - "refactor(ai): Robust Hybrid Database Lifecycle and Roadmap update (#7836)" on Nov 21, 2025, 9:51 AM
tobiu cross-referenced by #7837 on Nov 21, 2025, 10:14 AM