Skip to content

Commit 86f49a0

Browse files
authored
Merge pull request DeusData#2524 from DeusData/fix/issue-1366-search-hint-v2
fix(mcp): avoid misleading empty-search filter hints
2 parents 369cf76 + 91117bd commit 86f49a0

2 files changed

Lines changed: 185 additions & 6 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5368,12 +5368,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
53685368
if (semantic_only && vcount == 0) {
53695369
diagnostic_hint = "No semantic matches; use a moderate/full index or broader keywords.";
53705370
} else if (!semantic_only && out.total == 0) {
5371-
if (name_pattern && label) {
5372-
diagnostic_hint = "No results; remove label or broaden name_pattern.";
5373-
} else if (name_pattern) {
5374-
diagnostic_hint = "No nodes match; check spelling or broaden the regex.";
5375-
} else if (label) {
5376-
diagnostic_hint = "No nodes have this label; inspect get_graph_schema.";
5371+
if (has_filters) {
5372+
diagnostic_hint =
5373+
"No results match the current filters; broaden or remove filters and retry.";
53775374
}
53785375
} else if (core_fields_requested) {
53795376
diagnostic_hint = "Core qn/name/label/file/lines fields are already present.";

‎tests/test_mcp.c‎

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,187 @@ TEST(tool_search_graph_basic) {
25622562
static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz);
25632563
static void cleanup_snippet_dir(const char *tmp_dir);
25642564

2565+
/* A real Function/name match can disappear under any additional structural
2566+
* filter. The diagnostic must not turn that absence into a claim about the
2567+
* label or spelling. Exercise the same graph through both response encodings. */
2568+
TEST(tool_search_graph_empty_hint_describes_combined_filters_issue1366) {
2569+
static const char filtered_hint[] =
2570+
"No results match the current filters; broaden or remove filters and retry.";
2571+
static const char core_hint[] = "Core qn/name/label/file/lines fields are already present.";
2572+
static const char semantic_hint[] =
2573+
"No semantic matches; use a moderate/full index or broader keywords.";
2574+
static const struct {
2575+
const char *filters;
2576+
int total;
2577+
const char *hint;
2578+
bool semantic_only;
2579+
} cases[] = {
2580+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\"", 1, NULL, false},
2581+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\","
2582+
"\"file_pattern\":\"src/*.c\",\"qn_pattern\":\"^hint1366[.]src[.]entry$\","
2583+
"\"relationship\":\"CALLS\",\"min_degree\":1,\"max_degree\":1",
2584+
1, NULL, false},
2585+
{"\"label\":\"Function\",\"file_pattern\":\"missing-file-sentinel/*\"", 0, filtered_hint,
2586+
false},
2587+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\","
2588+
"\"qn_pattern\":\"missing-qn-sentinel\"",
2589+
0, filtered_hint, false},
2590+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\","
2591+
"\"relationship\":\"IMPORTS\"",
2592+
0, filtered_hint, false},
2593+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\",\"min_degree\":2", 0, filtered_hint,
2594+
false},
2595+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\",\"max_degree\":0", 0, filtered_hint,
2596+
false},
2597+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\","
2598+
"\"exclude_entry_points\":true",
2599+
0, filtered_hint, false},
2600+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\","
2601+
"\"file_pattern\":\"missing-file-sentinel/*\","
2602+
"\"qn_pattern\":\"missing-qn-sentinel\"",
2603+
0, filtered_hint, false},
2604+
{"\"file_pattern\":\"missing-file-sentinel/*\"", 0, filtered_hint, false},
2605+
{"\"label\":\"Function\",\"name_pattern\":\"^entry$\",\"fields\":[\"name\",\"file\"]", 1,
2606+
core_hint, false},
2607+
{"\"semantic_query\":[\"hint1366-no-vectors\"]", 0, semantic_hint, true},
2608+
};
2609+
enum { CASE_COUNT = sizeof(cases) / sizeof(cases[0]), FORMAT_COUNT = 2 };
2610+
char *responses[FORMAT_COUNT][CASE_COUNT] = {{0}};
2611+
mcp_search_cache_t cache;
2612+
ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-search-hint1366"));
2613+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
2614+
cbm_store_t *store = srv ? cbm_mcp_server_store(srv) : NULL;
2615+
bool seeded = store && cbm_store_upsert_project(store, "hint1366", cache.path) == CBM_STORE_OK;
2616+
if (seeded) {
2617+
cbm_mcp_server_set_project(srv, "hint1366");
2618+
cbm_node_t entry = {.project = "hint1366",
2619+
.label = "Function",
2620+
.name = "entry",
2621+
.qualified_name = "hint1366.src.entry",
2622+
.file_path = "src/entry.c",
2623+
.start_line = 1,
2624+
.end_line = 3};
2625+
cbm_node_t target = {.project = "hint1366",
2626+
.label = "Method",
2627+
.name = "target",
2628+
.qualified_name = "hint1366.src.target",
2629+
.file_path = "src/target.c",
2630+
.start_line = 1,
2631+
.end_line = 2};
2632+
int64_t entry_id = cbm_store_upsert_node(store, &entry);
2633+
int64_t target_id = cbm_store_upsert_node(store, &target);
2634+
cbm_edge_t call = {
2635+
.project = "hint1366", .source_id = entry_id, .target_id = target_id, .type = "CALLS"};
2636+
/* One outgoing CALLS edge and no incoming CALLS makes entry a real
2637+
* entry point for the search filter, independently of properties. */
2638+
seeded = entry_id > 0 && target_id > 0 && cbm_store_insert_edge(store, &call) > 0;
2639+
}
2640+
bool requests_fit = true;
2641+
if (seeded) {
2642+
for (int format = 0; format < FORMAT_COUNT; format++) {
2643+
for (int c = 0; c < CASE_COUNT; c++) {
2644+
char args[1024];
2645+
int written =
2646+
snprintf(args, sizeof(args), "{\"project\":\"hint1366\",\"format\":\"%s\",%s}",
2647+
format == 0 ? "tree" : "json", cases[c].filters);
2648+
bool fits = written > 0 && (size_t)written < sizeof(args);
2649+
requests_fit = requests_fit && fits;
2650+
if (fits) {
2651+
responses[format][c] = cbm_mcp_handle_tool(srv, "search_graph", args);
2652+
}
2653+
}
2654+
}
2655+
}
2656+
if (srv) {
2657+
cbm_mcp_server_free(srv);
2658+
}
2659+
bool cleaned = mcp_search_cache_close(&cache);
2660+
/* Restore the caller's cache environment before any result assertion. */
2661+
ASSERT_TRUE(seeded);
2662+
ASSERT_TRUE(requests_fit);
2663+
ASSERT_TRUE(cleaned);
2664+
2665+
for (int format = 0; format < FORMAT_COUNT; format++) {
2666+
for (int c = 0; c < CASE_COUNT; c++) {
2667+
char *response = responses[format][c];
2668+
ASSERT_NOT_NULL(response);
2669+
yyjson_doc *outer = yyjson_read(response, strlen(response), 0);
2670+
ASSERT_NOT_NULL(outer);
2671+
yyjson_val *result = yyjson_doc_get_root(outer);
2672+
ASSERT_TRUE(yyjson_is_obj(result));
2673+
ASSERT_FALSE(yyjson_get_bool(yyjson_obj_get(result, "isError")));
2674+
yyjson_val *content = yyjson_obj_get(result, "content");
2675+
ASSERT_TRUE(yyjson_is_arr(content));
2676+
ASSERT_EQ(yyjson_arr_size(content), 1);
2677+
yyjson_val *item = yyjson_arr_get(content, 0);
2678+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(item, "type")), "text");
2679+
const char *inner = yyjson_get_str(yyjson_obj_get(item, "text"));
2680+
ASSERT_NOT_NULL(inner);
2681+
if (format == 1) {
2682+
yyjson_doc *doc = yyjson_read(inner, strlen(inner), 0);
2683+
ASSERT_NOT_NULL(doc);
2684+
yyjson_val *root = yyjson_doc_get_root(doc);
2685+
yyjson_val *total = yyjson_obj_get(root, "total");
2686+
yyjson_val *count = yyjson_obj_get(root, "count");
2687+
ASSERT_TRUE(yyjson_is_int(total));
2688+
ASSERT_TRUE(yyjson_is_int(count));
2689+
ASSERT_EQ(yyjson_get_int(total), cases[c].total);
2690+
ASSERT_EQ(yyjson_get_int(count), cases[c].total);
2691+
yyjson_val *groups = yyjson_obj_get(root, "groups");
2692+
ASSERT_TRUE(yyjson_is_arr(groups));
2693+
ASSERT_EQ(yyjson_arr_size(groups), cases[c].total);
2694+
if (cases[c].total == 1) {
2695+
yyjson_val *group = yyjson_arr_get(groups, 0);
2696+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(group, "qn_prefix")),
2697+
"hint1366.src");
2698+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(group, "file")), "src/entry.c");
2699+
yyjson_val *rows = yyjson_obj_get(group, "rows");
2700+
ASSERT_TRUE(yyjson_is_arr(rows));
2701+
ASSERT_EQ(yyjson_arr_size(rows), 1);
2702+
yyjson_val *row = yyjson_arr_get(rows, 0);
2703+
ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(row, 0)), "entry");
2704+
ASSERT_STR_EQ(yyjson_get_str(yyjson_arr_get(row, 1)), "Function");
2705+
}
2706+
if (cases[c].hint) {
2707+
ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(root, "hint")), cases[c].hint);
2708+
} else {
2709+
ASSERT_NULL(yyjson_obj_get(root, "hint"));
2710+
}
2711+
yyjson_doc_free(doc);
2712+
} else {
2713+
ASSERT_NOT_NULL(strstr(inner, cases[c].total == 1 ? "total: 1\n" : "total: 0\n"));
2714+
ASSERT_NOT_NULL(
2715+
strstr(inner, cases[c].total == 1 ? "returned: 1\n" : "returned: 0\n"));
2716+
if (cases[c].semantic_only) {
2717+
ASSERT_NULL(strstr(inner, "results:"));
2718+
ASSERT_NOT_NULL(strstr(inner, "semantic: 0"));
2719+
} else {
2720+
ASSERT_NOT_NULL(
2721+
strstr(inner, cases[c].total == 1 ? "results: 1" : "results: 0"));
2722+
}
2723+
if (cases[c].total == 1) {
2724+
ASSERT_NOT_NULL(strstr(inner, "entry"));
2725+
ASSERT_NOT_NULL(strstr(inner, "Function"));
2726+
ASSERT_NOT_NULL(strstr(inner, "src/entry.c"));
2727+
}
2728+
if (cases[c].hint) {
2729+
ASSERT_NOT_NULL(strstr(inner, cases[c].hint));
2730+
} else {
2731+
ASSERT_NULL(strstr(inner, "hint:"));
2732+
}
2733+
}
2734+
ASSERT_NULL(strstr(inner, "No nodes have this label"));
2735+
ASSERT_NULL(strstr(inner, "check spelling"));
2736+
ASSERT_NULL(strstr(inner, "broaden name_pattern"));
2737+
ASSERT_NULL(strstr(inner, "missing-file-sentinel"));
2738+
ASSERT_NULL(strstr(inner, "missing-qn-sentinel"));
2739+
yyjson_doc_free(outer);
2740+
free(response);
2741+
}
2742+
}
2743+
PASS();
2744+
}
2745+
25652746
TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295) {
25662747
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
25672748
ASSERT_NOT_NULL(srv);
@@ -21568,6 +21749,7 @@ SUITE(mcp) {
2156821749
RUN_TEST(tool_get_file_outline_returns_bounded_filtered_columnar_rows_issue469);
2156921750
RUN_TEST(tool_get_file_outline_validates_json_path_limit_and_cancel_issue469);
2157021751
RUN_TEST(tool_search_graph_basic);
21752+
RUN_TEST(tool_search_graph_empty_hint_describes_combined_filters_issue1366);
2157121753
RUN_TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295);
2157221754
RUN_TEST(tool_search_graph_grouped_dotless_qn_round_trips);
2157321755
RUN_TEST(tool_trace_totals_respect_test_filter);

0 commit comments

Comments
 (0)