From 338ffdcf63a5de8a245190e162c13d10a872c0cc Mon Sep 17 00:00:00 2001 From: JuliaEdom Date: Mon, 6 Jul 2026 05:09:54 +0300 Subject: [PATCH] fix(g2-followup): tiebreak newest-first journal scans on event_id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bounded journal scan added in S4 (PR #158) ordered by `{time_column} DESC` with no secondary key, unlike the existing ascending path's `ASC, event_id ASC` total order. Two rows sharing an identical timestamp for the same entity resolved to whichever the backend happened to emit first — nondeterministic across backends and across reloads. Add `, event_id DESC` to restore a total order and match the old last-write-wins tie behavior deterministically. Found in an Opus review pass standing in for the unavailable Codex review step. --- src/serving/semantic_layer/query/engine.py | 2 +- tests/unit/test_pipeline_events_scan.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/serving/semantic_layer/query/engine.py b/src/serving/semantic_layer/query/engine.py index 10efc3f2..336d9943 100644 --- a/src/serving/semantic_layer/query/engine.py +++ b/src/serving/semantic_layer/query/engine.py @@ -222,7 +222,7 @@ def render(value: str) -> str: sql = f"{sql} WHERE {' AND '.join(where_clauses)}" order_column = time_column or "event_id" if newest_first: - sql = f"{sql} ORDER BY {order_column} DESC" + sql = f"{sql} ORDER BY {order_column} DESC, event_id DESC" else: sql = f"{sql} ORDER BY {order_column} ASC, event_id ASC" if limit is not None: diff --git a/tests/unit/test_pipeline_events_scan.py b/tests/unit/test_pipeline_events_scan.py index 86b2fc9e..49edba96 100644 --- a/tests/unit/test_pipeline_events_scan.py +++ b/tests/unit/test_pipeline_events_scan.py @@ -84,7 +84,7 @@ def test_family_filters_and_limit_are_static_sql() -> None: ((sql, _),) = backend.calls assert "topic = 'events.validated'" in sql assert "event_type IN ('click', 'page_view', 'add_to_cart')" in sql - assert sql.endswith("ORDER BY processed_at DESC LIMIT 10") + assert sql.endswith("ORDER BY processed_at DESC, event_id DESC LIMIT 10") def test_missing_journal_returns_empty_without_query() -> None: