Skip to content

Commit f6fe11d

Browse files
authored
Merge pull request DeusData#2446 from DeusData/fix/issue-1690-skill-project-name
fix(mcp): resolve a directory project arg to its owning project (DeusData#1690)
2 parents 01fd6cb + abf0076 commit f6fe11d

4 files changed

Lines changed: 293 additions & 4 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,11 @@ static const char skill_content[] =
15041504
"| Text search | `search_code` or Grep |\n"
15051505
"\n"
15061506
"## Exploration Workflow\n"
1507-
"1. `list_projects` — check if project is indexed\n"
1507+
/* #1690: a guessed repo or folder name misses the project whenever the
1508+
* checkout directory differs from it; the root_path is what identifies it. */
1509+
"1. `list_projects` — pick the project whose `root_path` contains your working directory; "
1510+
"pass its `name` (or the absolute directory itself) as `project`, never a repo or folder "
1511+
"name\n"
15081512
"2. `get_graph_schema` — understand node/edge types\n"
15091513
"3. `search_graph(label=\"Function\", name_pattern=\".*Pattern.*\")` — find code\n"
15101514
"4. `get_code_snippet(qualified_name=\"project.path.FuncName\")` — read source\n"
@@ -3274,6 +3278,9 @@ static const char agent_instructions_content[] =
32743278
"### Session resets and subagents\n"
32753279
"- At session start or after compaction, confirm the nearest graph project and generation with "
32763280
"`list_projects` or `index_status`, then choose Scout, Verify, or Auditor.\n"
3281+
"- The graph project is the `list_projects` entry whose `root_path` contains your working "
3282+
"directory; pass its `name` (or the absolute directory itself) as `project`, never a repo or "
3283+
"folder name.\n"
32773284
"- Before spawning a subagent, query the graph and coverage in the parent. Pass the tier, "
32783285
"project, generation/freshness, bounded scope, queries and pagination state, qualified "
32793286
"symbols, "
@@ -3679,6 +3686,8 @@ static const char aider_instructions_content[] =
36793686
"First use in a repo: codebase-memory-mcp cli index_repository '{\"repo_path\":\"<abs "
36803687
"path>\"}'\n"
36813688
"List indexed projects (for <name>): codebase-memory-mcp cli list_projects '{}'\n"
3689+
"<name> is the entry whose root_path contains your working directory; pass it (or the "
3690+
"absolute directory itself), never a repo or folder name.\n"
36823691
"\n"
36833692
"## When to fall back to grep/glob\n"
36843693
"- Searching for string literals, error messages, config values\n"

‎src/mcp/mcp.c‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,8 @@ static bool bare_project_encoding_usable(const char *encoded) {
15731573
return strcmp(encoded, "root") != 0 && cbm_validate_project_name(encoded);
15741574
}
15751575

1576+
static char *resolve_path_owner(const char *path, char *derived); /* #1690, defined below */
1577+
15761578
static char *normalize_project_arg(char *project) {
15771579
if (!project) {
15781580
return project;
@@ -1586,6 +1588,9 @@ static char *normalize_project_arg(char *project) {
15861588
} else {
15871589
project = canonicalize_repo_path_if_exists(project);
15881590
normalized = cbm_project_name_from_path(project);
1591+
if (normalized) {
1592+
normalized = resolve_path_owner(project, normalized);
1593+
}
15891594
}
15901595
/* Keep exactly one of the two strings and release the other. */
15911596
bool use_normalized = normalized && (!bare || bare_project_encoding_usable(normalized));
@@ -2641,10 +2646,13 @@ static char *build_project_list_error(const char *reason) {
26412646
enum { ERR_BUF_SZ = 5120 };
26422647
char buf[ERR_BUF_SZ];
26432648
if (count > 0) {
2649+
/* #1690: say how to pick the project, so an agent that guessed a repo
2650+
* or folder name can recover instead of dropping the graph. */
26442651
snprintf(buf, sizeof(buf),
2645-
"{\"error\":\"%s\",\"hint\":\"Use list_projects to see all indexed projects, "
2646-
"then pass it as the \\\"project\\\" "
2647-
"argument.\",\"available_projects\":[%s],\"count\":%d}",
2652+
"{\"error\":\"%s\",\"hint\":\"Use list_projects and pass the name whose "
2653+
"root_path contains your working directory (or that absolute directory) as "
2654+
"the \\\"project\\\" argument; never a repo or folder "
2655+
"name.\",\"available_projects\":[%s],\"count\":%d}",
26482656
reason, projects, count);
26492657
} else {
26502658
snprintf(buf, sizeof(buf),
@@ -12020,6 +12028,66 @@ bool cbm_path_within_root(const char *root_path, const char *abs_path) {
1202012028
return false;
1202112029
}
1202212030

12031+
/* #1690: a path-shaped `project` names a directory; the installed guidance tells
12032+
* agents to pass their working directory. Turning it into the path-DERIVED name
12033+
* alone missed a checkout indexed under an explicit name and every directory
12034+
* below an indexed root, so the agent got "project not found" and dropped the
12035+
* graph. Keep the derived name when an index exists under it (the unchanged
12036+
* fast path); otherwise adopt the indexed project whose stored root_path owns
12037+
* the directory. The deepest owning root wins. Two projects owning that root
12038+
* are ambiguous and keep the derived name, so the not-found error lists every
12039+
* project instead of a guess. Takes ownership of `derived`; returns a heap name. */
12040+
static char *resolve_path_owner(const char *path, char *derived) {
12041+
char db_path[CBM_SZ_1K];
12042+
project_db_path(derived, db_path, sizeof(db_path));
12043+
if (db_path[0] && cbm_file_exists(db_path)) {
12044+
return derived;
12045+
}
12046+
char real_path[CBM_SZ_4K];
12047+
char dir_path[CBM_SZ_1K];
12048+
cache_dir(dir_path, sizeof(dir_path));
12049+
cbm_dir_t *d =
12050+
resolve_canonical_path(path, real_path, sizeof(real_path)) ? cbm_opendir(dir_path) : NULL;
12051+
if (!d) {
12052+
return derived;
12053+
}
12054+
char *owner = NULL;
12055+
size_t owner_depth = 0;
12056+
bool ambiguous = false;
12057+
cbm_dirent_t *entry;
12058+
while ((entry = cbm_readdir(d)) != NULL) {
12059+
mcp_project_record_t record = {0};
12060+
if (!is_project_db_file(entry->name, strlen(entry->name)) ||
12061+
read_project_record_identity(dir_path, entry->name, 0, &record) != PROJECT_RECORD_OK) {
12062+
continue;
12063+
}
12064+
char real_root[CBM_SZ_4K];
12065+
if (record.root_path[0] &&
12066+
resolve_canonical_path(record.root_path, real_root, sizeof(real_root)) &&
12067+
canonical_path_has_root(real_root, real_path)) {
12068+
size_t depth = strlen(real_root);
12069+
if (depth > owner_depth) {
12070+
safe_free(owner);
12071+
owner = record.name;
12072+
record.name = NULL;
12073+
owner_depth = depth;
12074+
ambiguous = false;
12075+
} else if (depth == owner_depth) {
12076+
ambiguous = true;
12077+
}
12078+
}
12079+
project_record_clear(&record);
12080+
}
12081+
cbm_closedir(d);
12082+
if (!owner || ambiguous) {
12083+
safe_free(owner);
12084+
return derived;
12085+
}
12086+
cbm_log_info("mcp.project_path_resolved", "path", real_path, "resolved", owner);
12087+
safe_free(derived);
12088+
return owner;
12089+
}
12090+
1202312091
static char *resolve_snippet_source(const char *root_path, const char *file_path, int start,
1202412092
int end, char **out_abs_path) {
1202512093
*out_abs_path = NULL;

‎tests/test_cli.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14205,6 +14205,33 @@ TEST(cli_agent_instructions_content) {
1420514205
PASS();
1420614206
}
1420714207

14208+
/* #1690: an agent in a checkout whose directory name differs from the repo
14209+
* name guessed the project from the repo name, got "project not found" and
14210+
* dropped the graph. Every installed guidance variant must say how to pick the
14211+
* project: the list_projects entry whose root_path contains the working
14212+
* directory, or that absolute directory itself, never a repo or folder name. */
14213+
static int i1690_names_project_by_root(const char *text) {
14214+
return text && strstr(text, "root_path") != NULL &&
14215+
strstr(text, "contains your working directory") != NULL &&
14216+
strstr(text, "absolute directory") != NULL &&
14217+
strstr(text, "never a repo or folder name") != NULL;
14218+
}
14219+
14220+
TEST(cli_guidance_resolves_project_by_root_path_issue1690) {
14221+
const cbm_skill_t *skills = cbm_get_skills();
14222+
ASSERT_NOT_NULL(skills);
14223+
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
14224+
if (!i1690_names_project_by_root(skills[i].content)) {
14225+
fprintf(stderr, " [1690] skill %s does not say how to pick the project\n",
14226+
skills[i].name);
14227+
}
14228+
ASSERT(i1690_names_project_by_root(skills[i].content));
14229+
}
14230+
ASSERT(i1690_names_project_by_root(cbm_get_agent_instructions()));
14231+
ASSERT(i1690_names_project_by_root(cbm_get_aider_instructions()));
14232+
PASS();
14233+
}
14234+
1420814235
TEST(cli_qwen_windows_hook_command_uses_powershell_schema) {
1420914236
char command[1024];
1421014237
char shell[32];
@@ -17050,6 +17077,7 @@ SUITE(cli) {
1705017077
RUN_TEST(cli_upsert_instructions_no_duplicate);
1705117078
RUN_TEST(cli_remove_instructions);
1705217079
RUN_TEST(cli_agent_instructions_content);
17080+
RUN_TEST(cli_guidance_resolves_project_by_root_path_issue1690);
1705317081
RUN_TEST(cli_qwen_windows_hook_command_uses_powershell_schema);
1705417082
RUN_TEST(cli_windows_optional_hooks_require_a_documented_shell);
1705517083
RUN_TEST(cli_installed_skill_limits_match_server_contract);

‎tests/test_mcp.c‎

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8478,6 +8478,189 @@ TEST(tool_search_graph_accepts_project_name_alias_issue640) {
84788478
PASS();
84798479
}
84808480

8481+
/* #1690: the installed skill tells an agent to pass its absolute working
8482+
* directory (or the list_projects name whose root_path contains it) as
8483+
* `project`, never a guessed repo or folder name. A path-shaped project used to
8484+
* be turned into the path-DERIVED name only, so it missed a checkout indexed
8485+
* under an explicit name and every directory below an indexed root: the agent
8486+
* got "project not found" and dropped the graph. It must resolve to the indexed
8487+
* project whose stored root_path owns the path, whatever that project is
8488+
* called; the deepest owning root wins, and two owners of one root are never
8489+
* guessed between. */
8490+
static char *i1690_search(cbm_mcp_server_t *srv, const char *project) {
8491+
char args[CBM_SZ_4K];
8492+
snprintf(args, sizeof(args), "{\"project\":\"%s\",\"name_pattern\":\".*_fn_1690\"}", project);
8493+
return cbm_mcp_handle_tool(srv, "search_graph", args);
8494+
}
8495+
8496+
static char *i1690_index(cbm_mcp_server_t *srv, const char *repo, const char *name) {
8497+
char args[CBM_SZ_4K];
8498+
if (name) {
8499+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"name\":\"%s\"}", repo, name);
8500+
} else {
8501+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo);
8502+
}
8503+
return cbm_mcp_handle_tool(srv, "index_repository", args);
8504+
}
8505+
8506+
TEST(tool_project_path_resolves_owning_project_issue1690) {
8507+
char tmp[CBM_SZ_256];
8508+
char cache[CBM_SZ_256];
8509+
snprintf(tmp, sizeof(tmp), "/tmp/cbm-i1690r-XXXXXX");
8510+
snprintf(cache, sizeof(cache), "/tmp/cbm-i1690c-XXXXXX");
8511+
if (!cbm_mkdtemp(tmp) || !cbm_mkdtemp(cache)) {
8512+
FAIL("mkdtemp failed");
8513+
}
8514+
/* Forward slashes keep the JSON arguments valid on every platform. */
8515+
char base[CBM_SZ_4K]; /* cbm_canonical_path needs >= 4096 bytes */
8516+
if (!cbm_canonical_path(tmp, base, sizeof(base))) {
8517+
FAIL("cbm_canonical_path failed");
8518+
}
8519+
cbm_normalize_path_sep(base);
8520+
const char *saved_cache = getenv("CBM_CACHE_DIR");
8521+
char *saved_cache_copy = saved_cache ? cbm_strdup(saved_cache) : NULL;
8522+
const char *saved_sup = getenv("CBM_INDEX_SUPERVISOR");
8523+
char *saved_sup_copy = saved_sup ? cbm_strdup(saved_sup) : NULL;
8524+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
8525+
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
8526+
8527+
/* some_name: the checkout of "repo_name", indexed under that explicit name.
8528+
* some_name2: a similarly named checkout indexed by path (derived name).
8529+
* some_name_x: shares the "some_name" prefix and is NOT indexed.
8530+
* twin_root: one root indexed under two explicit names. */
8531+
char named[CBM_SZ_4K];
8532+
char named_sub[CBM_SZ_4K];
8533+
char similar[CBM_SZ_4K];
8534+
char similar_sub[CBM_SZ_4K];
8535+
char unindexed[CBM_SZ_4K];
8536+
char twin[CBM_SZ_4K];
8537+
snprintf(named, sizeof(named), "%s/some_name", base);
8538+
snprintf(named_sub, sizeof(named_sub), "%s/some_name/pkg", base);
8539+
snprintf(similar, sizeof(similar), "%s/some_name2", base);
8540+
snprintf(similar_sub, sizeof(similar_sub), "%s/some_name2/pkg", base);
8541+
snprintf(unindexed, sizeof(unindexed), "%s/some_name_x", base);
8542+
snprintf(twin, sizeof(twin), "%s/twin_root", base);
8543+
ASSERT_EQ(th_write_file(TH_PATH(named, "app.py"), "def named_fn_1690(x):\n return x\n"), 0);
8544+
ASSERT_EQ(th_write_file(TH_PATH(named_sub, "entry.py"), "def sub_fn_1690():\n return 1\n"),
8545+
0);
8546+
ASSERT_EQ(th_write_file(TH_PATH(similar, "app.py"), "def similar_fn_1690(x):\n return x\n"),
8547+
0);
8548+
ASSERT_EQ(
8549+
th_write_file(TH_PATH(similar_sub, "entry.py"), "def deep_fn_1690():\n return 2\n"), 0);
8550+
ASSERT_EQ(th_write_file(TH_PATH(unindexed, "app.py"), "def stray_fn_1690():\n return 3\n"),
8551+
0);
8552+
ASSERT_EQ(th_write_file(TH_PATH(twin, "app.py"), "def twin_fn_1690():\n return 4\n"), 0);
8553+
8554+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
8555+
ASSERT_NOT_NULL(srv);
8556+
char *r = i1690_index(srv, named, "repo_name1690");
8557+
ASSERT_NOT_NULL(r);
8558+
ASSERT_NOT_NULL(strstr(r, "repo_name1690"));
8559+
free(r);
8560+
r = i1690_index(srv, similar, NULL);
8561+
ASSERT_NOT_NULL(r);
8562+
ASSERT_NULL(strstr(r, "\"isError\":true"));
8563+
free(r);
8564+
r = i1690_index(srv, twin, "twin1690a");
8565+
ASSERT_NOT_NULL(r);
8566+
free(r);
8567+
r = i1690_index(srv, twin, "twin1690b");
8568+
ASSERT_NOT_NULL(r);
8569+
free(r);
8570+
8571+
/* 1. The explicitly named checkout's root path resolves to that project
8572+
* (RED before the fix: the derived name has no index). */
8573+
r = i1690_search(srv, named);
8574+
ASSERT_NOT_NULL(r);
8575+
if (strstr(r, "project not found")) {
8576+
fprintf(stderr, " [1690] FAIL named root path did not resolve: %.300s\n", r);
8577+
}
8578+
ASSERT_NULL(strstr(r, "project not found"));
8579+
ASSERT_NOT_NULL(strstr(r, "named_fn_1690"));
8580+
ASSERT_NULL(strstr(r, "similar_fn_1690"));
8581+
free(r);
8582+
8583+
/* 2. A working directory below that root resolves to the same project. */
8584+
r = i1690_search(srv, named_sub);
8585+
ASSERT_NOT_NULL(r);
8586+
if (strstr(r, "project not found")) {
8587+
fprintf(stderr, " [1690] FAIL subdirectory path did not resolve: %.300s\n", r);
8588+
}
8589+
ASSERT_NULL(strstr(r, "project not found"));
8590+
ASSERT_NOT_NULL(strstr(r, "sub_fn_1690"));
8591+
free(r);
8592+
8593+
/* 3. index_status names the resolved identity, not the derived name. */
8594+
char status_args[CBM_SZ_4K];
8595+
snprintf(status_args, sizeof(status_args), "{\"project\":\"%s\",\"format\":\"json\"}",
8596+
named_sub);
8597+
r = cbm_mcp_handle_tool(srv, "index_status", status_args);
8598+
ASSERT_NOT_NULL(r);
8599+
char *status = extract_text_content(r);
8600+
ASSERT_NOT_NULL(status);
8601+
ASSERT_NOT_NULL(strstr(status, "\"project\":\"repo_name1690\""));
8602+
free(status);
8603+
free(r);
8604+
8605+
/* 4. Control, normal naming: a path-derived project resolves as before,
8606+
* and its subdirectory resolves to it, never to the similar name. */
8607+
r = i1690_search(srv, similar);
8608+
ASSERT_NOT_NULL(r);
8609+
ASSERT_NOT_NULL(strstr(r, "similar_fn_1690"));
8610+
ASSERT_NULL(strstr(r, "named_fn_1690"));
8611+
free(r);
8612+
r = i1690_search(srv, similar_sub);
8613+
ASSERT_NOT_NULL(r);
8614+
ASSERT_NULL(strstr(r, "project not found"));
8615+
ASSERT_NOT_NULL(strstr(r, "deep_fn_1690"));
8616+
ASSERT_NULL(strstr(r, "named_fn_1690"));
8617+
free(r);
8618+
8619+
/* 5. Control, similar names: a sibling that only shares the "some_name"
8620+
* prefix is owned by no project and must not borrow one. */
8621+
r = i1690_search(srv, unindexed);
8622+
ASSERT_NOT_NULL(r);
8623+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8624+
free(r);
8625+
8626+
/* 6. Two projects owning one root are ambiguous: never guess. */
8627+
r = i1690_search(srv, twin);
8628+
ASSERT_NOT_NULL(r);
8629+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8630+
free(r);
8631+
8632+
/* 7. Control: the exact explicit name keeps working. */
8633+
r = i1690_search(srv, "repo_name1690");
8634+
ASSERT_NOT_NULL(r);
8635+
ASSERT_NOT_NULL(strstr(r, "named_fn_1690"));
8636+
free(r);
8637+
8638+
/* 8. A guessed repo name stays unresolved (nothing is guessed), but the
8639+
* error says how to pick the project so the agent can recover. */
8640+
r = i1690_search(srv, "repo_name");
8641+
ASSERT_NOT_NULL(r);
8642+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8643+
ASSERT_NOT_NULL(strstr(r, "root_path contains your working directory"));
8644+
free(r);
8645+
8646+
cbm_mcp_server_free(srv);
8647+
if (saved_cache_copy) {
8648+
cbm_setenv("CBM_CACHE_DIR", saved_cache_copy, 1);
8649+
free(saved_cache_copy);
8650+
} else {
8651+
cbm_unsetenv("CBM_CACHE_DIR");
8652+
}
8653+
if (saved_sup_copy) {
8654+
cbm_setenv("CBM_INDEX_SUPERVISOR", saved_sup_copy, 1);
8655+
free(saved_sup_copy);
8656+
} else {
8657+
cbm_unsetenv("CBM_INDEX_SUPERVISOR");
8658+
}
8659+
th_rmtree(tmp);
8660+
th_rmtree(cache);
8661+
PASS();
8662+
}
8663+
84818664
/* #1025: agents pass the repo FOLDER name ("codebase-memory-mcp"), but
84828665
* indexed project names derive from the full path
84838666
* (E:\project\graph\x -> "E-project-graph-x"), so exact lookup fails with
@@ -21188,6 +21371,7 @@ SUITE(mcp) {
2118821371
RUN_TEST(tool_get_architecture_rejects_unknown_aspect_pr560);
2118921372
RUN_TEST(tool_get_architecture_accepts_project_name_alias_issue640);
2119021373
RUN_TEST(tool_search_graph_accepts_project_name_alias_issue640);
21374+
RUN_TEST(tool_project_path_resolves_owning_project_issue1690);
2119121375
RUN_TEST(tool_project_arg_resolves_unique_tail_issue1025);
2119221376
RUN_TEST(tool_project_arg_resolves_non_ascii_folder_issue1827);
2119321377
RUN_TEST(tool_index_repository_reuses_existing_project_for_root_issue2134);

0 commit comments

Comments
 (0)