The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): refuse a moved chain before a streamed response starts
unsupportedpastelsa75d1426cce0
diff --git a/gateway/platforms/api_server_response_admissions.py b/gateway/platforms/api_server_response_admissions.pyindex 26c91e742ec..47a9bd8c7ad 100644--- a/gateway/platforms/api_server_response_admissions.py+++ b/gateway/platforms/api_server_response_admissions.py@@ -119,6 +119,18 @@ def _stored_owner(adapter, session_id):return authority.logical_owner(session_id) if authority is not None else session_id+async def _bind_successor(adapter, session_id, run_kwargs):+ """Bind the session a chain moved to before any response starts. A streaming request has+ answered 200 by the time its turn admits, so the bind's retryable ``chain_moved`` refusal must+ happen here to reach the caller as a 503. The admission's own bind then finds this binding."""+ from gateway.session_api import bind_api_steps, run_steps_off_loop+ from gateway.session_authorities import active_authority+ authority = active_authority(adapter.gateway_runner)+ if authority is not None:+ await run_steps_off_loop(authority, bind_api_steps(authority, session_id, walked_from=run_kwargs['walked_from'],+ caller_key=run_kwargs['gateway_session_key']))++async def prepare_context(adapter, request, body, gateway_session_key, durable_key, scope, key):"""Validate first-time requests; exact retries use the accepted payload and result only."""import asyncio@@ -180,5 +192,7 @@ async def prepare_context(adapter, request, body, gateway_session_key, durable_kif durable_key:return await _admit(adapter, durable_key, request_id, request_binding(adapter, request, durable_key),run_kwargs, body.get('model', adapter._model_name)), None+ if 'walked_from' in run_kwargs:+ await _bind_successor(adapter, session_id, run_kwargs)return dict(session_id=session_id, user_message=user_message, conversation_history=history,instructions=instructions, run_kwargs=run_kwargs, run_agent=adapter._run_agent), Nonediff --git a/tests/gateway/test_derived_generation_after_sweep.py b/tests/gateway/test_derived_generation_after_sweep.pyindex df07222ae69..205dc63de31 100644--- a/tests/gateway/test_derived_generation_after_sweep.py+++ b/tests/gateway/test_derived_generation_after_sweep.py@@ -186,6 +186,48 @@ async def test_a_fence_reclaimed_between_chain_lookup_and_bind_never_splits_the_assert owner.db.get_session(successor) is None, 'the chain split across the root and its successor'[email protected]('idempotency_key', [None, 'streamed-race'], ids=['plain', 'idempotent'])+async def test_a_streamed_continuation_that_races_a_reclaim_gets_a_retryable_503(+ api, owner, served, idempotency_key, monkeypatch):+ """The streaming Responses door answers the same race (the root's sweep marker reclaimed between+ the chain lookup and the successor's bind) like the non-streaming one: 503 ``chain_moved`` with+ ``Retry-After: 1`` before any SSE frame, not a 200 stream that ends in ``response.failed``."""+ from gateway.platforms import api_server_authority_runs as runs+ from gateway.session_api import api_generation_id+ app = web.Application()+ app.router.add_post('/v1/responses', api._handle_responses)+ auth = {'Authorization': 'Bearer ' + chat._API_KEY}+ keyed = {'Idempotency-Key': idempotency_key} if idempotency_key else {}+ store = api._current_response_store()+ async with TestClient(TestServer(app)) as client:+ opened = await client.post('/v1/responses', json={'input': 'remember 42'}, headers=auth)+ assert opened.status == 200, await opened.text()+ first = (await opened.json())['id']+ root = store.get(first)['session_id']+ successor = api_generation_id(root, 1)+ _sweep(owner, root)+ lookup = runs.continued_session++ async def reclaimed_after_lookup(*args, **kwargs):+ found = await lookup(*args, **kwargs)+ _past_the_horizon(owner)+ return found+ monkeypatch.setattr(runs, 'continued_session', reclaimed_after_lookup)+ body = {'input': 'and then?', 'previous_response_id': first, 'stream': True}+ raced = await client.post('/v1/responses', json=body, headers={**auth, **keyed})+ assert raced.status == 503, await raced.text()+ assert (await raced.json())['error']['code'] == 'chain_moved'+ assert raced.headers.get('Retry-After') == '1'+ assert owner.db.get_session(successor) is None, 'the successor was bound without pinning its walk'+ monkeypatch.setattr(runs, 'continued_session', lookup)+ again = await client.post('/v1/responses', json=body, headers={**auth, **keyed})+ assert again.status == 200, await again.text()+ assert 'response.completed' in await again.text()+ assert owner.db.get_session(root) is not None, 'the retried continuation left the root'+ assert owner.db.get_session(successor) is None, 'the chain split across the root and its successor'++def test_keep_passed_fences_reports_a_walk_it_could_not_pin(owner):"""``keep_passed_fences`` pins every fence a walk passed and says whether the walk still holds:a passed generation without a marker means the walk would now stop there."""