Skip to content

Commit 09148ab

Browse files
authored
Merge pull request DeusData#780 from SS-42/fix/adr-path-normalization
Normalize project paths for ADR lookups
2 parents cde1675 + 273a3af commit 09148ab

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
@@ -721,6 +723,59 @@ char *cbm_mcp_get_string_arg(const char *args_json, const char *key) {
721723
return result;
722724
}
723725

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

744799
int cbm_mcp_get_int_arg(const char *args_json, const char *key, int default_val) {
@@ -1164,6 +1219,26 @@ static char *build_no_store_error(const char *project) {
11641219
} \
11651220
} while (0)
11661221

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

11691244
/* Return true if filename is a valid project .db file (not temp/internal).
@@ -1412,10 +1487,7 @@ static char *handle_get_graph_schema(cbm_mcp_server_t *srv, const char *args) {
14121487
/* Check ADR presence */
14131488
cbm_project_t proj_info = {0};
14141489
if (cbm_store_get_project(store, project, &proj_info) == 0 && proj_info.root_path) {
1415-
char adr_path[CBM_SZ_4K];
1416-
snprintf(adr_path, sizeof(adr_path), "%s/.codebase-memory/adr.md", proj_info.root_path);
1417-
struct stat adr_st;
1418-
bool adr_exists = (stat(adr_path, &adr_st) == 0);
1490+
bool adr_exists = project_has_adr(store, project, proj_info.root_path);
14191491
yyjson_mut_obj_add_bool(doc, root, "adr_present", adr_exists);
14201492
if (!adr_exists) {
14211493
yyjson_mut_obj_add_str(
@@ -3293,17 +3365,14 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
32933365
}
32943366
}
32953367

3296-
char adr_path[CBM_SZ_4K];
3297-
snprintf(adr_path, sizeof(adr_path), "%s/.codebase-memory/adr.md", repo_path);
3298-
struct stat adr_st;
3299-
bool adr_exists = (stat(adr_path, &adr_st) == 0);
3368+
bool adr_exists = project_has_adr(store, project_name, repo_path);
33003369
yyjson_mut_obj_add_bool(doc, root, "adr_present", adr_exists);
33013370
if (!adr_exists && !degraded) {
33023371
yyjson_mut_obj_add_str(
33033372
doc, root, "adr_hint",
33043373
"Project indexed. Consider creating an Architecture Decision Record: "
33053374
"explore the codebase with get_architecture(aspects=['all']), then use "
3306-
"manage_adr(mode='store') to persist architectural insights across sessions.");
3375+
"manage_adr(mode='update') to persist architectural insights across sessions.");
33073376
}
33083377

33093378
bool has_artifact = cbm_artifact_exists(repo_path);
@@ -3329,6 +3398,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
33293398
return cbm_mcp_text_result("repo_path is required", true);
33303399
}
33313400

3401+
repo_path = canonicalize_repo_path_if_exists(repo_path);
3402+
33323403
if (mode_str && strcmp(mode_str, "cross-repo-intelligence") == 0) {
33333404
free(mode_str);
33343405
free(name_override);
@@ -4780,10 +4851,13 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
47804851

47814852
char *root_path = get_project_root(srv, project);
47824853
if (!root_path) {
4854+
char *err = build_no_store_error(project);
4855+
char *res = cbm_mcp_text_result(err, true);
4856+
free(err);
47834857
free(project);
47844858
free(base_branch);
47854859
free(scope);
4786-
return cbm_mcp_text_result("project not found", true);
4860+
return res;
47874861
}
47884862

47894863
if (!validate_search_path_arg(root_path)) {
@@ -4998,10 +5072,13 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
49985072
* the UI are visible to each other (#256). */
49995073
cbm_store_t *resolved = resolve_store(srv, project);
50005074
if (!resolved) {
5075+
char *err = build_no_store_error(project);
5076+
char *res = cbm_mcp_text_result(err, true);
5077+
free(err);
50015078
free(project);
50025079
free(mode_str);
50035080
free(content);
5004-
return cbm_mcp_text_result("project not found", true);
5081+
return res;
50055082
}
50065083

50075084
/* resolve_store opens file-backed projects READ-ONLY (query stores must
@@ -5017,10 +5094,13 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
50175094
if (resolved_db_path) {
50185095
owned_rw = cbm_store_open_path(resolved_db_path);
50195096
if (!owned_rw) {
5097+
char *err = build_no_store_error(project);
5098+
char *res = cbm_mcp_text_result(err, true);
5099+
free(err);
50205100
free(project);
50215101
free(mode_str);
50225102
free(content);
5023-
return cbm_mcp_text_result("project not found", true);
5103+
return res;
50245104
}
50255105
store = owned_rw;
50265106
}

‎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)