{"record":{"id":"3a704d7772c45163","repo":"thedotmack/claude-mem","slug":"context-requested-before-initialization-complete","errorCode":null,"errorMessage":"Context requested before initialization complete, returning empty","messagePattern":"Context requested before initialization complete, returning empty","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/services/worker-service.ts","lineNumber":321,"sourceCode":"  }\n\n  private registerSignalHandlers(): void {\n    // Do NOT pre-set isShuttingDown here: the flag is now the re-entrancy\n    // guard INSIDE shutdown() (runShutdownSequence), and pre-setting it would\n    // turn the signal-path shutdown into a no-op. The supervisor has its own\n    // signal re-entrancy guard (shutdownInitiated in src/supervisor/index.ts).\n    configureSupervisorSignalHandlers(async () => {\n      await this.shutdown('signal');\n    });\n  }\n\n  private registerRoutes(): void {\n\n    this.server.registerRoutes(new ChromaRoutes());\n\n    this.server.app.get('/api/context/inject', async (req, res, next) => {\n      if (!this.initializationCompleteFlag || !this.searchRoutes) {\n        logger.warn('SYSTEM', 'Context requested before initialization complete, returning empty');\n        res.status(200).json({ content: [{ type: 'text', text: '' }] });\n        return;\n      }\n\n      next(); \n    });\n\n    this.server.app.use(['/api', '/v1'], async (req, res, next) => {\n      if (\n        req.path === '/chroma/status' ||\n        req.path === '/health' ||\n        req.path === '/readiness' ||\n        req.path === '/version' ||\n        req.path === '/settings/dependency-health'\n      ) {\n        next();\n        return;\n      }","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/e2d1df569a8f04075d40e92461128ece7cf04c82/src/services/worker-service.ts#L303-L339","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Poll /readiness (not just /health) before requesting context injection","Retry the context inject once after a short delay if the response text is empty","If init is persistently slow, check worker logs for Chroma/uv/embedding startup failures"],"exampleFix":"// before\nconst res = await fetch(`${base}/api/context/inject?projects=${p}`);\n\n// after\nawait fetch(`${base}/readiness`); // resolves when DB+search init done\nconst res = await fetch(`${base}/api/context/inject?projects=${p}`);","handlingStrategy":"retry","validationCode":"async function waitUntilReady(base: string, timeoutMs = 30000): Promise<boolean> {\n  const deadline = Date.now() + timeoutMs;\n  while (Date.now() < deadline) {\n    try { if (await fetch(`${base}/readiness`).then(r => r.ok)) return true; } catch {}\n    await new Promise(r => setTimeout(r, 500));\n  }\n  return false;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Gate memory-dependent calls on /readiness, never on /health alone","Treat an empty-text 200 from /api/context/inject as 'not ready yet' and retry once","Expect this after every worker restart; keep the retry in the caller"],"tags":["worker","initialization","context-injection","race-condition"],"backgroundTag":"service-not-ready","analyzedSha":"e2d1df569a8f04075d40e92461128ece7cf04c82","analyzedAt":"2026-08-20T23:58:13.836Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}