⚡ Optimize run_python tool to use asyncio subprocess#4
Conversation
- Replaced blocking `subprocess.run` with `asyncio.create_subprocess_exec` in `src/matilda_brain/tools/builtins/code.py`. - Replaced `subprocess.run(["which", ...])` with `shutil.which` for faster python executable detection. - Refactored `src/matilda_brain/tools/builtins/config.py` to support async safe execution (`_safe_execute_async`). - Updated `tests/test_tools_builtin.py` to test `run_python` asynchronously using `pytest-asyncio`. This change allows concurrent execution of `run_python` tool calls, significantly improving performance when multiple tools are running or when the event loop is busy. Benchmark showed 2 concurrent 1-second tasks taking ~1s instead of ~2s. Co-authored-by: mudcube <101564+mudcube@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What:
Optimized the
run_pythontool insrc/matilda_brain/tools/builtins/code.pyto be non-blocking. This involved:run_pythonto be anasyncfunction.subprocess.runcall withasyncio.create_subprocess_exec.whichwithshutil.which.src/matilda_brain/tools/builtins/config.pyto expose_safe_execute_asyncwhile reusing sanitization and error handling logic.🎯 Why:
The previous implementation blocked the entire event loop while waiting for the subprocess to finish. In an async application (like an AI agent server or CLI with multiple tasks), this prevents other operations (network requests, other tool calls, etc.) from progressing during code execution.
📊 Measured Improvement:
A benchmark running two
run_pythoncalls (each sleeping for 1 second) concurrently:PR created automatically by Jules for task 11119271779874421824 started by @mudcube