Skip to content

Commit e7130f5

Browse files
committed
Test message re-ordering fixes
1 parent 0e893b6 commit e7130f5

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

examples/tutorials/00_sync/010_multiturn/tests/test_agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020

2121
import pytest
22-
from test_utils.sync import validate_text_in_string, collect_streaming_response
22+
from test_utils.sync import validate_text_in_string, collect_streaming_response, validate_message_ordering
2323

2424
from agentex import Agentex
2525
from agentex.types import TextContent, TextContentParam
@@ -101,6 +101,9 @@ def test_send_message(self, client: Agentex, agent_name: str, agent_id: str):
101101
)
102102
assert len(message_history) == (i + 1) * 2 # user + agent messages
103103

104+
# Validate messages are in chronological order (oldest first)
105+
validate_message_ordering(message_history)
106+
104107

105108
class TestStreamingMessages:
106109
"""Test streaming message sending."""
@@ -149,6 +152,9 @@ def test_stream_message(self, client: Agentex, agent_name: str, agent_id: str):
149152
)
150153
assert len(message_history) == (i + 1) * 2 # user + agent messages
151154

155+
# Validate messages are in chronological order (oldest first)
156+
validate_message_ordering(message_history)
157+
152158

153159
if __name__ == "__main__":
154160
pytest.main([__file__, "-v"])

examples/tutorials/00_sync/020_streaming/tests/test_agent.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os
2020

2121
import pytest
22-
from test_utils.sync import collect_streaming_response
22+
from test_utils.sync import collect_streaming_response, validate_message_ordering
2323

2424
from agentex import Agentex
2525
from agentex.types import TextContent, TextContentParam
@@ -100,6 +100,9 @@ def test_send_message(self, client: Agentex, agent_name: str, agent_id: str):
100100
)
101101
assert len(message_history) == (i + 1) * 2 # user + agent messages
102102

103+
# Validate messages are in chronological order (oldest first)
104+
validate_message_ordering(message_history)
105+
103106

104107
class TestStreamingMessages:
105108
"""Test streaming message sending."""
@@ -148,6 +151,9 @@ def test_send_stream_message(self, client: Agentex, agent_name: str, agent_id: s
148151
)
149152
assert len(message_history) == (i + 1) * 2 # user + agent messages
150153

154+
# Validate messages are in chronological order (oldest first)
155+
validate_message_ordering(message_history)
156+
151157

152158
if __name__ == "__main__":
153159
pytest.main([__file__, "-v"])

examples/tutorials/10_async/00_base/040_other_sdks/tests/test_agent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from test_utils.async_utils import (
3535
stream_agent_response,
3636
send_event_and_poll_yielding,
37+
validate_message_ordering,
3738
)
3839

