Skip to content

Commit 273a3af

Browse files
author
SS-42
committed
fix(mcp): normalize project paths for ADR lookups
Signed-off-by: SS-42 <noreply@incogni.to>
1 parent dcf98dc commit 273a3af

3 files changed

Lines changed: 562 additions & 16 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ enum {
6060
#include "pipeline/artifact.h"
6161

6262
#ifdef _WIN32
63+
#include <direct.h>
64+
#include <io.h>
6365
#include <process.h>
6466
#define getpid _getpid
6567
#else
@@ -720,6 +722,59 @@ char *cbm_mcp_get_string_arg(const char *args_json, const char *key) {
720722
return result;
721723
}
722724

725+
static char *canonicalize_repo_path_if_exists(char *repo_path) {
726+
if (!repo_path) {
727+
return NULL;
728+
}
729+
bool root_syntax = true;
730+
for (const char *p = repo_path; *p; p++) {
731+
if (*p != '/' && *p != '\\' && *p != ':') {
732+
root_syntax = false;
733+
break;
734+
}
735+
}
736+
if (root_syntax) {
737+
return repo_path;
738+
}
739+
740+
char real[CBM_SZ_4K];
741+
#ifdef _WIN32
742+
if (_access(repo_path, 0) == 0 && _fullpath(real, repo_path, sizeof(real))) {
743+
cbm_normalize_path_sep(real);
744+
char *canonical = heap_strdup(real);
745+
if (canonical) {
746+
free(repo_path);
747+
return canonical;
748+
}
749+
}
750+
#else
751+
if (realpath(repo_path, real)) {
752+
cbm_normalize_path_sep(real);
753+
char *canonical = heap_strdup(real);
754+
if (canonical) {
755+
free(repo_path);
756+
return canonical;
757+
}
758+
}
759+
#endif
760+
761+
return repo_path;
762+
}
763+
764+
static char *normalize_project_arg(char *project) {
765+
if (!project || (!strchr(project, '/') && !strchr(project, '\\'))) {
766+
return project;
767+
}
768+
769+
project = canonicalize_repo_path_if_exists(project);
770+
char *normalized = cbm_project_name_from_path(project);
771+
if (normalized) {
772+
free(project);
773+
return normalized;
774+
}
775+
return project;
776+
}
777+
723778
/* Resolve the project argument, accepting the canonical "project" key plus the
724779
* aliases a caller naturally reaches for (#640): list_projects surfaces the
725780
* field as "name" and the not-found hint says "pass the project name", so
@@ -737,7 +792,7 @@ static char *get_project_arg(const char *args_json) {
737792
if (!p) {
738793
p = cbm_mcp_get_string_arg(args_json, "projectName");
739794
}
740-
return p;
795+
return normalize_project_arg(p);
741796
}
742797

743798
int cbm_mcp_get_int_arg(const char *args_json, const char *key, int default_val) {
@@ -1163,6 +1218,26 @@ static char *build_no_store_error(const char *project) {
11631218
} \
11641219
} while (0)
11651220

1221+
static bool project_has_adr(cbm_store_t *store, const char *project, const char *root_path) {
1222+
if (store && project) {
1223+
cbm_adr_t adr;
1224+
memset(&adr, 0, sizeof(adr));
1225+
if (cbm_store_adr_get(store, project, &adr) == CBM_STORE_OK) {
1226+
cbm_store_adr_free(&adr);
1227+
return true;
1228+
}
1229+
}
1230+
1231+
if (!root_path) {
1232+
return false;
1233+
}
1234+
1235+
char adr_path[CBM_SZ_4K];
1236+
snprintf(adr_path, sizeof(adr_path), "%s/.codebase-memory/adr.md", root_path);
1237+
struct stat adr_st;
1238+
return stat(adr_path, &adr_st) == 0;
1239+
}
1240+
11661241
/* ── Tool handler implementations ─────────────────────────────── */
11671242

11681243
/* Return true if filename is a valid project .db file (not temp/internal).
@@ -1411,10 +1486,7 @@ static char *handle_get_graph_schema(cbm_mcp_server_t *srv, const char *args) {
14111486
/* Check ADR presence */
14121487
cbm_project_t proj_info = {0};
14131488
if (cbm_store_get_project(store, project, &proj_info) == 0 && proj_info.root_path) {
1414-
char adr_path[CBM_SZ_4K];
1415-
snprintf(adr_path, sizeof(adr_path), "%s/.codebase-memory/adr.md", proj_info.root_path);
1416-
struct stat adr_st;
1417-
bool adr_exists = (stat(adr_path, &adr_st) == 0);
1489+
bool adr_exists = project_has_adr(store, project, proj_info.root_path);
14181490
yyjson_mut_obj_add_bool(doc, root, "adr_present", adr_exists);
14191491
if (!adr_exists) {
14201492
yyjson_mut_obj_add_str(
@@ -3204,17 +3276,14 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
32043276
}
32053277
}
32063278

3207-
char adr_path[CBM_SZ_4K];
3208-
snprintf(adr_path, sizeof(adr_path), "%s/.codebase-memory/adr.md", repo_path);
3209-
struct stat adr_st;
3210-
bool adr_exists = (stat(adr_path, &adr_st) == 0);
3279+
bool adr_exists = project_has_adr(store, project_name, repo_path);
32113280
yyjson_mut_obj_add_bool(doc, root, "adr_present", adr_exists);
32123281
if (!adr_exists && !degraded) {
32133282
yyjson_mut_obj_add_str(
32143283
doc, root, "adr_hint",
32153284
"Project indexed. Consider creating an Architecture Decision Record: "
32163285
"explore the codebase with get_architecture(aspects=['all']), then use "
3217-
"manage_adr(mode='store') to persist architectural insights across sessions.");
3286+
"manage_adr(mode='update') to persist architectural insights across sessions.");
32183287
}
32193288

32203289
bool has_artifact = cbm_artifact_exists(repo_path);
@@ -3240,6 +3309,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
32403309
return cbm_mcp_text_result("repo_path is required", true);
32413310
}
32423311

