Skip to content

Commit 807a4a3

Browse files
authored
Merge pull request DeusData#1046 from metehanulusoy/fix/missed-shadow-project-listing
fix(mcp): ignore ::missed shadow rows when resolving a db's project name
2 parents 34867d9 + 3b1ea58 commit 807a4a3

2 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,10 +1793,23 @@ static bool db_internal_project_name(const char *full_path, char *name_out, size
17931793
cbm_project_t *projs = NULL;
17941794
int n = 0;
17951795
bool ok = false;
1796-
if (cbm_store_list_projects(st, &projs, &n) == CBM_STORE_OK && n == 1 && projs[0].name &&
1797-
projs[0].name[0]) {
1798-
snprintf(name_out, name_sz, "%s", projs[0].name);
1799-
ok = true;
1796+
if (cbm_store_list_projects(st, &projs, &n) == CBM_STORE_OK) {
1797+
/* Ignore internal shadow projects ("<name>::missed" miss-graph rows):
1798+
* they share the db with the primary project and must not make it
1799+
* unresolvable — requiring n == 1 over ALL rows made every project
1800+
* with a miss graph vanish from list_projects and the UI (#1044). */
1801+
int primary = -1;
1802+
int primary_count = 0;
1803+
for (int i = 0; i < n; i++) {
1804+
if (projs[i].name && projs[i].name[0] && !strstr(projs[i].name, "::")) {
1805+
primary = i;
1806+
primary_count++;
1807+
}
1808+
}
1809+
if (primary_count == 1) {
1810+
snprintf(name_out, name_sz, "%s", projs[primary].name);
1811+
ok = true;
1812+
}
18001813
}
18011814
cbm_store_free_projects(projs, n);
18021815
if (ok && out_store) {

‎tests/test_mcp.c‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4780,6 +4780,81 @@ TEST(tool_resolve_store_by_internal_name_issue704) {
47804780
PASS();
47814781
}
47824782

4783+
/* ── #1044: a "<name>::missed" shadow row must not hide the project ──
4784+
*
4785+
* The miss-graph pass inserts a second `projects` row ("<name>::missed") so
4786+
* its nodes satisfy the FK on nodes.project. db_internal_project_name
4787+
* required the projects table to hold EXACTLY ONE row, so any project with
4788+
* a miss graph vanished from list_projects and the graph UI, and the
4789+
* fallback-scan resolve path failed.
4790+
*
4791+
* RED on buggy code / GREEN on the fix:
4792+
* A. list_projects still advertises "delta1044" while the shadow row exists.
4793+
* B. the shadow name itself is never advertised.
4794+
* C. search_graph(project="delta1044") still resolves and returns the node.
4795+
*/
4796+
TEST(tool_list_projects_ignores_missed_shadow_issue1044) {
4797+
char cache[256];
4798+
snprintf(cache, sizeof(cache), "/tmp/cbm-issue1044-XXXXXX");
4799+
if (!cbm_mkdtemp(cache)) {
4800+
PASS(); /* skip if mkdtemp fails — not a #1044 signal */
4801+
}
4802+
4803+
const char *saved = getenv("CBM_CACHE_DIR");
4804+
char *saved_copy = saved ? strdup(saved) : NULL;
4805+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
4806+
4807+
ASSERT_TRUE(issue704_make_db(cache, "delta1044.db", "delta1044", "deltaFunc1044"));
4808+
4809+
/* Add the shadow row exactly the way the miss-graph pass does. */
4810+
char db_path[700];
4811+
snprintf(db_path, sizeof(db_path), "%s/delta1044.db", cache);
4812+
cbm_store_t *st = cbm_store_open_path(db_path);
4813+
ASSERT_NOT_NULL(st);
4814+
ASSERT_EQ(cbm_store_upsert_project(st, "delta1044::missed", ""), CBM_STORE_OK);
4815+
cbm_store_close(st);
4816+
4817+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4818+
ASSERT_NOT_NULL(srv);
4819+
4820+
/* ── A + B: primary advertised, shadow hidden ─────────────────── */
4821+
char *list =
4822+
cbm_mcp_server_handle(srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","
4823+
"\"params\":{\"name\":\"list_projects\",\"arguments\":{}}}");
4824+
ASSERT_NOT_NULL(list);
4825+
ASSERT_NOT_NULL(strstr(list, "delta1044")); /* RED before: db skipped as ghost */
4826+
ASSERT_NULL(strstr(list, "::missed")); /* shadow never advertised */
4827+
free(list);
4828+
4829+
/* ── C: the project still resolves and returns its node ───────── */
4830+
char *q = cbm_mcp_server_handle(
4831+
srv, "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
4832+
"\"params\":{\"name\":\"search_graph\",\"arguments\":{"
4833+
"\"project\":\"delta1044\",\"name_pattern\":\"deltaFunc1044\",\"limit\":5}}}");
4834+
ASSERT_NOT_NULL(q);
4835+
ASSERT_NOT_NULL(strstr(q, "deltaFunc1044"));
4836+
ASSERT_NULL(strstr(q, "not found"));
4837+
free(q);
4838+
4839+
cbm_mcp_server_free(srv);
4840+
4841+
/* ── cleanup ───────────────────────────────────────────────────── */
4842+
if (saved_copy) {
4843+
cbm_setenv("CBM_CACHE_DIR", saved_copy, 1);
4844+
free(saved_copy);
4845+
} else {
4846+
cbm_unsetenv("CBM_CACHE_DIR");
4847+
}
4848+
cbm_unlink(db_path);
4849+
char side1044[740];
4850+
snprintf(side1044, sizeof(side1044), "%s-wal", db_path);
4851+
cbm_unlink(side1044);
4852+
snprintf(side1044, sizeof(side1044), "%s-shm", db_path);
4853+
cbm_unlink(side1044);
4854+
cbm_rmdir(cache);
4855+
PASS();
4856+
}
4857+
47834858
/* ══════════════════════════════════════════════════════════════════
47844859
* QUERY STORE READ-ONLY (data-integrity reproductions)
47854860
*
@@ -6439,6 +6514,7 @@ SUITE(mcp) {
64396514
RUN_TEST(tool_bad_project_name_no_overflow_issue235);
64406515
RUN_TEST(tool_bad_project_error_valid_json_issue235);
64416516
RUN_TEST(tool_resolve_store_by_internal_name_issue704);
6517+
RUN_TEST(tool_list_projects_ignores_missed_shadow_issue1044);
64426518

64436519
/* auto_watch gate (distilled from PR #625) */
64446520
RUN_TEST(mcp_auto_watch_default_registers_watcher_on_connect);

0 commit comments

Comments
 (0)