Skip to content

The agent that grows with you

fix(gateway): keep SSE active during preflight

KoNit-K67a959ac2013
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index 84934c791d2..33f76cd7847 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -201,7 +201,9 @@ DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8642
MAX_STORED_RESPONSES = 100
MAX_REQUEST_BYTES = 10_000_000 # 10 MB — accommodates long agent conversations with tool calls
-CHAT_COMPLETIONS_SSE_KEEPALIVE_SECONDS = 30.0
+# Send a comment before remote API clients' common 20-second idle deadline.
+# This constant is shared by OpenAI chat/Responses and native session SSE.
+CHAT_COMPLETIONS_SSE_KEEPALIVE_SECONDS = 10.0
MAX_NORMALIZED_TEXT_LENGTH = 65_536 # 64 KB cap for normalized content parts
MAX_CONTENT_LIST_SIZE = 1_000 # Max items when content is an array
RESPONSES_AUTO_TRUNCATION_HISTORY_LIMIT = 100
diff --git a/tests/gateway/test_api_server_sse_keepalive.py b/tests/gateway/test_api_server_sse_keepalive.py
new file mode 100644
index 00000000000..dee1d0f160d
--- /dev/null
+++ b/tests/gateway/test_api_server_sse_keepalive.py
@@ -0,0 +1,51 @@
+"""Regression coverage for prompt SSE activity during long preflight work."""
+
+import asyncio
+
+import pytest
+
+from gateway.platforms import api_server
+from gateway.platforms.api_server_openai_routes import _iter_stream_items
+
+
+class _RecordingResponse:
+ def __init__(self) -> None:
+ self.writes: list[bytes] = []
+
+ async def write(self, data: bytes) -> None:
+ self.writes.append(data)
+
+
+async def test_idle_stream_writes_keepalive_before_remote_client_timeout(monkeypatch):
+ """A compression-bound agent must produce a byte within a 20s client window.
+
+ ``_iter_stream_items`` is shared by OpenAI chat and Responses streaming;
+ native session streaming uses the same API-server keepalive constant.
+ """
+ stream_q = asyncio.Queue()
+ agent_task = asyncio.get_running_loop().create_future()
+ response = _RecordingResponse()
+ clock = [0.0, 20.0, 20.0]
+
+ def monotonic() -> float:
+ return clock.pop(0) if clock else 20.0
+
+ monkeypatch.setattr("gateway.platforms.api_server_openai_routes.time.monotonic", monotonic)
+
+ waits = 0
+
+ async def elapsed_wait_for(*_args, **_kwargs):
+ nonlocal waits
+ waits += 1
+ if waits == 2:
+ agent_task.set_result(None)
+ raise asyncio.TimeoutError
+
+ monkeypatch.setattr(
+ "gateway.platforms.api_server_openai_routes.asyncio.wait_for", elapsed_wait_for
+ )
+
+ assert api_server.CHAT_COMPLETIONS_SSE_KEEPALIVE_SECONDS <= 20.0
+ assert [item async for item in _iter_stream_items(stream_q, agent_task, response)] == []
+ assert response.writes == [b": keepalive\n\n"]