hermes-agent/tests/gateway/test_gateway_streaming_nested_config.py
John Paul Soliva 16db511ed5 perf(config): route the config and manifest loaders through fast_safe_load
utils.fast_safe_load already exists, is pinned by tests/test_fast_safe_load.py, and
its comment names exactly these payers: 'startup parses config.yaml and every plugin
manifest, so the slow path cost ~0.9 s of cold start'. The migration was started —
hermes_cli/config.py uses it eight times, hermes_cli/main.py and hermes_cli/plugins.py
too — but the file-level loaders it was written for were never converted.

The cost is config SIZE, and the size is the installer's doing: it seeds config.yaml
by copying cli-config.yaml.example, 120,897 bytes of mostly comments. Nothing caches
load_gateway_config() and it has 238 production call sites.

Profiled before assuming a cause — reader.forward 34 ms, scanner.scan_to_next_token
28 ms, reader.peek 13 ms: the pure-Python PyYAML scanner, nothing else.

Measured A/B on one realistic pass (gateway config + every bundled plugin
description), medians of five runs, __pycache__ cleared between arms:
48.8 ms -> 2.46 ms. Per path: load_gateway_config 48.3 -> 1.84 ms, managed
config.yaml 44.4 -> 1.03 ms, 105 plugin.yaml manifests 59.3 -> 5.9 ms.

Same parse, same restricted tag set, same result — only the loader changes. Drops the
three 'import yaml' statements the swap orphaned.

(cherry picked from commit a4efd6a450)
2026-09-23 21:26:49 +05:30

105 lines
3.7 KiB
Python

"""Regression test for #25676 — nested gateway.streaming config must be loaded."""
from unittest.mock import patch
import hermes_yaml as yaml
def _load_with_yaml_dict(yaml_dict: dict, tmp_path):
"""Load a real config.yaml through the gateway's shared YAML reader."""
from gateway.config import load_gateway_config
(tmp_path / "config.yaml").write_text(yaml.safe_dump(yaml_dict), encoding="utf-8")
with patch("gateway.config.get_hermes_home", return_value=tmp_path):
return load_gateway_config()
class TestStreamingConfigNested:
def test_top_level_streaming(self, tmp_path):
cfg = _load_with_yaml_dict({"streaming": {"enabled": True, "transport": "draft"}}, tmp_path)
assert cfg.streaming.enabled is True
assert cfg.streaming.transport == "draft"
def test_top_level_takes_precedence(self, tmp_path):
cfg = _load_with_yaml_dict({
"streaming": {"enabled": True, "transport": "edit"},
"gateway": {"streaming": {"enabled": False, "transport": "draft"}},
}, tmp_path)
assert cfg.streaming.enabled is True
assert cfg.streaming.transport == "edit"
class TestStreamingModeAlias:
"""``streaming: {mode: ...}`` is an alias that also implies ``enabled``.
Regression for a live config footgun: ``streaming: {mode: auto}`` was
silently ignored (mode was never read), so streaming stayed disabled and
the whole reply buffered before the first Telegram send.
"""
def test_mode_auto_enables_streaming(self):
from gateway.config import StreamingConfig
sc = StreamingConfig.from_dict({"mode": "auto"})
assert sc.enabled is True
assert sc.transport == "auto"
def test_mode_edit_enables_streaming(self):
from gateway.config import StreamingConfig
sc = StreamingConfig.from_dict({"mode": "edit"})
assert sc.enabled is True
assert sc.transport == "edit"
def test_explicit_enabled_overrides_mode(self):
from gateway.config import StreamingConfig
sc = StreamingConfig.from_dict({"mode": "auto", "enabled": False})
assert sc.enabled is False
# transport still resolves from mode
assert sc.transport == "auto"
class TestStreamingYamlBooleanQuirk:
"""YAML 1.1 parses bare ``off``/``on`` as booleans; ``mode``/``transport``
must normalize those back to canonical string tokens.
Regression for the review on PR #62873: bare ``mode: off`` arrived as
Python ``False`` and stringified to ``"false"``, which is not ``"off"``,
so streaming was enabled instead of honoring the advertised disable.
"""
def test_mode_bare_off_boolean_disables(self):
from gateway.config import StreamingConfig
# yaml.safe_load("off") -> False
sc = StreamingConfig.from_dict({"mode": False})
assert sc.enabled is False
assert sc.transport == "off"
def test_mode_bare_on_boolean_enables(self):
from gateway.config import StreamingConfig
# yaml.safe_load("on") -> True
sc = StreamingConfig.from_dict({"mode": True})
assert sc.enabled is True
assert sc.transport == "auto"
def test_loader_normalizes_bare_yaml_off(self, tmp_path):
"""End-to-end through load_gateway_config(): unquoted ``mode: off``
(a YAML boolean) must keep streaming disabled."""
from gateway.config import load_gateway_config
(tmp_path / "config.yaml").write_text("streaming:\n mode: off\n", encoding="utf-8")
with patch("gateway.config.get_hermes_home", return_value=tmp_path):
cfg = load_gateway_config()
assert cfg.streaming.enabled is False
assert cfg.streaming.transport == "off"