Skip to content

The agent that grows with you

fix(update): start the post-swap child in the checkout

joaomarcos949a8bb0ab87
diff --git a/hermes_cli/update_handoff.py b/hermes_cli/update_handoff.py
index 6d1b5980432..13729846ce8 100644
--- a/hermes_cli/update_handoff.py
+++ b/hermes_cli/update_handoff.py
@@ -183,6 +183,17 @@ def _print_manual_continuation(cmd: list[str], exc: OSError) -> None:
print(f" {subprocess.list2cmdline(cmd)}")
+def _post_swap_cwd() -> str:
+ """Directory the child must start in so a stale editable finder can still import.
+
+ ``python -m`` puts the process cwd on ``sys.path[0]``, not the checkout. Invoking
+ ``hermes update`` from elsewhere then resolves new top-level names only through the
+ installed finder. If that map is stale, the child dies in ``import hermes_cli.main``
+ before the dependency sync can refresh it (#119466).
+ """
+ return str(Path(__file__).resolve().parent.parent)
+
+
def continue_update_in_fresh_interpreter(payload: dict[str, Any], *, argv_tail: list[str]) -> int | None:
"""Run the post-swap tail in a child interpreter on the pulled code.
@@ -201,13 +212,15 @@ def continue_update_in_fresh_interpreter(payload: dict[str, Any], *, argv_tail:
handoff_path = write_handoff(payload)
cmd = post_swap_command(handoff_path, argv_tail)
env = post_swap_child_env()
+ cwd = _post_swap_cwd()
logger.debug("Post-swap hand-off → %s", subprocess.list2cmdline(cmd))
sys.stdout.flush()
sys.stderr.flush()
if _running_from_windows_shim():
try:
- subprocess.Popen(cmd, env=detached_shim_child_env(env), stdin=subprocess.DEVNULL)
+ subprocess.Popen(
+ cmd, env=detached_shim_child_env(env), stdin=subprocess.DEVNULL, cwd=cwd)
except OSError as exc:
_print_manual_continuation(cmd, exc)
return None
@@ -217,7 +230,7 @@ def continue_update_in_fresh_interpreter(payload: dict[str, Any], *, argv_tail:
return 0
try:
- child = subprocess.Popen(cmd, env=env, stdin=sys.stdin)
+ child = subprocess.Popen(cmd, env=env, stdin=sys.stdin, cwd=cwd)
except OSError as exc:
_print_manual_continuation(cmd, exc)
return None
diff --git a/tests/hermes_cli/test_update_handoff_checkout_cwd.py b/tests/hermes_cli/test_update_handoff_checkout_cwd.py
new file mode 100644
index 00000000000..b3a0dbf278f
--- /dev/null
+++ b/tests/hermes_cli/test_update_handoff_checkout_cwd.py
@@ -0,0 +1,42 @@
+"""The post-swap child must start in the checkout, not the caller's cwd.
+
+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.
+"""
+
+import sys
+
+import hermes_cli.update_handoff as handoff
+
+
+class _Child:
+ def wait(self, timeout=None):
+ return 0
+
+
+def test_both_handoff_launches_use_the_checkout_cwd(monkeypatch, tmp_path):
+ recorded = []
+ checkout = tmp_path / "checkout"
+ checkout.mkdir()
+ assert handoff._post_swap_cwd() == str(handoff.Path(handoff.__file__).resolve().parent.parent)
+
+ monkeypatch.setattr(handoff, "write_handoff", lambda payload: tmp_path / "handoff.json")
+ monkeypatch.setattr(handoff, "post_swap_python", lambda: sys.executable)
+ monkeypatch.setattr(handoff, "post_swap_child_env", lambda: {"HERMES_UPDATE_POST_SWAP": "1"})
+ monkeypatch.setattr(handoff, "detached_shim_child_env", lambda env: env)
+ monkeypatch.setattr(handoff, "_post_swap_cwd", lambda: str(checkout))
+ monkeypatch.setattr(
+ handoff.subprocess,
+ "Popen",
+ lambda cmd, **kwargs: recorded.append(kwargs.get("cwd")) or _Child(),
+ )
+
+ monkeypatch.setattr(handoff, "_running_from_windows_shim", lambda: False)
+ assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=["--yes"]) == 0
+
+ monkeypatch.setattr(handoff, "_running_from_windows_shim", lambda: True)
+ assert handoff.continue_update_in_fresh_interpreter({}, argv_tail=["--yes"]) == 0
+
+ assert recorded == [str(checkout), str(checkout)]