@@ -5914,7 +5914,7 @@ TEST(tool_query_graph_prefix_directory_recovers_rows_beyond_raw_estimate) {
59145914 PASS();
59155915}
59165916
5917- TEST(tool_list_projects_stays_complete_across_store_switches_issue1115 ) {
5917+ TEST(tool_list_projects_tree_uses_one_stable_header_and_keeps_json_direct ) {
59185918 char cache[256];
59195919 snprintf(cache, sizeof(cache), "%s/cbm-list-lean-XXXXXX", cbm_tmpdir());
59205920 ASSERT_NOT_NULL(cbm_mkdtemp(cache));
@@ -5971,6 +5971,97 @@ TEST(tool_list_projects_stays_complete_across_store_switches_issue1115) {
59715971 bool tree_not_duplicated = tree_response && !strstr(tree_response, "structuredContent");
59725972 bool page_truthful = page && strstr(page, "projects: 5") && strstr(page, "returned: 5") &&
59735973 strstr(page, "has_more: true") && strstr(page, "next_offset: 5");
5974+ bool json_direct = false;
5975+ bool json_structured = json_response && strstr(json_response, "structuredContent");
5976+ yyjson_doc *doc = json ? yyjson_read(json, strlen(json), 0) : NULL;
5977+ if (doc) {
5978+ yyjson_val *projects = yyjson_obj_get(yyjson_doc_get_root(doc), "projects");
5979+ yyjson_val *first = projects ? yyjson_arr_get_first(projects) : NULL;
5980+ json_direct = projects && yyjson_arr_size(projects) == PROJECTS && first &&
5981+ yyjson_obj_get(first, "name") && yyjson_obj_get(first, "root_path") &&
5982+ yyjson_obj_get(first, "nodes") && yyjson_obj_get(first, "edges") &&
5983+ yyjson_obj_get(first, "size_bytes") && !strstr(json, "rows_refs");
5984+ yyjson_doc_free(doc);
5985+ }
5986+
5987+ free(identity);
5988+ free(identity_response);
5989+ free(tree);
5990+ free(tree_response);
5991+ free(json);
5992+ free(json_response);
5993+ free(page);
5994+ free(page_response);
5995+ cbm_mcp_server_free(srv);
5996+ restore_cache_dir(saved_cache_copy);
5997+ free(saved_cache_copy);
5998+ for (int i = 0; i < PROJECTS; i++) {
5999+ char project[32];
6000+ snprintf(project, sizeof(project), "lean-project-%02d", i);
6001+ cleanup_project_db(cache, project);
6002+ }
6003+ cbm_rmdir(cache);
6004+
6005+ ASSERT_TRUE(tree_shape);
6006+ ASSERT_TRUE(identity_lean);
6007+ ASSERT_TRUE(tree_is_leaner);
6008+ ASSERT_TRUE(tree_not_duplicated);
6009+ ASSERT_TRUE(page_truthful);
6010+ ASSERT_TRUE(json_direct);
6011+ ASSERT_TRUE(json_structured);
6012+ PASS();
6013+ }
6014+
6015+ /* #1115: with more indexed projects than were resident at once, the
6016+ * least-recently-used one answered "project not found or not indexed" from
6017+ * index_status, and list_projects and index_status disagreed in the same
6018+ * session. The server keeps one project store open and switches it whenever a
6019+ * call names another project, so twelve projects visited one after another
6020+ * force a switch on every step and each visit is the first touch of a project
6021+ * that is not resident. Every project must resolve through index_status and a
6022+ * query, and list_projects must report all twelve before and after the
6023+ * switches. */
6024+ TEST(tool_project_discovery_agrees_across_store_switches_issue1115) {
6025+ char cache[256];
6026+ snprintf(cache, sizeof(cache), "%s/cbm-switch-1115-XXXXXX", cbm_tmpdir());
6027+ ASSERT_NOT_NULL(cbm_mkdtemp(cache));
6028+ const char *saved_cache = getenv("CBM_CACHE_DIR");
6029+ char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
6030+ cbm_setenv("CBM_CACHE_DIR", cache, 1);
6031+
6032+ enum { PROJECTS = 12 };
6033+ for (int i = 0; i < PROJECTS; i++) {
6034+ char project[32];
6035+ char db_path[512];
6036+ char root_path[512];
6037+ snprintf(project, sizeof(project), "lean-project-%02d", i);
6038+ snprintf(db_path, sizeof(db_path), "%s/%s.db", cache, project);
6039+ snprintf(root_path, sizeof(root_path),
6040+ "/workspaces/organization/shared/services/lean-project-%02d", i);
6041+ cbm_store_t *store = cbm_store_open_path(db_path);
6042+ ASSERT_NOT_NULL(store);
6043+ ASSERT_EQ(cbm_store_upsert_project(store, project, root_path), CBM_STORE_OK);
6044+ cbm_node_t node = {.project = project,
6045+ .label = "Function",
6046+ .name = "entry",
6047+ .qualified_name = "shared.module.entry",
6048+ .file_path = "src/main.c",
6049+ .start_line = 1,
6050+ .end_line = 1};
6051+ ASSERT_GT(cbm_store_upsert_node(store, &node), 0);
6052+ cbm_store_close(store);
6053+ }
6054+
6055+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
6056+ ASSERT_NOT_NULL(srv);
6057+ char *pre_switch_response = cbm_mcp_handle_tool(srv, "list_projects", "{\"limit\":50}");
6058+ char *pre_switch = extract_text_content(pre_switch_response);
6059+ bool list_complete_before_switches = pre_switch && strstr(pre_switch, "total: 12") &&
6060+ strstr(pre_switch, "lean-project-00") &&
6061+ strstr(pre_switch, "lean-project-11");
6062+ free(pre_switch);
6063+ free(pre_switch_response);
6064+
59746065 bool projects_resolve_after_switches = true;
59756066 for (int i = PROJECTS - 1; i >= 0; i--) {
59766067 char project[32];
@@ -5986,8 +6077,7 @@ TEST(tool_list_projects_stays_complete_across_store_switches_issue1115) {
59866077 free(status);
59876078 free(status_response);
59886079
5989- snprintf(args, sizeof(args), "{\"project\":\"%s\",\"name_pattern\":\"entry\"}",
5990- project);
6080+ snprintf(args, sizeof(args), "{\"project\":\"%s\",\"name_pattern\":\"entry\"}", project);
59916081 char *search_response = cbm_mcp_handle_tool(srv, "search_graph", args);
59926082 char *search = extract_text_content(search_response);
59936083 if (!search || !strstr(search, "entry")) {
@@ -5998,32 +6088,12 @@ TEST(tool_list_projects_stays_complete_across_store_switches_issue1115) {
59986088 }
59996089 char *post_switch_response = cbm_mcp_handle_tool(srv, "list_projects", "{\"limit\":50}");
60006090 char *post_switch = extract_text_content(post_switch_response);
6001- bool list_complete_after_switches =
6002- post_switch && strstr(post_switch, "total: 12") && strstr(post_switch, "lean-project-00") &&
6003- strstr(post_switch, "lean-project-11");
6091+ bool list_complete_after_switches = post_switch && strstr(post_switch, "total: 12") &&
6092+ strstr(post_switch, "lean-project-00") &&
6093+ strstr(post_switch, "lean-project-11");
60046094 free(post_switch);
60056095 free(post_switch_response);
6006- bool json_direct = false;
6007- bool json_structured = json_response && strstr(json_response, "structuredContent");
6008- yyjson_doc *doc = json ? yyjson_read(json, strlen(json), 0) : NULL;
6009- if (doc) {
6010- yyjson_val *projects = yyjson_obj_get(yyjson_doc_get_root(doc), "projects");
6011- yyjson_val *first = projects ? yyjson_arr_get_first(projects) : NULL;
6012- json_direct = projects && yyjson_arr_size(projects) == PROJECTS && first &&
6013- yyjson_obj_get(first, "name") && yyjson_obj_get(first, "root_path") &&
6014- yyjson_obj_get(first, "nodes") && yyjson_obj_get(first, "edges") &&
6015- yyjson_obj_get(first, "size_bytes") && !strstr(json, "rows_refs");
6016- yyjson_doc_free(doc);
6017- }
60186096
6019- free(identity);
6020- free(identity_response);
6021- free(tree);
6022- free(tree_response);
6023- free(json);
6024- free(json_response);
6025- free(page);
6026- free(page_response);
60276097 cbm_mcp_server_free(srv);
60286098 restore_cache_dir(saved_cache_copy);
60296099 free(saved_cache_copy);
@@ -6034,15 +6104,9 @@ TEST(tool_list_projects_stays_complete_across_store_switches_issue1115) {
60346104 }
60356105 cbm_rmdir(cache);
60366106
6037- ASSERT_TRUE(tree_shape);
6038- ASSERT_TRUE(identity_lean);
6039- ASSERT_TRUE(tree_is_leaner);
6040- ASSERT_TRUE(tree_not_duplicated);
6041- ASSERT_TRUE(page_truthful);
6107+ ASSERT_TRUE(list_complete_before_switches);
60426108 ASSERT_TRUE(projects_resolve_after_switches);
60436109 ASSERT_TRUE(list_complete_after_switches);
6044- ASSERT_TRUE(json_direct);
6045- ASSERT_TRUE(json_structured);
60466110 PASS();
60476111}
60486112
@@ -25105,7 +25169,8 @@ SUITE(mcp) {
2510525169 RUN_TEST(tool_query_graph_budget_bounds_first_row_and_json_escaping);
2510625170 RUN_TEST(tool_query_graph_prefix_directory_is_lossless_and_json_stays_direct);
2510725171 RUN_TEST(tool_query_graph_prefix_directory_recovers_rows_beyond_raw_estimate);
25108- RUN_TEST(tool_list_projects_stays_complete_across_store_switches_issue1115);
25172+ RUN_TEST(tool_list_projects_tree_uses_one_stable_header_and_keeps_json_direct);
25173+ RUN_TEST(tool_project_discovery_agrees_across_store_switches_issue1115);
2510925174 RUN_TEST(tool_list_projects_preserves_root_beyond_one_kib);
2511025175 RUN_TEST(tool_index_status_no_project);
2511125176 RUN_TEST(tool_ingest_traces_stores_observed_calls);
0 commit comments