Skip to content

The agent that grows with you

fix(gateway): register hygiene_max_turn_hold_seconds + flat retry-after on turn-hold abandonment

Kshitij Kapoor2abf72ad971c
diff --git a/gateway/run.py b/gateway/run.py
index 025c64853e6..ae2554ae647 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -162,6 +162,14 @@ _HYGIENE_COOLDOWN_LADDER_MULTIPLIERS = (1, 3, 9)
# retry is cheap and still recovers within a session.
_HYGIENE_COOLDOWN_MAX_SECONDS = 3600.0
+# Flat retry-after recorded when a hygiene compression is ABANDONED because
+# the turn-hold budget expired while the summary was still streaming (not a
+# failure — the compressor was healthy, we just could not keep holding the
+# user's turn). Spaces out re-attempts so sustained traffic does not spawn,
+# hold, and cancel a fresh compressor on every single turn; deliberately
+# outside the failure-streak ladder above.
+_HYGIENE_TURNHOLD_RETRY_SECONDS = 60.0
+
def _hygiene_cooldown_for_failure(
gateway,
@@ -20661,6 +20669,23 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
context="session hygiene turn-hold",
)
_hyg_cleanup_deferred = True
+ # Short, NON-escalating retry-after. Without
+ # it, every subsequent turn re-spawns a fresh
+ # compressor, holds it for the turn-hold
+ # budget, and cancels it again — a per-turn
+ # summary-model token burn that never commits
+ # under sustained traffic. This is deliberately
+ # NOT _hygiene_cooldown_for_failure: the
+ # compressor is healthy, so the failure streak
+ # must not advance (behavior witness below);
+ # only the flat retry spacing is recorded.
+ _record_hygiene_cooldown(
+ self, session_entry.session_id,
+ _HYGIENE_TURNHOLD_RETRY_SECONDS,
+ "hygiene compression deferred: "
+ "turn-hold budget expired while the "
+ "summary was still streaming",
+ )
from agent.session_activity import (
ActivityProvenance,
)
diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py
index 864e9fe6899..ebc3825bad0 100644
--- a/hermes_cli/config_defaults.py
+++ b/hermes_cli/config_defaults.py
@@ -901,6 +901,13 @@ DEFAULT_CONFIG = {
# while tokens are still moving — bounds a degenerate
# trickle stream. Clamped to >= hygiene_timeout_seconds.
"hygiene_failure_cooldown_seconds": 300, # skip repeated failed hygiene attempts for this session
+ "hygiene_max_turn_hold_seconds": 10, # max seconds an ARRIVING user turn is held while a
+ # still-streaming hygiene summary finishes. Distinct from
+ # hygiene_timeout_seconds (compressor inactivity budget):
+ # this bounds user-visible latency once real input is
+ # waiting. Kept well under chat-transport idle timeouts
+ # (Telegram ~30s). On expiry the turn proceeds
+ # uncompressed — an availability boundary, not a failure.
"context_timeout_seconds": 120, # inactivity budget for in-agent compress_context
# (conversation loop, /compress, preflight, etc.).
# Same progress-aware semantics as hygiene_timeout_seconds:
diff --git a/tests/gateway/test_session_hygiene.py b/tests/gateway/test_session_hygiene.py
index 0bc5d29e65d..54f440bc0a1 100644
--- a/tests/gateway/test_session_hygiene.py
+++ b/tests/gateway/test_session_hygiene.py
@@ -800,16 +800,26 @@ async def test_session_hygiene_turn_hold_budget_abandons_streaming_wait(
for c in sent_contents
), f"turn-hold must send deferral notice, got: {sent_contents}"
- # Behavior witness 2: turn-hold must NOT advance the failure cooldown.
+ # Behavior witness 2: turn-hold must NOT advance the failure STREAK.
fake_db.get_compression_failure_cooldown.assert_called()
- # The cooldown is only *read* (to check for existing), never *written*
- # by the turn-hold path. The idle-timeout path would call
- # _hygiene_cooldown_for_failure + _record_hygiene_cooldown.
- # We verify by checking the DB mock was not asked to persist a new
- # failure streak.
- assert not hasattr(fake_db, 'set_compression_failure_cooldown') or \
- not fake_db.set_compression_failure_cooldown.called, \
- "turn-hold must not write failure cooldown"
+ # The escalating ladder (x1, x3, x9) is reserved for real failures via
+ # _hygiene_cooldown_for_failure -> increment_hygiene_failure_streak.
+ # The turn-hold path records only a flat, non-escalating retry-after
+ # (spacing out re-attempts so sustained traffic does not spawn and
+ # cancel a fresh compressor every turn) and must never touch the streak.
+ assert not fake_db.increment_hygiene_failure_streak.called, \
+ "turn-hold must not advance the failure streak"
+ assert fake_db.record_compression_failure_cooldown.called, \
+ "turn-hold must record the flat retry-after spacing"
+ _th_args = fake_db.record_compression_failure_cooldown.call_args[0]
+ import time as _time_mod
+ _th_retry = _th_args[1] - _time_mod.time()
+ assert _th_retry <= 120, (
+ f"turn-hold retry-after must stay flat (~60s), got {_th_retry:.0f}s "
+ "— escalating ladder leaked into the deferral path"
+ )
+ assert "turn-hold" in (_th_args[2] or ""), \
+ "retry-after reason must name the turn-hold deferral"
# Behavior witness 3: the #87011 contract remains truthful —
# "session hygiene compression timed out" still means a real idle