hermes-agent/tests/gateway/relay/test_cold_opt_out.py
ethernet 284dbaf537 fix(pm): isolate bootstrap dependencies and unify YAML on ruamel
Activation reaches plugin discovery before the application dependencies
exist. Give PM its own locked Python project and runtime so it can install
or repair the application without importing that dependency tree.

Keep PM outside the application workspace. A shared uv workspace resolves
the application graph and cannot provide this isolation. Route mutations
through an isolated worker and preserve transaction callbacks, cancellation,
custom package registrations, and correlated receipts.

Use the same runtime builder for source installs and packaged payloads.
Keep offline wheelhouse support in that builder. Nix builds the independent
PM lock as a separate derivation. Refuse lazy-disabled bootstrap before
installing tools or dependencies.

Move first-party YAML readers and writers to ruamel. Keep the application
lock's transitive PyYAML requirements for third-party packages.

Verification:
- Focused canonical Python suite: 177 passed, 1 host-gated skip.
- Electron backend probes: 12 passed. Electron typecheck passed.
- Both uv locks, scoped lint, Bash syntax, and whitespace checks passed.
- Cold activation, corrupt-app repair, offline staging, and relocation ran.
- Built and exercised the Nix PM runtime and standalone YAML merge script.

Six broader caller test files retain the same 24 failing test IDs as an
archive of HEAD. The existing real-home guard blocks those tests before
they can exercise the affected paths. No full-suite pass is claimed.
Native Windows signing and full Bionic package execution remain unverified.
2026-09-11 12:23:51 -04:00

64 lines
3 KiB
Python

"""Standalone relay routing must not bootstrap the gateway or reload secrets."""
import json
import os
from pathlib import Path
import subprocess
import sys
import textwrap
import pytest
import hermes_yaml as yaml
@pytest.mark.parametrize("case", ["native", "url-only", "disabled", "managed", "managed-only", "scoped"])
def test_cold_fronted_platforms_is_read_only(tmp_path, case):
home = tmp_path / "primary"
home.mkdir()
scoped = tmp_path / "secondary"
scoped.mkdir()
managed = tmp_path / "managed"
managed.mkdir()
config = {"platforms": {"relay": {"enabled": True}}}
if case == "disabled":
config["platforms"]["relay"]["enabled"] = False
if case not in {"native", "url-only", "managed-only"}:
(home / "config.yaml").write_text(yaml.safe_dump(config))
if case in {"managed", "managed-only"}:
(managed / "config.yaml").write_text("platforms:\n relay:\n enabled: false\n")
if case == "scoped":
(scoped / "config.yaml").write_text("platforms:\n relay:\n enabled: false\n")
for root in (home, scoped):
(root / ".env").write_text("GATEWAY_RELAY_SECRET=dotenv-only-test-secret\n")
# Allowlist only: never inherit live credentials, profile selectors, or
# pytest's imported modules. HOME and cwd are temporary as well as HERMES_HOME.
env = {k: os.environ[k] for k in ("PATH", "SYSTEMROOT", "WINDIR") if k in os.environ}
env.update({"HOME": str(tmp_path), "USERPROFILE": str(tmp_path),
"HERMES_HOME": str(home), "HERMES_MANAGED_DIR": str(managed),
"PYTHONPATH": str(Path(__file__).resolve().parents[3]),
"PYTHONDONTWRITEBYTECODE": "1", "PYTHONUTF8": "1"})
if case != "native":
env.update({"GATEWAY_RELAY_URL": "wss://connector.example/relay",
"GATEWAY_RELAY_PLATFORMS": "slack",
"GATEWAY_RELAY_SECRET": "inherited-test-secret"})
code = textwrap.dedent("""
import json, os, sys
from hermes_constants import set_hermes_home_override
if sys.argv[1] == "scoped":
set_hermes_home_override(sys.argv[2])
before = dict(os.environ)
assert "gateway.run" not in sys.modules
from gateway.relay import relay_fronted_platforms
fronted = relay_fronted_platforms()
changed = {k: [before.get(k), os.environ.get(k)]
for k in before.keys() | os.environ.keys()
if before.get(k) != os.environ.get(k)}
print(json.dumps({"fronted": sorted(fronted),
"bootstrapped": "gateway.run" in sys.modules,
"changed_env": changed}))
""")
child = subprocess.run([sys.executable, "-c", code, case, str(scoped)],
cwd=tmp_path, env=env, text=True, capture_output=True, timeout=45)
assert child.returncode == 0, child.stderr
observed = json.loads(child.stdout.splitlines()[-1])
assert observed == {"fronted": ["slack"] if case == "url-only" else [],
"bootstrapped": False, "changed_env": {}}