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(skill): 多行复合命令执行位识别 + 过期配置装配链修复(N1/N2/N3)

ZHan995032fd1c357d
diff --git a/jiuwenswarm/agents/harness/common/rails/skill_active_state.py b/jiuwenswarm/agents/harness/common/rails/skill_active_state.py
index 81f81d02c5..a6165ef7ff 100644
--- a/jiuwenswarm/agents/harness/common/rails/skill_active_state.py
+++ b/jiuwenswarm/agents/harness/common/rails/skill_active_state.py
@@ -155,27 +155,34 @@ def _is_interrupt_resume_invoke(ctx: AgentCallbackContext) -> bool:
def resolve_stale_invoke_limit(config: Any) -> Optional[int]:
"""从 agent 配置读取过期兜底轮数(CR-3b)。
- 读取 ``config["react"]["skill_stale_invoke_limit"]``:缺失/类型无效
- 返回 None(用 rail 默认值 5);``<= 0`` 原样透传(显式禁用兜底)。
+ 读取 ``skill_stale_invoke_limit``,兼容两种 config 形态(N2):
+ 完整 agent 配置(``config["react"][key]``)与 react 子 dict
+ (``config[key]``,reload 路径传入的形态)。缺失/类型无效返回
+ None(用 rail 默认值 5);``<= 0`` 原样透传(显式禁用兜底)。
装配点(interface_deep._build_skill_active_state_rail)消费。
"""
if not isinstance(config, dict):
return None
+ sections = [config]
react = config.get("react")
- if not isinstance(react, dict) or "skill_stale_invoke_limit" not in react:
- return None
- raw = react.get("skill_stale_invoke_limit")
- if raw is None:
- return None
- try:
- return int(raw)
- except (TypeError, ValueError):
- logger.warning(
- "[SkillActiveStateRail] invalid skill_stale_invoke_limit=%r, "
- "fallback to default",
- raw,
- )
- return None
+ if isinstance(react, dict):
+ sections.insert(0, react)
+ for section in sections:
+ if "skill_stale_invoke_limit" not in section:
+ continue
+ raw = section.get("skill_stale_invoke_limit")
+ if raw is None:
+ return None
+ try:
+ return int(raw)
+ except (TypeError, ValueError):
+ logger.warning(
+ "[SkillActiveStateRail] invalid skill_stale_invoke_limit=%r, "
+ "fallback to default",
+ raw,
+ )
+ return None
+ return None
def _extract_session_id(ctx: AgentCallbackContext) -> Optional[str]:
@@ -275,6 +282,11 @@ class SkillActiveStateRail(DeepAgentRail):
stale_invoke_limit = self.DEFAULT_STALE_INVOKE_LIMIT
self._stale_invoke_limit = int(stale_invoke_limit)
+ @property
+ def stale_invoke_limit(self) -> int:
+ """当前过期兜底轮数(公开只读,供装配点检测配置变化,N2)。"""
+ return self._stale_invoke_limit
+
def _resolve_session_id(self, ctx: AgentCallbackContext) -> str:
return resolve_skill_session_id(ctx, self._preset_session_id)
diff --git a/jiuwenswarm/agents/harness/common/rails/skill_credential_injection_rail.py b/jiuwenswarm/agents/harness/common/rails/skill_credential_injection_rail.py
index 92288a02ce..3934571aca 100644
--- a/jiuwenswarm/agents/harness/common/rails/skill_credential_injection_rail.py
+++ b/jiuwenswarm/agents/harness/common/rails/skill_credential_injection_rail.py
@@ -87,10 +87,12 @@ _TIMEOUT_DURATION_RE = re.compile(r"^[\d.]+[smh]?$")
def _split_subcommands(command: str) -> list[str]:
- """按引号外的 ``&& / || / ; / | / &`` 拆子命令。
+ """按引号外的 ``&& / || / ; / | / & / 换行`` 拆子命令。
引号内的分隔符不拆(``sed 's|/path/x.py|X|'`` 的 ``|`` 是 s 命令
分隔符);``2>&1`` 等重定向的 ``&``(前字符为 ``>``)不拆。
+ 换行(含 ``\\r\\n`` 残留的 ``\\r``)与 ``;`` 同为顺序执行分隔符,
+ 多行复合命令(N1)必须逐行识别,否则第二行脚本绕过执行位判定。
"""
parts: list[str] = []
buf: list[str] = []
@@ -115,7 +117,8 @@ def _split_subcommands(command: str) -> list[str]:
sep_len = 0
if command[i:i + 2] in ("&&", "||"):
sep_len = 2
- elif ch in (";", "|", "&"):
+ elif ch in ("\n", "\r", ";", "|", "&"):
+ # 换行与 ; 同为顺序执行分隔符(多行复合命令,N1);
# 2>&1 等重定向的 & 不拆(前一个字符是 > )
if ch == "&" and i > 0 and command[i - 1] == ">":
sep_len = 0
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface.py b/jiuwenswarm/server/runtime/agent_adapter/interface.py
index f8faa8564b..d8f88e132b 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface.py
@@ -80,6 +80,9 @@ from jiuwenswarm.agents.harness.common.rails.interrupt.interrupt_helpers import
EVOLUTION_INTERRUPT_METADATA_SOURCES,
is_interrupt_resume_payload,
)
+from jiuwenswarm.agents.harness.common.rails.skill_active_state import (
+ _CHAT_SEND_SOURCE_EXTRA_KEY,
+)
PLAN_REMINDER_ORIGINAL_QUERY_KEY = getattr(
_plan_approval,
@@ -1757,10 +1760,6 @@ class JiuWenSwarm:
# permission/confirm/ask_user 恢复轮——此类轮次是同一任务的继续,
# 不计入 active_skill 过期计数。
try:
- from jiuwenswarm.agents.harness.common.rails.skill_active_state import (
- _CHAT_SEND_SOURCE_EXTRA_KEY,
- )
-
chat_send_source = (
params.get("source").strip()
if isinstance(params.get("source"), str) and params.get("source").strip()
@@ -1782,8 +1781,11 @@ class JiuWenSwarm:
extra[_CHAT_SEND_SOURCE_EXTRA_KEY] = chat_send_source
extra["chat_send_source"] = chat_send_source
except Exception:
- logger.debug(
- "[_build_inputs] failed to attach chat_send_source",
+ # N3:透传失败 = HITL 恢复轮回到「计入过期计数」的旧行为,
+ # 生产不可静默——warning 级留痕便于倒查。
+ logger.warning(
+ "[_build_inputs] failed to attach chat_send_source; "
+ "HITL resume invokes will count toward skill stale limit",
exc_info=True,
)
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index 2c9e0e3322..e3e2353170 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -9439,7 +9439,11 @@ class JiuWenSwarmDeepAdapter:
)
rail_infos.insert(
1,
- _RailBuildInfo("_skill_active_state_rail", self._build_skill_active_state_rail),
+ _RailBuildInfo(
+ "_skill_active_state_rail",
+ self._build_skill_active_state_rail,
+ {"config": config},
+ ),
)
rail_infos.append(
@@ -9781,8 +9785,17 @@ class JiuWenSwarmDeepAdapter:
if self._skill_credential_injection_rail is not None:
skill_credential_rail_newly_created = True
- if self._skill_active_state_rail is None:
- self._skill_active_state_rail = self._build_skill_active_state_rail(config)
+ # reload 路径的 config 是 react 子 dict;limit 配置变化时重建 rail,
+ # 使已存在会话的过期兜底随配置生效(N2,计数存于模块级状态,重建无副作用)
+ new_stale_limit = resolve_stale_invoke_limit(config)
+ if new_stale_limit is None:
+ new_stale_limit = SkillActiveStateRail.DEFAULT_STALE_INVOKE_LIMIT
+ if self._skill_active_state_rail is None or (
+ self._skill_active_state_rail.stale_invoke_limit != new_stale_limit
+ ):
+ self._skill_active_state_rail = self._build_skill_active_state_rail(
+ config
+ )
progressive_tool_rail = None
if "tool_lazy_load" in config:
diff --git a/tests/unit_tests/agentserver/test_skill_active_state.py b/tests/unit_tests/agentserver/test_skill_active_state.py
index caedc1c150..71c0af7a2f 100644
--- a/tests/unit_tests/agentserver/test_skill_active_state.py
+++ b/tests/unit_tests/agentserver/test_skill_active_state.py
@@ -462,6 +462,35 @@ class TestResolveStaleInvokeLimit(unittest.TestCase):
is None
)
+ def test_react_section_form_accepted(self):
+ # N2:reload 路径传入的 config 本身即 react 子 dict,顶层 key 直接命中
+ assert (
+ resolve_stale_invoke_limit({"skill_stale_invoke_limit": 10}) == 10
+ )
+ assert resolve_stale_invoke_limit({"other": 1}) is None
+
+ def test_full_config_form_takes_precedence(self):
+ # 完整 agent 配置形态:react 子 dict 优先于顶层同名 key
+ assert (
+ resolve_stale_invoke_limit(
+ {
+ "skill_stale_invoke_limit": 99,
+ "react": {"skill_stale_invoke_limit": 7},
+ }
+ )
+ == 7
+ )
+
+ def test_rail_stale_limit_property(self):
+ # N2:公开只读属性,供装配点检测配置变化(默认 5 / 传参 / 禁用 0)
+ assert SkillActiveStateRail().stale_invoke_limit == 5
+ assert (
+ SkillActiveStateRail(stale_invoke_limit=10).stale_invoke_limit == 10
+ )
+ assert (
+ SkillActiveStateRail(stale_invoke_limit=0).stale_invoke_limit == 0
+ )
+
class TestAdoptDefaultActiveSkill(unittest.TestCase):
def tearDown(self):
diff --git a/tests/unit_tests/agentserver/test_skill_credential_injection_rail.py b/tests/unit_tests/agentserver/test_skill_credential_injection_rail.py
index 43236ddf12..f97932b1f0 100644
--- a/tests/unit_tests/agentserver/test_skill_credential_injection_rail.py
+++ b/tests/unit_tests/agentserver/test_skill_credential_injection_rail.py
@@ -303,6 +303,20 @@ class TestMatchSkillInCommand(unittest.TestCase):
cmd = "python3 /opt/skills/hwocr/scripts/run.py 查询 --no-save 2>&1"
assert match_skill_in_command(cmd, self.KNOWN) == "hwocr"
+ def test_multiline_compound_commands_matched(self):
+ # N1:换行与 ; 同为顺序执行分隔符,多行复合命令必须逐行识别
+ assert match_skills_in_command(
+ "python3 /a/hwocr/x.py\nnode /b/ppt-creation/y.js", self.KNOWN
+ ) == ["hwocr", "ppt-creation"]
+ # cd 行无执行位,第二行脚本正常命中
+ assert match_skills_in_command(
+ "cd /a/hwocr\nnode /b/ppt-creation/y.js", self.KNOWN
+ ) == ["ppt-creation"]
+ # \r\n 残留形态同样逐行识别
+ assert match_skills_in_command(
+ "python3 /a/hwocr/x.py\r\nnode /b/ppt-creation/y.js", self.KNOWN
+ ) == ["hwocr", "ppt-creation"]
+
class TestSkillGateAndFallback(unittest.TestCase):
def _make_rail(self):
@@ -490,6 +504,25 @@ class TestSkillGateAndFallback(unittest.TestCase):
assert "拆分" in message
assert "env" not in ctx.inputs.tool_args
+ @patch(
+ "jiuwenswarm.agents.harness.common.rails.skill_credential_injection_rail.get_session_active_skill"
+ )
+ def test_multiline_multi_skill_blocked_with_split_guidance(self, mock_get_skill):
+ # N1:多行复合命令逐行识别——多技能执行位全部命中,拆分指引拦截
+ mock_get_skill.return_value = None
+ rail = self._make_rail()
+ ctx = self._make_ctx(
+ tool_args={
+ "command": "python3 /a/hwocr/x.py\nnode /b/ppt-creation/y.js"
+ }
+ )
+ asyncio.run(rail.before_tool_call(ctx))
+ assert ctx.extra.get("_skip_tool") is True
+ message = str(ctx.inputs.tool_result)
+ assert "hwocr" in message and "ppt-creation" in message
+ assert "拆分" in message
+ assert "env" not in ctx.inputs.tool_args
+
@patch(
"jiuwenswarm.agents.harness.common.rails.skill_credential_injection_rail.get_session_active_skill"
)