: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(mcp-analytics): pre-sort uuid7 ids in tests that rely on session_id tiebreaker (#60025)
Paweł Szczur9865a5149fbc
diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.pyindex 8c7a6fb1748e..4b559a85106f 100644--- a/products/mcp_analytics/backend/tests/test_api.py+++ b/products/mcp_analytics/backend/tests/test_api.py@@ -10,6 +10,19 @@ from products.mcp_analytics.backend.facade import api, contracts, enumsfrom products.mcp_analytics.backend.models import MCPAnalyticsSubmission+def _sorted_uuid7s(n: int) -> list[str]:+ """Generate ``n`` uuid7 strings sorted lexicographically.++ Back-to-back ``uuid7()`` values share their millisecond prefix and carry+ 62 random bits each, so the relative order of consecutive values is+ effectively a coin flip. Tests that rely on the ``session_id ASC``+ tiebreaker for a stable order on tied sort keys (e.g. equal+ ``session_end``) should pre-sort the IDs so the tiebreaker decides+ deterministically.+ """+ return sorted(str(uuid7()) for _ in range(n))++class TestMCPAnalyticsFacade(APIBaseTest):def test_create_feedback_submission(self) -> None:submission = api.create_feedback_submission(@@ -201,9 +214,11 @@ class TestListMCPSessions(ClickhouseTestMixin, APIBaseTest):assert search("zzzz") == set()def test_order_by_whitelist(self) -> None:- old_id = str(uuid7())- new_id = str(uuid7())- big_id = str(uuid7())+ # new_id and big_id share a session_end below, so the+ # order("session_end") assertion relies on the session_id ASC tiebreaker+ # to put new_id before big_id. Use the pre-sorted helper so that+ # invariant doesn't depend on luck from uuid7()'s 62 random bits.+ old_id, new_id, big_id = _sorted_uuid7s(3)now = datetime.now(tz=UTC)# tool_call_count == number of events; counts chosen so big > old > new.@@ -221,7 +236,8 @@ class TestListMCPSessions(ClickhouseTestMixin, APIBaseTest):)# big_id starts in the middle but ends most recently (a long-running session), so# session_start order differs from session_end order — that's what proves the- # default sorts by session_start, not session_end.+ # default sorts by session_start, not session_end. session_end matches new_id's+ # on purpose: the assertion below exercises the session_id ASC tiebreaker.self._seed_session(big_id,["insight_get"] * 5,@@ -252,8 +268,9 @@ class TestListMCPSessions(ClickhouseTestMixin, APIBaseTest):def test_has_next_signals_more_pages(self) -> None:# Identical session_end across all three so only the session_id tiebreaker# gives a stable order — the assertion below would flake without it.+ # IDs are pre-sorted so the tiebreaker resolves deterministically.ts = datetime.now(tz=UTC) - timedelta(minutes=5)- ids = [str(uuid7()) for _ in range(3)]+ ids = _sorted_uuid7s(3)for session_id in ids:self._seed_session(session_id, ["query_run"], session_start=ts, session_end=ts)@@ -267,6 +284,7 @@ class TestListMCPSessions(ClickhouseTestMixin, APIBaseTest):assert len(second.results) == 1assert second.has_next is False- # Total order → the two pages cover every session exactly once (no skips/dupes).+ # With sorted IDs and the session_id ASC tiebreaker, pagination is a total+ # order — the two pages cover every session in id-ASC order, no skips/dupes.paged = [s.session_id for s in first.results] + [s.session_id for s in second.results]- assert sorted(paged) == sorted(ids)+ assert paged == ids