Skip to content

The agent that grows with you

fix(gateway): a pre-upgrade session whose custom endpoint moved resumes on the profile route

Teknium2048ba4918b7
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index 87c1e2844b8..b1491f027f1 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -111,14 +111,24 @@ def legacy_launch_params(row):
return params, route.get('api_mode')
-def _endpoint_owner(saved, config):
- """The provider whose credential may serve the saved endpoint. A named entry that now points
- elsewhere owns a credential for THAT host: rebind to the entry still serving the saved URL, or
- refuse by name rather than send the moved entry's key to the old host."""
+def _route_is_usage_stamp(row):
+ """True when the row's only route record is the ``billing_provider='custom'`` + ``billing_base_url``
+ usage stamp: no ``gateway_runtime`` and no top-level ``provider``/``base_url`` in ``model_config``.
+ That stamp records which endpoint billed the last turn (the profile's route at the time), not a
+ route the user picked for this session."""
+ from hermes_state_sessions import _parse_model_config
+ saved = _parse_model_config(row.get('model_config'))
+ runtime = saved.get('gateway_runtime')
+ if isinstance(runtime, dict) and runtime.get('provider'):
+ return False
+ if any(isinstance(saved.get(k), str) and saved[k].strip() for k in ('provider', 'base_url')):
+ return False
+ return str(row.get('billing_provider') or '').strip().lower() == 'custom'
+
+
+def _configured_owner(saved, config):
+ """The configured named entry (``providers:``/``custom_providers:``) serving the saved URL, or None."""
from gateway.session_route_identity import resolved_route, same_route
- identity, entry_url = resolved_route({'provider': saved['provider']}, config)
- if not entry_url or same_route(saved, {'provider': identity, 'base_url': entry_url}, config):
- return saved['provider']
names = list(config.get('providers') or {}) if isinstance(config.get('providers'), dict) else []
legacy = config.get('custom_providers')
names += [e.get('name') for e in legacy if isinstance(e, dict)] if isinstance(legacy, list) else []
@@ -126,6 +136,20 @@ def _endpoint_owner(saved, config):
owner, url = resolved_route({'provider': str(name or '')}, config)
if url and same_route(saved, {'provider': owner, 'base_url': url}, config):
return owner
+ return None
+
+
+def _endpoint_owner(saved, config):
+ """The provider whose credential may serve the saved endpoint. A named entry that now points
+ elsewhere owns a credential for THAT host: rebind to the entry still serving the saved URL, or
+ refuse by name rather than send the moved entry's key to the old host."""
+ from gateway.session_route_identity import resolved_route, same_route
+ identity, entry_url = resolved_route({'provider': saved['provider']}, config)
+ if not entry_url or same_route(saved, {'provider': identity, 'base_url': entry_url}, config):
+ return saved['provider']
+ owner = _configured_owner(saved, config)
+ if owner:
+ return owner
raise RuntimeStoreError('launch_credentials_unavailable', field='provider',
detail=f'{identity} no longer serves the saved endpoint; select a model to continue')
@@ -162,6 +186,14 @@ def adopt_legacy_session(authority, actor, row):
# credential (inline key, ${ENV} key, key_env) keeps its normal precedence.
params.pop('provider', None)
params.pop('base_url', None)
+ elif (_route_is_usage_stamp(row) and saved['base_url']
+ and resolved_route(saved, config)[0] == 'custom' and not _configured_owner(saved, config)):
+ # T21/F1: the only record is the usage stamp of a bare custom endpoint that no configured
+ # entry serves any more (the profile's server moved). Pre-authority resume healed that stamp
+ # to nothing and used the profile route; do the same rather than freeze a dead endpoint into
+ # the policy, where every turn retries it until the cycle gives up.
+ params.pop('provider', None)
+ params.pop('base_url', None)
else:
# Another route than the profile's: its endpoint, wire mode, context pin and credential
# (inline key, key_env pointer, api_key_from) stay with the profile, never paired with the
diff --git a/tests/gateway/test_legacy_session_route_turn.py b/tests/gateway/test_legacy_session_route_turn.py
index 33f786f4ab2..274974223f8 100644
--- a/tests/gateway/test_legacy_session_route_turn.py
+++ b/tests/gateway/test_legacy_session_route_turn.py
@@ -192,6 +192,28 @@ def test_unchanged_route_keeps_the_profile_credential_for_that_endpoint(monkeypa
assert {r['auth'] for r in legacy.requests} == {'Bearer sk-profile-only'}
[email protected]('source', ['cli', 'tui', 'oneshot'])
+def test_billing_only_custom_endpoint_the_profile_moved_off_resumes_on_the_profile_route(monkeypatch, source):
+ """T21/F1: a pre-upgrade row whose only route is the ``billing_provider='custom'`` +
+ ``billing_base_url`` usage stamp, at an endpoint no configured entry serves any more (the server
+ moved; config.yaml now points elsewhere). The pre-merge resume ignored that bare ``custom`` stamp
+ and used the profile route; adoption must too, instead of freezing the dead endpoint into the
+ policy, where every turn retries until it gives up."""
+ with FakeLLMServer([Text('profile')], api_key='sk-profile-only') as profile, \
+ FakeLLMServer([Text('stale')]) as old:
+ home = Path(os.environ['HERMES_HOME'])
+ monkeypatch.setattr('gateway.run._hermes_home', home)
+ _write_profile(home, profile.base_url) # no entry serves the old endpoint
+ policy, (model, runtime) = _adopt_and_resolve(home, {}, source=source, billing={
+ 'billing_provider': 'custom', 'billing_base_url': old.base_url})
+ result = _run_turn(policy, model, runtime)
+ assert old.requests == []
+ assert result['final_response'] == 'profile'
+ assert runtime['base_url'].rstrip('/') == profile.base_url.rstrip('/')
+ assert {r['auth'] for r in profile.requests} == {'Bearer sk-profile-only'}
+ assert old.base_url not in policy.config_json
+
+
@pytest.mark.parametrize('shape', ['desktop-custom', 'auto-base-url', 'stored-api-key', 'cli-billing-auto'])
def test_v0_21_19_row_shapes_go_to_their_own_endpoint_with_its_own_key(monkeypatch, shape):
"""v0.21.19 row shapes beyond the TUI/CLI ones: a Desktop chat (tui_gateway with ``source='desktop'``,