-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: pass RunContextWrapper to Session methods #3051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ..run_context import RunContextWrapper | ||
| from .session import Session | ||
|
|
||
| logger = logging.getLogger("openai-agents.openai.compaction") | ||
|
|
@@ -229,8 +230,12 @@ async def run_compaction(self, args: OpenAIResponsesCompactionArgs | None = None | |
| f"candidates={len(self._compaction_candidate_items)})" | ||
| ) | ||
|
|
||
| async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]: | ||
| return await self.underlying_session.get_items(limit) | ||
| async def get_items( | ||
| self, | ||
| limit: int | None = None, | ||
| wrapper: RunContextWrapper[Any] | None = None, | ||
| ) -> list[TResponseInputItem]: | ||
| return await self.underlying_session.get_items(limit, wrapper=wrapper) | ||
|
|
||
| async def _defer_compaction(self, response_id: str, store: bool | None = None) -> None: | ||
| if self._deferred_response_id is not None: | ||
|
|
@@ -258,8 +263,12 @@ def _get_deferred_compaction_response_id(self) -> str | None: | |
| def _clear_deferred_compaction(self) -> None: | ||
| self._deferred_response_id = None | ||
|
|
||
| async def add_items(self, items: list[TResponseInputItem]) -> None: | ||
| await self.underlying_session.add_items(items) | ||
| async def add_items( | ||
| self, | ||
| items: list[TResponseInputItem], | ||
| wrapper: RunContextWrapper[Any] | None = None, | ||
| ) -> None: | ||
| await self.underlying_session.add_items(items, wrapper=wrapper) | ||
| if self._compaction_candidate_items is not None: | ||
| new_items = _normalize_compaction_session_items(items) | ||
| new_candidates = select_compaction_candidate_items(new_items) | ||
|
|
@@ -268,15 +277,21 @@ async def add_items(self, items: list[TResponseInputItem]) -> None: | |
| if self._session_items is not None: | ||
| self._session_items.extend(_normalize_compaction_session_items(items)) | ||
|
|
||
| async def pop_item(self) -> TResponseInputItem | None: | ||
| popped = await self.underlying_session.pop_item() | ||
| async def pop_item( | ||
| self, | ||
| wrapper: RunContextWrapper[Any] | None = None, | ||
| ) -> TResponseInputItem | None: | ||
| popped = await self.underlying_session.pop_item(wrapper=wrapper) | ||
| if popped: | ||
| self._compaction_candidate_items = None | ||
| self._session_items = None | ||
| return popped | ||
|
|
||
| async def clear_session(self) -> None: | ||
| await self.underlying_session.clear_session() | ||
| async def clear_session( | ||
| self, | ||
| wrapper: RunContextWrapper[Any] | None = None, | ||
| ) -> None: | ||
| await self.underlying_session.clear_session(wrapper=wrapper) | ||
| self._compaction_candidate_items = [] | ||
| self._session_items = [] | ||
| self._deferred_response_id = None | ||
|
|
@@ -288,7 +303,9 @@ async def _ensure_compaction_candidates( | |
| if self._compaction_candidate_items is not None and self._session_items is not None: | ||
| return (self._compaction_candidate_items[:], self._session_items[:]) | ||
|
|
||
| history = _normalize_compaction_session_items(await self.underlying_session.get_items()) | ||
| history = _normalize_compaction_session_items( | ||
| await self.underlying_session.get_items() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| ) | ||
| candidates = select_compaction_candidate_items(history) | ||
| self._compaction_candidate_items = candidates | ||
| self._session_items = history | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ensure_compaction_candidatesaccepts awrapperbut still returns cached candidates/session items without checking which wrapper populated that cache. If oneOpenAIResponsesCompactionSessioninstance is reused across differentRunContextWrappers (the new feature this commit enables), the second context can reuse the first context’s cached history, causing compaction decisions and compacted inputs to be computed from the wrong tenant/user data.Useful? React with 👍 / 👎.