Skip to content

The agent that grows with you

test: causally synchronize recurring timing cases

ethernet14452bf88a9f
diff --git a/tests/agent/test_turn_liveness_watchdog.py b/tests/agent/test_turn_liveness_watchdog.py
index fd4d9a53767..d9946d08e55 100644
--- a/tests/agent/test_turn_liveness_watchdog.py
+++ b/tests/agent/test_turn_liveness_watchdog.py
@@ -219,18 +219,14 @@ def test_watchdog_force_aborts_silently_stalled_turn(watchdog_config, monkeypatc
agent = _agent_with_db(db)
interrupt_seen = {}
- t_start = time.time()
-
def stalled_loop(_agent, _message, _system, history, *_args, **_kwargs):
# Simulate the #95548 zombie: the loop makes no progress and never
# touches the activity clock. It only notices the watchdog's
# hard interrupt (real wedges may not even do that — see the lease
# test below).
- while not _agent._interrupt_requested:
- if time.time() - t_start > 10:
- break
- time.sleep(0.005)
- interrupt_seen["at"] = time.time()
+ assert _agent._hard_interrupt_requested.wait(30.0), (
+ "watchdog did not publish a hard interrupt"
+ )
interrupt_seen["message"] = _agent._interrupt_message
return {
"final_response": "aborted",
@@ -243,14 +239,9 @@ def test_watchdog_force_aborts_silently_stalled_turn(watchdog_config, monkeypatc
with caplog.at_level(logging.ERROR, logger="agent.turn_liveness"):
result = _run_turn(agent, stalled_loop, monkeypatch)
- elapsed = time.time() - t_start
-
# The turn was surfaced as interrupted, not hung.
assert result["interrupted"] is True
assert result["final_response"] == "aborted"
- # The watchdog fired before our 10s outer bound, and after the 0.3s idle
- # bound (poll interval makes the exact fire instant approximate).
- assert 0.2 <= elapsed < 10.0
# The stall was logged loudly with the session named.
assert any(
"Turn liveness watchdog fired" in record.getMessage()
diff --git a/tests/gateway/test_agents_command_delegations.py b/tests/gateway/test_agents_command_delegations.py
index ea833dce86e..d633343a123 100644
--- a/tests/gateway/test_agents_command_delegations.py
+++ b/tests/gateway/test_agents_command_delegations.py
@@ -48,31 +48,27 @@ class _Event:
async def test_agents_command_marks_stalling_delegation(monkeypatch):
monkeypatch.setattr(ad, "_STALE_CHECK_INTERVAL", 0.03)
monkeypatch.setattr(ad, "_STALE_IDLE_SECONDS", 0.1)
- # Long grace so the record stays in 'stalling' while we render.
- monkeypatch.setattr(ad, "_STALL_GRACE_SECONDS", 30.0)
+ # Force-finalization is a separate contract. Disable it so the stalling
+ # projection stays observable until this test releases the worker.
+ monkeypatch.setattr(ad, "_STALL_GRACE_SECONDS", float("inf"))
gate = threading.Event()
+ stalling = threading.Event()
+
+ def blocked_runner():
+ gate.wait()
+ return {}
res = ad.dispatch_async_delegation(
goal="wedged child", context=None, toolsets=None, role="leaf",
model="m", session_key="agent:main:test:dm:1", max_async_children=1,
- runner=lambda: {} if gate.wait(timeout=10) else {},
+ runner=blocked_runner,
+ interrupt_fn=stalling.set,
progress_fn=lambda: ((0, None), False),
)
assert res["status"] == "dispatched"
try:
- deadline = time.monotonic() + 5.0
- while time.monotonic() < deadline:
- items = ad.list_async_delegations()
- if any(
- d["delegation_id"] == res["delegation_id"]
- and d.get("status") == "stalling"
- for d in items
- ):
- break
- time.sleep(0.02)
- else:
- pytest.fail("delegation never reached stalling state")
+ assert stalling.wait(30.0), "delegation never reached stalling state"
runner = _make_runner()
out = await runner._handle_agents_command(_Event())
diff --git a/tests/gateway/test_clarify_delivery_fallback.py b/tests/gateway/test_clarify_delivery_fallback.py
index 3d429bc5df1..62d5400225c 100644
--- a/tests/gateway/test_clarify_delivery_fallback.py
+++ b/tests/gateway/test_clarify_delivery_fallback.py
@@ -27,6 +27,7 @@ class _CardAdapter(BasePlatformAdapter):
self.card = card
self.sent_text: list[str] = []
self.cards = 0
+ self.on_text_sent = None
def pause_typing_for_chat(self, chat_id):
return None
@@ -40,6 +41,9 @@ class _CardAdapter(BasePlatformAdapter):
async def send(self, chat_id, content, reply_to=None, metadata=None):
self.sent_text.append(content)
+ if self.on_text_sent is not None:
+ callback, self.on_text_sent = self.on_text_sent, None
+ callback()
return SendResult(success=True, message_id="t1")
async def retire_clarify_card(self, clarify_id, notice):
@@ -78,17 +82,8 @@ def _runner(adapter, loop, monkeypatch, timeout=5):
def _answer_once_text_prompt_is_seen(adapter, text):
- """Answer ONLY after the plain-text prompt was observed — never on a deadline, so a missing
- fallback leaves the waiter blocked and the test fails on elapsed time / sent_text, not luck."""
- def _wait_then_answer():
- deadline = time.monotonic() + 10
- while time.monotonic() < deadline and not adapter.sent_text:
- time.sleep(0.02)
- if not adapter.sent_text:
- return
- time.sleep(0.1)
- cm.resolve_text_response_for_session("sk-fallback", text)
- threading.Thread(target=_wait_then_answer, daemon=True).start()
+ """Answer causally from the adapter's plain-text send, never from a timing poll."""
+ adapter.on_text_sent = lambda: cm.resolve_text_response_for_session("sk-fallback", text)
# --- Atom: the platform rejects the native card -> plain-text question instead of a sentinel ---