{"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":"warning","filePath":"src/services/worker/ClaudeProvider.ts","lineNumber":269,"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":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/8bc631a71a487424b866756e43a6efa4574cc66b/src/services/worker/ClaudeProvider.ts#L251-L287","documentation":"The observer resumes SDK sessions via memorySessionId, but only when the current prompt is a continuation (lastPromptNumber > 1). On the first (INIT) prompt it always starts fresh and captures a new session id; if a stale memorySessionId already exists at prompt #1, the prior SDK context is unrecoverable (typically the worker restarted or crashed between session creation and the first prompt), so this warning records the discarded resume.","triggerScenarios":"Worker restart or crash after a session row was created but before its first prompt completed; a session-init hook running twice creating a reused row; database restored from a backup mid-session.","commonSituations":"Restarting claude-mem or the machine right after starting a coding session; worker crash loops; killing the worker between session-init and the first observation.","solutions":["No data is lost from claude-mem's own DB — the observer starts a new SDK session and continues capturing","To avoid it, keep the worker stable across session starts (check restart/crash logs around the timestamp)","If it happens constantly, look for a crash loop in worker logs and fix the underlying startup failure"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"function shouldResumeSdkSession(session: { lastPromptNumber: number; memorySessionId: string | null }): session is { lastPromptNumber: number; memorySessionId: string } {\n  return session.lastPromptNumber > 1 && Boolean(session.memorySessionId);\n}","typeGuard":"function isFirstPrompt(session: { lastPromptNumber: number }): boolean {\n  return session.lastPromptNumber <= 1;\n}","tryCatchPattern":null,"preventionTips":["Expect a fresh SDK session on the INIT prompt — never assume memorySessionId survives worker restarts","Keep the worker running across session starts to avoid the crash/restart window","Monitor worker stability (restart logs) if this warning appears frequently"],"tags":["sdk","session-resume","observer","worker-restart"],"backgroundTag":"session-context-lost","analyzedSha":"8bc631a71a487424b866756e43a6efa4574cc66b","analyzedAt":"2026-08-20T23:58:13.836Z","contentChangedAt":"2026-08-20T23:58:13.836Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}