Skip to content

The agent that grows with you

fix(gateway): a profile parked for an unusable store names the open failure

Tekniumadbc7cd0b553
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 42e80bf5387..43d9d120919 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -43,7 +43,9 @@ async def _build_profile_authority(runner, name, home, *, register):
# the handle every later scoped read of that profile uses (one writer per state.db).
with _profile_runtime_scope(home, hydrate_secrets=False):
db = getattr(runner._session_db, '_db', runner._session_db)
- if db is None or (await asyncio.to_thread(Path(db.db_path).resolve)).parent != home:
+ if db is None:
+ raise RuntimeError(await asyncio.to_thread(_store_open_failure, home))
+ if (await asyncio.to_thread(Path(db.db_path).resolve)).parent != home:
raise RuntimeError(f'session authority database does not belong to the reserved profile {home}')
registry.add(home, None, name=name)
try:
@@ -56,6 +58,20 @@ async def _build_profile_authority(runner, name, home, *, register):
return authority
+def _store_open_failure(home):
+ """Why *home*'s ``state.db`` has no handle: the scoped handle cache answers None for a failed open
+ (and while it backs off), so the park reason named an ownership mismatch over the real cause, a
+ legacy store that crashed its migration on every open (#141467)."""
+ from hermes_state_registry import acquire, release
+ path = home / 'state.db'
+ try:
+ release(acquire(path))
+ except Exception as exc: # any open failure is the reason; the traceback goes to the log
+ logger.warning("Session store %s did not open", path, exc_info=True)
+ return f'{path} did not open: {type(exc).__name__}: {exc}'
+ return f'{path} is unavailable (its last open failed; retry pending)'
+
+
def _park_reserved_profile(runner, name, home, exc):
"""A secondary whose store is unusable is parked: logged, unreserved, left out of
``served_profiles`` and named under ``parked_profiles`` in the runtime descriptor. Boot
diff --git a/tests/gateway/test_profile_authority_park_reason.py b/tests/gateway/test_profile_authority_park_reason.py
new file mode 100644
index 00000000000..93443ddfa0c
--- /dev/null
+++ b/tests/gateway/test_profile_authority_park_reason.py
@@ -0,0 +1,34 @@
+"""A reserved profile parked for an unusable store names the open failure, not an ownership mismatch.
+
+The runner's scoped handle cache answers None when a profile's ``state.db`` fails to open, and
+``_build_profile_authority`` reported every None as "session authority database does not belong to
+the reserved profile": a legacy store crashing its migration on every open was parked under a
+reason that sent the operator after the wrong problem (#141467).
+"""
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from gateway import run_runtime
+
+
+class _Registry:
+ def add(self, *a, **kw):
+ raise AssertionError('an unusable store must not reach registration')
+
+
+async def test_an_unopenable_store_is_parked_under_its_open_failure(tmp_path, monkeypatch):
+ home = (tmp_path / 'profiles' / 'worker').resolve()
+ home.mkdir(parents=True)
+ (home / 'state.db').mkdir() # sqlite cannot open a directory: a store that fails on every open
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ runner = SimpleNamespace(_session_db=None, session_authorities=_Registry(),
+ session_runtime_descriptor={'instance_id': 'owner'})
+
+ with pytest.raises(RuntimeError) as exc:
+ await run_runtime._build_profile_authority(runner, 'worker', home, register=False)
+ reason = str(exc.value)
+ assert 'does not belong' not in reason
+ assert str(Path(home) / 'state.db') in reason and 'did not open' in reason, reason