Skip to content

Commit abda964

Browse files
authored
Merge pull request DeusData#748 from DeusData/distill/650-trace-ambiguity
fix(mcp): trace_path must not conflate distinct same-named functions
2 parents 46c11ff + 7d6d52b commit abda964

2 files changed

Lines changed: 145 additions & 3 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,9 +2684,33 @@ static long node_resolution_score(const cbm_node_t *n) {
26842684
return label_rank * (long)RES_LABEL_WEIGHT + span;
26852685
}
26862686

2687-
/* Pick the best-resolving node among name matches. Sets *ambiguous when the top
2688-
* score is shared by more than one candidate (a genuine tie the caller must
2689-
* disambiguate) so resolution never silently traces the wrong same-named node. */
2687+
/* A "real" callable definition: a Function/Method node with a non-empty body
2688+
* span (end_line > start_line). A body-less node (start_line == end_line) is an
2689+
* ambient declaration / signature stub — e.g. a TypeScript `.d.ts` declaration
2690+
* — which is a *fragment* of one logical symbol, not a distinct definition. The
2691+
* distinction lets pick_resolved_node union a stub with its real implementation
2692+
* (#546) while still treating two genuinely-different same-named functions as
2693+
* ambiguous rather than conflating their caller sets. */
2694+
static bool node_is_real_callable_def(const cbm_node_t *n) {
2695+
if (!n->label) {
2696+
return false;
2697+
}
2698+
if (strcmp(n->label, "Function") != 0 && strcmp(n->label, "Method") != 0) {
2699+
return false;
2700+
}
2701+
return (long)n->end_line - (long)n->start_line > 0;
2702+
}
2703+
2704+
/* Pick the best-resolving node among name matches. Sets *ambiguous when the
2705+
* matches can't be reduced to one logical symbol, so resolution never silently
2706+
* traces (or conflates) the wrong same-named node:
2707+
* 1. the top score is shared by >1 candidate (a genuine rank/span tie), or
2708+
* 2. two or more *real* callable definitions share the name — distinct
2709+
* implementations, not a definition plus its body-less stub(s).
2710+
* Rule 2 completes rule 1: without it, two same-named functions whose bodies
2711+
* differ in length score differently, dodge the tie, and get their caller sets
2712+
* unioned by bfs_union_same_name (#546) into one confidently-conflated answer.
2713+
* Body-less .d.ts stubs still union with their implementation (#650). */
26902714
static int pick_resolved_node(const cbm_node_t *nodes, int count, bool *ambiguous) {
26912715
*ambiguous = false;
26922716
if (count <= 1) {
@@ -2702,10 +2726,17 @@ static int pick_resolved_node(const cbm_node_t *nodes, int count, bool *ambiguou
27022726
}
27032727
}
27042728
int top_count = 0;
2729+
int real_def_count = 0;
27052730
for (int i = 0; i < count; i++) {
27062731
if (node_resolution_score(&nodes[i]) == best_score) {
27072732
top_count++;
27082733
}
2734+
if (node_is_real_callable_def(&nodes[i])) {
2735+
real_def_count++;
2736+
}
2737+
}
2738+
if (real_def_count > 1) {
2739+
*ambiguous = true;
27092740
}
27102741
if (top_count > 1) {
27112742
*ambiguous = true;

‎tests/test_mcp.c‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,115 @@ TEST(tool_trace_call_path_prefers_definition) {
814814
PASS();
815815
}
816816

817+
/* Reproduce-first (#650, distilled): two GENUINELY-DIFFERENT same-named functions
818+
* whose bodies differ in length score differently, so the old exact-tie check did
819+
* not flag them ambiguous — and bfs_union_same_name (#546) then merged the caller
820+
* sets of both into one confidently-conflated answer (the mirror of #546's under-
821+
* report). The fix: 2+ real callable defs => ambiguous (disambiguate), never union
822+
* distinct symbols. RED before the pick_resolved_node real_def_count rule (response
823+
* merged callerA+callerB), GREEN after (response is ambiguous, no "callers"). */
824+
TEST(tool_trace_call_path_distinct_defs_not_over_unioned) {
825+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
826+
cbm_store_t *st = cbm_mcp_server_store(srv);
827+
const char *proj = "ou-proj";
828+
cbm_mcp_server_set_project(srv, proj);
829+
cbm_store_upsert_project(st, proj, "/tmp/ou");
830+
/* two unrelated real definitions of "dupreal", DIFFERENT body spans */
831+
cbm_node_t da = {.project = proj, .label = "Function", .name = "dupreal",
832+
.qualified_name = "ou-proj.a.dupreal", .file_path = "a.c",
833+
.start_line = 10, .end_line = 20}; /* span 10 */
834+
cbm_node_t db = {.project = proj, .label = "Function", .name = "dupreal",
835+
.qualified_name = "ou-proj.b.dupreal", .file_path = "b.c",
836+
.start_line = 10, .end_line = 40}; /* span 30 (no tie) */
837+
cbm_node_t ca = {.project = proj, .label = "Function", .name = "callerA",
838+
.qualified_name = "ou-proj.a.callerA", .file_path = "a.c",
839+
.start_line = 30, .end_line = 40};
840+
cbm_node_t cb = {.project = proj, .label = "Function", .name = "callerB",
841+
.qualified_name = "ou-proj.b.callerB", .file_path = "b.c",
842+
.start_line = 50, .end_line = 60};
843+
int64_t id_da = cbm_store_upsert_node(st, &da);
844+
int64_t id_db = cbm_store_upsert_node(st, &db);
845+
int64_t id_ca = cbm_store_upsert_node(st, &ca);
846+
int64_t id_cb = cbm_store_upsert_node(st, &cb);
847+
ASSERT_GT(id_da, 0);
848+
ASSERT_GT(id_db, 0);
849+
ASSERT_GT(id_ca, 0);
850+
ASSERT_GT(id_cb, 0);
851+
cbm_edge_t ea = {.project = proj, .source_id = id_ca, .target_id = id_da, .type = "CALLS"};
852+
cbm_edge_t eb = {.project = proj, .source_id = id_cb, .target_id = id_db, .type = "CALLS"};
853+
cbm_store_insert_edge(st, &ea);
854+
cbm_store_insert_edge(st, &eb);
855+
856+
char *resp = cbm_mcp_server_handle(
857+
srv, "{\"jsonrpc\":\"2.0\",\"id\":63,\"method\":\"tools/call\","
858+
"\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":\"dupreal\","
859+
"\"project\":\"ou-proj\",\"direction\":\"inbound\"}}}");
860+
ASSERT_NOT_NULL(resp);
861+
char *inner = extract_text_content(resp);
862+
ASSERT_NOT_NULL(inner);
863+
/* distinct symbols must be disambiguated, not merged into one caller set */
864+
ASSERT_NOT_NULL(strstr(inner, "ambiguous"));
865+
ASSERT_NOT_NULL(strstr(inner, "suggestions"));
866+
ASSERT_NULL(strstr(inner, "\"callers\""));
867+
free(inner);
868+
free(resp);
869+
cbm_mcp_server_free(srv);
870+
PASS();
871+
}
872+
873+
/* Guard that the ambiguity gate does NOT regress the #546 fix: a real .ts
874+
* implementation plus a body-less ambient .d.ts stub is ONE logical symbol
875+
* (one real callable def + a fragment), so it must stay non-ambiguous and the
876+
* caller sets from both nodes must be unioned. */
877+
TEST(tool_trace_call_path_dts_stub_unions_with_impl) {
878+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
879+
cbm_store_t *st = cbm_mcp_server_store(srv);
880+
const char *proj = "dts-proj";
881+
cbm_mcp_server_set_project(srv, proj);
882+
cbm_store_upsert_project(st, proj, "/tmp/dts");
883+
cbm_node_t impl = {.project = proj, .label = "Function", .name = "sym546",
884+
.qualified_name = "dts-proj.impl.sym546", .file_path = "src/sym.ts",
885+
.start_line = 10, .end_line = 30}; /* real body */
886+
cbm_node_t stub = {.project = proj, .label = "Function", .name = "sym546",
887+
.qualified_name = "dts-proj.stub.sym546", .file_path = "types/sym.d.ts",
888+
.start_line = 5, .end_line = 5}; /* body-less ambient decl */
889+
cbm_node_t crel = {.project = proj, .label = "Function", .name = "callerRel",
890+
.qualified_name = "dts-proj.callerRel", .file_path = "src/rel.ts",
891+
.start_line = 1, .end_line = 8};
892+
cbm_node_t cali = {.project = proj, .label = "Function", .name = "callerAlias",
893+
.qualified_name = "dts-proj.callerAlias", .file_path = "src/ali.ts",
894+
.start_line = 1, .end_line = 8};
895+
int64_t id_impl = cbm_store_upsert_node(st, &impl);
896+
int64_t id_stub = cbm_store_upsert_node(st, &stub);
897+
int64_t id_crel = cbm_store_upsert_node(st, &crel);
898+
int64_t id_cali = cbm_store_upsert_node(st, &cali);
899+
ASSERT_GT(id_impl, 0);
900+
ASSERT_GT(id_stub, 0);
901+
ASSERT_GT(id_crel, 0);
902+
ASSERT_GT(id_cali, 0);
903+
/* callers split by import style: relative -> impl, path-alias -> stub */
904+
cbm_edge_t er = {.project = proj, .source_id = id_crel, .target_id = id_impl, .type = "CALLS"};
905+
cbm_edge_t el = {.project = proj, .source_id = id_cali, .target_id = id_stub, .type = "CALLS"};
906+
cbm_store_insert_edge(st, &er);
907+
cbm_store_insert_edge(st, &el);
908+
909+
char *resp = cbm_mcp_server_handle(
910+
srv, "{\"jsonrpc\":\"2.0\",\"id\":64,\"method\":\"tools/call\","
911+
"\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":\"sym546\","
912+
"\"project\":\"dts-proj\",\"direction\":\"inbound\"}}}");
913+
ASSERT_NOT_NULL(resp);
914+
char *inner = extract_text_content(resp);
915+
ASSERT_NOT_NULL(inner);
916+
ASSERT_NULL(strstr(inner, "ambiguous"));
917+
/* union across impl + stub: BOTH callers appear (this is the #546 fix) */
918+
ASSERT_NOT_NULL(strstr(inner, "callerRel"));
919+
ASSERT_NOT_NULL(strstr(inner, "callerAlias"));
920+
free(inner);
921+
free(resp);
922+
cbm_mcp_server_free(srv);
923+
PASS();
924+
}
925+
817926
TEST(tool_delete_project_not_found) {
818927
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
819928

@@ -3122,6 +3231,8 @@ SUITE(mcp) {
31223231
RUN_TEST(tool_trace_missing_function_name);
31233232
RUN_TEST(tool_trace_call_path_ambiguous);
31243233
RUN_TEST(tool_trace_call_path_prefers_definition);
3234+
RUN_TEST(tool_trace_call_path_distinct_defs_not_over_unioned);
3235+
RUN_TEST(tool_trace_call_path_dts_stub_unions_with_impl);
31253236
RUN_TEST(tool_delete_project_not_found);
31263237
RUN_TEST(tool_get_architecture_empty);
31273238
RUN_TEST(tool_get_architecture_emits_populated_sections);

0 commit comments

Comments
 (0)