Fix conversation memory replaying history newest-first instead of oldest-first - #54
Merged
Merged
Conversation
…est-first AgentCoreMemoryProvider loaded chat history in the order returned by the AgentCore Memory ListEvents API, which is newest-first. Because the events were appended without sorting (and TryConvertEventToChatMessage dropped the EventTimestamp), history was presented to the model in reverse chronological order. Multi-turn follow-ups such as "prices please" then bound to the oldest turn instead of the most recent one. LoadHistoryAsync now sorts events ascending by EventTimestamp before converting them, so history is presented oldest-first. OrderBy is a stable sort, preserving relative order for events sharing a timestamp. The in-memory Memory emulator (AWS.AgentCore.Testing) previously returned events oldest-first, which did not match the real service and masked this bug. It now returns events newest-first to match production, and its tests were updated accordingly. A regression test feeds events newest-first and asserts the provider returns them oldest-first.
normj
approved these changes
Aug 20, 2026
normj
left a comment
Member
There was a problem hiding this comment.
Minor nit for the change log but otherwise change looks good.
dscpinheiro
approved these changes
Aug 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AWS.AgentCore.Hosting's conversation-memory provider replays chat history newest-first instead of oldest-first, so multi-turn follow-ups bind to a stale turn.Repro:
Because history comes back reversed, the oldest turn (dinners) ends up sitting right before the new "prices please" prompt, so that's what the model latches onto. OTel
gen_ai.input.messagesspans confirm the reversed order; the equivalent Strands/Python agent orders correctly.Root cause
AgentCoreMemoryProvider.LoadHistoryAsyncappendedListEventsresults without sorting. The AgentCore MemoryListEventsAPI returns events newest-first, and there was noOrderBy(EventTimestamp).TryConvertEventToChatMessagedroppedevt.EventTimestamp, so nothing downstream could re-sort.AWS.AgentCore.Testingreturned events oldest-first, the opposite of the real service.Fix
AgentCoreMemoryProvider.LoadHistoryAsyncnow collects events and sorts them ascending byEventTimestamp(stable sort) before converting, so history is presented oldest-first. Partial-pagination and error semantics are unchanged.InMemoryEventStore.ListEvents(emulator) now returns events newest-first to match the real service, so local test harnesses exercise the same ordering as production. Its unit + property tests were updated accordingly.Testing
AWS.AgentCore.Hosting.UnitTests: 102 passedAWS.AgentCore.Testing.UnitTests: 42 passedChange files
Minor version bump for both
AWS.AgentCore.HostingandAWS.AgentCore.Testing.