feat(code-interpreter): add session persistence and reconnection #308
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 enhances the AgentCore Code Interpreter integration to support automatic session persistence and reconnection across invocations in long-running AgentCore runtimes. This eliminates session lifecycle mismatches, prevents tool execution failures, and significantly improves performance.
Implement automatic session lifecycle management and cross-invocation session
reconnection for AgentCore Code Interpreter integration. This enhancement enables
sessions to persist across invocations in long-running AgentCore runtimes,
eliminating session recreation overhead and preventing tool execution failures.
Key changes:
This resolves session lifecycle mismatches between short-lived Python objects
and long-lived AgentCore sessions, improving performance by 69-72% and achieving
0% tool failure rate in multi-turn conversations.
Fixes: Session validation errors and unnecessary session recreation
Related: AgentCore runtime integration, Strands Agent framework
🎯 Problem Statement
The Session Lifecycle Mismatch
AgentCore Runtime Architecture:
@app.entrypointfunction callPrevious Integration Behavior:
AgentCoreCodeInterpreterinstance created on every invocation__del__()→ session cleanup✅ Solution Overview
This PR implements three key enhancements:
1. Session Persistence Control
Add
persist_sessionsflag (default:True) to prevent session cleanup on object destruction.2. Session Verification and Reconnection
Add
verify_session_before_useflag (default:True) to verify and reconnect to existing sessions.3. Session Discovery Mechanism
Use
list_sessions()API with name matching to find and reconnect to existing sessions across object instances.Type of Change
New Tool
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareTest Scenarios Validated:
First invocation creates session:
Second invocation reconnects:
Session name validation:
Session not found handling:
Session cleanup (when persist_sessions=False):
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.