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(server): report auto-routed model in usage summaries

de49de18dcf379a
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
index e21eac5d1a..f709280c47 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py
@@ -4331,6 +4331,14 @@ class JiuWenSwarmDeepAdapter:
return self._model_request_config.model_name or "unknown"
return "unknown"
+ def _resolve_usage_model_name(self) -> str:
+ """Report the active main model, including request-time Auto routing."""
+ react_agent = getattr(getattr(self, "_instance", None), "_react_agent", None)
+ model_name = getattr(getattr(react_agent, "_config", None), "model_name", None)
+ if isinstance(model_name, str) and model_name.strip():
+ return model_name.strip()
+ return self._resolve_model_name()
+
def _resolve_session_git_snapshot(
self,
project_dir: str,
@@ -14678,7 +14686,7 @@ class JiuWenSwarmDeepAdapter:
"event_type": "chat.usage_summary",
"session_id": session_id,
"usage": summary,
- "model": self._resolve_model_name(),
+ "model": self._resolve_usage_model_name(),
}
context_usage_percent, context_window_tokens = self._context_usage_fields(session_id)
if context_usage_percent is not None:
diff --git a/tests/unit_tests/agents/harness/common/rails/test_model_routing_applied_model_boundary.py b/tests/unit_tests/agents/harness/common/rails/test_model_routing_applied_model_boundary.py
index 44f80e5b2f..224dbf7799 100644
--- a/tests/unit_tests/agents/harness/common/rails/test_model_routing_applied_model_boundary.py
+++ b/tests/unit_tests/agents/harness/common/rails/test_model_routing_applied_model_boundary.py
@@ -123,12 +123,12 @@ def _current_model(content: str) -> str:
raise AssertionError(f"runtime.setting 里没有「当前模型」行:\n{content}")
-async def _run_real_chain(extra: dict) -> list[str]:
+async def _run_real_chain(extra: dict, summary_models: list[str] | None = None) -> list[str]:
"""真实 DeepAgent 事件链跑一轮,返回 runtime.setting 的内容。"""
agent = DeepAgent(AgentCard(id="boundary_agent", name="Boundary"))
agent.configure(
DeepAgentConfig(
- model=_build_model("mock-model"),
+ model=_build_model("deepseek-v4.1-flash"),
enable_task_loop=False,
max_iterations=2,
)
@@ -176,11 +176,35 @@ async def _run_real_chain(extra: dict) -> list[str]:
"run": {"kind": "normal", "context": {"extra": extra}},
},
)
+ if summary_models is not None:
+ from jiuwenswarm.server.runtime.agent_adapter.interface_deep import JiuWenSwarmDeepAdapter
+
+ adapter = JiuWenSwarmDeepAdapter.__new__(JiuWenSwarmDeepAdapter)
+ adapter._instance = agent
+ adapter._model_request_config = _build_model("deepseek-v4.1-flash").model_config
+ usage = adapter._new_usage_accumulator()
+ adapter._accumulate_usage(usage, {"input_tokens": 10, "output_tokens": 2, "total_tokens": 12})
+ chunk = adapter._log_and_make_usage_summary_chunk(
+ request_id="r1", channel_id="officeclaw", session_id="boundary-session",
+ usage_accumulator=usage,
+ )
+ assert chunk is not None
+ summary_models.append(chunk.payload["model"])
finally:
await Runner.stop()
return captured
[email protected]("routing_enabled, expected", [(True, "glm-5.3"), (False, "deepseek-v4.1-flash")])
+async def test_usage_summary_reports_active_model_after_routing(tmp_path, monkeypatch, routing_enabled, expected):
+ state_file = _write_runtime_state(tmp_path, model="deepseek-v4.1-flash", available_models=["deepseek-v4.1-flash"])
+ monkeypatch.setattr(_runtime_prompt_mod, "get_runtime_state_path", lambda session_id=None: state_file)
+ models = []
+ await _run_real_chain({"model_selection": "deepseek-v4.1-flash", "enable_routing": routing_enabled}, models)
+ assert models == [expected]
+
+
@pytest.mark.asyncio
async def test_applied_model_crosses_outer_to_inner_ctx_boundary(tmp_path, monkeypatch):
"""auto 档切到 glm-5.3:内层 RuntimePromptRail 必须报**路由后**的模型名。