Repository navigation
Python: reuse Foundry session-store client within each response - #8785
Harsheet Shah (harsheet-shah) wants to merge 1 commit into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6e2efa47-2c75-4226-9544-174e3163a924
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Store cleanup failures can escape before the buffered terminal response event is emitted.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds request-scoped reuse of the Foundry session-store client while preserving per-request ownership.
Changes:
- Adds lazy, concurrency-safe client reuse and cleanup.
- Integrates the scope into normal and consent response paths.
- Adds lifecycle tests and documentation.
| File | Description |
|---|---|
_state_store.py |
Implements request-scoped client reuse. |
_responses.py |
Connects store lifecycle to response handling. |
test_state_store.py |
Tests reuse, concurrency, failure, and cleanup. |
test_responses.py |
Tests per-response client isolation. |
README.md |
Documents the new lifecycle. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| if isinstance(session_storage, FoundryAgentSessionStore): | ||
| await resources.enter_async_context( | ||
| session_storage._request_scope() # pyright: ignore[reportPrivateUsage] | ||
| ) |
|
Harsheet Shah (@harsheet-shah) This branch currently conflicts with |
|
Harsheet Shah (@harsheet-shah) GitHub could not merge current |
|
Harsheet Shah (@harsheet-shah) Thanks for the work here. The branch still conflicts with |

Motivation & Context
The default Foundry agent-session store resolves a new backing store for each load and save. A warm Responses request consequently performs a second store-metadata lookup and rebuilds the client before persisting the updated session.
Reuse the backing client only within that request, retaining awaited session persistence and deterministic cleanup without introducing a cross-request cache.
Description & Review Guide
FoundryAgentSessionStoreand enter it through the Responses host's existing request-owned exit stack. Load/save share one initialized backing client. Initialization is single-flight, unsuccessful initialization is not cached, and the client is closed when the request unwinds. Both normal execution and the existing consent-response session-preservation path use the scope.SessionStoreimplementations retain their existing lifecycle. The scope is lazy, so a new session does not acquire storage earlier than before.Related Issue
Partially addresses #8280.
This is an intentionally narrower alternative to #8281, which caches backing clients across requests for the event-loop lifetime. This PR retains per-request ownership and removes only the redundant initialization within each request. It does not modify or supersede #8281 automatically; the approaches should not be merged together unchanged.
Contribution Checklist