Skip to content

fix(memory): guard retrieve_customer_context against empty message content - #544

Merged
nborges-aws merged 1 commit into
aws:mainfrom
dtaniwaki:fix/retrieve-customer-context-empty-content
Jun 23, 2026
Merged

fix(memory): guard retrieve_customer_context against empty message content#544
nborges-aws merged 1 commit into
aws:mainfrom
dtaniwaki:fix/retrieve-customer-context-empty-content

Conversation

@dtaniwaki

@dtaniwaki dtaniwaki commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for maintaining this project! Would appreciate a review when you get a chance.

Fixes #543

Problem

AgentCoreMemorySessionManager.retrieve_customer_context (registered as a MessageAddedEvent callback) indexes the first content block of the last message without checking that the content list is non-empty:

if not messages or messages[-1].get("role") != "user" or "text" not in messages[-1].get("content")[0]:
    return None

When the last message has an empty content list ([]), messages[-1].get("content")[0] raises IndexError. Because this runs inside a MessageAddedEvent hook, the exception propagates out of the event loop and aborts the whole agent invocation (surfacing as a 500 on AgentCore Runtime). The existing guard handles not messages but not an empty content list. (See #543 for a runnable end-to-end reproduction.)

Solution

Check that content is non-empty before indexing the first block, returning early otherwise. Added a regression test (test_retrieve_customer_context_empty_content) that fails without the guard and passes with it. All existing tests in the session manager suite still pass, and ruff check / ruff format are clean.

@dtaniwaki
dtaniwaki requested a review from a team June 23, 2026 02:57
@dtaniwaki dtaniwaki closed this Jun 23, 2026
@dtaniwaki
dtaniwaki deleted the fix/retrieve-customer-context-empty-content branch June 23, 2026 03:16
@dtaniwaki
dtaniwaki restored the fix/retrieve-customer-context-empty-content branch June 23, 2026 03:40
@dtaniwaki dtaniwaki reopened this Jun 23, 2026
@codecov-commenter

codecov-commenter commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@de79b6b). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #544   +/-   ##
=======================================
  Coverage        ?   89.39%           
=======================================
  Files           ?       98           
  Lines           ?     8575           
  Branches        ?     1271           
=======================================
  Hits            ?     7666           
  Misses          ?      585           
  Partials        ?      324           
Flag Coverage Δ
unittests 89.39% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nborges-aws
nborges-aws force-pushed the fix/retrieve-customer-context-empty-content branch from d79bf1f to ebca33d Compare June 23, 2026 21:15
…ntent

Signed-off-by: Daisuke Taniwaki <daisuketaniwaki@gmail.com>
@nborges-aws
nborges-aws force-pushed the fix/retrieve-customer-context-empty-content branch from ebca33d to e36ff8c Compare June 23, 2026 21:20
@nborges-aws

Copy link
Copy Markdown
Contributor

Breaking change bot failure is false alarm. Reported "No Breaking Changes Detected", but errored while attempting to post comment to PR, causing workflow to fail.

PR looks good to me. Thanks for the fix!

@nborges-aws
nborges-aws merged commit 56b215d into aws:main Jun 23, 2026
32 of 33 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.

retrieve_customer_context raises IndexError on empty message content

3 participants