From 8f4bc68a6f5ed36423f865cc5585b7e927415e39 Mon Sep 17 00:00:00 2001 From: Michael Panchenko Date: Tue, 3 Jun 2025 17:28:11 +0200 Subject: [PATCH] Agent: remove info on column and name in serialized symbol Fewer tokens and potentially less confusion for LLMs. name_path should be enough for all cases (name is just the last entry) --- src/serena/agent.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/serena/agent.py b/src/serena/agent.py index 8384896f..4240a342 100644 --- a/src/serena/agent.py +++ b/src/serena/agent.py @@ -57,6 +57,22 @@ SUCCESS_RESULT = "OK" DEFAULT_TOOL_TIMEOUT: float = 240 +def _sanitize_symbol_dict(symbol_dict: dict[str, Any]) -> dict[str, Any]: + """ + Sanitize a symbol dictionary inplace by removing unnecessary information. + """ + # We replace the location entry, which repeats line information already included in body_location + # and has unnecessary information on column, by just the relative path. + symbol_dict = copy(symbol_dict) + s_relative_path = symbol_dict.get("location", {}).get("relative_path") + if s_relative_path is not None: + symbol_dict["relative_path"] = s_relative_path + symbol_dict.pop("location", None) + # also remove name, name_path should be enough + symbol_dict.pop("name") + return symbol_dict + + def show_fatal_exception_safe(e: Exception) -> None: """ Shows the given exception in the GUI log viewer on the main thread and ensures that the exception is logged or at @@ -1245,7 +1261,7 @@ class FindSymbolTool(Tool): substring_matching=substring_matching, within_relative_path=relative_path, ) - symbol_dicts = [s.to_dict(kind=True, location=True, depth=depth, include_body=include_body) for s in symbols] + symbol_dicts = [_sanitize_symbol_dict(s.to_dict(kind=True, location=True, depth=depth, include_body=include_body)) for s in symbols] result = json.dumps(symbol_dicts) return self._limit_length(result, max_answer_chars) @@ -1290,6 +1306,7 @@ class FindReferencingSymbolsTool(Tool): reference_dicts = [] for ref in references_in_symbols: ref_dict = ref.symbol.to_dict(kind=True, location=True, depth=0, include_body=include_body) + ref_dict = _sanitize_symbol_dict(ref_dict) if not include_body: ref_relative_path = ref.symbol.location.relative_path assert ref_relative_path is not None, f"Referencing symbol {ref.symbol.name} has no relative path, this is likely a bug."