Skip to content

The agent that grows with you

fix(update): accept legacy post-swap completion

etherneta76da6e32288
diff --git a/hermes_bootstrap.py b/hermes_bootstrap.py
index bba7799741c..01d69f31976 100644
--- a/hermes_bootstrap.py
+++ b/hermes_bootstrap.py
@@ -335,6 +335,32 @@ except FileNotFoundError:
# import paths before the CLI's guards, so recover before any PM work.
os.chdir(_root)
+
+def _legacy_post_swap_invocation(argv: list[str]) -> tuple[Path, list[str]] | None:
+ """Recognize the exact fresh-checkout command emitted by shipped updaters."""
+ if not argv or argv[0] != "update":
+ return None
+ try:
+ marker = argv.index("--post-swap", 1)
+ except ValueError:
+ return None
+ if marker + 2 != len(argv):
+ return None
+ return Path(argv[marker + 1]), argv[1:marker]
+
+
+_legacy_post_swap = _legacy_post_swap_invocation(sys.argv[1:])
+if _legacy_post_swap is not None:
+ # This continuation exists precisely because the replacement tree may not
+ # run under the old release's dependency graph. Take it over before PM
+ # activation, launch preparation, or argparse imports any of that graph.
+ harden_import_path(str(_root))
+ from hermes_cli.update_handoff import _continue_legacy_post_swap
+
+ _handoff_path, _argv_tail = _legacy_post_swap
+ raise SystemExit(_continue_legacy_post_swap(_handoff_path, argv_tail=_argv_tail))
+
+
from pm.environments import activate_dependencies
from hermes_cli._early_recovery import recover_if_needed
diff --git a/hermes_cli/update_handoff.py b/hermes_cli/update_handoff.py
index c435d79c723..c62e9e58e46 100644
--- a/hermes_cli/update_handoff.py
+++ b/hermes_cli/update_handoff.py
@@ -105,6 +105,25 @@ def _takeover_request(payload: dict[str, Any], argv_tail: list[str] | None) -> d
return request
+def _continue_legacy_post_swap(handoff_path: str | Path, *, argv_tail: list[str]) -> int:
+ """Complete the command shape shipped before PM owned source updates.
+
+ Those releases start the replacement checkout as ``hermes update <flags>
+ --post-swap FILE``. The replacement bootstrap calls this before importing
+ its PM or CLI graph, then the existing takeover child prepares and finishes
+ the update entirely on replacement code.
+ """
+ payload = read_handoff(handoff_path)
+ try:
+ Path(handoff_path).unlink()
+ except OSError:
+ pass
+ from hermes_cli._old_updater import _run_child
+
+ code, _completed = _run_child(_takeover_request(payload, argv_tail))
+ return int(code)
+
+
def continue_update_in_fresh_interpreter(payload: dict[str, Any], *, argv_tail: list[str] | None = None) -> int | None:
"""Run the post-swap tail in a child interpreter on the pulled code.
diff --git a/tests/hermes_cli/test_old_updater_takeover.py b/tests/hermes_cli/test_old_updater_takeover.py
index 2919829aca2..6e76aad14cd 100644
--- a/tests/hermes_cli/test_old_updater_takeover.py
+++ b/tests/hermes_cli/test_old_updater_takeover.py
@@ -78,6 +78,59 @@ def test_historical_payload_maps_to_takeover_request_schema(tmp_path, desktop, r
assert "desktop" not in handoff and "assume_yes" not in handoff
[email protected]_system_guard_bypass
+def test_shipped_post_swap_argv_enters_takeover_before_current_cli(tmp_path):
+ """The 2026.9.21 updater starts HEAD as ``hermes update <flags> --post-swap FILE``.
+
+ That command must reach the historical takeover before current launch preparation or
+ argparse: both belong to the replacement updater and may require dependencies the old
+ environment has not installed yet.
+ """
+ source = Path(__file__).resolve().parents[2]
+ root = tmp_path / "updated checkout"
+ package = root / "hermes_cli"
+ package.mkdir(parents=True)
+ (package / "__init__.py").write_text("", encoding="utf-8")
+ (package / "main.py").write_text(
+ "import hermes_bootstrap\nraise AssertionError('current CLI parsed a legacy continuation')\n",
+ encoding="utf-8",
+ )
+ for relative in ("hermes_bootstrap.py", "hermes_cli/update_handoff.py", "hermes_cli/_old_updater.py"):
+ shutil.copy2(source / relative, root / relative)
+ (package / "_update_takeover.py").write_text(
+ "import json, sys\nfrom pathlib import Path\n"
+ "request = json.loads(Path(sys.argv[1]).read_text())\n"
+ "assert request['desktop'] is True\n"
+ "assert request['assume_yes'] is True\n"
+ "assert request['argv'][1:] == ['update', '--yes', '--no-gateway-restart', "
+ "'--branch', 'main', '--post-swap', request['legacy_handoff']]\n"
+ "Path(sys.argv[2]).write_text(json.dumps({'resume_handled': True}), encoding='utf-8')\n",
+ encoding="utf-8",
+ )
+ home = tmp_path / "home"
+ home.mkdir()
+ handoff = home / "post_swap_3185.json"
+ payload = {
+ "legacy_handoff": str(handoff),
+ "gateway_mode": False,
+ "had_desktop_app_before_update": True,
+ "windows_gateway_resume": None,
+ "plan": {"install_method": "git"},
+ "receipt": {"update_id": "old-correlation", "outcome": "running"},
+ }
+ handoff.write_text(json.dumps(payload), encoding="utf-8")
+ env = {key: value for key, value in os.environ.items()
+ if not key.startswith(("HERMES_", "PYTHON", "UV_"))}
+ env.update(HOME=str(home), HERMES_HOME=str(home))
+ result = subprocess.run(
+ [sys.executable, "-B", "-m", "hermes_cli.main", "update", "--yes",
+ "--no-gateway-restart", "--branch", "main", "--post-swap", str(handoff)],
+ cwd=root, env=env, capture_output=True, text=True, timeout=30,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert not handoff.exists()
+
+
@pytest.mark.parametrize("status", [0, 7])
@pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"])
@pytest.mark.parametrize("desktop", [None, False, True])