fix(memory): use current Strands bidi session hooks - #664
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #664 +/- ##
=======================================
Coverage ? 89.52%
=======================================
Files ? 123
Lines ? 10705
Branches ? 1678
=======================================
Hits ? 9584
Misses ? 733
Partials ? 388
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
notgitika
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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()?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
|
Breaking Change Check / Detect Breaking Changes |
Issue #, if available:
N/A
Description of changes:
Importing
AgentCoreMemorySessionManagerfails 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 bothAgentandBidiAgent, and useBidiAgentStopEventto save state when a bidi session stops. Keep initialization synchronous and offload persistence callbacks whenasync_mode=True.Flush pending message and state batches after the final state sync for both
AfterInvocationEventandBidiAgentStopEvent. Skip automatic context retrieval forBidiAgent, because modifying local message history does not deliver that context to the live model.Update the agent annotations to
LocalAgent, raise the minimumstrands-agentsversion to 1.56.0, and add regression coverage for callback ordering and bidi persistence without retrieval in both callback modes.Validation:
start,send,receive, andstopcalls 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.