Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/google/adk/flows/llm_flows/_code_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import annotations

import asyncio
import base64
import copy
import dataclasses
Expand Down Expand Up @@ -245,7 +246,8 @@ async def _run_pre_processor(
content=code_content,
)

code_execution_result = code_executor.execute_code(
code_execution_result = await asyncio.to_thread(
code_executor.execute_code,
invocation_context,
CodeExecutionInput(
code=code_str,
Expand Down Expand Up @@ -355,7 +357,8 @@ async def _run_post_processor(
actions=EventActions(),
)

code_execution_result = code_executor.execute_code(
code_execution_result = await asyncio.to_thread(
code_executor.execute_code,
invocation_context,
CodeExecutionInput(
code=code_str,
Expand Down
127 changes: 127 additions & 0 deletions tests/unittests/flows/llm_flows/test_code_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,65 @@
"""Unit tests for Code Execution logic."""

import ast
import asyncio
import datetime
import threading
from typing import Optional
from unittest.mock import AsyncMock
from unittest.mock import MagicMock
from unittest.mock import patch

from google.adk.agents.llm_agent import Agent
from google.adk.code_executors.base_code_executor import BaseCodeExecutor
from google.adk.code_executors.built_in_code_executor import BuiltInCodeExecutor
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.code_execution_utils import File
from google.adk.flows.llm_flows._code_execution import _DATA_FILE_HELPER_LIB
from google.adk.flows.llm_flows._code_execution import _get_data_file_preprocessing_code
from google.adk.flows.llm_flows._code_execution import request_processor
from google.adk.flows.llm_flows._code_execution import response_processor
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.genai import types
import pytest

from ... import testing_utils


class _ExecutionRecord:
"""Captures how the executor ran, for cross-thread inspection in tests."""

def __init__(self):
self.thread: Optional[threading.Thread] = None
self.released: Optional[bool] = None


class _RecordingCodeExecutor(BaseCodeExecutor):
"""A code executor that records the thread it runs on.

`execute_code` blocks on `release` so tests can verify it is offloaded from
the event loop: it records the running thread, signals `started`, then waits
for `release` before returning.
"""

model_config = {'arbitrary_types_allowed': True}

started: threading.Event
release: threading.Event
record: _ExecutionRecord

def execute_code(
self,
invocation_context,
code_execution_input: CodeExecutionInput,
) -> CodeExecutionResult:
self.record.thread = threading.current_thread()
self.started.set()
self.record.released = self.release.wait(timeout=2)
return CodeExecutionResult(stdout='ok')


@pytest.mark.asyncio
@patch('google.adk.flows.llm_flows._code_execution.datetime')
async def test_builtin_code_executor_image_artifact_creation(mock_datetime):
Expand Down Expand Up @@ -207,3 +246,91 @@ def test_get_data_file_preprocessing_code_injection_reproduction():
break

assert read_csv_arg == bad_filename


@pytest.mark.asyncio
async def test_post_processor_does_not_block_event_loop():
"""Response processor offloads blocking execute_code off the event loop."""
started = threading.Event()
release = threading.Event()
record = _ExecutionRecord()
loop_ran = False
code_executor = _RecordingCodeExecutor(
started=started, release=release, record=record
)
agent = Agent(name='test_agent', code_executor=code_executor)
invocation_context = await testing_utils.create_invocation_context(
agent=agent, user_content='test message'
)
invocation_context.artifact_service = MagicMock()
invocation_context.artifact_service.save_artifact = AsyncMock()

llm_response = LlmResponse(
content=types.Content(
parts=[types.Part(text='```python\nprint("hello")\n```')]
)
)

async def _release_when_started():
nonlocal loop_ran
while not started.is_set():
await asyncio.sleep(0.001)
loop_ran = True
release.set()

releaser = asyncio.create_task(_release_when_started())
_ = [
event
async for event in response_processor.run_async(
invocation_context, llm_response
)
]
await releaser

assert record.thread is not threading.main_thread()
assert record.released is True
assert loop_ran is True


@pytest.mark.asyncio
async def test_pre_processor_runs_execute_code_off_the_loop():
"""Request processor offloads blocking execute_code off the event loop."""
started = threading.Event()
release = threading.Event()
release.set()
record = _ExecutionRecord()
code_executor = _RecordingCodeExecutor(
started=started,
release=release,
record=record,
optimize_data_file=True,
)
agent = Agent(name='test_agent', code_executor=code_executor)
invocation_context = await testing_utils.create_invocation_context(
agent=agent, user_content='test message'
)

llm_request = LlmRequest(
contents=[
types.Content(
role='user',
parts=[
types.Part(
inline_data=types.Blob(
mime_type='text/csv',
data=b'col1,col2\n1,2\n',
)
)
],
)
]
)

_ = [
event
async for event in request_processor.run_async(
invocation_context, llm_request
)
]

assert record.thread is not threading.main_thread()