Skip to content

The agent that grows with you

refactor(agent/interrupt_control): unify lock/codex/abort/worker-fanout branches into helpers (425 -> 321 LOC)

Teknium576fd71fbc29
diff --git a/agent/interrupt_control.py b/agent/interrupt_control.py
index f55cb612904..9bb1b0b5b57 100644
--- a/agent/interrupt_control.py
+++ b/agent/interrupt_control.py
@@ -3,6 +3,7 @@
Soft/hard interrupt requests, tool-thread interrupt propagation, pending steer/redirect queues.
Extracted from ``run_agent.py``; every method resolves through ``AIAgent``'s MRO unchanged.
"""
+import contextlib
import logging
import threading
from typing import Optional
@@ -14,6 +15,68 @@ from tools.interrupt import set_interrupt as _set_interrupt
logger = logging.getLogger("run_agent")
+def _fence_cancel_before_commit(fence, *, when_in_flight: bool, failure_log: str) -> None:
+ """Call ``type(fence).cancel_before_commit(fence)`` when ``commit_in_flight`` matches.
+
+ Hard-cancel admission has two halves (#99758 P1). BEFORE the generation claim is
+ consumed only a commit already in flight is waited out (the call blocks on the fence
+ lock and returns False WITHOUT setting ``_cancelled``) — cancelling a still-pending
+ fence there would be irreversible for an abort that may yet be declined. AFTER the
+ claim survived, only a still-pending commit is cancelled; one that started meanwhile
+ owns the fence and completes on its own."""
+ if fence is None or bool(getattr(fence, "commit_in_flight", False)) is not when_in_flight:
+ return
+ cancel_before_commit = getattr(type(fence), "cancel_before_commit", None)
+ if callable(cancel_before_commit):
+ try:
+ cancel_before_commit(fence)
+ except Exception:
+ logger.debug(failure_log, exc_info=True)
+
+
+def _ic_lock(agent, attr: str):
+ """``with`` the lock stored at ``attr`` when present; __init__-less test stubs run unlocked."""
+ lock = getattr(agent, attr, None)
+ return contextlib.nullcontext() if lock is None else lock
+
+
+def _ic_codex_method(agent, name: str):
+ """Codex app-server owns its model/tool loop; return its ``name`` hook or None."""
+ if getattr(agent, "api_mode", None) != "codex_app_server":
+ return None
+ method = getattr(getattr(agent, "_codex_session", None), name, None)
+ return method if callable(method) else None
+
+
+def _ic_abort_active_request(agent, reason: str, failure_log: str) -> None:
+ """Shut the registered in-flight request's sockets (cron turns register their client here)."""
+ abort = getattr(agent, "_active_request_abort", None)
+ if callable(abort):
+ try:
+ abort(reason)
+ except Exception:
+ logger.debug(failure_log, exc_info=True)
+
+
+def _ic_signal_tool_workers(agent, active: bool, **kw) -> None:
+ """Fan the tool interrupt bit out to concurrent-tool worker tids.
+
+ ``is_interrupted()`` inside a tool only sees its own tid, so without this a hung
+ concurrent tool runs to its own timeout (and a stale bit could survive a turn
+ boundary onto a recycled tid). getattr covers __init__-less stubs."""
+ tracker = getattr(agent, "_tool_worker_threads", None)
+ tracker_lock = getattr(agent, "_tool_worker_threads_lock", None)
+ if tracker is None or tracker_lock is None:
+ return
+ with tracker_lock:
+ worker_tids = list(tracker)
+ for tid in worker_tids:
+ try:
+ _set_interrupt(active, tid, **kw)
+ except Exception:
+ pass
+
+
class InterruptControlMixin:
"""interrupt()/hard_interrupt()/clear_interrupt()/steer()/redirect() (see module docstring)."""
@@ -39,71 +102,24 @@ class InterruptControlMixin:
# progress invalidates it; it is CONSUMED at the final mutation edge, so a resumed turn abandons
# the abort.
with self._liveness_activity_lock():
- if (
- getattr(self, "_turn_liveness_activity_generation", 0)
- != require_generation
- ):
+ if getattr(self, "_turn_liveness_activity_generation", 0) != require_generation:
return False
self._turn_liveness_abort_claim = require_generation
- # A hard stop and redirect share one lock so /stop cannot race with an
- # accepted correction and accidentally turn itself into a retry.
- def _wait_for_compression_commit() -> None:
- # Pre-claim half of hard-cancel admission (#99758 P1): wait out a commit that already crossed its
- # boundary but mutate NOTHING — cancelling a pending fence is irreversible and must wait until the
- # generation claim survived the final mutation edge (_cancel_pending_compression_commit).
- fence = vars(self).get("_active_compression_commit_fence")
- if fence is None:
- return
- if not getattr(fence, "commit_in_flight", False):
- # No commit in flight — cancel_before_commit here WOULD cancel the pending commit; leave it to
- # the destructive half.
- return
- cancel_before_commit = getattr(
- type(fence), "cancel_before_commit", None
- )
- if callable(cancel_before_commit):
- try:
- # A commit holds the fence lock through finish_commit: this blocks until it finishes and
- # returns False WITHOUT setting _cancelled.
- cancel_before_commit(fence)
- except Exception:
- logger.debug(
- "Compression hard-cancel fence wait failed",
- exc_info=True,
- )
-
- def _cancel_pending_compression_commit() -> None:
- # Destructive half of hard-cancel admission (#99758 P1): runs only AFTER the claim survived, so a
- # declined abort never leaves the fence cancelled. A commit that started meanwhile owns the fence
- # and completes on its own; only a still-pending commit is cancelled here.
- fence = vars(self).get("_active_compression_commit_fence")
- if fence is None:
- return
- if getattr(fence, "commit_in_flight", False):
- return
- cancel_before_commit = getattr(
- type(fence), "cancel_before_commit", None
- )
- if callable(cancel_before_commit):
- try:
- # Marks the fence cancelled (or waits out a just-started commit) without touching the
- # hard-stop Event, which was published at the claim edge.
- cancel_before_commit(fence)
- except Exception:
- logger.debug(
- "Compression hard-cancel fence admission failed",
- exc_info=True,
- )
+ # Tool cancellation attribution stays separate from _interrupt_message, which may carry the user's
+ # full next message.
+ tool_interrupt_reason = (
+ (tool_reason or "explicit stop requested")
+ if hard_cancel
+ else ("user sent a new message" if message else "user interrupt")
+ )
def _publish_interrupt_state() -> None:
self._interrupt_requested = True
self._interrupt_message = message
self._tool_interrupt_reason = tool_interrupt_reason
if hard_cancel:
- _hard_event = getattr(
- self, "_hard_interrupt_requested", None
- )
+ _hard_event = getattr(self, "_hard_interrupt_requested", None)
if _hard_event is not None:
_hard_event.set()
@@ -117,101 +133,60 @@ class InterruptControlMixin:
_publish_interrupt_state()
return True
with self._liveness_activity_lock():
- if (
- getattr(self, "_turn_liveness_abort_claim", None)
- != require_generation
- ):
+ if getattr(self, "_turn_liveness_abort_claim", None) != require_generation:
return False
self._turn_liveness_abort_claim = None
_publish_interrupt_state()
return True
- # Tool cancellation attribution stays separate from _interrupt_message, which may carry the user's
- # full next message.
- tool_interrupt_reason = (
- (tool_reason or "explicit stop requested")
- if hard_cancel
- else ("user sent a new message" if message else "user interrupt")
- )
-
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is not None:
- with _redirect_lock:
- # The blocking in-flight-commit wait runs BEFORE the atomic claim edge (redirect lock still
- # held); the destructive pending-commit cancel runs AFTER the claim survives (#99758 P1).
- if hard_cancel:
- _wait_for_compression_commit()
- if not _consume_claim_and_publish_first_state():
- return False
- if hard_cancel:
- _cancel_pending_compression_commit()
- self._pending_redirect = None
- else:
+ # A hard stop and redirect share one lock so /stop cannot race with an accepted correction and
+ # accidentally turn itself into a retry. The blocking in-flight-commit wait runs BEFORE the atomic
+ # claim edge (redirect lock still held); the destructive pending-commit cancel runs AFTER the claim
+ # survives (#99758 P1).
+ with _ic_lock(self, "_pending_redirect_lock"):
if hard_cancel:
- _wait_for_compression_commit()
+ _fence_cancel_before_commit(
+ vars(self).get("_active_compression_commit_fence"),
+ when_in_flight=True,
+ failure_log="Compression hard-cancel fence wait failed",
+ )
if not _consume_claim_and_publish_first_state():
return False
if hard_cancel:
- _cancel_pending_compression_commit()
+ _fence_cancel_before_commit(
+ vars(self).get("_active_compression_commit_fence"),
+ when_in_flight=False,
+ failure_log="Compression hard-cancel fence admission failed",
+ )
self._pending_redirect = None
- # Codex app-server owns its model/tool loop and watches a private
- # interrupt event rather than Hermes' per-thread flag.
- if getattr(self, "api_mode", None) == "codex_app_server":
- _codex_session = getattr(self, "_codex_session", None)
- _request_interrupt = getattr(_codex_session, "request_interrupt", None)
- if callable(_request_interrupt):
- try:
- _request_interrupt()
- except Exception:
- logger.debug(
- "Failed to interrupt Codex app-server turn",
- exc_info=True,
- )
+ # Codex watches a private interrupt event rather than Hermes' per-thread flag.
+ _request_interrupt = _ic_codex_method(self, "request_interrupt")
+ if _request_interrupt is not None:
+ try:
+ _request_interrupt()
+ except Exception:
+ logger.debug("Failed to interrupt Codex app-server turn", exc_info=True)
# Cron turns request on the conversation thread (no nested interrupt-worker deadlock); their client
# is registered here so this cross-thread interrupt can still shut the sockets.
- _abort_active_request = getattr(self, "_active_request_abort", None)
- if callable(_abort_active_request):
- try:
- _abort_active_request("interrupt_abort")
- except Exception:
- logger.debug("Failed to abort active inline request", exc_info=True)
+ _ic_abort_active_request(self, "interrupt_abort", "Failed to abort active inline request")
# Scope the tool interrupt to this agent's execution thread so other in-process agents are unaffected.
if self._execution_thread_id is not None:
- _set_interrupt(
- True,
- self._execution_thread_id,
- reason=tool_interrupt_reason,
- )
+ _set_interrupt(True, self._execution_thread_id, reason=tool_interrupt_reason)
self._interrupt_thread_signal_pending = False
else:
# Interrupt arrived before run_conversation bound the execution thread: defer the tool-level
# signal instead of targeting the caller thread.
self._interrupt_thread_signal_pending = True
- # Fan out to concurrent-tool worker tids: is_interrupted() inside a tool only sees its own tid, so
- # without this a hung concurrent tool runs to its own timeout. getattr covers __init__-less stubs.
- _tracker = getattr(self, "_tool_worker_threads", None)
- _tracker_lock = getattr(self, "_tool_worker_threads_lock", None)
- if _tracker is not None and _tracker_lock is not None:
- with _tracker_lock:
- _worker_tids = list(_tracker)
- for _wtid in _worker_tids:
- try:
- _set_interrupt(True, _wtid, reason=tool_interrupt_reason)
- except Exception:
- pass
+ _ic_signal_tool_workers(self, True, reason=tool_interrupt_reason)
# Propagate interrupt to any running child agents (subagent delegation)
with self._active_children_lock:
children_copy = list(self._active_children)
for child in children_copy:
try:
if hard_cancel:
- request_hard_interrupt(
- child,
- message,
- tool_reason=tool_interrupt_reason,
- )
+ request_hard_interrupt(child, message, tool_reason=tool_interrupt_reason)
else:
child.interrupt(message)
except Exception as e:
@@ -231,12 +206,7 @@ class InterruptControlMixin:
Frontends feature-detect this and fall back to legacy ``interrupt()`` for third-party agents.
"""
# Bypass dynamic dispatch: legacy subclasses may override interrupt(message=None) without hard_cancel.
- InterruptControlMixin.interrupt(
- self,
- message,
- hard_cancel=True,
- tool_reason=tool_reason,
- )
+ InterruptControlMixin.interrupt(self, message, hard_cancel=True, tool_reason=tool_reason)
def clear_interrupt(self, *, preserve_redirect: bool = False) -> bool:
"""Clear the interrupt request and per-thread tool signal.
@@ -244,18 +214,7 @@ class InterruptControlMixin:
``preserve_redirect`` is only for the conversation loop rebuilding the same logical turn after
cancelling a model request; public hard-stop paths clear everything.
"""
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is not None:
- with _redirect_lock:
- if preserve_redirect and not self._pending_redirect:
- return False
- self._interrupt_requested = False
- self._interrupt_message = None
- self._tool_interrupt_reason = None
- getattr(self, "_hard_interrupt_requested", threading.Event()).clear()
- if not preserve_redirect:
- self._pending_redirect = None
- else:
+ with _ic_lock(self, "_pending_redirect_lock"):
if preserve_redirect and not getattr(self, "_pending_redirect", None):
return False
self._interrupt_requested = False
@@ -267,23 +226,10 @@ class InterruptControlMixin:
self._interrupt_thread_signal_pending = False
if self._execution_thread_id is not None:
_set_interrupt(False, self._execution_thread_id)
- # Also clear worker-thread bits so no stale interrupt survives a turn boundary onto a recycled tid.
- # getattr covers __init__-less test stubs.
- _tracker = getattr(self, "_tool_worker_threads", None)
- _tracker_lock = getattr(self, "_tool_worker_threads_lock", None)
- if _tracker is not None and _tracker_lock is not None:
- with _tracker_lock:
- _worker_tids = list(_tracker)
- for _wtid in _worker_tids:
- try:
- _set_interrupt(False, _wtid)
- except Exception:
- pass
+ _ic_signal_tool_workers(self, False)
# A hard interrupt supersedes any pending /steer — its target iteration will no longer happen.
- _steer_lock = getattr(self, "_pending_steer_lock", None)
- if _steer_lock is not None:
- with _steer_lock:
- self._pending_steer = None
+ with _ic_lock(self, "_pending_steer_lock"):
+ self._pending_steer = None
return True
def steer(self, text: str) -> bool:
@@ -295,17 +241,9 @@ class InterruptControlMixin:
if not text or not text.strip():
return False
cleaned = text.strip()
- _lock = getattr(self, "_pending_steer_lock", None)
- if _lock is None:
- # __init__-less test stubs: fall back to a direct attribute set.
+ with _ic_lock(self, "_pending_steer_lock"):
existing = getattr(self, "_pending_steer", None)
self._pending_steer = (existing + "\n" + cleaned) if existing else cleaned
- return True
- with _lock:
- if self._pending_steer:
- self._pending_steer = self._pending_steer + "\n" + cleaned
- else:
- self._pending_steer = cleaned
return True
def redirect(self, text: str) -> bool:
@@ -322,59 +260,35 @@ class InterruptControlMixin:
# Codex owns its internal reasoning/tool loop, so use its first-class
# active-turn steering protocol rather than interrupting the subprocess.
- if getattr(self, "api_mode", None) == "codex_app_server":
- _codex_session = getattr(self, "_codex_session", None)
- _native_steer = getattr(_codex_session, "request_steer", None)
- if callable(_native_steer):
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is not None:
- with _redirect_lock:
- if self._interrupt_requested:
- return False
- elif self._interrupt_requested:
- return False
- try:
- return bool(_native_steer(cleaned))
- except Exception:
- logger.debug("Codex app-server turn/steer failed", exc_info=True)
+ _native_steer = _ic_codex_method(self, "request_steer")
+ if _native_steer is not None:
+ with _ic_lock(self, "_pending_redirect_lock"):
+ if self._interrupt_requested:
return False
+ try:
+ return bool(_native_steer(cleaned))
+ except Exception:
+ logger.debug("Codex app-server turn/steer failed", exc_info=True)
+ return False
# Never kill a tool to deliver guidance; the steer drain puts it on the final tool result.
if getattr(self, "_executing_tools", False):
return self.steer(cleaned)
_model_active = getattr(self, "_model_request_active", None)
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is None:
+ with _ic_lock(self, "_pending_redirect_lock"):
if _model_active is None or not _model_active.is_set():
+ # The response completed before we acquired the state lock.
+ # Reject so the surface queues a new turn.
return False
existing = getattr(self, "_pending_redirect", None)
if self._interrupt_requested and not existing:
return False
self._pending_redirect = (
- f"{existing}\n\n[Additional user correction]\n{cleaned}"
- if existing
- else cleaned
+ f"{existing}\n\n[Additional user correction]\n{cleaned}" if existing else cleaned
)
self._interrupt_requested = True
self._interrupt_message = None
- else:
- with _redirect_lock:
- if _model_active is None or not _model_active.is_set():
- # The response completed before we acquired the state lock.
- # Reject so the surface queues a new turn.
- return False
- if self._interrupt_requested and not self._pending_redirect:
- return False
- if self._pending_redirect:
- self._pending_redirect = (
- f"{self._pending_redirect}\n\n"
- f"[Additional user correction]\n{cleaned}"
- )
- else:
- self._pending_redirect = cleaned
- self._interrupt_requested = True
- self._interrupt_message = None
# Interrupt only the model request. Do not fan out to tool workers or
# child agents as interrupt() does.
@@ -384,42 +298,24 @@ class InterruptControlMixin:
self._interrupt_thread_signal_pending = False
else:
self._interrupt_thread_signal_pending = True
- _abort_active_request = getattr(self, "_active_request_abort", None)
- if callable(_abort_active_request):
- try:
- _abort_active_request("redirect_abort")
- except Exception:
- logger.debug("Failed to abort request for redirect", exc_info=True)
+ _ic_abort_active_request(self, "redirect_abort", "Failed to abort request for redirect")
return True
def _has_pending_redirect(self) -> bool:
"""Return whether an active-turn redirect is waiting to be applied."""
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is None:
+ with _ic_lock(self, "_pending_redirect_lock"):
return bool(getattr(self, "_pending_redirect", None))
- with _redirect_lock:
- return bool(self._pending_redirect)
def _drain_pending_redirect(self) -> Optional[str]:
"""Return and clear pending active-turn correction text."""
- _redirect_lock = getattr(self, "_pending_redirect_lock", None)
- if _redirect_lock is None:
+ with _ic_lock(self, "_pending_redirect_lock"):
text = getattr(self, "_pending_redirect", None)
self._pending_redirect = None
- return text
- with _redirect_lock:
- text = self._pending_redirect
- self._pending_redirect = None
return text
def _drain_pending_steer(self) -> Optional[str]:
"""Return the pending steer text (if any) and clear the slot; None when nothing is pending."""
- _lock = getattr(self, "_pending_steer_lock", None)
- if _lock is None:
+ with _ic_lock(self, "_pending_steer_lock"):
text = getattr(self, "_pending_steer", None)
self._pending_steer = None
- return text
- with _lock:
- text = self._pending_steer
- self._pending_steer = None
return text