thedotmack/claude-mem · error · Error

Auto-reprime failed for corpus "${corpus.name}"

Error message

Auto-reprime failed for corpus "${corpus.name}"

What it means

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.

Source

Thrown at src/services/worker/knowledge/KnowledgeAgent.ts:109

      if (result.session_id !== corpus.session_id) {
        corpus.session_id = result.session_id;
        this.corpusStore.write(corpus);
      }
      return result;
    } catch (error) {
      if (!this.isSessionResumeError(error)) {
        if (error instanceof Error) {
          logger.error('WORKER', `Query failed for corpus "${corpus.name}"`, {}, error);
        } else {
          logger.error('WORKER', `Query failed for corpus "${corpus.name}" (non-Error thrown)`, { thrownValue: String(error) });
        }
        throw error;
      }
      logger.info('WORKER', `Session expired for corpus "${corpus.name}", auto-repriming...`);
      await this.prime(corpus);
      const refreshedCorpus = this.corpusStore.read(corpus.name);
      if (!refreshedCorpus || !refreshedCorpus.session_id) {
        throw new Error(`Auto-reprime failed for corpus "${corpus.name}"`);
      }
      const result = await this.executeQuery(refreshedCorpus, question);
      if (result.session_id !== refreshedCorpus.session_id) {
        refreshedCorpus.session_id = result.session_id;
        this.corpusStore.write(refreshedCorpus);
      }
      return result;
    }
  }

  async reprime(corpus: CorpusFile): Promise<string> {
    corpus.session_id = null;  
    return this.prime(corpus);
  }

  private isSessionResumeError(error: unknown): boolean {
    const message = error instanceof Error ? error.message : String(error);
    return /session|resume|expired|invalid.*session|not found/i.test(message);

View on GitHub (pinned to d768ba3643)

Solutions

  1. 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).
  2. Ensure the corpora directory is writable and the corpus file isn't deleted concurrently.
  3. Manually reprime the corpus (agent.reprime) and watch for the underlying prime failure.
  4. If the file is corrupt/missing, delete it and re-create + prime the corpus.
Defensive patterns

Strategy: try-catch

Validate before calling

// Before relying on auto-reprime, ensure the corpus file is writable and present
import { existsSync } from 'fs';
if (!existsSync(path.join(corporaDir, `${corpus.name}.corpus.json`))) {
  throw new Error('Corpus file missing — cannot auto-reprime');
}

Try / catch

try {
  return await agent.query(corpus, question);
} catch (err) {
  if (err instanceof Error && /Auto-reprime failed/.test(err.message)) {
    logger.error('WORKER', 'Reprime failed to persist session', { corpus: corpus.name });
    // manual recovery: reprime and inspect
    await agent.reprime(corpus);
  }
  throw err;
}

Prevention

When it happens

Trigger: isSessionResumeError matched (session/expired/not-found), prime() ran, then corpusStore.read(corpus.name) returned null or refreshedCorpus.session_id is falsy.

Common situations: 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.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d768ba3643 (2026-08-12). Data as JSON: /api/errors/02f97f10da8e49f9. Report an issue: GitHub.