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(agentserver): address runtime SSE review findings
guozirui14a0c13292563
diff --git a/jiuwenswarm/server/app_agentserver.py b/jiuwenswarm/server/app_agentserver.pyindex 354bf969f4..298d57fb54 100644--- a/jiuwenswarm/server/app_agentserver.py+++ b/jiuwenswarm/server/app_agentserver.py@@ -192,8 +192,19 @@ def _should_apply_sse_invoke_patch() -> bool:return "xiaoyi_claw" in modes-if _should_apply_sse_invoke_patch():+def refresh_openai_response_assembly_patch() -> bool:+ """Apply the SSE response assembly patch when the current config needs it.++ The patch is idempotent, so this can be called both at process startup and+ after a runtime config reload that enables ``xiaoyi_claw``.+ """+ if not _should_apply_sse_invoke_patch():+ return Falseapply_openai_response_assembly_patch()+ return True+++refresh_openai_response_assembly_patch()# [PERF 实验] 企业版 + symphony 关闭时短路 evolution rail 轨迹 drainfrom jiuwenswarm.perf.evolution_rail_short_circuit import (diff --git a/jiuwenswarm/server/handlers/ops.py b/jiuwenswarm/server/handlers/ops.pyindex 23327db926..33ef8f7ae8 100644--- a/jiuwenswarm/server/handlers/ops.py+++ b/jiuwenswarm/server/handlers/ops.py@@ -193,6 +193,14 @@ async def handle_agent_reload_config(ctx: RequestContext) -> None:**reload_kwargs,)+ # ``xiaoyi_claw`` can be enabled without restarting agent server.+ # Re-evaluate the gate after every config reload; the patch is idempotent.+ from jiuwenswarm.server.app_agentserver import (+ refresh_openai_response_assembly_patch,+ )++ refresh_openai_response_assembly_patch()+# Hot-reload ProactiveEngine config if availableshould_reload_proactive = not reload_scopes or bool(reload_scopes & {"model", "proactive", "agent_runtime"})if ctx.services.proactive_engine is not None and should_reload_proactive:diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.pyindex e1b5124025..d20d2908f4 100644--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py@@ -2934,6 +2934,7 @@ async def _consume_stream_with_query(session_id,{"event_type": "chat.error",+ "code": "team_stream_error","error": "Team stream ended with no output (possible pool/DB inconsistency or internal error)","session_id": session_id,},@@ -2967,6 +2968,7 @@ async def _consume_stream_with_query(session_id,{"event_type": "chat.error",+ "code": "team_stream_error","error": str(exc),"session_id": session_id,},diff --git a/tests/unit_tests/agentserver/test_agent_reload_scope.py b/tests/unit_tests/agentserver/test_agent_reload_scope.pyindex e23f3436f4..a79bc67abe 100644--- a/tests/unit_tests/agentserver/test_agent_reload_scope.py+++ b/tests/unit_tests/agentserver/test_agent_reload_scope.py@@ -243,13 +243,21 @@ async def test_reload_agents_config_resolves_config_once_when_config_none(monkey@pytest.mark.asyncioasync def test_agent_reload_config_handler_passes_explicit_scope(monkeypatch):+ from jiuwenswarm.server import app_agentserver+server = agent_ws_server_module.AgentWebSocketServer()calls = []+ refresh_sse_patch = MagicMock()async def fake_reload(config, env, **kwargs):calls.append((config, env, kwargs))monkeypatch.setattr(server._agent_manager, "reload_agents_config", fake_reload)+ monkeypatch.setattr(+ app_agentserver,+ "refresh_openai_response_assembly_patch",+ refresh_sse_patch,+ )patch_handler_name(monkeypatch, "encode_agent_response_for_wire", lambda resp, response_id: {"response_id": response_id,"ok": resp.ok,@@ -281,6 +289,7 @@ async def test_agent_reload_config_handler_passes_explicit_scope(monkeypatch):},)]+ refresh_sse_patch.assert_called_once_with()@pytest.mark.asynciodiff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.pyindex 972785c3d9..ef684ffd21 100644--- a/tests/unit_tests/agentserver/test_team_helpers.py+++ b/tests/unit_tests/agentserver/test_team_helpers.py@@ -3234,6 +3234,51 @@ async def test_leader_direct_answer_finishes_settled_round(monkeypatch):assert closed == ["closed"][email protected]("failure_mode", ["empty", "exception"])+async def test_team_stream_errors_include_structured_code(monkeypatch, failure_mode):+ """Team 流本地错误必须携带稳定 code,避免错误文本中的 403 被误判。"""+ broadcasted: list[dict] = []++ async def _failing_stream(**kwargs):+ if failure_mode == "exception":+ raise RuntimeError("upstream returned 403 while retrying")+ if False:+ yield None++ class _FakeManager(_InactiveTeamRuntimeManagerMixin):+ @staticmethod+ def clear_pending_runtime(session_id: str) -> None:+ pass++ @staticmethod+ def clear_active_runtime(session_id: str) -> None:+ pass++ @staticmethod+ def pop_stream_task(session_id: str) -> None:+ pass++ monkeypatch.setattr(team_helpers, "_run_agent_team_streaming", _failing_stream)+ monkeypatch.setattr(team_helpers, "get_team_manager", lambda channel_id: _FakeManager())+ monkeypatch.setattr(+ team_helpers,+ "_broadcast_event",+ lambda channel_id, session_id, event: broadcasted.append(event),+ )++ await _TeamHelpersTestApi.consume_stream_with_query(+ "officeclaw",+ f"sess-{failure_mode}",+ SimpleNamespace(team_name="demo-team"),+ "hello",+ )++ errors = [event for event in broadcasted if event.get("event_type") == "chat.error"]+ assert len(errors) == 1+ assert errors[0]["code"] == "team_stream_error"++@pytest.mark.anyio@pytest.mark.parametrize("blocker",diff --git a/tests/unit_tests/test_llm_sse_patch_granularity.py b/tests/unit_tests/test_llm_sse_patch_granularity.pyindex aa053a111a..6e5dc899dd 100644--- a/tests/unit_tests/test_llm_sse_patch_granularity.py+++ b/tests/unit_tests/test_llm_sse_patch_granularity.py@@ -167,3 +167,22 @@ def test_gate_defaults_to_true_when_config_unavailable(monkeypatch.setattr("jiuwenswarm.common.config.get_config", _raise)assert app_agentserver._should_apply_sse_invoke_patch() is True # pylint: disable=protected-access+++def test_runtime_refresh_applies_patch_after_xiaoyi_claw_is_enabled(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """运行时启用 xiaoyi_claw 后无需重启即可补打 SSE 响应组装补丁。"""+ config = {"channels": {"xiaoyi": {"apps": [{"mode": "xiaoyi_channel"}]}}}+ calls: list[bool] = []+ _stub_config(monkeypatch, config)+ monkeypatch.setattr(+ app_agentserver,+ "apply_openai_response_assembly_patch",+ lambda: calls.append(True),+ )++ assert app_agentserver.refresh_openai_response_assembly_patch() is False+ config["channels"]["xiaoyi"]["apps"][0]["mode"] = "xiaoyi_claw"+ assert app_agentserver.refresh_openai_response_assembly_patch() is True+ assert calls == [True]