fix(cron): forward manual run to the gateway for relay-fronted delivery (NS-773)
A manual 'hermes cron run' on a relay-fronted target has no live relay adapter
and no standalone sender, so it now forwards to the running gateway's
POST /api/jobs/{id}/run (marks due for the gateway ticker, which delivers via
the live relay adapter). Gateway unreachable -> the accurate 'start the gateway
or use cron trigger' error. Native topologies are untouched.
This commit is contained in:
committed by
Teknium
parent
2d1d65de46
commit
ad0e522362
@@ -0,0 +1,89 @@
|
||||
"""Manual `hermes cron run` forwarding for relay-fronted delivery targets.
|
||||
|
||||
A standalone CLI process has no live relay adapter and no standalone sender,
|
||||
so a manual run that targets a relay-fronted platform must forward to the
|
||||
running gateway (whose live relay adapter owns that delivery) rather than
|
||||
execute in-process and hit the native standalone fallback.
|
||||
"""
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from tools import cronjob_tools
|
||||
|
||||
|
||||
class _Resp:
|
||||
def __init__(self, status):
|
||||
self.status_code = status
|
||||
self.text = ""
|
||||
|
||||
|
||||
class TestRelayFrontedDeliveryPlatforms:
|
||||
def test_empty_when_nothing_fronted(self):
|
||||
with patch("gateway.relay.relay_fronted_platforms", return_value=set()):
|
||||
assert cronjob_tools._relay_fronted_delivery_platforms({"id": "j1"}) == set()
|
||||
|
||||
def test_detects_fronted_delivery_platform(self):
|
||||
job = {"id": "j1", "deliver": "discord"}
|
||||
with patch("gateway.relay.relay_fronted_platforms", return_value={"discord"}), patch(
|
||||
"cron.scheduler._resolve_delivery_targets",
|
||||
return_value=[{"platform": "discord", "chat_id": "123"}],
|
||||
):
|
||||
assert cronjob_tools._relay_fronted_delivery_platforms(job) == {"discord"}
|
||||
|
||||
def test_ignores_non_fronted_platform(self):
|
||||
job = {"id": "j1", "deliver": "discord"}
|
||||
with patch("gateway.relay.relay_fronted_platforms", return_value={"telegram"}), patch(
|
||||
"cron.scheduler._resolve_delivery_targets",
|
||||
return_value=[{"platform": "discord", "chat_id": "123"}],
|
||||
):
|
||||
assert cronjob_tools._relay_fronted_delivery_platforms(job) == set()
|
||||
|
||||
|
||||
class TestForwardRelayFrontedRun:
|
||||
def test_none_on_native_topology(self):
|
||||
with patch.object(
|
||||
cronjob_tools, "_relay_fronted_delivery_platforms", return_value=set()
|
||||
):
|
||||
assert cronjob_tools._forward_relay_fronted_run({"id": "j1"}) is None
|
||||
|
||||
def test_forwards_on_success(self):
|
||||
with patch.object(
|
||||
cronjob_tools, "_relay_fronted_delivery_platforms", return_value={"discord"}
|
||||
), patch("httpx.post", return_value=_Resp(200)):
|
||||
out = json.loads(cronjob_tools._forward_relay_fronted_run({"id": "j1"}))
|
||||
assert out["success"] is True
|
||||
assert out["forwarded_to_gateway"] is True
|
||||
|
||||
def test_errors_when_gateway_unreachable(self):
|
||||
with patch.object(
|
||||
cronjob_tools, "_relay_fronted_delivery_platforms", return_value={"discord"}
|
||||
), patch("httpx.post", side_effect=Exception("down")):
|
||||
out = json.loads(cronjob_tools._forward_relay_fronted_run({"id": "j1"}))
|
||||
assert out["success"] is False
|
||||
assert "relay-fronted" in out["error"]
|
||||
|
||||
def test_errors_on_gateway_4xx(self):
|
||||
with patch.object(
|
||||
cronjob_tools, "_relay_fronted_delivery_platforms", return_value={"discord"}
|
||||
), patch("httpx.post", return_value=_Resp(401)):
|
||||
out = json.loads(cronjob_tools._forward_relay_fronted_run({"id": "j1"}))
|
||||
assert out["success"] is False
|
||||
assert "relay-fronted" in out["error"]
|
||||
|
||||
def test_posts_to_run_route_with_bearer(self):
|
||||
sent = {}
|
||||
|
||||
def fake_post(url, headers=None, timeout=None):
|
||||
sent["url"] = url
|
||||
sent["headers"] = headers
|
||||
return _Resp(200)
|
||||
|
||||
with patch.object(
|
||||
cronjob_tools, "_relay_fronted_delivery_platforms", return_value={"discord"}
|
||||
), patch("httpx.post", side_effect=fake_post), patch(
|
||||
"agent.secret_scope.get_secret", return_value="secret-key-16chars"
|
||||
):
|
||||
cronjob_tools._forward_relay_fronted_run({"id": "abc123"})
|
||||
assert sent["url"].endswith("/api/jobs/abc123/run")
|
||||
assert sent["headers"]["Authorization"] == "Bearer secret-key-16chars"
|
||||
+93
-4
@@ -809,6 +809,89 @@ def _format_job(job: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return result
|
||||
|
||||
|
||||
def _relay_fronted_delivery_platforms(job: Dict[str, Any]) -> set:
|
||||
"""Delivery-platform names for this job that the relay connector fronts."""
|
||||
try:
|
||||
from gateway.relay import relay_fronted_platforms
|
||||
except Exception:
|
||||
return set()
|
||||
fronted = relay_fronted_platforms()
|
||||
if not fronted:
|
||||
return set()
|
||||
try:
|
||||
from cron.scheduler import _resolve_delivery_targets
|
||||
|
||||
targets = _resolve_delivery_targets(job) or []
|
||||
except Exception:
|
||||
return set()
|
||||
theirs = {t.get("platform") for t in targets if t.get("platform")}
|
||||
return theirs & fronted
|
||||
|
||||
|
||||
def _forward_relay_fronted_run(job: Dict[str, Any]) -> Optional[str]:
|
||||
"""Forward a manual run to the gateway when it targets a relay-fronted
|
||||
platform and this process has no live relay adapter.
|
||||
|
||||
Relay-fronted delivery has no standalone sender: the connector owns the
|
||||
credential and the gateway's live relay adapter is the only path. The
|
||||
gateway api_server's ``POST /api/jobs/{id}/run`` marks the job due for its
|
||||
own ticker, which fires it with the live adapter. Returns a JSON result
|
||||
string when forwarding engages (dispatch or the accurate error), else
|
||||
None to fall through to the normal in-process run.
|
||||
"""
|
||||
if not _relay_fronted_delivery_platforms(job):
|
||||
return None
|
||||
job_id = job["id"]
|
||||
import os
|
||||
|
||||
port_raw = os.getenv("API_SERVER_PORT", "").strip()
|
||||
try:
|
||||
port = int(port_raw) if port_raw else 8642
|
||||
except ValueError:
|
||||
port = 8642
|
||||
url = f"http://127.0.0.1:{port}/api/jobs/{job_id}/run"
|
||||
|
||||
from agent.secret_scope import get_secret
|
||||
|
||||
key = get_secret("API_SERVER_KEY", "") or ""
|
||||
|
||||
resp = None
|
||||
try:
|
||||
import httpx
|
||||
|
||||
resp = httpx.post(
|
||||
url, headers={"Authorization": f"Bearer {key}"}, timeout=10.0
|
||||
)
|
||||
except Exception:
|
||||
resp = None
|
||||
|
||||
if resp is not None and resp.status_code < 300:
|
||||
return json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"forwarded_to_gateway": True,
|
||||
"note": (
|
||||
"This job targets a relay-fronted platform; it was dispatched "
|
||||
"to the running gateway, whose live relay adapter owns that "
|
||||
"delivery."
|
||||
),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
"success": False,
|
||||
"error": (
|
||||
"This job targets a relay-fronted platform, which has no "
|
||||
"standalone sender. Start the gateway (it owns the live relay "
|
||||
"adapter) or use `hermes cron trigger` so the gateway ticker "
|
||||
"delivers it."
|
||||
),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
|
||||
|
||||
def _execute_job_now(
|
||||
job: Dict[str, Any], extra_prompt: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
@@ -1641,10 +1724,16 @@ def cronjob(
|
||||
# bg carries a terminal result (claim lost, or inline fallback
|
||||
# after pool rejection); None means background delivery is
|
||||
# unsupported here — run synchronously as before.
|
||||
exec_result = (
|
||||
bg if bg is not None
|
||||
else _execute_job_now(job, extra_prompt=extra_prompt)
|
||||
)
|
||||
if bg is not None:
|
||||
exec_result = bg
|
||||
else:
|
||||
# Relay-fronted manual run: a standalone process has no live
|
||||
# relay adapter and no standalone sender, so forward to the
|
||||
# running gateway (its live adapter owns that delivery).
|
||||
forwarded = _forward_relay_fronted_run(job)
|
||||
if forwarded is not None:
|
||||
return forwarded
|
||||
exec_result = _execute_job_now(job, extra_prompt=extra_prompt)
|
||||
# A claimed direct run advances next_run_at and may race the
|
||||
# external one-shot for the same occurrence. If Chronos loses that
|
||||
# claim, its consumed fire cannot re-arm itself; reconcile from the
|
||||
|
||||
Reference in New Issue
Block a user