Skip to content

fix(memory): use current Strands bidi session hooks - #664

Merged
Hweinstock merged 3 commits into
aws:mainfrom
pgrayy:agent-tasks/fix-strands-bidi-hooks
Sep 16, 2026
Merged

Hweinstock merged 3 commits into
aws:mainfrom
pgrayy:agent-tasks/fix-strands-bidi-hooks

Conversation

@pgrayy

@pgrayy pgrayy commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available:

N/A

Description of changes:

Importing AgentCoreMemorySessionManager fails with Strands 1.56.0 because it references bidi hook events removed in strands-agents/harness-sdk#4280. Use the shared initialization and message hooks for both Agent and BidiAgent, and use BidiAgentStopEvent to save state when a bidi session stops. Keep initialization synchronous and offload persistence callbacks when async_mode=True.

Flush pending message and state batches after the final state sync for both AfterInvocationEvent and BidiAgentStopEvent. Skip automatic context retrieval for BidiAgent, because modifying local message history does not deliver that context to the live model.

Update the agent annotations to LocalAgent, raise the minimum strands-agents version to 1.56.0, and add regression coverage for callback ordering and bidi persistence without retrieval in both callback modes.

Validation:

  • All pre-commit checks and the Bandit security scan passed.
  • The full test suite with coverage passed in the locked Python 3.10 development environment (91% coverage). The focused session-manager and configuration suites passed (200 tests).
  • Before the review fixes, five existing live AWS integration tests passed, covering initialization, conversation persistence, batching, and session restoration.
  • Before the review fixes, two one-off bidi checks passed against live AgentCore storage with a scripted model and the default batch size of 1. These exercised public start, send, receive, and stop calls in both callback modes, including message restoration and state saved on stop.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pgrayy
pgrayy requested a review from a team September 16, 2026 02:17
@github-actions github-actions Bot added the size/s PR size: S label Sep 16, 2026
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels Sep 16, 2026
jariy17
jariy17 previously approved these changes Sep 16, 2026
@codecov-commenter

codecov-commenter commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@9f31042). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...ore/memory/integrations/strands/session_manager.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #664   +/-   ##
=======================================
  Coverage        ?   89.52%           
=======================================
  Files           ?      123           
  Lines           ?    10705           
  Branches        ?     1678           
=======================================
  Hits            ?     9584           
  Misses          ?      733           
  Partials        ?      388           
Flag Coverage Δ
unittests 89.52% <92.85%> (?)

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.

@notgitika notgitika left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix, added 2 comments


registry.add_callback(BidiMessageAddedEvent, _on_bidi_message_added)
registry.add_callback(BidiAfterInvocationEvent, _offload(self.sync_bidi_agent, lambda e: e.agent))
registry.add_callback(BidiAgentStopEvent, _offload(self.sync_agent, lambda e: e.agent))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I might be missing something here, but with batching enabled, doesn’t sync_agent() just add the state to the buffer?

since Bidi doesn’t fire AfterInvocationEvent, what flushes the pending messages and state after agent.stop()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a flush and also adjusted the ordering for AfterInvocationEvent. The inline comment explains under the batch_size > 1 blocks.

await asyncio.to_thread(self.append_bidi_message, event.message, event.agent)
await asyncio.to_thread(self.sync_bidi_agent, event.agent)

registry.add_callback(BidiMessageAddedEvent, _on_bidi_message_added)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now that bidi uses MessageAddedEvent, will retrieve_customer_context run for bidi too? It looks like it updates agent.messages, while the model still receives the original input event. could we skip retrieval for bidi or inject it into the outgoing event?

@pgrayy pgrayy Sep 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a skip. This also highlights an important behavioral difference. It may actually make sense to go back to BidiMessageAddedEvent. I'll give this some thought. If I do update, I'll be sure to update here as well.

Another approach could be that Bidi just doesn't emit the event at all. And this is also something to consider for model providers that allow server side conversation management (e.g., OpenAI responses).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense!

@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels Sep 16, 2026

@notgitika notgitika left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@notgitika

Copy link
Copy Markdown
Contributor

Breaking Change Check / Detect Breaking Changes
Failing due to a 403 and not a breaking change.

@Hweinstock
Hweinstock merged commit cc980d1 into aws:main Sep 16, 2026
35 of 45 checks passed
@pgrayy
pgrayy deleted the agent-tasks/fix-strands-bidi-hooks branch September 16, 2026 14:43

This branch is waiting to be deployed

1 waiting deployment
manual-approval — 250a9013 Waiting Sep 16, 2026 by pgrayy via Test (policy) #1574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants