Skip to content

Commit de4ec9b

Browse files
authored
Merge pull request #1319 from JJordan0C/fix/search-graph-semantic-only-results
fix(mcp): isolate semantic-only JSON search
2 parents 0273250 + 27eab73 commit de4ec9b

2 files changed

Lines changed: 76 additions & 38 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,22 +3170,6 @@ static bool run_semantic_query_core(const char *args, cbm_store_t *store, const
31703170
return type_error;
31713171
}
31723172

3173-
/* Append the semantic_query vector-search results onto the doc. Returns
3174-
* true if semantic_query was provided as a non-array (type error — caller
3175-
* should surface to the user). */
3176-
static bool run_semantic_query(yyjson_mut_doc *doc, yyjson_mut_val *root, const char *args,
3177-
cbm_store_t *store, const char *project, int limit) {
3178-
cbm_vector_result_t *vresults = NULL;
3179-
int vcount = 0;
3180-
bool type_error =
3181-
run_semantic_query_core(args, store, project, limit, &vresults, &vcount, NULL);
3182-
if (vcount > 0) {
3183-
emit_semantic_results(doc, root, vresults, vcount);
3184-
cbm_store_free_vector_results(vresults, vcount);
3185-
}
3186-
return type_error;
3187-
}
3188-
31893173
/* ── Tree output for search_graph ───────────────────────────────────
31903174
* Default response encoding: grouped tree rows (compact_out.h). The same
31913175
* model is available as structured JSON via format:"json"; include_connected
@@ -3620,6 +3604,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
36203604
.min_degree = min_degree,
36213605
.max_degree = max_degree,
36223606
};
3607+
bool has_structural_filters = label || name_pattern || qn_pattern || file_pattern ||
3608+
relationship || exclude_entry_points ||
3609+
min_degree != CBM_NOT_FOUND || max_degree != CBM_NOT_FOUND;
36233610

36243611
if (!legacy_json) {
36253612
const char *fields[SG_MAX_EXTRA_FIELDS];
@@ -3640,10 +3627,7 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
36403627
/* Semantic-only calls get semantic results only: the legacy
36413628
* behavior also ran the UNFILTERED regex search and prepended
36423629
* up to `limit` unrelated enriched nodes to the response. */
3643-
bool has_filters = label || name_pattern || qn_pattern || file_pattern ||
3644-
relationship || exclude_entry_points ||
3645-
min_degree != CBM_NOT_FOUND || max_degree != CBM_NOT_FOUND;
3646-
bool semantic_only = sq_present && !has_filters;
3630+
bool semantic_only = sq_present && !has_structural_filters;
36473631

36483632
cbm_sb_t sb;
36493633
cbm_sb_init(&sb);
@@ -3727,8 +3711,31 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
37273711
true);
37283712
}
37293713

3714+
cbm_vector_result_t *vresults = NULL;
3715+
int vcount = 0;
3716+
bool sq_present = false;
3717+
bool sq_type_error =
3718+
run_semantic_query_core(args, store, project, limit, &vresults, &vcount, &sq_present);
3719+
if (sq_type_error) {
3720+
free(project);
3721+
free(label);
3722+
free(name_pattern);
3723+
free(qn_pattern);
3724+
free(file_pattern);
3725+
free(relationship);
3726+
return cbm_mcp_text_result(
3727+
"semantic_query must be an array of keyword strings, e.g. "
3728+
"[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
3729+
"into individual keywords; each is scored independently via per-keyword "
3730+
"min-cosine.",
3731+
true);
3732+
}
3733+
3734+
bool semantic_only = sq_present && !has_structural_filters;
37303735
cbm_search_output_t out = {0};
3731-
cbm_store_search(store, &params, &out);
3736+
if (!semantic_only) {
3737+
cbm_store_search(store, &params, &out);
3738+
}
37323739

37333740
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
37343741
yyjson_mut_val *root = yyjson_mut_obj(doc);
@@ -3767,23 +3774,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {
37673774
}
37683775
}
37693776

3770-
bool sq_type_error = run_semantic_query(doc, root, args, store, project, limit);
3771-
3772-
if (sq_type_error) {
3773-
yyjson_mut_doc_free(doc);
3774-
cbm_store_search_free(&out);
3775-
free(project);
3776-
free(label);
3777-
free(name_pattern);
3778-
free(qn_pattern);
3779-
free(file_pattern);
3780-
free(relationship);
3781-
return cbm_mcp_text_result(
3782-
"semantic_query must be an array of keyword strings, e.g. "
3783-
"[\"send\",\"pubsub\",\"publish\"] — not a single string. Split your query "
3784-
"into individual keywords; each is scored independently via per-keyword "
3785-
"min-cosine.",
3786-
true);
3777+
if (vcount > 0) {
3778+
emit_semantic_results(doc, root, vresults, vcount);
3779+
cbm_store_free_vector_results(vresults, vcount);
37873780
}
37883781

37893782
char *json = yy_doc_to_str(doc);

‎tests/test_mcp.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,50 @@ static cbm_mcp_server_t *setup_snippet_server(char *tmp_dir, size_t tmp_sz);
16371637
static void cleanup_snippet_dir(const char *tmp_dir);
16381638
static char *extract_text_content(const char *mcp_result);
16391639

1640+
TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295) {
1641+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1642+
ASSERT_NOT_NULL(srv);
1643+
cbm_store_t *st = cbm_mcp_server_store(srv);
1644+
const char *proj = "semantic-only";
1645+
cbm_mcp_server_set_project(srv, proj);
1646+
cbm_store_upsert_project(st, proj, "/tmp/semantic-only");
1647+
1648+
cbm_node_t unrelated = {.project = proj,
1649+
.label = "Function",
1650+
.name = "unrelated_node",
1651+
.qualified_name = "semantic-only.unrelated_node",
1652+
.file_path = "unrelated.c",
1653+
.start_line = 1,
1654+
.end_line = 2};
1655+
ASSERT_GT(cbm_store_upsert_node(st, &unrelated), 0);
1656+
1657+
char *resp = cbm_mcp_handle_tool(
1658+
srv, "search_graph",
1659+
"{\"project\":\"semantic-only\",\"semantic_query\":[\"publish\"],\"limit\":5}");
1660+
ASSERT_NOT_NULL(resp);
1661+
char *inner = extract_text_content(resp);
1662+
free(resp);
1663+
ASSERT_NOT_NULL(inner);
1664+
ASSERT_NULL(strstr(inner, "unrelated_node"));
1665+
free(inner);
1666+
1667+
resp = cbm_mcp_handle_tool(srv, "search_graph",
1668+
"{\"project\":\"semantic-only\",\"semantic_query\":[\"publish\"],"
1669+
"\"format\":\"json\",\"limit\":5}");
1670+
ASSERT_NOT_NULL(resp);
1671+
inner = extract_text_content(resp);
1672+
free(resp);
1673+
ASSERT_NOT_NULL(inner);
1674+
ASSERT_NOT_NULL(strstr(inner, "\"total\":0"));
1675+
ASSERT_NOT_NULL(strstr(inner, "\"count\":0"));
1676+
ASSERT_NOT_NULL(strstr(inner, "\"groups\":[]"));
1677+
ASSERT_NULL(strstr(inner, "unrelated_node"));
1678+
free(inner);
1679+
1680+
cbm_mcp_server_free(srv);
1681+
PASS();
1682+
}
1683+
16401684
/* callers_total/callees_total must count what the caller can enumerate: with
16411685
* include_tests=false (default) test-file rows are hidden from the table, so
16421686
* the totals must apply the same filter — a raw visited_count overstated the
@@ -10815,6 +10859,7 @@ SUITE(mcp) {
1081510859
RUN_TEST(tool_get_graph_schema_empty);
1081610860
RUN_TEST(tool_unknown_tool);
1081710861
RUN_TEST(tool_search_graph_basic);
10862+
RUN_TEST(tool_search_graph_semantic_only_skips_structural_results_issue1295);
1081810863
RUN_TEST(tool_trace_totals_respect_test_filter);
1081910864
RUN_TEST(tool_trace_totals_respect_test_filter_tests_root_subtree_issue1294);
1082010865
RUN_TEST(tool_get_architecture_cycles_detects_scc);

0 commit comments

Comments
 (0)