Files
hermes-agent/scripts/bundles/bytecode.py
BearHuddleston ad8b2afa46 fix(bundles): audit archived pycs under the staged interpreter
verify_hpkg_archive_bytecode() read only the first 16 bytes of each
archived pyc and no caller set its expected_magic parameter, so a
header-only pyc, one stamped with another interpreter's magic, or one
whose body is not a code object passed both the pack check and
_uncovered(). Only the later staged import probe caught them, after the
loose package and its dist-info were already removed.

The audit now runs under the staged interpreter, the one zipimport loads
the archive with: each pyc must carry its magic number, the
unchecked-hash flag and a code object it can unmarshal, and a .py member
without a pyc is compiled with its grammar. Packing runs it before it
touches the loose tree, and the staged import probe keeps only the
zipimport, metadata and inspect checks, so one function defines a valid
archived pyc. _uncovered() now takes the interpreter to pass it through.

Reported by Greptile on #161819.
2026-11-30 04:52:19 -08:00

197 lines
9.3 KiB
Python

"""Bake the payload's bytecode cache at staging time.
A sealed payload ships no __pycache__: the runtime redirects bytecode
writes to a user-level cache (signature-breaking on macOS, read-only
mount on AppImage/MSIX), so every fresh install pays a cold-compile
stall on first launch. Baking turns that into a warm read:
* compileall runs with the payload's OWN staged interpreter (a pyc
minted by any other interpreter is silently ignored — the magic tag
is part of the cache filename);
* the roots are the agent tree, both site-packages and the staged
interpreter's own library: a payload Python spawned without a cache
prefix (Desktop's ticket bridge; ``-I`` drops an inherited one)
imports the stdlib too;
* ``--invalidation-mode unchecked-hash``: the pyc is trusted without
source validation and never rewritten, so staging-repack mtimes
cannot invalidate it and a stale source cannot trigger a rewrite
into the sealed tree (on macOS that rewrite breaks the app's code
signature, and Gatekeeper then holds the next update);
* the baked pycs are chmodded owner-writable (0o644) BEFORE packaging:
Squirrel.Mac's ShipIt clears the quarantine attribute off every file
in the .app and aborts the whole update with EACCES on any read-only
one (#138659), and on Windows a read-only pyc stopped the next
prepare's rmtree (#137717). A read-only bit never blocked a rewrite
anyway: CPython replaces a pyc through its writable __pycache__ dir.
What keeps default imports (all the launcher does) off these pycs is
unchecked-hash plus the asserted coverage below;
* the runtime's sys.pycache_prefix redirect is dropped when the baked
marker is present (the prefix relocates READS too — see
scripts/build/launcher_wrapper.py), so imports read the source-
adjacent baked pycs, which is Python's default multi-root lookup:
payload modules read theirs; plugin/user modules keep caching beside
their own sources under HERMES_HOME.
Coverage is the performance contract: every parseable module under
the baked roots must have a pyc. Unparseable fixtures (deliberately
invalid test data) are counted and skipped, not failed.
"""
import json
import subprocess
from pathlib import Path
MARKER = ".hermes-baked-pycache"
# Run by the staged interpreter: the magic number, the marshal format and the grammar
# an archived module must satisfy are those of the interpreter that will import it.
_HPKG_AUDIT = """\
import importlib.util, json, marshal, sys, types, zipfile
checked, missing, unparseable = 0, [], 0
with zipfile.ZipFile(sys.argv[1]) as zf:
names = set(zf.namelist())
for info in zf.infolist():
py = info.filename
if info.is_dir() or not py.endswith('.py'):
continue
if py + 'c' in names:
raw = zf.read(py + 'c')
try:
code = marshal.loads(raw[16:])
except (EOFError, ValueError, TypeError):
code = None
if (raw[:4] == importlib.util.MAGIC_NUMBER and int.from_bytes(raw[4:8], 'little') == 1
and isinstance(code, types.CodeType)):
checked += 1
continue
else:
try:
compile(zf.read(info), py, 'exec')
except SyntaxError:
unparseable += 1
continue
missing.append(py)
print(json.dumps([checked, missing, unparseable]))
"""
def verify_hpkg_archive_bytecode(archive_path: Path, python: Path) -> tuple[int, list[str], int]:
"""Judge every ``.py`` member of a ``.hpkg`` archive by its adjacent ``.pyc`` (the legacy
layout ``zipimporter`` loads directly), under *python*, the staged interpreter that will
import it: the pyc must carry that interpreter's magic number, the unchecked-hash flag and
a code object it can unmarshal.
Returns ``(checked_py_count, missing_or_invalid_member_names, unparseable_count)``.
"""
proc = subprocess.run(
[str(python), "-I", "-c", _HPKG_AUDIT, str(archive_path)],
capture_output=True, text=True, encoding="utf-8", errors="replace",
check=False, timeout=10 * 60, stdin=subprocess.DEVNULL,
)
if proc.returncode != 0:
raise RuntimeError(f"bytecode audit of {archive_path.name} failed under {python}: {proc.stderr}")
checked, missing, unparseable = json.loads(proc.stdout)
return checked, missing, unparseable
def _interpreter_stdlib(root: Path, python: Path) -> Path:
"""The staged interpreter's own library (``Lib`` beside ``python.exe``,
``lib/python3.N`` above ``bin/``), found by CPython's landmark ``os.py``."""
home = python.parent
found = [lib for lib in [home / "Lib", *sorted((home.parent / "lib").glob("python3.*"))]
if (lib / "os.py").is_file()]
if len(found) != 1:
raise ValueError(f"expected exactly one standard library beside {python}, found {len(found)}")
if not found[0].resolve().is_relative_to(root):
raise ValueError(f"the staged interpreter's standard library is outside the payload: {found[0]}")
return found[0]
def _import_roots(root: Path, python: Path) -> list[Path]:
"""The payload's import roots, in launcher order (repo, venv site, PM), then the stdlib."""
roots = [root / "hermes-agent"]
for source_root in roots + [root / "venv", root / "pm-runtime"]:
if not source_root.is_dir():
raise FileNotFoundError(f"missing import root: {source_root}")
sites = sorted((root / "venv").glob("lib/python*/site-packages")) + \
sorted((root / "venv").glob("Lib/site-packages"))
if len(sites) != 1:
raise ValueError(f"expected exactly one staged venv site-packages, found {len(sites)}")
roots.append(sites[0])
pm_sites = sorted((root / "pm-runtime").glob("lib/python*/site-packages")) + \
sorted((root / "pm-runtime").glob("Lib/site-packages"))
if len(pm_sites) != 1:
raise ValueError(f"expected exactly one PM runtime site-packages, found {len(pm_sites)}")
roots.append(pm_sites[0])
roots.append(_interpreter_stdlib(root, python))
return roots
def _uncovered(root: Path, python: Path) -> tuple[list[Path], int]:
"""Parseable .py files without a pyc (loose files and ``*.hpkg`` archives), plus unparseable skips."""
from importlib.util import cache_from_source
missing: list[Path] = []
unparseable = 0
for path in root.rglob("*.py"):
if "__pycache__" in path.parts:
continue
if Path(cache_from_source(path)).exists():
continue
try:
compile(path.read_bytes(), str(path), "exec")
except SyntaxError:
# Deliberately invalid test fixtures ship in the snapshot; they
# can never be imported, so no pyc is expected.
unparseable += 1
else:
missing.append(path)
for archive in sorted(root.glob("*.hpkg")):
if not archive.is_file():
continue
_checked, archive_missing, archive_unparseable = verify_hpkg_archive_bytecode(archive, python)
unparseable += archive_unparseable
missing.extend(Path(f"{archive}:{member}") for member in archive_missing)
return missing, unparseable
def bake_bytecode(root: Path, python: Path) -> dict:
"""Compile every payload module with the staged interpreter and seal the
caches owner-writable. ``root`` is the payload root; ``python`` MUST be the
payload's own staged interpreter binary."""
root = Path(root).resolve()
staged = Path(python).absolute()
python = staged.resolve()
if not python.is_file():
raise FileNotFoundError(f"staged interpreter is missing: {python}")
roots = _import_roots(root, staged)
total = 0
for source_root in roots:
# -f: compileall keeps a timestamp pyc whose mtime still matches (uv's, the build's
# own imports'), and those ship stale once packaging moves the sources' mtimes.
subprocess.run(
[str(python), "-I", "-m", "compileall", "-q", "-f",
"--invalidation-mode", "unchecked-hash", str(source_root)],
check=True, timeout=30 * 60, stdin=subprocess.DEVNULL)
missing, _unparseable = _uncovered(source_root, python)
if missing:
sample = ", ".join(str(m.relative_to(root)) for m in missing[:5])
raise ValueError(f"{len(missing)} payload modules have no bytecode pyc "
f"({sample}…): a fresh install would cold-compile them "
"every launch")
total += sum(1 for _ in source_root.rglob("__pycache__/*.pyc"))
# Pycs get owner write BEFORE packaging: Squirrel.Mac's ShipIt clears
# the quarantine attribute from every file in the .app and aborts the
# whole update with EACCES on any file lacking it (#138659) — a
# read-only pyc wedges every macOS update at the swap step. Default
# imports still leave these pycs alone: unchecked-hash never
# revalidates against the source and asserted coverage leaves no cache
# miss to write. `--check-hash-based-pycs always` or `-O` can still
# write here, as they could when the pycs were read-only.
for source_root in roots:
for cache in source_root.rglob("__pycache__"):
for pyc in cache.iterdir():
if pyc.is_file():
pyc.chmod(0o644)
(root / MARKER).write_text("unchecked-hash\n", encoding="utf-8")
return {"modules": total, "marker": str(root / MARKER)}