Skip to content

The agent that grows with you

fix(sessions): scope refresh interrupts to active turns

fangliquanflq6b25e67047d6
diff --git a/run_agent.py b/run_agent.py
index 9399fadecf8..198a9a8a273 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -8088,6 +8088,7 @@ class AIAgent:
durable_turn_lease_thread = None
durable_turn_lease_activity_lock = threading.Lock()
durable_turn_lease_turn_active = False
+ durable_turn_lease_interrupt_message = None
token = None
acct_token = None
task_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_message
with 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 = message
try:
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 = True
durable_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.py
index 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 = message
agent.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 = message
agent.interrupt = track_interrupt
@@ -391,29 +393,33 @@ def test_refresh_error_after_loop_completion_does_not_poison_next_turn(monkeypat
agent._session_turn_lease_refresh_interval = 0.01
refresh_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 = message
agent.interrupt = track_interrupt
def 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_error
def 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_calls
def 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 True
assert agent._interrupt_requested is False
+ assert agent._interrupt_message is None
def 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 = message
agent.interrupt = track_interrupt