11from typing import Any , Dict
22from ..code_finder import CodeFinder
33from ...utils .debug_log import debug_log
4+ from ...utils .tool_limits import get_tool_result_limit
5+
46
57def find_dead_code (code_finder : CodeFinder , ** args ) -> Dict [str , Any ]:
68 """Tool to find potentially dead code across the entire project."""
@@ -9,16 +11,25 @@ def find_dead_code(code_finder: CodeFinder, **args) -> Dict[str, Any]:
911 try :
1012 debug_log (f"Finding dead code. repo_path={ repo_path } " )
1113 results = code_finder .find_dead_code (exclude_decorated_with = exclude_decorated_with , repo_path = repo_path )
12-
14+
15+ limit = get_tool_result_limit ("find_dead_code" )
16+ unused = results .get ("potentially_unused_functions" , [])
17+ truncated = False
18+ if limit and len (unused ) > limit :
19+ unused = unused [:limit ]
20+ truncated = True
21+
1322 return {
1423 "success" : True ,
1524 "query_type" : "dead_code" ,
16- "results" : results
25+ "results" : {** results , "potentially_unused_functions" : unused },
26+ ** ({"result_limit" : limit , "truncated" : truncated } if truncated else {}),
1727 }
1828 except Exception as e :
1929 debug_log (f"Error finding dead code: { str (e )} " )
2030 return {"error" : f"Failed to find dead code: { str (e )} " }
2131
32+
2233def calculate_cyclomatic_complexity (code_finder : CodeFinder , ** args ) -> Dict [str , Any ]:
2334 """Tool to calculate cyclomatic complexity for a given function."""
2435 function_name = args .get ("function_name" )
@@ -28,23 +39,24 @@ def calculate_cyclomatic_complexity(code_finder: CodeFinder, **args) -> Dict[str
2839 try :
2940 debug_log (f"Calculating cyclomatic complexity for function: { function_name } , repo_path={ repo_path } " )
3041 results = code_finder .get_cyclomatic_complexity (function_name , path , repo_path = repo_path )
31-
42+
3243 response = {
3344 "success" : True ,
3445 "function_name" : function_name ,
3546 "results" : results
3647 }
3748 if path :
3849 response ["path" ] = path
39-
50+
4051 return response
4152 except Exception as e :
4253 debug_log (f"Error calculating cyclomatic complexity: { str (e )} " )
4354 return {"error" : f"Failed to calculate cyclomatic complexity: { str (e )} " }
4455
56+
4557def find_most_complex_functions (code_finder : CodeFinder , ** args ) -> Dict [str , Any ]:
4658 """Tool to find the most complex functions."""
47- limit = args .get ("limit" , 10 )
59+ limit = get_tool_result_limit ( "find_most_complex_functions" , default = args .get ("limit" , 10 ) )
4860 repo_path = args .get ("repo_path" )
4961 try :
5062 debug_log (f"Finding the top { limit } most complex functions. repo_path={ repo_path } " )
@@ -58,6 +70,7 @@ def find_most_complex_functions(code_finder: CodeFinder, **args) -> Dict[str, An
5870 debug_log (f"Error finding most complex functions: { str (e )} " )
5971 return {"error" : f"Failed to find most complex functions: { str (e )} " }
6072
73+
6174def analyze_code_relationships (code_finder : CodeFinder , ** args ) -> Dict [str , Any ]:
6275 """Tool to analyze code relationships"""
6376 query_type = args .get ("query_type" )
@@ -74,41 +87,65 @@ def analyze_code_relationships(code_finder: CodeFinder, **args) -> Dict[str, Any
7487 "module_deps" , "variable_scope" , "find_complexity" , "find_functions_by_argument" , "find_functions_by_decorator"
7588 ]
7689 }
77-
90+
7891 try :
7992 debug_log (f"Analyzing relationships: { query_type } for { target } , repo_path={ repo_path } " )
8093 results = code_finder .analyze_code_relationships (query_type , target , context , repo_path = repo_path )
81-
82- return {
94+
95+ # Apply per-query-type limit (falls back to tool-level limit)
96+ limit = get_tool_result_limit (query_type ) or get_tool_result_limit ("analyze_code_relationships" )
97+ truncated = False
98+ if limit and isinstance (results , list ) and len (results ) > limit :
99+ results = results [:limit ]
100+ truncated = True
101+
102+ response = {
83103 "success" : True , "query_type" : query_type , "target" : target ,
84- "context" : context , "results" : results
104+ "context" : context , "results" : results ,
85105 }
86-
106+ if truncated :
107+ response ["result_limit" ] = limit
108+ response ["truncated" ] = True
109+ return response
110+
87111 except Exception as e :
88112 debug_log (f"Error analyzing relationships: { str (e )} " )
89113 return {"error" : f"Failed to analyze relationships: { str (e )} " }
90114
115+
91116def find_code (code_finder : CodeFinder , ** args ) -> Dict [str , Any ]:
92117 """Tool to find relevant code snippets"""
93118 query = args .get ("query" )
94119 DEFAULT_EDIT_DISTANCE = 2
95120 DEFAULT_FUZZY_SEARCH = False
96-
121+
97122 fuzzy_search = args .get ("fuzzy_search" , DEFAULT_FUZZY_SEARCH )
98123 edit_distance = args .get ("edit_distance" , DEFAULT_EDIT_DISTANCE )
99124 repo_path = args .get ("repo_path" )
100125
101126 if fuzzy_search :
102- # Preserve case for Lucene / Levenshtein name matching; lowercasing breaks
103- # camelCase fuzzy hits (see GH #758).
104- query = query .replace ("_" , " " ).strip ()
105-
127+ # For Lucene backends the replace('_', ' ') improves token splitting.
128+ # For portable (Kùzu/FalkorDB) backends _find_by_name_fuzzy_portable
129+ # handles normalisation internally, so we leave the query as-is here.
130+ pass # transformation deferred to find_related_code / _find_by_name_fuzzy_portable
131+
106132 try :
107133 debug_log (f"Finding code for query: { query } with fuzzy_search={ fuzzy_search } , edit_distance={ edit_distance } , repo_path={ repo_path } " )
108134 results = code_finder .find_related_code (query , fuzzy_search , edit_distance , repo_path = repo_path )
109135
110- return {"success" : True , "query" : query , "results" : results }
111-
136+ limit = get_tool_result_limit ("find_code" )
137+ ranked = results .get ("ranked_results" , [])
138+ truncated = False
139+ if limit and len (ranked ) > limit :
140+ ranked = ranked [:limit ]
141+ truncated = True
142+
143+ response = {"success" : True , "query" : query , "results" : {** results , "ranked_results" : ranked }}
144+ if truncated :
145+ response ["result_limit" ] = limit
146+ response ["truncated" ] = True
147+ return response
148+
112149 except Exception as e :
113150 debug_log (f"Error finding code: { str (e )} " )
114151 return {"error" : f"Failed to find code: { str (e )} " }
0 commit comments