`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.
Parallel directories for one source package, folded into the mirror:
tests/acp -> tests/acp_adapter (its __init__/conftest move with it)
tests/cli -> tests/hermes_cli (prompt_toolkit fixture merged into
hermes_cli/conftest.py)
tests/run_agent -> tests/agent (backoff fixture becomes
agent/conftest.py)
tests/relay -> tests/gateway/relay
tests/state -> tests/hermes_state
246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.
Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.
Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).
Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
157 lines
5.3 KiB
Python
157 lines
5.3 KiB
Python
"""Regression tests for #56391.
|
|
|
|
When context compression is in flight (state.db compression lock held),
|
|
gateway ``busy_input_mode='interrupt'`` must demote to queue semantics so a
|
|
rapid message burst cannot start a follow-up turn against the pre-rotation
|
|
parent and fork orphaned compression siblings.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import threading
|
|
import time
|
|
import types
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
_tg = types.ModuleType("telegram")
|
|
_tg.constants = types.ModuleType("telegram.constants")
|
|
_ct = MagicMock()
|
|
_ct.SUPERGROUP = "supergroup"
|
|
_ct.GROUP = "group"
|
|
_ct.PRIVATE = "private"
|
|
_tg.constants.ChatType = _ct
|
|
sys.modules.setdefault("telegram", _tg)
|
|
sys.modules.setdefault("telegram.constants", _tg.constants)
|
|
sys.modules.setdefault("telegram.ext", types.ModuleType("telegram.ext"))
|
|
|
|
from gateway.platforms.base import (
|
|
SessionSource,
|
|
build_session_key,
|
|
)
|
|
from gateway.platforms.event import MessageEvent, MessageType
|
|
from gateway.run import GatewayRunner, _AGENT_PENDING_SENTINEL # noqa: E402
|
|
|
|
|
|
def _make_event(text: str = "hello", chat_id: str = "123") -> MessageEvent:
|
|
source = SessionSource(
|
|
platform=MagicMock(value="telegram"),
|
|
chat_id=chat_id,
|
|
chat_type="private",
|
|
user_id="user1",
|
|
)
|
|
return MessageEvent(
|
|
text=text,
|
|
message_type=MessageType.TEXT,
|
|
source=source,
|
|
message_id="msg1",
|
|
)
|
|
|
|
|
|
def _make_runner(*, session_id: str = "parent-session") -> GatewayRunner:
|
|
runner = object.__new__(GatewayRunner)
|
|
runner._running_agents = {}
|
|
runner._running_agents_ts = {}
|
|
runner._pending_messages = {}
|
|
runner._busy_ack_ts = {}
|
|
runner._draining = False
|
|
runner.adapters = {}
|
|
runner.config = MagicMock()
|
|
runner.hooks = MagicMock()
|
|
runner.hooks.emit = AsyncMock()
|
|
runner.pairing_store = MagicMock()
|
|
runner.pairing_store.is_approved.return_value = True
|
|
runner._is_user_authorized = lambda _source: True
|
|
runner._busy_input_mode = "interrupt"
|
|
session_key = build_session_key(_make_event().source)
|
|
entry = SimpleNamespace(session_key=session_key, session_id=session_id)
|
|
session_store = SimpleNamespace(
|
|
_lock=threading.Lock(),
|
|
_entries={session_key: entry},
|
|
switch_session=MagicMock(),
|
|
)
|
|
session_store._ensure_loaded_locked = lambda: None
|
|
runner.session_store = session_store
|
|
runner._session_db = MagicMock()
|
|
runner._session_db._db = MagicMock()
|
|
runner._session_db._db.get_compression_lock_holder.return_value = None
|
|
return runner
|
|
|
|
|
|
def _make_adapter() -> MagicMock:
|
|
adapter = MagicMock()
|
|
adapter._pending_messages = {}
|
|
adapter._send_with_retry = AsyncMock()
|
|
adapter.config = MagicMock()
|
|
adapter.config.extra = {}
|
|
adapter.platform = MagicMock(value="telegram")
|
|
return adapter
|
|
|
|
|
|
def _make_parent_no_subagents() -> MagicMock:
|
|
parent = MagicMock()
|
|
parent._active_children = []
|
|
parent._active_children_lock = threading.Lock()
|
|
parent.get_activity_summary.return_value = {
|
|
"api_call_count": 3,
|
|
"max_iterations": 60,
|
|
"current_tool": "terminal",
|
|
}
|
|
return parent
|
|
|
|
|
|
class TestSessionHasCompressionInFlight:
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_returns_true_when_lock_held(self) -> None:
|
|
runner = _make_runner()
|
|
sk = build_session_key(_make_event().source)
|
|
runner._session_db._db.get_compression_lock_holder.return_value = "holder-1"
|
|
assert await runner._session_has_compression_in_flight(sk) is True
|
|
|
|
|
|
class TestBusyHandlerDemotesInterruptForCompression:
|
|
@pytest.mark.asyncio
|
|
async def test_does_not_interrupt_when_compression_in_flight(self) -> None:
|
|
runner = _make_runner()
|
|
adapter = _make_adapter()
|
|
event = _make_event(text="follow up during compression")
|
|
sk = build_session_key(event.source)
|
|
parent = _make_parent_no_subagents()
|
|
runner._running_agents[sk] = parent
|
|
runner.adapters[event.source.platform] = adapter
|
|
runner._session_db._db.get_compression_lock_holder.return_value = "compressing"
|
|
|
|
handled = await runner._handle_active_session_busy_message(event, sk)
|
|
|
|
assert handled is True
|
|
parent.interrupt.assert_not_called()
|
|
assert adapter._pending_messages.get(sk) is event
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ack_explains_compression_demotion(self) -> None:
|
|
runner = _make_runner()
|
|
adapter = _make_adapter()
|
|
event = _make_event(text="hi mid-compress")
|
|
sk = build_session_key(event.source)
|
|
parent = _make_parent_no_subagents()
|
|
runner._running_agents[sk] = parent
|
|
runner._running_agents_ts[sk] = time.time() - 120
|
|
runner.adapters[event.source.platform] = adapter
|
|
runner._session_db._db.get_compression_lock_holder.return_value = "compressing"
|
|
|
|
with patch("gateway.platforms.base.merge_pending_message_event"):
|
|
await runner._handle_active_session_busy_message(event, sk)
|
|
|
|
adapter._send_with_retry.assert_called_once()
|
|
content = adapter._send_with_retry.call_args.kwargs.get("content", "")
|
|
assert "Compressing context" in content
|
|
assert "queued" in content.lower()
|
|
assert "/stop" in content
|
|
assert "Interrupting" not in content
|
|
|
|
|