Skip to content

Fix conversation memory replaying history newest-first instead of oldest-first - #54

Merged
GarrettBeatty merged 2 commits into
devfrom
gcbeatty/fix-memory-history-ordering
Aug 20, 2026
Merged

Fix conversation memory replaying history newest-first instead of oldest-first#54
GarrettBeatty merged 2 commits into
devfrom
gcbeatty/fix-memory-history-ordering

Conversation

@GarrettBeatty

Copy link
Copy Markdown
Contributor

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:

you: top 3 dinners for a rainy Thursday?   → bot lists 3 dinners
you: what candy for movie night?           → bot lists 3 candies
you: prices please                         → bot gives the *dinner* prices (should be candy)

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.messages spans confirm the reversed order; the equivalent Strands/Python agent orders correctly.

Root cause

  • AgentCoreMemoryProvider.LoadHistoryAsync appended ListEvents results without sorting. The AgentCore Memory ListEvents API returns events newest-first, and there was no OrderBy(EventTimestamp).
  • TryConvertEventToChatMessage dropped evt.EventTimestamp, so nothing downstream could re-sort.
  • The bug was masked in tests: the in-memory Memory emulator in AWS.AgentCore.Testing returned events oldest-first, the opposite of the real service.

Fix

  • AgentCoreMemoryProvider.LoadHistoryAsync now collects events and sorts them ascending by EventTimestamp (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.
  • Added a provider regression test that feeds events newest-first (as the real service does) and asserts the provider returns them oldest-first.

Testing

  • AWS.AgentCore.Hosting.UnitTests: 102 passed
  • AWS.AgentCore.Testing.UnitTests: 42 passed

Change files

Minor version bump for both AWS.AgentCore.Hosting and AWS.AgentCore.Testing.

…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.
@GarrettBeatty
GarrettBeatty changed the base branch from main to dev August 20, 2026 15:29
@GarrettBeatty
GarrettBeatty marked this pull request as ready for review August 20, 2026 15:31
@GarrettBeatty
GarrettBeatty requested review from a team as code owners August 20, 2026 15:31
@GarrettBeatty
GarrettBeatty requested a review from normj August 20, 2026 15:31

@normj normj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit for the change log but otherwise change looks good.

Comment thread .autover/changes/fix-memory-history-ordering.json Outdated
Comment thread .autover/changes/fix-memory-history-ordering.json Outdated
@GarrettBeatty
GarrettBeatty merged commit d499f87 into dev Aug 20, 2026
5 checks passed
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.

3 participants