The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): scope refresh interrupts to active turns
fangliquanflq6b25e67047d6
diff --git a/run_agent.py b/run_agent.pyindex 9399fadecf8..198a9a8a273 100644--- a/run_agent.py+++ b/run_agent.py@@ -8088,6 +8088,7 @@ class AIAgent:durable_turn_lease_thread = Nonedurable_turn_lease_activity_lock = threading.Lock()durable_turn_lease_turn_active = False+ durable_turn_lease_interrupt_message = Nonetoken = Noneacct_token = Nonetask_started = False@@ -8101,6 +8102,29 @@ class AIAgent:if durable_turn_lease_stop is not None:durable_turn_lease_stop.set()+ def _clear_durable_turn_lease_interrupt() -> None:+ """Clear only the interrupt admitted by this turn's refresher."""+ message = durable_turn_lease_interrupt_message+ if not message:+ return++ def _clear_if_owned() -> None:+ if getattr(self, "_interrupt_message", None) != message:+ return+ self._interrupt_requested = False+ self._interrupt_message = None+ getattr(self, "_hard_interrupt_requested", threading.Event()).clear()+ self._interrupt_thread_signal_pending = False+ if self._execution_thread_id is not None:+ _set_interrupt(False, self._execution_thread_id)++ redirect_lock = getattr(self, "_pending_redirect_lock", None)+ if redirect_lock is None:+ _clear_if_owned()+ else:+ with redirect_lock:+ _clear_if_owned()+try:# Serialize the full load -> run -> flush region across Hermes# processes. Gateway's asyncio lease closes alias routing inside one@@ -8270,12 +8294,14 @@ class AIAgent:def _refresh_durable_turn_lease() -> None:def _interrupt_turn(message: str) -> None:+ nonlocal durable_turn_lease_interrupt_messagewith durable_turn_lease_activity_lock:if (durable_turn_lease_stop.is_set()or not durable_turn_lease_turn_active):return+ durable_turn_lease_interrupt_message = messagetry:self.interrupt(message, hard_cancel=True)except Exception:@@ -8316,14 +8342,12 @@ class AIAgent:)return- with durable_turn_lease_activity_lock:- durable_turn_lease_turn_active = Truedurable_turn_lease_thread = threading.Thread(target=_refresh_durable_turn_lease,name="session-turn-lease-refresh",daemon=True,)- durable_turn_lease_thread.start()+relay_lease = relay_runtime.SESSION_COORDINATOR.acquire_conversation(profile_key=relay_runtime.current_profile_key(),@@ -8367,6 +8391,10 @@ class AIAgent:# which may be observed from another thread.with bind_subagent_parent(self), scoped_runtime_main({}):try:+ if durable_turn_lease_thread is not None:+ with durable_turn_lease_activity_lock:+ durable_turn_lease_turn_active = True+ durable_turn_lease_thread.start()result = run_conversation(self,user_message,@@ -8385,6 +8413,7 @@ class AIAgent:# those post-loop steps must not receive a late refresh# interrupt that poisons the next turn on this cached agent._stop_durable_turn_lease_refresher()+ _clear_durable_turn_lease_interrupt()terminal = result if isinstance(result, dict) else {}if terminal.get("interrupted") is True:relay_outcome = "cancelled"diff --git a/tests/run_agent/test_cross_process_turn_lease.py b/tests/run_agent/test_cross_process_turn_lease.pyindex 2d68196dd3e..252e92d6edf 100644--- a/tests/run_agent/test_cross_process_turn_lease.py+++ b/tests/run_agent/test_cross_process_turn_lease.py@@ -300,6 +300,7 @@ def test_run_conversation_interrupts_when_lease_refresh_lost(monkeypatch):def track_interrupt(message=None, hard_cancel=False):interrupt_calls.append((message, hard_cancel))agent._interrupt_requested = True+ agent._interrupt_message = messageagent.interrupt = track_interrupt@@ -349,6 +350,7 @@ def test_run_conversation_interrupts_when_lease_refresh_errors(monkeypatch):def track_interrupt(message=None, hard_cancel=False):interrupt_calls.append((message, hard_cancel))agent._interrupt_requested = True+ agent._interrupt_message = messageagent.interrupt = track_interrupt@@ -391,29 +393,33 @@ def test_refresh_error_after_loop_completion_does_not_poison_next_turn(monkeypatagent._session_turn_lease_refresh_interval = 0.01refresh_started = threading.Event()release_refresh = threading.Event()+ interrupt_started = threading.Event()interrupt_calls = []def track_interrupt(message=None, hard_cancel=False):interrupt_calls.append((message, hard_cancel))+ interrupt_started.set()+ release_refresh.wait(timeout=2.0)agent._interrupt_requested = True+ agent._interrupt_message = messageagent.interrupt = track_interruptdef delayed_refresh_error(session_id, holder, **kwargs):refresh_started.set()- release_refresh.wait(timeout=2.0)raise OSError("database unavailable")db.refresh_session_turn_lease = delayed_refresh_errordef fake_run(_agent, _message, _system, history, *_args, **_kwargs):assert refresh_started.wait(timeout=2.0)+ assert interrupt_started.wait(timeout=2.0)+ threading.Timer(0.05, release_refresh.set).start()return {"final_response": "ok", "messages": history, "failed": False}original_finish = relay_runtime.SESSION_COORDINATOR.finish_logical_callsdef finish_after_refresh(turn, *, outcome):- release_refresh.set()time.sleep(0.05)return original_finish(turn, outcome=outcome)@@ -431,8 +437,10 @@ def test_refresh_error_after_loop_completion_does_not_poison_next_turn(monkeypat)assert result["final_response"] == "ok"- assert interrupt_calls == []+ assert len(interrupt_calls) == 1+ assert interrupt_calls[0][1] is Trueassert agent._interrupt_requested is False+ assert agent._interrupt_message is Nonedef test_late_refresh_miss_after_release_does_not_interrupt(monkeypatch):@@ -445,6 +453,7 @@ def test_late_refresh_miss_after_release_does_not_interrupt(monkeypatch):def track_interrupt(message=None, hard_cancel=False):interrupt_calls.append((message, hard_cancel))agent._interrupt_requested = True+ agent._interrupt_message = messageagent.interrupt = track_interrupt