Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(conversations): break ticket message order ties on id (#102295)

posthog[bot]1b89e7f46573
diff --git a/products/conversations/backend/api/tests/test_tickets.py b/products/conversations/backend/api/tests/test_tickets.py
index 66de0444e00b..a274012de872 100644
--- a/products/conversations/backend/api/tests/test_tickets.py
+++ b/products/conversations/backend/api/tests/test_tickets.py
@@ -3,6 +3,7 @@ from concurrent.futures import ThreadPoolExecutor
from datetime import timedelta
from decimal import Decimal
from threading import Barrier, Event
+from uuid import UUID
from posthog.test.base import (
APIBaseTest,
@@ -2902,15 +2903,25 @@ class TestTicketMessagesAPI(APIBaseTest):
def test_messages_pagination(self, mock_on_commit):
base = timezone.now()
- for i in range(5):
- comment = Comment.objects.create(
+ # msg-2 and msg-3 tie on created_at, and msg-3 holds the lower id. The ids are
+ # explicit so the row order in the table contradicts the expected order.
+ rows = [
+ ("msg-0", UUID("00000000-0000-7000-8000-000000000001"), 0),
+ ("msg-1", UUID("00000000-0000-7000-8000-000000000002"), 1),
+ ("msg-2", UUID("00000000-0000-7000-8000-000000000009"), 2),
+ ("msg-3", UUID("00000000-0000-7000-8000-000000000003"), 2),
+ ("msg-4", UUID("00000000-0000-7000-8000-000000000004"), 3),
+ ]
+ for content, comment_id, offset in rows:
+ Comment.objects.create(
+ id=comment_id,
team=self.team,
scope="conversations_ticket",
item_id=str(self.ticket.id),
- content=f"msg-{i}",
+ content=content,
item_context={"author_type": "customer"},
)
- Comment.objects.filter(id=comment.id).update(created_at=base + timedelta(seconds=i))
+ Comment.objects.filter(id=comment_id).update(created_at=base + timedelta(seconds=offset))
response = self.client.get(self.url, {"limit": 2})
assert response.status_code == status.HTTP_200_OK
@@ -2925,6 +2936,15 @@ class TestTicketMessagesAPI(APIBaseTest):
assert len(body["results"]) == 1
assert body["results"][0]["content"] == "msg-4"
+ # Walk every page. Tied rows must keep one stable position, so no message is
+ # lost between pages and none appears twice.
+ walked: list[str] = []
+ for offset in range(0, len(rows), 2):
+ page = self.client.get(self.url, {"limit": 2, "offset": offset}).json()
+ walked.extend(message["id"] for message in page["results"])
+
+ assert walked == [str(comment_id) for _, comment_id, _ in sorted(rows, key=lambda row: (row[2], row[1]))]
+
@patch.object(transaction, "on_commit", side_effect=immediate_on_commit)
class TestTicketReplyAPI(APIBaseTest):
diff --git a/products/conversations/backend/api/tickets.py b/products/conversations/backend/api/tickets.py
index 257477af6efb..de42d0bcdb3c 100644
--- a/products/conversations/backend/api/tickets.py
+++ b/products/conversations/backend/api/tickets.py
@@ -1319,7 +1319,9 @@ class TicketViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, AccessContro
deleted=False,
)
.select_related("created_by")
- .order_by("created_at")
+ # id breaks ties so separate page queries agree on the order of
+ # messages that share a created_at.
+ .order_by("created_at", "id")
)
page = self.paginate_queryset(comments)