Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

perf(backend): drop eager-include on notification batch reads (#13030)

Zamil Majdyee68c3878b44
diff --git a/autogpt_platform/backend/backend/data/notifications.py b/autogpt_platform/backend/backend/data/notifications.py
index 921077897f..b54574563b 100644
--- a/autogpt_platform/backend/backend/data/notifications.py
+++ b/autogpt_platform/backend/backend/data/notifications.py
@@ -521,21 +521,15 @@ async def get_user_notification_oldest_message_in_batch(
notification_type: NotificationType,
) -> UserNotificationEventDTO | None:
try:
- batch = await UserNotificationBatch.prisma().find_first(
- where={"userId": user_id, "type": notification_type},
- include={"Notifications": True},
- )
- if not batch:
- return None
- if not batch.Notifications:
- return None
- sorted_notifications = sorted(batch.Notifications, key=lambda x: x.createdAt)
-
- return (
- UserNotificationEventDTO.from_db(sorted_notifications[0])
- if sorted_notifications
- else None
+ oldest = await NotificationEvent.prisma().find_first(
+ where={
+ "UserNotificationBatch": {
+ "is": {"userId": user_id, "type": notification_type}
+ }
+ },
+ order={"createdAt": "asc"},
)
+ return UserNotificationEventDTO.from_db(oldest) if oldest else None
except Exception as e:
raise DatabaseError(
f"Failed to get user notification last message in batch for user {user_id} and type {notification_type}: {e}"
@@ -659,6 +653,7 @@ async def get_user_notification_batch(
async def get_all_batches_by_type(
notification_type: NotificationType,
) -> list[UserNotificationBatchDTO]:
+ # Caller re-fetches events per batch when actually sending; no eager include here.
try:
batches = await UserNotificationBatch.prisma().find_many(
where={
@@ -667,7 +662,6 @@ async def get_all_batches_by_type(
"some": {} # Only return batches with at least one notification
},
},
- include={"Notifications": True},
)
return [UserNotificationBatchDTO.from_db(batch) for batch in batches]
except Exception as e:
diff --git a/autogpt_platform/backend/backend/data/notifications_test.py b/autogpt_platform/backend/backend/data/notifications_test.py
index f4cf879b06..16e5986303 100644
--- a/autogpt_platform/backend/backend/data/notifications_test.py
+++ b/autogpt_platform/backend/backend/data/notifications_test.py
@@ -19,6 +19,8 @@ from backend.data.notifications import (
NotificationEventModel,
create_or_add_to_user_notification_batch,
empty_user_notification_batch,
+ get_all_batches_by_type,
+ get_user_notification_oldest_message_in_batch,
)
from backend.util.test import SpinTestServer
@@ -396,3 +398,67 @@ async def test_upsert_retries_on_unique_violation(
assert event_count == 2
finally:
await _cleanup_test_user(user_id)
+
+
[email protected](loop_scope="session")
+async def test_get_oldest_message_returns_single_event(server: SpinTestServer):
+ # Hot-path egress fix: must return a single oldest event without dragging
+ # the whole batch back through Prisma's eager-include path.
+ user_id = f"notif-oldest-{uuid4()}"
+ await _create_test_user(user_id)
+
+ try:
+ for i in range(5):
+ await create_or_add_to_user_notification_batch(
+ user_id=user_id,
+ notification_type=NotificationType.AGENT_RUN,
+ notification_data=_make_agent_run_event(user_id, agent_name=f"e{i}"),
+ )
+
+ oldest = await get_user_notification_oldest_message_in_batch(
+ user_id, NotificationType.AGENT_RUN
+ )
+ assert oldest is not None
+ all_events = await NotificationEvent.prisma().find_many(
+ where={"UserNotificationBatch": {"is": {"userId": user_id}}},
+ order={"createdAt": "asc"},
+ )
+ assert oldest.created_at == all_events[0].createdAt
+ finally:
+ await _cleanup_test_user(user_id)
+
+
[email protected](loop_scope="session")
+async def test_get_oldest_message_returns_none_when_empty(server: SpinTestServer):
+ user_id = f"notif-oldest-empty-{uuid4()}"
+ await _create_test_user(user_id)
+ try:
+ oldest = await get_user_notification_oldest_message_in_batch(
+ user_id, NotificationType.AGENT_RUN
+ )
+ assert oldest is None
+ finally:
+ await _cleanup_test_user(user_id)
+
+
[email protected](loop_scope="session")
+async def test_get_all_batches_does_not_eager_load_events(server: SpinTestServer):
+ # Caller iterates batches and re-fetches events per batch when actually
+ # sending; loading them here was the egress hot path.
+ user_id = f"notif-allbatches-{uuid4()}"
+ await _create_test_user(user_id)
+
+ try:
+ for i in range(3):
+ await create_or_add_to_user_notification_batch(
+ user_id=user_id,
+ notification_type=NotificationType.AGENT_RUN,
+ notification_data=_make_agent_run_event(user_id, agent_name=f"e{i}"),
+ )
+
+ batches = await get_all_batches_by_type(NotificationType.AGENT_RUN)
+ mine = [b for b in batches if b.user_id == user_id]
+ assert len(mine) == 1
+ assert mine[0].notifications == []
+ finally:
+ await _cleanup_test_user(user_id)