Skip to content

The agent that grows with you

fix(gateway): read the UTF-16 Windows launchers when looking for an existing install

unsupportedpastels1a3816fa329f
diff --git a/hermes_cli/gateway_runtime_service_identity.py b/hermes_cli/gateway_runtime_service_identity.py
index 79554b3e82c..b8c16667334 100644
--- a/hermes_cli/gateway_runtime_service_identity.py
+++ b/hermes_cli/gateway_runtime_service_identity.py
@@ -306,9 +306,10 @@ def read_definition(path: Path) -> bytes:
return value
-def _launcher_text(raw: bytes) -> str:
+def launcher_text(raw: bytes) -> str:
"""Current .vbs launchers are UTF-16 with a BOM (wscript.exe reads a BOM-less script in the ANSI
- code page); launchers written by older versions are UTF-8. Both must verify."""
+ code page); launchers written by older versions, and every other service definition, are UTF-8.
+ Undecodable bytes raise ``ValueError``."""
if raw[:2] in (b"\xff\xfe", b"\xfe\xff"):
try:
return raw.decode("utf-16")
@@ -393,6 +394,6 @@ def verify_windows_task(xml: str | bytes, home: Path, account: str, sid: str) ->
script = _absolute(match[1])
if script.suffix.lower() != '.vbs':
_unverified()
- configured, argv = _vbs_identity(_launcher_text(read_definition(script)))
+ configured, argv = _vbs_identity(launcher_text(read_definition(script)))
verify_home(home, configured)
verify_gateway_argv(argv, home)
diff --git a/hermes_cli/install_state.py b/hermes_cli/install_state.py
index fd3def7ff08..debb8fcfbd0 100644
--- a/hermes_cli/install_state.py
+++ b/hermes_cli/install_state.py
@@ -266,10 +266,11 @@ def _service_homes() -> Iterable[Path]:
def _definition_homes(definition: Path, *, machine_wide: bool, follow: bool = True) -> Iterator[Path]:
"""The homes one definition names, and those named by the launchers it starts (the Windows
Startup-folder fallback names only its launcher). Unreadable: none."""
+ from hermes_cli.gateway_runtime_service_identity import launcher_text
from hermes_cli.gateway_service_owner import service_definition_homes, service_definition_launchers
try:
- text = _read_text(definition)
+ text = launcher_text(_read_bytes(definition))
except (OSError, UnicodeError, ValueError):
return
account = _unit_account(text) or ("root" if machine_wide else None)
@@ -463,10 +464,14 @@ CHECKS: tuple[tuple[Evidence, Callable[[_Root], bool]], ...] = (
# ── small readers ─────────────────────────────────────────────────────────────────────────────
-def _read_text(path: Path) -> str:
+def _read_bytes(path: Path) -> bytes:
if path.stat().st_size > _MAX_FILE_BYTES:
raise ValueError(f"{path} is larger than {_MAX_FILE_BYTES} bytes")
- return path.read_text(encoding="utf-8-sig")
+ return path.read_bytes()
+
+
+def _read_text(path: Path) -> str:
+ return _read_bytes(path).decode("utf-8-sig")
def _read_mapping(path: Path, parse: Callable[[str], Any]) -> dict[str, Any]:
diff --git a/tests/hermes_cli/test_gateway_runtime_service.py b/tests/hermes_cli/test_gateway_runtime_service.py
index d9f5354b1ab..8a0264d4001 100644
--- a/tests/hermes_cli/test_gateway_runtime_service.py
+++ b/tests/hermes_cli/test_gateway_runtime_service.py
@@ -348,8 +348,7 @@ def test_task_discovery_skips_listing_lines_that_are_not_task_rows(tmp_path, mon
return SimpleNamespace(returncode=0, stdout=next(text for key, text in out.items() if key in argv).encode())
monkeypatch.setattr(service, "_run", run)
- monkeypatch.setattr(service.sys, "platform", "win32")
- found = service.discover_existing_gateway_service(home, deadline=time.monotonic() + 5)
+ found = service._windows(home, time.monotonic() + 5)
if installed:
assert found is not None and found.start_argv == ("schtasks.exe", "/Run", "/TN", name)
else:
diff --git a/tests/hermes_cli/test_install_state.py b/tests/hermes_cli/test_install_state.py
index cdb71c21079..36fb33e9e5e 100644
--- a/tests/hermes_cli/test_install_state.py
+++ b/tests/hermes_cli/test_install_state.py
@@ -163,22 +163,29 @@ def test_gateway_service_definitions_name_their_homes(tmp_path, monkeypatch):
def test_a_gateway_started_by_a_scheduled_task_or_the_startup_fallback_names_its_home(tmp_path, monkeypatch):
"""A Scheduled Task install removes the Startup-folder entries, and the Startup fallback only starts
- the home's own launcher: both name their home through the ``gateway.vbs`` they start."""
+ the home's own launcher: both name their home through the ``gateway.vbs`` they start. The installer
+ writes both launchers as UTF-16 with a BOM, which discovery must read like the UTF-8 ones it wrote before."""
+ import sys
+
from hermes_cli import gateway_windows
from hermes_platform.resolver import known_dirs
- def launcher(home: Path) -> Path: # what _write_task_script leaves in the home
- script = home / "gateway-service" / "Hermes_Gateway.vbs"
+ def launcher(home: Path) -> Path: # the .cmd wrapper _write_task_script leaves in the home, gateway.vbs beside it
+ script = home / "gateway-service" / "Hermes_Gateway.cmd"
script.parent.mkdir(parents=True)
- script.write_text(f'env.Item("HERMES_HOME") = "{home}"\r\n', encoding="utf-8")
- return script
+ monkeypatch.setattr(gateway_windows, "get_task_script_path", lambda: script)
+ monkeypatch.setattr(gateway_windows, "_launcher_settings", lambda: (sys.executable, str(home), str(home), ""))
+ return gateway_windows._write_task_script()
- by_task, by_startup = tmp_path / "task home", tmp_path / "startup home"
+ monkeypatch.setattr(gateway_windows, "_assert_windows", lambda: None)
+ by_task, by_startup = tmp_path / "task home 方舟", tmp_path / "startup home Usuário"
startup = tmp_path / "Startup" / "Hermes_Gateway.vbs"
- startup.parent.mkdir()
- startup.write_text(gateway_windows._build_startup_launcher(launcher(by_startup).with_suffix(".cmd")), encoding="utf-8")
+ monkeypatch.setattr(gateway_windows, "get_startup_entry_path", lambda: startup)
+ monkeypatch.setattr(gateway_windows, "_legacy_startup_entry_path", lambda: startup.with_suffix(".cmd"))
+ gateway_windows._install_startup_entry(launcher(by_startup))
+ task_launcher = launcher(by_task).with_suffix(".vbs")
monkeypatch.setattr(known_dirs, "gateway_service_definition_globs", lambda: (str(startup.parent / "Hermes_Gateway*"),))
- monkeypatch.setattr(gateway_windows, "registered_task_launchers", lambda: [str(launcher(by_task))], raising=False)
+ monkeypatch.setattr(gateway_windows, "registered_task_launchers", lambda: [str(task_launcher)], raising=False)
assert {by_task, by_startup} <= set(install_state._service_homes())