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(jiuwenswarm): resolve_active_office_claw_tool_id 增加清洗名回退,适配 agent-core 工具名出口清洗
renquanjia2fba63cac036
diff --git a/jiuwenswarm/common/mcp_config.py b/jiuwenswarm/common/mcp_config.pyindex 1be1d29c4f..6aa9b3c92b 100644--- a/jiuwenswarm/common/mcp_config.py+++ b/jiuwenswarm/common/mcp_config.py@@ -28,6 +28,15 @@ from openjiuwen.core.common.exception.errors import build_errorfrom openjiuwen.core.common.logging import loggerfrom openjiuwen.core.foundation.tool import McpServerConfig, Tool, ToolCard+try:+ # 较新版本 openjiuwen(agent-core)提供;旧版本无此模块时降级为 None——+ # 此时上游不清洗工具名,清洗名回退匹配无需启用(见+ # resolve_active_office_claw_tool_id),保证 jiuwenswarm 对新旧+ # agent-core 前后兼容、CI 不强依赖上游合入顺序。+ from openjiuwen.core.foundation.tool.name_sanitize import sanitize_llm_tool_name+except ImportError: # pragma: no cover - depends on agent-core revision+ sanitize_llm_tool_name = None # type: ignore[assignment]+try:from openjiuwen.core.foundation.tool.mcp.client import (sse_client as _sse_client, # noqa: F401@@ -1872,6 +1881,25 @@ def resolve_active_office_claw_tool_id(tool_name: str) -> str | None:for tool_id in allowedif tool_id.endswith(suffix) or tool_id.rsplit(".", 1)[-1] == name]+ if not matches and sanitize_llm_tool_name is not None:+ # Fallback for sanitized model-facing names: AbilityManager may expose a+ # raw tool name (embedded after the ``<scope>.<server>.`` part of the id)+ # in an LLM-safe form, e.g. ``aippt.doc_beautify`` -> ``aippt_doc_beautify``+ # (OpenAI-compatible providers reject dots). Scan every dot position so+ # dotted raw names still match their sanitized exposed name. Requires+ # the newer agent-core; without it no sanitized names exist upstream,+ # so exact matching above is authoritative and this stays skipped.+ for tool_id in allowed:+ _, sep, remainder = tool_id.partition(".")+ if not sep or "." not in remainder:+ continue+ position = remainder.find(".")+ while position != -1:+ candidate = remainder[position + 1:]+ if candidate and sanitize_llm_tool_name(candidate) == name:+ matches.append(tool_id)+ break+ position = remainder.find(".", position + 1)if not matches:return Noneif len(matches) == 1:diff --git a/tests/unit_tests/agentserver/test_resolve_active_office_claw_tool_id_sanitized_name.py b/tests/unit_tests/agentserver/test_resolve_active_office_claw_tool_id_sanitized_name.pynew file mode 100644index 0000000000..a462e64d3b--- /dev/null+++ b/tests/unit_tests/agentserver/test_resolve_active_office_claw_tool_id_sanitized_name.py@@ -0,0 +1,67 @@+# coding: utf-8+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.++"""resolve_active_office_claw_tool_id must also match LLM-sanitized tool names.++AbilityManager exposes connector tools whose raw names carry dots (e.g.+``aippt.doc_beautify`` from a 金山文档 connector) in a sanitized, provider-safe+form (``aippt_doc_beautify``). The stale-id recovery path resolves a model-facing+name back to the active registration's tool id, so it must match the sanitized+form against the raw name embedded in the id.++依赖较新版本 openjiuwen(agent-core)的 ``name_sanitize``:旧版本下该模块不存在,+jiuwenswarm 容错降级——不启用回退匹配(上游本不产生清洗名,精确匹配已足够),+相关用例 skip;降级行为用例不依赖新模块,任何版本都执行。+"""++import pytest++import jiuwenswarm.common.mcp_config as mcp_config+from jiuwenswarm.common.mcp_config import (+ bind_active_office_claw_mcp_tools,+ resolve_active_office_claw_tool_id,+)++_HAS_NAME_SANITIZE = mcp_config.sanitize_llm_tool_name is not None+pytestmark = pytest.mark.skipif(+ not _HAS_NAME_SANITIZE,+ reason="requires newer openjiuwen (tool.name_sanitize)",+)+++def test_resolves_sanitized_name_of_dotted_raw_tool():+ owned = "office-claw-request-abc123.金山文档.aippt.doc_beautify"+ with bind_active_office_claw_mcp_tools([owned]):+ assert resolve_active_office_claw_tool_id("aippt_doc_beautify") == owned+ # Raw name lookups keep working unchanged.+ assert resolve_active_office_claw_tool_id("aippt.doc_beautify") == owned+ assert resolve_active_office_claw_tool_id("missing_tool") is None+++def test_resolves_sanitized_name_for_registry_scoped_ids():+ owned = "mcp-registry-request-abc123.金山文档.dbsheet.create_fields"+ with bind_active_office_claw_mcp_tools([owned]):+ assert resolve_active_office_claw_tool_id("dbsheet_create_fields") == owned+++def test_direct_tail_match_still_wins_over_sanitized_scan():+ owned = "office-claw-request-abc123.office-claw.office_claw_preview_scheduled_task"+ with bind_active_office_claw_mcp_tools([owned]):+ assert (+ resolve_active_office_claw_tool_id("office_claw_preview_scheduled_task")+ == owned+ )+++def test_fallback_disabled_without_name_sanitize(monkeypatch):+ """旧版 agent-core 降级行为:回退匹配关闭。++ 直接把模块属性置 None 模拟旧版本导入失败,验证精确匹配不受影响。+ """+ monkeypatch.setattr(mcp_config, "sanitize_llm_tool_name", None)+ owned = "office-claw-request-abc123.金山文档.aippt.doc_beautify"+ with bind_active_office_claw_mcp_tools([owned]):+ # 精确匹配不受影响+ assert resolve_active_office_claw_tool_id("aippt.doc_beautify") == owned+ # 清洗名回退被禁用(上游本来就不产生清洗名)+ assert resolve_active_office_claw_tool_id("aippt_doc_beautify") is None