Skip to content

The agent that grows with you

fix(install): preserve apostrophe paths in Windows runtime launch

Gilleea114c3e98c3
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index 8c6464c9ae6..2702fef02a2 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -23,6 +23,14 @@ if __name__ == "__main__":
from pm.environments import store_root
+def _inline_string_literal(value: str) -> str:
+ """Keep inline Python source intact through Windows PowerShell's native argv quoting."""
+ if os.name != "nt":
+ return repr(value)
+ escaped = value.encode("unicode_escape").decode("ascii")
+ return "'" + escaped.replace("'", "\\x27").replace('"', "\\x22") + "'"
+
+
def runtime_command(repo_root: Path, args=(), *, module: str = "hermes_cli.main",
code: str | None = None, python: str | Path | None = None,
home: str | Path | None = None) -> list[str]:
@@ -34,15 +42,15 @@ def runtime_command(repo_root: Path, args=(), *, module: str = "hermes_cli.main"
"""
root = Path(repo_root).resolve()
python = python or resolve_store_python(root) or Path(sys.executable)
- entry = f"exec({code!r})" if code is not None else (
- f"runpy.run_module({module!r}, run_name='__main__', alter_sys=True)")
- default_home = (f"{str(home)!r}" if home is not None else
+ entry = f"exec({_inline_string_literal(code)})" if code is not None else (
+ f"runpy.run_module({_inline_string_literal(module)}, run_name='__main__', alter_sys=True)")
+ default_home = (_inline_string_literal(str(home)) if home is not None else
"str(__import__('hermes_constants').get_default_hermes_root())")
bootstrap = (
"import os, sys, runpy; "
"os.environ.pop('PYTHONHOME', None); os.environ.pop('PYTHONPATH', None); "
"os.environ.pop('VIRTUAL_ENV', None); "
- f"sys.path.insert(0, {str(root)!r}); "
+ f"sys.path.insert(0, {_inline_string_literal(str(root))}); "
f"os.environ['HERMES_HOME'] = os.environ.get('HERMES_HOME') or {default_home}; "
"import hermes_bootstrap; "
+ entry
diff --git a/tests/scripts/install/test_install_ps1_execution_policy.py b/tests/scripts/install/test_install_ps1_execution_policy.py
index 6f1f5e5842a..7da92978124 100644
--- a/tests/scripts/install/test_install_ps1_execution_policy.py
+++ b/tests/scripts/install/test_install_ps1_execution_policy.py
@@ -8,11 +8,16 @@ the resolved runtime command must actually run (a `$command` local once
collided with Invoke-Native's `$Command` parameter and recursed forever).
"""
from pathlib import Path
+import json
+import os
import shutil
import subprocess
+import sys
import pytest
+from hermes_cli._launchers import runtime_command
+
pytestmark = pytest.mark.platforms("windows")
INSTALLER = Path(__file__).resolve().parents[3] / "scripts" / "install.ps1"
@@ -38,3 +43,39 @@ def test_installed_hermes_runs_under_restricted_policy(tmp_path):
output = result.stdout + result.stderr
assert "running scripts is disabled" not in output, output
assert f"runtime-from {helper.parents[2]} setup" in result.stdout, output
+
+
+def test_installer_runs_runtime_command_from_apostrophe_account_path(tmp_path):
+ """A Windows account apostrophe must survive launcher JSON -> PowerShell -> Python -c."""
+ powershell = shutil.which("powershell")
+ assert powershell
+ install_root = tmp_path / "O'Brien" / "hermes-agent"
+ helper = install_root / "scripts" / "desktop-update" / "runtime.ps1"
+ helper.parent.mkdir(parents=True)
+ (install_root / "hermes_bootstrap.py").write_text("", encoding="utf-8")
+ helper.write_text(
+ "function Get-HermesRuntimeCommand([string]$InstallRoot) {\n"
+ " $items = $env:HERMES_TEST_RUNTIME_COMMAND | ConvertFrom-Json\n"
+ " foreach ($item in $items) { Write-Output $item }\n"
+ "}\n",
+ encoding="utf-8",
+ )
+ selected = runtime_command(
+ install_root, code="print('setup-runtime-ok')", python=sys.executable,
+ home=install_root.parent / ".hermes",
+ )
+ env = os.environ.copy()
+ env["HERMES_TEST_RUNTIME_COMMAND"] = json.dumps(selected)
+ quoted_installer = str(INSTALLER).replace("'", "''")
+ quoted_root = str(install_root).replace("'", "''")
+ command = (f". '{quoted_installer}'; "
+ f"$InstallDir = '{quoted_root}'; "
+ "Invoke-InstalledHermes @('setup')")
+ result = subprocess.run(
+ [powershell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", command],
+ cwd=tmp_path, env=env, stdin=subprocess.DEVNULL,
+ capture_output=True, text=True, timeout=60,
+ )
+ output = result.stdout + result.stderr
+ assert result.returncode == 0, output
+ assert "setup-runtime-ok" in result.stdout, output