{"record":{"id":"ad23a35d3aa13ebe","repo":"rohitg00/agentmemory","slug":"agentmemory-failed-to-backfill-memories-into-bm2","errorCode":null,"errorMessage":"[agentmemory] Failed to backfill memories into BM25:","messagePattern":"\\[agentmemory\\] Failed to backfill memories into BM25:","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/index.ts","lineNumber":524,"sourceCode":"          timestamp: memory.createdAt,\n          type: \"decision\",\n          title: memory.title,\n          facts: [memory.content],\n          narrative: memory.content,\n          concepts: memory.concepts,\n          files: memory.files,\n          importance: memory.strength,\n        });\n        backfilled++;\n      }\n      if (backfilled > 0) {\n        bootLog(\n          `Backfilled ${backfilled} memories into BM25 (legacy index gap)`,\n        );\n        indexPersistence.scheduleSave();\n      }\n    } catch (err) {\n      console.warn(\n        `[agentmemory] Failed to backfill memories into BM25:`,\n        err,\n      );\n    }\n  }\n\n  // Ready / Endpoints lines are emitted via `bootLog` so they're\n  // buffered in quiet mode and printed verbatim under --verbose. The\n  // CLI surfaces a compact summary when it sees the worker reach\n  // ready state.\n  bootLog(\n    `Ready. ${embeddingProvider ? \"Triple-stream (BM25+Vector+Graph)\" : \"BM25+Graph\"} search active.`,\n  );\n  bootLog(\n    `REST API: 130 endpoints at http://localhost:${config.restPort}/agentmemory/*`,\n  );\n  bootLog(\n    `MCP surface (opt-in via \\`npx @agentmemory/mcp\\`): ${getAllTools().length} tools · 6 resources · 3 prompts`,","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/rohitg00/agentmemory/blob/e04ba88819c365c9acf9d6661ea802143e728bd6/src/index.ts#L506-L542","documentation":"During daemon startup, agentmemory walks KV.memories and backfills any memories missing from the restored BM25 search index (a gap in versions <0.9.5 where mem::remember never indexed memories). If this backfill loop throws — most commonly a KV read failure — the startup code catches it and logs this warning instead of crashing, leaving the daemon running with an incomplete search index. Memory saving still works; only memory_smart_search recall may be degraded until a rebuild succeeds.","triggerScenarios":"Raised in main() when the catch around `await kv.list<Memory>(KV.memories)` fires, or when bm25Index.add() throws on a malformed memory record during the legacy backfill path (users upgrading from <0.9.5 whose persisted BM25 lacks memory entries).","commonSituations":"Corrupt or locked SQLite state store (data/state_store.db); KV.memories entries that fail schema expectations mid-upgrade; disk I/O errors on the state DB; partially completed version upgrades leaving mixed-shape memory records.","solutions":["Inspect the logged `err` object to identify whether the KV read or an index add failed.","Check data/state_store.db integrity: stop the daemon, back it up, and try restarting; if corrupt, restore from backup.","Restart the daemon so the backfill re-runs — it is idempotent (bm25Index.has() short-circuits already-indexed ids).","If specific memory records are malformed, delete or repair them, or trigger a full search-index rebuild to regenerate the BM25 index from scratch.","Verify disk space and file permissions on the agentmemory data directory."],"exampleFix":"// before — root cause often a bare kv.list failing silently at boot\nconst memories = await kv.list<Memory>(KV.memories);\n// after — guard the boot path and fall back to a full rebuild\nlet memories: Memory[] = [];\ntry { memories = await kv.list<Memory>(KV.memories); }\ncatch { await rebuildSearchIndex(); return; }","handlingStrategy":"fallback","validationCode":"// before relying on smart search after an upgrade, verify index coverage\nconst memories = await kv.list<Memory>(KV.memories);\nconst missing = memories.filter(m => m.isLatest !== false && !bm25Index.has(m.id));\nif (missing.length > 0) console.warn(`${missing.length} memories not in BM25 index — schedule rebuild`);","typeGuard":"function isIndexableMemory(m: unknown): m is Memory {\n  return !!m && typeof m === 'object' && typeof (m as Memory).id === 'string'\n    && typeof (m as Memory).title === 'string' && typeof (m as Memory).content === 'string';\n}","tryCatchPattern":"try {\n  await kv.list<Memory>(KV.memories);\n} catch (err) {\n  console.warn('[agentmemory] backfill skipped:', err);\n  await rebuildSearchIndex().catch(() => {}); // fallback to full rebuild\n}","preventionTips":["Keep the state DB healthy: monitor disk space and back up data/state_store.db before upgrades.","After version upgrades, run a full search-index rebuild once instead of relying only on incremental backfill.","Validate memory records on write so malformed entries never enter KV.memories.","Watch boot logs for any '[agentmemory] Failed to ...' warnings after each restart."],"tags":["sqlite","kv-store","search-index","startup","upgrade-migration"],"backgroundTag":"kv-read-failure","analyzedSha":"e04ba88819c365c9acf9d6661ea802143e728bd6","analyzedAt":"2026-08-30T01:07:40.754Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}