Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/core/src/opendirector/agent/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
68 changes: 68 additions & 0 deletions tests/test_windows_git_control_pipe.py
Original file line number Diff line number Diff line change
@@ -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]))
Loading