Commit fbce2fc
authored
feat(memory): event metadata state identification, message batching, and redundant sync elimination (aws#244)
* feat(memory): use event metadata for session/agent state identification
Replace actorId prefix-based approach with event metadata for distinguishing
session and agent state events. Add auto-migration for legacy events.
Changes:
- Add StateType enum (SESSION, AGENT) and metadata keys
- Update create_session/create_agent to include stateType metadata
- Update read_session/read_agent to filter by metadata
- Add backwards-compatible auto-migration: legacy events are converted
to new format on read (create new with metadata, delete old)
- Add tests for legacy migration behavior
* fix(memory): handle eventual consistency and pagination with metadata filters
- Fix pagination bug in list_events where API returns nextToken even with
0 results, causing "metadata filter mismatch" error on subsequent page
- Add _retry_with_backoff method for handling eventual consistency when
reading newly created agents via metadata filter
- Track created agent IDs to handle updates during consistency window
- Update test to account for retry behavior in legacy migration
* fix: Fixed pagination bug with nextToken
* feat(memory): implement true message batching for flush_messages() (aws#256)
* feat(memory): add batch_size configuration for message buffering
Add optional batch_size parameter to AgentCoreMemoryConfig that allows
customers to buffer messages before sending to AgentCore Memory.
Changes:
- Add batch_size parameter (default=1, max=100) to AgentCoreMemoryConfig
- Add message buffering with thread-safe _message_buffer and _buffer_lock
- Modify create_message() to buffer when batch_size > 1
- Add flush_messages() to send all buffered messages
- Add pending_message_count() to check buffer size
- Add close() and context manager for cleanup
- Add 24 unit tests covering batching functionality
Default batch_size=1 preserves backward compatibility (immediate send).
* feat(memory): implement true message batching in flush_messages()
Previously, batch_size buffered messages but still made N separate API
calls for N messages. Now flush_messages() groups conversational messages
by session_id and combines them into a single create_event() call per
session, significantly reducing API calls.
Key changes:
- Group conversational messages by session_id into combined payloads
- Preserve message order within each session's payload (earliest first)
- Use the latest timestamp from grouped messages for the combined event
- Send blob messages (>9KB) individually (different API path)
- Clear buffer only after ALL API calls succeed to prevent data loss
- Improve error messages to include session context
Tests added for critical scenarios:
- Multiple sessions grouped into separate API calls
- Latest timestamp used for combined events
- Partial failure with multiple sessions preserves entire buffer
- Multiple blob messages sent individually (not batched)
- Mixed sessions with blobs and conversational messages
Also fixes pre-existing test issues:
- Fix test_read_agent_legacy_migration mock setup to match actual impl
- Fix test_load_long_term_memories_with_validation_failure for strands API
* refactor: rename flush_messages to _flush_messages (private method)
* refactor(tests): deduplicate batching test fixtures
Extract _create_session_manager helper to eliminate triple-nested
context managers and move batching_config/batching_session_manager
to module-level fixtures shared across all test classes.
* fix(memory): prevent flush exception from masking original exception in __exit__
Wrap _flush_messages() in try/except so that if the with-block raises
and flush also fails, the flush error is logged instead of replacing
the original exception.
* fix(memory): fix events_to_messages ordering for batched events
Iterate reversed(events) instead of reversing the flat message list
after iteration, which scrambled intra-event payload order for batched
events. Add 6 unit tests covering multi-event ordering, batched payload
ordering, mixed blob/conversational ordering, and malformed payloads.
* test(memory): rewrite integration tests to use real Agent objects
Replace 5 tests that called session manager methods directly (duplicating
unit test coverage) with 2 Agent-based integration tests that validate
the full lifecycle: context manager flush + session resume, and multi-turn
conversation with batching. Remove unnecessary time.sleep from blocking
close() calls.
* fix(tests): resolve ruff E501 line-too-long and F401 unused import
Break long test method signatures and constructor args to stay within
120-char limit. Remove unused json import.
* fix: remove unused imports in session_manager.py
Remove unused time, Callable, and TypeVar imports flagged by ruff.1 parent e8b63be commit fbce2fc
7 files changed
Lines changed: 1406 additions & 93 deletions
File tree
- src/bedrock_agentcore/memory
- integrations/strands
- tests_integ/memory/integrations
- tests/bedrock_agentcore/memory/integrations/strands
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
788 | 788 | | |
789 | 789 | | |
790 | 790 | | |
| 791 | + | |
791 | 792 | | |
792 | 793 | | |
793 | 794 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| 40 | + | |
0 commit comments