Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion api/oss/src/dbs/postgres/sessions/records/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ async def get_records(
RecordDBE.project_id == project_id,
RecordDBE.session_id == session_id,
)
.order_by(RecordDBE.created_at.asc(), RecordDBE.record_index.asc())
# Producer event time first: it is the only key that is monotonic across
# turns. `record_index` restarts at 0 every turn, and the worker can batch
# records from two turns into one write so they share `created_at` — the old
# (created_at, record_index) order then sorted the NEXT turn's first record
# ahead of the PREVIOUS turn's later ones, interleaving the conversation.
# Rows written before `timestamp` existed sort last within their ingest batch.
.order_by(
RecordDBE.timestamp.asc().nullslast(),
RecordDBE.created_at.asc(),
RecordDBE.record_index.asc(),
)
)

dbes = (await session.execute(stmt)).scalars().all()
Expand Down
Loading