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(agent): sync runtime attachment with live mode
l_x_decbe9b149e51
diff --git a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.pyindex 42f6ba80fb..77699d3051 100644--- a/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/runtime_prompt_rail.py@@ -44,6 +44,7 @@ class RuntimePromptRail(DeepAgentRail):timezone_offset: int = 8,) -> None:super().__init__()+ self._agent = Noneself.system_prompt_builder = Noneself.attachment_manager = Noneself._language = language@@ -59,6 +60,7 @@ class RuntimePromptRail(DeepAgentRail):def init(self, agent) -> None:"""从 agent 获取 system_prompt_builder 引用。"""+ self._agent = agentself.system_prompt_builder = getattr(agent, "system_prompt_builder", None)self.attachment_manager = getattr(agent, "prompt_attachment_manager", None)@@ -72,6 +74,7 @@ class RuntimePromptRail(DeepAgentRail):self.system_prompt_builder.remove_section("browser_tool_policy")self.system_prompt_builder.remove_section("tui_current_project_policy")self.system_prompt_builder.remove_section("trusted_dirs_policy")+ self._agent = Noneself.system_prompt_builder = Noneself.attachment_manager = None@@ -181,6 +184,41 @@ class RuntimePromptRail(DeepAgentRail):logger.debug("Failed to read configured model names: %s", exc)return []+ def _resolve_current_mode(+ self,+ ctx: AgentCallbackContext,+ configured_mode: str,+ ) -> str:+ """用 DeepAgent session state 覆盖 code 模式的请求初始快照。"""+ if configured_mode not in {"code", "code.normal", "code.plan"}:+ return configured_mode++ agent = self._agent or ctx.agent+ load_state = getattr(agent, "load_state", None)+ if not callable(load_state) or ctx.session is None:+ return configured_mode++ try:+ state = load_state(ctx.session)+ plan_state = getattr(state, "plan_mode", None)+ if isinstance(plan_state, dict):+ plan_mode = plan_state.get("mode")+ else:+ plan_mode = getattr(plan_state, "mode", None)+ except Exception as exc:+ logger.debug(+ "[RuntimePromptRail] Failed to resolve live agent mode: %s",+ exc,+ )+ return configured_mode++ normalized = str(plan_mode or "").strip().lower()+ if normalized == "plan":+ return "code.plan"+ if normalized in {"normal", "auto"}:+ return "code.normal"+ return configured_mode+async def before_model_call(self, ctx: AgentCallbackContext) -> None:if not self.system_prompt_builder:return@@ -198,13 +236,13 @@ class RuntimePromptRail(DeepAgentRail):# ── time ──if not self._force_english and self._language == "cn":time_content = (- f"# 时间说明\n\n"+ "# 时间说明\n\n""- 当用户询问“最新、当前、今年、本年、实时、近期”等信息并需要搜索时,""搜索 query 必须优先使用当前年份或日期")else:time_content = (- f"# Time Description\n\n"+ "# Time Description\n\n""- When the user asks for latest/current/this-year/recent information and search is needed, ""search queries must prefer the current year or date.")@@ -245,7 +283,10 @@ class RuntimePromptRail(DeepAgentRail):or "unknown").strip()available_models_str = ", ".join(available_models) if available_models else model- mode = (runtime_state.get("mode") or self._mode or "unknown").strip()+ configured_mode = str(+ runtime_state.get("mode") or self._mode or "unknown"+ ).strip()+ mode = self._resolve_current_mode(ctx, configured_mode)# Language section controls the model's *response* language and must# follow the user's preferred language. ``_force_english`` only# affects system-prompt scaffolding (time / runtime / env sections),diff --git a/tests/unit_tests/agentserver/test_system_prompt_restructure.py b/tests/unit_tests/agentserver/test_system_prompt_restructure.pyindex 82598775f4..40386b7ae0 100644--- a/tests/unit_tests/agentserver/test_system_prompt_restructure.py+++ b/tests/unit_tests/agentserver/test_system_prompt_restructure.py@@ -52,6 +52,17 @@ class _FakeAgent:self.prompt_attachment_manager = PromptAttachmentManager()+class _FakeLiveModeAgent(_FakeAgent):+ def __init__(self, builder: SystemPromptBuilder, mode: str) -> None:+ super().__init__(builder)+ self.mode = mode++ def load_state(self, session):+ return SimpleNamespace(+ plan_mode=SimpleNamespace(mode=self.mode),+ )++class _FakeAbilityManager:def __init__(self) -> None:self._items = {@@ -503,6 +514,44 @@ async def test_runtime_dynamic_sections_go_to_prompt_attachment_when_manager_avaassert "# Browser Tool Policy" in prompt+async def test_runtime_attachment_tracks_live_code_agent_mode(tmp_path, monkeypatch):+ monkeypatch.setattr(_utils_mod, "get_config_dir", lambda: tmp_path)+ runtime_state = tmp_path / "runtime_state" / "default.yaml"+ runtime_state.parent.mkdir(parents=True, exist_ok=True)+ runtime_state.write_text(+ "model: model-x\n"+ "available_models:\n"+ " - model-x\n"+ "mode: code.normal\n",+ encoding="utf-8",+ )+ builder = SystemPromptBuilder(language="en")+ agent = _FakeLiveModeAgent(builder, mode="plan")+ runtime_rail = RuntimePromptRail(language="en", channel="tui")+ runtime_rail.init(agent)+ ctx = AgentCallbackContext(+ # Inner ReactAgent callbacks do not expose DeepAgent.load_state().+ agent=SimpleNamespace(),+ inputs=None,+ session=_FakeSession(),+ extra={},+ )++ await runtime_rail.before_model_call(ctx)+ items = await agent.prompt_attachment_manager.collect_for_session("sess1")+ rendered = agent.prompt_attachment_manager.render(items)+ assert "Current mode: code.plan" in rendered+ assert "Current mode: code.normal" not in rendered++ agent.mode = "normal"+ await runtime_rail.before_model_call(ctx)+ items = await agent.prompt_attachment_manager.collect_for_session("sess1")+ rendered = agent.prompt_attachment_manager.render(items)+ assert "Current mode: code.normal" in rendered+ assert "Current mode: code.plan" not in rendered++@pytest.mark.asyncioasync def test_runtime_git_status_attachment_clears_when_git_context_disappears(tmp_path, monkeypatch):monkeypatch.setattr(_utils_mod, "get_config_dir", lambda: tmp_path)