Files
hermes-agent/pm/recovery.py
ethernet 284dbaf537 fix(pm): isolate bootstrap dependencies and unify YAML on ruamel
Activation reaches plugin discovery before the application dependencies
exist. Give PM its own locked Python project and runtime so it can install
or repair the application without importing that dependency tree.

Keep PM outside the application workspace. A shared uv workspace resolves
the application graph and cannot provide this isolation. Route mutations
through an isolated worker and preserve transaction callbacks, cancellation,
custom package registrations, and correlated receipts.

Use the same runtime builder for source installs and packaged payloads.
Keep offline wheelhouse support in that builder. Nix builds the independent
PM lock as a separate derivation. Refuse lazy-disabled bootstrap before
installing tools or dependencies.

Move first-party YAML readers and writers to ruamel. Keep the application
lock's transitive PyYAML requirements for third-party packages.

Verification:
- Focused canonical Python suite: 177 passed, 1 host-gated skip.
- Electron backend probes: 12 passed. Electron typecheck passed.
- Both uv locks, scoped lint, Bash syntax, and whitespace checks passed.
- Cold activation, corrupt-app repair, offline staging, and relocation ran.
- Built and exercised the Nix PM runtime and standalone YAML merge script.

Six broader caller test files retain the same 24 failing test IDs as an
archive of HEAD. The existing real-home guard blocks those tests before
they can exercise the affected paths. No full-suite pass is claimed.
Native Windows signing and full Bionic package execution remain unverified.
2026-09-11 12:23:51 -04:00

59 lines
2.4 KiB
Python

"""Restore dependency generations without importing the damaged environment."""
from __future__ import annotations
import contextlib
import subprocess
import sys
from pathlib import Path
from pm.package import InstallError
STARTUP_IMPORTS = (
("ruamel.yaml", "ruamel.yaml", "YAML"),
("python-dotenv", "dotenv", "load_dotenv"),
("click", "click", "Command"),
("certifi", "certifi", "contents"),
("rich", "rich", "print"),
("cryptography", "cryptography.hazmat.bindings._rust", "openssl"),
("PyJWT", "jwt", "encode"),
)
def validate_environment(python: Path, *, env: dict, cwd: Path) -> None:
"""Run startup import checks in the candidate, never the repairing process."""
script = (
"import importlib, importlib.metadata, pathlib, re, tomllib\n"
"project = tomllib.loads(pathlib.Path('pyproject.toml').read_text(encoding='utf-8-sig'))['project']\n"
"required = {re.split(r'[\\[<>=!~; @]', dep, 1)[0].lower().replace('_', '-')\n"
" for dep in project.get('dependencies', [])}\n"
f"checks = {STARTUP_IMPORTS!r}\n"
"for distribution, module, attribute in checks:\n"
" try:\n"
" importlib.metadata.distribution(distribution)\n"
" except importlib.metadata.PackageNotFoundError:\n"
" if distribution.lower().replace('_', '-') in required:\n"
" raise\n"
" continue\n"
" loaded = importlib.import_module(module)\n"
" getattr(loaded, attribute)\n"
" if module == 'certifi':\n"
" bundle = pathlib.Path(loaded.where())\n"
" assert bundle.is_file() and bundle.stat().st_size >= 1024, 'CA bundle is missing'\n"
)
result = subprocess.run([str(python), "-I", "-c", script], cwd=cwd, env=env,
capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60)
if result.returncode:
raise InstallError("venv", f"startup validation failed: {result.stderr.strip()[-1000:]}")
def repair_dependencies(project_root: Path) -> None:
"""Restore this installation's recorded set; never repair a foreign tree."""
from pm.client import sync_venv
from pm.paths import repo_root
if Path(project_root).resolve() != repo_root().resolve():
raise InstallError("venv", "recovery root does not match this PM installation")
with contextlib.redirect_stdout(sys.stderr):
sync_venv(repair=True)