A programming framework for agentic AI
Code execute cancellation (#299)
peterychang136af65b7407
diff --git a/python/benchmarks/HumanEval/Templates/TwoAgents/scenario.py b/python/benchmarks/HumanEval/Templates/TwoAgents/scenario.pyindex cadd340a7..b17c106a6 100644--- a/python/benchmarks/HumanEval/Templates/TwoAgents/scenario.py+++ b/python/benchmarks/HumanEval/Templates/TwoAgents/scenario.py@@ -169,7 +169,7 @@ class Executor(TypeRoutedAgent):code = self._extract_execution_request(message.execution_request)if code is not None:execution_requests = [CodeBlock(code=code, language="python")]- result = await self._executor.execute_code_blocks(execution_requests)+ result = await self._executor.execute_code_blocks(execution_requests, cancellation_token)await self.publish_message(CodeExecutionResultMessage(output=result.output,diff --git a/python/samples/patterns/coder_executor.py b/python/samples/patterns/coder_executor.pyindex c64d60201..0246c3f20 100644--- a/python/samples/patterns/coder_executor.py+++ b/python/samples/patterns/coder_executor.py@@ -155,7 +155,9 @@ class Executor(TypeRoutedAgent):)return# Execute code blocks.- result = await self._executor.execute_code_blocks(code_blocks=code_blocks)+ result = await self._executor.execute_code_blocks(+ code_blocks=code_blocks, cancellation_token=cancellation_token+ )# Publish the code execution result.await self.publish_message(CodeExecutionTaskResult(output=result.output, exit_code=result.exit_code, session_id=message.session_id),diff --git a/python/src/agnext/components/code_executor/_base.py b/python/src/agnext/components/code_executor/_base.pyindex c4b3c02de..4feaff860 100644--- a/python/src/agnext/components/code_executor/_base.py+++ b/python/src/agnext/components/code_executor/_base.py@@ -6,6 +6,8 @@ from __future__ import annotationsfrom dataclasses import dataclassfrom typing import List, Protocol, runtime_checkable+from agnext.core import CancellationToken+@dataclassclass CodeBlock:@@ -27,7 +29,9 @@ class CodeResult:class CodeExecutor(Protocol):"""Executes code blocks and returns the result."""- async def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CodeResult:+ async def execute_code_blocks(+ self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken+ ) -> CodeResult:"""Execute code blocks and return the result.This method should be implemented by the code executor.diff --git a/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py b/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.pyindex aa10507c4..686835df2 100644--- a/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py+++ b/python/src/agnext/components/code_executor/_impl/local_commandline_code_executor.py@@ -8,7 +8,7 @@ import warningsfrom hashlib import md5from pathlib import Pathfrom string import Template-from typing import Any, Callable, ClassVar, List, Sequence, Union, Optional+from typing import Any, Callable, ClassVar, List, Sequence, Unionfrom typing_extensions import ParamSpec@@ -22,7 +22,6 @@ from .._func_with_reqs import ()from .command_line_code_result import CommandLineCodeResultfrom .utils import PYTHON_VARIANTS, get_file_name_from_content, lang_to_cmd, silence_pip # type: ignore-from ....core import CancellationToken__all__ = ("LocalCommandLineCodeExecutor",)@@ -146,7 +145,7 @@ $functions""""""(Experimental) The working directory for the code execution."""return self._work_dir- async def _setup_functions(self, cancellation_token: Optional[CancellationToken]) -> None:+ async def _setup_functions(self, cancellation_token: CancellationToken) -> None:func_file_content = build_python_functions_file(self._functions)func_file = self._work_dir / f"{self._functions_module}.py"func_file.write_text(func_file_content)@@ -170,8 +169,7 @@ $functions"""stderr=asyncio.subprocess.PIPE,))- if cancellation_token:- cancellation_token.link_future(task)+ cancellation_token.link_future(task)try:proc = await taskstdout, stderr = await asyncio.wait_for(proc.communicate(), self._timeout)@@ -194,13 +192,13 @@ $functions"""self._setup_functions_complete = Trueasync def execute_code_blocks(- self, code_blocks: List[CodeBlock], cancellation_token: Optional[CancellationToken] = None+ self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken) -> CommandLineCodeResult:"""(Experimental) Execute the code blocks and return the result.Args:code_blocks (List[CodeBlock]): The code blocks to execute.- cancellation_token (CancellationToken|None): an optional token to cancel the operation+ cancellation_token (CancellationToken): a token to cancel the operationReturns:CommandLineCodeResult: The result of the code execution."""@@ -211,7 +209,7 @@ $functions"""return await self._execute_code_dont_check_setup(code_blocks, cancellation_token)async def _execute_code_dont_check_setup(- self, code_blocks: List[CodeBlock], cancellation_token: Optional[CancellationToken]+ self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken) -> CommandLineCodeResult:logs_all: str = ""file_names: List[Path] = []@@ -262,8 +260,7 @@ $functions"""stderr=asyncio.subprocess.PIPE,))- if cancellation_token:- cancellation_token.link_future(task)+ cancellation_token.link_future(task)try:proc = await taskstdout, stderr = await asyncio.wait_for(proc.communicate(), self._timeout)diff --git a/python/src/agnext/components/tools/_code_execution.py b/python/src/agnext/components/tools/_code_execution.pyindex 6f34f9247..40a9f6fb6 100644--- a/python/src/agnext/components/tools/_code_execution.py+++ b/python/src/agnext/components/tools/_code_execution.py@@ -25,6 +25,8 @@ class PythonCodeExecutionTool(BaseTool[CodeExecutionInput, CodeExecutionResult])async def run(self, args: CodeExecutionInput, cancellation_token: CancellationToken) -> CodeExecutionResult:code_blocks = [CodeBlock(code=args.code, language="python")]- result = await self._executor.execute_code_blocks(code_blocks=code_blocks)+ result = await self._executor.execute_code_blocks(+ code_blocks=code_blocks, cancellation_token=cancellation_token+ )return CodeExecutionResult(success=result.exit_code == 0, output=result.output)diff --git a/python/teams/team-one/src/team_one/agents/coder.py b/python/teams/team-one/src/team_one/agents/coder.pyindex 6b8d62538..0e984f550 100644--- a/python/teams/team-one/src/team_one/agents/coder.py+++ b/python/teams/team-one/src/team_one/agents/coder.py@@ -80,7 +80,7 @@ class Executor(BaseWorker):code = self._extract_execution_request(message_content_to_str(message.content))if code is not None:execution_requests = [CodeBlock(code=code, language="python")]- result = await self._executor.execute_code_blocks(execution_requests)+ result = await self._executor.execute_code_blocks(execution_requests, cancellation_token)if result.output.strip() == "":# Sometimes agents forget to print(). Remind the to print somethingdiff --git a/python/tests/execution/test_commandline_code_executor.py b/python/tests/execution/test_commandline_code_executor.pyindex f49a0033b..5fbe0a8bd 100644--- a/python/tests/execution/test_commandline_code_executor.py+++ b/python/tests/execution/test_commandline_code_executor.py@@ -1,12 +1,14 @@# File based from: https://github.com/microsoft/autogen/blob/main/test/coding/test_commandline_code_executor.py# Credit to original authors+import asyncioimport sysimport tempfilefrom pathlib import Pathimport pytestfrom agnext.components.code_executor import CodeBlock, LocalCommandLineCodeExecutor+from agnext.core import CancellationTokenUNIX_SHELLS = ["bash", "sh", "shell"]WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]@@ -15,12 +17,12 @@ PYTHON_VARIANTS = ["python", "Python", "py"]@pytest.mark.asyncioasync def test_execute_code() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(work_dir=temp_dir)-# Test single code block.code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")]- code_result = await executor.execute_code_blocks(code_blocks)+ code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None# Test multiple code blocks.@@ -28,7 +30,7 @@ async def test_execute_code() -> None:CodeBlock(code="import sys; print('hello world!')", language="python"),CodeBlock(code="a = 100 + 100; print(a)", language="python"),]- code_result = await executor.execute_code_blocks(code_blocks)+ code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)assert (code_result.exit_code == 0and "hello world!" in code_result.output@@ -39,13 +41,13 @@ async def test_execute_code() -> None:# Test bash script.if sys.platform not in ["win32"]:code_blocks = [CodeBlock(code="echo 'hello world!'", language="bash")]- code_result = await executor.execute_code_blocks(code_blocks)+ code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None# Test running code.file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")]- code_result = await executor.execute_code_blocks(code_blocks)+ code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)assert (code_result.exit_code == 0and "hello world!" in code_result.output@@ -62,11 +64,26 @@ async def test_execute_code() -> None:@pytest.mark.asyncioasync def test_commandline_code_executor_timeout() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(timeout=1, work_dir=temp_dir)code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]- code_result = await executor.execute_code_blocks(code_blocks)+ code_result = await executor.execute_code_blocks(code_blocks, cancellation_token)assert code_result.exit_code and "Timeout" in code_result.output+async def test_commandline_code_executor_cancellation() -> None:+ with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()+ executor = LocalCommandLineCodeExecutor(work_dir=temp_dir)+ code_blocks = [CodeBlock(code="import time; time.sleep(10); print('hello world!')", language="python")]++ coro = executor.execute_code_blocks(code_blocks, cancellation_token)++ await asyncio.sleep(1)+ cancellation_token.cancel()+ code_result = await coro++ assert code_result.exit_code and "Cancelled" in code_result.outputdef test_local_commandline_code_executor_restart() -> None:executor = LocalCommandLineCodeExecutor()@@ -77,24 +94,26 @@ def test_local_commandline_code_executor_restart() -> None:@pytest.mark.asyncioasync def test_invalid_relative_path() -> None:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor()code = """# filename: /tmp/test.pyprint("hello world")"""- result = await executor.execute_code_blocks([CodeBlock(code=code, language="python")])+ result = await executor.execute_code_blocks([CodeBlock(code=code, language="python")], cancellation_token=cancellation_token)assert result.exit_code == 1 and "Filename is not in the workspace" in result.output@pytest.mark.asyncioasync def test_valid_relative_path() -> None:with tempfile.TemporaryDirectory() as temp_dir_str:+ cancellation_token = CancellationToken()temp_dir = Path(temp_dir_str)executor = LocalCommandLineCodeExecutor(work_dir=temp_dir)code = """# filename: test.pyprint("hello world")"""- result = await executor.execute_code_blocks([CodeBlock(code=code, language="python")])+ result = await executor.execute_code_blocks([CodeBlock(code=code, language="python")], cancellation_token=cancellation_token)assert result.exit_code == 0assert "hello world" in result.outputassert result.code_file is not Nonediff --git a/python/tests/execution/test_user_defined_functions.py b/python/tests/execution/test_user_defined_functions.pyindex 2176f0b65..58a33fd20 100644--- a/python/tests/execution/test_user_defined_functions.py+++ b/python/tests/execution/test_user_defined_functions.py@@ -11,7 +11,7 @@ from agnext.components.code_executor import (LocalCommandLineCodeExecutor,with_requirements,)-+from agnext.core import CancellationTokendef add_two_numbers(a: int, b: int) -> int:"""Add two numbers together."""@@ -50,6 +50,7 @@ def function_missing_reqs() -> "polars.DataFrame":@pytest.mark.asyncioasync def test_can_load_function_with_reqs() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[load_data])@@ -63,7 +64,8 @@ print(data['name'][0])"""result = await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)assert result.output == "John\n"assert result.exit_code == 0@@ -72,6 +74,7 @@ print(data['name'][0])"""@pytest.mark.asyncioasync def test_can_load_function() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[add_two_numbers])@@ -81,7 +84,8 @@ print(add_two_numbers(1, 2))"""result = await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)assert result.output == "3\n"assert result.exit_code == 0@@ -90,6 +94,7 @@ print(add_two_numbers(1, 2))"""@pytest.mark.asyncioasync def test_fails_for_function_incorrect_import() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[function_incorrect_import])@@ -100,13 +105,15 @@ function_incorrect_import()"""await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)@pytest.mark.asyncioasync def test_fails_for_function_incorrect_dep() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()executor = LocalCommandLineCodeExecutor(work_dir=temp_dir, functions=[function_incorrect_dep])@@ -117,7 +124,8 @@ function_incorrect_dep()"""await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)@@ -159,6 +167,7 @@ def add_two_numbers(a: int, b: int) -> int:@pytest.mark.asyncioasync def test_can_load_str_function_with_reqs() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()func = FunctionWithRequirements.from_str('''def add_two_numbers(a: int, b: int) -> int:@@ -174,7 +183,8 @@ print(add_two_numbers(1, 2))"""result = await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)assert result.output == "3\n"assert result.exit_code == 0@@ -195,6 +205,7 @@ invaliddef add_two_numbers(a: int, b: int) -> int:@pytest.mark.asyncioasync def test_cant_run_broken_str_function_with_reqs() -> None:with tempfile.TemporaryDirectory() as temp_dir:+ cancellation_token = CancellationToken()func = FunctionWithRequirements.from_str('''def add_two_numbers(a: int, b: int) -> int:@@ -210,7 +221,8 @@ print(add_two_numbers(object(), False))"""result = await executor.execute_code_blocks(code_blocks=[CodeBlock(language="python", code=code),- ]+ ],+ cancellation_token=cancellation_token)assert "TypeError: unsupported operand type(s) for +:" in result.outputassert result.exit_code == 1