Skip to content

The agent that grows with you

fix(gateway): validate human delay bounds

lorenzozaneeb6a5b41e2331
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 092decb7474..49f4ddcfcf5 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -4012,6 +4012,8 @@ class BasePlatformAdapter(ABC):
if mode != "natural": # custom mode tolerates malformed env vars
lo = _or_default(lambda: int(os.getenv("HERMES_HUMAN_DELAY_MIN_MS", str(lo))), lo)
hi = _or_default(lambda: int(os.getenv("HERMES_HUMAN_DELAY_MAX_MS", str(hi))), hi)
+ if lo < 0 or hi < 0 or lo > hi:
+ lo, hi = 800, 2500
return random.uniform(lo / 1000.0, hi / 1000.0)
async def _synthesize_auto_tts(self, text_content: str) -> Tuple[List[str], Optional[str]]:
diff --git a/tests/gateway/test_platform_base.py b/tests/gateway/test_platform_base.py
index 15d4abd2c53..8609fecfb23 100644
--- a/tests/gateway/test_platform_base.py
+++ b/tests/gateway/test_platform_base.py
@@ -1184,6 +1184,45 @@ class TestGetHumanDelay:
delay = BasePlatformAdapter._get_human_delay()
assert 0.8 <= delay <= 2.5
+ def test_custom_mode_rejects_negative_bounds(self):
+ env = {
+ "HERMES_HUMAN_DELAY_MODE": "custom",
+ "HERMES_HUMAN_DELAY_MIN_MS": "-1",
+ "HERMES_HUMAN_DELAY_MAX_MS": "1000",
+ }
+ with patch.dict(os.environ, env), patch(
+ "gateway.platforms.base.random.uniform", return_value=1.0
+ ) as uniform:
+ delay = BasePlatformAdapter._get_human_delay()
+ assert delay == 1.0
+ uniform.assert_called_once_with(0.8, 2.5)
+
+ def test_custom_mode_rejects_inverted_bounds(self):
+ env = {
+ "HERMES_HUMAN_DELAY_MODE": "custom",
+ "HERMES_HUMAN_DELAY_MIN_MS": "3000",
+ "HERMES_HUMAN_DELAY_MAX_MS": "1000",
+ }
+ with patch.dict(os.environ, env), patch(
+ "gateway.platforms.base.random.uniform", return_value=1.0
+ ) as uniform:
+ delay = BasePlatformAdapter._get_human_delay()
+ assert delay == 1.0
+ uniform.assert_called_once_with(0.8, 2.5)
+
+ def test_custom_mode_preserves_valid_bounds(self):
+ env = {
+ "HERMES_HUMAN_DELAY_MODE": "custom",
+ "HERMES_HUMAN_DELAY_MIN_MS": "1000",
+ "HERMES_HUMAN_DELAY_MAX_MS": "2000",
+ }
+ with patch.dict(os.environ, env), patch(
+ "gateway.platforms.base.random.uniform", return_value=1.5
+ ) as uniform:
+ delay = BasePlatformAdapter._get_human_delay()
+ assert delay == 1.5
+ uniform.assert_called_once_with(1.0, 2.0)
+
# ---------------------------------------------------------------------------
# utf16_len / _prefix_within_utf16_limit / truncate_message with len_fn