refactor(exec): centralize process-tree lifecycle (#6623)

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
Xubin Renandchengyongru authored and GitHub committed 2026-11-12 11:44:48 +08:00
1 parent c37af82e3f
commit 8607b6445c
16 files changed
+356 -534

No files matched your search

+8 -7
View File
@@ -16,6 +16,7 @@ from nanobot.agent.tools.rg import RgTool
from nanobot.agent.tools.search import FindFilesTool, GrepTool
from nanobot.agent.tools.shell import ExecTool, _PreparedCommand
from nanobot.config.schema import ToolsConfig
from nanobot.execution import processes
@pytest.mark.parametrize("installed", [True, False])
@@ -187,16 +188,16 @@ async def test_direct_process_timeout(tmp_path):
async def test_direct_process_cancellation_reaps_child(tmp_path, monkeypatch):
spawned = asyncio.Event()
processes = []
original_spawn = ExecTool._spawn
children = []
original_spawn = processes.spawn_process_tree
async def capture(*args, **kwargs):
process = await original_spawn(*args, **kwargs)
processes.append(process)
tree = await original_spawn(*args, **kwargs)
children.append(tree.process)
spawned.set()
return process
return tree
monkeypatch.setattr(ExecTool, "_spawn", staticmethod(capture))
monkeypatch.setattr(processes, "spawn_process_tree", capture)
task = asyncio.create_task(ExecTool(working_dir=str(tmp_path)).execute(
command=[sys.executable, "-c", "import time; time.sleep(20)"],
))
@@ -204,7 +205,7 @@ async def test_direct_process_cancellation_reaps_child(tmp_path, monkeypatch):
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
assert isinstance(processes[0].returncode, int)
assert isinstance(children[0].returncode, int)
@pytest.mark.skipif(shutil.which("rg") is None, reason="ripgrep not installed")