Skip to content

The agent that grows with you

fix(acp): adopt pre-upgrade editor threads on their saved route

unsupportedpastels3a5d65866ca8
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index 554f7d2fd4f..0f8ab11b025 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -7,6 +7,7 @@ Compression and reset may later advance the physical target, never FIFO identity
"""
from dataclasses import asdict, replace
from datetime import datetime
+import json
from pathlib import Path
from gateway.config import Platform
@@ -61,6 +62,25 @@ def resolve_local_target(authority, actor, sid):
return adopt_legacy_session(authority, actor, row)
+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."""
+ 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')}
+ 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']
+ params.update({key: route[key] for key in ('provider', 'base_url') if key in route})
+ return params, route.get('api_mode')
+
+
def adopt_legacy_session(authority, actor, row):
if actor.profile_id != authority.profile_id:
raise RuntimeStoreError('profile_mismatch')
@@ -78,13 +98,14 @@ def adopt_legacy_session(authority, actor, row):
request_id = 'legacy:' + row['id']
sid = row['id']
- params = {'source': LEGACY_CREATION_SOURCES.get(row['source'], row['source'])}
- if row.get('cwd'):
- params['cwd'] = row['cwd']
- if row.get('model'):
- params['model'] = row['model']
+ params, api_mode = legacy_launch_params(row)
+ params['source'] = LEGACY_CREATION_SOURCES.get(row['source'], row['source'])
private = {}
policy = build_policy(params, _load_gateway_config(), private_secrets=private)
+ if api_mode:
+ config = json.loads(policy.config_json)
+ config.setdefault('model', {})['api_mode'] = api_mode
+ policy = replace(policy, config_json=json.dumps(config))
if policy.model is None:
policy = replace(policy, model=_resolve_gateway_model(policy.config()))
policy = bind_launch_key(authority, sid, policy, None, config_secrets=private)
diff --git a/tests/gateway/test_legacy_acp_route.py b/tests/gateway/test_legacy_acp_route.py
new file mode 100644
index 00000000000..f84b95b60e9
--- /dev/null
+++ b/tests/gateway/test_legacy_acp_route.py
@@ -0,0 +1,50 @@
+"""A thread main's in-process ACP server wrote resumes on the route it saved (``model_config``).
+
+Main's ``SessionManager._persist`` stored the session's provider, base_url and api_mode in
+``model_config`` (and, on older rows, the workspace too) and ``_restore`` rebuilt the agent from them.
+Adoption under the gateway must keep that route rather than the profile's current endpoint.
+"""
+import json
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.session_contract import Principal
+
+
+async def test_adopted_acp_thread_keeps_its_saved_route_and_workspace(tmp_path, monkeypatch):
+ 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': 'openrouter',
+ 'base_url': 'https://openrouter.ai/api/v1', 'api_mode': 'chat_completions'},
+ 'platform_toolsets': {'acp': []}})
+ 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'})
+ workspace = tmp_path / 'workspace'
+ workspace.mkdir()
+ # An older row: the workspace only in model_config (the cwd column was added later).
+ authority.db.create_session('old-editor-thread', source='acp', model='saved-model', model_config={
+ 'cwd': str(workspace), 'provider': 'custom', 'base_url': 'http://127.0.0.1:8080/v1',
+ 'api_mode': 'anthropic_messages'})
+ try:
+ resolve_local_target(authority, actor, 'old-editor-thread')
+ policy = local_receipt(authority.db, 'old-editor-thread')['policy']
+ route = json.loads(policy['config_json'])['model']
+ assert policy['source'] == 'acp' and policy['model'] == 'saved-model'
+ assert policy['cwd'] == str(workspace)
+ assert (route['provider'], route['base_url'], route['api_mode']) == (
+ 'custom', 'http://127.0.0.1:8080/v1', 'anthropic_messages'), route
+ finally:
+ store._db.close()