Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

fix: Windows: retry replacing a file another process holds; create the browser's last-used marker without a rename; platform-neutral tests

Pnant77f958e2a63b
diff --git a/agent_reach/runtime/browser.py b/agent_reach/runtime/browser.py
index b6b1e7f5..df0215c8 100644
--- a/agent_reach/runtime/browser.py
+++ b/agent_reach/runtime/browser.py
@@ -140,8 +140,9 @@ def touch() -> None:
"""Record an Agent Reach use of the browser (resets the idle timer)."""
make_private_dir(browser_root())
path = _last_used_path()
- if not path.exists():
- atomic_write_private_text(path, "")
+ # Create-if-missing without a rename: concurrent callers on Windows cannot replace a file another one holds.
+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | getattr(os, "O_NOFOLLOW", 0), 0o600)
+ os.close(fd)
os.utime(path)
diff --git a/agent_reach/utils/paths.py b/agent_reach/utils/paths.py
index 6e460b7f..43cc17c7 100644
--- a/agent_reach/utils/paths.py
+++ b/agent_reach/utils/paths.py
@@ -6,6 +6,7 @@ import os
import stat
import sys
import tempfile
+import time
from pathlib import Path
@@ -67,6 +68,18 @@ def make_private_dir(path: str | Path) -> Path:
return target
+def _replace(src: Path, dst: Path) -> None:
+ """os.replace; on Windows retry briefly while another process has `dst` open."""
+ for attempt in range(20):
+ try:
+ os.replace(src, dst)
+ return
+ except PermissionError:
+ if os.name != "nt" or attempt == 19:
+ raise
+ time.sleep(0.05)
+
+
def atomic_write_private_text(
path: str | Path,
text: str,
@@ -105,7 +118,7 @@ def atomic_write_private_text(
ensure_no_symlink_path(parent, "父目录")
ensure_no_symlink_path(target, "目标文件")
- os.replace(tmp_path, target)
+ _replace(tmp_path, target)
if os.name != "nt" and hasattr(os, "O_DIRECTORY"):
try:
diff --git a/tests/test_manage.py b/tests/test_manage.py
index 2f8bfc9e..1376c24e 100644
--- a/tests/test_manage.py
+++ b/tests/test_manage.py
@@ -78,6 +78,15 @@ def _fake_exe(path, version):
path.chmod(0o755)
+def _fake_version(exe):
+ """What `<exe> --version` prints for a _fake_exe; read instead of run so it works on Windows."""
+ try:
+ m = re.search(r"echo (\S+)", open(exe, encoding="utf-8").read())
+ except (OSError, TypeError):
+ return None
+ return m.group(1) if m else None
+
+
@pytest.fixture
def env(monkeypatch):
monkeypatch.setenv("PATH", os.environ.get("PATH", "")) # cli.main extends PATH; restore it
@@ -85,6 +94,7 @@ def env(monkeypatch):
monkeypatch.setattr(manage, "_channels", lambda: chans)
system = {} # command -> path the "user's PATH" has
monkeypatch.setattr(tools.shutil, "which", lambda c, *a, **k: system.get(c))
+ monkeypatch.setattr(tools, "_command_version", _fake_version)
installs, versions = [], {}
def fake_install(tool, *, global_=False):
@@ -97,7 +107,8 @@ def env(monkeypatch):
for p in paths:
_fake_exe(p, version)
if tool.kind in ("pip", "ffmpeg"):
- site = tools.tool_dir(tool.name) / "venv" / "lib" / "python3.12" / "site-packages"
+ venv = tools.tool_dir(tool.name) / "venv"
+ site = venv / "Lib" / "site-packages" if tools._WIN else venv / "lib" / "python3.12" / "site-packages"
dist = re.sub(r"[-_.]+", "_", tools._dist_name(tool))
(site / f"{dist}-{version}.dist-info").mkdir(parents=True, exist_ok=True)
if tool.kind == "npm" and not global_:
@@ -539,7 +550,7 @@ def test_skill_replaces_our_old_copy_but_not_a_foreign_one(env):
_seed_our_copy(native_old)
result = manage.place_skill()
assert ours.is_symlink()
- assert not foreign.is_symlink() and "someone-else" in (foreign / "SKILL.md").read_text()
+ assert not foreign.is_symlink() and "someone-else" in (foreign / "SKILL.md").read_text(encoding="utf-8")
assert [n["agent"] for n in result["not_linked"]] == ["cursor"]
assert not native_old.exists() and result["removed_old_copies"] == [str(native_old)]
@@ -558,7 +569,7 @@ def test_skill_falls_back_to_a_copy_without_symlink_rights(env, monkeypatch):
assert result["copies"] == [str(copy)] and result["links"] == []
(copy / "SKILL.md").write_text("---\nname: agent-reach\n---\nstale\n")
manage.place_skill()
- assert "stale" not in (copy / "SKILL.md").read_text(), "update refreshes recorded copies"
+ assert "stale" not in (copy / "SKILL.md").read_text(encoding="utf-8"), "update refreshes recorded copies"
manage.uninstall()
assert not copy.exists()
diff --git a/tests/test_unified_entry.py b/tests/test_unified_entry.py
index cba74534..540f6fc2 100644
--- a/tests/test_unified_entry.py
+++ b/tests/test_unified_entry.py
@@ -356,6 +356,7 @@ def fake_browser(monkeypatch, tmp_path):
from agent_reach.runtime import browser
monkeypatch.setenv("HOME", str(tmp_path))
+ monkeypatch.setattr(browser, "can_load_extension", lambda: True) # the macOS/Linux path; Windows has its own test
state = {"contexts": ["daily"], "running": True, "opened": [], "launches": 0, "launch_delay": 0.0,
"watched": False}