Skip to content

The agent that grows with you

fix(tests): de-flake gateway SSE and goal-verdict tests that raced spawned tasks (#88975)

Tekniuma75d1b5c8921
diff --git a/tests/gateway/test_goal_verdict_send.py b/tests/gateway/test_goal_verdict_send.py
index 4b8912b2e21..9c24526c510 100644
--- a/tests/gateway/test_goal_verdict_send.py
+++ b/tests/gateway/test_goal_verdict_send.py
@@ -96,6 +96,18 @@ def _make_runner_with_adapter(session_id: str = None):
return runner, adapter, session_entry, src
+async def _drain_until(condition, timeout=5.0):
+ """Yield to the event loop until ``condition()`` is truthy (bounded).
+
+ The goal-continuation path finishes its sends/enqueues on spawned tasks;
+ a fixed 0.05s sleep raced them on loaded CI runners (#88975). Returns as
+ soon as the condition holds — the asserts after the call stay exact.
+ """
+ deadline = asyncio.get_event_loop().time() + timeout
+ while not condition() and asyncio.get_event_loop().time() < deadline:
+ await asyncio.sleep(0.01)
+
+
@pytest.mark.asyncio
async def test_goal_verdict_continue_enqueues_continuation(hermes_home):
"""When the judge says continue, both the 'continuing' status and the
@@ -115,7 +127,7 @@ async def test_goal_verdict_continue_enqueues_continuation(hermes_home):
source=src,
final_response="here's a partial edit",
)
- await asyncio.sleep(0.05)
+ await _drain_until(lambda: adapter.sends and adapter._pending_messages)
# Status line sent back
assert len(adapter.sends) == 1
@@ -143,7 +155,7 @@ async def test_goal_verdict_budget_exhausted_sends_pause(hermes_home):
source=src,
final_response="still partial",
)
- await asyncio.sleep(0.05)
+ await _drain_until(lambda: adapter.sends)
assert len(adapter.sends) == 1
content = adapter.sends[0]["content"]
diff --git a/tests/gateway/test_session_api.py b/tests/gateway/test_session_api.py
index 95b44aead48..f1db8f03c0c 100644
--- a/tests/gateway/test_session_api.py
+++ b/tests/gateway/test_session_api.py
@@ -450,6 +450,10 @@ async def test_session_chat_stream_treats_pre_existing_poisoned_row_as_no_model(
json={"message": "hi"},
)
assert resp.status == 200
+ # Drain the SSE body: the 200 lands before the streaming task
+ # invokes _run_agent, so asserting on call_args without reading
+ # the body races the handler (flaked on loaded CI runners).
+ await resp.text()
_, kwargs = mock_run.call_args
assert kwargs["session_model"] is None