The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a late repeated Stop no longer starts the next cached-agent turn interrupted
Tekniuma8e2f51be82d
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex a5b554a2f12..bb166f6b3eb 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -63,6 +63,10 @@ class SessionAuthority:# Stops accepted for a running generation whose agent does not exist yet# (first-turn construction); consumed by adopt_agent, keyed session -> generation.self.pending_stops = {}+ # session -> generation of the last Stop delivered to a live agent. A repeated+ # 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 = {}# Set by profile retirement (unserve): the drain claims no successor after its running turn.self.retiring = False@@ -432,6 +436,7 @@ class SessionAuthority:agent = self.agent(ref)if agent is not None:agent.interrupt()+ self.delivered_stops[ref.session_id] = generationelse:# Accepted for this exact claim; the turn must not construct its agent# afterwards and run the work as if no Stop had arrived.@@ -440,7 +445,13 @@ class SessionAuthority:def adopt_agent(self, session_id, generation, agent):"""The turn installs its agent for the running claim; a Stop latched while there- was no agent to deliver it to fires now, never against a later generation."""+ was no agent to deliver it to fires now, never against a later generation. A Stop+ delivered to the cached agent for an earlier generation may have landed after that+ turn's finalizer cleared it, so it is dropped rather than cancelling this turn. A managed+ worker is a fresh process per turn and carries no earlier flag to drop."""+ clear = getattr(agent, 'clear_interrupt', None)+ if self.delivered_stops.pop(session_id, generation) != generation and clear is not None:+ clear()if self.pending_stops.get(session_id) == generation:del self.pending_stops[session_id]agent.interrupt()diff --git a/tests/gateway/test_session_authority_late_stop.py b/tests/gateway/test_session_authority_late_stop.pynew file mode 100644index 00000000000..526cfa2287c--- /dev/null+++ b/tests/gateway/test_session_authority_late_stop.py@@ -0,0 +1,64 @@+"""A Stop for a finishing turn reaches only that turn, never the next one on the cached agent."""+import asyncio++import pytest++from tests.gateway.test_session_authority_cancel_settlement import ACTOR, REF, _authority, _submit, _wire_turn_agent+++class Agent:+ """The session's cached agent: one interrupt flag shared by every turn that reuses it."""++ def __init__(self):+ self.interrupted = False++ def interrupt(self, *args, **kwargs):+ self.interrupted = True++ def clear_interrupt(self, *args, **kwargs):+ self.interrupted = False+++async def test_a_stop_after_the_turn_finished_does_not_cancel_the_next_turn(tmp_path, monkeypatch):+ from gateway import session_finite++ db, authority = _authority(tmp_path, monkeypatch)+ agent = Agent()+ authority.runner._cached_agent_for = lambda route: agent+ finished, settle, ready, wired = asyncio.Event(), asyncio.Event(), asyncio.Event(), asyncio.Event()+ started = {}++ async def execute(authority, ref, row):+ if row['request_id'] == 'stopped-early':+ ready.set()+ await asyncio.wait_for(wired.wait(), 5) # Stop lands before this turn wires its agent.+ _wire_turn_agent(authority, row['generation'], agent)+ started[row['request_id']] = agent.interrupted+ if row['request_id'] == 'finishing':+ agent.clear_interrupt() # The turn's finalizer.+ finished.set()+ await asyncio.wait_for(settle.wait(), 5) # Its tail, before the claim is settled.+ return 'done'+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)++ with db:+ await _submit(authority, 'finishing')+ await asyncio.wait_for(finished.wait(), 5)+ # A repeated Stop for that turn arrives while it is still settling.+ await authority.interrupt(ACTOR, REF, authority._handle(REF).execution_generation)+ assert agent.interrupted+ settle.set()+ await asyncio.wait_for(authority.sessions['s'].task, 5)++ await _submit(authority, 'next')+ await asyncio.wait_for(authority.sessions['s'].task, 5)+ assert started['next'] is False, 'the late Stop must not start the next turn interrupted'++ # A Stop for a turn that has not wired the cached agent yet still reaches it.+ await _submit(authority, 'stopped-early')+ await asyncio.wait_for(ready.wait(), 5)+ await authority.interrupt(ACTOR, REF, authority._handle(REF).execution_generation)+ wired.set()+ await asyncio.wait_for(authority.sessions['s'].task, 5)+ assert started['stopped-early'] is True