thedotmack/claude-mem · error · Error

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

Error message

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

What it means

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.

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 d768ba3643)

Solutions

  1. Check WORKER logs for the SDK error/exit reason around the prime attempt (logged at debug when session was captured, rethrown otherwise).
  2. Verify OAuth/auth env (buildIsolatedEnvWithFreshOAuth) provides valid credentials.
  3. Confirm findClaudeExecutable returns a capable CLI version (see errors 150/153).
  4. Retry prime after fixing auth; if persistent, inspect the raw SDK messages to see what was emitted.
Defensive patterns

Strategy: try-catch

Validate before calling

// Before priming, confirm prerequisites
const claudePath = findClaudeExecutable('WORKER'); // throws its own clear errors
const env = await buildIsolatedEnvWithFreshOAuth();
if (!env.ANTHHOPIC_AUTH_TOKEN && !env.CLAUDE_CODE_OAUTH_TOKEN) {
  throw new Error('Cannot prime corpus: auth credentials missing');
}

Try / catch

try {
  await agent.prime(corpus);
} catch (err) {
  if (err instanceof Error && /Failed to capture session_id/.test(err.message)) {
    logger.error('WORKER', 'Prime produced no session — check auth/CLI', { corpus: corpus.name });
    // surface actionable guidance, do not silently retry
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

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

Related errors


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