Skip to content

Conversation

@sundargthb
Copy link
Contributor

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:

  • Add persist_sessions flag to prevent premature session cleanup
  • Add verify_session_before_use flag for session verification and reconnection
  • Implement list_sessions()-based session discovery for cross-instance reconnection
  • Add session name validation and cleaning for AWS CodeInterpreter constraints
  • Store session name-to-ID mapping for reliable session lookup

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:

  • Long-running Python process that persists across invocations
  • Each user request triggers a new @app.entrypoint function call
  • Code Interpreter sessions are designed to persist across these invocations

Previous Integration Behavior:

  • New AgentCoreCodeInterpreter instance created on every invocation
  • Python object destruction triggered __del__() → session cleanup
  • Next invocation had no knowledge of previous sessions
  • Resulted in continuous session creation/destruction cycle

✅ Solution Overview

This PR implements three key enhancements:

1. Session Persistence Control

Add persist_sessions flag (default: True) to prevent session cleanup on object destruction.

2. Session Verification and Reconnection

Add verify_session_before_use flag (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

  • [ x] I ran hatch run prepare

Test Scenarios Validated:

  1. First invocation creates session:

    ✅ Session created with cleaned name
    ✅ Session ID stored
    ✅ Tool execution successful
    
  2. Second invocation reconnects:

    ✅ list_sessions() called
    ✅ Session found by name
    ✅ Reconnected to existing session
    ✅ No new session created
    ✅ Tool execution successful
    
  3. Session name validation:

    Input:  "16e4dcba-5792-4643-9e54-42b43dc58637"
    Output: "16e4dcba579246439e5442b43dc58637"
    ✅ Meets validation: [0-9a-zA-Z]{1,40}
    
  4. Session not found handling:

    ✅ list_sessions() returns no match
    ✅ Falls back to auto_create
    ✅ New session created successfully
    
  5. Session cleanup (when persist_sessions=False):

    ✅ Verifies session status before stopping
    ✅ Skips already-stopped sessions
    ✅ No errors from double-stop attempts
    

Checklist

  • [x ] I have read the CONTRIBUTING document
  • [x ] I have added any necessary tests that prove my fix is effective or my feature works
  • [x ] I have updated the documentation accordingly
  • [x ] I have added an appropriate example to the documentation to outline the feature
  • [x ] My changes generate no new warnings
  • [x ] 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.

@sundargthb sundargthb requested a review from a team as a code owner November 11, 2025 04:56
@sundargthb sundargthb force-pushed the feat-ci-session-persistence branch from cebd04f to d0c2afe Compare November 11, 2025 05:02
… get_session(aws_session_id) calls without needing verification.
@dbschmigelski dbschmigelski merged commit 6322306 into strands-agents:main Nov 18, 2025
16 checks passed
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.

3 participants