: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(comments): break created_at ties in the comments cursor (#102909)
posthog[bot]1d0f4b1cd4f9
diff --git a/posthog/api/comments.py b/posthog/api/comments.pyindex d670287da152..2dedd5f10c9a 100644--- a/posthog/api/comments.py+++ b/posthog/api/comments.py@@ -596,7 +596,10 @@ class CommentErrorSerializer(serializers.Serializer):class CommentPagination(pagination.CursorPagination):- ordering = "-created_at"+ # `created_at` is not unique, and DRF encodes only the first ordering field in the cursor,+ # resolving ties with an offset. That offset is reproducible only when rows sharing a+ # timestamp come back in a stable order, which Postgres does not otherwise guarantee.+ ordering = ("-created_at", "-id")page_size = 100diff --git a/posthog/api/test/test_comments.py b/posthog/api/test/test_comments.pyindex 6ab05f5eea38..959aaa2437b9 100644--- a/posthog/api/test/test_comments.py+++ b/posthog/api/test/test_comments.py@@ -12,6 +12,7 @@ from django.utils import timezonefrom parameterized import parameterizedfrom rest_framework import status+from posthog.api.comments import CommentPaginationfrom posthog.models import Userfrom posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.comment import Comment@@ -1357,6 +1358,39 @@ class TestComments(APIBaseTest, QueryMatchingTest):assert response.json()["results"][0]["content"] == "comment other reply"assert response.json()["results"][1]["content"] == "comment reply"+ @parameterized.expand(+ [+ ("list", False),+ ("thread", True),+ ]+ )+ def test_pages_comments_written_in_the_same_instant(self, _name: str, as_thread: bool) -> None:+ root = self._create_comment({"content": "root", "scope": "Notebook", "item_id": "1"}) if as_thread else None+ reply_to = {"source_comment": root["id"]} if root else {}+ tied_ids = [+ self._create_comment({"content": f"tied {index}", "scope": "Notebook", "item_id": "1", **reply_to})["id"]+ for index in range(9)+ ]+ newest_id = self._create_comment({"content": "newest", "scope": "Notebook", "item_id": "1", **reply_to})["id"]+ # created_at is auto_now_add, so the tie has to be forced once the comments exist+ Comment.objects.filter(id__in=tied_ids).update(created_at=timezone.now() - timedelta(hours=1))+ Comment.objects.filter(id=newest_id).update(created_at=timezone.now())++ url: str | None = (+ f"/api/projects/{self.team.id}/comments/{root['id']}/thread"+ if root+ else f"/api/projects/{self.team.id}/comments/?scope=Notebook&item_id=1"+ )+ paged_ids: list[str] = []+ with mock.patch.object(CommentPagination, "page_size", 2):+ while url:+ payload = self.client.get(url).json()+ paged_ids.extend(comment["id"] for comment in payload["results"])+ url = payload["next"]+ assert len(paged_ids) <= 10++ assert paged_ids == [newest_id, *reversed(tied_ids)]+@parameterized.expand([("no_comments", [], "", 0),