diff --git a/src/services/worker/SessionManager.ts b/src/services/worker/SessionManager.ts index 8915d6ed..34ab6460 100644 --- a/src/services/worker/SessionManager.ts +++ b/src/services/worker/SessionManager.ts @@ -106,6 +106,15 @@ export class SessionManager { memory_session_id: dbSession.memory_session_id }); + // Log warning if we're discarding a stale memory_session_id (Issue #817) + if (dbSession.memory_session_id) { + logger.warn('SESSION', `Discarding stale memory_session_id from previous worker instance (Issue #817)`, { + sessionDbId, + staleMemorySessionId: dbSession.memory_session_id, + reason: 'SDK context lost on worker restart - will capture new ID' + }); + } + // Use currentUserPrompt if provided, otherwise fall back to database (first prompt) const userPrompt = currentUserPrompt || dbSession.user_prompt; @@ -124,11 +133,15 @@ export class SessionManager { } // Create active session - // Load memorySessionId from database if previously captured (enables resume across restarts) + // CRITICAL: Do NOT load memorySessionId from database here (Issue #817) + // When creating a new in-memory session, any database memory_session_id is STALE + // because the SDK context was lost when the worker restarted. The SDK agent will + // capture a new memorySessionId on the first response and persist it. + // Loading stale memory_session_id causes "No conversation found" crashes on resume. session = { sessionDbId, contentSessionId: dbSession.content_session_id, - memorySessionId: dbSession.memory_session_id || null, + memorySessionId: null, // Always start fresh - SDK will capture new ID project: dbSession.project, userPrompt, pendingMessages: [], @@ -143,10 +156,11 @@ export class SessionManager { currentProvider: null // Will be set when generator starts }; - logger.debug('SESSION', 'Creating new session object', { + logger.debug('SESSION', 'Creating new session object (memorySessionId cleared to prevent stale resume)', { sessionDbId, contentSessionId: dbSession.content_session_id, - memorySessionId: dbSession.memory_session_id || '(none - fresh session)', + dbMemorySessionId: dbSession.memory_session_id || '(none in DB)', + memorySessionId: '(cleared - will capture fresh from SDK)', lastPromptNumber: promptNumber || this.dbManager.getSessionStore().getPromptNumberFromUserPrompts(dbSession.content_session_id) });