rohitg00/agentmemory · warning

[agentmemory] Failed to backfill memories into BM25:

Error message

[agentmemory] Failed to backfill memories into BM25:

What it means

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.

Source

Thrown at src/index.ts:524

          timestamp: memory.createdAt,
          type: "decision",
          title: memory.title,
          facts: [memory.content],
          narrative: memory.content,
          concepts: memory.concepts,
          files: memory.files,
          importance: memory.strength,
        });
        backfilled++;
      }
      if (backfilled > 0) {
        bootLog(
          `Backfilled ${backfilled} memories into BM25 (legacy index gap)`,
        );
        indexPersistence.scheduleSave();
      }
    } catch (err) {
      console.warn(
        `[agentmemory] Failed to backfill memories into BM25:`,
        err,
      );
    }
  }

  // Ready / Endpoints lines are emitted via `bootLog` so they're
  // buffered in quiet mode and printed verbatim under --verbose. The
  // CLI surfaces a compact summary when it sees the worker reach
  // ready state.
  bootLog(
    `Ready. ${embeddingProvider ? "Triple-stream (BM25+Vector+Graph)" : "BM25+Graph"} search active.`,
  );
  bootLog(
    `REST API: 130 endpoints at http://localhost:${config.restPort}/agentmemory/*`,
  );
  bootLog(
    `MCP surface (opt-in via \`npx @agentmemory/mcp\`): ${getAllTools().length} tools · 6 resources · 3 prompts`,

View on GitHub (pinned to e04ba88819)

Solutions

  1. Inspect the logged `err` object to identify whether the KV read or an index add failed.
  2. Check data/state_store.db integrity: stop the daemon, back it up, and try restarting; if corrupt, restore from backup.
  3. Restart the daemon so the backfill re-runs — it is idempotent (bm25Index.has() short-circuits already-indexed ids).
  4. If specific memory records are malformed, delete or repair them, or trigger a full search-index rebuild to regenerate the BM25 index from scratch.
  5. Verify disk space and file permissions on the agentmemory data directory.

Example fix

// before — root cause often a bare kv.list failing silently at boot
const memories = await kv.list<Memory>(KV.memories);
// after — guard the boot path and fall back to a full rebuild
let memories: Memory[] = [];
try { memories = await kv.list<Memory>(KV.memories); }
catch { await rebuildSearchIndex(); return; }
Defensive patterns

Strategy: fallback

Validate before calling

// before relying on smart search after an upgrade, verify index coverage
const memories = await kv.list<Memory>(KV.memories);
const missing = memories.filter(m => m.isLatest !== false && !bm25Index.has(m.id));
if (missing.length > 0) console.warn(`${missing.length} memories not in BM25 index — schedule rebuild`);

Type guard

function isIndexableMemory(m: unknown): m is Memory {
  return !!m && typeof m === 'object' && typeof (m as Memory).id === 'string'
    && typeof (m as Memory).title === 'string' && typeof (m as Memory).content === 'string';
}

Try / catch

try {
  await kv.list<Memory>(KV.memories);
} catch (err) {
  console.warn('[agentmemory] backfill skipped:', err);
  await rebuildSearchIndex().catch(() => {}); // fallback to full rebuild
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/ad23a35d3aa13ebe. Report an issue: GitHub.