From 212e85277a073924052e4323b19ab89ae9015f69 Mon Sep 17 00:00:00 2001 From: Hniii98 <752608928@qq.com> Date: Mon, 30 Nov 2026 18:01:59 +0800 Subject: [PATCH] [bugfix] agent: detach stdin for OpenCode git init on Windows ## summary - Detach stdin (`stdin=asyncio.subprocess.DEVNULL`) when running `git init --quiet --template=` for bundled OpenCode worktrees. - Add a Windows desktop control-pipe regression test verifying that `git init` completes while the host control reader blocks on stdin. Signed-off-by: Hniii98 <752608928@qq.com> --- .../core/src/opendirector/agent/native.py | 3 + tests/test_windows_git_control_pipe.py | 68 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tests/test_windows_git_control_pipe.py diff --git a/packages/core/src/opendirector/agent/native.py b/packages/core/src/opendirector/agent/native.py index b3e98f3c7..573e020a2 100644 --- a/packages/core/src/opendirector/agent/native.py +++ b/packages/core/src/opendirector/agent/native.py @@ -214,6 +214,9 @@ async def stream(self, run: RunContext, gateway: ToolGateway): if runtime == 'opencode': git = await asyncio.create_subprocess_exec( 'git', 'init', '--quiet', '--template=', cwd=work, + # The desktop host is already reading its control pipe. + # Git for Windows can block inspecting that inherited handle. + stdin=asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.PIPE, ) _, error = await git.communicate() diff --git a/tests/test_windows_git_control_pipe.py b/tests/test_windows_git_control_pipe.py new file mode 100644 index 000000000..0d35464ff --- /dev/null +++ b/tests/test_windows_git_control_pipe.py @@ -0,0 +1,68 @@ +"""Windows desktop control-pipe regression for the bundled OpenCode worktree.""" +import asyncio +import os +from pathlib import Path +import subprocess +import sys +import threading +from types import SimpleNamespace +from unittest.mock import AsyncMock, patch +from uuid import uuid4 + +import pytest + + +def run_host(directory): + from opendirector.agent import native + from opendirector.agent.context import RunContext + + # The desktop keeps its control reader blocked while the Agent starts. + reading = threading.Event() + + def read_control(): + reading.set() + os.read(0, 1) + + threading.Thread(target=read_control, daemon=True).start() + assert reading.wait(5) + run = RunContext(runtime='default', project_id=uuid4(), turn_id=uuid4(), + session='test', directory=directory, config=None, messages=[], selected_skills=[]) + work = directory / 'agent-runs' / str(run.turn_id) / 'work' + + class Initialized(Exception): + pass + + async def start(): + # Stop at the next startup step, before any model or CLI is needed. + with patch.object(native, 'prepare_skills', side_effect=Initialized): + with pytest.raises(Initialized): + await anext(native.NativeRuntime().stream(run, SimpleNamespace(close=AsyncMock()))) + assert (work / '.git' / 'HEAD').is_file() + + asyncio.run(start()) + + +@pytest.mark.cpu +@pytest.mark.merge +@pytest.mark.skipif(sys.platform != 'win32', reason='Windows desktop control pipe') +def test_git_init_finishes_while_desktop_reads_control_pipe(tmp_path): + with subprocess.Popen([sys.executable, __file__, str(tmp_path)], stdin=subprocess.PIPE) as host: + try: + # Keep the writer open and idle, as Electron does between commands. + assert host.wait(timeout=15) == 0 + except subprocess.TimeoutExpired: + work, = (tmp_path / 'agent-runs').glob('*/work') + assert not (work / '.git' / 'HEAD').exists(), 'host stalled after git init' + pytest.fail('git init stalled while the desktop control pipe was open') + finally: + host.stdin.close() + if host.poll() is None: + # Closing the writer releases the reader and lets Git exit. + try: + host.wait(timeout=5) + except subprocess.TimeoutExpired: + subprocess.run(['taskkill', '/F', '/T', '/PID', str(host.pid)], check=True) + + +if __name__ == '__main__': + run_host(Path(sys.argv[1]))