Skip to content

The agent that grows with you

fix(gateway): a typed /model alias on a local session keeps its own endpoint and key

Tekniuma92501f2b16b
diff --git a/gateway/session_setting_commands.py b/gateway/session_setting_commands.py
index a62aa1afc79..01e52992b3c 100644
--- a/gateway/session_setting_commands.py
+++ b/gateway/session_setting_commands.py
@@ -84,7 +84,9 @@ def defer_local_model_pick(authority, live, result, effort=''):
from hermes_state_local import local_receipt
sid = live.source.chat_id
owner = SimpleNamespace(subject=local_receipt(authority.db, sid)['principal_id'])
- payload = {'model': result.new_model}
+ # The alias the user typed, not the bare model it names: two aliases may name one model on one
+ # provider at different endpoints, and re-resolving the bare id picks the first such alias.
+ payload = {'model': result.resolved_via_alias or result.new_model}
if result.target_provider:
payload['provider'] = result.target_provider
if effort:
diff --git a/tests/gateway/test_session_model_command_commits.py b/tests/gateway/test_session_model_command_commits.py
index 778aca9b103..ed78bc8419c 100644
--- a/tests/gateway/test_session_model_command_commits.py
+++ b/tests/gateway/test_session_model_command_commits.py
@@ -9,7 +9,7 @@ import threading
import pytest
-from tests.gateway.fixtures.local_recovery_probe import daemon, rpc, websocket
+from tests.gateway.fixtures.local_recovery_probe import Model, daemon, rpc, websocket
from tests.gateway.test_session_mutation_model import CatalogModel
from tests.waiting import async_wait_until
@@ -71,3 +71,75 @@ def test_typed_model_command_on_a_local_session_moves_the_next_request(tmp_path)
peer.shutdown()
peer.server_close()
thread.join(timeout=5)
+
+
+class _AliasEndpoint(Model):
+ def do_GET(self):
+ payload = json.dumps({'data': [{'id': name} for name in ('original', 'shared')]}).encode()
+ self.send_response(200)
+ self.send_header('Content-Type', 'application/json')
+ self.send_header('Content-Length', str(len(payload)))
+ self.end_headers()
+ self.wfile.write(payload)
+
+ def do_POST(self):
+ self.server.auth.append(self.headers.get('Authorization'))
+ super().do_POST()
+
+
+def test_typed_alias_on_a_local_session_keeps_its_own_endpoint_and_key(tmp_path):
+ """Two aliases name one model on ``custom`` at different endpoints: the typed (second) alias's
+ endpoint and key serve the next request; the first alias's endpoint receives nothing."""
+ root = Path(__file__).resolve().parents[2]
+ home, user = tmp_path / 'state', tmp_path / 'user'
+ home.mkdir(mode=0o700)
+ user.mkdir()
+ peers = []
+ for _ in range(3):
+ peer = ThreadingHTTPServer(('127.0.0.1', 0), _AliasEndpoint)
+ peer.requests, peer.auth = [], []
+ peer.blocked, peer.release = threading.Event(), threading.Event()
+ threading.Thread(target=peer.serve_forever, daemon=True).start()
+ peers.append(peer)
+ launch, first, second = (f'http://127.0.0.1:{p.server_port}/v1' for p in peers)
+ cfg = {'gateway': {'multiplex_profiles': False},
+ 'model': {'provider': 'custom', 'default': 'original', 'base_url': launch},
+ 'model_aliases': {
+ 'first-alias': {'model': 'shared', 'provider': 'custom', 'base_url': first, 'api_key': 'key-one'},
+ 'second-alias': {'model': 'shared', 'provider': 'custom', 'base_url': second, 'api_key': 'key-two'}},
+ 'auxiliary': {'title_generation': {'enabled': False}}, 'platform_toolsets': {'cli': []}}
+ (home / 'config.yaml').write_text(json.dumps(cfg))
+ (home / 'models_dev_cache.json').write_text(json.dumps({'custom': {'id': 'custom', 'models': {
+ name: {'id': name, 'name': name, 'limit': {'context': 100000, 'output': 4000}}
+ for name in ('original', 'shared')}}}))
+ env = {k: os.environ[k] for k in ('PATH', 'LANG', 'TZ', 'TIRITH_ENABLED') if k in os.environ}
+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),
+ OPENAI_API_KEY='loopback-only', OPENAI_BASE_URL=launch, PYTHONUNBUFFERED='1')
+
+ async def run(desc):
+ async with websocket(home, desc) as ws:
+ async def turn(sid, text):
+ assert 'result' in await rpc(ws, 'prompt.submit', session_id=sid, input_id=text, text=text)
+
+ def terminal():
+ with sqlite3.connect(home / 'state.db') as db:
+ row = db.execute('SELECT status FROM session_admissions WHERE request_id=?', (text,)).fetchone()
+ return row and row[0] == 'terminal'
+ await async_wait_until(terminal, timeout=30, what=f'{text} terminal')
+
+ sid = (await rpc(ws, 'session.create', source='tui', request_id='owner', toolsets=[]))['result']['session_id']
+ await turn(sid, '/model second-alias')
+ await turn(sid, 'AFTER')
+ try:
+ with daemon(root, home, env, barrier=False) as (_, desc):
+ asyncio.run(run(desc))
+ assert [r['model'] for r in peers[2].requests] == ['shared'], [p.requests for p in peers]
+ assert set(peers[2].auth) == {'Bearer key-two'}, peers[2].auth
+ assert peers[1].auth == [], peers[1].auth
+ assert peers[1].requests == [], peers[1].requests
+ finally:
+ for peer in peers:
+ peer.release.set()
+ peer.shutdown()
+ peer.server_close()