Skip to content

The agent that grows with you

fix(clarify): a Stop or gateway shutdown withdraws a pending clarify wait

Tekniumcd4f93cca4e9
diff --git a/tests/tools/test_clarify_gateway.py b/tests/tools/test_clarify_gateway.py
index a73e4e2017f..1a038fc304c 100644
--- a/tests/tools/test_clarify_gateway.py
+++ b/tests/tools/test_clarify_gateway.py
@@ -266,6 +266,33 @@ class TestUnlimitedWait:
assert not t.is_alive()
assert result_box["r"] == "B"
+ def test_interrupt_on_waiting_thread_withdraws_unlimited_wait(self):
+ """/stop or gateway shutdown signals the turn's thread; the wait is released as cancelled
+ (as the approval wait is) instead of holding the turn for the whole clarify timeout."""
+ from tools import clarify_gateway as cm
+ from tools.interrupt import set_interrupt
+
+ cm.register("u2", "sk", "Q?", ["A", "B"])
+ box = {}
+
+ def waiter():
+ box["tid"] = threading.get_ident()
+ box["r"] = cm.wait_for_response("u2", timeout=0)
+
+ t = threading.Thread(target=waiter, daemon=True) # a regression must fail, not hang exit
+ t.start()
+ while "tid" not in box:
+ time.sleep(0.01)
+ try:
+ set_interrupt(True, box["tid"], reason="gateway shutdown")
+ t.join(timeout=5.0)
+ finally:
+ set_interrupt(False, box["tid"])
+ assert not t.is_alive()
+ assert box["r"] == cm.CANCELLED
+ assert not cm.has_pending("sk")
+ assert cm.resolve_gateway_clarify("u2", "late") is False
+
class TestMultiSelectTextFallback:
"""Multi-select clarifies via the gateway text fallback.
diff --git a/tools/clarify_gateway.py b/tools/clarify_gateway.py
index 67761db098c..4b0b610fa7b 100644
--- a/tools/clarify_gateway.py
+++ b/tools/clarify_gateway.py
@@ -70,12 +70,21 @@ def wait_for_response(clarify_id: str, timeout: float) -> Optional[str]:
from tools.environments.base import touch_activity_if_due
except Exception: # pragma: no cover - optional
touch_activity_if_due = None
+ from tools.interrupt import is_interrupted
deadline = None if timeout is None or float(timeout) <= 0.0 else time.monotonic() + float(timeout)
activity_state = {"last_touch": time.monotonic(), "start": time.monotonic()}
while True:
remaining = 1.0 if deadline is None else deadline - time.monotonic()
if remaining <= 0 or entry.event.wait(timeout=min(1.0, remaining)):
break
+ if is_interrupted():
+ # /stop or gateway shutdown on this turn's thread: withdraw the prompt the way the
+ # approval wait does, or a planned stop sits out the whole clarify timeout and the
+ # turn is left started (unknown after restart). A real answer that won the race stays.
+ with _lock:
+ if not entry.event.is_set():
+ entry.response = CANCELLED
+ break
# Periodic activity touch so the gateway's inactivity timeout doesn't kill the agent during long
# code execution (#10807).
if touch_activity_if_due is not None: