Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: paginate through backend pages when filtering shared events (#13910)

Xingyao Wangeab9d9e3c702
diff --git a/enterprise/server/sharing/shared_event_router.py b/enterprise/server/sharing/shared_event_router.py
index e986ad682c..1a16f6b96b 100644
--- a/enterprise/server/sharing/shared_event_router.py
+++ b/enterprise/server/sharing/shared_event_router.py
@@ -101,19 +101,36 @@ async def search_shared_events(
] = 100,
shared_event_service: SharedEventService = shared_event_service_dependency,
) -> EventPage:
- """Search / List events for a shared conversation."""
- page = await shared_event_service.search_shared_events(
- conversation_id=UUID(conversation_id),
- kind__eq=kind__eq,
- timestamp__gte=timestamp__gte,
- timestamp__lt=timestamp__lt,
- sort_order=sort_order,
- page_id=page_id,
- limit=limit,
- )
+ """Search / List events for a shared conversation.
+
+ Because non-viewable events (e.g. ``ConversationStateUpdateEvent``) are
+ filtered out after fetching, a single backend page may yield fewer items
+ than *limit*. This method transparently fetches additional backend pages
+ until the requested *limit* is reached or there are no more results.
+ """
+ conv_id = UUID(conversation_id)
+ viewable: list[Event] = []
+ cursor = page_id
+
+ while len(viewable) < limit:
+ remaining = limit - len(viewable)
+ page = await shared_event_service.search_shared_events(
+ conversation_id=conv_id,
+ kind__eq=kind__eq,
+ timestamp__gte=timestamp__gte,
+ timestamp__lt=timestamp__lt,
+ sort_order=sort_order,
+ page_id=cursor,
+ limit=remaining,
+ )
+ viewable.extend(e for e in page.items if _is_viewable(e))
+ cursor = page.next_page_id
+ if cursor is None:
+ break
+
return EventPage(
- items=[e for e in page.items if _is_viewable(e)],
- next_page_id=page.next_page_id,
+ items=viewable[:limit],
+ next_page_id=cursor,
)
diff --git a/enterprise/tests/unit/test_sharing/test_shared_event_filtering.py b/enterprise/tests/unit/test_sharing/test_shared_event_filtering.py
index b91babcfcb..93f116ad98 100644
--- a/enterprise/tests/unit/test_sharing/test_shared_event_filtering.py
+++ b/enterprise/tests/unit/test_sharing/test_shared_event_filtering.py
@@ -87,10 +87,10 @@ class TestSearchSharedEvents:
)
@pytest.mark.asyncio
- async def test_preserves_next_page_id(self):
+ async def test_empty_page_unchanged(self):
mock_service = AsyncMock()
mock_service.search_shared_events.return_value = EventPage(
- items=[_make_state_event()], next_page_id='abc'
+ items=[], next_page_id=None
)
result = await search_shared_events(
@@ -98,24 +98,118 @@ class TestSearchSharedEvents:
shared_event_service=mock_service,
)
- assert result.next_page_id == 'abc'
assert result.items == []
+ assert result.next_page_id is None
@pytest.mark.asyncio
- async def test_empty_page_unchanged(self):
+ async def test_fetches_additional_pages_when_filtering_reduces_count(self):
+ """Fetch next page when first page has only state events."""
+ msg = _make_message_event()
+ state = _make_state_event()
mock_service = AsyncMock()
- mock_service.search_shared_events.return_value = EventPage(
- items=[], next_page_id=None
+ mock_service.search_shared_events.side_effect = [
+ # Page 1: only state events β€” all filtered out
+ EventPage(items=[state, state, state], next_page_id='page2'),
+ # Page 2: all viewable
+ EventPage(items=[msg, msg, msg], next_page_id=None),
+ ]
+
+ result = await search_shared_events(
+ conversation_id=uuid4().hex,
+ limit=3,
+ shared_event_service=mock_service,
)
+ assert len(result.items) == 3
+ assert result.next_page_id is None
+ assert mock_service.search_shared_events.call_count == 2
+
+ @pytest.mark.asyncio
+ async def test_multiple_pages_until_limit_reached(self):
+ """Keep fetching mixed pages until limit viewable events accumulated."""
+ msg = _make_message_event()
+ state = _make_state_event()
+ mock_service = AsyncMock()
+ mock_service.search_shared_events.side_effect = [
+ EventPage(items=[msg, state], next_page_id='p2'),
+ EventPage(items=[state, msg], next_page_id='p3'),
+ EventPage(items=[msg], next_page_id='p4'),
+ ]
+
result = await search_shared_events(
conversation_id=uuid4().hex,
+ limit=3,
shared_event_service=mock_service,
)
- assert result.items == []
+ assert len(result.items) == 3
+ assert result.next_page_id == 'p4'
+ assert mock_service.search_shared_events.call_count == 3
+
+ @pytest.mark.asyncio
+ async def test_stops_when_no_more_pages(self):
+ """Return partial results when no more backend pages are available."""
+ msg = _make_message_event()
+ state = _make_state_event()
+ mock_service = AsyncMock()
+ mock_service.search_shared_events.side_effect = [
+ EventPage(items=[msg, state], next_page_id='p2'),
+ EventPage(items=[state], next_page_id=None),
+ ]
+
+ result = await search_shared_events(
+ conversation_id=uuid4().hex,
+ limit=5,
+ shared_event_service=mock_service,
+ )
+
+ assert len(result.items) == 1
assert result.next_page_id is None
+ @pytest.mark.asyncio
+ async def test_passes_remaining_as_limit_to_backend(self):
+ """Pass remaining needed count as limit to each backend call."""
+ msg = _make_message_event()
+ state = _make_state_event()
+ conv_id = uuid4().hex
+ mock_service = AsyncMock()
+ mock_service.search_shared_events.side_effect = [
+ # First call: limit=3, returns 1 viewable
+ EventPage(items=[msg, state, state], next_page_id='p2'),
+ # Second call: limit should be 2 (remaining)
+ EventPage(items=[msg, msg], next_page_id=None),
+ ]
+
+ await search_shared_events(
+ conversation_id=conv_id,
+ limit=3,
+ shared_event_service=mock_service,
+ )
+
+ calls = mock_service.search_shared_events.call_args_list
+ assert calls[0].kwargs['limit'] == 3
+ assert calls[1].kwargs['limit'] == 2
+
+ @pytest.mark.asyncio
+ async def test_preserves_next_page_id_when_all_filtered(self):
+ """Continue fetching when all events on a page are filtered out."""
+ msg = _make_message_event()
+ state = _make_state_event()
+ mock_service = AsyncMock()
+ mock_service.search_shared_events.side_effect = [
+ EventPage(items=[state], next_page_id='p2'),
+ EventPage(items=[msg], next_page_id='p3'),
+ ]
+
+ result = await search_shared_events(
+ conversation_id=uuid4().hex,
+ limit=1,
+ shared_event_service=mock_service,
+ )
+
+ assert len(result.items) == 1
+ assert result.next_page_id == 'p3'
+
# ---------------------------------------------------------------------------
# batch_get_shared_events