Skip to content

fix(memory): order get_last_k_turns messages chronologically#552

Open
valter-silva-au wants to merge 1 commit into
aws:mainfrom
valter-silva-au:fix/get-last-k-turns-chronological-order
Open

fix(memory): order get_last_k_turns messages chronologically#552
valter-silva-au wants to merge 1 commit into
aws:mainfrom
valter-silva-au:fix/get-last-k-turns-chronological-order

Conversation

@valter-silva-au

Copy link
Copy Markdown

Summary

get_last_k_turns groups events into turns by starting a new turn at each USER message. The ListEvents API returns events newest-first, but the method iterated them in arrival order without re-sorting. As a result, 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 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 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 behavior are unchanged.

Fixes #253

Changes

  • src/bedrock_agentcore/memory/client.py and src/bedrock_agentcore/memory/session.py: sort each batch chronologically before turn grouping.
  • tests/.../test_client.py and tests/.../test_session.py: add regression tests that feed events newest-first (as the real API does) and assert a single, correctly ordered USERASSISTANT turn.

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 last k turns 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.

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
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.

Bug Report: get_last_k_turns() returns messages in reversed order within each turn

1 participant