Skip to content

The agent that grows with you

fix(cron): preserve profile context during session DB init

Kevindfa5004a0408
diff --git a/cron/scheduler.py b/cron/scheduler.py
index c46037a9628..429bf6286f5 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -6406,7 +6406,18 @@ def run_job(
if _session_db_timeout > 0:
_session_db_pool = concurrent.futures.ThreadPoolExecutor(max_workers=1)
- _session_db_future = _session_db_pool.submit(SessionDB)
+ # The timeout worker is a second thread, so it does not inherit
+ # the multiplexed profile ContextVar automatically. Capture the
+ # active context before submitting SessionDB and pass the
+ # resolved database path explicitly so a profile cron run
+ # cannot fall back to the process-global default state.db.
+ _session_db_context = contextvars.copy_context()
+ _session_db_path = _get_hermes_home() / "state.db"
+ _session_db_future = _session_db_pool.submit(
+ _session_db_context.run,
+ SessionDB,
+ db_path=_session_db_path,
+ )
try:
_session_db = _session_db_future.result(timeout=_session_db_timeout)
except concurrent.futures.TimeoutError:
diff --git a/tests/cron/test_sessiondb_init_hang.py b/tests/cron/test_sessiondb_init_hang.py
index 4402600ec7c..3130e3e0ae0 100644
--- a/tests/cron/test_sessiondb_init_hang.py
+++ b/tests/cron/test_sessiondb_init_hang.py
@@ -75,6 +75,55 @@ def _session_db_executor(timeouts: list, *, instant_timeout: bool = True):
class TestSessionDbInitTimeout:
+ def test_sessiondb_init_preserves_multiplex_profile_context(
+ self, tmp_path, monkeypatch
+ ):
+ """The timeout worker must construct SessionDB under the active profile."""
+ from hermes_constants import (
+ get_hermes_home,
+ reset_hermes_home_override,
+ set_hermes_home_override,
+ )
+
+ default_home = tmp_path / "default"
+ profile_home = tmp_path / "profiles" / "jobsearch"
+ monkeypatch.setenv("HERMES_HOME", str(default_home))
+ observed_homes = []
+ observed_paths = []
+ fake_db = MagicMock()
+
+ def make_session_db(*, db_path=None):
+ observed_homes.append(get_hermes_home())
+ observed_paths.append(db_path)
+ return fake_db
+
+ job = {"id": "profile-sessiondb", "name": "test", "prompt": "hello"}
+ profile_token = set_hermes_home_override(profile_home)
+ try:
+ with patch("cron.scheduler._hermes_home", None), \
+ patch("cron.scheduler._resolve_origin", return_value=None), \
+ patch("hermes_cli.env_loader.load_hermes_dotenv"), \
+ patch("hermes_cli.env_loader.reset_secret_source_cache"), \
+ patch("hermes_state.SessionDB", side_effect=make_session_db), \
+ patch(
+ "hermes_cli.runtime_provider.resolve_runtime_provider",
+ return_value=_RUNTIME,
+ ), \
+ patch("run_agent.AIAgent") as mock_agent_cls:
+ mock_agent = MagicMock()
+ mock_agent.run_conversation.return_value = {"final_response": "ok"}
+ mock_agent_cls.return_value = mock_agent
+
+ success, _output, final_response, error = run_job(job)
+ finally:
+ reset_hermes_home_override(profile_token)
+
+ assert success is True
+ assert error is None
+ assert final_response == "ok"
+ assert observed_homes == [profile_home]
+ assert observed_paths == [profile_home / "state.db"]
+
def test_run_job_does_not_hang_when_sessiondb_init_wedges(self, tmp_path, monkeypatch):
"""run_job proceeds without a session store when SessionDB init times out."""
monkeypatch.setenv("HERMES_CRON_SESSION_DB_TIMEOUT", "0.2")