Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(runtime): retry venv swaps denied while Windows releases handles

Harsh81fa3124b4f7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 648be42..bf48a2a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+
+- **`/seo setup` no longer fails on Windows with WinError 5 at the final venv
+ swap (#348).** Right after pip and Playwright write the new environment,
+ Windows (often Defender) can keep handles open in it for about a second, so
+ renaming it onto `.venv` failed with "Access is denied" after the whole
+ install had finished. The three directory swaps, including the rollback that
+ restores the previous `.venv`, now retry a denied rename with a short backoff
+ (up to 8 attempts, about 7 seconds in total). Diagnosis and fix shape by
+ @Krtgya.
+
## [2.5.0] - 2026-10-22
Security and accuracy fixes from a full audit, and Claude Desktop installs again.
diff --git a/scripts/runtime.py b/scripts/runtime.py
index 492de95..6414a90 100755
--- a/scripts/runtime.py
+++ b/scripts/runtime.py
@@ -290,6 +290,23 @@ class SetupLock:
self.path.unlink(missing_ok=True)
+def _replace_with_retry(source: Path, target: Path, attempts: int = 8) -> None:
+ """Swap a directory into place, tolerating Windows handle-release lag.
+
+ Right after pip or Playwright writes a venv, Windows (often Defender) can
+ hold handles in it briefly, so the rename fails with WinError 5 and then
+ succeeds about a second later.
+ """
+ for attempt in range(attempts):
+ try:
+ source.replace(target)
+ return
+ except PermissionError:
+ if attempt == attempts - 1:
+ raise
+ time.sleep(0.25 * (attempt + 1))
+
+
def command_setup(args: argparse.Namespace) -> int:
if not _python_supported():
print(_python_range_message(), file=sys.stderr)
@@ -351,9 +368,9 @@ def command_setup(args: argparse.Namespace) -> int:
)
shutil.rmtree(backup, ignore_errors=True)
if final_venv.exists():
- final_venv.replace(backup)
+ _replace_with_retry(final_venv, backup)
previous_moved = True
- staged.replace(final_venv)
+ _replace_with_retry(staged, final_venv)
swapped = True
state = dict(status["expected"])
state["browser_ready"] = browser_ready
@@ -368,7 +385,7 @@ def command_setup(args: argparse.Namespace) -> int:
if swapped:
shutil.rmtree(final_venv, ignore_errors=True)
if previous_moved and had_previous and backup.exists():
- backup.replace(final_venv)
+ _replace_with_retry(backup, final_venv)
print(f"Claude SEO setup failed: {_redact(str(exc))}", file=sys.stderr)
return 1
if browser_ready or args.skip_browser:
diff --git a/tests/test_runtime.py b/tests/test_runtime.py
index ce15d8a..2b9a268 100644
--- a/tests/test_runtime.py
+++ b/tests/test_runtime.py
@@ -201,6 +201,76 @@ def test_setup_restores_previous_environment_when_staged_swap_fails(
assert not (data / ".runtime-state.next").exists()
+def _setup_with_previous_environment(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> tuple[Path, Path]:
+ root = _fixture_root(tmp_path)
+ data = tmp_path / "data"
+ final = data / ".venv"
+ old_python = final / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
+ old_python.parent.mkdir(parents=True)
+ old_python.write_text("old", encoding="utf-8")
+ (final / "old-sentinel").write_text("keep", encoding="utf-8")
+ monkeypatch.setenv("CLAUDE_PLUGIN_DATA", str(data))
+ monkeypatch.setattr(runtime, "_root", lambda: root)
+
+ def fake_checked(argv: list[str], *, env: dict[str, str], stage: str) -> subprocess.CompletedProcess[str]:
+ if stage == "virtual environment creation":
+ staged_python = runtime._venv_python(Path(argv[-1]))
+ staged_python.parent.mkdir(parents=True)
+ staged_python.write_text("new", encoding="utf-8")
+ return subprocess.CompletedProcess(argv, 0, "", "")
+
+ monkeypatch.setattr(runtime, "_run_checked", fake_checked)
+ monkeypatch.setattr(runtime.time, "sleep", lambda _seconds: None)
+ return data, final
+
+
+def test_setup_retries_venv_swaps_denied_while_windows_releases_handles(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ data, final = _setup_with_previous_environment(tmp_path, monkeypatch)
+ original_replace = Path.replace
+ denied: set[str] = set()
+
+ def deny_first_venv_swap(self: Path, target: Path) -> Path:
+ if self.name.startswith(".venv") and self.name not in denied:
+ denied.add(self.name)
+ raise PermissionError(5, "Access is denied")
+ return original_replace(self, target)
+
+ monkeypatch.setattr(Path, "replace", deny_first_venv_swap)
+ rc = runtime.command_setup(SimpleNamespace(skip_browser=True))
+ assert rc == 0
+ assert len(denied) == 2
+ assert runtime._venv_python(final).read_text(encoding="utf-8") == "new"
+ assert not (data / ".venv.previous").exists()
+
+
+def test_setup_rollback_retries_a_denied_restore_of_the_previous_venv(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ data, final = _setup_with_previous_environment(tmp_path, monkeypatch)
+ original_replace = Path.replace
+ restore_denied = False
+
+ def fail_staged_then_deny_restore(self: Path, target: Path) -> Path:
+ nonlocal restore_denied
+ if self.name.startswith(".venv.next-"):
+ raise OSError("injected staged swap failure")
+ if self.name == ".venv.previous" and not restore_denied:
+ restore_denied = True
+ raise PermissionError(5, "Access is denied")
+ return original_replace(self, target)
+
+ monkeypatch.setattr(Path, "replace", fail_staged_then_deny_restore)
+ rc = runtime.command_setup(SimpleNamespace(skip_browser=True))
+ assert rc == 1
+ assert restore_denied
+ assert (final / "old-sentinel").read_text(encoding="utf-8") == "keep"
+ assert not (data / ".venv.previous").exists()
+
+
def test_run_propagates_child_signal(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
root = _fixture_root(tmp_path)
managed_python = tmp_path / "python"