|
10 | 10 | #include "../src/foundation/log.h" |
11 | 11 | #include "../src/foundation/platform.h" /* cbm_file_size */ |
12 | 12 | #include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */ |
| 13 | +#include "../src/foundation/str_util.h" |
13 | 14 | #include "../src/foundation/subprocess.h" |
14 | 15 | #include "../src/foundation/workspace.h" |
15 | 16 | #include "../src/mcp/compact_out.h" |
@@ -17734,6 +17735,186 @@ static bool issue704_make_db(const char *dir, const char *filename, const char * |
17734 | 17735 | return ok; |
17735 | 17736 | } |
17736 | 17737 |
|
| 17738 | +/* Cache entry classification must use exact internal names. CI workspace |
| 17739 | + * paths can produce real project names beginning with one or more underscores. */ |
| 17740 | +TEST(tool_underscore_projects_visible_in_cache_scans) { |
| 17741 | + char cache[CBM_SZ_512]; |
| 17742 | + int path_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-cache-XXXXXX", cbm_tmpdir()); |
| 17743 | + ASSERT_TRUE(path_len > 0 && (size_t)path_len < sizeof(cache)); |
| 17744 | + ASSERT_NOT_NULL(cbm_mkdtemp(cache)); |
| 17745 | + const char *saved = getenv("CBM_CACHE_DIR"); |
| 17746 | + char *saved_copy = saved ? cbm_strdup(saved) : NULL; |
| 17747 | + bool setup = (!saved || saved_copy) && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0; |
| 17748 | + bool seeded = setup && |
| 17749 | + issue704_make_db(cache, "__w-org-uscorerepo.db", "__w-org-uscorerepo", |
| 17750 | + "uscore_target_fn") && |
| 17751 | + issue704_make_db(cache, "_renamed.db", "uscore-drift", "uscore_drift_fn") && |
| 17752 | + issue704_make_db(cache, "_config-extra.db", "_config-extra", "uscore_extra_fn") && |
| 17753 | + issue704_make_db(cache, CBM_CONFIG_DB_FILENAME, "hidden-config", "hidden_fn") && |
| 17754 | + issue704_make_db(cache, CBM_CROSS_REPO_DB_FILENAME, "hidden-cross", "hidden_fn"); |
| 17755 | + cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL; |
| 17756 | + bool server_created = srv != NULL; |
| 17757 | + bool listed = false; |
| 17758 | + bool queried[3] = {false}; |
| 17759 | + bool hinted = false; |
| 17760 | + bool hidden = false; |
| 17761 | + if (srv) { |
| 17762 | + char *response = cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}"); |
| 17763 | + char *inner = response ? extract_text_content(response) : NULL; |
| 17764 | + yyjson_doc *doc = inner ? yyjson_read(inner, strlen(inner), 0) : NULL; |
| 17765 | + if (doc) { |
| 17766 | + yyjson_val *root = yyjson_doc_get_root(doc); |
| 17767 | + yyjson_val *projects = yyjson_obj_get(root, "projects"); |
| 17768 | + bool found[3] = {false}; |
| 17769 | + const char *expected[] = {"__w-org-uscorerepo", "uscore-drift", "_config-extra"}; |
| 17770 | + for (size_t i = 0; i < yyjson_arr_size(projects); i++) { |
| 17771 | + const char *name = |
| 17772 | + yyjson_get_str(yyjson_obj_get(yyjson_arr_get(projects, i), "name")); |
| 17773 | + for (size_t j = 0; j < 3; j++) { |
| 17774 | + found[j] = found[j] || (name && strcmp(name, expected[j]) == 0); |
| 17775 | + } |
| 17776 | + } |
| 17777 | + listed = yyjson_arr_size(projects) == 3 && |
| 17778 | + yyjson_get_int(yyjson_obj_get(root, "total")) == 3 && found[0] && found[1] && |
| 17779 | + found[2]; |
| 17780 | + yyjson_doc_free(doc); |
| 17781 | + } |
| 17782 | + free(inner); |
| 17783 | + free(response); |
| 17784 | + const char *args[] = { |
| 17785 | + "{\"project\":\"__w-org-uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}", |
| 17786 | + "{\"project\":\"uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}", |
| 17787 | + "{\"project\":\"uscore-drift\",\"name_pattern\":\"uscore_drift_fn\"}"}; |
| 17788 | + const char *targets[] = {"uscore_target_fn", "uscore_target_fn", "uscore_drift_fn"}; |
| 17789 | + for (size_t i = 0; i < 3; i++) { |
| 17790 | + response = cbm_mcp_handle_tool(srv, "search_graph", args[i]); |
| 17791 | + queried[i] = response && !strstr(response, "\"isError\":true") && |
| 17792 | + !strstr(response, "project not found") && strstr(response, targets[i]); |
| 17793 | + free(response); |
| 17794 | + } |
| 17795 | + response = |
| 17796 | + cbm_mcp_handle_tool(srv, "search_graph", |
| 17797 | + "{\"project\":\"unknown-uscore-project\",\"name_pattern\":\".*\"}"); |
| 17798 | + hinted = response && strstr(response, "project not found") && |
| 17799 | + strstr(response, "__w-org-uscorerepo") && strstr(response, "uscore-drift") && |
| 17800 | + strstr(response, "_config-extra") && !strstr(response, "hidden-config") && |
| 17801 | + !strstr(response, "hidden-cross"); |
| 17802 | + free(response); |
| 17803 | + response = cbm_mcp_handle_tool( |
| 17804 | + srv, "search_graph", "{\"project\":\"hidden-config\",\"name_pattern\":\"hidden_fn\"}"); |
| 17805 | + hidden = response && strstr(response, "project not found"); |
| 17806 | + free(response); |
| 17807 | + } |
| 17808 | + cbm_mcp_server_free(srv); |
| 17809 | + bool restored = !setup || (saved_copy ? cbm_setenv("CBM_CACHE_DIR", saved_copy, 1) |
| 17810 | + : cbm_unsetenv("CBM_CACHE_DIR")) == 0; |
| 17811 | + free(saved_copy); |
| 17812 | + bool removed = th_rmtree(cache) == 0; |
| 17813 | + ASSERT_TRUE(setup && seeded && server_created); |
| 17814 | + ASSERT_TRUE(restored && removed); |
| 17815 | + ASSERT_TRUE(listed); |
| 17816 | + ASSERT_TRUE(queried[0] && queried[1] && queried[2]); |
| 17817 | + ASSERT_TRUE(hinted && hidden); |
| 17818 | + PASS(); |
| 17819 | +} |
| 17820 | + |
| 17821 | +/* The fifth cache scan adopts an existing root owner before reindexing. |
| 17822 | + * Ignoring its underscore name would create a second path-derived project. */ |
| 17823 | +TEST(tool_reindex_reuses_underscore_root_owner) { |
| 17824 | + char repo[CBM_SZ_4K]; |
| 17825 | + char cache[CBM_SZ_4K]; |
| 17826 | + int repo_len = snprintf(repo, sizeof(repo), "%s/cbm-uscore-repo-XXXXXX", cbm_tmpdir()); |
| 17827 | + int cache_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-root-cache-XXXXXX", cbm_tmpdir()); |
| 17828 | + ASSERT_TRUE(repo_len > 0 && (size_t)repo_len < sizeof(repo)); |
| 17829 | + ASSERT_TRUE(cache_len > 0 && (size_t)cache_len < sizeof(cache)); |
| 17830 | + bool repo_created = cbm_mkdtemp(repo) != NULL; |
| 17831 | + bool cache_created = repo_created && cbm_mkdtemp(cache) != NULL; |
| 17832 | + char canonical[CBM_SZ_4K]; |
| 17833 | + bool setup = cache_created && cbm_canonical_path(repo, canonical, sizeof(canonical)); |
| 17834 | + if (setup) { |
| 17835 | + cbm_normalize_path_sep(canonical); |
| 17836 | + } |
| 17837 | + const char *keys[] = {"CBM_CACHE_DIR", "CBM_INDEX_SUPERVISOR"}; |
| 17838 | + char *saved[2] = {NULL}; |
| 17839 | + bool present[2] = {false}; |
| 17840 | + for (size_t i = 0; i < 2; i++) { |
| 17841 | + const char *value = getenv(keys[i]); |
| 17842 | + present[i] = value != NULL; |
| 17843 | + saved[i] = value ? cbm_strdup(value) : NULL; |
| 17844 | + setup = setup && (!value || saved[i]); |
| 17845 | + } |
| 17846 | + bool env_changed = setup; |
| 17847 | + if (setup) { |
| 17848 | + setup = cbm_setenv(keys[0], cache, 1) == 0 && cbm_setenv(keys[1], "0", 1) == 0; |
| 17849 | + } |
| 17850 | + char path[CBM_SZ_4K]; |
| 17851 | + int length = setup ? snprintf(path, sizeof(path), "%s/mod.py", repo) : -1; |
| 17852 | + FILE *source = length > 0 && (size_t)length < sizeof(path) ? cbm_fopen(path, "w") : NULL; |
| 17853 | + bool written = source && fputs("def underscore_root_target():\n return 1\n", source) >= 0; |
| 17854 | + if (source) { |
| 17855 | + written = fclose(source) == 0 && written; |
| 17856 | + } |
| 17857 | + length = setup ? snprintf(path, sizeof(path), "%s/_root-owner.db", cache) : -1; |
| 17858 | + cbm_store_t *store = |
| 17859 | + written && length > 0 && (size_t)length < sizeof(path) ? cbm_store_open_path(path) : NULL; |
| 17860 | + bool seeded = |
| 17861 | + store && cbm_store_upsert_project(store, "_root-owner", canonical) == CBM_STORE_OK; |
| 17862 | + if (store) { |
| 17863 | + cbm_store_close(store); |
| 17864 | + } |
| 17865 | + cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL; |
| 17866 | + bool server_created = srv != NULL; |
| 17867 | + bool session_bound = srv && cbm_mcp_server_set_session_context(srv, canonical, canonical); |
| 17868 | + bool indexed = false; |
| 17869 | + bool resolved = false; |
| 17870 | + bool no_duplicate = false; |
| 17871 | + if (session_bound) { |
| 17872 | + yyjson_mut_doc *args = yyjson_mut_doc_new(NULL); |
| 17873 | + yyjson_mut_val *object = args ? yyjson_mut_obj(args) : NULL; |
| 17874 | + char *serialized = NULL; |
| 17875 | + if (object) { |
| 17876 | + yyjson_mut_doc_set_root(args, object); |
| 17877 | + if (yyjson_mut_obj_add_str(args, object, "repo_path", canonical)) { |
| 17878 | + serialized = yyjson_mut_write(args, 0, NULL); |
| 17879 | + } |
| 17880 | + } |
| 17881 | + char *response = |
| 17882 | + serialized ? cbm_mcp_handle_tool(srv, "index_repository", serialized) : NULL; |
| 17883 | + indexed = |
| 17884 | + response && !strstr(response, "\"isError\":true") && strstr(response, "_root-owner"); |
| 17885 | + free(response); |
| 17886 | + free(serialized); |
| 17887 | + yyjson_mut_doc_free(args); |
| 17888 | + response = cbm_mcp_handle_tool( |
| 17889 | + srv, "search_graph", |
| 17890 | + "{\"project\":\"_root-owner\",\"name_pattern\":\"underscore_root_target\"}"); |
| 17891 | + resolved = response && !strstr(response, "\"isError\":true") && |
| 17892 | + strstr(response, "underscore_root_target") && |
| 17893 | + !strstr(response, "project not found"); |
| 17894 | + free(response); |
| 17895 | + char *derived = cbm_project_name_from_path(canonical); |
| 17896 | + length = derived ? snprintf(path, sizeof(path), "%s/%s.db", cache, derived) : -1; |
| 17897 | + no_duplicate = derived && strcmp(derived, "_root-owner") != 0 && length > 0 && |
| 17898 | + (size_t)length < sizeof(path) && !cbm_file_exists(path); |
| 17899 | + free(derived); |
| 17900 | + } |
| 17901 | + cbm_mcp_server_free(srv); |
| 17902 | + bool restored = true; |
| 17903 | + for (size_t i = 0; i < 2; i++) { |
| 17904 | + if (env_changed) { |
| 17905 | + int rc = present[i] ? cbm_setenv(keys[i], saved[i], 1) : cbm_unsetenv(keys[i]); |
| 17906 | + restored = rc == 0 && restored; |
| 17907 | + } |
| 17908 | + free(saved[i]); |
| 17909 | + } |
| 17910 | + bool repo_removed = !repo_created || th_rmtree(repo) == 0; |
| 17911 | + bool cache_removed = !cache_created || th_rmtree(cache) == 0; |
| 17912 | + ASSERT_TRUE(setup && written && seeded && server_created && session_bound); |
| 17913 | + ASSERT_TRUE(restored && repo_removed && cache_removed); |
| 17914 | + ASSERT_TRUE(indexed && resolved && no_duplicate); |
| 17915 | + PASS(); |
| 17916 | +} |
| 17917 | + |
17737 | 17918 | TEST(tool_resolve_store_by_internal_name_issue704) { |
17738 | 17919 | char cache[256]; |
17739 | 17920 | snprintf(cache, sizeof(cache), "/tmp/cbm-issue704-XXXXXX"); |
@@ -21350,6 +21531,8 @@ SUITE(mcp) { |
21350 | 21531 | RUN_TEST(snippet_source_invalid_utf8); |
21351 | 21532 | RUN_TEST(tool_bad_project_name_no_overflow_issue235); |
21352 | 21533 | RUN_TEST(tool_bad_project_error_valid_json_issue235); |
| 21534 | + RUN_TEST(tool_underscore_projects_visible_in_cache_scans); |
| 21535 | + RUN_TEST(tool_reindex_reuses_underscore_root_owner); |
21353 | 21536 | RUN_TEST(tool_resolve_store_by_internal_name_issue704); |
21354 | 21537 | RUN_TEST(tool_list_projects_ignores_missed_shadow_issue1044); |
21355 | 21538 |
|
|
0 commit comments