Skip to content

The agent that grows with you

test(update): prove the hand-off child imports past a stale finder

joaomarcoscfa455e5983f
diff --git a/tests/hermes_cli/test_update_handoff_checkout_cwd.py b/tests/hermes_cli/test_update_handoff_checkout_cwd.py
index b3a0dbf278f..15c8c9055fb 100644
--- a/tests/hermes_cli/test_update_handoff_checkout_cwd.py
+++ b/tests/hermes_cli/test_update_handoff_checkout_cwd.py
@@ -2,20 +2,100 @@
A stale setuptools finder cannot import a new top-level package. ``python -m``
only adds the process cwd to ``sys.path``, so a hand-off spawned from outside
-the checkout dies in ``import hermes_cli.main`` before the dependency sync can
-refresh the map.
+the checkout dies before the dependency sync can refresh the map.
"""
+import os
+import subprocess
import sys
+import textwrap
+import time
import hermes_cli.update_handoff as handoff
+FINDER_SRC = textwrap.dedent(
+ """\
+ import sys
+ from importlib.machinery import PathFinder
+ from pathlib import Path
+
+ MAPPING = {"oldpkg": %r}
+
+ class _Finder:
+ @classmethod
+ def find_spec(cls, fullname, path, target=None):
+ root = MAPPING.get(fullname.split(".", 1)[0])
+ if root is None:
+ return None
+ return PathFinder.find_spec(fullname, [str(Path(root).parent)])
+
+ def install():
+ if _Finder not in sys.meta_path:
+ sys.meta_path.insert(0, _Finder)
+ """
+)
+
+CHILD_SRC = textwrap.dedent(
+ """\
+ import importlib.util
+ import os
+ import pathlib
+
+ spec = importlib.util.spec_from_file_location(
+ "stale_finder", os.environ["HANDOFF_FINDER"])
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ mod.install()
+ import oldpkg
+ pathlib.Path(os.environ["HANDOFF_MARKER"]).write_text("sync:" + oldpkg.MARKER)
+ """
+)
+
+
class _Child:
def wait(self, timeout=None):
return 0
+def _layout(tmp_path):
+ checkout = tmp_path / "checkout"
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ oldpkg = checkout / "oldpkg"
+ newpkg = checkout / "handoff_newpkg"
+ oldpkg.mkdir(parents=True)
+ newpkg.mkdir()
+ (oldpkg / "__init__.py").write_text(
+ "import handoff_newpkg\nMARKER = handoff_newpkg.MARKER\n",
+ encoding="utf-8",
+ )
+ (newpkg / "__init__.py").write_text("MARKER = 'sync'\n", encoding="utf-8")
+ finder = tmp_path / "stale_finder.py"
+ finder.write_text(FINDER_SRC % str(oldpkg), encoding="utf-8")
+ return checkout, outside, finder
+
+
+def _child_env(finder, marker):
+ env = os.environ.copy()
+ env["HANDOFF_FINDER"] = str(finder)
+ env["HANDOFF_MARKER"] = str(marker)
+ env["PYTHONPATH"] = ""
+ env.pop("PYTHONSAFEPATH", None)
+ return env
+
+
+def _run(cmd_cwd, env):
+ return subprocess.run(
+ [sys.executable, "-c", CHILD_SRC],
+ cwd=cmd_cwd,
+ env=env,
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+
+
def test_both_handoff_launches_use_the_checkout_cwd(monkeypatch, tmp_path):
recorded = []
checkout = tmp_path / "checkout"
@@ -40,3 +120,79 @@ def test_both_handoff_launches_use_the_checkout_cwd(monkeypatch, tmp_path):
assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=["--yes"]) == 0
assert recorded == [str(checkout), str(checkout)]
+
+
+def test_outside_cwd_child_reaches_the_new_package(monkeypatch, tmp_path):
+ """Parent cwd is outside; the stale map omits the new package; the child still imports it."""
+ checkout, outside, finder = _layout(tmp_path)
+ marker = tmp_path / "sync.txt"
+ env = _child_env(finder, marker)
+ monkeypatch.chdir(outside)
+
+ direct = _run(outside, env)
+ assert direct.returncode != 0
+ assert "handoff_newpkg" in (direct.stderr or "")
+ assert not marker.exists()
+
+ inside = _run(checkout, env)
+ assert inside.returncode == 0
+ assert marker.read_text(encoding="utf-8") == "sync:sync"
+ marker.unlink()
+
+ monkeypatch.setattr(handoff, "write_handoff", lambda payload: tmp_path / "handoff.json")
+ monkeypatch.setattr(handoff, "post_swap_command", lambda path, tail: [sys.executable, "-c", CHILD_SRC])
+ monkeypatch.setattr(handoff, "post_swap_child_env", lambda: env)
+ monkeypatch.setattr(handoff, "detached_shim_child_env", lambda child_env: child_env)
+ monkeypatch.setattr(handoff, "_post_swap_cwd", lambda: str(checkout))
+ monkeypatch.setattr(handoff, "_running_from_windows_shim", lambda: False)
+ monkeypatch.setattr(sys, "stdin", open(os.devnull, "r"))
+
+ assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=[]) == 0
+ assert marker.read_text(encoding="utf-8") == "sync:sync"
+ marker.unlink()
+
+ monkeypatch.setattr(handoff, "_running_from_windows_shim", lambda: True)
+ assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=[]) == 0
+ for _ in range(50):
+ if marker.exists():
+ break
+ time.sleep(0.1)
+ assert marker.read_text(encoding="utf-8") == "sync:sync"
+
+
+def test_unrelated_import_error_still_fails(monkeypatch, tmp_path):
+ checkout, outside, _finder = _layout(tmp_path)
+ monkeypatch.chdir(outside)
+ monkeypatch.setattr(handoff, "write_handoff", lambda payload: tmp_path / "handoff.json")
+ monkeypatch.setattr(
+ handoff,
+ "post_swap_command",
+ lambda path, tail: [sys.executable, "-c", "import this_package_is_not_in_the_tree"],
+ )
+ monkeypatch.setattr(handoff, "post_swap_child_env", lambda: os.environ.copy())
+ monkeypatch.setattr(handoff, "_post_swap_cwd", lambda: str(checkout))
+ monkeypatch.setattr(handoff, "_running_from_windows_shim", lambda: False)
+ monkeypatch.setattr(sys, "stdin", open(os.devnull, "r"))
+
+ assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=[]) != 0
+
+
+def test_current_map_imports_without_the_checkout_cwd(tmp_path):
+ """A package already visible on PYTHONPATH does not need the hand-off cwd."""
+ _checkout, outside, _finder = _layout(tmp_path)
+ visible = tmp_path / "visible"
+ pkg = visible / "handoff_newpkg"
+ pkg.mkdir(parents=True)
+ (pkg / "__init__.py").write_text("MARKER = 'sync'\n", encoding="utf-8")
+ env = os.environ.copy()
+ env["PYTHONPATH"] = str(visible)
+ proc = subprocess.run(
+ [sys.executable, "-c", "import handoff_newpkg; print(handoff_newpkg.MARKER)"],
+ cwd=outside,
+ env=env,
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ assert proc.returncode == 0
+ assert proc.stdout.strip() == "sync"
diff --git a/tests/hermes_cli/test_update_skip_unchanged_editable_install.py b/tests/hermes_cli/test_update_skip_unchanged_editable_install.py
index 3ab43556d5e..190146d8735 100644
--- a/tests/hermes_cli/test_update_skip_unchanged_editable_install.py
+++ b/tests/hermes_cli/test_update_skip_unchanged_editable_install.py
@@ -70,24 +70,29 @@ def _declare_packages(repo, include):
_commit(repo, "declare packages")
-def test_missing_new_top_level_package_forces_reinstall(repo, monkeypatch):
[email protected]("annotated", [False, True])
+def test_stale_map_refuses_the_skip(repo, monkeypatch, annotated):
+ """A missing top-level name refuses the skip for both finder spellings."""
_declare_packages(repo, ["agent", "agent.*", "newpkg", "newpkg.*"])
before = _head(repo)
(repo / "newpkg").mkdir()
(repo / "newpkg" / "__init__.py").write_text("")
_commit(repo, "add package")
- _finder(repo, {"agent": str(repo / "agent"), "cli": str(repo / "cli")}, annotated=True)
+ _finder(repo, {"agent": str(repo / "agent"), "cli": str(repo / "cli")}, annotated=annotated)
monkeypatch.setattr("hermes_cli.update_cmd_deps.project_venv_dir", lambda _: repo / "venv")
assert _editable_install_is_current(GIT, repo, before) is False
-def test_covered_annotated_finder_allows_source_churn(repo, monkeypatch):
- """A current setuptools map uses annotated assignment and module stems, not cli.py."""
[email protected]("annotated", [False, True])
[email protected]("layout", ["lib/python3.11/site-packages", "Lib/site-packages"])
+def test_covered_map_skips_source_churn(repo, monkeypatch, layout, annotated):
+ """A map that already names the checkout still skips source edits."""
_declare_packages(repo, ["agent", "agent.*"])
_finder(
repo,
{"agent": str(repo / "agent"), "cli": str(repo / "cli")},
- annotated=True,
+ layout=layout,
+ annotated=annotated,
)
monkeypatch.setattr("hermes_cli.update_cmd_deps.project_venv_dir", lambda _: repo / "venv")
before = _head(repo)
@@ -96,17 +101,6 @@ def test_covered_annotated_finder_allows_source_churn(repo, monkeypatch):
assert _editable_install_is_current(GIT, repo, before) is True
[email protected]("layout", ["lib/python3.11/site-packages", "Lib/site-packages"])
-def test_windows_and_posix_site_packages_are_both_read(repo, monkeypatch, layout):
- _declare_packages(repo, ["agent", "agent.*"])
- _finder(repo, {"agent": str(repo / "agent"), "cli": str(repo / "cli")}, layout=layout, annotated=True)
- monkeypatch.setattr("hermes_cli.update_cmd_deps.project_venv_dir", lambda _: repo / "venv")
- before = _head(repo)
- (repo / "agent" / "loop.py").write_text("y = 3\n")
- _commit(repo, "source churn")
- assert _editable_install_is_current(GIT, repo, before) is True
-
-
def test_unreadable_finder_fails_closed(repo, monkeypatch):
before = _head(repo)
site = repo / "venv" / "lib" / "python3.11" / "site-packages"