Skip to content

feat(web): sort session messages by time-ordered ID in admin traces - #5029

Merged
eshurakov merged 1 commit into
mainfrom
session/agent_f256aba5-c67b-4c97-83a4-84aa74900d05
Aug 5, 2026
Merged

feat(web): sort session messages by time-ordered ID in admin traces#5029
eshurakov merged 1 commit into
mainfrom
session/agent_f256aba5-c67b-4c97-83a4-84aa74900d05

Conversation

@eshurakov

@eshurakov eshurakov commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

The admin session trace viewer (/admin/session-traces) rendered V2 session messages in the raw order returned by the session-ingest worker's /api/session/:id/export endpoint. That endpoint streams messages and parts in ingest order (ORDER BY ingested_at, id), which diverges from conversation order when a session is re-ingested (e.g., assistant turns ingested before their user prompt) — producing traces where agent replies appear before the user message they answer.

The cloud-agent-next UI avoids this by hydrating messages into SDK storage via insertSorted/insertPartSorted — binary insertion by message/part ID. Since IDs are time-ordered (msg_/part_ + big-endian hex timestamp + random suffix), lexicographic ascending order equals chronological order.

This PR applies the same ordering in the admin path:

  • Add sortSessionMessagesForDisplay (apps/web/src/lib/cloud-agent-next/message-ordering.ts) — sorts messages by info.id and parts by part.id, mirroring the SDK's storage ordering; non-mutating.
  • admin.sessionTraces.getMessages (V2 branch) returns the snapshot through the helper, so both the viewer and the "Download JSON" payload match the cloud-agent-next display order.
  • Add unit tests for the helper and a router-level test asserting an out-of-order snapshot is returned sorted.

Implement message and part re-ordering in the admin session traces router
to ensure the display order matches the cloud-agent-next UI. This fixes
issues where messages ingested out of order (e.g., assistant turns
arriving before user prompts) would appear incorrectly in the admin
trace view.

- Add `sortSessionMessagesForDisplay` utility to handle message and part
  ordering based on IDs.
- Update `adminRouter.sessionTraces.getMessages` to use the new sorting
  logic.
- Add unit tests for message ordering logic and integration tests for
  the admin router.

Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
@kilo-code-bot

kilo-code-bot Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

Reviewed the admin session-trace message ordering fix (new sortSessionMessagesForDisplay helper, V2 getMessages integration, and unit/router tests) against current HEAD 70fa9b1; the comparator faithfully mirrors the SDK's insertSorted/insertPartSorted ID ordering, the helper is verified non-mutating, and no correctness, security, or memory-leak concerns were found with high confidence.

Files Reviewed (4 files)
  • apps/web/src/lib/cloud-agent-next/message-ordering.ts
  • apps/web/src/lib/cloud-agent-next/message-ordering.test.ts
  • apps/web/src/routers/admin-router.ts
  • apps/web/src/routers/admin-session-traces.test.ts

Reviewed by kimi-k3 · Input: 100.4K · Output: 6.5K · Cached: 359.6K

Review guidance: REVIEW.md from base branch main

@eshurakov
eshurakov requested a review from jeanduplessis August 5, 2026 12:47
@eshurakov
eshurakov merged commit 16d0d16 into main Aug 5, 2026
21 checks passed
@eshurakov
eshurakov deleted the session/agent_f256aba5-c67b-4c97-83a4-84aa74900d05 branch August 5, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants