Skip to content

Commit 1d1772a

Browse files
committed
fix(mcp): resolve projects by internal name + filter ghost dbs (DeusData#704)
Project resolution was filename-addressed but internal-name-validated: resolve_store opened <cache>/<passed>.db and required the internal projects.name row to equal the passed name, while list_projects advertised the .db FILENAME. When a db's filename differed from its internal name (a copied/renamed db, or a legacy '.'-vs-'-' username twin from a past path sanitization), it showed in list_projects but every query returned "project not found or not indexed". Node rows are keyed on the internal name, so just opening the file is not enough -- queries must use the internal name too. The intermittency ("works now, fails 10 min later") was a warm in-process store cache masking a broken cold resolve; a fresh cli call is always cold. Key list + resolve on the INTERNAL project name, with no file renames or mutation: - list_projects / collect_db_project_names / build_project_json_entry now advertise each db's internal projects.name (via db_internal_project_name) and skip ghost/empty/corrupt dbs (0-byte, no project row) so the listed names are exactly the ones that resolve. - resolve_store keeps the fast path (open <passed>.db, get_project -- the common case where filename == internal name). On a miss it runs a bounded cache-dir scan, adopting the db whose sole internal project name equals the passed name (gated on exactly one project row so it never serves another project's data). No match -> NULL (a typo stays not-found). The scan runs only on the fallback. Reproduce-first: tool_resolve_store_by_internal_name_issue704 builds an isolated CBM_CACHE_DIR with a control db, a drifted db (filename != internal name), and a 0-byte ghost; asserts list advertises the internal name (not the filename), the drifted project resolves via the scan, and the ghost is neither listed nor resolvable. RED (113/1, list lacked the internal name) -> GREEN (114/0). Full suite 5723/0. Also hardened tool_bad_project_name_no_overflow_issue235: its fixture used 1-byte non-SQLite files that the new ghost filter would skip, making the buffer-overflow guard vacuous; rebuilt it with valid dbs carrying long internal names so the guard still fires. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent f957dff commit 1d1772a

2 files changed

Lines changed: 316 additions & 29 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 128 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,22 @@ static const char *project_db_path(const char *project, char *buf, size_t bufsz)
885885

886886
/* ── Store resolution ──────────────────────────────────────────── */
887887

888+
/* Read the sole INTERNAL project name from a .db file at full_path.
889+
* Opens the file query-mode (no create) and succeeds ONLY when the db holds
890+
* exactly one project row with a non-empty name — this filters ghost/empty
891+
* /corrupt dbs (0-byte file, missing `projects` table, or >1 row). On success
892+
* the internal name is copied into name_out; if out_store is non-NULL the open
893+
* handle is transferred to the caller (who must cbm_store_close it). On failure
894+
* the store is always closed. Defined after is_project_db_file below. */
895+
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
896+
cbm_store_t **out_store);
897+
898+
/* #704 fallback: scan the cache dir for the db whose sole internal project name
899+
* equals `project`, returning an open store handle (caller owns it) or NULL.
900+
* Used only when <project>.db is absent or its internal name differs from the
901+
* passed name (drifted filename). Defined after is_project_db_file below. */
902+
static cbm_store_t *resolve_store_fallback_scan(const char *project);
903+
888904
/* Open the right project's .db file for query tools.
889905
* Caches the connection — reopens only when project changes.
890906
* Tracks last-access time so the event loop can evict idle stores. */
@@ -941,12 +957,29 @@ static cbm_store_t *resolve_store(cbm_mcp_server_t *srv, const char *project) {
941957
* Linux where unlink defers actual removal). Opening an empty/deleted
942958
* store without closing it leaks the SQLite connection. */
943959
cbm_project_t proj_verify = {0};
944-
if (cbm_store_get_project(srv->store, project, &proj_verify) != CBM_STORE_OK) {
945-
cbm_store_close(srv->store);
946-
srv->store = NULL;
947-
return NULL;
948-
}
949-
cbm_project_free_fields(&proj_verify);
960+
if (cbm_store_get_project(srv->store, project, &proj_verify) == CBM_STORE_OK) {
961+
cbm_project_free_fields(&proj_verify);
962+
srv->owns_store = true;
963+
free(srv->current_project);
964+
srv->current_project = heap_strdup(project);
965+
return srv->store; /* fast path: filename == internal name */
966+
}
967+
/* #704: <project>.db exists but its INTERNAL project name differs from
968+
* the passed name (a copied/renamed db, or a legacy '.'-vs-'-' username
969+
* twin). Close it and fall through to the cache-dir scan below. */
970+
cbm_store_close(srv->store);
971+
srv->store = NULL;
972+
}
973+
974+
/* #704 fallback: either <project>.db is absent or its internal name drifted
975+
* from its filename. Node rows are keyed on the INTERNAL name (== the passed
976+
* name, since list_projects now advertises internal names), so scan the
977+
* cache dir for the db whose sole internal project name equals `project` and
978+
* adopt it. Runs ONLY on the fallback — the common fast path is unchanged.
979+
* No match → NULL (a genuine typo stays not-found). */
980+
cbm_store_t *scanned = resolve_store_fallback_scan(project);
981+
if (scanned) {
982+
srv->store = scanned;
950983
srv->owns_store = true;
951984
free(srv->current_project);
952985
srv->current_project = heap_strdup(project);
@@ -977,12 +1010,21 @@ static int collect_db_project_names(const char *dir_path, char *out, size_t out_
9771010
if (!is_project_db_file(n, len)) {
9781011
continue;
9791012
}
1013+
/* #704: advertise the db's INTERNAL project name, not its filename, and
1014+
* skip ghost/empty/corrupt dbs — so the hint lists names the user can
1015+
* actually pass to resolve a store. */
1016+
char full_path[CBM_SZ_2K];
1017+
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, n);
1018+
char iname[CBM_SZ_1K];
1019+
if (!db_internal_project_name(full_path, iname, sizeof(iname), NULL)) {
1020+
continue;
1021+
}
9801022
if ((size_t)offset >= out_sz)
9811023
break; /* bounds check before write */
9821024
if (count > 0 && offset < (int)out_sz - MCP_SEPARATOR) {
9831025
out[offset++] = ',';
9841026
}
985-
int wrote = snprintf(out + offset, out_sz - (size_t)offset, "\"%.*s\"", (int)(len - 3), n);
1027+
int wrote = snprintf(out + offset, out_sz - (size_t)offset, "\"%s\"", iname);
9861028
if (wrote > 0) {
9871029
offset += wrote;
9881030
if ((size_t)offset >= out_sz) {
@@ -1081,34 +1123,96 @@ static bool is_project_db_file(const char *name, size_t len) {
10811123
return true;
10821124
}
10831125

1126+
/* db_internal_project_name — see forward declaration above resolve_store. */
1127+
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
1128+
cbm_store_t **out_store) {
1129+
if (out_store) {
1130+
*out_store = NULL;
1131+
}
1132+
cbm_store_t *st = cbm_store_open_path_query(full_path);
1133+
if (!st) {
1134+
return false; /* nonexistent / unreadable */
1135+
}
1136+
cbm_project_t *projs = NULL;
1137+
int n = 0;
1138+
bool ok = false;
1139+
if (cbm_store_list_projects(st, &projs, &n) == CBM_STORE_OK && n == 1 && projs[0].name &&
1140+
projs[0].name[0]) {
1141+
snprintf(name_out, name_sz, "%s", projs[0].name);
1142+
ok = true;
1143+
}
1144+
cbm_store_free_projects(projs, n);
1145+
if (ok && out_store) {
1146+
*out_store = st; /* transfer ownership to caller */
1147+
} else {
1148+
cbm_store_close(st);
1149+
}
1150+
return ok;
1151+
}
1152+
1153+
/* resolve_store_fallback_scan — see forward declaration above resolve_store. */
1154+
static cbm_store_t *resolve_store_fallback_scan(const char *project) {
1155+
char dir_path[CBM_SZ_1K];
1156+
cache_dir(dir_path, sizeof(dir_path));
1157+
cbm_dir_t *d = cbm_opendir(dir_path);
1158+
if (!d) {
1159+
return NULL;
1160+
}
1161+
cbm_store_t *found = NULL;
1162+
cbm_dirent_t *entry;
1163+
while ((entry = cbm_readdir(d)) != NULL) {
1164+
const char *n = entry->name;
1165+
size_t len = strlen(n);
1166+
if (!is_project_db_file(n, len)) {
1167+
continue;
1168+
}
1169+
char full_path[CBM_SZ_2K];
1170+
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, n);
1171+
char iname[CBM_SZ_1K];
1172+
cbm_store_t *st = NULL;
1173+
if (db_internal_project_name(full_path, iname, sizeof(iname), &st)) {
1174+
if (strcmp(iname, project) == 0) {
1175+
found = st; /* adopt — caller takes ownership */
1176+
break;
1177+
}
1178+
cbm_store_close(st);
1179+
}
1180+
}
1181+
cbm_closedir(d);
1182+
return found;
1183+
}
1184+
10841185
/* Open a .db file briefly, collect node/edge counts and root_path,
10851186
* then append a JSON entry to arr. */
10861187
static void build_project_json_entry(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *dir_path,
10871188
const char *name, size_t name_len, int64_t size_bytes) {
1088-
char project_name[CBM_SZ_1K];
1089-
snprintf(project_name, sizeof(project_name), "%.*s", (int)(name_len - 3), name);
1189+
(void)name_len;
10901190

10911191
char full_path[CBM_SZ_2K];
10921192
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, name);
10931193

1094-
cbm_store_t *pstore = cbm_store_open_path(full_path);
1095-
int nodes = 0;
1096-
int edges = 0;
1194+
/* #704: key on the db's INTERNAL project name, not its filename. Node/edge
1195+
* rows are tagged with the internal name, so a drifted filename (copied or
1196+
* renamed db, legacy '.'-vs-'-' username twin) would otherwise report 0
1197+
* nodes/edges and be unresolvable. Skip ghost/empty/corrupt dbs entirely so
1198+
* they don't appear as resolvable projects. */
1199+
char project_name[CBM_SZ_1K];
1200+
cbm_store_t *pstore = NULL;
1201+
if (!db_internal_project_name(full_path, project_name, sizeof(project_name), &pstore)) {
1202+
return; /* ghost / unreadable — not a resolvable project */
1203+
}
1204+
1205+
int nodes = cbm_store_count_nodes(pstore, project_name);
1206+
int edges = cbm_store_count_edges(pstore, project_name);
10971207
char root_path_buf[CBM_SZ_1K] = "";
1098-
if (pstore) {
1099-
nodes = cbm_store_count_nodes(pstore, project_name);
1100-
edges = cbm_store_count_edges(pstore, project_name);
1101-
cbm_project_t proj = {0};
1102-
if (cbm_store_get_project(pstore, project_name, &proj) == CBM_STORE_OK) {
1103-
if (proj.root_path) {
1104-
snprintf(root_path_buf, sizeof(root_path_buf), "%s", proj.root_path);
1105-
}
1106-
safe_str_free(&proj.name);
1107-
safe_str_free(&proj.indexed_at);
1108-
safe_str_free(&proj.root_path);
1208+
cbm_project_t proj = {0};
1209+
if (cbm_store_get_project(pstore, project_name, &proj) == CBM_STORE_OK) {
1210+
if (proj.root_path) {
1211+
snprintf(root_path_buf, sizeof(root_path_buf), "%s", proj.root_path);
11091212
}
1110-
cbm_store_close(pstore);
1213+
cbm_project_free_fields(&proj);
11111214
}
1215+
cbm_store_close(pstore);
11121216

11131217
yyjson_mut_val *p = yyjson_mut_obj(doc);
11141218
yyjson_mut_obj_add_strcpy(doc, p, "name", project_name);

0 commit comments

Comments
 (0)