thedotmack/claude-mem · error · Error

Failed to capture session_id while priming corpus

Error message

Failed to capture session_id while priming corpus "${corpus.name}"

What it means

KnowledgeAgent.prime() starts a Claude session for a corpus and extracts the returned session_id; if the session started but no session_id could be captured, it throws this error. Without a session_id the corpus cannot be queried later, so priming aborts and the corpus file is left un-primed (or with a stale session).

Solutions

  1. Retry prime() — transient output issues often resolve on a second run
  2. Check the parsed response shape against your installed Claude SDK/CLI version (session_id location may have changed)
  3. Log the raw priming result to see why sessionId stayed undefined
  4. Update or pin the Claude Agent SDK version to one matching the parser, then reprime

Example fix

// before
const result = await runPriming(corpus);
const sessionId = result.session_id; // undefined after SDK upgrade
// after
const result = await runPriming(corpus);
const sessionId = result.session_id ?? result.sessionId ?? undefined; // handle both shapes
if (!sessionId) logger.debug('WORKER', 'prime result', { result });
Defensive patterns

Strategy: retry

Validate before calling

function primeResultLooksValid(result: unknown): boolean {
  const r = result as { session_id?: unknown } | null;
  return !!r && typeof r.session_id === 'string' && r.session_id.length > 0;
}

Try / catch

try {
  const sessionId = await agent.prime(corpus);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Failed to capture session_id')) {
    logger.warn('prime did not yield session_id — check SDK version / response shape', { corpus: corpus.name, cause: err });
    // retry once after logging the raw response shape
  } else {
    throw err;
  }
}

Prevention

When it happens

Trigger: The priming conversation completed but the SDK response contained no session_id — e.g. the claude CLI/SDK changed its response shape, output parsing failed, or the process errored in a way that returned without an id but was not rethrown.

Common situations: Claude Agent SDK version upgrade changing where session_id appears in the result; malformed output from the priming prompt; corpus store write permission issue masking a partially completed prime; transient backend error swallowed by an earlier catch branch.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/c75e65925762cf95. Report an issue: GitHub.

Appendix: source

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

        if (msg.session_id) sessionId = msg.session_id;
        if (msg.type === 'result') {
          logger.info('WORKER', `Knowledge agent primed for corpus "${corpus.name}"`);
        }
      }
    } catch (error) {
      if (sessionId) {
        if (error instanceof Error) {
          logger.debug('WORKER', `SDK process exited after priming corpus "${corpus.name}" — session captured, continuing`, {}, error);
        } else {
          logger.debug('WORKER', `SDK process exited after priming corpus "${corpus.name}" — session captured, continuing (non-Error thrown)`, { thrownValue: String(error) });
        }
      } else {
        throw error;
      }
    }

    if (!sessionId) {
      throw new Error(`Failed to capture session_id while priming corpus "${corpus.name}"`);
    }

    corpus.session_id = sessionId;
    this.corpusStore.write(corpus);

    return sessionId;
  }

  async query(corpus: CorpusFile, question: string): Promise<QueryResult> {
    if (!corpus.session_id) {
      throw new Error(`Corpus "${corpus.name}" has no session — call prime first`);
    }

    try {
      const result = await this.executeQuery(corpus, question);
      if (result.session_id !== corpus.session_id) {
        corpus.session_id = result.session_id;
        this.corpusStore.write(corpus);

View on GitHub (pinned to d8bc9755e7)