Skip to content

The agent that grows with you

fix(gateway): resume pre-upgrade Desktop chats stamped with a Nous login

unsupportedpastels50527b469209
diff --git a/hermes_state_local_migration.py b/hermes_state_local_migration.py
index 16f7be62e8e..769b6446bd7 100644
--- a/hermes_state_local_migration.py
+++ b/hermes_state_local_migration.py
@@ -16,11 +16,19 @@ LEGACY_LOCAL_SOURCES = frozenset({'cli', 'tui', 'gui', 'desktop', 'oneshot', 'to
LEGACY_CREATION_SOURCES = {'desktop': 'gui'}
+def legacy_account_label(row):
+ # main's Desktop stamped the signed-in Nous account on its chats. That is a label, not a route or
+ # an authenticated owner, so it does not block adoption of an otherwise unrouted Desktop row.
+ user_id = row.get('user_id')
+ return row['source'] == 'desktop' and isinstance(user_id, str) and user_id.startswith('nous:')
+
+
def validate_legacy_row(db, row):
# Unrouted old local history only. Existing identity, foreign profile,
# API, messaging, delegate and canonical rows require their own authority.
if (row['source'] not in LEGACY_LOCAL_SOURCES
- or any(row.get(k) for k in ('session_key', 'chat_id', 'user_id', 'origin_json'))
+ or any(row.get(k) for k in ('session_key', 'chat_id', 'origin_json'))
+ or (row.get('user_id') and not legacy_account_label(row))
or row.get('profile_name') not in (None, '', db._own_profile_name() or 'default')):
raise RuntimeStoreError('not_found')
diff --git a/tests/gateway/test_legacy_session_migration.py b/tests/gateway/test_legacy_session_migration.py
index 39b0624ac10..92b2a5cc937 100644
--- a/tests/gateway/test_legacy_session_migration.py
+++ b/tests/gateway/test_legacy_session_migration.py
@@ -170,8 +170,8 @@ async def test_upgraded_oneshot_and_tool_history_resumes_by_id_and_title_but_sta
@pytest.mark.asyncio
async def test_upgraded_desktop_history_resumes_by_id(tmp_path, monkeypatch):
"""main's Desktop stored its chats with ``source='desktop'``. After the upgrade the native owner
- adopts them by id like ``gui`` history. A row that carries an account identity is unchanged:
- it still needs its own authority."""
+ adopts them by id like ``gui`` history, including the ones stamped with the signed-in Nous
+ account. Any other identity still needs its own authority."""
from gateway import run
from gateway.config import GatewayConfig
from gateway.session import SessionStore
@@ -197,6 +197,15 @@ async def test_upgraded_desktop_history_resumes_by_id(tmp_path, monkeypatch):
assert [m['content'] for m in resumed['result']['messages']] == ['OLD-DESKTOP']
from gateway.config import Platform
assert runner.adapters[Platform.LOCAL].policies['old-desktop'].platform == 'desktop'
- refused = await native.dispatch({'id': 2, 'method': 'session.resume', 'params': {'session_id': 'old-desktop-account'}})
- assert 'error' in refused, refused
- assert db.get_session('old-desktop-account')['user_id'] == 'nous:account'
+ stamped = await native.dispatch({'id': 2, 'method': 'session.resume', 'params': {'session_id': 'old-desktop-account'}})
+ assert stamped.get('result', {}).get('session_id') == 'old-desktop-account', stamped
+ assert [m['content'] for m in stamped['result']['messages']] == ['OLD-DESKTOP-ACCOUNT']
+ # Any other identity, or a Nous stamp on a non-Desktop row, still needs its own authority.
+ for sid, source, user_id in (('old-desktop-foreign', 'desktop', 'telegram:42'),
+ ('old-cli-account', 'cli', 'nous:account')):
+ db.create_session(sid, source=source, model='old-model', cwd=str(tmp_path), user_id=user_id)
+ db.append_message(sid, role='user', content=sid.upper())
+ db.end_session(sid, 'session_switch')
+ refused = await native.dispatch({'id': 3, 'method': 'session.resume', 'params': {'session_id': sid}})
+ assert 'error' in refused, refused
+ assert db.get_session(sid)['user_id'] == user_id