feat(session): add DatabaseSessionManager for PostgreSQL/MySQL/SQLite support #1031
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds
DatabaseSessionManager
to enable SQL database persistence for agent sessions, providing an alternative to file-based and S3-based session storage.Why Database Sessions?
Key Features
Implementation Details
DatabaseSessionManager Class (
src/strands/session/database_session_manager.py
)SessionRepository
interface viaRepositorySessionManager
_db_session
) for transaction management with auto-commit and rollback_validate_session_id
,_validate_agent_id
,_validate_ids
__del__
for owned enginesDatabase Models (
src/strands/session/database_models.py
)StrandsSession
: Session metadata (id, type, timestamps)StrandsAgent
: Agent state and conversation manager state (JSON columns)StrandsMessage
: Messages with optional redaction (JSON columns, append-only)Alembic Migrations (
alembic/
)Optional Dependencies (
pyproject.toml
)database
optional dependency groupsqlalchemy>=2.0.0
,alembic>=1.13.0
,psycopg2-binary
,pymysql
all
extras groupComprehensive Testing (
tests/strands/session/test_database_session_manager.py
)Usage Example
1. Set up database schema with Alembic
2. Use DatabaseSessionManager in your code
Design Decisions
S3SessionManager
andFileSessionManager
update_message
, matching S3SessionManager behaviorpool_pre_ping
for robustnessRelated Issues
N/A - This is a new feature implementation
Documentation PR
Docs PR: strands-agents/docs#285
Comprehensive documentation has been added to the docs repository, including:
Type of Change
New feature
Testing
hatch test
) - 114 tests including 43 new tests for DatabaseSessionManagerhatch fmt --linter
)hatch fmt --formatter
)Test Coverage
The implementation includes comprehensive test coverage:
Checklist
Additional Notes
feat(session): ...
)By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.