Mechanism: under a session authority /api/sessions/{id}/chat runs through
the canonical turn, which builds its agent in session_execution with
fallback_model=_refresh_fallback_model() and re-applies the profile chain to
a reused cached agent, regardless of the admission's confirmed_runtime_lock.
On a primary 429/auth failure a locked turn silently moved to the profile
fallback, and nothing at settlement compared the served runtime with the
lock. Main builds the locked agent with fallback_model=None and refuses a
mismatched turn in _finish_turn_result.
Fix: session_api_turn.api_runtime_locked() names the executing API
admission's confirmed lock. The fresh build passes no fallback chain under
it, a reused agent gets an empty chain, and a cached agent already moved
onto a fallback is evicted and rebuilt. At settlement
(_run_conversation_with_approval) a locked turn runs the adapter's existing
_turn_runtime_metadata: its runtime rides the usage, and a mismatch makes
the stored result a failed turn carrying main's error text.
Red on r12-integ1:
tests/gateway/test_api_model_lock_fallback.py::test_locked_session_chat_does_not_fall_back_to_the_profile_chain
(real daemon + API server; loopback primary 429 insufficient_quota, healthy
loopback backup in fallback_providers; the backup received the locked turn).
tests/gateway/test_api_model_lock_fallback.py::test_locked_turn_served_by_another_runtime_settles_failed
(settlement mismatch refusal).
Review: dokterdok N63
(cherry picked from commit 2baba0cfb0075de5a4aa34fbfaac6e5184d11f29)
87 lines
4.5 KiB
Python
87 lines
4.5 KiB
Python
"""A confirmed Browser model lock on a canonical API turn never falls back (main parity).
|
|
|
|
One ordinary daemon with the API server: the locked primary answers 429 ``insufficient_quota``
|
|
and ``fallback_providers`` names a healthy backup. Main builds the locked agent with
|
|
``fallback_model=None`` and fails a turn served by another runtime; the shared owner must too.
|
|
"""
|
|
import asyncio
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import socket
|
|
|
|
import aiohttp
|
|
import pytest
|
|
|
|
from tests.gateway.fixtures.local_recovery_probe import child_env, daemon
|
|
from tests.gateway.test_managed_worker_construct import Backup, Primary, _serve
|
|
|
|
|
|
@pytest.mark.platforms("linux")
|
|
def test_locked_session_chat_does_not_fall_back_to_the_profile_chain(tmp_path):
|
|
root = Path(__file__).resolve().parents[2]
|
|
home, user = tmp_path / 'state', tmp_path / 'user'
|
|
home.mkdir(mode=0o700)
|
|
user.mkdir()
|
|
primary, backup = _serve(Primary), _serve(Backup)
|
|
primary.refuse = True
|
|
purl, burl = (f'http://127.0.0.1:{s.server_port}/v1' for s in (primary, backup))
|
|
with socket.socket() as sock:
|
|
sock.bind(('127.0.0.1', 0))
|
|
api_port = sock.getsockname()[1]
|
|
config = {'model': {'provider': 'custom', 'default': 'primary-model', 'base_url': purl},
|
|
'gateway': {'multiplex_profiles': False,
|
|
'platforms': {'api_server': {'enabled': True, 'port': api_port, 'host': '127.0.0.1'}}},
|
|
'auxiliary': {'title_generation': {'enabled': False}}, 'terminal': {'cwd': str(home)},
|
|
'platform_toolsets': {'api_server': []},
|
|
'fallback_providers': [{'provider': 'custom', 'model': 'backup-model', 'base_url': burl,
|
|
'api_key': 'loopback-only'}]}
|
|
(home / 'config.yaml').write_text(json.dumps(config))
|
|
env = {**child_env(), **{k: os.environ[k] for k in ('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=purl, API_SERVER_KEY='lock-owned-secret',
|
|
API_SERVER_ENABLED='true', API_SERVER_PORT=str(api_port), PYTHONUNBUFFERED='1')
|
|
|
|
async def exercise():
|
|
headers = {'Authorization': 'Bearer lock-owned-secret'}
|
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=90)) as client:
|
|
base = f'http://127.0.0.1:{api_port}/api/sessions'
|
|
async with client.post(base, json={'id': 'locked', 'title': 'Locked'}, headers=headers) as response:
|
|
assert response.status in (200, 201), await response.text()
|
|
async with client.post(f'{base}/locked/chat', headers=headers, json={
|
|
'message': 'NEED_FALLBACK', 'provider': 'custom', 'model': 'primary-model',
|
|
'require_model_lock': True}) as response:
|
|
return response.status, await response.text()
|
|
try:
|
|
with daemon(root, home, env, barrier=False):
|
|
status, text = asyncio.run(exercise())
|
|
finally:
|
|
for server in (primary, backup):
|
|
server.shutdown()
|
|
server.server_close()
|
|
assert primary.requests, (status, text)
|
|
assert backup.requests == [], 'a confirmed model lock was served by the profile fallback chain'
|
|
body = json.loads(text)
|
|
assert body['status'] == 'failed' and 'BACKUP_OK' not in text, body
|
|
assert body['usage']['runtime']['model_lock'] == 'confirmed', body
|
|
|
|
|
|
def test_locked_turn_served_by_another_runtime_settles_failed(api):
|
|
"""Main's settlement refusal: a locked turn whose agent ran on another provider/model is a
|
|
failed turn carrying the mismatch, never a completed answer from the wrong runtime."""
|
|
from types import SimpleNamespace
|
|
from gateway.run_turn_runner import _settle_api_runtime_lock
|
|
from gateway.session_api_turn import api_execution
|
|
|
|
agent = SimpleNamespace(provider='custom', model='backup-model', _hermes_api_runtime={'provider': 'custom'})
|
|
result, captured = {'final_response': 'BACKUP_OK', 'completed': True}, {'usage': {}}
|
|
token = api_execution.set({'adapter': api, 'settings': {
|
|
'confirmed_runtime_lock': True, 'route': {'provider': 'custom', 'model': 'primary-model'},
|
|
'route_source': 'raw_request'}})
|
|
try:
|
|
_settle_api_runtime_lock(agent, result, captured)
|
|
finally:
|
|
api_execution.reset(token)
|
|
assert result['failed'] is True and result['completed'] is False and result['final_response'] == ''
|
|
assert 'confirmed model lock runtime mismatch' in result['error']
|