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(flash): 补齐加速指引运行时异常兜底

wangjiudong242b2728f8c5
diff --git a/jiuwenswarm/agents/harness/flash/rails/flash_turbo_prompt_rail.py b/jiuwenswarm/agents/harness/flash/rails/flash_turbo_prompt_rail.py
index d10a63a9a6..2253bf204d 100644
--- a/jiuwenswarm/agents/harness/flash/rails/flash_turbo_prompt_rail.py
+++ b/jiuwenswarm/agents/harness/flash/rails/flash_turbo_prompt_rail.py
@@ -4,6 +4,8 @@
from __future__ import annotations
+import logging
+
from openjiuwen.core.single_agent.rail.base import AgentCallbackContext
from openjiuwen.harness.prompts import PromptSection
@@ -11,6 +13,8 @@ from jiuwenswarm.agents.harness.common.prompt.prompt_builder import PromptPriori
from jiuwenswarm.server.runtime.skill_turbo.rails.skill_prompt_rail import SkillTurboPromptRail
from jiuwenswarm.server.runtime.skill_turbo.region_edit import region_edit_keyword_summary
+logger = logging.getLogger(__name__)
+
# Stable section/tool identifiers; do not import private implementation constants.
_SECTION_NAME = "skill_turbo_guide"
_SKILL_ACCELERATION_TOOL = "skill_acceleration_exec"
@@ -31,20 +35,28 @@ class FlashTurboPromptRail(SkillTurboPromptRail):
return
# Reuse cached discovery; Flash owns its guide without changing the shared rail.
- from jiuwenswarm.server.runtime.skill_turbo.skill_turbo_tools import (
- refresh_skill_acceleration_description,
- )
+ try:
+ from jiuwenswarm.server.runtime.skill_turbo.skill_turbo_tools import (
+ refresh_skill_acceleration_description,
+ )
- skill_names = refresh_skill_acceleration_description()
+ skill_names = refresh_skill_acceleration_description()
+ except Exception as exc:
+ logger.warning("[FlashTurboPromptRail] refresh description failed: %s", exc)
+ skill_names = []
if not skill_names:
builder.remove_section(_SECTION_NAME)
return
- language = self._resolve_language()
- builder.add_section(PromptSection(
- name=_SECTION_NAME,
- content={language: self._build_guide_text(language, skill_names)},
- priority=self._resolve_priority(_SECTION_NAME, PromptPriority.SKILL_PROTOCOL),
- ))
+ try:
+ language = self._resolve_language()
+ builder.add_section(PromptSection(
+ name=_SECTION_NAME,
+ content={language: self._build_guide_text(language, skill_names)},
+ priority=self._resolve_priority(_SECTION_NAME, PromptPriority.SKILL_PROTOCOL),
+ ))
+ except Exception as exc:
+ logger.warning("[FlashTurboPromptRail] build skill_turbo_guide section failed: %s", exc)
+ builder.remove_section(_SECTION_NAME)
def _build_guide_text(self, language: str, skill_names: list[str]) -> str:
names = "、".join(skill_names) if language == "cn" else ", ".join(skill_names)
diff --git a/tests/unit_tests/agentserver/test_flash_turbo_discovery.py b/tests/unit_tests/agentserver/test_flash_turbo_discovery.py
index 85549b5a79..c064071836 100644
--- a/tests/unit_tests/agentserver/test_flash_turbo_discovery.py
+++ b/tests/unit_tests/agentserver/test_flash_turbo_discovery.py
@@ -392,3 +392,52 @@ async def test_flash_guide_preserves_priority_and_cleans_up(monkeypatch, skill_r
rail.uninit(agent)
assert builder.get_section("skill_turbo_guide") is None
assert rail.system_prompt_builder is None
+
+
[email protected]("failure_stage", ["import", "refresh", "guide", "priority", "add_section"])
+async def test_flash_guide_runtime_failure_cleans_up_and_recovers(monkeypatch, failure_stage):
+ from jiuwenswarm.agents.harness.flash.rails import flash_turbo_prompt_rail
+
+ rail = flash_turbo_prompt_rail.FlashTurboPromptRail()
+ builder = SystemPromptBuilder(language="cn")
+ builder.add_section(PromptSection(
+ name="skill_turbo_guide", content={"cn": "stale"}, priority=321,
+ ))
+ builder.add_section(PromptSection(name="unrelated", content={"cn": "keep"}, priority=322))
+ agent = SimpleNamespace(system_prompt_builder=builder)
+ rail.init(agent)
+ ctx = SimpleNamespace(agent=agent, inputs=SimpleNamespace(tools=[skill_turbo_tools.skill_turbo.card]))
+ monkeypatch.setattr(skill_turbo_tools, "refresh_skill_acceleration_description", Mock(return_value=["pptx-craft"]))
+ error = ImportError("Turbo refresh import failed") if failure_stage == "import" else RuntimeError(
+ "Turbo guide runtime failure",
+ )
+ warning = Mock()
+ monkeypatch.setattr(flash_turbo_prompt_rail.logger, "warning", warning)
+ with monkeypatch.context() as faults:
+ if failure_stage == "import":
+ original_import = builtins.__import__
+
+ def failing_import(name, *args, **kwargs):
+ if name == "jiuwenswarm.server.runtime.skill_turbo.skill_turbo_tools":
+ raise error
+ return original_import(name, *args, **kwargs)
+
+ faults.setattr(builtins, "__import__", failing_import)
+ elif failure_stage == "refresh":
+ faults.setattr(skill_turbo_tools, "refresh_skill_acceleration_description", Mock(side_effect=error))
+ elif failure_stage == "guide":
+ faults.setattr(flash_turbo_prompt_rail, "region_edit_keyword_summary", Mock(side_effect=error))
+ elif failure_stage == "priority":
+ faults.setattr(rail, "_resolve_priority", Mock(side_effect=error))
+ else:
+ faults.setattr(builder, "add_section", Mock(side_effect=error))
+ # Call the rail directly so the SDK cannot mask a missing local exception handler.
+ await rail.before_model_call(ctx)
+ assert builder.get_section("skill_turbo_guide") is None
+ assert builder.get_section("unrelated").content == {"cn": "keep"}
+ message = "refresh description" if failure_stage in ("import", "refresh") else "build skill_turbo_guide section"
+ warning.assert_called_once_with(f"[FlashTurboPromptRail] {message} failed: %s", error)
+ await rail.before_model_call(ctx)
+ assert "pptx-craft" in builder.get_section("skill_turbo_guide").content["cn"]
+ assert warning.call_count == 1