The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): clear the write bit before unlinking read-only pack temps on Windows
liuhao10249bd542c4a0ee
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.pyindex 7fba7b13eaf..1f3a7f39faf 100644--- a/hermes_cli/gitlock.py+++ b/hermes_cli/gitlock.py@@ -14,6 +14,9 @@ from typing import Callable, Iterable, List, Optionallogger = logging.getLogger(__name__)+# Module-level (not inline) so tests can flip it without breaking pathlib's os.name check.+_IS_WINDOWS = os.name == "nt"+# Files younger than this are presumed live (a fetch may be in flight) and are never removed. Lock# files live for seconds and a healthy fetch completes in minutes; 10 minutes is abandoned.STALE_LOCK_MIN_AGE_SECONDS = 10 * 60@@ -58,11 +61,21 @@ def _sweep_stale(directory: Path, candidates: Callable[[], Iterable[Path]], *, mfor entry in candidates():try:if entry.is_file() and (st := entry.stat()).st_mtime < cutoff:+ if _IS_WINDOWS:+ # git renames its transfer temps into place read-only; Windows refuses to+ # unlink a read-only file (EACCES/13), so without clearing the write bit+ # this sweep silently removes nothing on real debris (#116384).+ try:+ os.chmod(entry, 0o666)+ except OSError:+ passentry.unlink()removed.append(str(entry))log_removed(entry, st.st_size)- except OSError:- logger.debug("Could not clear %s (skipping)", entry, exc_info=True)+ except OSError as exc:+ # A cleaner that fails silently is worse than none: debug-level skips hid the+ # Windows read-only unlink failure for months while debris grew to gigabytes.+ logger.warning("Could not clear %s (skipping): %s", entry, exc)return removeddiff --git a/tests/hermes_cli/test_gitlock_tmp_packs.py b/tests/hermes_cli/test_gitlock_tmp_packs.pyindex 4f8b8194de5..da9cfd4665e 100644--- a/tests/hermes_cli/test_gitlock_tmp_packs.py+++ b/tests/hermes_cli/test_gitlock_tmp_packs.py@@ -8,6 +8,7 @@ uses.from __future__ import annotations+import loggingimport osimport timefrom pathlib import Path@@ -99,3 +100,59 @@ def test_never_raises_on_unlink_failure(tmp_path, monkeypatch):monkeypatch.setattr(Path, "unlink", failing_unlink)assert clear_stale_tmp_packs(repo) == [] # skipped, not raised+++def test_windows_readonly_debris_is_cleared(tmp_path, monkeypatch):+ """git renames its transfer temps into place read-only, and Windows refuses to unlink a+ read-only file with EACCES — the exact rule that let aborted-fetch debris survive this+ sweep for months (#116384). Path.unlink is faked to that Windows rule so the regression+ is exercised on POSIX CI too."""+ repo = _mkrepo(tmp_path)+ monkeypatch.setattr("hermes_cli.gitlock._git_proc_running", lambda: False)+ monkeypatch.setattr("hermes_cli.gitlock._IS_WINDOWS", True)+ pack = repo / ".git" / "objects" / "pack"++ debris = pack / "tmp_pack_ReadOnly"+ debris.write_bytes(b"x" * 128)+ _age(debris, STALE_TMP_PACK_MIN_AGE_SECONDS + 60)+ os.chmod(debris, 0o444) # read-only, as git writes its pack temps++ real_unlink = Path.unlink++ def windows_unlink(self, *a, **k):+ if not self.stat().st_mode & 0o222: # Windows: no write bit -> EACCES+ raise PermissionError(13, "Permission denied")+ return real_unlink(self, *a, **k)++ monkeypatch.setattr(Path, "unlink", windows_unlink)++ removed = clear_stale_tmp_packs(repo)+ assert removed == [str(debris)] # write bit cleared first, then unlinked+ assert not debris.exists()+++def test_unlink_failure_is_surfaced_at_warning(tmp_path, monkeypatch, caplog):+ """The skip used to be logged at debug, which hid the months-long Windows no-op sweep;+ a cleaner that fails silently is worse than none, so every skip must be visible (#116384)."""+ repo = _mkrepo(tmp_path)+ monkeypatch.setattr("hermes_cli.gitlock._git_proc_running", lambda: False)+ pack = repo / ".git" / "objects" / "pack"+ stuck = pack / "tmp_pack_stuck"+ stuck.write_bytes(b"x")+ _age(stuck, STALE_TMP_PACK_MIN_AGE_SECONDS + 60)++ real_unlink = Path.unlink++ def failing_unlink(self, *a, **k):+ if self.name == "tmp_pack_stuck":+ raise OSError(13, "Permission denied")+ return real_unlink(self, *a, **k)++ monkeypatch.setattr(Path, "unlink", failing_unlink)++ with caplog.at_level(logging.WARNING, logger="hermes_cli.gitlock"):+ assert clear_stale_tmp_packs(repo) == []+ assert any(+ r.levelname == "WARNING" and "tmp_pack_stuck" in r.getMessage()+ for r in caplog.records+ )