From 3c861ed800654acdfdc01a4e6925f7b271cd8df1 Mon Sep 17 00:00:00 2001 From: Ravomi Date: Thu, 19 Nov 2026 03:11:39 +0800 Subject: [PATCH 1/2] Remove partial OpenCode install when symlink creation fails --- adapters/opencode/install.py | 26 ++++++++++++++++++++- tests/unit/test_opencode_adapter.py | 36 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/adapters/opencode/install.py b/adapters/opencode/install.py index 906ddbff..9881bc3d 100644 --- a/adapters/opencode/install.py +++ b/adapters/opencode/install.py @@ -6,9 +6,11 @@ import argparse import hashlib import json +import os from pathlib import Path import re import shutil +import subprocess ROOT = Path(__file__).resolve().parents[2] HOST_VERSION = "1.18.29" @@ -27,6 +29,20 @@ def name(path: Path) -> str: return match[1] +def _link_skill_dir(link: Path, target: Path) -> None: + """Symlink a skill directory, falling back to a Windows junction if unprivileged.""" + try: + link.symlink_to(target, target_is_directory=True) + except OSError: + if os.name != "nt": + raise + subprocess.run( + ["cmd", "/c", "mklink", "/J", str(link), str(target)], + check=True, + capture_output=True, + ) + + def install(destination: Path) -> Path: """Create a new installation; refuse to overwrite any existing path.""" destination = destination.expanduser().absolute() @@ -45,6 +61,14 @@ def install(destination: Path) -> Path: if destination.is_relative_to(ROOT): raise ValueError("Install outside the source tree") destination.mkdir(parents=True) + try: + return _install_into(destination) + except BaseException: + shutil.rmtree(destination, ignore_errors=True) + raise + + +def _install_into(destination: Path) -> Path: bundle = destination / "bundle" bundle.mkdir() for directory in ("skills", "agents", "resources", "docs"): @@ -113,7 +137,7 @@ def install(destination: Path) -> Path: commands = {} for skill in sorted((bundle / "skills").glob("*/SKILL.md")): skill_name = name(skill) - (skills / skill_name).symlink_to(skill.parent, target_is_directory=True) + _link_skill_dir(skills / skill_name, skill.parent) commands[skill_name] = { "description": f"Run {skill_name} (experimental)", "agent": "egm", diff --git a/tests/unit/test_opencode_adapter.py b/tests/unit/test_opencode_adapter.py index 5a1fcc3c..fd075d23 100644 --- a/tests/unit/test_opencode_adapter.py +++ b/tests/unit/test_opencode_adapter.py @@ -187,3 +187,39 @@ def test_env_matching_destination_is_rejected(tmp_path: Path, suffix: str) -> No with pytest.raises(ValueError, match="read rules"): ADAPTER["install"](tmp_path / suffix) assert not list(tmp_path.iterdir()) + + +def test_install_cleans_up_partial_destination_on_symlink_failure( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + destination = tmp_path / "egm-oc" + + def fail_symlink(self: Path, target: Path, target_is_directory: bool = False) -> None: + raise OSError("symlinks disabled") + + monkeypatch.setattr(Path, "symlink_to", fail_symlink) + monkeypatch.setattr(os, "name", "posix") + with pytest.raises(OSError, match="symlinks disabled"): + ADAPTER["install"](destination) + assert not destination.exists() + + +def test_install_falls_back_to_windows_junction_on_symlink_failure( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + destination = tmp_path / "egm-oc" + calls: list[list[str]] = [] + + def fail_symlink(self: Path, target: Path, target_is_directory: bool = False) -> None: + raise OSError("WinError 1314") + + def fake_run(cmd: list[str], **kwargs: object) -> None: + calls.append(cmd) + + monkeypatch.setattr(Path, "symlink_to", fail_symlink) + monkeypatch.setattr(os, "name", "nt") + monkeypatch.setattr("subprocess.run", fake_run) + config = ADAPTER["install"](destination) + assert config.exists() + assert calls + assert calls[0][:4] == ["cmd", "/c", "mklink", "/J"] From 3986405ab54cf9122e7f2c9ce06630322bae113c Mon Sep 17 00:00:00 2001 From: Ravomi Date: Sun, 22 Nov 2026 01:48:57 +0800 Subject: [PATCH 2/2] Limit OpenCode installer fix to partial cleanup --- adapters/opencode/install.py | 18 +---------------- tests/unit/test_opencode_adapter.py | 31 ++++++----------------------- 2 files changed, 7 insertions(+), 42 deletions(-) diff --git a/adapters/opencode/install.py b/adapters/opencode/install.py index 9881bc3d..e60d0b6c 100644 --- a/adapters/opencode/install.py +++ b/adapters/opencode/install.py @@ -6,11 +6,9 @@ import argparse import hashlib import json -import os from pathlib import Path import re import shutil -import subprocess ROOT = Path(__file__).resolve().parents[2] HOST_VERSION = "1.18.29" @@ -29,20 +27,6 @@ def name(path: Path) -> str: return match[1] -def _link_skill_dir(link: Path, target: Path) -> None: - """Symlink a skill directory, falling back to a Windows junction if unprivileged.""" - try: - link.symlink_to(target, target_is_directory=True) - except OSError: - if os.name != "nt": - raise - subprocess.run( - ["cmd", "/c", "mklink", "/J", str(link), str(target)], - check=True, - capture_output=True, - ) - - def install(destination: Path) -> Path: """Create a new installation; refuse to overwrite any existing path.""" destination = destination.expanduser().absolute() @@ -137,7 +121,7 @@ def _install_into(destination: Path) -> Path: commands = {} for skill in sorted((bundle / "skills").glob("*/SKILL.md")): skill_name = name(skill) - _link_skill_dir(skills / skill_name, skill.parent) + (skills / skill_name).symlink_to(skill.parent, target_is_directory=True) commands[skill_name] = { "description": f"Run {skill_name} (experimental)", "agent": "egm", diff --git a/tests/unit/test_opencode_adapter.py b/tests/unit/test_opencode_adapter.py index fd075d23..db2ab60a 100644 --- a/tests/unit/test_opencode_adapter.py +++ b/tests/unit/test_opencode_adapter.py @@ -195,31 +195,12 @@ def test_install_cleans_up_partial_destination_on_symlink_failure( destination = tmp_path / "egm-oc" def fail_symlink(self: Path, target: Path, target_is_directory: bool = False) -> None: + assert (destination / "bundle").is_dir() raise OSError("symlinks disabled") - monkeypatch.setattr(Path, "symlink_to", fail_symlink) - monkeypatch.setattr(os, "name", "posix") - with pytest.raises(OSError, match="symlinks disabled"): - ADAPTER["install"](destination) + with monkeypatch.context() as patch: + patch.setattr(Path, "symlink_to", fail_symlink) + with pytest.raises(OSError, match="symlinks disabled"): + ADAPTER["install"](destination) assert not destination.exists() - - -def test_install_falls_back_to_windows_junction_on_symlink_failure( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch -) -> None: - destination = tmp_path / "egm-oc" - calls: list[list[str]] = [] - - def fail_symlink(self: Path, target: Path, target_is_directory: bool = False) -> None: - raise OSError("WinError 1314") - - def fake_run(cmd: list[str], **kwargs: object) -> None: - calls.append(cmd) - - monkeypatch.setattr(Path, "symlink_to", fail_symlink) - monkeypatch.setattr(os, "name", "nt") - monkeypatch.setattr("subprocess.run", fake_run) - config = ADAPTER["install"](destination) - assert config.exists() - assert calls - assert calls[0][:4] == ["cmd", "/c", "mklink", "/J"] + assert ADAPTER["install"](destination).is_file()