mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-25 05:52:34 +00:00
feat(plugins): add standalone_sender_fn for out-of-process cron delivery
Plugin platforms (IRC, Teams, Google Chat) currently fail with `No live adapter for platform '<name>'` when a `deliver=<plugin>` cron job runs in a separate process from the gateway, even though the platforms are eligible cron targets via `cron_deliver_env_var` (added in #21306). Built-in platforms (Telegram, Discord, Slack, etc.) use direct REST helpers in `tools/send_message_tool.py` so cron can deliver without holding the gateway in the same process; plugin platforms historically depended on `_gateway_runner_ref()` which returns `None` out of process. This change adds an optional `standalone_sender_fn` field to `PlatformEntry` so plugins can register an ephemeral send path that opens its own connection, sends, and closes without needing the live adapter. The dispatch site in `_send_via_adapter` falls through to the hook when the gateway runner is unavailable, with a descriptive error when neither path applies. The hook is optional, so existing plugins are unaffected. Reference migrations land in the same change for IRC, Teams, and Google Chat, exercising the hook across stdlib (asyncio + IRC protocol), Bot Framework OAuth client_credentials, and Google service-account flows respectively. Security hardening on the new code paths: * IRC: control-character stripping on chat_id and message body to block CRLF command injection; bounded nick-collision retries; JOIN before PRIVMSG so channels with the default `+n` mode accept the delivery. * Teams: TEAMS_SERVICE_URL validated against an allowlist of known Bot Framework hosts (`smba.trafficmanager.net`, `smba.infra.gov.teams.microsoft.us`) to block SSRF; chat_id and tenant_id constrained to the documented Bot Framework character set; per-request timeouts so a slow STS endpoint cannot starve the activity POST. * Google Chat: chat_id and thread_id validated against strict resource-name regexes; service-account refresh wrapped in `asyncio.wait_for` so a hung token endpoint cannot stall the scheduler. Test coverage: 20 new tests covering happy path, missing-config errors, network failure modes, and each defensive validation. Existing tests unchanged. `bash scripts/run_tests.sh tests/tools/test_send_message_tool.py tests/gateway/test_irc_adapter.py tests/gateway/test_teams.py tests/gateway/test_google_chat.py` reports 341 passed, 0 regressions. Documentation: new "Out-of-process cron delivery" section in website/docs/developer-guide/adding-platform-adapters.md and an entry in gateway/platforms/ADDING_A_PLATFORM.md naming the hook.
This commit is contained in:
parent
3801825efd
commit
93e25ceb13
11 changed files with 1456 additions and 24 deletions
|
|
@ -2696,3 +2696,173 @@ class TestCronSchedulerRegistry:
|
|||
from cron.scheduler import _resolve_home_env_var
|
||||
|
||||
assert _resolve_home_env_var("google_chat") == "GOOGLE_CHAT_HOME_CHANNEL"
|
||||
|
||||
|
||||
# ── _standalone_send (out-of-process cron delivery) ──────────────────────
|
||||
|
||||
|
||||
class _FakeAiohttpResponse:
|
||||
def __init__(self, status: int, payload, text_body: str = ""):
|
||||
self.status = status
|
||||
self._payload = payload
|
||||
self._text = text_body or (str(payload) if payload is not None else "")
|
||||
|
||||
async def json(self):
|
||||
return self._payload
|
||||
|
||||
async def text(self):
|
||||
return self._text
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return None
|
||||
|
||||
|
||||
class _FakeAiohttpSession:
|
||||
def __init__(self, scripts):
|
||||
self._scripts = list(scripts)
|
||||
self.calls: list[tuple[str, dict]] = []
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return None
|
||||
|
||||
def post(self, url, **kwargs):
|
||||
self.calls.append((url, kwargs))
|
||||
if not self._scripts:
|
||||
raise AssertionError(f"No scripted response for POST {url}")
|
||||
return self._scripts.pop(0)
|
||||
|
||||
|
||||
def _install_fake_aiohttp(monkeypatch, session):
|
||||
fake_aiohttp = types.SimpleNamespace(
|
||||
ClientSession=lambda timeout=None: session,
|
||||
ClientTimeout=lambda total=None: None,
|
||||
)
|
||||
monkeypatch.setitem(sys.modules, "aiohttp", fake_aiohttp)
|
||||
|
||||
|
||||
def _install_fake_google_auth_transport(monkeypatch):
|
||||
fake_request_module = types.SimpleNamespace(Request=lambda: object())
|
||||
monkeypatch.setitem(sys.modules, "google.auth.transport", types.SimpleNamespace(requests=fake_request_module))
|
||||
monkeypatch.setitem(sys.modules, "google.auth.transport.requests", fake_request_module)
|
||||
|
||||
|
||||
class TestGoogleChatStandaloneSend:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_standalone_send_refreshes_token_and_posts_message(
|
||||
self, monkeypatch, tmp_path
|
||||
):
|
||||
sa_file = tmp_path / "sa.json"
|
||||
sa_file.write_text(json.dumps({
|
||||
"type": "service_account",
|
||||
"client_email": "bot@example.iam.gserviceaccount.com",
|
||||
"private_key": "fake",
|
||||
"token_uri": "https://example/token",
|
||||
}))
|
||||
monkeypatch.setenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", str(sa_file))
|
||||
|
||||
fake_creds = MagicMock()
|
||||
fake_creds.token = "the-token"
|
||||
fake_creds.refresh = MagicMock(return_value=None)
|
||||
|
||||
original = _gc_mod.service_account.Credentials.from_service_account_info
|
||||
_gc_mod.service_account.Credentials.from_service_account_info = MagicMock(
|
||||
return_value=fake_creds
|
||||
)
|
||||
try:
|
||||
_install_fake_google_auth_transport(monkeypatch)
|
||||
send_resp = _FakeAiohttpResponse(200, {"name": "spaces/AAA/messages/MMM"})
|
||||
session = _FakeAiohttpSession([send_resp])
|
||||
_install_fake_aiohttp(monkeypatch, session)
|
||||
|
||||
result = await _gc_mod._standalone_send(
|
||||
PlatformConfig(enabled=True, extra={}),
|
||||
"spaces/AAAA-BBBB",
|
||||
"hello cron",
|
||||
)
|
||||
finally:
|
||||
_gc_mod.service_account.Credentials.from_service_account_info = original
|
||||
|
||||
assert result == {
|
||||
"success": True,
|
||||
"message_id": "spaces/AAA/messages/MMM",
|
||||
}
|
||||
fake_creds.refresh.assert_called_once()
|
||||
assert len(session.calls) == 1
|
||||
url, kwargs = session.calls[0]
|
||||
assert url == "https://chat.googleapis.com/v1/spaces/AAAA-BBBB/messages"
|
||||
assert kwargs["headers"]["Authorization"] == "Bearer the-token"
|
||||
assert kwargs["json"] == {"text": "hello cron"}
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_standalone_send_returns_error_on_invalid_chat_id(self, monkeypatch):
|
||||
monkeypatch.delenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", raising=False)
|
||||
result = await _gc_mod._standalone_send(
|
||||
PlatformConfig(enabled=True, extra={}),
|
||||
"not-a-resource-name",
|
||||
"hi",
|
||||
)
|
||||
assert "error" in result
|
||||
assert "spaces/" in result["error"] or "users/" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_standalone_send_propagates_api_failure(self, monkeypatch, tmp_path):
|
||||
sa_file = tmp_path / "sa.json"
|
||||
sa_file.write_text(json.dumps({
|
||||
"type": "service_account",
|
||||
"client_email": "bot@example.iam.gserviceaccount.com",
|
||||
"private_key": "fake",
|
||||
"token_uri": "https://example/token",
|
||||
}))
|
||||
monkeypatch.setenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", str(sa_file))
|
||||
|
||||
fake_creds = MagicMock()
|
||||
fake_creds.token = "the-token"
|
||||
fake_creds.refresh = MagicMock(return_value=None)
|
||||
|
||||
original = _gc_mod.service_account.Credentials.from_service_account_info
|
||||
_gc_mod.service_account.Credentials.from_service_account_info = MagicMock(
|
||||
return_value=fake_creds
|
||||
)
|
||||
try:
|
||||
_install_fake_google_auth_transport(monkeypatch)
|
||||
send_resp = _FakeAiohttpResponse(
|
||||
403,
|
||||
{"error": {"code": 403, "message": "forbidden"}},
|
||||
text_body='{"error":{"code":403,"message":"forbidden"}}',
|
||||
)
|
||||
session = _FakeAiohttpSession([send_resp])
|
||||
_install_fake_aiohttp(monkeypatch, session)
|
||||
|
||||
result = await _gc_mod._standalone_send(
|
||||
PlatformConfig(enabled=True, extra={}),
|
||||
"spaces/AAAA-BBBB",
|
||||
"hi",
|
||||
)
|
||||
finally:
|
||||
_gc_mod.service_account.Credentials.from_service_account_info = original
|
||||
|
||||
assert "error" in result
|
||||
assert "403" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_standalone_send_rejects_chat_id_with_path_traversal(self, monkeypatch):
|
||||
monkeypatch.delenv("GOOGLE_CHAT_SERVICE_ACCOUNT_JSON", raising=False)
|
||||
|
||||
# Attempt to inject extra path segments after the prefix passes the
|
||||
# startswith check. The strict regex must reject this.
|
||||
result = await _gc_mod._standalone_send(
|
||||
PlatformConfig(enabled=True, extra={}),
|
||||
"spaces/AAAA/messages?messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD",
|
||||
"hi",
|
||||
)
|
||||
|
||||
assert "error" in result
|
||||
# The error names the expected resource shape so plugin authors can self-correct
|
||||
assert "spaces/" in result["error"] or "users/" in result["error"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue