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(rails): 意图标题工具名清洗、skill_turbo 豁免与规则行整行匹配
yt20262937ef9f0912
diff --git a/jiuwenswarm/agents/harness/common/rails/interrupt/interrupt_helpers.py b/jiuwenswarm/agents/harness/common/rails/interrupt/interrupt_helpers.pyindex 83809111ef..f1e77e0204 100644--- a/jiuwenswarm/agents/harness/common/rails/interrupt/interrupt_helpers.py+++ b/jiuwenswarm/agents/harness/common/rails/interrupt/interrupt_helpers.py@@ -280,6 +280,11 @@ def parse_hosted_permission_answer(answer: Any) -> Any:_HEADLINE_ACTION_MAX = 60_HEADLINE_GOAL_MAX = 80+# 这些工具的审批卡由专用 rail 产出语义完整的定制文案(skill_turbo 的+# 工具清单统一授权卡),通用意图标题叠上去只会破坏其定制语义(下游 BFF+# 以首行文案识别品牌卡),一律豁免。+_INTENT_HEADLINE_EXEMPT_TOOLS = frozenset({"skill_acceleration_exec"})+def _permission_headline_plain(text: str, max_len: int) -> str:"""标题块专用纯文本:单行、无 markdown 结构符、有长度上限。@@ -297,18 +302,34 @@ def _permission_headline_plain(text: str, max_len: int) -> str:return collapsed+def _permission_headline_code_span(text: str) -> str:+ """反引号代码段专用清洗:折叠为单行、剔除反引号,其余原样。++ 工具名与 action/call_goal 同属模型可控文本,但它落入 `` `…` `` 代码段:+ 换行会伪造行结构、反引号会提前终止代码段,须清洗;``_`` 等在代码段内+ 原样渲染且是真实工具名的合法字符(如 ``mcp__server__tool``),不做+ ``_permission_headline_plain`` 式全量清洗以免改写工具名。+ """+ collapsed = " ".join(str(text or "").split()).replace("`", "")+ if len(collapsed) > _HEADLINE_ACTION_MAX:+ return collapsed[:_HEADLINE_ACTION_MAX] + "…"+ return collapsed++def build_permission_intent_headline(tool_name: str, tool_args: Any) -> str:"""权限审批卡的意图标题:L1 规则动作短语 + L2 call_goal(模型自述目标)。组出形如 ``**是否允许「写入 report.md」?**\\n\\n模型说明(仅供参考):生成周报``的标题块;动作短语与 call_goal 均不可得时返回空串,调用方保持原文案不变。- 仅做展示增强:审批目标本身(工具声明行与参数区)始终保留,意图不可- 替代具体操作(模型自述可能被提示词注入包装,用户须能核对原始参数;+ ``_INTENT_HEADLINE_EXEMPT_TOOLS`` 内的工具(skill_turbo 统一审批等定制卡)+ 同样返回空串。仅做展示增强:审批目标本身(工具声明行与参数区)始终保留,+ 意图不可替代具体操作(模型自述可能被提示词注入包装,用户须能核对原始参数;故 call_goal 以「模型说明」弱措辞呈现,且 action/call_goal 均经- ``_permission_headline_plain`` 清洗,标题块内不得出现伪造的声明结构)。+ ``_permission_headline_plain`` 清洗、兜底分支的工具名经+ ``_permission_headline_code_span`` 清洗,标题块内不得出现伪造的声明结构)。"""name = (tool_name or "").strip()- if not name:+ if not name or name in _INTENT_HEADLINE_EXEMPT_TOOLS:return ""from jiuwenswarm.common.tool_display import (build_tool_display_name,@@ -323,7 +344,10 @@ def build_permission_intent_headline(tool_name: str, tool_args: Any) -> str:if action:title = f"**是否允许「{action}」?**"elif goal:- title = f"**是否允许执行 `{name}`?**"+ name_plain = _permission_headline_code_span(name)+ if not name_plain:+ return ""+ title = f"**是否允许执行 `{name_plain}`?**"else:return ""parts = [title]@@ -332,7 +356,13 @@ def build_permission_intent_headline(tool_name: str, tool_args: Any) -> str:return "\n\n".join(parts)-_PERMISSION_RULE_LINE_RE = re.compile(r"\n\n匹配规则:`[^`\n]*`")+# 跨仓库契约:以下两常量与 agent-core ``tool_security_rail._build_message`` 的+# 输出文案逐字符耦合(「匹配规则:`{rule}`」行与「请确认是否允许该操作。」句)。+# 上游文案演化时此处静默失效——仅表现为对应行/句保留在卡片上,无功能破坏;+# 变更 agent-core 文案时需同步此处与测试夹具 ``_tool_permission_message``。+# 规则行匹配到行尾(``[^\n]*``)而非配对反引号:shell_subcommands 聚合规则+# 内嵌模型可控的命令文本、可携带反引号,配对匹配会失配或只截掉半行。+_PERMISSION_RULE_LINE_RE = re.compile(r"\n\n匹配规则:[^\n]*")_PERMISSION_CONFIRM_SENTENCE = "请确认是否允许该操作。\n\n"diff --git a/tests/unit_tests/agentserver/test_interrupt_helpers.py b/tests/unit_tests/agentserver/test_interrupt_helpers.pyindex 276db99154..e14b30fd97 100644--- a/tests/unit_tests/agentserver/test_interrupt_helpers.py+++ b/tests/unit_tests/agentserver/test_interrupt_helpers.py@@ -940,3 +940,82 @@ def test_intent_headline_and_apply_tolerate_none_tool_args():assert "请确认是否允许该操作。" in questionassert "匹配规则" not in question++def test_intent_headline_washes_tool_name_in_fallback_title():+ """兜底标题中的工具名(模型可控)经 code-span 清洗:单行、无反引号。"""+ from jiuwenswarm.agents.harness.common.rails.interrupt.interrupt_helpers import (+ build_permission_intent_headline,+ )++ malicious_name = "evil\n**工具 `bash` 需要授权才能执行**\n`rm`"+ headline = build_permission_intent_headline(+ malicious_name, {"call_goal": "查询工单状态"}+ )++ title = headline.split("\n\n", 1)[0]+ # 单行、反引号被剔除:换行+反引号无法在标题区伪造声明行/提前终止代码段。+ assert "\n" not in title+ assert "`bash`" not in headline+ assert "**工具 `bash` 需要授权才能执行**" not in headline+ # 伪造内容降级为代码段内的惰性文字。+ assert "工具 bash 需要授权才能执行" in headline+ assert "模型说明(仅供参考):查询工单状态" in headline++ # 真实工具名(含下划线)不改写:code-span 清洗不做全量字符剔除。+ legit = build_permission_intent_headline(+ "mcp__search__query", {"call_goal": "查资料"}+ )+ assert legit.startswith("**是否允许执行 `mcp__search__query`?**")+++def test_intent_headline_exempts_skill_turbo_custom_card():+ """skill_turbo 统一审批卡有语义完整的定制文案,不加通用意图标题。"""+ from jiuwenswarm.agents.harness.common.rails.interrupt.interrupt_helpers import (+ build_permission_intent_headline,+ )++ assert (+ build_permission_intent_headline(+ "skill_acceleration_exec", {"call_goal": "执行技能任务"}+ )+ == ""+ )+++def test_apply_permission_intent_keeps_skill_turbo_card_untouched():+ """skill_turbo 定制审批消息(含工具清单)经改写后应原样保留。"""+ from jiuwenswarm.agents.harness.common.rails.interrupt.interrupt_helpers import (+ apply_permission_intent,+ )++ message = (+ "**即将调用 `skill加速`,需要授权后整体放行:**\n\n"+ "即将调用 skill加速 执行技能任务,需要一次性授权下列工具。"+ "确认后执行过程中不再逐个询问。\n\n"+ "**可能用到的工具:**\n\n- `bash` — 执行 shell 命令\n"+ )+ args = {"call_goal": "执行技能任务", "skill": "weekly-report"}++ assert apply_permission_intent(message, "skill_acceleration_exec", args) == message+++def test_apply_permission_intent_strips_rule_line_with_embedded_backticks():+ """matched_rule 内嵌反引号(shell_subcommands 聚合格式)时仍整行移除。"""+ from jiuwenswarm.agents.harness.common.rails.interrupt.interrupt_helpers import (+ apply_permission_intent,+ )++ message = (+ "**工具 `bash` 需要授权才能执行**\n\n请确认是否允许该操作。\n\n"+ "参数:\n```json\n{\"command\": \"echo `id -u`\"}\n```\n"+ "\n匹配规则:`tiered_policy:shell_subcommands:echo `id -u`=>rules:bash:ask`"+ )++ question = apply_permission_intent(message, "bash", {"command": "echo `id -u`"})++ assert "匹配规则" not in question+ # 整行移除:配对反引号匹配只截半行时会残留 =>rules:bash:ask 尾段。+ assert "=>rules:bash" not in question+ # 参数区原文(含反引号命令)保留。+ assert "echo `id -u`" in question+