Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions python/packages/core/tests/core/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,8 @@ def test_unknown_type_tag_remains_a_raw_dict(self) -> None:
}

def test_registered_child_tag_does_not_hijack_message_payload(self) -> None:
from agent_framework import _sessions

@dataclass
class TextState:
value: str
Expand All @@ -979,13 +981,17 @@ class TextState:
encoder=lambda value: {"value": value.value},
decoder=lambda value: TextState(str(value["value"])),
)
session = AgentSession(session_id="message")
session.state["message"] = Message(role="user", contents=["hello"])
try:
session = AgentSession(session_id="message")
session.state["message"] = Message(role="user", contents=["hello"])

restored = AgentSession.from_dict(session.to_dict())
restored = AgentSession.from_dict(session.to_dict())

assert isinstance(restored.state["message"], Message)
assert restored.state["message"].text == "hello"
assert isinstance(restored.state["message"], Message)
assert restored.state["message"].text == "hello"
finally:
_sessions._STATE_TYPE_REGISTRY.pop("text", None)
_sessions._STATE_CLASS_REGISTRY.pop(TextState, None)

@pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")])
def test_non_finite_float_preserves_legacy_dictionary_serialization(self, value: float) -> None:
Expand Down
2 changes: 0 additions & 2 deletions python/packages/devui/agent_framework_devui/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,6 @@ async def create_response(request: AgentFrameworkRequest, raw_request: Request)
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
},
)
return await openai_executor.execute_sync(request)
Expand Down Expand Up @@ -898,7 +897,6 @@ async def create_response(request: AgentFrameworkRequest, raw_request: Request)
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Response-ID": response_id, # Include ID for debugging/tracking
},
)
Expand Down
66 changes: 65 additions & 1 deletion python/packages/devui/tests/devui/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections.abc import AsyncGenerator
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock
from unittest.mock import AsyncMock, MagicMock

import pytest
from agent_framework import AgentResponseUpdate, Content
Expand Down Expand Up @@ -635,6 +635,70 @@ def test_streaming_response_does_not_hardcode_acao_header():
)


def test_streaming_response_omits_connection_specific_header(monkeypatch: pytest.MonkeyPatch):
"""A streaming /v1/responses must not set a connection-specific Connection header.

RFC 9113 section 8.2.2 forbids connection-specific header fields such as
`Connection: keep-alive` in HTTP/2 and HTTP/3. Persistent connections are the
default in HTTP/1.1, and transport-level connection headers are managed by
the ASGI server. The endpoint must omit the Connection header while preserving
Cache-Control and X-Response-ID.
"""
server = _server_with_mock_agent(auth_token="s3cret")
mock_executor = MagicMock()
mock_executor.get_entity_info.return_value = MagicMock(name="Mock", type="agent")
monkeypatch.setattr(server, "_ensure_executor", AsyncMock(return_value=mock_executor))

async def _dummy_stream(executor: Any, request: Any, response_id: str) -> AsyncGenerator[str]:
yield "data: {}\n\n"

monkeypatch.setattr(server, "_stream_with_cancellation", _dummy_stream)
app = server.get_app()

with TestClient(app, base_url="http://127.0.0.1") as client:
response = client.post(
"/v1/responses",
json={"metadata": {"entity_id": "mock"}, "input": "hello", "stream": True},
headers={"Authorization": "Bearer s3cret"},
)

assert response.status_code == 200
assert "text/event-stream" in response.headers.get("content-type", "")
assert "connection" not in {k.lower() for k in response.headers}, (
"Streaming response includes connection-specific 'Connection' header"
)
assert response.headers.get("cache-control") == "no-cache"
assert "x-response-id" in {k.lower() for k in response.headers}


def test_streaming_response_openai_proxy_mode_omits_connection_specific_header(monkeypatch: pytest.MonkeyPatch):
"""A streaming /v1/responses in OpenAI proxy mode must not set Connection: keep-alive."""
server = _server_with_mock_agent(auth_token="s3cret")
mock_openai_executor = MagicMock()
mock_openai_executor.is_configured = True
monkeypatch.setattr(server, "_ensure_openai_executor", AsyncMock(return_value=mock_openai_executor))

async def _dummy_stream(executor: Any, request: Any) -> AsyncGenerator[str]:
yield "data: {}\n\n"

monkeypatch.setattr(server, "_stream_openai_execution", _dummy_stream)
app = server.get_app()

with TestClient(app, base_url="http://127.0.0.1") as client:
response = client.post(
"/v1/responses",
json={"metadata": {"entity_id": "mock"}, "input": "hello", "stream": True},
headers={"Authorization": "Bearer s3cret", "X-Proxy-Backend": "openai"},
)

assert response.status_code == 200
assert "text/event-stream" in response.headers.get("content-type", "")
assert "connection" not in {k.lower() for k in response.headers}, (
"Streaming response in OpenAI proxy mode includes connection-specific 'Connection' header"
)
assert response.headers.get("cache-control") == "no-cache"


def test_unsupported_conversation_item_content_returns_client_error():
"""Posting an item whose content parts are all unsupported is a client error on an
existing conversation; 404 stays reserved for a missing conversation."""
Expand Down
Loading