mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
141 lines
4.5 KiB
Python
141 lines
4.5 KiB
Python
"""Regression coverage for Telegram final delivery after streamed edit failure."""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import PlatformConfig
|
|
from gateway.platforms.base import SendResult
|
|
from gateway.stream_consumer import GatewayStreamConsumer, StreamConsumerConfig
|
|
from plugins.platforms.telegram.adapter import TelegramAdapter
|
|
|
|
|
|
def _adapter() -> MagicMock:
|
|
adapter = MagicMock()
|
|
adapter.REQUIRES_EDIT_FINALIZE = True
|
|
adapter.FALLBACK_ON_FINAL_EDIT_FLOOD = True
|
|
adapter.RESEND_FINAL_ON_EMPTY_STREAM_FALLBACK = True
|
|
adapter.MAX_MESSAGE_LENGTH = 4096
|
|
adapter.edit_message = AsyncMock()
|
|
adapter.send = AsyncMock()
|
|
adapter.delete_message = AsyncMock(return_value=True)
|
|
return adapter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_turn_final_flood_immediately_delivers_missing_tail():
|
|
"""A short visible preview must not suppress the completed answer."""
|
|
adapter = _adapter()
|
|
adapter.edit_message.return_value = SendResult(
|
|
success=False,
|
|
error="Flood control exceeded. Retry in 180 seconds",
|
|
retry_after=180.0,
|
|
)
|
|
adapter.send.return_value = SendResult(success=True, message_id="tail-1")
|
|
|
|
consumer = GatewayStreamConsumer(
|
|
adapter,
|
|
"chat-1",
|
|
StreamConsumerConfig(cursor=" ▉"),
|
|
metadata={"thread_id": "77"},
|
|
)
|
|
consumer._message_id = "preview-1"
|
|
consumer._last_sent_text = ":("
|
|
consumer._already_sent = True
|
|
|
|
ok = await consumer._send_or_edit(
|
|
":( The completed answer follows here.",
|
|
finalize=True,
|
|
is_turn_final=True,
|
|
)
|
|
|
|
assert ok is False
|
|
assert consumer._flood_strikes == 1
|
|
assert consumer._fallback_final_send is True
|
|
assert consumer.final_content_delivered is False
|
|
assert adapter.edit_message.await_count == 1
|
|
|
|
await consumer._send_fallback_final(":( The completed answer follows here.")
|
|
|
|
adapter.send.assert_awaited_once()
|
|
assert adapter.send.await_args.kwargs["content"] == "The completed answer follows here."
|
|
assert adapter.send.await_args.kwargs["metadata"] == {
|
|
"thread_id": "77",
|
|
"notify": True,
|
|
}
|
|
adapter.delete_message.assert_not_awaited()
|
|
assert consumer.final_response_sent is True
|
|
assert consumer.final_content_delivered is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_non_opt_in_adapter_keeps_adaptive_final_edit_retry():
|
|
"""Immediate final fallback remains scoped to opted-in adapters."""
|
|
adapter = _adapter()
|
|
adapter.FALLBACK_ON_FINAL_EDIT_FLOOD = False
|
|
adapter.edit_message.return_value = SendResult(
|
|
success=False,
|
|
error="Flood control exceeded. Retry in 30 seconds",
|
|
retry_after=30.0,
|
|
)
|
|
|
|
consumer = GatewayStreamConsumer(adapter, "chat-1")
|
|
consumer._message_id = "preview-1"
|
|
consumer._last_sent_text = "partial"
|
|
consumer._already_sent = True
|
|
|
|
ok = await consumer._send_or_edit(
|
|
"partial plus final",
|
|
finalize=True,
|
|
is_turn_final=True,
|
|
)
|
|
|
|
assert ok is False
|
|
assert consumer._flood_strikes == 1
|
|
assert consumer._fallback_final_send is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_empty_tail_commit_honors_retry_after(monkeypatch):
|
|
adapter = _adapter()
|
|
adapter.send.side_effect = [
|
|
SendResult(
|
|
success=False,
|
|
error="Flood control exceeded",
|
|
retry_after=3.0,
|
|
),
|
|
SendResult(success=True, message_id="final-1"),
|
|
]
|
|
sleep = AsyncMock()
|
|
monkeypatch.setattr("gateway.stream_consumer.asyncio.sleep", sleep)
|
|
|
|
consumer = GatewayStreamConsumer(adapter, "chat-1")
|
|
consumer._message_id = "preview-1"
|
|
consumer._last_sent_text = "Final answer"
|
|
consumer._fallback_final_send = True
|
|
|
|
await consumer._send_fallback_final("Final answer")
|
|
|
|
assert adapter.send.await_count == 2
|
|
sleep.assert_awaited_once_with(3.0)
|
|
assert consumer.final_content_delivered is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_telegram_long_flood_result_keeps_retry_after():
|
|
"""The real adapter contract preserves the server delay for consumers."""
|
|
class FloodError(Exception):
|
|
retry_after = 30.0
|
|
|
|
adapter = TelegramAdapter(PlatformConfig(enabled=True, token="test-token"))
|
|
adapter._bot = MagicMock()
|
|
adapter._bot.edit_message_text = AsyncMock(side_effect=FloodError("Retry after 30"))
|
|
|
|
result = await adapter.edit_message("123", "456", "Final answer", finalize=False)
|
|
|
|
assert result.success is False
|
|
assert result.error == "flood_control:30.0"
|
|
assert result.retry_after == 30.0
|
|
|
|
|