Skip to content

Commit abf0076

Browse files
committed
fix(mcp): resolve a directory project arg to its owning project (DeusData#1690)
An agent in a checkout whose directory name differs from the repository name (git clone .../repo_name.git some_name) could not find the indexed project. The installed skill only said "list_projects - check if project is indexed", so Codex and Copilot guessed the project from the repo name, got "project not found" and stopped using the graph. Passing the working directory did not help whenever it was not exactly the indexed root: a path was only ever turned into the path-DERIVED name, which misses a checkout indexed under an explicit name (the stored identity, see DeusData#1660) and every directory below an indexed root. Fix: - mcp: a path-shaped project keeps the derived name when an index exists under it (unchanged fast path); otherwise it resolves to the indexed project whose stored root_path owns the directory. The deepest owning root wins; two projects owning that root stay unresolved, so the not-found error lists every project instead of guessing. Reuses the list_projects identity reader and the canonical-path containment helpers. - mcp: the project-not-found hint says how to pick the project (the name whose root_path contains the working directory, or that absolute directory; never a repo or folder name), so an agent that guessed can recover. - cli: the SKILL.md every skill client installs (Codex, Copilot and the rest), the shared agent instructions and the Aider CLI instructions say the same thing. Proof (dev binary, synthetic HOME; repo_name cloned as some_name and indexed by path; a second checkout other_dir indexed with name=my-alias): before: project=<other_dir> -> project not found project=<some_name>/pkg -> project not found after: project=<other_dir> -> my-alias (index_status agrees) project=<some_name>/pkg -> the some_name project unchanged: the checkout root path and the explicit name resolve; the unindexed sibling some_name_x stays not found; a guessed "repo_name" stays not found and now carries the how-to-pick hint. Tests: - mcp tool_project_path_resolves_owning_project_issue1690: an explicitly named root and its subdirectory resolve; normal naming and similar names (some_name2, some_name_x) are not confused; two owners of one root are never guessed; a guessed repo name gets the hint. RED 3x before, green after, RED again when the production change is reverted (the hint check also goes RED on its own revert). - cli cli_guidance_resolves_project_by_root_path_issue1690: pins the instruction in the skill, agent-instructions and Aider variants. Fixes DeusData#1690 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0f52d30 commit abf0076

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
@@ -1572,6 +1572,8 @@ static bool bare_project_encoding_usable(const char *encoded) {
15721572
return strcmp(encoded, "root") != 0 && cbm_validate_project_name(encoded);
15731573
}
15741574

1575+
static char *resolve_path_owner(const char *path, char *derived); /* #1690, defined below */
1576+
15751577
static char *normalize_project_arg(char *project) {
15761578
if (!project) {
15771579
return project;
@@ -1585,6 +1587,9 @@ static char *normalize_project_arg(char *project) {
15851587
} else {
15861588
project = canonicalize_repo_path_if_exists(project);
15871589
normalized = cbm_project_name_from_path(project);
1590+
if (normalized) {
1591+
normalized = resolve_path_owner(project, normalized);
1592+
}
15881593
}
15891594
/* Keep exactly one of the two strings and release the other. */
15901595
bool use_normalized = normalized && (!bare || bare_project_encoding_usable(normalized));
@@ -2640,10 +2645,13 @@ static char *build_project_list_error(const char *reason) {
26402645
enum { ERR_BUF_SZ = 5120 };
26412646
char buf[ERR_BUF_SZ];
26422647
if (count > 0) {
2648+
/* #1690: say how to pick the project, so an agent that guessed a repo
2649+
* or folder name can recover instead of dropping the graph. */
26432650
snprintf(buf, sizeof(buf),
2644-
"{\"error\":\"%s\",\"hint\":\"Use list_projects to see all indexed projects, "
2645-
"then pass it as the \\\"project\\\" "
2646-
"argument.\",\"available_projects\":[%s],\"count\":%d}",
2651+
"{\"error\":\"%s\",\"hint\":\"Use list_projects and pass the name whose "
2652+
"root_path contains your working directory (or that absolute directory) as "
2653+
"the \\\"project\\\" argument; never a repo or folder "
2654+
"name.\",\"available_projects\":[%s],\"count\":%d}",
26472655
reason, projects, count);
26482656
} else {
26492657
snprintf(buf, sizeof(buf),
@@ -11844,6 +11852,66 @@ bool cbm_path_within_root(const char *root_path, const char *abs_path) {
1184411852
return false;
1184511853
}
1184611854

11855+
/* #1690: a path-shaped `project` names a directory; the installed guidance tells
11856+
* agents to pass their working directory. Turning it into the path-DERIVED name
11857+
* alone missed a checkout indexed under an explicit name and every directory
11858+
* below an indexed root, so the agent got "project not found" and dropped the
11859+
* graph. Keep the derived name when an index exists under it (the unchanged
11860+
* fast path); otherwise adopt the indexed project whose stored root_path owns
11861+
* the directory. The deepest owning root wins. Two projects owning that root
11862+
* are ambiguous and keep the derived name, so the not-found error lists every
11863+
* project instead of a guess. Takes ownership of `derived`; returns a heap name. */
11864+
static char *resolve_path_owner(const char *path, char *derived) {
11865+
char db_path[CBM_SZ_1K];
11866+
project_db_path(derived, db_path, sizeof(db_path));
11867+
if (db_path[0] && cbm_file_exists(db_path)) {
11868+
return derived;
11869+
}
11870+
char real_path[CBM_SZ_4K];
11871+
char dir_path[CBM_SZ_1K];
11872+
cache_dir(dir_path, sizeof(dir_path));
11873+
cbm_dir_t *d =
11874+
resolve_canonical_path(path, real_path, sizeof(real_path)) ? cbm_opendir(dir_path) : NULL;
11875+
if (!d) {
11876+
return derived;
11877+
}
11878+
char *owner = NULL;
11879+
size_t owner_depth = 0;
11880+
bool ambiguous = false;
11881+
cbm_dirent_t *entry;
11882+
while ((entry = cbm_readdir(d)) != NULL) {
11883+
mcp_project_record_t record = {0};
11884+
if (!is_project_db_file(entry->name, strlen(entry->name)) ||
11885+
read_project_record_identity(dir_path, entry->name, 0, &record) != PROJECT_RECORD_OK) {
11886+
continue;
11887+
}
11888+
char real_root[CBM_SZ_4K];
11889+
if (record.root_path[0] &&
11890+
resolve_canonical_path(record.root_path, real_root, sizeof(real_root)) &&
11891+
canonical_path_has_root(real_root, real_path)) {
11892+
size_t depth = strlen(real_root);
11893+
if (depth > owner_depth) {
11894+
safe_free(owner);
11895+
owner = record.name;
11896+
record.name = NULL;
11897+
owner_depth = depth;
11898+
ambiguous = false;
11899+
} else if (depth == owner_depth) {
11900+
ambiguous = true;
11901+
}
11902+
}
11903+
project_record_clear(&record);
11904+
}
11905+
cbm_closedir(d);
11906+
if (!owner || ambiguous) {
11907+
safe_free(owner);
11908+
return derived;
11909+
}
11910+
cbm_log_info("mcp.project_path_resolved", "path", real_path, "resolved", owner);
11911+
safe_free(derived);
11912+
return owner;
11913+
}
11914+
1184711915
static char *resolve_snippet_source(const char *root_path, const char *file_path, int start,
1184811916
int end, char **out_abs_path) {
1184911917
*out_abs_path = NULL;

‎tests/test_cli.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14154,6 +14154,33 @@ TEST(cli_agent_instructions_content) {
1415414154
PASS();
1415514155
}
1415614156

14157+
/* #1690: an agent in a checkout whose directory name differs from the repo
14158+
* name guessed the project from the repo name, got "project not found" and
14159+
* dropped the graph. Every installed guidance variant must say how to pick the
14160+
* project: the list_projects entry whose root_path contains the working
14161+
* directory, or that absolute directory itself, never a repo or folder name. */
14162+
static int i1690_names_project_by_root(const char *text) {
14163+
return text && strstr(text, "root_path") != NULL &&
14164+
strstr(text, "contains your working directory") != NULL &&
14165+
strstr(text, "absolute directory") != NULL &&
14166+
strstr(text, "never a repo or folder name") != NULL;
14167+
}
14168+
14169+
TEST(cli_guidance_resolves_project_by_root_path_issue1690) {
14170+
const cbm_skill_t *skills = cbm_get_skills();
14171+
ASSERT_NOT_NULL(skills);
14172+
for (int i = 0; i < CBM_SKILL_COUNT; i++) {
14173+
if (!i1690_names_project_by_root(skills[i].content)) {
14174+
fprintf(stderr, " [1690] skill %s does not say how to pick the project\n",
14175+
skills[i].name);
14176+
}
14177+
ASSERT(i1690_names_project_by_root(skills[i].content));
14178+
}
14179+
ASSERT(i1690_names_project_by_root(cbm_get_agent_instructions()));
14180+
ASSERT(i1690_names_project_by_root(cbm_get_aider_instructions()));
14181+
PASS();
14182+
}
14183+
1415714184
TEST(cli_qwen_windows_hook_command_uses_powershell_schema) {
1415814185
char command[1024];
1415914186
char shell[32];
@@ -16964,6 +16991,7 @@ SUITE(cli) {
1696416991
RUN_TEST(cli_upsert_instructions_no_duplicate);
1696516992
RUN_TEST(cli_remove_instructions);
1696616993
RUN_TEST(cli_agent_instructions_content);
16994+
RUN_TEST(cli_guidance_resolves_project_by_root_path_issue1690);
1696716995
RUN_TEST(cli_qwen_windows_hook_command_uses_powershell_schema);
1696816996
RUN_TEST(cli_windows_optional_hooks_require_a_documented_shell);
1696916997
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
@@ -8417,6 +8417,189 @@ TEST(tool_search_graph_accepts_project_name_alias_issue640) {
84178417
PASS();
84188418
}
84198419

8420+
/* #1690: the installed skill tells an agent to pass its absolute working
8421+
* directory (or the list_projects name whose root_path contains it) as
8422+
* `project`, never a guessed repo or folder name. A path-shaped project used to
8423+
* be turned into the path-DERIVED name only, so it missed a checkout indexed
8424+
* under an explicit name and every directory below an indexed root: the agent
8425+
* got "project not found" and dropped the graph. It must resolve to the indexed
8426+
* project whose stored root_path owns the path, whatever that project is
8427+
* called; the deepest owning root wins, and two owners of one root are never
8428+
* guessed between. */
8429+
static char *i1690_search(cbm_mcp_server_t *srv, const char *project) {
8430+
char args[CBM_SZ_4K];
8431+
snprintf(args, sizeof(args), "{\"project\":\"%s\",\"name_pattern\":\".*_fn_1690\"}", project);
8432+
return cbm_mcp_handle_tool(srv, "search_graph", args);
8433+
}
8434+
8435+
static char *i1690_index(cbm_mcp_server_t *srv, const char *repo, const char *name) {
8436+
char args[CBM_SZ_4K];
8437+
if (name) {
8438+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"name\":\"%s\"}", repo, name);
8439+
} else {
8440+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo);
8441+
}
8442+
return cbm_mcp_handle_tool(srv, "index_repository", args);
8443+
}
8444+
8445+
TEST(tool_project_path_resolves_owning_project_issue1690) {
8446+
char tmp[CBM_SZ_256];
8447+
char cache[CBM_SZ_256];
8448+
snprintf(tmp, sizeof(tmp), "/tmp/cbm-i1690r-XXXXXX");
8449+
snprintf(cache, sizeof(cache), "/tmp/cbm-i1690c-XXXXXX");
8450+
if (!cbm_mkdtemp(tmp) || !cbm_mkdtemp(cache)) {
8451+
FAIL("mkdtemp failed");
8452+
}
8453+
/* Forward slashes keep the JSON arguments valid on every platform. */
8454+
char base[CBM_SZ_4K]; /* cbm_canonical_path needs >= 4096 bytes */
8455+
if (!cbm_canonical_path(tmp, base, sizeof(base))) {
8456+
FAIL("cbm_canonical_path failed");
8457+
}
8458+
cbm_normalize_path_sep(base);
8459+
const char *saved_cache = getenv("CBM_CACHE_DIR");
8460+
char *saved_cache_copy = saved_cache ? cbm_strdup(saved_cache) : NULL;
8461+
const char *saved_sup = getenv("CBM_INDEX_SUPERVISOR");
8462+
char *saved_sup_copy = saved_sup ? cbm_strdup(saved_sup) : NULL;
8463+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
8464+
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
8465+
8466+
/* some_name: the checkout of "repo_name", indexed under that explicit name.
8467+
* some_name2: a similarly named checkout indexed by path (derived name).
8468+
* some_name_x: shares the "some_name" prefix and is NOT indexed.
8469+
* twin_root: one root indexed under two explicit names. */
8470+
char named[CBM_SZ_4K];
8471+
char named_sub[CBM_SZ_4K];
8472+
char similar[CBM_SZ_4K];
8473+
char similar_sub[CBM_SZ_4K];
8474+
char unindexed[CBM_SZ_4K];
8475+
char twin[CBM_SZ_4K];
8476+
snprintf(named, sizeof(named), "%s/some_name", base);
8477+
snprintf(named_sub, sizeof(named_sub), "%s/some_name/pkg", base);
8478+
snprintf(similar, sizeof(similar), "%s/some_name2", base);
8479+
snprintf(similar_sub, sizeof(similar_sub), "%s/some_name2/pkg", base);
8480+
snprintf(unindexed, sizeof(unindexed), "%s/some_name_x", base);
8481+
snprintf(twin, sizeof(twin), "%s/twin_root", base);
8482+
ASSERT_EQ(th_write_file(TH_PATH(named, "app.py"), "def named_fn_1690(x):\n return x\n"), 0);
8483+
ASSERT_EQ(th_write_file(TH_PATH(named_sub, "entry.py"), "def sub_fn_1690():\n return 1\n"),
8484+
0);
8485+
ASSERT_EQ(th_write_file(TH_PATH(similar, "app.py"), "def similar_fn_1690(x):\n return x\n"),
8486+
0);
8487+
ASSERT_EQ(
8488+
th_write_file(TH_PATH(similar_sub, "entry.py"), "def deep_fn_1690():\n return 2\n"), 0);
8489+
ASSERT_EQ(th_write_file(TH_PATH(unindexed, "app.py"), "def stray_fn_1690():\n return 3\n"),
8490+
0);
8491+
ASSERT_EQ(th_write_file(TH_PATH(twin, "app.py"), "def twin_fn_1690():\n return 4\n"), 0);
8492+
8493+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
8494+
ASSERT_NOT_NULL(srv);
8495+
char *r = i1690_index(srv, named, "repo_name1690");
8496+
ASSERT_NOT_NULL(r);
8497+
ASSERT_NOT_NULL(strstr(r, "repo_name1690"));
8498+
free(r);
8499+
r = i1690_index(srv, similar, NULL);
8500+
ASSERT_NOT_NULL(r);
8501+
ASSERT_NULL(strstr(r, "\"isError\":true"));
8502+
free(r);
8503+
r = i1690_index(srv, twin, "twin1690a");
8504+
ASSERT_NOT_NULL(r);
8505+
free(r);
8506+
r = i1690_index(srv, twin, "twin1690b");
8507+
ASSERT_NOT_NULL(r);
8508+
free(r);
8509+
8510+
/* 1. The explicitly named checkout's root path resolves to that project
8511+
* (RED before the fix: the derived name has no index). */
8512+
r = i1690_search(srv, named);
8513+
ASSERT_NOT_NULL(r);
8514+
if (strstr(r, "project not found")) {
8515+
fprintf(stderr, " [1690] FAIL named root path did not resolve: %.300s\n", r);
8516+
}
8517+
ASSERT_NULL(strstr(r, "project not found"));
8518+
ASSERT_NOT_NULL(strstr(r, "named_fn_1690"));
8519+
ASSERT_NULL(strstr(r, "similar_fn_1690"));
8520+
free(r);
8521+
8522+
/* 2. A working directory below that root resolves to the same project. */
8523+
r = i1690_search(srv, named_sub);
8524+
ASSERT_NOT_NULL(r);
8525+
if (strstr(r, "project not found")) {
8526+
fprintf(stderr, " [1690] FAIL subdirectory path did not resolve: %.300s\n", r);
8527+
}
8528+
ASSERT_NULL(strstr(r, "project not found"));
8529+
ASSERT_NOT_NULL(strstr(r, "sub_fn_1690"));
8530+
free(r);
8531+
8532+
/* 3. index_status names the resolved identity, not the derived name. */
8533+
char status_args[CBM_SZ_4K];
8534+
snprintf(status_args, sizeof(status_args), "{\"project\":\"%s\",\"format\":\"json\"}",
8535+
named_sub);
8536+
r = cbm_mcp_handle_tool(srv, "index_status", status_args);
8537+
ASSERT_NOT_NULL(r);
8538+
char *status = extract_text_content(r);
8539+
ASSERT_NOT_NULL(status);
8540+
ASSERT_NOT_NULL(strstr(status, "\"project\":\"repo_name1690\""));
8541+
free(status);
8542+
free(r);
8543+
8544+
/* 4. Control, normal naming: a path-derived project resolves as before,
8545+
* and its subdirectory resolves to it, never to the similar name. */
8546+
r = i1690_search(srv, similar);
8547+
ASSERT_NOT_NULL(r);
8548+
ASSERT_NOT_NULL(strstr(r, "similar_fn_1690"));
8549+
ASSERT_NULL(strstr(r, "named_fn_1690"));
8550+
free(r);
8551+
r = i1690_search(srv, similar_sub);
8552+
ASSERT_NOT_NULL(r);
8553+
ASSERT_NULL(strstr(r, "project not found"));
8554+
ASSERT_NOT_NULL(strstr(r, "deep_fn_1690"));
8555+
ASSERT_NULL(strstr(r, "named_fn_1690"));
8556+
free(r);
8557+
8558+
/* 5. Control, similar names: a sibling that only shares the "some_name"
8559+
* prefix is owned by no project and must not borrow one. */
8560+
r = i1690_search(srv, unindexed);
8561+
ASSERT_NOT_NULL(r);
8562+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8563+
free(r);
8564+
8565+
/* 6. Two projects owning one root are ambiguous: never guess. */
8566+
r = i1690_search(srv, twin);
8567+
ASSERT_NOT_NULL(r);
8568+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8569+
free(r);
8570+
8571+
/* 7. Control: the exact explicit name keeps working. */
8572+
r = i1690_search(srv, "repo_name1690");
8573+
ASSERT_NOT_NULL(r);
8574+
ASSERT_NOT_NULL(strstr(r, "named_fn_1690"));
8575+
free(r);
8576+
8577+
/* 8. A guessed repo name stays unresolved (nothing is guessed), but the
8578+
* error says how to pick the project so the agent can recover. */
8579+
r = i1690_search(srv, "repo_name");
8580+
ASSERT_NOT_NULL(r);
8581+
ASSERT_NOT_NULL(strstr(r, "project not found"));
8582+
ASSERT_NOT_NULL(strstr(r, "root_path contains your working directory"));
8583+
free(r);
8584+
8585+
cbm_mcp_server_free(srv);
8586+
if (saved_cache_copy) {
8587+
cbm_setenv("CBM_CACHE_DIR", saved_cache_copy, 1);
8588+
free(saved_cache_copy);
8589+
} else {
8590+
cbm_unsetenv("CBM_CACHE_DIR");
8591+
}
8592+
if (saved_sup_copy) {
8593+
cbm_setenv("CBM_INDEX_SUPERVISOR", saved_sup_copy, 1);
8594+
free(saved_sup_copy);
8595+
} else {
8596+
cbm_unsetenv("CBM_INDEX_SUPERVISOR");
8597+
}
8598+
th_rmtree(tmp);
8599+
th_rmtree(cache);
8600+
PASS();
8601+
}
8602+
84208603
/* #1025: agents pass the repo FOLDER name ("codebase-memory-mcp"), but
84218604
* indexed project names derive from the full path
84228605
* (E:\project\graph\x -> "E-project-graph-x"), so exact lookup fails with
@@ -21020,6 +21203,7 @@ SUITE(mcp) {
2102021203
RUN_TEST(tool_get_architecture_rejects_unknown_aspect_pr560);
2102121204
RUN_TEST(tool_get_architecture_accepts_project_name_alias_issue640);
2102221205
RUN_TEST(tool_search_graph_accepts_project_name_alias_issue640);
21206+
RUN_TEST(tool_project_path_resolves_owning_project_issue1690);
2102321207
RUN_TEST(tool_project_arg_resolves_unique_tail_issue1025);
2102421208
RUN_TEST(tool_project_arg_resolves_non_ascii_folder_issue1827);
2102521209
RUN_TEST(tool_get_architecture_path_scoping);

0 commit comments

Comments
 (0)