fix(gateway): a healed entry keeps its own key when it shares a relay URL

Greptile P1 on #151956: when entry-a and entry-b point at one relay with different keys,
the same-route check after the heal saw equal URLs and put the chat back on entry-a's
credential. A healed entry is its own route, so that check is skipped for it.
This commit is contained in:
alt-glitch
2026-11-10 04:28:06 +05:30
committed by Siddharth Balyan
parent 00d9d600b2
commit 4f6ebb9afe
2 changed files with 13 additions and 6 deletions

View File

@@ -287,6 +287,7 @@ def adopt_legacy_session(authority, actor, row):
config = _load_gateway_config()
params, api_mode = legacy_bot_chat_launch(row, params, api_mode, config)
model_cfg = config.get('model')
healed = None
if 'provider' not in params and params.get('model') and params['model'] != _profile_default(model_cfg):
# A row with a model but no provider would run on the profile's slot, which serves another model
# (#132648); its saved URL/wire may be stamps of exactly those misrouted turns.
@@ -295,11 +296,12 @@ def adopt_legacy_session(authority, actor, row):
params['provider'] = healed
params.pop('base_url', None)
api_mode = None
# A healed entry is its own route, even when it shares a relay URL with the profile's entry.
if isinstance(model_cfg, dict) and ('provider' in params or 'base_url' in params):
from gateway.session_route_identity import ROUTE_BOUND_KEYS, resolved_route, same_route
saved = {'provider': params.get('provider', model_cfg.get('provider')), 'base_url': params.get('base_url')}
# ``auto`` matches every route, so an ``auto`` side proves nothing about the saved endpoint.
if ('auto' not in (resolved_route(model_cfg, config)[0], resolved_route(saved, config)[0])
if (not healed and 'auto' not in (resolved_route(model_cfg, config)[0], resolved_route(saved, config)[0])
and same_route(saved, model_cfg, config)):
# The profile still selects this endpoint: resume on the profile's route, so its own
# credential (inline key, ${ENV} key, key_env) keeps its normal precedence.

View File

@@ -1,19 +1,23 @@
"""#132648 on the path a resumed CLI/TUI chat really takes: the gateway adopts the saved row.
A row with a model but no provider was launched on the profile's main slot, which serves another model, so
every turn 404ed even though the CLI printed the right provider. Two loopback providers record what each one
every turn 404ed even though the CLI printed the right provider. Loopback providers record what each one
receives on the adopted session's next turn.
"""
import os
from pathlib import Path
import pytest
from tests.fakes.fake_llm_provider import FakeLLMServer, Text
from tests.gateway.test_legacy_session_route_turn import _adopt_and_resolve
def test_an_adopted_row_with_no_provider_runs_on_the_entry_that_serves_its_model(monkeypatch):
@pytest.mark.parametrize('shared_relay', [False, True], ids=['own-endpoints', 'one-relay-two-keys'])
def test_an_adopted_row_with_no_provider_runs_on_the_entry_that_serves_its_model(monkeypatch, shared_relay):
from run_agent import AIAgent
with FakeLLMServer([Text('main')]) as main, FakeLLMServer([Text('entry-b ok')]) as entry_b:
with FakeLLMServer([Text('main')]) as main, FakeLLMServer([Text('entry-b ok')]) as other:
entry_b = main if shared_relay else other
home = Path(os.environ['HERMES_HOME'])
monkeypatch.setattr('gateway.run._hermes_home', home)
home.mkdir(parents=True, exist_ok=True)
@@ -28,6 +32,7 @@ def test_an_adopted_row_with_no_provider_runs_on_the_entry_that_serves_its_model
quiet_mode=True, skip_context_files=True, skip_memory=True, enabled_toolsets=['memory'])
agent.run_conversation('hello again')
assert main.requests == []
assert [r['model'] for r in entry_b.main_requests()] == ['legacy-model']
assert {r['auth'] for r in entry_b.requests} == {'Bearer sk-b'}
assert {r['auth'] for r in entry_b.requests} == {'Bearer sk-b'} # entry-b's own key, never entry-a's
if not shared_relay:
assert main.requests == []