3312+
repo_path = canonicalize_repo_path_if_exists(repo_path);
3313+
32433314
if (mode_str && strcmp(mode_str, "cross-repo-intelligence") == 0) {
32443315
free(mode_str);
32453316
free(name_override);
@@ -4678,10 +4749,13 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
46784749

46794750
char *root_path = get_project_root(srv, project);
46804751
if (!root_path) {
4752+
char *err = build_no_store_error(project);
4753+
char *res = cbm_mcp_text_result(err, true);
4754+
free(err);
46814755
free(project);
46824756
free(base_branch);
46834757
free(scope);
4684-
return cbm_mcp_text_result("project not found", true);
4758+
return res;
46854759
}
46864760

46874761
if (!validate_search_path_arg(root_path)) {
@@ -4896,10 +4970,13 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
48964970
* the UI are visible to each other (#256). */
48974971
cbm_store_t *resolved = resolve_store(srv, project);
48984972
if (!resolved) {
4973+
char *err = build_no_store_error(project);
4974+
char *res = cbm_mcp_text_result(err, true);
4975+
free(err);
48994976
free(project);
49004977
free(mode_str);
49014978
free(content);
4902-
return cbm_mcp_text_result("project not found", true);
4979+
return res;
49034980
}
49044981

49054982
/* resolve_store opens file-backed projects READ-ONLY (query stores must
@@ -4915,10 +4992,13 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
49154992
if (resolved_db_path) {
49164993
owned_rw = cbm_store_open_path(resolved_db_path);
49174994
if (!owned_rw) {
4995+
char *err = build_no_store_error(project);
4996+
char *res = cbm_mcp_text_result(err, true);
4997+
free(err);
49184998
free(project);
49194999
free(mode_str);
49205000
free(content);
4921-
return cbm_mcp_text_result("project not found", true);
5001+
return res;
49225002
}
49235003
store = owned_rw;
49245004
}

‎src/pipeline/fqn.c‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#include <stdio.h>
1515
#include <stdlib.h>
1616
#include <string.h> // strdup
17+
#ifdef _WIN32
18+
#include <direct.h>
19+
#include <io.h>
20+
#endif
1721

1822
/* Maximum path segments in a FQN (CBM_SZ_256 slots total, -2 for project + name) */
1923
#define FQN_MAX_PATH_SEGS 254
@@ -384,13 +388,45 @@ static char *fqn_bound_name_len(char *name) {
384388
return name;
385389
}
386390

391+
static bool path_is_root_syntax(const char *path) {
392+
if (!path || !path[0]) {
393+
return false;
394+
}
395+
for (const char *p = path; *p; p++) {
396+
if (*p != '/' && *p != '\\' && *p != ':') {
397+
return false;
398+
}
399+
}
400+
return true;
401+
}
402+
387403
char *cbm_project_name_from_path(const char *abs_path) {
388404
if (!abs_path || !abs_path[0]) {
389405
return strdup("root");
390406
}
407+
if (path_is_root_syntax(abs_path)) {
408+
return strdup("root");
409+
}
410+
411+
char real[CBM_SZ_4K];
412+
const char *name_path = abs_path;
413+
#ifdef _WIN32
414+
if (_access(abs_path, 0) == 0 && _fullpath(real, abs_path, sizeof(real))) {
415+
cbm_normalize_path_sep(real);
416+
name_path = real;
417+
}
418+
#else
419+
if (realpath(abs_path, real)) {
420+
cbm_normalize_path_sep(real);
421+
name_path = real;
422+
}
423+
#endif
391424

392425
/* Work on mutable copy */
393-
char *path = strdup(abs_path);
426+
char *path = strdup(name_path);
427+
if (!path) {
428+
return NULL;
429+
}
394430
size_t len = strlen(path);
395431

396432
/* Normalize path separators */

0 commit comments

Comments
 (0)