Skip to content

Commit ad422bc

Browse files
authored
Merge pull request DeusData#717 from DeusData/fix/704-store-resolution
fix(mcp): resolve projects by internal name + filter ghost dbs (DeusData#704)
2 parents c11e659 + 1d1772a commit ad422bc

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
@@ -920,6 +920,22 @@ static const char *project_db_path(const char *project, char *buf, size_t bufsz)
920920

921921
/* ── Store resolution ──────────────────────────────────────────── */
922922

923+
/* Read the sole INTERNAL project name from a .db file at full_path.
924+
* Opens the file query-mode (no create) and succeeds ONLY when the db holds
925+
* exactly one project row with a non-empty name — this filters ghost/empty
926+
* /corrupt dbs (0-byte file, missing `projects` table, or >1 row). On success
927+
* the internal name is copied into name_out; if out_store is non-NULL the open
928+
* handle is transferred to the caller (who must cbm_store_close it). On failure
929+
* the store is always closed. Defined after is_project_db_file below. */
930+
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
931+
cbm_store_t **out_store);
932+
933+
/* #704 fallback: scan the cache dir for the db whose sole internal project name
934+
* equals `project`, returning an open store handle (caller owns it) or NULL.
935+
* Used only when <project>.db is absent or its internal name differs from the
936+
* passed name (drifted filename). Defined after is_project_db_file below. */
937+
static cbm_store_t *resolve_store_fallback_scan(const char *project);
938+
923939
/* Open the right project's .db file for query tools.
924940
* Caches the connection — reopens only when project changes.
925941
* Tracks last-access time so the event loop can evict idle stores. */
@@ -976,12 +992,29 @@ static cbm_store_t *resolve_store(cbm_mcp_server_t *srv, const char *project) {
976992
* Linux where unlink defers actual removal). Opening an empty/deleted
977993
* store without closing it leaks the SQLite connection. */
978994
cbm_project_t proj_verify = {0};
979-
if (cbm_store_get_project(srv->store, project, &proj_verify) != CBM_STORE_OK) {
980-
cbm_store_close(srv->store);
981-
srv->store = NULL;
982-
return NULL;
983-
}
984-
cbm_project_free_fields(&proj_verify);
995+
if (cbm_store_get_project(srv->store, project, &proj_verify) == CBM_STORE_OK) {
996+
cbm_project_free_fields(&proj_verify);
997+
srv->owns_store = true;
998+
free(srv->current_project);
999+
srv->current_project = heap_strdup(project);
1000+
return srv->store; /* fast path: filename == internal name */
1001+
}
1002+
/* #704: <project>.db exists but its INTERNAL project name differs from
1003+
* the passed name (a copied/renamed db, or a legacy '.'-vs-'-' username
1004+
* twin). Close it and fall through to the cache-dir scan below. */
1005+
cbm_store_close(srv->store);
1006+
srv->store = NULL;
1007+
}
1008+
1009+
/* #704 fallback: either <project>.db is absent or its internal name drifted
1010+
* from its filename. Node rows are keyed on the INTERNAL name (== the passed
1011+
* name, since list_projects now advertises internal names), so scan the
1012+
* cache dir for the db whose sole internal project name equals `project` and
1013+
* adopt it. Runs ONLY on the fallback — the common fast path is unchanged.
1014+
* No match → NULL (a genuine typo stays not-found). */
1015+
cbm_store_t *scanned = resolve_store_fallback_scan(project);
1016+
if (scanned) {
1017+
srv->store = scanned;
9851018
srv->owns_store = true;
9861019
free(srv->current_project);
9871020
srv->current_project = heap_strdup(project);
@@ -1012,12 +1045,21 @@ static int collect_db_project_names(const char *dir_path, char *out, size_t out_
10121045
if (!is_project_db_file(n, len)) {
10131046
continue;
10141047
}
1048+
/* #704: advertise the db's INTERNAL project name, not its filename, and
1049+
* skip ghost/empty/corrupt dbs — so the hint lists names the user can
1050+
* actually pass to resolve a store. */
1051+
char full_path[CBM_SZ_2K];
1052+
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, n);
1053+
char iname[CBM_SZ_1K];
1054+
if (!db_internal_project_name(full_path, iname, sizeof(iname), NULL)) {
1055+
continue;
1056+
}
10151057
if ((size_t)offset >= out_sz)
10161058
break; /* bounds check before write */
10171059
if (count > 0 && offset < (int)out_sz - MCP_SEPARATOR) {
10181060
out[offset++] = ',';
10191061
}
1020-
int wrote = snprintf(out + offset, out_sz - (size_t)offset, "\"%.*s\"", (int)(len - 3), n);
1062+
int wrote = snprintf(out + offset, out_sz - (size_t)offset, "\"%s\"", iname);
10211063
if (wrote > 0) {
10221064
offset += wrote;
10231065
if ((size_t)offset >= out_sz) {
@@ -1135,34 +1177,96 @@ static bool is_project_db_file(const char *name, size_t len) {
11351177
return true;
11361178
}
11371179

1180+
/* db_internal_project_name — see forward declaration above resolve_store. */
1181+
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
1182+
cbm_store_t **out_store) {
1183+
if (out_store) {
1184+
*out_store = NULL;
1185+
}
1186+
cbm_store_t *st = cbm_store_open_path_query(full_path);
1187+
if (!st) {
1188+
return false; /* nonexistent / unreadable */
1189+
}
1190+
cbm_project_t *projs = NULL;
1191+
int n = 0;
1192+
bool ok = false;
1193+
if (cbm_store_list_projects(st, &projs, &n) == CBM_STORE_OK && n == 1 && projs[0].name &&
1194+
projs[0].name[0]) {
1195+
snprintf(name_out, name_sz, "%s", projs[0].name);
1196+
ok = true;
1197+
}
1198+
cbm_store_free_projects(projs, n);
1199+
if (ok && out_store) {
1200+
*out_store = st; /* transfer ownership to caller */
1201+
} else {
1202+
cbm_store_close(st);
1203+
}
1204+
return ok;
1205+
}
1206+
1207+
/* resolve_store_fallback_scan — see forward declaration above resolve_store. */
1208+
static cbm_store_t *resolve_store_fallback_scan(const char *project) {
1209+
char dir_path[CBM_SZ_1K];
1210+
cache_dir(dir_path, sizeof(dir_path));
1211+
cbm_dir_t *d = cbm_opendir(dir_path);
1212+
if (!d) {
1213+
return NULL;
1214+
}
1215+
cbm_store_t *found = NULL;
1216+
cbm_dirent_t *entry;
1217+
while ((entry = cbm_readdir(d)) != NULL) {
1218+
const char *n = entry->name;
1219+
size_t len = strlen(n);
1220+
if (!is_project_db_file(n, len)) {
1221+
continue;
1222+
}
1223+
char full_path[CBM_SZ_2K];
1224+
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, n);
1225+
char iname[CBM_SZ_1K];
1226+
cbm_store_t *st = NULL;
1227+
if (db_internal_project_name(full_path, iname, sizeof(iname), &st)) {
1228+
if (strcmp(iname, project) == 0) {
1229+
found = st; /* adopt — caller takes ownership */
1230+
break;
1231+
}
1232+
cbm_store_close(st);
1233+
}
1234+
}
1235+
cbm_closedir(d);
1236+
return found;
1237+
}
1238+
11381239
/* Open a .db file briefly, collect node/edge counts and root_path,
11391240
* then append a JSON entry to arr. */
11401241
static void build_project_json_entry(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *dir_path,
11411242
const char *name, size_t name_len, int64_t size_bytes) {
1142-
char project_name[CBM_SZ_1K];
1143-
snprintf(project_name, sizeof(project_name), "%.*s", (int)(name_len - 3), name);
1243+
(void)name_len;
11441244

11451245
char full_path[CBM_SZ_2K];
11461246
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, name);
11471247

1148-
cbm_store_t *pstore = cbm_store_open_path(full_path);
1149-
int nodes = 0;
1150-
int edges = 0;
1248+
/* #704: key on the db's INTERNAL project name, not its filename. Node/edge
1249+
* rows are tagged with the internal name, so a drifted filename (copied or
1250+
* renamed db, legacy '.'-vs-'-' username twin) would otherwise report 0
1251+
* nodes/edges and be unresolvable. Skip ghost/empty/corrupt dbs entirely so
1252+
* they don't appear as resolvable projects. */
1253+
char project_name[CBM_SZ_1K];
1254+
cbm_store_t *pstore = NULL;
1255+
if (!db_internal_project_name(full_path, project_name, sizeof(project_name), &pstore)) {
1256+
return; /* ghost / unreadable — not a resolvable project */
1257+
}
1258+
1259+
int nodes = cbm_store_count_nodes(pstore, project_name);
1260+
int edges = cbm_store_count_edges(pstore, project_name);
11511261
char root_path_buf[CBM_SZ_1K] = "";
1152-
if (pstore) {
1153-
nodes = cbm_store_count_nodes(pstore, project_name);
1154-
edges = cbm_store_count_edges(pstore, project_name);
1155-
cbm_project_t proj = {0};
1156-
if (cbm_store_get_project(pstore, project_name, &proj) == CBM_STORE_OK) {
1157-
if (proj.root_path) {
1158-
snprintf(root_path_buf, sizeof(root_path_buf), "%s", proj.root_path);
1159-
}
1160-
safe_str_free(&proj.name);
1161-
safe_str_free(&proj.indexed_at);
1162-
safe_str_free(&proj.root_path);
1262+
cbm_project_t proj = {0};
1263+
if (cbm_store_get_project(pstore, project_name, &proj) == CBM_STORE_OK) {
1264+
if (proj.root_path) {
1265+
snprintf(root_path_buf, sizeof(root_path_buf), "%s", proj.root_path);
11631266
}
1164-
cbm_store_close(pstore);
1267+
cbm_project_free_fields(&proj);
11651268
}
1269+
cbm_store_close(pstore);
11661270

11671271
yyjson_mut_val *p = yyjson_mut_obj(doc);
11681272
yyjson_mut_obj_add_strcpy(doc, p, "name", project_name);

0 commit comments

Comments
 (0)