From a35099147907d53e277e44569c797e60c3cd73a6 Mon Sep 17 00:00:00 2001 From: Weiming Liao Date: Thu, 3 Dec 2026 11:56:53 +0800 Subject: [PATCH 1/2] [test] agent: isolate bundled OpenCode workers ## summary Give ordinary and forced-timeout OpenCode workers separate temporary, XDG and pytest directories and a private home on POSIX. Refs #162. Signed-off-by: Weiming Liao --- docs/development/lessons.md | 1 + tests/test_native_runtime.py | 39 ++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/docs/development/lessons.md b/docs/development/lessons.md index 9b06bfa4f..f20d96388 100644 --- a/docs/development/lessons.md +++ b/docs/development/lessons.md @@ -145,6 +145,7 @@ When work exposes a consequential risk or a recurring problem, add a lesson or u - **Trigger:** Forwarding streamed model responses through the loopback gateway. - **Lesson:** [Issue #274](https://github.com/ThinkFlowLab/OpenDirector/issues/274) reproduced bundled OpenCode 1.19.5 / Bun 1.4.4 receiving every byte of a close-delimited response but never completing its body reader. Tools could execute before a 300-second timeout replayed the same model context. Python consumers completed the same response, so Python-only tests missed the integration failure. - **Check:** Preserve incremental forwarding with explicit HTTP framing. Terminate the body only after successful upstream exhaustion; leave transport failures and cancellation distinguishable from completion. Exercise the actual bundled consumer with a local mock model, asserting request and tool execution counts, continuation with tool results, final text and persisted state. HTTP completion is not model completion or business acceptance. +- **OpenCode worker follow-up:** A [same-runner comparison](https://github.com/FayeSpica/OpenDirector/actions/runs/47029419494) reproduced the visual watchdog after the full OpenCode prefix in three legacy runs. Three interleaved runs passed with per-worker temporary, XDG, pytest and POSIX home paths. Apply the same isolation before launching both ordinary workers and forced-timeout workers. The experiment changed these paths together, so it does not identify a single trigger. ## Native reply text can contain presentation protocols diff --git a/tests/test_native_runtime.py b/tests/test_native_runtime.py index 58e1b15d1..f6f4e486e 100644 --- a/tests/test_native_runtime.py +++ b/tests/test_native_runtime.py @@ -357,6 +357,32 @@ async def consume(): assert json.loads(saved.read_text()) == previous +def _isolated_worker_launch(tmp_path: Path, node_id: str, extra_env: dict[str, str] | None = None) -> tuple[list[str], dict[str, str]]: + env = {**os.environ, **(extra_env or {}), 'OPENDIRECTOR_REAL_CLI_WORKER': '1'} + args = [sys.executable, '-m', 'pytest', node_id, '-s', '-q'] + state = tmp_path / 'worker-state' + paths = { + 'TMPDIR': 'tmp', + 'TMP': 'tmp', + 'TEMP': 'tmp', + 'XDG_DATA_HOME': 'data', + 'XDG_STATE_HOME': 'state', + 'XDG_CACHE_HOME': 'cache', + 'XDG_CONFIG_HOME': 'config', + 'PYTEST_DEBUG_TEMPROOT': 'workers', + } + if os.name != 'nt': + paths.update(HOME='home', XDG_RUNTIME_DIR='runtime') + for variable, name in paths.items(): + path = state / name + path.mkdir(parents=True, exist_ok=True) + env[variable] = str(path) + if os.name != 'nt': + Path(env['XDG_RUNTIME_DIR']).chmod(0o700) + env['OPENDIRECTOR_WORKER_STATE_ISOLATED'] = '1' + return [*args, '--basetemp', str(state / 'pytest')], env + + @pytest.fixture def real_opencode_worker(tmp_path, monkeypatch, request): """Bound real CLI cases in isolated workers and clean up owned processes.""" @@ -365,14 +391,15 @@ def real_opencode_worker(tmp_path, monkeypatch, request): assert native.executable('default') is not None, 'Bundled OpenCode is not installed' timeout = 105 if 'windows_unicode_pipeline' in request.node.name else 60 if os.environ.get('OPENDIRECTOR_REAL_CLI_WORKER') != '1': - result = subprocess.run([sys.executable, '-m', 'pytest', request.node.nodeid, '-s', '-q'], - env={**os.environ, 'OPENDIRECTOR_REAL_CLI_WORKER': '1'}, + args, env = _isolated_worker_launch(tmp_path, request.node.nodeid) + result = subprocess.run(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", timeout=timeout + 30) assert result.returncode == 0, result.stdout return False - for name in ('XDG_DATA_HOME', 'XDG_STATE_HOME', 'XDG_CACHE_HOME'): - monkeypatch.setenv(name, str(tmp_path / name.lower())) + if os.getenv('OPENDIRECTOR_WORKER_STATE_ISOLATED') != '1': + for name in ('XDG_DATA_HOME', 'XDG_STATE_HOME', 'XDG_CACHE_HOME'): + monkeypatch.setenv(name, str(tmp_path / name.lower())) from opendirector.agent.process import _signal_tree processes = [] spawn = asyncio.create_subprocess_exec @@ -780,8 +807,8 @@ def test_real_opencode_watchdog_cleans_up_stalled_cli(tmp_path): pytest.skip('Bundled OpenCode is not installed') marker = tmp_path / 'pid' case = f'{__file__}::test_real_opencode_native_skill_and_gateway_write[False-default]' - result = subprocess.run([sys.executable, '-m', 'pytest', case, '-s', '-q'], - env={**os.environ, 'OPENDIRECTOR_REAL_CLI_WORKER': '1', 'OPENDIRECTOR_TEST_CLI_STALL': str(marker)}, + args, env = _isolated_worker_launch(tmp_path, case, {'OPENDIRECTOR_TEST_CLI_STALL': str(marker)}) + result = subprocess.run(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=25) assert result.returncode != 0 assert 'stage: fresh turn' in result.stdout From 8fc698e02c81fb134b8f1b10fae758020f319e4a Mon Sep 17 00:00:00 2001 From: Weiming Liao Date: Thu, 3 Dec 2026 12:05:35 +0800 Subject: [PATCH 2/2] [test] agent: fix OpenCode worker helper naming Signed-off-by: Weiming Liao --- tests/test_native_runtime.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_native_runtime.py b/tests/test_native_runtime.py index f6f4e486e..b23ace852 100644 --- a/tests/test_native_runtime.py +++ b/tests/test_native_runtime.py @@ -357,7 +357,9 @@ async def consume(): assert json.loads(saved.read_text()) == previous -def _isolated_worker_launch(tmp_path: Path, node_id: str, extra_env: dict[str, str] | None = None) -> tuple[list[str], dict[str, str]]: +def isolated_worker_launch( + tmp_path: Path, node_id: str, extra_env: dict[str, str] | None = None +) -> tuple[list[str], dict[str, str]]: env = {**os.environ, **(extra_env or {}), 'OPENDIRECTOR_REAL_CLI_WORKER': '1'} args = [sys.executable, '-m', 'pytest', node_id, '-s', '-q'] state = tmp_path / 'worker-state' @@ -391,7 +393,7 @@ def real_opencode_worker(tmp_path, monkeypatch, request): assert native.executable('default') is not None, 'Bundled OpenCode is not installed' timeout = 105 if 'windows_unicode_pipeline' in request.node.name else 60 if os.environ.get('OPENDIRECTOR_REAL_CLI_WORKER') != '1': - args, env = _isolated_worker_launch(tmp_path, request.node.nodeid) + args, env = isolated_worker_launch(tmp_path, request.node.nodeid) result = subprocess.run(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", timeout=timeout + 30) assert result.returncode == 0, result.stdout @@ -807,7 +809,7 @@ def test_real_opencode_watchdog_cleans_up_stalled_cli(tmp_path): pytest.skip('Bundled OpenCode is not installed') marker = tmp_path / 'pid' case = f'{__file__}::test_real_opencode_native_skill_and_gateway_write[False-default]' - args, env = _isolated_worker_launch(tmp_path, case, {'OPENDIRECTOR_TEST_CLI_STALL': str(marker)}) + args, env = isolated_worker_launch(tmp_path, case, {'OPENDIRECTOR_TEST_CLI_STALL': str(marker)}) result = subprocess.run(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=25) assert result.returncode != 0