Skip to content

The agent that grows with you

fix(tests): share the account tool store only when its binaries are runnable

Teknium1eeef54ca307
diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh
index cbdb2706599..543f2b31868 100755
--- a/scripts/run_tests.sh
+++ b/scripts/run_tests.sh
@@ -77,38 +77,12 @@ _account_tool_store() {
_store_has_pins "$account/.hermes/tools" || return 1
printf '%s\n' "$account"
}
-# facts.json selects ONE entry per package, not one per version: provisioning
-# a checkout with other pins re-points the account's own runtime at them and
-# pulls the selection from under a concurrent run on the store's pins. So
-# share only when every tool `pm install` would provision here (cmd_install's
-# roots, defaults and the venv's deps) is already recorded at this checkout's
-# pins. Read-only: no lock, no install, no bytecode; any unreadable record or
-# error means "do not share".
+# Read-only check that the store already records this checkout's pins
+# (scripts/tool_store_pins.py says why and what counts).
_store_has_pins() {
local python
python="$(HERMES_RUNTIME_DIR="$1" hermes_bootstrap_python "$REPO_ROOT")" || return 1
- PYTHONHOME= "$python" -I -B - "$REPO_ROOT" "$1" >/dev/null 2>&1 <<'PY'
-import sys
-from pathlib import Path
-
-repo, store = sys.argv[1], Path(sys.argv[2])
-sys.path.insert(0, repo)
-from pm.defaults import default_packages
-from pm.lock import Facts, Lockfile
-from pm.package import StatePackage
-from pm.registry import tool_roots, walk
-from pm.store import current_target
-
-lock = Lockfile(Path(repo, "pm", "lock.json"))
-facts = Facts(store / "facts.json", strict=True)
-names, target = lock.names(), current_target()
-for package in walk(tool_roots(names) + default_packages(names) + ["venv"]):
- if isinstance(package, StatePackage):
- continue
- shas = tuple(a["sha256"] for a in lock.artifacts(package.name, target))
- if not facts.installed(package.name, lock.version(package.name), store, (target, shas) if shas else None):
- sys.exit(1)
-PY
+ PYTHONHOME= "$python" -I -B "$REPO_ROOT/scripts/tool_store_pins.py" "$REPO_ROOT" "$1" >/dev/null 2>&1
}
if _account="$(_account_tool_store)"; then
export HERMES_RUNTIME_DIR="$_account/.hermes/tools"
diff --git a/scripts/tool_store_pins.py b/scripts/tool_store_pins.py
new file mode 100644
index 00000000000..4486b3888a4
--- /dev/null
+++ b/scripts/tool_store_pins.py
@@ -0,0 +1,56 @@
+"""Exit 0 only when a tool store already holds this checkout's pins.
+
+Usage: python -I -B scripts/tool_store_pins.py REPO STORE
+
+scripts/run_tests.sh asks this before sharing the account's tool store with a
+faked HOME. facts.json selects ONE entry per package, not one per version:
+provisioning a checkout with other pins re-points the account's own runtime at
+them and pulls the selection from under a concurrent run on the store's pins.
+So share only when every tool `pm install` would provision here (cmd_install's
+roots, defaults and the venv's deps) is already recorded at this checkout's
+pins AND runnable as is: a recorded entry whose binary is missing or not
+executable sends setup-hermes.sh (uv) or `pm install` back into a re-stage
+whose unlocked remove + publish races other runs on the shared store. Unlike
+PM's own lookup this never heals an exec bit. Read-only: no lock, no install,
+no bytecode; any unreadable record or error means "do not share".
+"""
+import os
+import sys
+from pathlib import Path
+
+
+def _runnable(binary: Path) -> bool:
+ return binary.is_file() and (os.name == "nt" or os.access(binary, os.X_OK))
+
+
+def _setup_uv(store: Path, target: str, version: str) -> Path:
+ """setup-hermes.sh's exact uv path: $store/uv-$uv_version-$os-$arch/uv[.exe]."""
+ os_arch = "-".join(target.split("-")[:2])
+ return store / f"uv-{version}-{os_arch}" / ("uv.exe" if os_arch.startswith("win32") else "uv")
+
+
+def main(repo: str, store: Path) -> int:
+ sys.path.insert(0, repo)
+ from pm.defaults import default_packages
+ from pm.lock import Facts, Lockfile
+ from pm.package import StatePackage
+ from pm.registry import tool_roots, walk
+ from pm.store import current_target
+
+ lock = Lockfile(Path(repo, "pm", "lock.json"))
+ facts = Facts(store / "facts.json", strict=True)
+ names, target = lock.names(), current_target()
+ for package in walk(tool_roots(names) + default_packages(names) + ["venv"]):
+ if isinstance(package, StatePackage):
+ continue
+ shas = tuple(a["sha256"] for a in lock.artifacts(package.name, target))
+ if not facts.installed(package.name, lock.version(package.name), store, (target, shas) if shas else None):
+ return 1
+ binary = package.binary(store / facts.get(package.name)["entry"], target)
+ if binary is not None and not _runnable(binary):
+ return 1
+ return 0 if _runnable(_setup_uv(store, target, lock.version("uv"))) else 1
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1], Path(sys.argv[2])))
diff --git a/tests/scripts/test_tool_store_pins.py b/tests/scripts/test_tool_store_pins.py
new file mode 100644
index 00000000000..d5c353b1e08
--- /dev/null
+++ b/tests/scripts/test_tool_store_pins.py
@@ -0,0 +1,72 @@
+"""run_tests.sh shares the account's tool store only when it can run from it as is.
+
+A store whose facts match the checkout's pins but whose uv binary is missing or
+not executable must not be shared: setup-hermes.sh would then re-stage uv into
+the shared store with an unlocked ``rm -rf`` + publish that races other runs.
+"""
+import json
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+from pm.defaults import default_packages
+from pm.lock import Lockfile
+from pm.package import StatePackage
+from pm.registry import tool_roots, walk
+from pm.store import current_target
+
+ROOT = Path(__file__).resolve().parents[2]
+
+
+def _matching_store(store: Path) -> Path:
+ """A store whose facts record every walked tool at the lock's pins, each
+ entry holding an executable binary. Returns setup-hermes.sh's uv path."""
+ lock = Lockfile(ROOT / "pm" / "lock.json")
+ names, target = lock.names(), current_target()
+ setup_target = "-".join(target.split("-")[:2]) # setup-hermes.sh: "$os-$arch"
+ packages = {}
+ for package in walk(tool_roots(names) + default_packages(names) + ["venv"]):
+ if isinstance(package, StatePackage):
+ continue
+ version = lock.version(package.name)
+ entry_name = f"{package.name}-{version}-{setup_target if package.name == 'uv' else target}"
+ entry = store / entry_name
+ entry.mkdir(parents=True)
+ binary = package.binary(entry, target)
+ if binary is not None:
+ binary.parent.mkdir(parents=True, exist_ok=True)
+ binary.write_text("#!/bin/sh\n")
+ binary.chmod(0o755)
+ packages[package.name] = {
+ "entry": entry_name, "version": version, "target": target, "env": {},
+ "artifacts": [a["sha256"] for a in lock.artifacts(package.name, target)],
+ }
+ (store / "facts.json").write_text(json.dumps({"schema": 1, "packages": packages}))
+ return store / packages["uv"]["entry"] / "uv"
+
+
+def _shares(store: Path) -> bool:
+ result = subprocess.run(
+ [sys.executable, "-I", "-B", str(ROOT / "scripts/tool_store_pins.py"), str(ROOT), str(store)],
+ capture_output=True, text=True, timeout=60, check=False,
+ )
+ return result.returncode == 0
+
+
[email protected]("breakage", ["missing", "not_executable"])
+def test_store_without_a_runnable_uv_is_not_shared(tmp_path, breakage):
+ store = tmp_path / "tools"
+ uv = _matching_store(store)
+ assert _shares(store), "control: a complete store at this checkout's pins must be shared"
+ if breakage == "missing":
+ uv.unlink()
+ else:
+ uv.chmod(0o644)
+ facts_before = (store / "facts.json").read_bytes()
+ assert not _shares(store)
+ assert uv.exists() == (breakage == "not_executable")
+ assert breakage == "missing" or not uv.stat().st_mode & 0o111, "the check must stay read-only"
+ assert (store / "facts.json").read_bytes() == facts_before