The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(local-runtime): reject incomplete downloads and repair corrupt cache
Ryan Hill745e3264ff8e
diff --git a/hermes_cli/local_runtime/binaries.py b/hermes_cli/local_runtime/binaries.pyindex d18985646c2..9b62264643b 100644--- a/hermes_cli/local_runtime/binaries.py+++ b/hermes_cli/local_runtime/binaries.py@@ -2,6 +2,7 @@from __future__ import annotations+import gzipimport hashlibimport jsonimport logging@@ -9,8 +10,10 @@ import osimport platformimport shutilimport subprocess+import tarfileimport urllib.requestimport zipfile+import zlibfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Callable@@ -186,18 +189,26 @@ def _download(url: str, dest: Path,the server sends no Content-Length) — a several-hundred-MB archive must never look hung."""logger.info("downloading %s", url)tmp = dest.with_suffix(dest.suffix + ".part")- with urllib.request.urlopen(url, timeout=120) as r, open(tmp, "wb") as f:- total = int(r.headers.get("Content-Length") or 0)- done = 0- while True:- chunk = r.read(1 << 20)- if not chunk:- break- f.write(chunk)- done += len(chunk)- if progress is not None:- progress(done, total)- tmp.replace(dest)+ try:+ with urllib.request.urlopen(url, timeout=120) as r, open(tmp, "wb") as f:+ length = r.headers.get("Content-Length")+ total = int(length) if length is not None else 0+ done = 0+ while True:+ chunk = r.read(1 << 20)+ if not chunk:+ break+ f.write(chunk)+ done += len(chunk)+ if progress is not None:+ progress(done, total)+ # Chunked reads can return EOF without raising IncompleteRead.+ if length is not None and done != total:+ raise BinaryResolutionError(+ f"incomplete download for {dest.name}: expected {total} bytes, got {done}")+ tmp.replace(dest)+ finally:+ tmp.unlink(missing_ok=True)def _extract(archive: Path, dest: Path,@@ -207,7 +218,6 @@ def _extract(archive: Path, dest: Path,opener, list_members, size = zipfile.ZipFile, "infolist", "file_size"kwargs = {}else:- import tarfileopener, list_members, size = tarfile.open, "getmembers", "size"kwargs = {"filter": "data"}with opener(archive) as ar:@@ -289,19 +299,31 @@ def ensure_runtime_installed(tag: str, backend: str,for i, asset in enumerate(plan.assets, 1):label = f"{i}/{n_assets}" if n_assets > 1 else ""archive = downloads / asset- if not archive.exists():- _download(RELEASE_URL.format(tag=tag, asset=asset), archive,- progress=stage_progress("download", label))- if progress is not None:- progress("verify", 0, 0, label)- digest = _sha256(archive)- expected = (expected_sha256 or {}).get(asset)- if expected and digest != expected:- archive.unlink(missing_ok=True)- raise BinaryResolutionError(- f"sha256 mismatch for {asset}: expected {expected}, got {digest}")+ # Older versions could cache truncated responses. Replace a corrupt+ # cache entry once; a corrupt fresh download must fail and be evicted.+ for attempt in range(2):+ cached = archive.exists()+ if not cached:+ _download(RELEASE_URL.format(tag=tag, asset=asset), archive,+ progress=stage_progress("download", label))+ if progress is not None:+ progress("verify", 0, 0, label)+ digest = _sha256(archive)+ expected = (expected_sha256 or {}).get(asset)+ if expected and digest != expected:+ archive.unlink(missing_ok=True)+ raise BinaryResolutionError(+ f"sha256 mismatch for {asset}: expected {expected}, got {digest}")+ try:+ _extract(archive, install_dir, progress=stage_progress("extract", label))+ except (zipfile.BadZipFile, tarfile.ReadError, gzip.BadGzipFile, EOFError, zlib.error) as exc:+ archive.unlink(missing_ok=True)+ if not cached or attempt == 1:+ raise BinaryResolutionError(f"invalid runtime archive {asset}: {exc}") from exc+ logger.warning("discarding corrupt cached runtime archive %s: %s", asset, exc)+ else:+ breakrecorded[asset] = digest- _extract(archive, install_dir, progress=stage_progress("extract", label))if progress is not None:progress("verify", 0, 0, "")diff --git a/tests/hermes_cli/test_local_runtime_downloads.py b/tests/hermes_cli/test_local_runtime_downloads.pynew file mode 100644index 00000000000..55233083a55--- /dev/null+++ b/tests/hermes_cli/test_local_runtime_downloads.py@@ -0,0 +1,135 @@+"""Runtime downloads must not turn a transient transfer failure into a poisoned cache."""++import hashlib+import io+import json+import sys+import tarfile+import threading+import zipfile+from http.server import BaseHTTPRequestHandler, HTTPServer+from pathlib import Path++import pytest++from hermes_cli.local_runtime import binaries+++def asset_server():+ responses = []+ requests = []++ class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ requests.append(self.path)+ body, length = responses.pop(0)+ self.send_response(200)+ if length is not None:+ self.send_header("Content-Length", str(length))+ self.end_headers()+ self.wfile.write(body)+ self.close_connection = True++ def log_message(self, *args):+ pass++ with HTTPServer(("127.0.0.1", 0), Handler) as server:+ thread = threading.Thread(target=server.serve_forever, daemon=True)+ thread.start()+ try:+ yield f"http://127.0.0.1:{server.server_port}/{{asset}}", responses, requests+ finally:+ server.shutdown()+ thread.join(timeout=5)++[email protected]("failure", ["short", "callback"])+def test_download_only_publishes_complete_transfers(tmp_path, asset_server, failure):+ url, responses, requests = asset_server+ dest = tmp_path / "runtime.zip"+ payload = b"runtime archive bytes"+ responses.append((payload[:-1], len(payload)))++ def progress(done, total):+ if failure == "callback":+ raise RuntimeError("cancelled")++ error = RuntimeError if failure == "callback" else binaries.BinaryResolutionError+ with pytest.raises(error):+ binaries._download(url.format(asset=dest.name), dest, progress=progress)+ assert not dest.exists()+ assert not dest.with_suffix(".zip.part").exists()++ # A retry works, including servers that omit Content-Length.+ responses.append((payload, None))+ ticks = []+ binaries._download(url.format(asset=dest.name), dest,+ progress=lambda done, total: ticks.append((done, total)))+ assert dest.read_bytes() == payload+ assert ticks[-1] == (len(payload), 0)+ assert len(requests) == 2++[email protected]("archive_format,corruption", [+ ("zip", "truncated"), ("zip", "crc"), ("tar.gz", "truncated"),+])[email protected]("replacement_kind", ["valid", "invalid", "permission"])+def test_install_recovers_corrupt_cache_once(tmp_path, monkeypatch, asset_server,+ archive_format, corruption, replacement_kind):+ url, responses, requests = asset_server+ # Use the real cache/root resolution, HTTP transfer, digest and extraction.+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))+ tag = f"b{sys.version_info.major}"+ asset = f"runtime.{archive_format}"+ plan = binaries.AssetPlan(tag, "cpu", [asset])+ monkeypatch.setattr(binaries, "resolve_assets", lambda *args: plan)+ monkeypatch.setattr(binaries, "RELEASE_URL", url)+ # Exercise the real --version subprocess without downloading a native runtime.+ monkeypatch.setattr(binaries, "server_binary", lambda _: Path(sys.executable))+ payload = b"complete runtime payload"+ stream = io.BytesIO()+ if archive_format == "zip":+ with zipfile.ZipFile(stream, "w") as archive:+ archive.writestr("bin/runtime.dat", payload)+ else:+ with tarfile.open(fileobj=stream, mode="w:gz") as archive:+ member = tarfile.TarInfo("bin/runtime.dat")+ member.size = len(payload)+ archive.addfile(member, io.BytesIO(payload))+ valid = stream.getvalue()+ corrupt = (valid.replace(payload, b"!" * len(payload))+ if corruption == "crc" else valid[:10])+ cached = binaries.runtimes_root() / "downloads" / asset+ cached.parent.mkdir(parents=True)+ cached.write_bytes(corrupt)+ replacement = valid if replacement_kind == "valid" else corrupt+ responses.append((replacement, len(replacement)))+ ticks = []++ if replacement_kind == "permission":+ def denied(*args, **kwargs):+ raise PermissionError("destination is not writable")++ monkeypatch.setattr(binaries, "_extract", denied)+ with pytest.raises(PermissionError):+ binaries.ensure_runtime_installed(tag, "cpu")+ assert cached.read_bytes() == corrupt+ assert not requests+ return++ if replacement_kind == "valid":+ installed = binaries.ensure_runtime_installed(tag, "cpu", progress=lambda *p: ticks.append(p))+ assert (installed / "bin/runtime.dat").read_bytes() == payload+ manifest = json.loads((installed / "manifest.json").read_text(encoding="utf-8"))+ assert manifest["assets"][asset] == hashlib.sha256(valid).hexdigest()+ assert manifest["verified_version"]+ assert cached.read_bytes() == valid+ assert {p[0] for p in ticks} == {"download", "verify", "extract"}+ assert binaries.ensure_runtime_installed(tag, "cpu") == installed+ else:+ with pytest.raises(binaries.BinaryResolutionError, match="archive"):+ binaries.ensure_runtime_installed(tag, "cpu")+ assert not cached.exists()+ assert not (plan.install_dir / "manifest.json").exists()+ assert len(requests) == 1