Skip to content

The agent that grows with you

fix(gateway): restore legacy local session routes

KoNit-K7adc0e59c222
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index 0f8ab11b025..13434faa7e1 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -63,21 +63,42 @@ def resolve_local_target(authority, actor, sid):
def legacy_launch_params(row):
- """``(create params, api_mode)`` a legacy row resumes on: its cwd and model and, for main's
- in-process ACP server, the route it saved in ``model_config`` and rebuilt the agent from
- (``SessionManager._persist`` / ``_restore``, billing columns as its fallback). Older ACP rows
- hold the workspace only there. ``api_mode`` is no create field, so it is returned apart."""
+ """``(create params, api_mode)`` a legacy row resumes on.
+
+ The old local surfaces persisted their effective route and runtime choices in
+ ``model_config``. Preserve those choices while adopting them, rather than
+ combining their model with a later profile default. Older ACP rows also hold
+ their workspace only there. ``api_mode`` is no create field, so it is
+ returned apart.
+ """
params = {key: row[key] for key in ('cwd', 'model') if row.get(key)}
- if row['source'] != 'acp':
- return params, None
from hermes_state_sessions import _parse_model_config
saved = _parse_model_config(row.get('model_config'))
- route = {'cwd': saved.get('cwd'), 'provider': saved.get('provider') or row.get('billing_provider'),
- 'base_url': saved.get('base_url') or row.get('billing_base_url'), 'api_mode': saved.get('api_mode')}
+ from hermes_state_gateway import SessionGatewayMixin
+ route = SessionGatewayMixin.session_gateway_runtime(row)
route = {key: value.strip() for key, value in route.items() if isinstance(value, str) and value.strip()}
- if 'cwd' not in params and Path(route.get('cwd', '')).is_absolute():
- params['cwd'] = route['cwd']
+ billing_base_url = row.get('billing_base_url')
+ if 'base_url' not in route and isinstance(billing_base_url, str) and billing_base_url.strip():
+ route['base_url'] = billing_base_url.strip()
+ if row['source'] == 'acp' and 'cwd' not in params and Path(str(saved.get('cwd') or '')).is_absolute():
+ params['cwd'] = saved['cwd']
params.update({key: route[key] for key in ('provider', 'base_url') if key in route})
+ reasoning = saved.get('reasoning_config')
+ if isinstance(reasoning, dict):
+ reasoning = 'none' if reasoning.get('enabled') is False else reasoning.get('effort')
+ if isinstance(reasoning, str):
+ from hermes_constants import parse_reasoning_effort
+ if parse_reasoning_effort(reasoning) is not None:
+ params['reasoning'] = reasoning
+ tier = saved.get('service_tier')
+ if isinstance(tier, str):
+ from agent.fast_mode import parse_exact_service_tier
+ try:
+ parse_exact_service_tier(tier)
+ except ValueError:
+ pass
+ else:
+ params['service_tier'] = tier
return params, route.get('api_mode')
diff --git a/tests/gateway/test_legacy_acp_route.py b/tests/gateway/test_legacy_acp_route.py
index f84b95b60e9..d1736ff5765 100644
--- a/tests/gateway/test_legacy_acp_route.py
+++ b/tests/gateway/test_legacy_acp_route.py
@@ -48,3 +48,43 @@ async def test_adopted_acp_thread_keeps_its_saved_route_and_workspace(tmp_path,
'custom', 'http://127.0.0.1:8080/v1', 'anthropic_messages'), route
finally:
store._db.close()
+
+
+async def test_adopted_legacy_tui_session_keeps_saved_route_and_runtime_settings(tmp_path, monkeypatch):
+ """Regression for #138912: pre-authority TUI rows must not inherit a new profile route."""
+ 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_local_migration import bind_native_transport, resolve_local_target
+ from hermes_state_local import local_receipt
+
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: {
+ 'model': {'default': 'profile-model', 'provider': 'gemini',
+ 'base_url': 'https://generativelanguage.googleapis.com/v1beta'},
+ 'agent': {'reasoning_effort': 'low', 'service_tier': 'normal'},
+ 'platform_toolsets': {'tui': []}})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False,
+ _cached_agent_for=lambda route: None, _adapter_for_source=lambda source: None)
+ authority = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='fixture')
+ actor = Principal('uid:1000', str(tmp_path), frozenset({'session:read', 'session:create'}), 'native')
+ bind_native_transport(authority, actor, {'provider': 'local', 'profile_id': str(tmp_path),
+ 'instance_id': 'fixture'})
+ authority.db.create_session('old-tui-session', source='tui', model='gpt-6.1-sol-900k', model_config={
+ 'provider': 'openai-codex', 'base_url': 'https://chatgpt.com/backend-api/codex',
+ 'api_mode': 'codex_responses', 'reasoning_config': {'enabled': True, 'effort': 'xhigh'},
+ 'service_tier': 'priority'})
+ try:
+ resolve_local_target(authority, actor, 'old-tui-session')
+ policy = local_receipt(authority.db, 'old-tui-session')['policy']
+ config = json.loads(policy['config_json'])
+ assert policy['source'] == 'tui' and policy['model'] == 'gpt-6.1-sol-900k'
+ assert config['model'] == {
+ 'default': 'profile-model', 'provider': 'openai-codex',
+ 'base_url': 'https://chatgpt.com/backend-api/codex', 'api_mode': 'codex_responses'}
+ assert config['agent']['reasoning_effort'] == 'xhigh'
+ assert config['agent']['service_tier'] == 'priority'
+ finally:
+ store._db.close()