Skip to content

The agent that grows with you

fix(gateway): the owner backfills legacy session owners at authority bring-up

unsupportedpastels307774683aa6
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 003b4b7308b..52d86839740 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -1024,6 +1024,28 @@ class SessionAuthority:
await asyncio.wait_for(released.wait(), _MARKER_RELEASE_WAIT_S)
+def backfill_legacy_session_owners(db):
+ """Stamp this store's own profile onto pre-ownership NULL rows (#94724).
+
+ The owner is the only writer while it holds the profile, so the offline REST backfill is
+ refused for as long as it runs; doing it here is what migrates an upgraded install. Idempotent
+ and never overwrites an owner, so every start runs it. A store outside the profile tree has
+ no derivable owner and keeps NULL.
+ """
+ import logging
+ stamp = db._own_profile_name()
+ if not stamp:
+ return 0
+ try:
+ stamped = db.backfill_null_session_profiles(stamp)
+ except Exception:
+ logging.getLogger(__name__).warning('Legacy session owner backfill failed for %s', db.db_path, exc_info=True)
+ return 0
+ if stamped:
+ logging.getLogger(__name__).info('Stamped %d legacy session row(s) with owner profile %r', stamped, stamp)
+ return stamped
+
+
async def initialize_session_authority(runner, *, profile_id, instance_id, db=None, register=True):
"""Call after exclusive profile ownership, before connecting adapters/API.
@@ -1039,6 +1061,7 @@ async def initialize_session_authority(runner, *, profile_id, instance_id, db=No
epoch = await asyncio.to_thread(begin_runtime_epoch, db, instance_id=instance_id)
# Hot-serve builds a profile while the loop serves the others: both writes stay off it.
await asyncio.to_thread(recover_session_inputs, db, epoch=epoch)
+ await asyncio.to_thread(backfill_legacy_session_owners, db)
authority = SessionAuthority(runner, profile_id=profile_id, instance_id=instance_id, db=db, epoch=epoch)
if register:
runner.session_authority = authority
diff --git a/tests/gateway/test_legacy_session_owner_backfill.py b/tests/gateway/test_legacy_session_owner_backfill.py
new file mode 100644
index 00000000000..54852ef2be9
--- /dev/null
+++ b/tests/gateway/test_legacy_session_owner_backfill.py
@@ -0,0 +1,49 @@
+"""The owner migrates pre-ownership NULL session rows itself (#94724).
+
+While a gateway owns a profile the offline REST backfill is refused, so authority bring-up is
+the one place an upgraded install's legacy rows get their owner.
+"""
+from hermes_state import SessionDB
+
+from gateway.session_authority import backfill_legacy_session_owners
+
+
+def _legacy(db, session_id, profile_name=None):
+ db.create_session(session_id, source='cli')
+ db._conn.execute('UPDATE sessions SET profile_name = ? WHERE id = ?', (profile_name, session_id))
+ db._conn.commit()
+
+
+def _owners(db):
+ return dict(db._conn.execute('SELECT id, profile_name FROM sessions ORDER BY id').fetchall())
+
+
+def test_default_store_stamps_only_unowned_rows(_isolate_hermes_home):
+ from hermes_constants import get_default_hermes_root
+ with SessionDB(db_path=get_default_hermes_root() / 'state.db') as db:
+ _legacy(db, 'null-row')
+ _legacy(db, 'blank-row', ' ')
+ _legacy(db, 'owned-row', 'work')
+
+ assert backfill_legacy_session_owners(db) == 2
+ assert _owners(db) == {'blank-row': 'default', 'null-row': 'default', 'owned-row': 'work'}
+ assert backfill_legacy_session_owners(db) == 0
+
+
+def test_named_profile_store_stamps_its_own_name(_isolate_hermes_home):
+ from hermes_constants import get_default_hermes_root
+ home = get_default_hermes_root() / 'profiles' / 'work'
+ home.mkdir(parents=True)
+ with SessionDB(db_path=home / 'state.db') as db:
+ _legacy(db, 'null-row')
+
+ assert backfill_legacy_session_owners(db) == 1
+ assert _owners(db) == {'null-row': 'work'}
+
+
+def test_store_outside_the_profile_tree_keeps_null(tmp_path, _isolate_hermes_home):
+ with SessionDB(db_path=tmp_path / 'elsewhere' / 'state.db') as db:
+ _legacy(db, 'null-row')
+
+ assert backfill_legacy_session_owners(db) == 0
+ assert _owners(db) == {'null-row': None}