-
Notifications
You must be signed in to change notification settings - Fork 671
fix: avoid nil session dereference in OpenAI local mode #2233
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
Open
anxkhn
wants to merge
2
commits into
kagent-dev:main
Choose a base branch
from
anxkhn:fix/openai-local-nil-session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
python/packages/kagent-openai/tests/test_agent_executor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for OpenAIAgentExecutor session handling.""" | ||
|
|
||
| import uuid | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from a2a.server.agent_execution.context import RequestContext | ||
| from a2a.server.events.event_queue import EventQueue | ||
| from a2a.types import Message, Part, Role, TextPart | ||
|
|
||
| from kagent.openai._agent_executor import OpenAIAgentExecutor | ||
|
|
||
|
|
||
| def _make_context() -> MagicMock: | ||
| context = MagicMock(spec=RequestContext) | ||
| context.message = Message( | ||
| message_id=str(uuid.uuid4()), | ||
| role=Role.user, | ||
| parts=[Part(TextPart(text="hello"))], | ||
| ) | ||
| context.current_task = None | ||
| context.task_id = "task-1" | ||
| context.context_id = "ctx-1" | ||
| context.session_id = None | ||
| context.get_user_input.return_value = "hello" | ||
| return context | ||
|
|
||
|
|
||
| def _make_streamed_result() -> MagicMock: | ||
| async def _stream_events(): | ||
| for _ in (): | ||
| yield None | ||
|
|
||
| result = MagicMock() | ||
| result.stream_events = _stream_events | ||
| result.final_output = "done" | ||
| return result | ||
|
|
||
|
|
||
| class TestLocalModeSession: | ||
| """The executor built by build_local() has session_factory=None.""" | ||
|
|
||
| @patch("kagent.openai._agent_executor.Runner.run_streamed") | ||
| async def test_execute_without_session_factory_does_not_raise(self, mock_run_streamed): | ||
| mock_run_streamed.return_value = _make_streamed_result() | ||
|
|
||
| executor = OpenAIAgentExecutor(agent=MagicMock(), app_name="test", session_factory=None) | ||
| context = _make_context() | ||
| event_queue = AsyncMock(spec=EventQueue) | ||
|
|
||
| await executor.execute(context, event_queue) | ||
|
|
||
| mock_run_streamed.assert_called_once() | ||
|
|
||
| @patch("kagent.openai._agent_executor.Runner.run_streamed") | ||
| async def test_session_context_falls_back_to_context_id(self, mock_run_streamed): | ||
| mock_run_streamed.return_value = _make_streamed_result() | ||
|
|
||
| executor = OpenAIAgentExecutor(agent=MagicMock(), app_name="test", session_factory=None) | ||
| context = _make_context() | ||
| event_queue = AsyncMock(spec=EventQueue) | ||
|
|
||
| await executor.execute(context, event_queue) | ||
|
|
||
| session_context = mock_run_streamed.call_args.kwargs["context"] | ||
| assert session_context.session_id == context.context_id | ||
|
|
||
| @patch("kagent.openai._agent_executor.Runner.run_streamed") | ||
| async def test_session_context_uses_context_session_id(self, mock_run_streamed): | ||
| mock_run_streamed.return_value = _make_streamed_result() | ||
|
|
||
| executor = OpenAIAgentExecutor(agent=MagicMock(), app_name="test", session_factory=None) | ||
| context = _make_context() | ||
| context.session_id = "session-1" | ||
| event_queue = AsyncMock(spec=EventQueue) | ||
|
|
||
| await executor.execute(context, event_queue) | ||
|
|
||
| session_context = mock_run_streamed.call_args.kwargs["context"] | ||
| assert session_context.session_id == context.session_id |
Oops, something went wrong.
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.
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.
execute() already computes this same fallback and could just pass that session_id into _stream_agent_events instead of recomputing it here.