Skip to content

The agent that grows with you

test: shrink the goal-DB timing tests to sub-second wall time

Teknium9d86ac62b41d
diff --git a/tests/gateway/test_goal_max_turns_config.py b/tests/gateway/test_goal_max_turns_config.py
index f98eddb1d1e..4e4f8657d93 100644
--- a/tests/gateway/test_goal_max_turns_config.py
+++ b/tests/gateway/test_goal_max_turns_config.py
@@ -76,22 +76,26 @@ async def test_gateway_goal_uses_goals_max_turns_from_full_config(tmp_path, monk
@pytest.mark.asyncio
-async def test_goal_command_slow_db_init_keeps_loop_free_and_persists(tmp_path, monkeypatch):
- """A slow state.db init (cold cache, first /goal of the process)
- must not freeze the event loop or silently drop the goal write.
- The cache is warmed off-loop, so the reply is honest at any init
- duration. Review follow-up (#88965): the bootstrap window alone
- froze the loop for the init duration and still dropped the write
- past ~1.5s."""
+async def test_goal_command_slow_db_init_still_persists(tmp_path, monkeypatch):
+ """A slow state.db init (cold cache, first /goal of the process) must
+ not silently drop the goal write: the gateway warms the cache off-loop,
+ so the reply is honest at any init duration.
+
+ The init is deliberately slower than the (shrunk) bootstrap init
+ window, so the window-only path would drop the write — this test
+ discriminates the off-loop warm-up from mere window-widening without
+ multi-second sleeps. Loop-freeze bounds are covered separately in
+ tests/hermes_cli/test_goals_db_bootstrap_off_loop.py; no wall-clock
+ gap assertions here (those are their own flake class).
+ """
import hermes_state
- # Past the init window: the window-only path expires its wait and
- # drops the write, so this test discriminates warm-up from windows.
- # The margin is 2.5s so the loop-gap ceiling below can be 2.0s (the
- # flake-policy floor) and an on-loop init still exceeds it.
- INIT_S = goals._DB_BOOTSTRAP_INIT_WAIT_S + 2.5
+ monkeypatch.setattr(goals, "_DB_BOOTSTRAP_INIT_WAIT_S", 0.2)
+ INIT_S = 0.8 # past the shrunk init window
- class _SlowSessionDB(hermes_state.SessionDB):
+ real_session_db = hermes_state.SessionDB
+
+ class _SlowSessionDB(real_session_db):
def __init__(self, *a, **k):
time.sleep(INIT_S)
super().__init__(*a, **k)
@@ -107,32 +111,12 @@ async def test_goal_command_slow_db_init_keeps_loop_free_and_persists(tmp_path,
runner = _make_runner()
event = _make_goal_event()
- gaps = {"max": 0.0}
- stop = asyncio.Event()
-
- async def _ticker():
- last = time.monotonic()
- while not stop.is_set():
- await asyncio.sleep(0.05)
- now = time.monotonic()
- gaps["max"] = max(gaps["max"], now - last)
- last = now
-
- ticker = asyncio.create_task(_ticker())
- await asyncio.sleep(0.15)
try:
response = await GatewayRunner._handle_goal_command(runner, event)
assert "⊙ Goal set (7-turn budget): ship the benchmark" in response
state = goals.GoalManager("sid-gateway-goal-config").state
assert state is not None, "goal write must persist even with a slow init"
- # 2.0s is the loose-bound floor from the flake policy. The init
- # (4.0s) runs off-loop, so an on-loop regression exceeds this
- # ceiling and a loaded runner does not.
- assert gaps["max"] < 2.0, (
- f"event loop frozen for {gaps['max']:.2f}s while the init ran off-loop"
- )
+ assert state.max_turns == 7
finally:
- stop.set()
- await ticker
goals._DB_CACHE.clear()
diff --git a/tests/hermes_cli/test_goals_db_bootstrap_off_loop.py b/tests/hermes_cli/test_goals_db_bootstrap_off_loop.py
index 12b747eab99..5bc8cae5bfe 100644
--- a/tests/hermes_cli/test_goals_db_bootstrap_off_loop.py
+++ b/tests/hermes_cli/test_goals_db_bootstrap_off_loop.py
@@ -112,15 +112,24 @@ def test_slow_construction_does_not_block_the_loop(monkeypatch):
"""A SessionDB whose init blocks (locked-DB migration) must not
stall the event loop. The kick call is bounded by the one-time init
window. Every later call is bounded by the short per-call window.
- Both calls degrade to None."""
+ Both calls degrade to None.
+
+ The windows are monkeypatched down so the test costs well under a
+ second of wall time instead of sleeping through the production
+ 1.5s window plus margins; the two-window CONTRACT is what's under
+ test, not the production constants.
+ """
import hermes_state
+ monkeypatch.setattr(goals, "_DB_BOOTSTRAP_INIT_WAIT_S", 0.3)
+ monkeypatch.setattr(goals, "_DB_BOOTSTRAP_LOOP_WAIT_S", 0.05)
+
class _BlockingDB:
def __init__(self):
- # Far past both wait windows. The margin keeps the "still
- # None" assertions from racing the bootstrap thread on a
- # loaded runner (negative-timing race, flake policy).
- time.sleep(6.0) # simulated contended migration
+ # Far past both (shrunk) wait windows. The margin keeps the
+ # "still None" assertions from racing the bootstrap thread on
+ # a loaded runner (negative-timing race, flake policy).
+ time.sleep(1.5) # simulated contended migration
def get_meta(self, key):
return None
@@ -142,15 +151,20 @@ def test_slow_construction_does_not_block_the_loop(monkeypatch):
asyncio.run(main())
assert result is None, "contended init must degrade to None"
- # The slack on each ceiling is 2.0s, the loose-bound floor from the
- # flake policy. The windows are 1.5s and 0.25s, so the two ceilings
- # stay far apart and a loaded runner does not cross either one.
- assert elapsed is not None and elapsed < goals._DB_BOOTSTRAP_INIT_WAIT_S + 2.0, (
+ # Each ceiling gets ~1s slack over its (shrunk) window; the 1.5s
+ # blocking init exceeds both ceilings if it ever runs on-loop, and a
+ # loaded runner does not cross either one.
+ assert elapsed is not None and elapsed < 1.0, (
f"kick call blocked for {elapsed:.2f}s — past the one-time init window"
)
# The in-flight call waits the short window, not the init window.
# A contended migration must not stall the loop repeatedly.
assert result2 is None, "contended init must keep degrading to None"
- assert elapsed2 is not None and elapsed2 < goals._DB_BOOTSTRAP_LOOP_WAIT_S + 2.0, (
+ assert elapsed2 is not None and elapsed2 < 1.0, (
f"in-flight call blocked for {elapsed2:.2f}s — watchdog territory"
)
+ # The kick call must wait the LONGER window: otherwise a healthy cold
+ # init loses its grace period and the first write drops again.
+ assert elapsed > elapsed2, (
+ "kick call should wait the one-time init window; in-flight calls the short one"
+ )