Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: cloud event search timestamp comparison (str vs datetime) (#14399)

Xingyao Wangb44c4b6fc72d
diff --git a/openhands/app_server/event/event_service_base.py b/openhands/app_server/event/event_service_base.py
index 50181e74d5..381b4d06f6 100644
--- a/openhands/app_server/event/event_service_base.py
+++ b/openhands/app_server/event/event_service_base.py
@@ -91,16 +91,20 @@ class EventServiceBase(EventService, ABC):
events = await asyncio.gather(
*[loop.run_in_executor(None, self._load_event, path) for path in paths] # type: ignore[arg-type]
)
+ # Convert datetime filters to ISO strings so they can be compared
+ # against event.timestamp (which is stored as an ISO 8601 string).
+ timestamp_gte_str = timestamp__gte.isoformat() if timestamp__gte else None
+ timestamp_lt_str = timestamp__lt.isoformat() if timestamp__lt else None
+
items = []
for event in events:
if not event:
continue
if kind__eq and event.kind != kind__eq:
continue
- # TODO: Are these comparison operators valid?
- if timestamp__gte and event.timestamp < timestamp__gte: # type: ignore[operator]
+ if timestamp_gte_str and event.timestamp < timestamp_gte_str:
continue
- if timestamp__lt and event.timestamp >= timestamp__lt: # type: ignore[operator]
+ if timestamp_lt_str and event.timestamp >= timestamp_lt_str:
continue
items.append(event)
diff --git a/tests/unit/app_server/test_filesystem_event_service.py b/tests/unit/app_server/test_filesystem_event_service.py
index a382518b57..606e9afcca 100644
--- a/tests/unit/app_server/test_filesystem_event_service.py
+++ b/tests/unit/app_server/test_filesystem_event_service.py
@@ -5,6 +5,8 @@ focusing on search functionality.
"""
import tempfile
+import time
+from datetime import datetime
from pathlib import Path
from uuid import uuid4
@@ -268,6 +270,124 @@ class TestFilesystemEventServiceSearchEvents:
# Should have found all 5 token events
assert len(collected_ids) == 5
+ @pytest.mark.asyncio
+ async def test_search_events_filter_by_timestamp_gte(
+ self, service: FilesystemEventService
+ ):
+ """Test that search_events filters events by timestamp__gte.
+
+ This verifies the fix for a bug where event.timestamp (str) was
+ compared directly against a datetime object, raising TypeError.
+ """
+ conversation_id = uuid4()
+
+ # Create events with a small delay so timestamps differ
+ early_event = create_token_event()
+ await service.save_event(conversation_id, early_event)
+ time.sleep(0.01)
+
+ cutoff = datetime.now()
+ time.sleep(0.01)
+
+ late_event = create_token_event()
+ await service.save_event(conversation_id, late_event)
+
+ result = await service.search_events(conversation_id, timestamp__gte=cutoff)
+
+ assert len(result.items) == 1
+ assert result.items[0].id == late_event.id
+
+ @pytest.mark.asyncio
+ async def test_search_events_filter_by_timestamp_lt(
+ self, service: FilesystemEventService
+ ):
+ """Test that search_events filters events by timestamp__lt.
+
+ This verifies the fix for a bug where event.timestamp (str) was
+ compared directly against a datetime object, raising TypeError.
+ """
+ conversation_id = uuid4()
+
+ early_event = create_token_event()
+ await service.save_event(conversation_id, early_event)
+ time.sleep(0.01)
+
+ cutoff = datetime.now()
+ time.sleep(0.01)
+
+ late_event = create_token_event()
+ await service.save_event(conversation_id, late_event)
+
+ result = await service.search_events(conversation_id, timestamp__lt=cutoff)
+
+ assert len(result.items) == 1
+ assert result.items[0].id == early_event.id
+
+ @pytest.mark.asyncio
+ async def test_search_events_filter_by_timestamp_range(
+ self, service: FilesystemEventService
+ ):
+ """Test that search_events filters events by a timestamp range."""
+ conversation_id = uuid4()
+
+ event1 = create_token_event()
+ await service.save_event(conversation_id, event1)
+ time.sleep(0.01)
+
+ range_start = datetime.now()
+ time.sleep(0.01)
+
+ event2 = create_token_event()
+ await service.save_event(conversation_id, event2)
+ time.sleep(0.01)
+
+ range_end = datetime.now()
+ time.sleep(0.01)
+
+ event3 = create_token_event()
+ await service.save_event(conversation_id, event3)
+
+ result = await service.search_events(
+ conversation_id,
+ timestamp__gte=range_start,
+ timestamp__lt=range_end,
+ )
+
+ assert len(result.items) == 1
+ assert result.items[0].id == event2.id
+
+ @pytest.mark.asyncio
+ async def test_search_events_timestamp_filter_with_desc_sort(
+ self, service: FilesystemEventService
+ ):
+ """Test timestamp filters combined with TIMESTAMP_DESC sort order."""
+ conversation_id = uuid4()
+
+ event1 = create_token_event()
+ await service.save_event(conversation_id, event1)
+ time.sleep(0.01)
+
+ cutoff = datetime.now()
+ time.sleep(0.01)
+
+ event2 = create_token_event()
+ await service.save_event(conversation_id, event2)
+ time.sleep(0.01)
+
+ event3 = create_token_event()
+ await service.save_event(conversation_id, event3)
+
+ result = await service.search_events(
+ conversation_id,
+ timestamp__gte=cutoff,
+ sort_order=EventSortOrder.TIMESTAMP_DESC,
+ )
+
+ assert len(result.items) == 2
+ # Descending: event3 before event2
+ assert result.items[0].id == event3.id
+ assert result.items[1].id == event2.id
+
class TestFilesystemEventServiceIntegration:
"""Integration tests for FilesystemEventService."""