LearnNewsExamplesServices
Frontmatter
id7864
titleRefactor SessionService for better error handling and config management
stateClosed
labels
enhancementai
assigneestobiu
createdAtNov 22, 2025, 8:03 PM
updatedAtNov 22, 2025, 8:17 PM
githubUrlhttps://github.com/neomjs/neo/issues/7864
authortobiu
commentsCount1
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtNov 22, 2025, 8:17 PM

Refactor SessionService for better error handling and config management

Closed v11.9.0 enhancementai
tobiu
tobiu commented on Nov 22, 2025, 8:03 PM

During a self-review of ai/mcp/server/memory-core/services/SessionService.mjs, the following potential improvements were identified:

  1. Error Handling: findSessionsToSummarize performs multiple database calls (memory fetch loop, summary fetch loop) without internal try/catch blocks. While the caller (summarizeSessions -> initAsync) likely handles errors, a failure in the memory fetch loop (e.g., partial network failure) currently causes the entire process to abort. Consider if granular error handling or retry logic is needed here.
  2. Hardcoded Configuration: The batch limit (const limit = 2000;) is hardcoded inside the method. This should be moved to ai/mcp/server/memory-core/config.mjs to allow for environment-based tuning (e.g., smaller batches for memory-constrained environments).
  3. Loop Safety: While hasMore is controlled by batch.ids.length < limit, adding a safety break (max iterations) could prevent infinite loops in case of weird driver behavior where it keeps returning full pages of the same data (though unlikely with offset).

Goal: Refactor SessionService.mjs to address these code quality and robustness points.

tobiu assigned to @tobiu on Nov 22, 2025, 8:05 PM
tobiu added the enhancement label on Nov 22, 2025, 8:05 PM
tobiu added the ai label on Nov 22, 2025, 8:05 PM
tobiu referenced in commit a2b3480 - "Refactor SessionService for better error handling and config management #7864" on Nov 22, 2025, 8:16 PM
tobiu
tobiu Nov 22, 2025, 8:16 PM

Input from Gemini:

✦ Refactoring completed.

Summary of Changes:

  1. Config Management: Added summarizationBatchLimit to config.mjs (default: 2000) to control database fetch sizes.
  2. Robust Database Scanning:
    • Updated findSessionsToSummarize to use pagination loops with safety breaks (maxIterations: 1000).
    • Ensured database queries use dynamically built configuration objects to avoid where: undefined errors.
    • Removed internal try/catch from findSessionsToSummarize to allow errors to propagate correctly.
  3. Tool Error Handling:
    • Updated summarizeSessions (the public tool method) to wrap logic in a try/catch block.
    • On failure, it now returns a structured error object ({ error, message, code }) instead of crashing or returning misleading empty results.

The service is now consistent with the project's error handling patterns and safe for large-scale deployments.

tobiu closed this issue on Nov 22, 2025, 8:17 PM