{"record":{"id":"02f97f10da8e49f9","repo":"thedotmack/claude-mem","slug":"auto-reprime-failed-for-corpus-corpus-name","errorCode":null,"errorMessage":"Auto-reprime failed for corpus \"${corpus.name}\"","messagePattern":"Auto-reprime failed for corpus \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/services/worker/knowledge/KnowledgeAgent.ts","lineNumber":109,"sourceCode":"      if (result.session_id !== corpus.session_id) {\n        corpus.session_id = result.session_id;\n        this.corpusStore.write(corpus);\n      }\n      return result;\n    } catch (error) {\n      if (!this.isSessionResumeError(error)) {\n        if (error instanceof Error) {\n          logger.error('WORKER', `Query failed for corpus \"${corpus.name}\"`, {}, error);\n        } else {\n          logger.error('WORKER', `Query failed for corpus \"${corpus.name}\" (non-Error thrown)`, { thrownValue: String(error) });\n        }\n        throw error;\n      }\n      logger.info('WORKER', `Session expired for corpus \"${corpus.name}\", auto-repriming...`);\n      await this.prime(corpus);\n      const refreshedCorpus = this.corpusStore.read(corpus.name);\n      if (!refreshedCorpus || !refreshedCorpus.session_id) {\n        throw new Error(`Auto-reprime failed for corpus \"${corpus.name}\"`);\n      }\n      const result = await this.executeQuery(refreshedCorpus, question);\n      if (result.session_id !== refreshedCorpus.session_id) {\n        refreshedCorpus.session_id = result.session_id;\n        this.corpusStore.write(refreshedCorpus);\n      }\n      return result;\n    }\n  }\n\n  async reprime(corpus: CorpusFile): Promise<string> {\n    corpus.session_id = null;  \n    return this.prime(corpus);\n  }\n\n  private isSessionResumeError(error: unknown): boolean {\n    const message = error instanceof Error ? error.message : String(error);\n    return /session|resume|expired|invalid.*session|not found/i.test(message);","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d768ba364302d12b76e69e4f021f0bb1d2d50ed6/src/services/worker/knowledge/KnowledgeAgent.ts#L91-L127","documentation":"Thrown by KnowledgeAgent.query during the auto-reprime recovery path: after a session-expired error, prime() was called, but re-reading the corpus from disk (corpusStore.read) returned null or a corpus with no session_id. This indicates prime completed yet failed to persist a session, or the file was deleted/corrupted between write and read.","triggerScenarios":"isSessionResumeError matched (session/expired/not-found), prime() ran, then corpusStore.read(corpus.name) returned null or refreshedCorpus.session_id is falsy.","commonSituations":"prime() silently failed to capture session_id but didn't throw (race); corpora directory/file was removed concurrently; corpus file exists but session_id field is null due to a prior failed prime that still wrote the file; disk/permission issue on write.","solutions":["Investigate why prime() did not persist a session_id — check WORKER logs for the prime attempt and any 'Failed to capture session_id' (error 145).","Ensure the corpora directory is writable and the corpus file isn't deleted concurrently.","Manually reprime the corpus (agent.reprime) and watch for the underlying prime failure.","If the file is corrupt/missing, delete it and re-create + prime the corpus."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before relying on auto-reprime, ensure the corpus file is writable and present\nimport { existsSync } from 'fs';\nif (!existsSync(path.join(corporaDir, `${corpus.name}.corpus.json`))) {\n  throw new Error('Corpus file missing — cannot auto-reprime');\n}","typeGuard":null,"tryCatchPattern":"try {\n  return await agent.query(corpus, question);\n} catch (err) {\n  if (err instanceof Error && /Auto-reprime failed/.test(err.message)) {\n    logger.error('WORKER', 'Reprime failed to persist session', { corpus: corpus.name });\n    // manual recovery: reprime and inspect\n    await agent.reprime(corpus);\n  }\n  throw err;\n}","preventionTips":["Ensure the corpora directory is writable and not deleted concurrently.","After prime(), verify the persisted file has a non-null session_id.","Investigate any prime() that completes without capturing a session_id."],"tags":["knowledge-agent","corpus","session","recovery","concurrency"],"backgroundTag":null,"analyzedSha":"d768ba364302d12b76e69e4f021f0bb1d2d50ed6","analyzedAt":"2026-08-12T23:52:55.241Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}