3940
from agentex import AsyncAgentex
@@ -255,6 +256,10 @@ async def test_multi_turn_conversation_with_state(self, client: AsyncAgentex, ag
255256
state = states[0].state
256257
assert state.get("turn_number") == 2
257258

259+
# Validate messages are in chronological order (oldest first)
260+
message_history = await client.messages.list(task_id=task.id)
261+
validate_message_ordering(message_history)
262+
258263

259264
class TestStreamingEvents:
260265
"""Test streaming event sending with MCP tools and custom streaming patterns."""

examples/tutorials/10_async/10_temporal/010_agent_chat/tests/test_agent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from test_utils.async_utils import (
3434
stream_agent_response,
3535
send_event_and_poll_yielding,
36+
validate_message_ordering,
3637
)
3738

3839
from agentex import AsyncAgentex
@@ -202,6 +203,10 @@ async def test_multi_turn_conversation(self, client: AsyncAgentex, agent_id: str
202203

203204
assert found_response, "Did not receive final agent text response with context recall"
204205

206+
# Validate messages are in chronological order (oldest first)
207+
message_history = await client.messages.list(task_id=task.id)
208+
validate_message_ordering(message_history)
209+
205210

206211
class TestStreamingEvents:
207212
"""Test streaming event sending with OpenAI Agents SDK and tool usage."""

examples/tutorials/10_async/10_temporal/020_state_machine/tests/test_agent.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from test_utils.async_utils import (
3434
stream_task_messages,
3535
send_event_and_poll_yielding,
36+
validate_message_ordering,
3637
)
3738

3839
from agentex import AsyncAgentex
@@ -125,6 +126,10 @@ async def test_send_event_and_poll_simple_query(self, client: AsyncAgentex, agen
125126
assert starting_deep_research_message, "Did not start deep research"
126127
assert uses_tool_requests, "Did not use tool requests"
127128

129+
# Validate messages are in chronological order (oldest first)
130+
message_history = await client.messages.list(task_id=task.id)
131+
validate_message_ordering(message_history)
132+
128133
class TestStreamingEvents:
129134
"""Test streaming event sending with state machine workflow."""
130135
@pytest.mark.asyncio
@@ -188,6 +193,10 @@ async def stream_second_turn() -> None:
188193
assert starting_deep_research_message, "Did not start deep research"
189194
assert uses_tool_requests, "Did not use tool requests"
190195

196+
# Validate messages are in chronological order (oldest first)
197+
message_history = await client.messages.list(task_id=task.id)
198+
validate_message_ordering(message_history)
199+
191200

192201
if __name__ == "__main__":
193202
pytest.main([__file__, "-v"])

examples/tutorials/test_utils/async_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,32 @@ async def stream_task_messages(
284284
task_message = finished_message
285285
if task_message:
286286
yield task_message
287+
288+
289+
def validate_message_ordering(messages: list[TaskMessage]) -> None:
290+
"""
291+
Validate that messages are in chronological order (oldest first).
292+
293+
This ensures the agent receives messages in the correct order for
294+
multi-turn conversations.
295+
296+
Args:
297+
messages: List of TaskMessage objects to validate
298+
299+
Raises:
300+
AssertionError: If messages are not in chronological order
301+
"""
302+
if len(messages) < 2:
303+
return # Nothing to validate with fewer than 2 messages
304+
305+
for i in range(1, len(messages)):
306+
prev_msg = messages[i - 1]
307+
curr_msg = messages[i]
308+
309+
# Validate created_at timestamps are in ascending order
310+
if prev_msg.created_at is not None and curr_msg.created_at is not None:
311+
assert prev_msg.created_at <= curr_msg.created_at, (
312+
f"Messages are not in chronological order. "
313+
f"Message {i - 1} (created_at={prev_msg.created_at}) should come before "
314+
f"message {i} (created_at={curr_msg.created_at})"
315+
)

examples/tutorials/test_utils/sync.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import List, Callable, Optional, Generator
1010

1111
from agentex.types import TextDelta, TextContent
12+
from agentex.types.task_message import TaskMessage
1213
from agentex.types.agent_rpc_result import StreamTaskMessageDone
1314
from agentex.types.agent_rpc_response import SendMessageResponse
1415
from agentex.types.task_message_update import StreamTaskMessageFull, StreamTaskMessageDelta
@@ -93,3 +94,32 @@ def collect_streaming_response(
9394
raise AssertionError("No content was received in the streaming response.")
9495

9596
return aggregated_content, chunks
97+
98+
99+
def validate_message_ordering(messages: List[TaskMessage]) -> None:
100+
"""
101+
Validate that messages are in chronological order (oldest first).
102+
103+
This ensures the agent receives messages in the correct order for
104+
multi-turn conversations.
105+
106+
Args:
107+
messages: List of TaskMessage objects to validate
108+
109+
Raises:
110+
AssertionError: If messages are not in chronological order
111+
"""
112+
if len(messages) < 2:
113+
return # Nothing to validate with fewer than 2 messages
114+
115+
for i in range(1, len(messages)):
116+
prev_msg = messages[i - 1]
117+
curr_msg = messages[i]
118+
119+
# Validate created_at timestamps are in ascending order
120+
if prev_msg.created_at is not None and curr_msg.created_at is not None:
121+
assert prev_msg.created_at <= curr_msg.created_at, (
122+
f"Messages are not in chronological order. "
123+
f"Message {i - 1} (created_at={prev_msg.created_at}) should come before "
124+
f"message {i} (created_at={curr_msg.created_at})"
125+
)

0 commit comments

Comments
 (0)