fix(agent): ensure turns are sorted

Ensures that session turns retrieved from the agent persistence layer are
sorted by their `started_at` timestamp, as the key-value store does not
guarantee order.

Closes #2852

## Test Plan

- [ ] Add unit tests
This commit is contained in:
Omer Tuchfeld 2025-07-22 13:56:20 +02:00
parent b5a6ecc331
commit a39848e51b

View file

@ -128,6 +128,11 @@ class AgentPersistence:
except Exception as e: except Exception as e:
log.error(f"Error parsing turn: {e}") log.error(f"Error parsing turn: {e}")
continue continue
# The kvstore does not guarantee order, so we sort by started_at
# to ensure consistent ordering of turns.
turns.sort(key=lambda t: t.started_at)
return turns return turns
async def get_session_turn(self, session_id: str, turn_id: str) -> Turn | None: async def get_session_turn(self, session_id: str, turn_id: str) -> Turn | None: