mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
141 lines
5 KiB
Python
141 lines
5 KiB
Python
"""Phase 0 foundations for multi-profile gateway multiplexing.
|
|
|
|
Covers the three Phase 0 deliverables:
|
|
1. ``gateway.multiplex_profiles`` config flag (default False, round-trips).
|
|
2. ``hermes_cli.profiles.profiles_to_serve`` enumeration.
|
|
3. Profile-stamped ``build_session_key`` that is BYTE-IDENTICAL when the
|
|
flag is off (the orphan-every-session guard) and namespace-segmented when
|
|
on, without disturbing the positional key layout downstream parsers rely
|
|
on.
|
|
"""
|
|
import pytest
|
|
from datetime import datetime
|
|
from unittest.mock import patch
|
|
import yaml
|
|
|
|
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
|
|
from gateway.config import GatewayConfig, Platform
|
|
from gateway.session import SessionSource, SessionStore, build_session_key
|
|
|
|
|
|
def _src(**kw) -> SessionSource:
|
|
kw.setdefault("platform", Platform.TELEGRAM)
|
|
kw.setdefault("chat_id", "99")
|
|
kw.setdefault("chat_type", "dm")
|
|
return SessionSource(**kw)
|
|
|
|
|
|
class TestSessionKeyByteIdenticalWhenOff:
|
|
"""The non-negotiable guard: with no profile (or 'default'), every key is
|
|
byte-for-byte what it was before Phase 0. A diff here orphans every
|
|
existing session on upgrade."""
|
|
|
|
@pytest.mark.parametrize("profile", [None, "default"])
|
|
def test_dm_with_chat_id(self, profile):
|
|
s = _src(chat_id="99", chat_type="dm")
|
|
assert build_session_key(s, profile=profile) == "agent:main:telegram:dm:99"
|
|
|
|
|
|
@pytest.mark.parametrize("profile", [None, "default"])
|
|
def test_group_per_user(self, profile):
|
|
s = _src(platform=Platform.DISCORD, chat_id="g1", chat_type="group", user_id="alice")
|
|
assert (
|
|
build_session_key(s, profile=profile)
|
|
== "agent:main:discord:group:g1:alice"
|
|
)
|
|
|
|
|
|
class TestSessionKeyNamespacedWhenOn:
|
|
"""A named profile occupies the namespace slot, isolating its sessions."""
|
|
|
|
|
|
def test_named_profile_group_per_user(self):
|
|
s = _src(platform=Platform.DISCORD, chat_id="g1", chat_type="group", user_id="alice")
|
|
assert (
|
|
build_session_key(s, profile="coder")
|
|
== "agent:coder:discord:group:g1:alice"
|
|
)
|
|
|
|
def test_two_profiles_same_chat_do_not_collide(self):
|
|
s = _src(chat_id="99", chat_type="dm")
|
|
a = build_session_key(s, profile="default")
|
|
b = build_session_key(s, profile="coder")
|
|
c = build_session_key(s, profile="writer")
|
|
assert a != b != c and a != c
|
|
|
|
|
|
class TestMultiplexConfigFlag:
|
|
"""gateway.multiplex_profiles defaults off and round-trips."""
|
|
|
|
def test_default_is_false(self):
|
|
assert GatewayConfig().multiplex_profiles is False
|
|
|
|
|
|
def test_from_dict_top_level(self):
|
|
cfg = GatewayConfig.from_dict({"multiplex_profiles": True})
|
|
assert cfg.multiplex_profiles is True
|
|
|
|
|
|
class TestSessionStoreProfileResolution:
|
|
"""SessionStore._generate_session_key honors the flag: legacy namespace
|
|
when off, active-profile namespace when on."""
|
|
|
|
def _store(self, tmp_path, **cfg_kw):
|
|
config = GatewayConfig(**cfg_kw)
|
|
with patch("gateway.session.SessionStore._ensure_loaded"):
|
|
s = SessionStore(sessions_dir=tmp_path, config=config)
|
|
s._db = None
|
|
s._loaded = True
|
|
return s
|
|
|
|
def test_flag_off_uses_legacy_namespace(self, tmp_path):
|
|
store = self._store(tmp_path) # multiplex_profiles defaults False
|
|
s = _src(chat_id="99", chat_type="dm")
|
|
assert store._generate_session_key(s) == "agent:main:telegram:dm:99"
|
|
assert store._generate_session_key(s) == build_session_key(s)
|
|
|
|
|
|
class _RecoveringDB:
|
|
def __init__(self, row):
|
|
self.row = row
|
|
self.reopened = []
|
|
|
|
def find_latest_gateway_session_for_peer(self, **_kwargs):
|
|
return self.row
|
|
|
|
def reopen_session(self, session_id):
|
|
self.reopened.append(session_id)
|
|
|
|
|
|
class TestSessionStoreUnmultiplexedRecovery:
|
|
"""Turning multiplexing off must not recover another profile's session."""
|
|
|
|
def _store_with_row(self, tmp_path, row, **cfg_kw):
|
|
config = GatewayConfig(**cfg_kw)
|
|
with patch("gateway.session.SessionStore._ensure_loaded"):
|
|
store = SessionStore(sessions_dir=tmp_path, config=config)
|
|
store._db = _RecoveringDB(row)
|
|
store._loaded = True
|
|
return store
|
|
|
|
|
|
def test_flag_off_allows_active_profile_peer_fallback(self, tmp_path):
|
|
row = {
|
|
"id": "sess-coder",
|
|
"started_at": 1700000000,
|
|
"session_key": "agent:coder:telegram:dm:99",
|
|
}
|
|
store = self._store_with_row(tmp_path, row)
|
|
source = _src(chat_id="99", chat_type="dm")
|
|
|
|
with patch("hermes_cli.profiles.get_active_profile_name", return_value="coder"):
|
|
recovered = store._recover_session_from_db(
|
|
session_key="agent:main:telegram:dm:99",
|
|
source=source,
|
|
now=datetime.fromtimestamp(1700000001),
|
|
)
|
|
|
|
assert recovered is not None
|
|
assert recovered.session_id == "sess-coder"
|
|
assert recovered.session_key == "agent:main:telegram:dm:99"
|
|
assert store._db.reopened == ["sess-coder"]
|