{"record":{"id":"c75e65925762cf95","repo":"thedotmack/claude-mem","slug":"failed-to-capture-session-id-while-priming-corpus","errorCode":null,"errorMessage":"Failed to capture session_id while priming corpus \"${corpus.name}\"","messagePattern":"Failed to capture session_id while priming corpus \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/services/worker/knowledge/KnowledgeAgent.ts","lineNumber":75,"sourceCode":"        if (msg.session_id) sessionId = msg.session_id;\n        if (msg.type === 'result') {\n          logger.info('WORKER', `Knowledge agent primed for corpus \"${corpus.name}\"`);\n        }\n      }\n    } catch (error) {\n      if (sessionId) {\n        if (error instanceof Error) {\n          logger.debug('WORKER', `SDK process exited after priming corpus \"${corpus.name}\" — session captured, continuing`, {}, error);\n        } else {\n          logger.debug('WORKER', `SDK process exited after priming corpus \"${corpus.name}\" — session captured, continuing (non-Error thrown)`, { thrownValue: String(error) });\n        }\n      } else {\n        throw error;\n      }\n    }\n\n    if (!sessionId) {\n      throw new Error(`Failed to capture session_id while priming corpus \"${corpus.name}\"`);\n    }\n\n    corpus.session_id = sessionId;\n    this.corpusStore.write(corpus);\n\n    return sessionId;\n  }\n\n  async query(corpus: CorpusFile, question: string): Promise<QueryResult> {\n    if (!corpus.session_id) {\n      throw new Error(`Corpus \"${corpus.name}\" has no session — call prime first`);\n    }\n\n    try {\n      const result = await this.executeQuery(corpus, question);\n      if (result.session_id !== corpus.session_id) {\n        corpus.session_id = result.session_id;\n        this.corpusStore.write(corpus);","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d768ba364302d12b76e69e4f021f0bb1d2d50ed6/src/services/worker/knowledge/KnowledgeAgent.ts#L57-L93","documentation":"Thrown by KnowledgeAgent.prime after it ran the Claude Agent SDK query loop for a corpus but never received a msg.session_id on any streamed message. The session id is required to persist corpus.session_id for later query() calls. If the SDK errored AND no session was captured, the original error is rethrown instead (line 70); this message fires only when the loop completed/errored without ever emitting session_id.","triggerScenarios":"prime() consumes the SDK async iterator; no message carried session_id. Causes: SDK exited immediately, model refused the prompt, auth/env failure producing non-session messages, or the result message arrived without session_id.","commonSituations":"OAuth credentials missing/expired so the SDK can't start a session; CLAUDE_CODE_PATH resolves to an incompatible binary that exits early; model error or rate limit before session establishment; isolatedEnv missing required tokens.","solutions":["Check WORKER logs for the SDK error/exit reason around the prime attempt (logged at debug when session was captured, rethrown otherwise).","Verify OAuth/auth env (buildIsolatedEnvWithFreshOAuth) provides valid credentials.","Confirm findClaudeExecutable returns a capable CLI version (see errors 150/153).","Retry prime after fixing auth; if persistent, inspect the raw SDK messages to see what was emitted."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before priming, confirm prerequisites\nconst claudePath = findClaudeExecutable('WORKER'); // throws its own clear errors\nconst env = await buildIsolatedEnvWithFreshOAuth();\nif (!env.ANTHHOPIC_AUTH_TOKEN && !env.CLAUDE_CODE_OAUTH_TOKEN) {\n  throw new Error('Cannot prime corpus: auth credentials missing');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await agent.prime(corpus);\n} catch (err) {\n  if (err instanceof Error && /Failed to capture session_id/.test(err.message)) {\n    logger.error('WORKER', 'Prime produced no session — check auth/CLI', { corpus: corpus.name });\n    // surface actionable guidance, do not silently retry\n  }\n  throw err;\n}","preventionTips":["Ensure OAuth credentials are valid and present in the isolated env before priming.","Confirm findClaudeExecutable returns a capable CLI (watch for errors 150/153).","Log raw SDK message types during prime to diagnose why session_id never arrived."],"tags":["knowledge-agent","claude-sdk","session","prime","auth"],"backgroundTag":null,"analyzedSha":"d768ba364302d12b76e69e4f021f0bb1d2d50ed6","analyzedAt":"2026-08-12T23:52:55.241Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}