-
Notifications
You must be signed in to change notification settings - Fork 2.7k
prevent infinite loop in code_executor by preserving code execute #3941
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 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
|
Collaborator
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. how is the changes in this file related to the fix ? |
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 |
|---|---|---|
|
|
@@ -65,7 +65,7 @@ async def check_prime(nums: list[int]) -> str: | |
|
|
||
|
|
||
| root_agent = Agent( | ||
| model='gemini-2.0-flash', | ||
| model='gemini-pro', | ||
|
Collaborator
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. is it a valid model ? |
||
| name='hello_world_agent', | ||
| description=( | ||
| 'hello world agent that can roll a dice of 8 sides and check prime' | ||
|
|
@@ -98,6 +98,7 @@ async def check_prime(nums: list[int]) -> str: | |
| # ), | ||
| # ), | ||
| generate_content_config=types.GenerateContentConfig( | ||
| max_output_tokens=100, | ||
| safety_settings=[ | ||
| types.SafetySetting( # avoid false alarm about rolling dice. | ||
| category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Test to verify the code executor infinite loop fix.""" | ||
| import asyncio | ||
| from google.adk import Agent | ||
| from google.adk.code_executors import UnsafeLocalCodeExecutor | ||
| from google.adk.runners import InMemoryRunner | ||
| from google.genai import types | ||
|
|
||
|
|
||
| async def test_code_executor(): | ||
| """Test that code executor doesn't cause infinite loop.""" | ||
| root_agent = Agent( | ||
| model='gemini-pro', | ||
| name='hello_world_agent', | ||
| description='hello world agent.', | ||
| instruction=""" | ||
| you are an agent that knows how to produce python code. | ||
| """, | ||
| code_executor=UnsafeLocalCodeExecutor(), | ||
| ) | ||
|
|
||
| app_name = 'test_app' | ||
| user_id = 'test_user' | ||
| runner = InMemoryRunner( | ||
| agent=root_agent, | ||
| app_name=app_name, | ||
| ) | ||
|
|
||
| session = await runner.session_service.create_session( | ||
| app_name=app_name, user_id=user_id | ||
| ) | ||
|
|
||
| content = types.Content( | ||
| role='user', | ||
| parts=[types.Part.from_text(text='write python code that computes 2+2.')] | ||
| ) | ||
|
|
||
| print('Sending request to agent...') | ||
| event_count = 0 | ||
| max_events = 20 # Prevent true infinite loop in test | ||
|
|
||
| async for event in runner.run_async( | ||
| user_id=user_id, | ||
| session_id=session.id, | ||
| new_message=content, | ||
| ): | ||
| event_count += 1 | ||
| print(f'Event {event_count}: {event.author} - {type(event.content).__name__ if event.content else "no content"}') | ||
|
|
||
| if event.content and event.content.parts: | ||
| for part in event.content.parts: | ||
| if part.text: | ||
| print(f' Text: {part.text[:100]}...' if len(part.text) > 100 else f' Text: {part.text}') | ||
| if part.executable_code: | ||
| print(f' Code: {part.executable_code.code[:100]}...') | ||
| if part.code_execution_result: | ||
| print(f' Result: {part.code_execution_result.output[:100] if part.code_execution_result.output else "empty"}') | ||
|
|
||
| if event_count >= max_events: | ||
| print(f'ERROR: Reached {max_events} events - likely infinite loop!') | ||
| return False | ||
|
|
||
| print(f'\nTest completed successfully with {event_count} events') | ||
| return event_count < max_events | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import sys | ||
| success = asyncio.run(test_code_executor()) | ||
| if success: | ||
| print('\n✓ Test PASSED - No infinite loop detected') | ||
| else: | ||
| print('\n✗ Test FAILED - Infinite loop detected') | ||
| sys.exit(1) | ||
YatindraRai002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
remove empty lines