Skip to content

Conversation

mkmeral
Copy link

@mkmeral mkmeral commented Sep 12, 2025

Description

This PR introduces comprehensive session management functionality to the Strands Agent Builder, enabling users to persist and resume conversation sessions across CLI invocations. The implementation provides a secure, robust foundation for maintaining conversation context between terminal sessions.

Key Features Added:

🔄 Session Persistence & Management

  • Automatic Session Storage: Conversations are automatically saved using FileSessionManager integration
  • Session Resumption: Resume previous conversations with full context and history display
  • Session Discovery: List and manage existing sessions with metadata (creation date, message count)

🖥️ Enhanced CLI Interface

  • New CLI Arguments:
    • --session-path <path> - Specify session storage directory
    • --session-id <id> - Resume specific session or create with custom ID
    • --list-sessions - Display all available sessions with details
  • Environment Variable: STRANDS_SESSION_PATH for default session storage location
  • Interactive Commands:
    • !session info - Show current session details
    • !session list - List all available sessions
    • !session - Display help for session commands

🎨 Rich User Experience

  • Colored Output: Consistent Rich-formatted messages with color coding
  • Session History Display: Visual conversation history when resuming sessions
  • Progress Indicators: Clear feedback for session operations
  • Error Handling: Graceful error messages with actionable guidance

Implementation Details:

Core Architecture

  • session_utils.py: Complete session management utility module with 15+ functions
  • Session Validation: validate_session_id() and validate_session_path() for security
  • Session Lifecycle: Create, list, resume, and manage session operations
  • Integration: Seamless integration with existing Agent class and CLI workflow

Related Issues

Adding a way to store/continue conversations #31

Documentation PR

[No associated documentation PR at this time]

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

  • Added unit tests
  • Manually tested functionality

Automated Testing

  • hatch fmt --linter - All linting checks pass ✅
  • hatch fmt --formatter - Code formatting is consistent ✅
  • hatch test - All existing and new tests pass ✅

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

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

Murat Kaan Meral added 6 commits September 12, 2025 12:04
…variables

- Update mocking patterns to properly isolate tests from environment variables
- Mock os.getenv, os.getcwd, and Path to ensure consistent test behavior
- Fixes tests that were failing due to environment variable interference
@mkmeral mkmeral requested a review from a team as a code owner September 12, 2025 15:20
**Configuration:**
- Use `--session-path <path>` to specify where sessions are stored
- Or set `STRANDS_SESSION_PATH` environment variable
- Sessions are only enabled when a path is provided
Copy link
Member

Choose a reason for hiding this comment

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

I dont think we need a path defined, it uses the temp dir automatically. Can we make this parameter optional?

# Create session manager
session_manager = create_session_manager(resolved_session_id, session_base_path)
if session_manager:
resolved_session_id = session_manager.session_id
Copy link
Member

Choose a reason for hiding this comment

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

why do you keep setting resolved_session_id? Is it different then the one passed in?

) -> Optional[FileSessionManager]:
"""Create a FileSessionManager with the given or generated session ID. Returns None if no base_path."""
if not base_path or not validate_session_path(base_path):
return None
Copy link
Member

Choose a reason for hiding this comment

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

Should we raise here instead?

return session_manager, resolved_session_id, is_resuming


def handle_session_commands(command: str, session_id: Optional[str], session_base_path: Optional[str]) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

Can you document what the expected failure cases are? If we are going to rely on session persistence, then we should probably fail if its not working.

console.print(f"[bold cyan]Total messages:[/bold cyan] {info['total_messages']}")
return True

elif command == "session list" and session_base_path:
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if the keyword command handling was all done in the strands.py file, and we just have a list_sessions function in this file. I think that would be a bit more readable. That way we can remove all (or most) of the console printing logic from this file

Copy link
Author

Choose a reason for hiding this comment

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

I actually have more changes on top of it. I am extracting commands to commands_util.py and i want to use prompt toolkit for autocomplete. I think it'll be useful later when we bring mcp prompt support too

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.

2 participants