Skip to content

The agent that grows with you

fix(gateway): resume pre-upgrade chat -q / --source tool sessions by id and title

Teknium571abb58e80a
diff --git a/gateway/session_a2a.py b/gateway/session_a2a.py
index 651660e9c98..26a6a9bdd55 100644
--- a/gateway/session_a2a.py
+++ b/gateway/session_a2a.py
@@ -49,8 +49,14 @@ def forward_author(policy):
def storage_source(db, source, session_id, fallback):
"""Retain the owner's creation label when its LOCAL transport refreshes peer metadata."""
from gateway.config import Platform
- if source.platform != Platform.LOCAL or not str(source.chat_id).startswith('local-'):
+ if source.platform != Platform.LOCAL:
return fallback
+ if not str(source.chat_id).startswith('local-'):
+ # An adopted pre-authority row keeps its historical id as the route (session_local_migration).
+ from hermes_state_local_migration import LEGACY_PREFIX
+ with db._read_ctx() as conn:
+ if conn.execute('SELECT 1 FROM state_meta WHERE key=?', (LEGACY_PREFIX + str(source.chat_id),)).fetchone() is None:
+ return fallback
from hermes_state_local import local_receipt
receipt = local_receipt(db, source.chat_id)
if session_id not in receipt.get('lineage', [receipt['session_id']]):
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index f70c2d15720..ffee7b01830 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -351,8 +351,9 @@ class AuthorityConnection:
ref = resolve_titled_session(self.authority, self.actor, params['title'])
if 'title' not in params:
from gateway.session_local_migration import resolve_local_target
+ from hermes_state_local_migration import LEGACY_LOCAL_SOURCES
row = self.authority.db.get_session(ref.session_id)
- if row and row['source'] in {'cli', 'tui', 'gui'}:
+ if row and row['source'] in LEGACY_LOCAL_SOURCES:
ref = resolve_local_target(self.authority, self.actor,
self.authority.db.get_compression_tip(ref.session_id) or ref.session_id)
if params.get('editor') is not None:
diff --git a/hermes_state_local_migration.py b/hermes_state_local_migration.py
index c92bbe25f4a..4371d75ab86 100644
--- a/hermes_state_local_migration.py
+++ b/hermes_state_local_migration.py
@@ -7,10 +7,16 @@ from hermes_state_runtime import RuntimeStoreError, _json
LEGACY_PREFIX = 'gateway.local_legacy.v1:'
+# Sources the pre-authority local CLI/TUI/GUI wrote. ``oneshot`` (finite ``chat -q`` / ``-z``) and ``tool``
+# (``chat --source tool``) are CLI runs that printed ``hermes --resume <id>``: adopted for resume like
+# ``cli`` and keeping their label, so human pickers still hide them (INTERNAL_LISTING_SOURCES).
+LEGACY_LOCAL_SOURCES = frozenset({'cli', 'tui', 'gui', 'oneshot', 'tool'})
+
+
def validate_legacy_row(db, row):
- # Unrouted old CLI/TUI/GUI history only. Existing identity, foreign profile,
+ # Unrouted old local history only. Existing identity, foreign profile,
# API, messaging, delegate and canonical rows require their own authority.
- if (row['source'] not in {'cli', 'tui', 'gui'}
+ 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 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 4155695f1ea..0a210d714b7 100644
--- a/tests/gateway/test_legacy_session_migration.py
+++ b/tests/gateway/test_legacy_session_migration.py
@@ -126,3 +126,42 @@ async def test_legacy_adoption_refuses_live_owner_and_foreign_profile_without_wr
from gateway.session_bot import _target
submitter = Principal(native.subject, native.profile_id, native.capabilities | {'session:submit'}, native.transport_id)
assert _target(authority, submitter)[2].session_id == 'reset-tip'
+
+
+async def test_upgraded_oneshot_and_tool_history_resumes_by_id_and_title_but_stays_out_of_pickers(tmp_path, monkeypatch):
+ """main's `chat -q` stored ``oneshot`` rows (``--source tool`` stored ``tool``) and printed
+ `hermes --resume <id>` for each. After the upgrade, the native owner adopts them by id and by
+ title like ``cli`` history, and they keep their label so human pickers still hide them."""
+ from gateway import run
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_controls import AuthorityConnection
+
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: {'platform_toolsets': {'cli': []}})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ db = store._db
+ for sid, source in (('old-oneshot', 'oneshot'), ('old-tool', 'tool')):
+ db.create_session(sid, source=source, model='old-model', cwd=str(tmp_path))
+ db.set_session_title(sid, sid + ' title')
+ db.append_message(sid, role='user', content=sid.upper())
+ db.end_session(sid, 'cli_exit')
+ runner = SimpleNamespace(session_store=store, _session_db=db, adapters={}, _draining=False,
+ _cached_agent_for=lambda route: None)
+ runner._adapter_for_source = lambda source: runner.adapters.get(source.platform)
+ authority = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='first')
+ native = AuthorityConnection(authority, object(), {'provider': 'local', 'user_id': 'native',
+ 'profile_id': str(tmp_path), 'instance_id': 'first'})
+ by_id = await native.dispatch({'id': 1, 'method': 'session.resume', 'params': {'session_id': 'old-oneshot'}})
+ assert by_id.get('result', {}).get('session_id') == 'old-oneshot', by_id
+ assert [m['content'] for m in by_id['result']['messages']] == ['OLD-ONESHOT']
+ by_title = await native.dispatch({'id': 2, 'method': 'session.resume', 'params': {'title': 'old-tool title'}})
+ assert by_title['result']['session_id'] == 'old-tool', by_title
+ # Every turn refreshes the routing peer through the transport (``local``); the adopted label survives it.
+ source = authority.sessions['old-oneshot'].source
+ store._record_gateway_session_peer('old-oneshot', authority.sessions['old-oneshot'].route, source)
+ assert {db.get_session(s)['source'] for s in ('old-oneshot', 'old-tool')} == {'oneshot', 'tool'}
+ listed = await native.dispatch({'id': 3, 'method': 'session.list', 'params': {}})
+ assert listed['result']['sessions'] == [], listed