fix(memory): order get_last_k_turns messages chronologically#552
Open
valter-silva-au wants to merge 1 commit into
Open
fix(memory): order get_last_k_turns messages chronologically#552valter-silva-au wants to merge 1 commit into
valter-silva-au wants to merge 1 commit into
Conversation
The ListEvents API returns events newest-first, but get_last_k_turns grouped them in arrival order. Because a new turn is started at each USER message, processing newest-first misaligns turns — an ASSISTANT reply is orphaned into a leading turn and every subsequent turn pairs a USER message with the previous turn's ASSISTANT response. The effect is most visible when tool use splits a turn across single-message events. Sort each fetched batch by eventTimestamp (oldest-first) before grouping, in both MemoryClient.get_last_k_turns and MemorySessionManager.get_last_k_turns. The sort is skipped when any event in the batch lacks a timestamp, preserving existing behavior for callers that don't supply one. Public signatures and pagination are unchanged. Add regression tests in both modules that feed events newest-first (as the real API does) and assert a single, correctly ordered USER->ASSISTANT turn. Fixes aws#253
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.
Summary
get_last_k_turnsgroups events into turns by starting a new turn at eachUSERmessage. The ListEvents API returns events newest-first, but the method iterated them in arrival order without re-sorting. As a result, anASSISTANTreply is orphaned into a leading turn and every subsequent turn pairs aUSERmessage with the previous turn'sASSISTANTresponse — the whole conversation is shifted by one. The effect is most visible when tool use splits a turn across single-message events (as in the issue report).This sorts each fetched batch by
eventTimestamp(oldest-first) before grouping, in bothMemoryClient.get_last_k_turnsandMemorySessionManager.get_last_k_turns. The sort is skipped when any event in the batch lacks a timestamp, preserving existing behavior for callers that don't supply one. Public signatures and pagination behavior are unchanged.Fixes #253
Changes
src/bedrock_agentcore/memory/client.pyandsrc/bedrock_agentcore/memory/session.py: sort each batch chronologically before turn grouping.tests/.../test_client.pyandtests/.../test_session.py: add regression tests that feed events newest-first (as the real API does) and assert a single, correctly orderedUSER→ASSISTANTturn.Note for maintainers
This fixes the reported message-alignment bug with a minimal, per-batch sort. There is a separate, latent question of whether
turns[:k]should return the first or the lastkturns once ordering is chronological — I've intentionally left that out of scope to keep this change small and behavior-preserving. Happy to follow up separately if you'd like that addressed too.Testing
uv run pytest tests/bedrock_agentcore/memory/(excluding the credential-gated strands integration tests) — all pass.