Files
hermes-webui/tests/test_live_rebuild_budget_warn_rate_limit.py
nesquena-hermes 26e133e3e8
Some checks failed
Release & Docker / release (push) Has been cancelled
[HELD — independent review pending] Release v0.51.340 — bg_task agent wakeup (trio #2968+#2971+#2979) (#3867)
* stage bg_task trio combined (#2979 superset) on master for deep review

* fix(bg_task): unsubscribe SessionChannel on header-write failure (Codex deep-review catch) + regression test

* test: realign on-subscribe-recovery anchor to subscribe_to_session_channel after leak fix

* CHANGELOG: bg_task trio as v0.51.340 LD (HELD pending independent review)

* bg_task trio: apply 3 independent-review (greptile) fixes

1. start_session_turn now threads the session PROFILE model defaults
   (_read_profile_model_config) into the wakeup model-resolve, so a brand-new
   session with an empty model falls back to the profile default not global
   DEFAULT_MODEL. Updated the white-box spy test signature accordingly.
2. /api/session/stream omits the Connection header (HTTP/1.1 keep-alive
   default) to match the #3103 long-lived-SSE pattern.
3. Reaper now prunes _LAST_EMIT_TS for collected sessions so the coalesce
   timestamp map can't grow one permanent entry per session forever.

nesquena APPROVED the PR; these are the 3 non-blocking greptile suggestions.

* test: realign _start_session_turn adapter stub lambda to new profile-defaults signature
2026-06-08 22:36:18 -07:00

60 lines
2.4 KiB
Python

"""Live-rebuild-budget warning rate-limit — Q-2979-A3.
Per Copilot discussion_r3305864400 the budget-exceeded warning at
api/config.py is potentially high-volume: a hung upstream probe or a sustained
burst of cold callers could flood the log at warning level. The fix wraps the
warning with ``_should_warn_budget``: the FIRST hit in a cooldown window logs
at warning, subsequent hits in the same window log at info — so the signal is
retained but the volume is bounded.
"""
from __future__ import annotations
import time
def test_should_warn_budget_first_call_returns_true():
from api import config as cfg
# Isolate per-test state.
cfg._BUDGET_WARN_STATE.pop("unit-test-reason-A", None)
assert cfg._should_warn_budget("unit-test-reason-A", cooldown_s=300.0) is True
def test_should_warn_budget_inside_cooldown_returns_false():
from api import config as cfg
cfg._BUDGET_WARN_STATE.pop("unit-test-reason-B", None)
assert cfg._should_warn_budget("unit-test-reason-B", cooldown_s=300.0) is True
# Second hit within cooldown — must be False (caller should demote to info).
assert cfg._should_warn_budget("unit-test-reason-B", cooldown_s=300.0) is False
assert cfg._should_warn_budget("unit-test-reason-B", cooldown_s=300.0) is False
def test_should_warn_budget_after_cooldown_returns_true_again():
from api import config as cfg
cfg._BUDGET_WARN_STATE.pop("unit-test-reason-C", None)
assert cfg._should_warn_budget("unit-test-reason-C", cooldown_s=0.05) is True
assert cfg._should_warn_budget("unit-test-reason-C", cooldown_s=0.05) is False
time.sleep(0.1)
# Cooldown elapsed — warning level resumes.
assert cfg._should_warn_budget("unit-test-reason-C", cooldown_s=0.05) is True
def test_should_warn_budget_distinct_reasons_have_independent_windows():
from api import config as cfg
for k in ("unit-test-reason-D1", "unit-test-reason-D2"):
cfg._BUDGET_WARN_STATE.pop(k, None)
assert cfg._should_warn_budget("unit-test-reason-D1", cooldown_s=300.0) is True
# A different reason MUST get its own first-hit warning even while D1 is
# still inside cooldown.
assert cfg._should_warn_budget("unit-test-reason-D2", cooldown_s=300.0) is True
# Both are now inside cooldown — both demote to info.
assert cfg._should_warn_budget("unit-test-reason-D1", cooldown_s=300.0) is False
assert cfg._should_warn_budget("unit-test-reason-D2", cooldown_s=300.0) is False