Skip to content

The agent that grows with you

fix(relay): validate migration through runtime layers

Alex Fournier423da3700694
diff --git a/hermes_cli/relay_plugin_migrate.py b/hermes_cli/relay_plugin_migrate.py
index f50834ac474..7d06322d014 100644
--- a/hermes_cli/relay_plugin_migrate.py
+++ b/hermes_cli/relay_plugin_migrate.py
@@ -14,6 +14,7 @@ from __future__ import annotations
import logging
from dataclasses import dataclass, field
from pathlib import Path
+from tempfile import TemporaryDirectory
from typing import Any, Mapping, Optional
from hermes_cli.relay_plugin_cutover import (
@@ -125,12 +126,18 @@ def dumps_toml(document: Mapping[str, Any]) -> str:
def validate_relay_plugin_payload(payload: Mapping[str, Any]) -> list:
- """Run the payload through Relay's own validator; returns the warnings (empty = clean).
- Raises when Relay rejects the document, including by error-level diagnostics."""
+ """Validate the serialized payload through the same file layering the runtime uses.
+
+ Returns warnings (empty = clean) and raises when Relay rejects the document,
+ including by error-level diagnostics.
+ """
from nemo_relay import plugin
- # validate() would layer the payload over the ambient user config; the runtime never does.
- diagnostics = list(plugin.validate_exact(dict(payload))["config"]["diagnostics"])
+ with TemporaryDirectory(prefix="hermes-relay-validation-") as directory:
+ config_path = Path(directory) / RELAY_PLUGINS_TOML_NAME
+ config_path.write_text(dumps_toml(payload), encoding="utf-8")
+ report = plugin.validate({}, additional_plugins_toml=config_path)
+ diagnostics = list(report["config"]["diagnostics"])
# Relay 0.8's initialize() raised on these; 0.9's validator only reports them.
if errors := [d for d in diagnostics if d.get("level") == "error"]:
raise ValueError("; ".join(str(d.get("message") or d.get("code") or d) for d in errors))
diff --git a/tests/hermes_cli/test_relay_plugin_migrate.py b/tests/hermes_cli/test_relay_plugin_migrate.py
index 3e2b7d5def9..a343bac7367 100644
--- a/tests/hermes_cli/test_relay_plugin_migrate.py
+++ b/tests/hermes_cli/test_relay_plugin_migrate.py
@@ -96,8 +96,8 @@ def _relay_report(*diagnostics):
def test_error_diagnostics_reject_the_payload_like_relay_0_8_did(monkeypatch):
monkeypatch.setattr(
nemo_relay.plugin,
- "validate_exact",
- lambda _payload: _relay_report(
+ "validate",
+ lambda _payload, additional_plugins_toml=None: _relay_report(
{"level": "warning", "code": "unknown_field", "message": "unknown field 'x'"},
{"level": "error", "code": "unsupported_value", "message": "atof.mode 'nope' is unsupported"},
),
@@ -109,9 +109,19 @@ def test_error_diagnostics_reject_the_payload_like_relay_0_8_did(monkeypatch):
def test_warning_diagnostics_are_returned_not_raised(monkeypatch):
warning = {"level": "warning", "code": "unknown_field", "message": "unknown field 'x'"}
- monkeypatch.setattr(nemo_relay.plugin, "validate_exact", lambda _payload: _relay_report(warning))
+ observed = {}
+
+ def validate(payload, additional_plugins_toml=None):
+ observed["payload"] = payload
+ observed["document"] = tomllib.loads(
+ Path(additional_plugins_toml).read_text(encoding="utf-8")
+ )
+ return _relay_report(warning)
+
+ monkeypatch.setattr(nemo_relay.plugin, "validate", validate)
assert validate_relay_plugin_payload({"version": 1}) == [warning]
+ assert observed == {"payload": {}, "document": {"version": 1}}
def test_error_diagnostics_leave_env_untouched(profile_env, monkeypatch):
@@ -119,8 +129,10 @@ def test_error_diagnostics_leave_env_untouched(profile_env, monkeypatch):
before = (profile_env / ".env").read_text(encoding="utf-8")
monkeypatch.setattr(
nemo_relay.plugin,
- "validate_exact",
- lambda _payload: _relay_report({"level": "error", "code": "bad", "message": "rejected"}),
+ "validate",
+ lambda _payload, additional_plugins_toml=None: _relay_report(
+ {"level": "error", "code": "bad", "message": "rejected"}
+ ),
)
result = migrate_profile_relay_env(profile_env)