thedotmack/claude-mem · info

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 registered before background initialization finishes. If a request arrives while initializationCompleteFlag is false or searchRoutes is not yet set, the worker answers immediately with an empty text content block (HTTP 200) instead of serving real injected context.

Solutions

  1. Retry the request after a short delay until initialization completes (add client-side retry with backoff).
  2. Check worker logs for background-initialization errors — if init failed, searchRoutes will never be set; fix the underlying init failure.
  3. Wait for a worker health/ready signal before sending /api/context/inject requests.
  4. If it persists on every request, restart the worker and watch for init errors.

Example fix

// before: fire immediately after spawn
await fetch('http://127.0.0.1:37777/api/context/inject', ...)
// after: poll readiness first
await waitFor(() => fetch('http://127.0.0.1:37777/api/health').ok, { intervalMs: 250 });
await fetch('http://127.0.0.1:37777/api/context/inject', ...)
Defensive patterns

Strategy: retry

Validate before calling

async function isWorkerReady(base) {
  const r = await fetch(base + '/api/health');
  return r.ok;
}

Try / catch

try {
  const res = await fetch(base + '/api/context/inject');
  const body = await res.json();
  if (body.content?.[0]?.text === '') {
    await sleep(500); // init likely incomplete; retry
  }
} catch (err) { /* handle */ }

Prevention

When it happens

Trigger: registerRoutes (constructor) sets up the /api/context/inject middleware; any client request hitting that endpoint during worker startup — or if searchRoutes never initialized (background init crashed) — triggers the empty response.

Common situations: MCP client or script starts a query the instant the worker port opens; slow Chroma/DB initialization after restart; background init failure leaving searchRoutes unset so every request returns empty.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

  private startDeferredSessionEndReplay(): void {
    if (this.deferredSessionEndReplayTimer !== null) return;

    this.deferredSessionEndReplayTimer = setInterval(() => {
      void this.drainDeferredSessionEndQueue().catch((error: unknown) => {
        logger.warn('SESSION', 'Deferred SessionEnd replay loop failed', {},
          error instanceof Error ? error : new Error(String(error)));
      });
    }, 30_000);
    this.deferredSessionEndReplayTimer.unref?.();
  }

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