Skip to content

[Bug]: SMG conversation item ordering is not deterministic — added_at is used as sequence proxy without uniqueness guarantee #1362

@khoaatra

Description

@khoaatra

Bug Description

SMG reconstructs conversation item order using ORDER BY added_at, item_id in the normalized conversation item link table.

This is unsafe because added_at is not guaranteed to be unique for items in the same conversation:

the manual conversation-items API assigns the same added_at to every item in a batch
response persistence uses wall-clock time but does not guarantee uniqueness
some backends reduce timestamp precision further (for example, in-memory uses seconds, Redis uses milliseconds)
When added_at ties occur, ordering falls back to item_id, but item_id is not a deterministic sequence field and may be randomly generated.

As a result, /v1/conversations/{id}/items and next-turn conversation reconstruction can return items in an order that does not match production order.

Steps to Reproduce

N/A

Expected Behavior

Conversation items should be returned in the exact order they were produced.

Actual Behavior

Ordering depends on timestamp uniqueness and arbitrary item_id tie-breaking.

Component

model-gateway (core routing)

Routing Policy (if applicable)

None

Connection Mode

None

Configuration

Logs / Error Output

Environment

Cargo

Deployment Environment

Local development

Streaming Context

  • Issue occurs with streaming (SSE) responses
  • Issue occurs with non-streaming responses
  • Issue occurs in both modes

Additional Context

Provide 2 full test files, one is openai, one is smg
Testing flow: create a conv => response 1 with conv_id => response 2 with conv_id => get conv items => mcp call => get conv items

Pre-submission Checklist

  • I have searched existing issues and discussions
  • I can reproduce this issue consistently
  • I am using the latest version of SMG

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions