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
Refactor HttpSseAgentServerClient to use a dictionary for inflight stream IDs, allowing session-based stream supersession. Updated tests to validate new behavior for stream cancellation and session independence.
NickFuryXXX4a67883a9933
diff --git a/jiuwenswarm/gateway/routing/http_agent_client.py b/jiuwenswarm/gateway/routing/http_agent_client.pyindex 9c5a4e7a54..9f81bf4121 100644--- a/jiuwenswarm/gateway/routing/http_agent_client.py+++ b/jiuwenswarm/gateway/routing/http_agent_client.py@@ -143,7 +143,7 @@ class HttpSseAgentServerClient(AgentServerClient):self._push_handlers: set[asyncio.Task[None]] = set()self._stream_lock = asyncio.Lock()self._cancelled_request_ids: set[str] = set()- self._inflight_stream_ids: set[str] = set()+ self._inflight_stream_ids: dict[str, tuple[str, ...] | None] = {}self._link_mtls = link_mtls_configdef _link_config(self) -> LinkMTLSConfig:@@ -310,10 +310,14 @@ class HttpSseAgentServerClient(AgentServerClient):def _is_stream_cancelled(self, rid: str) -> bool:return bool(rid) and rid in self._cancelled_request_ids- def _supersede_other_streams(self, rid: str) -> None:- """新流开始时标记其它 in-flight rid,旧 SSE 残余不再 yield。"""- for other in list(self._inflight_stream_ids):- if other and other != rid:+ def _supersede_other_streams(+ self, rid: str, scope: tuple[str, ...] | None+ ) -> None:+ """Only supersede streams belonging to the same identified session."""+ if scope is None:+ return+ for other, other_scope in list(self._inflight_stream_ids.items()):+ if other and other != rid and other_scope == scope:self._cancelled_request_ids.add(other)asyncio.create_task(self._delayed_cleanup_cancelled_request_id(other))@@ -323,7 +327,7 @@ class HttpSseAgentServerClient(AgentServerClient):async with self._stream_lock:already = rid in self._cancelled_request_idsself._cancelled_request_ids.add(rid)- self._inflight_stream_ids.discard(rid)+ self._inflight_stream_ids.pop(rid, None)if not already:asyncio.create_task(self._delayed_cleanup_cancelled_request_id(rid))@@ -360,10 +364,19 @@ class HttpSseAgentServerClient(AgentServerClient):assembled.url,assembled.used_rpc_fallback,)+ # This client is shared across users and Runtime-routed Pods. A new+ # request must never cancel another session merely by sharing the client.+ scope = (+ tuple(str(value or "") for value in (+ envelope.channel, envelope.user_id, envelope.agent_id,+ envelope.service_id, envelope.workspace_key, envelope.session_id,+ ))+ if envelope.session_id and str(envelope.user_id or "").strip() else None+ )async with self._stream_lock:- self._supersede_other_streams(rid)+ self._supersede_other_streams(rid, scope)if rid:- self._inflight_stream_ids.add(rid)+ self._inflight_stream_ids[rid] = scopetimeout = httpx.Timeout(None, connect=_CONNECT_TIMEOUT_SECONDS)try:async with http.stream(diff --git a/tests/unit_tests/gateway/test_http_agent_client.py b/tests/unit_tests/gateway/test_http_agent_client.pyindex 4aa083291c..c5fc8d5041 100644--- a/tests/unit_tests/gateway/test_http_agent_client.py+++ b/tests/unit_tests/gateway/test_http_agent_client.py@@ -1047,7 +1047,15 @@ async def test_send_request_stream_cancel_drops_trailing_chunks():@pytest.mark.asyncio-async def test_new_stream_supersedes_old_stream_trailing_chunks():+ "old_user,new_session,new_user,superseded",+ [("alice", "s1", "alice", True), ("alice", "s2", "alice", False),+ ("alice", "s1", "bob", False), ("alice", None, "alice", False),+ (None, "s1", None, False), ("", "s1", "", False)],+)+async def test_new_stream_supersedes_only_same_session(+ old_user, new_session, new_user, superseded+):server, port, first_sent, release_trailing = await _start_trailing_sse_stub()base = f"http://127.0.0.1:{port}"client = HttpSseAgentServerClient()@@ -1062,6 +1070,7 @@ async def test_new_stream_supersedes_old_stream_trailing_chunks():request_id="chat-old",channel_id="web",session_id="s1",+ user_id=old_user,req_method=ReqMethod.CHAT_SEND,params={"query": "old"},is_stream=True,@@ -1069,7 +1078,8 @@ async def test_new_stream_supersedes_old_stream_trailing_chunks():new_env = e2a_from_agent_fields(request_id="chat-new",channel_id="web",- session_id="s1",+ session_id=new_session,+ user_id=new_user,req_method=ReqMethod.CHAT_SEND,params={"query": "new"},is_stream=True,@@ -1102,7 +1112,7 @@ async def test_new_stream_supersedes_old_stream_trailing_chunks():release_trailing.set()await asyncio.wait_for(asyncio.gather(*tasks), timeout=5)assert "first" in old_yielded- assert "STALE_TAIL" not in old_yielded+ assert ("STALE_TAIL" not in old_yielded) is supersededassert new_yielded == ["first", "STALE_TAIL"]finally:release_trailing.set()@@ -1112,3 +1122,64 @@ async def test_new_stream_supersedes_old_stream_trailing_chunks():await client.disconnect()server.close()await server.wait_closed()+++async def test_three_sessions_continue_independently_while_bob_first_stream_waits():+ """Transport regression: Carol's second request overlaps Bob's first SSE."""+ bob_started = asyncio.Event()+ carol_second = asyncio.Event()+ received = {}++ class Stream(httpx.AsyncByteStream):+ def __init__(self, rid):+ self.rid = rid++ async def __aiter__(self):+ if self.rid == "bob-1":+ bob_started.set()+ await asyncio.wait_for(carol_second.wait(), 5)+ else:+ await asyncio.wait_for(bob_started.wait(), 5)+ if self.rid == "carol-2":+ carol_second.set()+ for sequence, event in enumerate(("chat.delta", "chat.final")):+ yield _sse_blob(encode_agent_chunk_for_wire(+ AgentResponseChunk(+ request_id=self.rid, channel_id="web",+ payload={"event_type": event, "content": self.rid.split("-")[0]},+ is_complete=event == "chat.final",+ ), response_id=self.rid, sequence=sequence,+ ))++ def handler(request):+ return httpx.Response(200, stream=Stream(request.headers["X-Request-Id"]))++ async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http:+ client = HttpSseAgentServerClient(http_client=http)++ async def conversation(user):+ for turn in (1, 2):+ rid = f"{user}-{turn}"+ env = e2a_from_agent_fields(+ request_id=rid, channel_id="web", user_id=user,+ session_id=f"session-{user}", req_method=ReqMethod.CHAT_SEND,+ params={"query": "code"}, is_stream=True,+ )+ received[rid] = [chunk async for chunk in client.send_request_stream(+ env, base_url="http://as.test:8080"+ )]+ assert [c.payload["event_type"] for c in received[rid]] == [+ "chat.delta", "chat.final"+ ]+ assert all(c.request_id == rid and c.payload["content"] == user+ for c in received[rid])++ try:+ await asyncio.wait_for(asyncio.gather(*(+ conversation(user) for user in ("alice", "bob", "carol")+ )), 10)+ assert len(received) == 6+ assert not client._inflight_stream_ids+ finally:+ await client.disconnect()