-
Notifications
You must be signed in to change notification settings - Fork 2.4k
added isolated code executor #3225
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
AlexFierro9
wants to merge
28
commits into
google:main
Choose a base branch
from
AlexFierro9:main
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.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f98c652
added isolated code executor
AlexFierro9 b3cdd32
Update src/google/adk/code_executors/isolated_code_executor.py
AlexFierro9 a77b0c2
Update src/google/adk/code_executors/isolated_code_executor.py
AlexFierro9 8fbd73b
added the units tests
AlexFierro9 9cf3ef0
Merge branch 'main' into main
AlexFierro9 98e5a83
Merge branch 'main' into main
AlexFierro9 ff06b44
Merge branch 'main' into main
hangfei ba5dbbd
Merge branch 'main' into main
AlexFierro9 b0f3f6c
run autoformat.sh
AlexFierro9 1e49154
Merge branch 'main' of https://github.com/AlexFierro9/adk-python
AlexFierro9 5d01c91
Merge branch 'main' into main
AlexFierro9 6424fc4
Merge remote-tracking branch 'upstream/main'
AlexFierro9 c1e3487
Merge branch 'main' into main
hangfei 039b9b9
Added Isolated code executor as a config option for unsafe local code…
AlexFierro9 0dc3c26
Merge branch 'main' of https://github.com/AlexFierro9/adk-python
AlexFierro9 6a9ee6b
Added Isolated Code Executor as a separate config
AlexFierro9 1cc3a2e
Merge branch 'main' into main
AlexFierro9 7603e57
got rid of isolated code executor file, added tests cases (with assis…
AlexFierro9 bf19756
Merge branch 'main' of https://github.com/AlexFierro9/adk-python
AlexFierro9 8f12cc6
Merge branch 'main' into main
AlexFierro9 453e786
changed use_isolated_process to use_separate_process, ran isort for f…
AlexFierro9 f742aea
Merge branch 'main' into main
AlexFierro9 ec4d631
Merge branch 'main' into main
hangfei 1c07fa0
Revert "Added Isolated Code Executor as a separate config"
AlexFierro9 f64a8ab
Merge branch 'main' of https://github.com/AlexFierro9/adk-python
AlexFierro9 5840c02
Merge branch 'main' into main
AlexFierro9 55ffc93
Merge branch 'main' into main
hangfei ba6a56c
Merge branch 'main' into main
hangfei 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
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
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
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
112 changes: 112 additions & 0 deletions
112
tests/unittests/code_executors/test_isolated_code_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,112 @@ | ||
| import os | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from google.adk.agents.base_agent import BaseAgent | ||
| from google.adk.agents.invocation_context import InvocationContext | ||
| from google.adk.code_executors.code_execution_utils import CodeExecutionInput | ||
| from google.adk.code_executors.code_execution_utils import CodeExecutionResult | ||
| from google.adk.code_executors.isolated_code_executor import IsolatedCodeExecutor | ||
| from google.adk.sessions.base_session_service import BaseSessionService | ||
| from google.adk.sessions.session import Session | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_invocation_context() -> InvocationContext: | ||
| """Provides a mock InvocationContext.""" | ||
| mock_agent = MagicMock(spec=BaseAgent) | ||
| mock_session = MagicMock(spec=Session) | ||
| mock_session_service = MagicMock(spec=BaseSessionService) | ||
| return InvocationContext( | ||
| invocation_id="test_invocation", | ||
| agent=mock_agent, | ||
| session=mock_session, | ||
| session_service=mock_session_service, | ||
| ) | ||
|
|
||
|
|
||
| class TestIsolatedCodeExecutor: | ||
|
|
||
| def test_init_default(self): | ||
| executor = IsolatedCodeExecutor() | ||
| assert not executor.stateful | ||
| assert not executor.optimize_data_file | ||
|
|
||
| def test_init_stateful_raises_error(self): | ||
| with pytest.raises( | ||
| ValueError, | ||
| match="Cannot set `stateful=True` in IsolatedCodeExecutor.", | ||
| ): | ||
| IsolatedCodeExecutor(stateful=True) | ||
|
|
||
| def test_init_optimize_data_file_raises_error(self): | ||
| with pytest.raises( | ||
| ValueError, | ||
| match=( | ||
| "Cannot set `optimize_data_file=True` in IsolatedCodeExecutor." | ||
| ), | ||
| ): | ||
| IsolatedCodeExecutor(optimize_data_file=True) | ||
|
|
||
| def test_execute_code_simple_print( | ||
| self, mock_invocation_context: InvocationContext | ||
| ): | ||
| executor = IsolatedCodeExecutor() | ||
| code_input = CodeExecutionInput(code='print("hello world")') | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
|
|
||
| assert isinstance(result, CodeExecutionResult) | ||
| assert result.stdout == "hello world\n" | ||
| assert result.stderr == "" | ||
| assert result.output_files == [] | ||
|
|
||
| def test_execute_code_with_error( | ||
| self, mock_invocation_context: InvocationContext | ||
| ): | ||
| executor = IsolatedCodeExecutor() | ||
| code_input = CodeExecutionInput(code='raise ValueError("Test error")') | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
|
|
||
| assert isinstance(result, CodeExecutionResult) | ||
| assert result.stdout == "" | ||
| assert "Test error" in result.stderr | ||
| assert result.output_files == [] | ||
|
|
||
| def test_execute_code_variable_assignment( | ||
| self, mock_invocation_context: InvocationContext | ||
| ): | ||
| executor = IsolatedCodeExecutor() | ||
| code_input = CodeExecutionInput(code="x = 10\nprint(x * 2)") | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
|
|
||
| assert result.stdout == "20\n" | ||
| assert result.stderr == "" | ||
|
|
||
| def test_execute_code_empty(self, mock_invocation_context: InvocationContext): | ||
| executor = IsolatedCodeExecutor() | ||
| code_input = CodeExecutionInput(code="") | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
| assert result.stdout == "" | ||
| assert result.stderr == "" | ||
|
|
||
| def test_execute_code_with_import( | ||
| self, mock_invocation_context: InvocationContext | ||
| ): | ||
| executor = IsolatedCodeExecutor() | ||
| code = "import os; print(os.linesep)" | ||
| code_input = CodeExecutionInput(code=code) | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
|
|
||
| assert result.stdout.strip() == os.linesep.strip() | ||
AlexFierro9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert result.stderr == "" | ||
|
|
||
| def test_execute_code_multiline_output( | ||
| self, mock_invocation_context: InvocationContext | ||
| ): | ||
| executor = IsolatedCodeExecutor() | ||
| code = 'print("line 1")\nprint("line 2")' | ||
| code_input = CodeExecutionInput(code=code) | ||
| result = executor.execute_code(mock_invocation_context, code_input) | ||
|
|
||
| assert result.stdout == "line 1\nline 2\n" | ||
| assert result.stderr == "" | ||
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
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.
The test asserts that "Test error" is in
result.stderr. It would be more robust to assert that theValueErrorexception type is also present in thestderr. This ensures that the correct exception is being raised and captured.