Skip to content

The agent that grows with you

fix: foreground priority for background review cancel timeout

kshitijk4poorb883756b7937
diff --git a/agent/background_review.py b/agent/background_review.py
index 51e20e92daa..42979f3bd05 100644
--- a/agent/background_review.py
+++ b/agent/background_review.py
@@ -113,7 +113,13 @@ def finish_background_review_run(
def _interrupt_background_review(review_agent: Any) -> None:
- """Request abort off-thread so a broken abort hook cannot stall foreground."""
+ """Request abort off-thread so a broken abort hook cannot stall foreground.
+
+ The bounded wait on ``request_done`` in
+ :func:`cancel_background_review_for_live_turn` is only effective if
+ ``interrupt()`` returns quickly. Off-loading to a daemon thread ensures
+ a slow or wedged abort path cannot block the foreground turn (#84423).
+ """
def _interrupt() -> None:
try:
@@ -137,11 +143,13 @@ def _interrupt_background_review(review_agent: Any) -> None:
)
-def cancel_background_review_for_live_turn(agent: Any) -> bool:
+def cancel_background_review_for_live_turn(agent: Any) -> None:
"""Cancel the current review and await its request-phase acknowledgement.
- Returns ``False`` when acknowledgement is unavailable or misses the bounded
- deadline. Callers must then stop before same-session turn-context work.
+ Foreground priority is preserved: if the review does not acknowledge within
+ the bounded deadline, a warning is logged and the live turn proceeds
+ anyway. The review is non-critical self-improvement work and must never
+ block a user-facing turn (#84423).
"""
lock = getattr(agent, "_background_review_lock", None)
if lock is not None:
@@ -154,9 +162,9 @@ def cancel_background_review_for_live_turn(agent: Any) -> bool:
if run is None:
if legacy_agent is None:
- return True
+ return
_interrupt_background_review(legacy_agent)
- return False
+ return
review_agent = run.cancel()
if review_agent is not None:
@@ -166,12 +174,11 @@ def cancel_background_review_for_live_turn(agent: Any) -> bool:
timeout=_BACKGROUND_REVIEW_CANCEL_TIMEOUT_SECONDS
)
if not acknowledged:
- logger.error(
+ logger.warning(
"Background review did not acknowledge cancellation within %.1fs; "
- "refusing to start overlapping live turn",
+ "proceeding with foreground live turn",
_BACKGROUND_REVIEW_CANCEL_TIMEOUT_SECONDS,
)
- return acknowledged
# ---------------------------------------------------------------------------
diff --git a/run_agent.py b/run_agent.py
index 107c0f33ab7..62377381419 100644
--- a/run_agent.py
+++ b/run_agent.py
@@ -8496,28 +8496,11 @@ class AIAgent:
# A review deliberately shares this agent's session_id for prompt-cache
# parity. Fence review startup or interrupt an admitted request, then
# await that request's exit before opening any live-turn Relay or task
- # instrumentation for the same session.
+ # instrumentation for the same session. Foreground priority is retained
+ # if the review does not acknowledge within the bounded deadline (#84423).
from agent.background_review import cancel_background_review_for_live_turn
- if not cancel_background_review_for_live_turn(self):
- failure = (
- "Live turn not started: the prior background review did not "
- "acknowledge cancellation before the bounded safety deadline. "
- "Retry after the review has stopped."
- )
- existing_messages = conversation_history
- if existing_messages is None:
- existing_messages = getattr(self, "_session_messages", None)
- return {
- "final_response": failure,
- "messages": list(existing_messages or []),
- "completed": False,
- "api_calls": 0,
- "error": failure,
- "failed": True,
- "retryable": True,
- "background_review_cancellation_timeout": True,
- }
+ cancel_background_review_for_live_turn(self)
from agent.aux_accounting import (
reset_accounting_context,
diff --git a/tests/run_agent/test_background_review.py b/tests/run_agent/test_background_review.py
index b1ef719005f..930dfb74280 100644
--- a/tests/run_agent/test_background_review.py
+++ b/tests/run_agent/test_background_review.py
@@ -585,8 +585,10 @@ def test_live_turn_cancels_review_during_startup_before_provider(monkeypatch):
assert live_result == {"boundary_reached": True}
-def test_live_turn_stops_safely_when_review_acknowledgement_times_out(monkeypatch):
- """A broken review abort path must not create an unbounded foreground wait."""
+def test_live_turn_proceeds_when_review_acknowledgement_times_out(monkeypatch):
+ """A broken review abort path must not block the foreground indefinitely.
+ The live turn proceeds after the bounded wait, retaining foreground priority.
+ """
import time
import agent.background_review as background_review_module
@@ -637,11 +639,15 @@ def test_live_turn_stops_safely_when_review_acknowledgement_times_out(monkeypatc
relay_calls = _install_relay_recorder(monkeypatch, run)
started = time.monotonic()
- result = AIAgent.run_conversation(
- agent,
- "next turn",
- task_id="live-task",
+ live_result = {}
+ live = _REAL_THREAD(
+ target=_run_wrapped_live_turn_to_boundary,
+ args=(agent, live_result),
+ daemon=True,
)
+ live.start()
+ live.join(timeout=5.0)
+
elapsed = time.monotonic() - started
allow_interrupt_return.set()
@@ -652,17 +658,21 @@ def test_live_turn_stops_safely_when_review_acknowledgement_times_out(monkeypatc
assert interrupt_entered.is_set()
assert interrupt_returned.wait(2.0)
assert not worker.is_alive()
- assert relay_calls == []
- assert boundary_calls == []
- assert result is not None
- assert result["completed"] is False
- assert result["failed"] is True
- assert result["background_review_cancellation_timeout"] is True
- assert "not started" in result["final_response"].lower()
+ assert not live.is_alive()
+ # Foreground retains priority: Relay/turn-context proceed even though
+ # the review did not acknowledge within the bounded deadline.
+ assert boundary_calls == [True]
+ assert live_result == {"boundary_reached": True}
+ assert relay_calls == [
+ ("acquire", False),
+ ("begin", False),
+ ("start_task_run", False),
+ ]
+ assert agent.session_id == "test-session"
-def test_live_turn_fails_closed_for_untracked_legacy_review_stub(monkeypatch):
- """Directly constructed agents without a handshake must not overlap turns."""
+def test_live_turn_interrupts_legacy_review_but_keeps_foreground_priority(monkeypatch):
+ """Legacy stubs are interrupted without turning review into a user blocker."""
interrupts = []
interrupt_called = threading.Event()
@@ -680,18 +690,26 @@ def test_live_turn_fails_closed_for_untracked_legacy_review_stub(monkeypatch):
)
relay_calls = _install_relay_recorder(monkeypatch)
- result = AIAgent.run_conversation(
- agent,
- "next turn",
- task_id="live-task",
+ live_result = {}
+ live = _REAL_THREAD(
+ target=_run_wrapped_live_turn_to_boundary,
+ args=(agent, live_result),
+ daemon=True,
)
+ live.start()
+ live.join(timeout=5.0)
assert interrupt_called.wait(2.0)
assert interrupts == ["superseded by a new live turn"]
- assert relay_calls == []
- assert boundary_calls == []
- assert result["background_review_cancellation_timeout"] is True
- assert result["failed"] is True
+ assert not live.is_alive()
+ assert boundary_calls == [True]
+ assert live_result == {"boundary_reached": True}
+ assert relay_calls == [
+ ("acquire", False),
+ ("begin", False),
+ ("start_task_run", False),
+ ]
+ assert agent.session_id == "test-session"
def test_stale_review_cleanup_cannot_clear_or_signal_newer_review(monkeypatch):