Skip to content

The agent that grows with you

refactor(tui_gateway): follow the repo's _AT_IMPORT resolve pattern

anhtahaylove30b143cff3cb
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index aa41ddcf411..eebae0cdf00 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -23,7 +23,7 @@ from typing import Any, Callable, NamedTuple, Optional # noqa: F401 (Callable:
# namespace (method_ctx.bind_module) — deleting one breaks a handler at call time, not import time.
from agent.secret_scope import build_profile_secret_scope, reset_secret_scope, set_secret_scope # noqa: F401
from hermes_constants import (
- get_hermes_home, get_hermes_home_override, profile_name_for_home,
+ get_hermes_home, get_hermes_home_override, get_process_hermes_home, profile_name_for_home,
reset_hermes_home_override, set_hermes_home_override)
from hermes_cli.env_loader import load_hermes_dotenv
from utils import file_signature, is_truthy_value
@@ -46,7 +46,7 @@ from tui_gateway.transport import (FanoutTransport, StdioTransport, Transport, b
logger = logging.getLogger(__name__)
-_hermes_home = get_hermes_home()
+_hermes_home = _HERMES_HOME_AT_IMPORT = get_hermes_home()
load_hermes_dotenv(hermes_home=_hermes_home, project_env=Path(__file__).parent.parent / ".env")
@@ -377,13 +377,14 @@ _start_idle_reaper()
def _launch_state_db_path() -> Path:
- """``state.db`` under the launch home, resolved at first use.
-
- Reads ``HERMES_HOME`` (or the platform default) directly and deliberately skips the
- context-local override that ``get_hermes_home()`` honours — see ``_get_db``.
- """
- env_home = os.environ.get("HERMES_HOME", "").strip()
- return Path(env_home if env_home else _hermes_home) / "state.db"
+ """Launch profile's ``state.db`` at call time: the patched ``_hermes_home`` when a test changed
+ it, else the live process home — resolved through :func:`get_process_hermes_home`, which honours
+ ``HERMES_HOME`` but ignores the context-local override. The desktop multiplex cron ticker sets
+ that override per profile at startup, and a first touch inside a foreign window would bind this
+ process-wide handle to another profile's ``state.db`` (#102526). Resolving here rather than at
+ import time lets a harness that redirects ``HERMES_HOME`` after import be honoured (#112692)."""
+ home = _hermes_home if _hermes_home != _HERMES_HOME_AT_IMPORT else get_process_hermes_home()
+ return Path(home) / "state.db"
def _get_db():
@@ -391,12 +392,8 @@ def _get_db():
if _db is None:
from hermes_state_registry import acquire
try:
- # Pin to the LAUNCH home, ignoring the context-local override (#102526): the
- # desktop multiplex cron ticker overrides it per profile at startup, and a first
- # touch inside a foreign window would permanently bind this process-wide handle
- # to the wrong state.db. Resolve at first use rather than import time so a test
- # harness that redirects HERMES_HOME after import is honoured (#112692) — the
- # override, not the env var, is what #102526 guards against.
+ # Launch home, never the context-local override (#102526); resolved at first
+ # use, not import time (#112692). See _launch_state_db_path.
_db, _db_error = acquire(_launch_state_db_path()), None
except Exception as exc:
_db_error = str(exc)