Skip to content

cbm v0.8.1 silently deletes project DBs on "corrupt" detection — data loss with no recovery #557

Description

@jonlimx

Summary

codebase-memory-mcp v0.8.1 (and earlier) contains a deleting corrupt db code
path that silently unlinks the project database file when its startup
integrity check raises any anomaly. This is a "delete on first suspicion"
design that causes total data loss with no backup, no log warning, and no
recovery path — even when the "corruption" is a false positive.

Evidence

The behavior is in the compiled binary and observable via strings:

deleting corrupt db
ERROR store.corrupt table=projects rows=%d (expected 1)
ERROR store.corrupt table=projects bad_root_path=%s
integrity_check_failed
malformed database schema (%s)
file is not a database
broke stale lock on %s

Related binary paths (WAL mode + atomic rename used for normal writes):

PRAGMA journal_mode = WAL;
%s.tmp
dump: rename failed
rename_to_cache

Trigger conditions (false positives)

In my case the project DB was deleted after I performed a sequence of
pkill -9 on the cbm process and binary swaps between standard and ui
variants (the install.sh --ui switch). The next cbm startup detected
"corruption" and deleted the 28 MB <project>.db file. Other plausible
triggers I can think of:

  1. WAL file leftover from a previously-SIGKILL'd cbm (most likely in
    my case). When the old process is killed, SQLite's -wal and -shm
    sidecar files can be left in a state the new process interprets as
    corruption.
  2. Binary version drift between cbm builds (e.g., switching between
    codebase-memory-mcp-darwin-arm64.tar.gz and
    codebase-memory-mcp-ui-darwin-arm64.tar.gz). Even minor schema-version
    differences would trip the check.
  3. Lock-file contention — the broke stale lock on %s string shows cbm
    forcibly breaks stale locks; the DB state immediately after lock break
    may look anomalous and trigger the delete.

Repro

  1. codebase-memory-mcp cli index_repository '{"repo_path":"/path/to/proj","mode":"full"}'
  2. pkill -9 -f "codebase-memory-mcp" while index is in progress (or
    between operations)
  3. Start cbm again. The <proj>.db file disappears.
  4. No log line other than level=info msg=mem.init ... precedes the loss.

Impact

  • Total data loss of the indexed project (in my case: 8 740 nodes /
    26 412 edges / 28 MB SQLite, plus the graph.db.zst artifact)
  • No backup, no .corrupt.<timestamp> rename, no in-process log warning
  • User has no idea what happened until they re-open the UI and see
    "No indexed projects"
  • Recovery requires re-running the full reindex, which costs time
    (3-4 min for Linux kernel per the README, several minutes for any
    real codebase)

Suggested fix

Replace the unlink with a rename to a timestamped .corrupt file and
emit a prominent stderr log so the user has a chance to act:

// pseudocode for the recovery branch
if (integrity_check_fails) {
    char corrupt_path[PATH_MAX];
    snprintf(corrupt_path, sizeof(corrupt_path),
             "%s.corrupt.%ld", db_path, (long)time(NULL));
    rename(db_path, corrupt_path);
    fprintf(stderr,
        "ERROR project database marked corrupt: %s\n"
        "ERROR   preserved as: %s\n"
        "ERROR   re-index:  codebase-memory-mcp cli index_repository "
        "'{\"repo_path\":\"...\",\"mode\":\"full\"}'\n",
        db_path, corrupt_path);
}

This preserves the evidence, lets the user inspect what was wrong, and
gives them a recovery path (file a bug with the corrupt DB attached).

Environment

  • cbm 0.8.1 (binary --version and serverInfo.version both report 0.8.1;
    note: prior 0.6.1 release had a serverInfo.version: 0.10.0 mismatch
    per issue #350)
  • macOS 26.4.1, Darwin 25.4.0 arm64, Apple Silicon
  • Binary: Mach-O 64-bit executable arm64, 270.9 MB (UI variant)
  • Related: issue #350
    (UI HTTP server doesn't bind, closed) and
    issue #402
    (UI binary crashes mid-render, open) — both UI-related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstability/performanceServer crashes, OOM, hangs, high CPU/memory

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions