Skip to content

The agent that grows with you

fix(gateway): resolve the mirror sessions index at call time, not import time

anhtahaylovef27c719a93cc
diff --git a/gateway/mirror.py b/gateway/mirror.py
index db3fbdf23d3..513bf882dae 100644
--- a/gateway/mirror.py
+++ b/gateway/mirror.py
@@ -8,6 +8,7 @@ agent knows what was sent. Standalone: works from CLI, cron and gateway context
import json
import logging
from datetime import datetime
+from pathlib import Path
from typing import Optional
from hermes_cli.config import get_hermes_home
@@ -16,6 +17,16 @@ logger = logging.getLogger(__name__)
_SESSIONS_DIR = get_hermes_home() / "sessions"
_SESSIONS_INDEX = _SESSIONS_DIR / "sessions.json"
+_SESSIONS_INDEX_AT_IMPORT = _SESSIONS_INDEX
+
+
+def _resolve_sessions_index() -> Path:
+ """Active profile's ``sessions.json`` at call time: the patched ``_SESSIONS_INDEX`` when a test
+ changed it, else live profile-scoped HERMES_HOME — under the multiplexed gateway one process
+ serves every profile, so the import-time constant would resolve every profile's pre-migration
+ session lookup against the launch profile's index."""
+ return (_SESSIONS_INDEX if _SESSIONS_INDEX != _SESSIONS_INDEX_AT_IMPORT
+ else get_hermes_home() / "sessions" / "sessions.json")
def _origin_user_id(entry: dict) -> str:
@@ -86,10 +97,11 @@ def _find_session_id(platform: str, chat_id: str, thread_id: Optional[str] = Non
except Exception as e:
logger.debug("Mirror state.db session lookup failed: %s", e)
- if not _SESSIONS_INDEX.exists():
+ sessions_index = _resolve_sessions_index()
+ if not sessions_index.exists():
return None
try:
- data = json.loads(_SESSIONS_INDEX.read_text(encoding="utf-8"))
+ data = json.loads(sessions_index.read_text(encoding="utf-8"))
except Exception:
return None
diff --git a/tests/gateway/test_mirror.py b/tests/gateway/test_mirror.py
index fb76c466189..63f4e64021c 100644
--- a/tests/gateway/test_mirror.py
+++ b/tests/gateway/test_mirror.py
@@ -1,5 +1,6 @@
"""Tests for gateway/mirror.py — session mirroring."""
+import importlib
import json
from unittest.mock import patch, MagicMock
@@ -160,3 +161,50 @@ class TestAppendToSqlite:
"the shared handle must be released exactly once after use"
)
+
+class TestSessionsIndexProfileScoping:
+ """#112844: the fallback index must follow the active profile, not the launch one."""
+
+ @staticmethod
+ def _write_index(home, session_id):
+ d = home / "sessions"
+ d.mkdir(parents=True, exist_ok=True)
+ (d / "sessions.json").write_text(json.dumps({
+ "agent:main:telegram:dm": {
+ "session_id": session_id,
+ "origin": {"platform": "telegram", "chat_id": "12345"},
+ "updated_at": "2026-01-01T00:00:00",
+ }
+ }), encoding="utf-8")
+
+ def test_fallback_follows_active_profile_home(self, tmp_path, monkeypatch):
+ """A profile switched in after import must be read, not the launch profile's index.
+
+ The module captures ``sessions.json`` under the home that was live at import. Under the
+ multiplexed gateway one process serves every profile, so a lookup for another profile
+ must not resolve against the launch profile's index and return its session id.
+ """
+ launch_home, active_home = tmp_path / "launch", tmp_path / "active"
+ self._write_index(launch_home, "sess_launch")
+ self._write_index(active_home, "sess_active")
+
+ # Re-import the module with the launch home live: this is the import-time capture.
+ monkeypatch.setenv("HERMES_HOME", str(launch_home))
+ importlib.reload(mirror_mod)
+ try:
+ # A request for a different profile is now served by the same process.
+ monkeypatch.setenv("HERMES_HOME", str(active_home))
+ assert mirror_mod._find_session_id("telegram", "12345") == "sess_active"
+ finally:
+ monkeypatch.undo()
+ importlib.reload(mirror_mod)
+
+ def test_patched_constant_still_wins(self, tmp_path, monkeypatch):
+ """Tests that patch ``_SESSIONS_INDEX`` keep overriding the live home."""
+ patched = tmp_path / "patched"
+ self._write_index(patched, "sess_patched")
+
+ monkeypatch.setattr(mirror_mod, "_SESSIONS_INDEX", patched / "sessions" / "sessions.json")
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "elsewhere"))
+
+ assert mirror_mod._find_session_id("telegram", "12345") == "sess_patched"