Skip to content

The agent that grows with you

fix(update): carry lazily installed extras into the first PM generation

ethernetb9a5e2d022ba
diff --git a/hermes_cli/_update_takeover.py b/hermes_cli/_update_takeover.py
index 491bb2c278b..61d70283b08 100644
--- a/hermes_cli/_update_takeover.py
+++ b/hermes_cli/_update_takeover.py
@@ -37,7 +37,9 @@ def prepare(request: dict) -> tuple[Path, dict[str, str]]:
problems = [problem for problem in activate(allow_incomplete=True) if not problem.startswith("venv:")]
if problems:
raise RuntimeError(f"tools not on PATH before venv sync: {'; '.join(problems)}")
- extras = ["all"] if not runtime_facts_path(root).is_file() else None
+ from pm.extras import legacy_selection
+
+ extras = legacy_selection(root) if not runtime_facts_path(root).is_file() else None
repair_marker = install_state_dir(root) / ".repair-incomplete"
# Repair preserves the old stamp. Changed source inputs instead need
# an ordinary sync, which builds and validates a fresh generation too.
diff --git a/hermes_cli/venv_sync.py b/hermes_cli/venv_sync.py
index 21f41dbb502..9b9703c7e75 100644
--- a/hermes_cli/venv_sync.py
+++ b/hermes_cli/venv_sync.py
@@ -288,9 +288,10 @@ def _finish_source_update(root: Path, *, current: bool, pending: Path) -> None:
# tail must leave the tail, not a "current" install with nothing built.
refuse_foreign_owned_venv(root)
arm_completion(root)
- # Main-era installers selected [all] but had no PM ledger. Established
- # PM installs retain their recorded extras and plugin union instead.
- extras = ["all"] if not runtime_facts_path(root).is_file() else None
+ # Main-era installs have no PM ledger; carry what their venv held.
+ # Established PM installs retain their recorded extras and plugin union instead.
+ from pm.extras import legacy_selection
+ extras = legacy_selection(root) if not runtime_facts_path(root).is_file() else None
pm.sync_venv(extras, explicit=True, project_root=root)
collect_superseded_generations(root)
# These can predate the swap. Once PM commits the replacement they
diff --git a/pm/extras.py b/pm/extras.py
index 6e2bcbbe77f..f123831e390 100644
--- a/pm/extras.py
+++ b/pm/extras.py
@@ -9,6 +9,7 @@ true; pm owns HOW (uv sync inside the venv package).
from __future__ import annotations
import importlib.util
+from pathlib import Path
from typing import Callable
@@ -189,7 +190,8 @@ def ensure_import(extra: str) -> None:
app_running = bool(getattr(get_app_or_none(), "is_running", False))
if not app_running and sys.stdin.isatty() and sys.stdout.isatty():
try:
- answer = input(f"\nExtra {extra!r} requires: {', '.join(missing(extra))}\nInstall now? [Y/n] ").strip().lower()
+ answer = input(f"\nThis needs Hermes' optional {extra!r} feature, which isn't installed yet.\n"
+ "Install it now? [Y/n] ").strip().lower()
except (EOFError, KeyboardInterrupt):
answer = "n"
if answer and answer not in {"y", "yes"}:
@@ -235,3 +237,39 @@ def ensure_and_bind(extra, importer, target_globals) -> bool:
def missing(extra: str) -> tuple[str, ...]:
return tuple(a for a in _anchors(extra) if not _importable(a))
+
+
+def _installed_in(site_packages: Path, anchor: str) -> bool:
+ """Is ``anchor`` installed under a site-packages we must not import from?"""
+ *parents, leaf = anchor.split(".")
+ parent = site_packages.joinpath(*parents)
+ if (parent / leaf).is_dir():
+ return True
+ # A top-level module can be a plain file or a compiled extension.
+ return any(entry.name.rsplit(".", 1)[-1] in {"py", "so", "pyd"}
+ for entry in parent.glob(f"{leaf}.*"))
+
+
+def legacy_selection(project_root: Path) -> list[str]:
+ """The extras PM's first generation selects when it replaces a main-era venv.
+
+ Main-era installers selected ``[all]`` and then lazily installed opt-in
+ extras (FAL, messaging SDKs, ...) into the checkout's own venv, with no
+ ledger. Selecting only ``[all]`` drops those, and the first launch after
+ the migration asks to reinstall a feature that already worked. The old
+ venv's site-packages is read, never imported: the migrating process may
+ not run from it.
+ """
+ root = Path(project_root)
+ trees = [tree for venv in (root / "venv", root / ".venv")
+ for tree in (*venv.glob("lib/python*/site-packages"), venv / "Lib" / "site-packages")
+ if tree.is_dir()]
+ carried = sorted(
+ # An umbrella extra shares its anchor with one member; carrying it
+ # would install every sibling the user never chose.
+ extra for extra in ANCHORS if extra not in {"messaging", "voice", "wake"}
+ # PM refuses a gated extra outside its platform even if a hand-synced venv carried it.
+ if extra_supported(extra, importable=lambda _anchor: False)
+ and any(all(_installed_in(tree, anchor) for anchor in _anchors(extra)) for tree in trees)
+ )
+ return ["all", *carried]
diff --git a/tests/pm/test_extras.py b/tests/pm/test_extras.py
index 0013fed04a5..0a27ee8427a 100644
--- a/tests/pm/test_extras.py
+++ b/tests/pm/test_extras.py
@@ -217,3 +217,25 @@ def test_every_anchor_extra_exists_in_pyproject():
declared = set(py["project"]["optional-dependencies"])
orphans = set(extras.ANCHORS) - declared
assert not orphans, f"ANCHORS names extras pyproject does not declare: {sorted(orphans)}"
+
+
+def test_legacy_selection_carries_extras_the_main_era_venv_lazily_installed(monkeypatch, tmp_path):
+ """Migrating a main-era venv must keep opt-in extras it already had (FAL
+ image generation, a messaging SDK), or the first PM launch prompts to
+ reinstall them. Umbrella and gated-off extras are never carried."""
+ monkeypatch.setattr(extras, "_PLATFORM_GATES", {"piper": "python_version < '0'"})
+ site = tmp_path / "venv" / "lib" / "python3.11" / "site-packages"
+ (site / "fal_client").mkdir(parents=True)
+ (site / "telegram").mkdir()
+ (site / "piper").mkdir()
+ (site / "google").mkdir()
+ (site / "google" / "auth").mkdir()
+ (site / "exa_py.cpython-311-x86_64-linux-gnu.so").write_bytes(b"")
+
+ selection = extras.legacy_selection(tmp_path)
+
+ assert selection[0] == "all"
+ assert {"fal", "telegram", "vertex", "exa"} <= set(selection)
+ assert "messaging" not in selection
+ assert "piper" not in selection
+ assert extras.legacy_selection(tmp_path / "no-venv") == ["all"]