The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a replayed rewind receipt no longer evicts a newer turn's agent
Teknium0d38040caca4
diff --git a/gateway/session_mutations.py b/gateway/session_mutations.pyindex a3840532466..156b088500b 100644--- a/gateway/session_mutations.py+++ b/gateway/session_mutations.py@@ -124,7 +124,12 @@ async def mutate_session(authority, actor, ref, params):# retries repeat them (like delete's retirement); only the one-shot event is fenced._project_committed(authority, ref, operation, result)if live is not None:- if operation == 'rewind':+ # The cached agent is the rewind's to repair only while the session is still at the+ # receipt's execution generation (rewind advances the expected one by exactly one):+ # once a later admission claimed, the cache holds ITS agent, and evicting it would+ # make Stop latch instead of arriving.+ if (operation == 'rewind' and authority.db.get_session(ref.session_id)['runtime_generation']+ == params['expected_generation'] + 1):authority.runner._evict_cached_agent(live.route)if applied:live.event_stream.publish(ref.session_id, result, event_type='session.updated')diff --git a/tests/gateway/test_session_mutation_retry_repair.py b/tests/gateway/test_session_mutation_retry_repair.pyindex 17ddb00c39e..10be705cb3a 100644--- a/tests/gateway/test_session_mutation_retry_repair.py+++ b/tests/gateway/test_session_mutation_retry_repair.py@@ -44,3 +44,51 @@ async def test_exact_retry_repairs_runtime_after_post_commit_failure_without_secassert live.event_stream.since(*watermark)['events'] == [], 'exact retry re-emitted the one-shot event'finally:await owner.close()+++async def test_rewind_replay_never_evicts_a_later_admissions_running_agent(tmp_path):+ """R3: an exact rewind retry after a later admission claimed must leave that turn's adopted+ agent in the real cache, so the interrupt RPC delivers Stop instead of latching it."""+ import threading+ from collections import OrderedDict+ from gateway.run import GatewayRunner+ from gateway.session_contract import SessionRef++ class Agent:+ interrupts = 0++ def interrupt(self, *args, **kwargs):+ Agent.interrupts += 1++ with SessionDB(db_path=tmp_path / 'state.db') as db:+ db.create_session('s', source='test')+ db.append_message('s', role='user', content='keep')+ db.append_message('s', role='assistant', content='drop')+ db.append_message('s', role='user', content='drop too')+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')+ runner = object.__new__(GatewayRunner)+ runner._agent_cache, runner._agent_cache_lock, runner._running_agents = OrderedDict(), threading.Lock(), {}+ runner._draining = False+ authority = SessionAuthority(runner, profile_id='owned', instance_id='owner', db=db, epoch=epoch)+ authority.sessions['s'] = LiveSession(None, 'route')+ owner = AuthorityConnection(authority, object(), {'user_id': 'human'})+ request = {'id': 1, 'method': 'session.mutate', 'params': {'session_id': 's',+ 'request_id': 'rewind', 'expected_revision': 0, 'expected_generation': 0,+ 'operation': 'rewind', 'payload': {'target_message_id': db.get_messages('s')[2]['id']}}}+ try:+ first = await owner.dispatch(request)+ rt.admit_session_input(db, epoch=epoch, principal_id='human', session_id='s',+ request_id='later', payload={'text': 'later'})+ row = rt.claim_session_input(db, epoch=epoch, session_id='s')+ agent = Agent()+ runner._agent_cache['route'] = (agent, 'signature')+ runner._running_agents['route'] = agent+ authority.adopt_agent('s', row['generation'], agent)+ assert (await owner.dispatch(request))['result'] == first['result']+ ref = SessionRef('owned', 's')+ assert authority.agent(ref) is agent, 'rewind replay evicted the running successor agent'+ await authority.interrupt(owner.actor, ref, row['generation'])+ assert Agent.interrupts == 1 and authority.pending_stops == {}+ finally:+ await owner.close()