From e77f1ed5f7c294c762e5f29dd12f7d5a69fe5c08 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Thu, 21 May 2026 19:23:14 -0700 Subject: [PATCH] fix(agent): widen toolset gate to context engine tools (#5544 sibling) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The memory-provider gate added in the prior commit closes one of two blind-injection sites in agent_init.py. The context engine block (lines ~1445) follows the identical pattern: agent.context_compressor.get_tool_schemas() (lcm_grep, lcm_describe, lcm_expand) was appended to agent.tools unconditionally, ignoring enabled_toolsets. Same bug class, same local-model latency penalty, same one-line gate — using 'context_engine' as the toolset name (matches the existing plugin-system convention in plugins.py, plugins_cmd.py, etc.). Also adds Lempkey to scripts/release.py AUTHOR_MAP for the prior commit's authorship. --- agent/agent_init.py | 16 +++++- scripts/release.py | 2 + tests/agent/test_memory_provider.py | 89 +++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/agent/agent_init.py b/agent/agent_init.py index db6d447681d..512e7a47160 100644 --- a/agent/agent_init.py +++ b/agent/agent_init.py @@ -1446,8 +1446,22 @@ def init_agent( # errors. Even with the cache fix, dedup is the right defense # against plugin paths that may register the same schemas via # ctx.register_tool(). Mirrors the memory tools dedup above. + # + # Respect the platform's enabled_toolsets configuration (#5544): + # context engine tools follow the same gating pattern as memory + # provider tools — without the gate, `platform_toolsets: telegram: []` + # would still leak lcm_* tools into the tool surface and incur the + # same local-model latency penalty. agent._context_engine_tool_names: set = set() - if hasattr(agent, "context_compressor") and agent.context_compressor and agent.tools is not None: + if ( + hasattr(agent, "context_compressor") + and agent.context_compressor + and agent.tools is not None + and ( + agent.enabled_toolsets is None + or "context_engine" in agent.enabled_toolsets + ) + ): _existing_tool_names = { t.get("function", {}).get("name") for t in agent.tools diff --git a/scripts/release.py b/scripts/release.py index 27f9fbd8f98..067d61328cc 100755 --- a/scripts/release.py +++ b/scripts/release.py @@ -68,6 +68,8 @@ AUTHOR_MAP = { "mr.aashiz@gmail.com": "aashizpoudel", "70629228+shaun0927@users.noreply.github.com": "shaun0927", "98262967+Bihruze@users.noreply.github.com": "Bihruze", + "189280367+Lempkey@users.noreply.github.com": "Lempkey", + "leovillalbajr@gmail.com": "Lempkey", "nidhi2894@gmail.com": "nidhi-singh02", "30312689+aashizpoudel@users.noreply.github.com": "aashizpoudel", "oleksii.lisikh@gmail.com": "olisikh", diff --git a/tests/agent/test_memory_provider.py b/tests/agent/test_memory_provider.py index d58016bac52..6f8cfc8a93d 100644 --- a/tests/agent/test_memory_provider.py +++ b/tests/agent/test_memory_provider.py @@ -1159,3 +1159,92 @@ class TestMemoryToolToolsetGate: mgr = self._mgr_with_tools("fact_store", "memory_search", "memory_add") tools, names = self._run_memory_injection(None, mgr) assert names == {"fact_store", "memory_search", "memory_add"} + + +class TestContextEngineToolsetGate: + """Issue #5544 (sibling): context engine tools follow the same gate. + + `agent.context_compressor.get_tool_schemas()` (e.g. lcm_grep, lcm_describe, + lcm_expand) was appended to AIAgent.tools unconditionally. Same blind + injection class as the memory bug; same local-model penalty. Gate name: + "context_engine" (matches the existing plugin-system convention). + """ + + @staticmethod + def _run_context_engine_injection(enabled_toolsets, compressor): + """Simulate the gated context-engine injection block from agent_init.py.""" + tools = [] + valid_tool_names = set() + engine_tool_names = set() + + if ( + compressor is not None + and tools is not None + and ( + enabled_toolsets is None + or "context_engine" in enabled_toolsets + ) + ): + _existing = { + t.get("function", {}).get("name") + for t in tools + if isinstance(t, dict) + } + for _schema in compressor.get_tool_schemas(): + _tname = _schema.get("name", "") + if _tname and _tname in _existing: + continue + tools.append({"type": "function", "function": _schema}) + if _tname: + valid_tool_names.add(_tname) + engine_tool_names.add(_tname) + _existing.add(_tname) + + return tools, valid_tool_names, engine_tool_names + + class _FakeCompressor: + def __init__(self, schemas): + self._schemas = schemas + + def get_tool_schemas(self): + return list(self._schemas) + + def _compressor_with(self, *tool_names): + return self._FakeCompressor( + [{"name": n, "description": n, "parameters": {}} for n in tool_names] + ) + + def test_none_toolsets_injects(self): + """enabled_toolsets=None injects context-engine tools — backward compat.""" + c = self._compressor_with("lcm_grep", "lcm_describe", "lcm_expand") + tools, names, engine_names = self._run_context_engine_injection(None, c) + assert engine_names == {"lcm_grep", "lcm_describe", "lcm_expand"} + + def test_context_engine_in_toolsets_injects(self): + """enabled_toolsets including 'context_engine' injects the tools.""" + c = self._compressor_with("lcm_grep") + tools, names, engine_names = self._run_context_engine_injection( + ["terminal", "context_engine"], c + ) + assert "lcm_grep" in engine_names + + def test_empty_toolsets_blocks_injection(self): + """`platform_toolsets: telegram: []` must suppress context-engine tools.""" + c = self._compressor_with("lcm_grep") + tools, names, engine_names = self._run_context_engine_injection([], c) + assert tools == [] + assert engine_names == set() + + def test_toolsets_without_context_engine_blocks_injection(self): + """A toolset list that doesn't name 'context_engine' suppresses injection.""" + c = self._compressor_with("lcm_grep", "lcm_describe") + tools, names, engine_names = self._run_context_engine_injection( + ["terminal", "memory"], c + ) + assert tools == [] + assert engine_names == set() + + def test_no_compressor_no_injection(self): + """Gate is moot without a context_compressor.""" + tools, names, engine_names = self._run_context_engine_injection(None, None) + assert tools == []