@@ -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+
817926TEST (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