thedotmack/claude-mem · warning

Context requested before initialization complete, returning

Error message

Context requested before initialization complete, returning empty

What it means

The worker's /api/context/inject route is guarded by middleware: until initializationCompleteFlag is set and searchRoutes is registered (DB plus search index fully initialized), the route short-circuits with a 200 containing empty text instead of real memory context. This warning records that a caller asked for context during that window and silently got nothing.

Source

Thrown at src/services/worker-service.ts:321

  }

  private registerSignalHandlers(): void {
    // Do NOT pre-set isShuttingDown here: the flag is now the re-entrancy
    // guard INSIDE shutdown() (runShutdownSequence), and pre-setting it would
    // turn the signal-path shutdown into a no-op. The supervisor has its own
    // signal re-entrancy guard (shutdownInitiated in src/supervisor/index.ts).
    configureSupervisorSignalHandlers(async () => {
      await this.shutdown('signal');
    });
  }

  private registerRoutes(): void {

    this.server.registerRoutes(new ChromaRoutes());

    this.server.app.get('/api/context/inject', async (req, res, next) => {
      if (!this.initializationCompleteFlag || !this.searchRoutes) {
        logger.warn('SYSTEM', 'Context requested before initialization complete, returning empty');
        res.status(200).json({ content: [{ type: 'text', text: '' }] });
        return;
      }

      next(); 
    });

    this.server.app.use(['/api', '/v1'], async (req, res, next) => {
      if (
        req.path === '/chroma/status' ||
        req.path === '/health' ||
        req.path === '/readiness' ||
        req.path === '/version' ||
        req.path === '/settings/dependency-health'
      ) {
        next();
        return;
      }

View on GitHub (pinned to e2d1df569a)

Solutions

  1. Poll /readiness (not just /health) before requesting context injection
  2. Retry the context inject once after a short delay if the response text is empty
  3. If init is persistently slow, check worker logs for Chroma/uv/embedding startup failures

Example fix

// before
const res = await fetch(`${base}/api/context/inject?projects=${p}`);

// after
await fetch(`${base}/readiness`); // resolves when DB+search init done
const res = await fetch(`${base}/api/context/inject?projects=${p}`);
Defensive patterns

Strategy: retry

Validate before calling

async function waitUntilReady(base: string, timeoutMs = 30000): Promise<boolean> {
  const deadline = Date.now() + timeoutMs;
  while (Date.now() < deadline) {
    try { if (await fetch(`${base}/readiness`).then(r => r.ok)) return true; } catch {}
    await new Promise(r => setTimeout(r, 500));
  }
  return false;
}

Prevention

When it happens

Trigger: A hook fires /api/context/inject right after the worker process spawned but before DB/search init finished; the worker restarted mid-session and the next prompt's context fetch races initialization; slow first-run Chroma/embedding setup widens the window.

Common situations: First prompt after a cold start or `claude-mem restart`; large transcript backlog making init slow; health check (/health) passing while readiness (/readiness) has not — callers that only poll health hit this.

Related errors


AI-assisted analysis of thedotmack/claude-mem@e2d1df569a (2026-08-20). Data as JSON: /api/errors/3a704d7772c45163. Report an issue: GitHub.