refactor(tools): consolidate duplicated _truncate and get_mcp_manager into src/tool_utils (#3478)

* refactor(tools): consolidate duplicated _truncate and get_mcp_manager into src/tool_utils

Move all copies of _truncate(), get_mcp_manager(), and set_mcp_manager()
into a single leaf module (src/tool_utils.py) that imports only from
src.constants. This eliminates the lazy-import hack
('from src import agent_tools' inside function bodies) in tool_execution.py
and tool_implementations.py, and fixes a latent bug: the _truncate copy in
tool_execution.py was missing the isinstance guard and would crash on None.

Also deletes mcp_servers/_common.py — it was dead code with zero callers
anywhere in the codebase, containing its own copy of truncate() and
constants that already exist in src/constants.py.

* fix(tools): route remaining get_mcp_manager imports to src.tool_utils

The maintainer's feedback flagged src/task_scheduler.py:1857 and
routes/task_routes.py:977. A project-wide search found a third call site
in src/agent_loop.py that also imported get_mcp_manager from
src.agent_tools instead of src.tool_utils.

All three are now sourced from the canonical location in src.tool_utils.

---------

Co-authored-by: mcnoliveira <mcnoliveira@gmail.com>
This commit is contained in:
Mateus Oliveira
2026-06-08 20:05:30 -03:00
committed by GitHub
parent 62ffcb6236
commit f7ae85590b
10 changed files with 78 additions and 97 deletions
+9 -19
View File
@@ -1,27 +1,17 @@
"""Regression: the shared MCP truncate() must tolerate non-string input."""
import importlib.machinery
import importlib.util
from pathlib import Path
"""Canonical _truncate must tolerate non-string input (regression).
_PATH = Path(__file__).resolve().parents[1] / "mcp_servers" / "_common.py"
def _load():
loader = importlib.machinery.SourceFileLoader("odysseus_mcp_common", str(_PATH))
spec = importlib.util.spec_from_loader(loader.name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
Originally this tested mcp_servers/_common.py's copy, which was deleted
since it had zero callers. Now it tests the canonical version.
"""
from src.tool_utils import _truncate
def test_truncate_handles_none_and_nonstring():
c = _load()
assert c.truncate(None) == ""
assert c.truncate(12345) == "12345"
assert _truncate(None) == "" # pyright: ignore[reportArgumentType]
assert _truncate(12345) == "12345" # pyright: ignore[reportArgumentType]
def test_truncate_string_behaviour_unchanged():
c = _load()
assert c.truncate("hello", limit=100) == "hello"
out = c.truncate("x" * 50, limit=10)
assert _truncate("hello", limit=100) == "hello"
out = _truncate("x" * 50, limit=10)
assert out.startswith("x" * 10) and "truncated" in out
+22
View File
@@ -0,0 +1,22 @@
"""Verify src.tool_utils has no project imports beyond src.constants.
If someone adds an import from src.settings, src.database, or any other
project module inside tool_utils.py, the circular import that this module
exists to break will silently return a partially-initialized module.
This test catches that statically.
"""
import ast
import pathlib
def test_tool_utils_has_no_project_imports():
src = pathlib.Path("src/tool_utils.py").read_text()
tree = ast.parse(src)
for node in ast.walk(tree):
if isinstance(node, (ast.Import, ast.ImportFrom)):
if isinstance(node, ast.ImportFrom) and node.module:
msg = f"Illegal project import in tool_utils.py: {node.module}"
assert node.module in ("src.constants",) or not node.module.startswith(
"src."
), msg