Skip to content

Commit 97fcae0

Browse files
committed
test(mcp): regression guard for relative repo_path canonicalization
On v0.8.1, index_repository with repo_path "." flowed the literal dot through everything derived from the path: the project was named "root", the projects table stored root_path="." verbatim, the post-dump integrity check flagged bad_root_path and auto-deleted the entire DB — while the response still reported status "indexed" with full node counts. Verified fixed on current main (the path is canonicalized before derivation: correct project name, absolute root_path, persistent and queryable DB). The guard indexes with repo_path "." from inside the repo and asserts the three things the old failure mode broke: path-derived project name (never "root"), absolute canonical root_path in the projects table, and a DB that survives the run and answers queries. Closes DeusData#794 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 23fd0e8 commit 97fcae0

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

‎tests/test_index_resilience.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,107 @@ TEST(index_not_indexed_by_design_reported) {
651651
PASS();
652652
}
653653

654+
/* INV(relative-repo-path, #794): index_repository with repo_path "." must
655+
* canonicalize to the absolute path BEFORE anything derives from it. On
656+
* v0.8.1 the literal "." flowed through: the project was named "root", the
657+
* projects table stored root_path="." verbatim, the post-dump integrity
658+
* check flagged bad_root_path and AUTO-DELETED the whole DB — while the
659+
* response still claimed status "indexed". The guard asserts the three
660+
* things that failure mode breaks: a path-derived project name (not
661+
* "root"), an absolute stored root_path, and a DB that persists and stays
662+
* queryable after the run. */
663+
TEST(index_relative_repo_path_canonicalized) {
664+
RProj lp;
665+
memset(&lp, 0, sizeof(lp));
666+
snprintf(lp.tmpdir, sizeof(lp.tmpdir), "/tmp/cbm_resil_XXXXXX");
667+
if (!cbm_mkdtemp(lp.tmpdir)) {
668+
FAIL("mkdtemp failed");
669+
}
670+
rh_to_fwd_slashes(lp.tmpdir);
671+
ri_write_text(lp.tmpdir, "good.py", "def alpha():\n return 1\n");
672+
673+
/* Resolve the canonical form of tmpdir (macOS: /tmp -> /private/tmp) so
674+
* the derived-name and stored-root assertions compare like with like. */
675+
char canon[700];
676+
if (!realpath(lp.tmpdir, canon)) {
677+
snprintf(canon, sizeof(canon), "%s", lp.tmpdir);
678+
}
679+
lp.project = cbm_project_name_from_path(canon);
680+
if (!lp.project) {
681+
FAIL("project name derivation failed");
682+
}
683+
684+
const char *home = getenv("HOME");
685+
if (!home) {
686+
home = "/tmp";
687+
}
688+
char cache_dir[512];
689+
snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home);
690+
cbm_mkdir(cache_dir);
691+
snprintf(lp.dbpath, sizeof(lp.dbpath), "%s/%s.db", cache_dir, lp.project);
692+
unlink(lp.dbpath);
693+
694+
lp.srv = cbm_mcp_server_new(NULL);
695+
if (!lp.srv) {
696+
FAIL("server alloc failed");
697+
}
698+
699+
/* chdir into the repo and index "." — restore cwd immediately after. */
700+
char oldcwd[700];
701+
if (!getcwd(oldcwd, sizeof(oldcwd))) {
702+
FAIL("getcwd failed");
703+
}
704+
if (chdir(lp.tmpdir) != 0) {
705+
FAIL("chdir failed");
706+
}
707+
char *resp = cbm_mcp_handle_tool(lp.srv, "index_repository", "{\"repo_path\":\".\"}");
708+
int rc_back = chdir(oldcwd);
709+
if (rc_back != 0) {
710+
free(resp);
711+
FAIL("chdir back failed");
712+
}
713+
ASSERT_NOT_NULL(resp);
714+
715+
/* The response names the path-derived project, never the literal-dot
716+
* artifact "root". */
717+
ASSERT_NULL(strstr(resp, "\"project\":\"root\""));
718+
ASSERT_NOT_NULL(strstr(resp, lp.project));
719+
ASSERT_NOT_NULL(strstr(resp, "\"status\":\"indexed\""));
720+
free(resp);
721+
722+
/* The DB survived the run (v0.8.1 auto-deleted it here)... */
723+
cbm_store_t *store = cbm_store_open_path(lp.dbpath);
724+
if (!store) {
725+
FAIL("db missing after relative-path index (auto-clean regression)");
726+
}
727+
728+
/* ...with an ABSOLUTE canonical root_path in the projects table... */
729+
cbm_project_t info = {0};
730+
ASSERT_EQ(cbm_store_get_project(store, lp.project, &info), CBM_STORE_OK);
731+
ASSERT_NOT_NULL(info.root_path);
732+
ASSERT_TRUE(info.root_path[0] == '/');
733+
ASSERT_STR_EQ(info.root_path, canon);
734+
free((char *)info.name);
735+
free((char *)info.indexed_at);
736+
free((char *)info.root_path);
737+
738+
/* ...and it stays queryable. */
739+
cbm_node_t *hits = NULL;
740+
int hit_count = 0;
741+
ASSERT_EQ(cbm_store_find_nodes_by_name(store, lp.project, "alpha", &hits, &hit_count),
742+
CBM_STORE_OK);
743+
ASSERT_GTE(hit_count, 1);
744+
cbm_store_free_nodes(hits, hit_count);
745+
746+
rh_cleanup(&lp, store);
747+
PASS();
748+
}
749+
654750
SUITE(index_resilience) {
655751
RUN_TEST(index_oversized_file_reported);
656752
RUN_TEST(index_clean_run_no_logfile);
657753
RUN_TEST(index_parse_partial_reported);
658754
RUN_TEST(index_parse_partial_clears_on_fix);
659755
RUN_TEST(index_not_indexed_by_design_reported);
756+
RUN_TEST(index_relative_repo_path_canonicalized);
660757
}

0 commit comments

Comments
 (0)