@@ -1001,6 +1001,62 @@ TEST(tool_trace_call_path_prefers_definition) {
10011001 PASS ();
10021002}
10031003
1004+ /* Reproduce-first (#887): the client-supplied `depth` on trace_call_path must be
1005+ * clamped to the MCP ceiling (cbm_mcp_max_depth(), default 15). On origin/main
1006+ * an MCP_MAX_DEPTH=15 constant was defined but never applied — `depth` flowed
1007+ * straight into bfs_union_same_name, so an unbounded value drives the shared
1008+ * cbm_store_bfs to arbitrary depth. Over an 18-node call chain, depth=1000
1009+ * reaches n16/n17 (RED); with the clamp the walk stops at hop 15, so n15 is
1010+ * reached but n16 is not (GREEN). Quoted tokens ("n15"/"n16") match only the
1011+ * node-name field, never the qualified_name (preceded by '.'), so the boundary
1012+ * check is exact. */
1013+ TEST (tool_trace_call_path_depth_clamped ) {
1014+ cbm_mcp_server_t * srv = cbm_mcp_server_new (NULL );
1015+ cbm_store_t * st = cbm_mcp_server_store (srv );
1016+ const char * proj = "depth-proj" ;
1017+ cbm_mcp_server_set_project (srv , proj );
1018+ cbm_store_upsert_project (st , proj , "/tmp/depth" );
1019+
1020+ /* Linear call chain n00 -CALLS-> n01 -> ... -> n17 (18 nodes). */
1021+ int64_t ids [18 ];
1022+ for (int i = 0 ; i < 18 ; i ++ ) {
1023+ char name [8 ];
1024+ char qn [32 ];
1025+ snprintf (name , sizeof (name ), "n%02d" , i );
1026+ snprintf (qn , sizeof (qn ), "depth-proj.n%02d" , i );
1027+ cbm_node_t n = {.project = proj ,
1028+ .label = "Function" ,
1029+ .name = name ,
1030+ .qualified_name = qn ,
1031+ .file_path = "chain.c" ,
1032+ .start_line = 1 ,
1033+ .end_line = 2 };
1034+ ids [i ] = cbm_store_upsert_node (st , & n );
1035+ }
1036+ for (int i = 0 ; i < 17 ; i ++ ) {
1037+ cbm_edge_t e = {
1038+ .project = proj , .source_id = ids [i ], .target_id = ids [i + 1 ], .type = "CALLS" };
1039+ cbm_store_insert_edge (st , & e );
1040+ }
1041+
1042+ char * resp = cbm_mcp_server_handle (
1043+ srv , "{\"jsonrpc\":\"2.0\",\"id\":71,\"method\":\"tools/call\","
1044+ "\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":\"n00\","
1045+ "\"project\":\"depth-proj\",\"direction\":\"outbound\",\"depth\":1000}}}" );
1046+ ASSERT_NOT_NULL (resp );
1047+ char * inner = extract_text_content (resp );
1048+ ASSERT_NOT_NULL (inner );
1049+
1050+ /* Reached within the ceiling (proves the traversal ran) but clamped at 15. */
1051+ ASSERT_NOT_NULL (strstr (inner , "\"n15\"" ));
1052+ ASSERT_NULL (strstr (inner , "\"n16\"" ));
1053+
1054+ free (inner );
1055+ free (resp );
1056+ cbm_mcp_server_free (srv );
1057+ PASS ();
1058+ }
1059+
10041060/* Reproduce-first (#650, distilled): two GENUINELY-DIFFERENT same-named functions
10051061 * whose bodies differ in length score differently, so the old exact-tie check did
10061062 * not flag them ambiguous — and bfs_union_same_name (#546) then merged the caller
@@ -4711,6 +4767,7 @@ SUITE(mcp) {
47114767 RUN_TEST (tool_trace_missing_function_name );
47124768 RUN_TEST (tool_trace_call_path_ambiguous );
47134769 RUN_TEST (tool_trace_call_path_prefers_definition );
4770+ RUN_TEST (tool_trace_call_path_depth_clamped );
47144771 RUN_TEST (tool_trace_call_path_distinct_defs_not_over_unioned );
47154772 RUN_TEST (tool_trace_call_path_dts_stub_unions_with_impl );
47164773 RUN_TEST (tool_delete_project_not_found );
0 commit comments