@@ -120,53 +120,60 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
120120/**
121121 * Proactively summarizes unsummarized sessions on startup.
122122 * Runs asynchronously to avoid blocking server startup.
123- *
123+ * Records the result in HealthService so agents can see what happened.
124+ *
124125 * @returns {Promise<void> }
125126 */
126127async function summarizeSessionsOnStartup ( ) {
127128 logger . info ( '[Startup] Checking for unsummarized sessions...' ) ;
128-
129+
129130 try {
130131 const result = await SessionService . summarizeSessions ( { } ) ;
131-
132+
132133 if ( result . processed > 0 ) {
133134 logger . info ( `✅ [Startup] Summarized ${ result . processed } session(s):` ) ;
134135 result . sessions . forEach ( session => {
135136 logger . info ( ` - ${ session . title } (${ session . memoryCount } memories)` ) ;
136137 } ) ;
138+ HealthService . recordStartupSummarization ( 'completed' , {
139+ processed : result . processed ,
140+ sessions : result . sessions . map ( s => ( { title : s . title , memoryCount : s . memoryCount } ) )
141+ } ) ;
137142 } else {
138143 logger . info ( '[Startup] No unsummarized sessions found' ) ;
144+ HealthService . recordStartupSummarization ( 'completed' , { processed : 0 } ) ;
139145 }
140146 } catch ( error ) {
141147 logger . warn ( '⚠️ [Startup] Session summarization failed:' , error . message ) ;
142148 logger . warn ( ' You can manually trigger summarization using the summarize_sessions tool' ) ;
149+ HealthService . recordStartupSummarization ( 'failed' , { error : error . message } ) ;
143150 }
144151}
145152
146153/**
147154 * Main startup sequence for the Memory Core MCP server.
148- *
155+ *
149156 * Performs the following steps:
150- * 1. Health check - verifies ChromaDB connectivity
151- * 2. Status reporting - logs detailed diagnostics
152- * 3. Auto-summarization - processes unsummarized sessions (if healthy)
153- * 4. Server startup - connects stdio transport
154- *
157+ * 1. Wait for async services - ensures ChromaManager is initialized
158+ * 2. Health check - verifies ChromaDB connectivity
159+ * 3. Status reporting - logs detailed diagnostics
160+ * 4. Auto-summarization - processes unsummarized sessions (if healthy)
161+ * 5. Server startup - connects stdio transport
162+ *
155163 * The server starts even if ChromaDB is unavailable, but tools will fail
156164 * gracefully with helpful error messages until dependencies are resolved.
157165 */
158166async function main ( ) {
159- // Wait for async services to initialize
167+ // Wait for async services to initialize (fixes race condition)
160168 await ChromaManager . ready ( ) ;
161-
162169 // Perform initial health check (non-blocking)
163170 const health = await HealthService . healthcheck ( ) ;
164171
165172 // Report status based on health check results
166173 if ( health . status === 'unhealthy' ) {
167174 logger . warn ( '⚠️ [Startup] Memory Core is unhealthy. Server will start but tools will fail until resolved.' ) ;
168175 health . details . forEach ( detail => logger . warn ( ` ${ detail } ` ) ) ;
169-
176+
170177 // Provide helpful guidance based on process status
171178 if ( ! health . database . process . running ) {
172179 logger . warn ( ' 💡 Tip: Use the start_database tool after server starts, or run:' ) ;
@@ -176,7 +183,7 @@ async function main() {
176183 } else if ( health . status === 'degraded' ) {
177184 logger . warn ( '⚠️ [Startup] Memory Core is degraded. Some features may be unavailable.' ) ;
178185 health . details . forEach ( detail => logger . warn ( ` ${ detail } ` ) ) ;
179-
186+
180187 // Still proceed with summarization if ChromaDB is accessible, even without API key
181188 // This allows the user to see what would be summarized
182189 logger . info ( '✅ [Startup] ChromaDB connectivity confirmed' ) ;
@@ -191,7 +198,7 @@ async function main() {
191198 logger . info ( ` - Memories: ${ health . database . connection . collections . memories . count } ` ) ;
192199 logger . info ( ` - Summaries: ${ health . database . connection . collections . summaries . count } ` ) ;
193200 }
194-
201+
195202 // Only auto-summarize if we're fully healthy
196203 if ( health . features . summarization ) {
197204 // Run summarization asynchronously (non-blocking)
@@ -201,6 +208,7 @@ async function main() {
201208 } else {
202209 logger . warn ( '⚠️ [Startup] GEMINI_API_KEY not set - skipping automatic session summarization' ) ;
203210 logger . warn ( ' Set GEMINI_API_KEY environment variable to enable summarization features' ) ;
211+ HealthService . recordStartupSummarization ( 'skipped' , { reason : 'GEMINI_API_KEY not set' } ) ;
204212 }
205213 }
206214
0 commit comments