{"record":{"id":"7e5c52b136013bb7","repo":"thedotmack/claude-mem","slug":"skipping-resume-for-init-prompt-despite-existing-m","errorCode":null,"errorMessage":"Skipping resume for INIT prompt despite existing memorySessionId=${session.memorySessionId} - SDK context was lost (worker restart or crash recovery)","messagePattern":"Skipping resume for INIT prompt despite existing memorySessionId=(.+?) - SDK context was lost \\(worker restart or crash recovery\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/services/worker/ClaudeProvider.ts","lineNumber":293,"sourceCode":"\n      logger.info('SDK', 'Starting SDK query', {\n        sessionDbId: session.sessionDbId,\n        contentSessionId: session.contentSessionId,\n        memorySessionId: session.memorySessionId ?? undefined,\n        hasRealMemorySessionId,\n        shouldResume,\n        resume_parameter: shouldResume ? session.memorySessionId : '(none - fresh start)',\n        lastPromptNumber: session.lastPromptNumber,\n        authMethod\n      });\n\n      if (session.lastPromptNumber > 1) {\n        logger.debug('SDK', `[ALIGNMENT] Resume Decision | contentSessionId=${session.contentSessionId} | memorySessionId=${session.memorySessionId} | prompt#=${session.lastPromptNumber} | hasRealMemorySessionId=${hasRealMemorySessionId} | shouldResume=${shouldResume} | resumeWith=${shouldResume ? session.memorySessionId : 'NONE'}`);\n      } else {\n        const hasStaleMemoryId = hasRealMemorySessionId;\n        logger.debug('SDK', `[ALIGNMENT] First Prompt (INIT) | contentSessionId=${session.contentSessionId} | prompt#=${session.lastPromptNumber} | hasStaleMemoryId=${hasStaleMemoryId} | action=START_FRESH | Will capture new memorySessionId from SDK response`);\n        if (hasStaleMemoryId) {\n          logger.warn('SDK', `Skipping resume for INIT prompt despite existing memorySessionId=${session.memorySessionId} - SDK context was lost (worker restart or crash recovery)`);\n        }\n      }\n\n      ensureDir(OBSERVER_SESSIONS_DIR);\n      const queryResult = query({\n        prompt: messageGenerator,\n        options: buildHardenedSdkOptions({\n          source: 'Observer',\n          sessionDbId: session.sessionDbId,\n          contentSessionId: session.contentSessionId,\n          project: session.project,\n          model: modelId,\n          env: isolatedEnv,  // Use isolated credentials from ~/.claude-mem/.env, not process.env\n          pathToClaudeCodeExecutable: claudePath,\n          abortController: session.abortController,\n          ...(shouldResume && session.memorySessionId ? { resume: session.memorySessionId } : {}),\n          spawnClaudeCodeProcess: createSdkSpawnFactory(session.sessionDbId, slotReservation, observerExtraArgs),\n        }),","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/services/worker/ClaudeProvider.ts#L275-L311","documentation":"startSession normally resumes an existing SDK session via memorySessionId. For the first (INIT) prompt, however, a stored memorySessionId is necessarily stale — the SDK context was lost (worker restart or crash recovery) — so the provider deliberately starts fresh and logs this warning instead of attempting a resume that would fail.","triggerScenarios":"promptNumber === 1 (INIT), hasRealMemorySessionId is true (a memorySessionId persisted from before), shouldResume is false, so the code chooses START_FRESH and warns about the skipped resume.","commonSituations":"Worker restarted between the project's previous session and a new INIT prompt; crash recovery restored the session record with an old memorySessionId; database retains a session row whose SDK process no longer exists.","solutions":["No action needed — starting fresh is the correct recovery; a new memorySessionId will be captured from the SDK response","If this recurs unexpectedly, investigate why the worker restarts between prompts (crash logs)","Clear stale session rows if old memorySessionIds accumulate and cause confusion"],"exampleFix":"null","handlingStrategy":"validation","validationCode":"// Only resume when there is a real session and it is not the first prompt\nconst canResume = session.lastPromptNumber > 1 && !!session.memorySessionId && memorySessionIdLooksReal(session.memorySessionId);\nif (!canResume) console.log('starting fresh session');","typeGuard":null,"tryCatchPattern":"try {\n  await startSession(session, /* resume */ shouldResume);\n} catch (err) {\n  // stale session id: restart fresh\n  await startSession(session, false);\n}","preventionTips":["Treat memorySessionId from before a worker restart as stale","Always capture the fresh memorySessionId from the first SDK response","Log resume decisions (prompt#, hasRealMemorySessionId) for easier diagnosis"],"tags":["sdk","session","resume","recovery"],"backgroundTag":"invalid-state-transition","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}