refactor: replace direct gateway message sending with safe dispatch helper

This commit is contained in:
Abhimanyu Saharan
2026-02-10 15:18:39 +05:30
parent 42b061f72d
commit e75b2844bb
5 changed files with 87 additions and 70 deletions

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import re
from pathlib import Path
@@ -26,3 +27,27 @@ def test_api_does_not_import_openclaw_gateway_client_directly() -> None:
"`app.services.openclaw.shared`) instead of directly from `app.api`. "
f"Violations: {', '.join(violations)}"
)
def test_api_uses_safe_gateway_dispatch_helper() -> None:
"""API modules should use `send_gateway_agent_message_safe`, not direct send."""
repo_root = Path(__file__).resolve().parents[2]
api_root = repo_root / "backend" / "app" / "api"
direct_send_pattern = re.compile(r"\bsend_gateway_agent_message\b")
violations: list[str] = []
for path in api_root.rglob("*.py"):
rel = path.relative_to(repo_root)
for lineno, raw_line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1):
line = raw_line.strip()
if not direct_send_pattern.search(line):
continue
if "send_gateway_agent_message_safe" in line:
continue
violations.append(f"{rel}:{lineno}")
assert not violations, (
"Use `send_gateway_agent_message_safe` from `app.services.openclaw.shared` "
"for API-level gateway notification dispatch. "
f"Violations: {', '.join(violations)}"
)