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(gateway): stop tracking temporary WebSocket session ids
NickFuryXXX699c497dc634
diff --git a/jiuwenswarm/gateway/channel_manager/web/web_connect.py b/jiuwenswarm/gateway/channel_manager/web/web_connect.pyindex c9c6114ac9..d22e5c592e 100644--- a/jiuwenswarm/gateway/channel_manager/web/web_connect.py+++ b/jiuwenswarm/gateway/channel_manager/web/web_connect.py@@ -1110,13 +1110,15 @@ class WebChannel(BaseWsChannel):)session_id = _explicit_session_id if has_explicit_session else self._make_session_id()- # 追踪 ws → session_id 映射,用于断连时清理- ws_id = id(ws)- sessions = self._ws_sessions.get(ws_id)- if sessions is None:- sessions = set()- self._ws_sessions[ws_id] = sessions- sessions.add(session_id)+ # 追踪 ws → 真实 session_id,用于断连清理/日志。+ # 与 register_ws 一致:仅显式 session 入集;临时 id 只供 Message 构造,避免膨胀。+ if has_explicit_session:+ ws_id = id(ws)+ sessions = self._ws_sessions.get(ws_id)+ if sessions is None:+ sessions = set()+ self._ws_sessions[ws_id] = sessions+ sessions.add(session_id)params = await self._process_files(params)diff --git a/tests/unit_tests/channel/test_web_channel_ws_sessions.py b/tests/unit_tests/channel/test_web_channel_ws_sessions.pynew file mode 100644index 0000000000..c219b00af8--- /dev/null+++ b/tests/unit_tests/channel/test_web_channel_ws_sessions.py@@ -0,0 +1,133 @@+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.++"""WebChannel `_ws_sessions` 仅追踪显式 session(Issue #2334)。"""++from __future__ import annotations++import json+from typing import Any++import pytest++from jiuwenswarm.gateway.channel_manager.base import RobotMessageRouter+from jiuwenswarm.gateway.channel_manager.web.web_connect import (+ WebChannel,+ WebChannelConfig,+)+++class _FakeWebSocket:+ def __init__(self) -> None:+ self.sent_frames: list[dict[str, Any]] = []+ self.remote_address = ("127.0.0.1", 12345)++ async def send(self, data: str) -> None:+ self.sent_frames.append(json.loads(data))+++def _req(*, req_id: str, method: str, params: dict[str, Any] | None = None) -> str:+ return json.dumps(+ {+ "type": "req",+ "id": req_id,+ "method": method,+ "params": params or {},+ },+ ensure_ascii=False,+ )+++async def test_ws_sessions_tracks_explicit_session_id():+ channel = WebChannel(WebChannelConfig(enabled=True), RobotMessageRouter())+ ws = _FakeWebSocket()+ channel.on_message(lambda msg: None)++ await channel._handle_raw_message(+ ws,+ _req(+ req_id="req-chat",+ method="chat.send",+ params={"session_id": "sess-real", "content": "hi"},+ ),+ {},+ )++ assert channel._ws_sessions.get(id(ws)) == {"sess-real"}+++async def test_ws_sessions_ignores_temporary_session_without_explicit_id():+ channel = WebChannel(WebChannelConfig(enabled=True), RobotMessageRouter())+ ws = _FakeWebSocket()+ seen = []++ channel.on_message(lambda msg: seen.append(msg))++ await channel._handle_raw_message(+ ws,+ _req(req_id="req-config", method="config.get", params={}),+ {},+ )++ assert id(ws) not in channel._ws_sessions+ assert len(seen) == 1+ assert seen[0].session_id.startswith("sess_")+++async def test_ws_sessions_does_not_accumulate_temp_ids_across_requests():+ channel = WebChannel(WebChannelConfig(enabled=True), RobotMessageRouter())+ ws = _FakeWebSocket()+ channel.on_message(lambda msg: None)++ await channel._handle_raw_message(+ ws,+ _req(+ req_id="req-1",+ method="chat.send",+ params={"session_id": "sess-a", "content": "a"},+ ),+ {},+ )+ for i in range(5):+ await channel._handle_raw_message(+ ws,+ _req(req_id=f"req-poll-{i}", method="skills.list", params={}),+ {},+ )+ await channel._handle_raw_message(+ ws,+ _req(+ req_id="req-2",+ method="chat.send",+ params={"session_id": "sess-b", "content": "b"},+ ),+ {},+ )+ await channel._handle_raw_message(+ ws,+ _req(req_id="req-history", method="history.get", params={}),+ {},+ )++ assert channel._ws_sessions.get(id(ws)) == {"sess-a", "sess-b"}+++async def test_ws_sessions_ignores_empty_string_session_id():+ channel = WebChannel(WebChannelConfig(enabled=True), RobotMessageRouter())+ ws = _FakeWebSocket()+ channel.on_message(lambda msg: None)++ await channel._handle_raw_message(+ ws,+ _req(+ req_id="req-empty-sid",+ method="config.get",+ params={"session_id": ""},+ ),+ {},+ )++ assert id(ws) not in channel._ws_sessions