Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

test(repro): data-loss + watcher reproductions for #557, #520

Martin Vogel8c35d5e2f437
diff --git a/Makefile.cbm b/Makefile.cbm
index 02060f0c0..4c8b6eb65 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -406,7 +406,9 @@ TEST_REPRO_SRCS = \
tests/repro/repro_issue546.c \
tests/repro/repro_issue627.c \
tests/repro/repro_issue514.c \
- tests/repro/repro_issue510.c
+ tests/repro/repro_issue510.c \
+ tests/repro/repro_issue557.c \
+ tests/repro/repro_issue520.c
ALL_TEST_SRCS =$(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_PHP_LSP_SRCS) $(TEST_CS_LSP_SRCS) $(TEST_CS_LSP_BENCH_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_PY_LSP_BENCH_SRCS) $(TEST_PY_LSP_STRESS_SRCS) $(TEST_PY_LSP_SCALE_SRCS) $(TEST_TS_LSP_SRCS) $(TEST_JAVA_LSP_SRCS) $(TEST_KOTLIN_LSP_SRCS) $(TEST_RUST_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_HTTPD_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_STACK_OVERFLOW_SRCS) $(TEST_INTEGRATION_SRCS)
diff --git a/tests/repro/repro_issue520.c b/tests/repro/repro_issue520.c
new file mode 100644
index 000000000..db8a25f25
--- /dev/null
+++ b/tests/repro/repro_issue520.c
@@ -0,0 +1,168 @@
+/*
+ * repro_issue520.c -- Reproduce-first case for OPEN bug #520.
+ *
+ * Issue: #520 -- "New files not detected without explicit re-index
+ * (watcher doesn't trigger for file creation)"
+ *
+ * Root cause (src/mcp/mcp.c: handle_detect_changes):
+ * detect_changes builds its changed-file list by running two git commands:
+ * (1) git diff --name-only <base>...HEAD (committed changes)
+ * (2) git diff --name-only (unstaged tracked changes)
+ * Neither command reports UNTRACKED new files. Those only appear in
+ * git status --porcelain (prefix "??"). Because handle_detect_changes
+ * never calls git status, a brand-new file that has not been git-added
+ * is completely invisible to the tool until the user manually calls
+ * index_repository again.
+ *
+ * Expected (correct) behaviour:
+ * After creating a new source file in a watched repo, calling
+ * detect_changes MUST include that file in "changed_files" so callers
+ * know the graph is stale and needs re-indexing (or so the incremental
+ * path can pick it up automatically).
+ *
+ * Actual (buggy) behaviour:
+ * detect_changes returns {"changed_files":[], "changed_count":0}.
+ * The new file is invisible until the user manually calls index_repository.
+ *
+ * Why RED on current code:
+ * The assertion below checks that "new_func.py" appears somewhere in the
+ * detect_changes JSON response. On current code the response contains an
+ * empty changed_files array, so strstr returns NULL and ASSERT_NOT_NULL
+ * fails.
+ *
+ * Fix location (not implemented here):
+ * src/mcp/mcp.c, handle_detect_changes(): after the existing git-diff
+ * popen block, add a second popen for:
+ * git --no-optional-locks -C <root> status --porcelain
+ * --untracked-files=normal 2>/dev/null
+ * and include lines prefixed "??" (untracked) and "A " (staged new file)
+ * in the changed_files output. The watcher already does exactly this via
+ * git_is_dirty() in src/watcher/watcher.c:140.
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+#include "test_helpers.h"
+#include <mcp/mcp.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+/* ── Local git helper (mirrors test_watcher.c:wt_git) ─────────── */
+
+/* Run "git -C <dir> <args>" with a neutral identity so the test
+ * needs no global git config and works under cmd.exe on Windows.
+ * Returns the git exit status. */
+static int r520_git(const char *dir, const char *args) {
+ char cmd[1024];
+ snprintf(cmd, sizeof(cmd),
+ "git -C \"%s\" -c user.name=t -c [email protected] "
+ "-c init.defaultBranch=main -c commit.gpgsign=false %s",
+ dir, args);
+ return system(cmd);
+}
+
+/* ── Test ──────────────────────────────────────────────────────── */
+
+/*
+ * Scenario (matches the exact steps from issue #520 comment):
+ *
+ * 1. Create a fresh git repo with one committed Python file.
+ * 2. Index the repo via the MCP index_repository tool so the server
+ * has a valid project handle (needed for detect_changes to resolve
+ * the project root).
+ * 3. Write a NEW untracked Python file (not git-added, not committed).
+ * 4. Call detect_changes -- this is the tool users call to discover
+ * what has changed since the last index.
+ * 5. Assert the new file name ("new_func.py") appears in the response.
+ *
+ * On current code step 5 FAILS: detect_changes only runs git-diff and
+ * misses untracked files entirely.
+ *
+ * No sleep is used: detect_changes is a synchronous, single-call API
+ * that runs git commands inline. There is no background thread or timer
+ * to wait for; the bug is purely in which git command is chosen.
+ */
+TEST(repro_issue520_detect_changes_includes_new_untracked_file) {
+ /* --- set up a temporary git repo -------------------------------- */
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_r520_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir))
+ FAIL("cbm_mkdtemp failed");
+
+ if (r520_git(tmpdir, "init -q") != 0) {
+ th_rmtree(tmpdir);
+ FAIL("git init failed");
+ }
+
+ /* Commit one baseline file so HEAD exists (needed for git diff base...HEAD) */
+ {
+ char p[512];
+ snprintf(p, sizeof(p), "%s/existing.py", tmpdir);
+ th_write_file(p, "def existing(): pass\n");
+ }
+ if (r520_git(tmpdir, "add existing.py") != 0 ||
+ r520_git(tmpdir, "commit -q -m \"init\"") != 0) {
+ th_rmtree(tmpdir);
+ FAIL("git commit failed");
+ }
+
+ /* --- index the repo via the MCP production flow ----------------- */
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ if (!srv) {
+ th_rmtree(tmpdir);
+ FAIL("cbm_mcp_server_new returned NULL");
+ }
+
+ {
+ char args[512];
+ snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", tmpdir);
+ char *resp = cbm_mcp_handle_tool(srv, "index_repository", args);
+ free(resp);
+ }
+
+ /* --- create a brand-new untracked file (never git-added) -------- */
+ {
+ char p[512];
+ snprintf(p, sizeof(p), "%s/new_func.py", tmpdir);
+ th_write_file(p, "def new_func(): return 42\n");
+ }
+
+ /* --- call detect_changes synchronously -------------------------- */
+ /* Use base_branch="main" -- the branch name matches init.defaultBranch
+ * set above. detect_changes runs git diff main...HEAD (same commit,
+ * no committed change) + git diff (no staged change), so on current
+ * code the result is always {"changed_files":[],"changed_count":0}.
+ * After the fix, git status --porcelain would also be consulted and
+ * new_func.py (marked "??") would appear in the output. */
+ char dc_args[512];
+ snprintf(dc_args, sizeof(dc_args),
+ "{\"base_branch\":\"main\"}");
+ char *dc_resp = cbm_mcp_handle_tool(srv, "detect_changes", dc_args);
+
+ /* --- assert the new file is reported ---------------------------- */
+ /* Expected: dc_resp contains "new_func.py" in the changed_files list.
+ * Actual (buggy): dc_resp contains "changed_count":0 and an empty
+ * changed_files array -- strstr returns NULL -- ASSERT_NOT_NULL FAILS. */
+ ASSERT_NOT_NULL(dc_resp);
+ int found = (strstr(dc_resp, "new_func.py") != NULL) ? 1 : 0;
+
+ free(dc_resp);
+ cbm_mcp_server_free(srv);
+ th_rmtree(tmpdir);
+
+ /* This is the reproduce-first assertion: RED until the fix lands.
+ * found == 0 means detect_changes ignored the untracked new file. */
+ ASSERT_EQ(found, 1);
+
+ PASS();
+}
+
+/* ── Suite entry point ─────────────────────────────────────────── */
+
+SUITE(repro_issue520) {
+ RUN_TEST(repro_issue520_detect_changes_includes_new_untracked_file);
+}
diff --git a/tests/repro/repro_issue557.c b/tests/repro/repro_issue557.c
new file mode 100644
index 000000000..9093d45ac
--- /dev/null
+++ b/tests/repro/repro_issue557.c
@@ -0,0 +1,285 @@
+/*
+ * repro_issue557.c -- Reproduce-first case for OPEN bug #557.
+ *
+ * Issue: #557 -- "cbm v0.8.1 silently deletes project DBs on 'corrupt'
+ * detection -- data loss with no recovery"
+ *
+ * DESTROYING CODE PATH:
+ * src/mcp/mcp.c resolve_store() lines 796-810
+ *
+ * The sequence is:
+ * 1. resolve_store() opens the project DB with cbm_store_open_path_query().
+ * 2. It calls cbm_store_check_integrity() (src/store/store.c:664).
+ * That function returns false when the projects table contains a row
+ * whose root_path does not start with '/', 'A'-'Z', or 'a'-'z' (the
+ * numeric-string corruption pattern -- e.g. "826" -- observed in the
+ * binary and confirmed in the issue report).
+ * 3. On false, resolve_store() calls cbm_unlink(path) at mcp.c:803,
+ * then cbm_unlink(wal_path) and cbm_unlink(shm_path) -- with NO rename,
+ * NO backup, NO recovery path. The user's indexed project is gone.
+ *
+ * ROOT CAUSE:
+ * "Delete on first suspicion" design in resolve_store(). The unlink is
+ * unconditional and irreversible. Any false-positive integrity signal
+ * (WAL/SHM leftover after SIGKILL, schema-version drift between standard
+ * and UI binary variants, or a root_path value that happens not to match
+ * the narrow whitelist) causes permanent data loss.
+ *
+ * EXPECTED (correct) behaviour:
+ * After cbm_store_check_integrity() returns false and resolve_store()
+ * executes its cleanup path, EITHER:
+ * (a) the original DB file must still exist at db_path (zero deletion), OR
+ * (b) a backup file must exist at a nearby path (e.g. "<db_path>.corrupt"
+ * or "<db_path>.bak") so the user can recover the data.
+ * The original DB must NOT be silently destroyed with no recovery path.
+ *
+ * ACTUAL (buggy) behaviour on v0.8.1:
+ * cbm_unlink(path) at mcp.c:803 destroys the DB file. After resolve_store()
+ * returns, access(db_path, F_OK) returns -1 (ENOENT) and no backup file
+ * exists -- total data loss.
+ *
+ * WHY RED on current code:
+ * The final ASSERT_TRUE checks that EITHER db_still_exists OR backup_exists.
+ * On buggy code cbm_unlink() runs with no rename, so both conditions are
+ * false and ASSERT_TRUE fires -- RED.
+ *
+ * TRIGGER:
+ * We construct the scenario directly at the store API level (no full index
+ * needed -- the integrity check runs before any graph data is consulted):
+ *
+ * 1. Set CBM_CACHE_DIR to a temp directory so the DB lands in a controlled
+ * location and does not pollute the real cache.
+ * 2. Create the DB via cbm_store_open_path() (creates schema + tables).
+ * 3. Insert one projects row with root_path = "826" -- the exact numeric
+ * string from the binary evidence in the issue report. This passes the
+ * "> 5 rows" check (only 1 row) but trips the bad_root_path check in
+ * cbm_store_check_integrity() because '8' is not '/', 'A'-'Z', or 'a'-'z'.
+ * 4. Close the store, verify the DB file exists (precondition).
+ * 5. Call cbm_mcp_handle_tool(srv, "search_graph", ...) with the project
+ * name. search_graph resolves the project store via resolve_store(),
+ * which opens the DB, runs the integrity check, detects bad_root_path,
+ * and executes the destroying cbm_unlink() at mcp.c:803.
+ * 6. Assert survival: DB file still exists OR a backup exists.
+ *
+ * NOTE on determinism:
+ * The "826" root_path value is a deterministically planted value -- not
+ * dependent on kill timing or WAL state. cbm_store_check_integrity() is
+ * a pure SQL query; its result for root_path="826" is guaranteed to be
+ * false on any build. The trigger is 100% reproducible.
+ *
+ * FIX LOCATION (not implemented here):
+ * src/mcp/mcp.c resolve_store() around line 803:
+ * Replace cbm_unlink(path) with a rename to a timestamped .corrupt path,
+ * then log a prominent error so the user knows where the preserved file is.
+ */
+
+#include <foundation/compat.h>
+#include "test_framework.h"
+
+#include <store/store.h>
+#include <mcp/mcp.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+/* Project name used throughout: must pass cbm_validate_project_name().
+ * Kept short and slug-safe so it is valid on every platform. */
+#define REPRO557_PROJECT "cbm-repro557-test"
+
+/* ── Helper: check whether a file exists ────────────────────────────── */
+
+static int file_exists(const char *path) {
+ struct stat st;
+ return (stat(path, &st) == 0) ? 1 : 0;
+}
+
+/* ── Test ─────────────────────────────────────────────────────────────
+ *
+ * repro_issue557_corrupt_db_not_silently_deleted
+ *
+ * Precondition (must be GREEN to prove the setup is correct):
+ * The DB file exists at db_path after we create and populate it.
+ * If this fires RED, the temp dir or store creation failed -- not #557.
+ *
+ * The failing assertion (RED on buggy code):
+ * After resolve_store() detects bad_root_path and runs its cleanup path,
+ * EITHER the DB file still exists OR a backup file exists.
+ * On buggy code: neither exists -- ASSERT_TRUE fires.
+ * ─────────────────────────────────────────────────────────────────── */
+
+TEST(repro_issue557_corrupt_db_not_silently_deleted) {
+ /* ── Step 1: redirect CBM_CACHE_DIR to a temp dir ─────────────────
+ *
+ * cbm_resolve_cache_dir() checks the CBM_CACHE_DIR env var first.
+ * Pointing it at a fresh temp dir ensures:
+ * - the test DB is isolated from the user's real cache
+ * - we know the exact db_path before the MCP call
+ *
+ * The static buffer in cbm_resolve_cache_dir() is updated on the
+ * next call because it re-reads CBM_CACHE_DIR each time. We must
+ * also call cbm_mkdir on the directory before opening the store.
+ */
+ char tmp_cache[512];
+ snprintf(tmp_cache, sizeof(tmp_cache), "/tmp/cbm_repro557_XXXXXX");
+ if (!cbm_mkdtemp(tmp_cache)) {
+ /* mkdtemp failed -- cannot run the test */
+ ASSERT_NOT_NULL(NULL); /* marks setup failure clearly */
+ }
+
+ /* Set the env var so all subsequent cbm_resolve_cache_dir() calls
+ * return tmp_cache. setenv is POSIX; Windows uses _putenv_s. */
+#if defined(_WIN32)
+ char ev[600];
+ snprintf(ev, sizeof(ev), "CBM_CACHE_DIR=%s", tmp_cache);
+ _putenv(ev);
+#else
+ setenv("CBM_CACHE_DIR", tmp_cache, 1 /* overwrite */);
+#endif
+
+ /* ── Step 2: build the DB path we will inspect ────────────────────
+ *
+ * project_db_path() in mcp.c computes: <cache_dir>/<project>.db
+ * Mirror the same formula here so db_path matches exactly.
+ */
+ char db_path[700];
+ snprintf(db_path, sizeof(db_path), "%s/%s.db", tmp_cache, REPRO557_PROJECT);
+
+ /* ── Step 3: create the DB via cbm_store_open_path() ──────────────
+ *
+ * cbm_store_open_path() calls store_open_internal() with
+ * SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, then runs init_schema()
+ * to create all tables including `projects`. This gives us a
+ * fully-structured DB at db_path.
+ */
+ cbm_store_t *setup_store = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(setup_store); /* precondition: store creation must work */
+
+ /* ── Step 4: insert a project row with a bad root_path ────────────
+ *
+ * root_path = "826" is the exact numeric string from the binary
+ * evidence in the issue report and confirmed by the integrity check
+ * SQL in cbm_store_check_integrity():
+ *
+ * SELECT root_path FROM projects
+ * WHERE root_path != ''
+ * AND NOT (substr(root_path,1,1) = '/'
+ * OR substr(...) BETWEEN 'A' AND 'Z'
+ * OR substr(...) BETWEEN 'a' AND 'z')
+ * LIMIT 1;
+ *
+ * '8' does not satisfy any of the three path-start conditions, so
+ * the query returns the row and cbm_store_check_integrity() returns
+ * false -- which is the exact trigger for the destroying path.
+ *
+ * cbm_store_upsert_project() is the store's own public API for
+ * writing project rows (used by the pipeline on every full index).
+ */
+ int rc = cbm_store_upsert_project(setup_store, REPRO557_PROJECT, "826");
+ ASSERT_EQ(rc, CBM_STORE_OK); /* precondition: row must be written */
+
+ cbm_store_close(setup_store);
+ setup_store = NULL;
+
+ /* ── Step 5: verify the DB exists before triggering the MCP path ──
+ *
+ * This is the precondition that confirms setup succeeded.
+ * If this fires RED, something in Steps 2-4 broke -- not #557.
+ */
+ ASSERT_TRUE(file_exists(db_path)); /* precondition: DB must exist now */
+
+ /* ── Step 6: drive resolve_store() via cbm_mcp_handle_tool ────────
+ *
+ * search_graph is the lightest query tool that reaches resolve_store().
+ * The tool handler calls resolve_store(srv, project) which:
+ * 1. Calls cbm_store_open_path_query(path) -- opens read-write/no-create.
+ * The DB was created in step 3 so SQLITE_OPEN_READWRITE succeeds.
+ * 2. Calls cbm_store_check_integrity() -- returns false (root_path="826").
+ * 3. Closes the store and calls cbm_unlink(path) at mcp.c:803.
+ * Then cbm_unlink(wal_path) and cbm_unlink(shm_path).
+ * 4. Returns NULL (resolve_store() returns NULL on corrupt detection).
+ *
+ * We do not assert anything about the search_graph response -- the
+ * response is irrelevant (it will be an error about the project not
+ * being found). What matters is the side-effect on db_path.
+ */
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv); /* precondition: server must initialise */
+
+ char args[512];
+ snprintf(args, sizeof(args),
+ "{\"project\":\"%s\","
+ "\"query\":\"Function\","
+ "\"limit\":1}",
+ REPRO557_PROJECT);
+
+ char *resp = cbm_mcp_handle_tool(srv, "search_graph", args);
+ /* Response may be NULL or an error string -- we do not assert on it.
+ * The side-effect (unlink) is what we are testing. */
+ if (resp) {
+ free(resp);
+ }
+ cbm_mcp_server_free(srv);
+
+ /* ── Step 7: PRIMARY ASSERTION -- the DB must survive ─────────────
+ *
+ * Correct behaviour: the DB is quarantined (renamed to a backup path)
+ * rather than silently destroyed. We accept either:
+ * (a) the original DB still exists at db_path (zero deletion), or
+ * (b) a backup file exists at a conventional backup path.
+ *
+ * Two conventional backup suffixes from the suggested fix in #557:
+ * "<db_path>.corrupt" -- timestamped or plain rename
+ * "<db_path>.bak" -- simpler alternative
+ *
+ * WHY RED on buggy code:
+ * cbm_unlink(path) at mcp.c:803 removes the file.
+ * No rename to .corrupt or .bak is performed.
+ * db_still_exists == 0 and backup_exists == 0.
+ * ASSERT_TRUE(0) fires -- RED.
+ */
+ int db_still_exists = file_exists(db_path);
+
+ char backup_corrupt[720], backup_bak[720];
+ snprintf(backup_corrupt, sizeof(backup_corrupt), "%s.corrupt", db_path);
+ snprintf(backup_bak, sizeof(backup_bak), "%s.bak", db_path);
+ int backup_exists = file_exists(backup_corrupt) || file_exists(backup_bak);
+
+ /* Clean up temp dir (best effort -- before the assertion so the dir
+ * is removed even when the assertion fails and longjmp unwinds). */
+ unlink(db_path);
+ unlink(backup_corrupt);
+ unlink(backup_bak);
+ char wal[730], shm[730];
+ snprintf(wal, sizeof(wal), "%s-wal", db_path);
+ snprintf(shm, sizeof(shm), "%s-shm", db_path);
+ unlink(wal);
+ unlink(shm);
+ rmdir(tmp_cache);
+
+#if defined(_WIN32)
+ _putenv("CBM_CACHE_DIR=");
+#else
+ unsetenv("CBM_CACHE_DIR");
+#endif
+
+ /*
+ * THE KEY ASSERTION -- must be RED on unpatched code:
+ *
+ * db_still_exists -- 1 if the DB was preserved in-place (zero-delete fix)
+ * backup_exists -- 1 if a .corrupt or .bak rename was made (quarantine fix)
+ *
+ * On buggy code: both are 0 because cbm_unlink() ran with no backup.
+ * On fixed code: at least one is 1.
+ */
+ ASSERT_TRUE(db_still_exists || backup_exists);
+
+ PASS();
+}
+
+/* ── Suite ─────────────────────────────────────────────────────────── */
+SUITE(repro_issue557) {
+ RUN_TEST(repro_issue557_corrupt_db_not_silently_deleted);
+}
diff --git a/tests/repro/repro_main.c b/tests/repro/repro_main.c
index 95564aff4..042f9aedd 100644
--- a/tests/repro/repro_main.c
+++ b/tests/repro/repro_main.c
@@ -41,6 +41,8 @@ extern void suite_repro_issue546(void);
extern void suite_repro_issue627(void);
extern void suite_repro_issue514(void);
extern void suite_repro_issue510(void);
+extern void suite_repro_issue557(void);
+extern void suite_repro_issue520(void);
int main(void) {
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
@@ -69,6 +71,8 @@ int main(void) {
RUN_SUITE(repro_issue627);
RUN_SUITE(repro_issue514);
RUN_SUITE(repro_issue510);
+ RUN_SUITE(repro_issue557);
+ RUN_SUITE(repro_issue520);
TEST_SUMMARY();
}