Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(harness): 让已注册的压缩召回工具留在模型工具表中

xukang2a645b382832
diff --git a/jiuwenswarm/agents/harness/common/rails/progressive_tool_rail.py b/jiuwenswarm/agents/harness/common/rails/progressive_tool_rail.py
index 18897fddb9..71e7180f35 100644
--- a/jiuwenswarm/agents/harness/common/rails/progressive_tool_rail.py
+++ b/jiuwenswarm/agents/harness/common/rails/progressive_tool_rail.py
@@ -4,6 +4,9 @@
- eager_tools: always visible in the model tools schema
- skill-gated tools: direct-visible only while their required skill is active
+- recall_compressed_context: kept in the schema when registered, ungated, and
+ not disabled, even if it is absent from eager_tools. It is not listed as a
+ deferred tool, so navigation does not tell the model that a direct call fails
- deferred_tools: registered at runtime but hidden from schema; accessed via
tools_search + invoke_tool
- disabled_tools / DisabledToolsRail: excluded from schema, navigation,
@@ -59,6 +62,9 @@ logger = logging.getLogger(__name__)
_LOG_PREFIX = "[ProgressiveToolRail]"
_REQUIRED_SKILL_KEY = "required_skill"
+# Registered by agent-core only while compression recall is enabled. Pinning the
+# name here does not register the tool; an input list without it is unchanged.
+_PINNED_SCHEMA_TOOL_NAME = "recall_compressed_context"
_EAGER_DEEPRESEARCH_BINDINGS_KEY = "__eager_deepresearch_bindings__"
_DEEPRESEARCH_CONTEXT_TOOLS = frozenset({
"deepresearch_execute",
@@ -119,6 +125,9 @@ class ProgressiveToolRail(DeepAgentRail):
- eager_tools: always visible in the model tools schema
- skill-gated tools: direct-visible only while their declared skill is active
+ - recall_compressed_context: schema-visible when registered and ungated,
+ ordered after eager and active skill-gated tools. Disabled and skill
+ gates still win. Absent from the input list, it is not invented
- deferred_tools: registered in ability_manager but hidden from schema;
accessed via tools_search (get schema) + invoke_tool (proxy call)
- ordinary tool schema stays stable, maximizing LLM prefix cache
@@ -803,14 +812,23 @@ class ProgressiveToolRail(DeepAgentRail):
if name not in active_gated_names:
active_gated_names.append(name)
continue
- if not lazy_load_active or name in self.eager_tools:
+ if (
+ not lazy_load_active
+ or name in self.eager_tools
+ or name == _PINNED_SCHEMA_TOOL_NAME
+ ):
filtered_tools.append(tool)
if lazy_load_active:
# Canonical eager order — never follow ability_manager / MCP rebind order.
# Mid-task send_file_to_user <-> tools_search swaps break LLM prefix cache.
+ # _order_tools_by_name_list emits every name in the key, so the pinned
+ # tool is appended only when eager and active gates do not already place it.
+ ordered_names = [*self.eager_tools, *active_gated_names]
+ if _PINNED_SCHEMA_TOOL_NAME not in ordered_names:
+ ordered_names.append(_PINNED_SCHEMA_TOOL_NAME)
inputs.tools = self._order_tools_by_name_list(
filtered_tools,
- [*self.eager_tools, *active_gated_names],
+ ordered_names,
)
else:
inputs.tools = filtered_tools
@@ -1115,26 +1133,45 @@ class ProgressiveToolRail(DeepAgentRail):
)
return []
+ def _deferred_tools(self, tools: list[Any], disabled: Iterable[str]) -> list[Any]:
+ """Return tools that stay hidden and are advertised as not directly callable.
+
+ Skill-gated and disabled tools are omitted. The pinned recall tool is
+ schema-visible, so leaving it here would tell the model a direct call fails.
+ """
+ blocked = set(disabled)
+ deferred: list[Any] = []
+ for tool in tools:
+ name = str(getattr(tool, "name", "") or "")
+ declared, _required_skill = _required_skill_requirement(tool)
+ if (
+ not name
+ or declared
+ or name in self.eager_tools
+ or name in blocked
+ or name == _PINNED_SCHEMA_TOOL_NAME
+ ):
+ continue
+ deferred.append(tool)
+ return deferred
+
async def _refresh_deferred_tool_cache(
self, agent: Any = None
) -> None:
"""Refresh cached tool lists from live ability_manager.
``_cached_all_tool_infos`` stays unfiltered for staleness checks.
- Deferred/nav/search/invoke omit both non-eager and disabled tools so
- blacklist entries never appear in the navigation section.
+ Deferred/nav/search/invoke omit disabled tools, skill-gated tools, and
+ the pinned recall tool. The recall tool is schema-visible, so navigation
+ must not describe it as a tool that cannot be called directly.
"""
resolved = agent or self._resolve_runtime_agent()
all_tools = await self._get_all_tool_infos(resolved)
self._cached_all_tool_infos = all_tools
- disabled = self._current_disabled_tools()
- deferred_tools = []
- for tool in all_tools:
- name = str(getattr(tool, "name", "") or "")
- declared, _required_skill = _required_skill_requirement(tool)
- if not declared and name not in self.eager_tools and name not in disabled:
- deferred_tools.append(tool)
- self._cached_deferred_tool_infos = deferred_tools
+ self._cached_deferred_tool_infos = self._deferred_tools(
+ all_tools,
+ self._current_disabled_tools(),
+ )
@staticmethod
def _tool_name_set(tools: list[Any]) -> set[str]:
@@ -1193,13 +1230,10 @@ class ProgressiveToolRail(DeepAgentRail):
):
await self._refresh_deferred_tool_cache(agent)
return
- disabled = self._current_disabled_tools()
- live_deferred = []
- for tool in live_tools:
- name = str(getattr(tool, "name", "") or "")
- declared, _required_skill = _required_skill_requirement(tool)
- if not declared and name not in self.eager_tools and name not in disabled:
- live_deferred.append(tool)
+ live_deferred = self._deferred_tools(
+ live_tools,
+ self._current_disabled_tools(),
+ )
if self._tool_name_set(live_deferred) != self._tool_name_set(
self._cached_deferred_tool_infos
):
diff --git a/tests/unit_tests/agentserver/test_progressive_recall_tool_visibility.py b/tests/unit_tests/agentserver/test_progressive_recall_tool_visibility.py
new file mode 100644
index 0000000000..3768b8af03
--- /dev/null
+++ b/tests/unit_tests/agentserver/test_progressive_recall_tool_visibility.py
@@ -0,0 +1,225 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+
+"""recall_compressed_context stays in the model tools schema once registered."""
+
+from types import SimpleNamespace
+
+import pytest
+from openjiuwen.core.foundation.tool import ToolCard
+
+from openjiuwen.harness.prompts.sections import SectionName
+
+from jiuwenswarm.agents.harness.common.rails.progressive_tool_rail import (
+ ProgressiveToolRail,
+)
+
+_RECALL = "recall_compressed_context"
+_MISSING = object()
+
+
[email protected](autouse=True)
+def _navigation_section_name(monkeypatch: pytest.MonkeyPatch) -> None:
+ """This agent-core build has no ``TOOL_NAVIGATION`` constant.
+
+ The rail still assigns that name when it builds the deferred-tool section.
+ Set it only for these tests and let monkeypatch remove it afterwards, so
+ other modules keep failing on the missing constant instead of inheriting it.
+ """
+ if not hasattr(SectionName, "TOOL_NAVIGATION"):
+ monkeypatch.setattr(
+ SectionName,
+ "TOOL_NAVIGATION",
+ "tool_navigation",
+ raising=False,
+ )
+
+
+def _card(name: str, required_skill: object = _MISSING) -> ToolCard:
+ properties = {}
+ if required_skill is not _MISSING:
+ properties["required_skill"] = required_skill
+ return ToolCard(
+ id=f"id-{name}",
+ name=name,
+ description=f"description-{name}",
+ input_params={},
+ properties=properties,
+ )
+
+
+class _Prompt:
+ """Only ``add_section``. ``has_section`` would touch a section enum this tree lacks."""
+
+ def __init__(self) -> None:
+ self.sections: list[object] = []
+
+ def add_section(self, section: object) -> None:
+ self.sections.append(section)
+
+
+def _agent(cards: list[ToolCard]) -> SimpleNamespace:
+ return SimpleNamespace(
+ ability_manager=SimpleNamespace(list=lambda: list(cards)),
+ system_prompt_builder=_Prompt(),
+ )
+
+
+def _rail(
+ cards: list[ToolCard],
+ *,
+ eager_tools: list[str],
+ disabled_tools: list[str] | None = None,
+ active_skill: str | None = None,
+ enabled: bool = True,
+) -> tuple[ProgressiveToolRail, SimpleNamespace]:
+ agent = _agent(cards)
+ rail = ProgressiveToolRail(
+ enabled=enabled,
+ eager_tools=list(eager_tools),
+ disabled_tools=disabled_tools,
+ active_skill_provider=lambda: active_skill,
+ )
+ rail.init(agent)
+ return rail, agent
+
+
+def _names(tools: list[object]) -> list[str]:
+ return [str(getattr(tool, "name", "") or "") for tool in tools]
+
+
+def _nav_cn(agent: SimpleNamespace) -> str:
+ sections = agent.system_prompt_builder.sections
+ if not sections:
+ return ""
+ content = getattr(sections[0], "content", {})
+ if isinstance(content, dict):
+ return str(content.get("cn") or "")
+ return str(content)
+
+
+async def test_registered_recall_stays_after_eager_and_active_gates() -> None:
+ """Lazy load must keep an ungated recall tool, after eager and skill gates."""
+ read_file = _card("read_file")
+ gated = _card("gated_direct", "research-skill")
+ deferred = _card("ordinary_deferred")
+ recall = _card(_RECALL)
+ cards = [deferred, recall, gated, read_file]
+ rail, agent = _rail(
+ cards,
+ eager_tools=["read_file"],
+ active_skill="research-skill",
+ )
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=list(cards)))
+
+ await rail.before_model_call(ctx)
+
+ assert _names(ctx.inputs.tools) == ["read_file", "gated_direct", _RECALL]
+ nav = _nav_cn(agent)
+ assert "ordinary_deferred" in nav
+ assert _RECALL not in nav
+
+
+async def test_recall_already_in_eager_list_keeps_that_position_once() -> None:
+ read_file = _card("read_file")
+ recall = _card(_RECALL)
+ bash = _card("bash")
+ deferred = _card("ordinary_deferred")
+ cards = [bash, recall, deferred, read_file]
+ rail, agent = _rail(
+ cards,
+ eager_tools=["read_file", _RECALL, "bash"],
+ )
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=list(cards)))
+
+ await rail.before_model_call(ctx)
+
+ assert _names(ctx.inputs.tools) == ["read_file", _RECALL, "bash"]
+ assert _names(ctx.inputs.tools).count(_RECALL) == 1
+
+
+async def test_disabled_recall_is_removed() -> None:
+ read_file = _card("read_file")
+ recall = _card(_RECALL)
+ cards = [read_file, recall]
+ rail, agent = _rail(
+ cards,
+ eager_tools=["read_file"],
+ disabled_tools=[_RECALL],
+ )
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=list(cards)))
+
+ await rail.before_model_call(ctx)
+
+ assert _names(ctx.inputs.tools) == ["read_file"]
+ assert _RECALL not in _nav_cn(agent)
+
+
+async def test_skill_gated_recall_stays_hidden_until_its_skill_is_active() -> None:
+ read_file = _card("read_file")
+ other = _card("other_gated", "research-skill")
+ recall = _card(_RECALL, "research-skill")
+ cards = [recall, other, read_file]
+ hidden_rail, hidden_agent = _rail(
+ cards,
+ eager_tools=["read_file"],
+ active_skill=None,
+ )
+ hidden = SimpleNamespace(
+ agent=hidden_agent,
+ inputs=SimpleNamespace(tools=list(cards)),
+ )
+
+ await hidden_rail.before_model_call(hidden)
+
+ assert _names(hidden.inputs.tools) == ["read_file"]
+
+ shown_rail, shown_agent = _rail(
+ cards,
+ eager_tools=["read_file"],
+ active_skill="research-skill",
+ )
+ shown = SimpleNamespace(
+ agent=shown_agent,
+ inputs=SimpleNamespace(tools=list(cards)),
+ )
+
+ await shown_rail.before_model_call(shown)
+
+ assert _names(shown.inputs.tools) == ["read_file", _RECALL, "other_gated"]
+ assert _names(shown.inputs.tools).count(_RECALL) == 1
+
+
+async def test_absent_recall_tool_is_not_invented() -> None:
+ read_file = _card("read_file")
+ deferred = _card("ordinary_deferred")
+ cards = [deferred, read_file]
+ rail, agent = _rail(cards, eager_tools=["read_file"])
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=list(cards)))
+
+ await rail.before_model_call(ctx)
+
+ assert _names(ctx.inputs.tools) == ["read_file"]
+ assert _RECALL not in _nav_cn(agent)
+
+
+async def test_lazy_load_off_keeps_input_order_for_non_disabled_tools() -> None:
+ read_file = _card("read_file")
+ deferred = _card("ordinary_deferred")
+ recall = _card(_RECALL)
+ cards = [recall, deferred, read_file]
+ rail, agent = _rail(
+ cards,
+ eager_tools=["read_file"],
+ enabled=False,
+ )
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=list(cards)))
+
+ await rail.before_model_call(ctx)
+
+ assert _names(ctx.inputs.tools) == [_RECALL, "ordinary_deferred", "read_file"]