diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py index 8d93c587e7..9974bfc449 100644 --- a/gateway/run_runtime.py +++ b/gateway/run_runtime.py @@ -228,7 +228,9 @@ def stop_authority_turns(authority, *, in_process=False): generation = live.event_stream.execution.get('execution_generation') if generation is None or sid in workers: continue - agent = running.get(live.route) + adopted_generation, agent = authority.adopted.get(sid, (None, None)) + if adopted_generation != generation: + agent = running.get(live.route) if agent is None or agent is _AGENT_PENDING_SENTINEL: authority.pending_stops[sid] = generation else: diff --git a/tests/gateway/test_session_authority_adopted_stop.py b/tests/gateway/test_session_authority_adopted_stop.py index ceaff4fb94..924db86b7c 100644 --- a/tests/gateway/test_session_authority_adopted_stop.py +++ b/tests/gateway/test_session_authority_adopted_stop.py @@ -39,3 +39,34 @@ async def test_stop_between_adoption_and_promotion_reaches_the_adopted_agent_aft await asyncio.wait_for(authority.sessions['s'].task, 5) assert ran['adopted'] is True, 'the Stop reached nobody: the adopted agent ran on' assert not authority.adopted, 'settlement must drop the adopted agent reference' + + +@pytest.mark.asyncio +async def test_retire_stop_between_adoption_and_promotion_reaches_the_adopted_agent(tmp_path, monkeypatch): + """The same window on the retire/shutdown path: stop_authority_turns(in_process=True) used to + re-resolve the running slot (still the pending sentinel) and latch a generation adopt_agent had + already consumed, so the adopted agent ran on through the profile stop.""" + from gateway import session_finite + from gateway.run_runtime import stop_authority_turns + + db, authority = _authority(tmp_path, monkeypatch) + fresh = Agent() + authority.runner._cached_agent_for = {}.get + adopted, stopped = asyncio.Event(), asyncio.Event() + ran = {} + + async def execute(authority, ref, row): + _wire_turn_agent(authority, row['generation'], fresh) + adopted.set() + await asyncio.wait_for(stopped.wait(), 5) + ran[row['request_id']] = fresh.interrupted + return 'done' + monkeypatch.setattr(session_finite, 'execute_finite_admission', execute) + + with db: + await _submit(authority, 'adopted') + await asyncio.wait_for(adopted.wait(), 5) + stop_authority_turns(authority, in_process=True) + stopped.set() + await asyncio.wait_for(authority.sessions['s'].task, 5) + assert ran['adopted'] is True, 'the profile Stop reached nobody: the adopted agent ran on'