The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a Stop after adoption goes to the adopted agent, not the cache
Teknium3e839b23a5b4
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex de0df1c1807..fcf5318aa87 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -85,9 +85,10 @@ class SessionAuthority:# Stop can land on the session's reusable cached agent after that turn's finalizer# cleared it; the next generation's adopt_agent drops it instead of starting interrupted.self.delivered_stops = {}- # session -> generation whose turn adopted its agent. Until then the resident- # agent is the previous turn's cached one, which pre-turn compression or a changed agent- # config replaces, so a Stop that only reached it would let the rebuilt agent run the turn.+ # session -> (generation, agent) its turn adopted. Until then the resident agent is the+ # previous turn's cached one, which pre-turn compression or a changed agent config+ # replaces, so a Stop that only reached it would let the rebuilt agent run the turn. After+ # it, a Stop goes to this agent, never through a cache that may have evicted it.self.adopted = {}# Queued admissions whose cancellation may have committed before its observers settled.self.cancel_obligations = set()@@ -572,11 +573,13 @@ class SessionAuthority:if handle.execution_generation != generation:raise RuntimeStoreError('stale_generation')if handle.execution_state == 'running':- agent = self.agent(ref)+ adopted_generation, agent = self.adopted.get(ref.session_id, (None, None))+ if adopted_generation != generation:+ agent = self.agent(ref)if agent is not None:agent.interrupt()self.delivered_stops[ref.session_id] = generation- if self.adopted.get(ref.session_id) != generation and not self._runs_own_agent(ref):+ if adopted_generation != generation and not self._runs_own_agent(ref):# Accepted for this exact claim before its turn adopted an agent: whatever agent# the turn adopts for this generation must not run the work as if no Stop arrived.self.pending_stops[ref.session_id] = generation@@ -596,7 +599,7 @@ class SessionAuthority:delivered to the cached agent for an earlier generation may have landed after thatturn's finalizer cleared it, so it is dropped rather than cancelling this turn. A managedworker is a fresh process per turn and carries no earlier flag to drop."""- self.adopted[session_id] = generation+ self.adopted[session_id] = (generation, agent)clear = getattr(agent, 'clear_interrupt', None)if self.delivered_stops.pop(session_id, generation) != generation and clear is not None:clear()diff --git a/tests/gateway/test_session_authority_adopted_stop.py b/tests/gateway/test_session_authority_adopted_stop.pynew file mode 100644index 00000000000..ceaff4fb942--- /dev/null+++ b/tests/gateway/test_session_authority_adopted_stop.py@@ -0,0 +1,41 @@+"""A Stop after the turn adopted its agent reaches that agent even if the cache no longer holds it."""+import asyncio++import pytest++from tests.gateway.test_session_authority_cancel_settlement import ACTOR, REF, _authority, _submit, _wire_turn_agent+from tests.gateway.test_session_authority_preadopt_stop import Agent+++async def test_stop_between_adoption_and_promotion_reaches_the_adopted_agent_after_cache_eviction(tmp_path, monkeypatch):+ """adopt_agent ran, the route's running slot is still the claim's pending sentinel, and the+ agent cache entry was evicted (pressure / idle sweep): the Stop must still interrupt the agent+ this generation adopted, not resolve nobody through the cache and latch nothing."""+ from gateway import session_finite++ db, authority = _authority(tmp_path, monkeypatch)+ fresh = Agent()+ cache = {}+ authority.runner._cached_agent_for = cache.get+ adopted, stopped = asyncio.Event(), asyncio.Event()+ ran = {}++ async def execute(authority, ref, row):+ cache['route'] = fresh+ _wire_turn_agent(authority, row['generation'], fresh)+ cache.pop('route') # evicted before the running slot is promoted+ 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)+ await authority.interrupt(ACTOR, REF, authority._handle(REF).execution_generation)+ stopped.set()+ 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'