fix(client): preserve explicitly supplied host or port during address resolution (#701)

_resolve_service_address overwrote both fields whenever either host or
port was missing, silently discarding the explicitly supplied one in
favor of the discovered or default pair. Resolve the fallback pair
first and fill only the missing field, so explicit values always win
while discovery and defaults still cover omitted fields.

Add regression tests covering explicit-only host/port, service
discovery precedence, invalid discovery payloads, and the unaffected
MCP stdio transport.
This commit is contained in:
golemon 2026-11-02 11:50:08 +08:00 • committed by GitHub
parent 1e51e0dc1b
commit 966c691d12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 88 additions and 4 deletions

View file

@ -27,15 +27,15 @@ class BaseClient(BaseComponent):
def _resolve_service_address(self, host: str | None, port: int | None) -> tuple[str, int]:
"""Resolve explicit, discovered, or default network coordinates."""
if not (host and port):
fallback_host, fallback_port = REME_DEFAULT_HOST, REME_DEFAULT_PORT
if service_info := os.environ.get(REME_SERVICE_INFO):
try:
data = json.loads(service_info)
host, port = data["host"], data["port"]
fallback_host, fallback_port = data["host"], data["port"]
except Exception:
self.logger.warning(f"Invalid service info: {service_info}")
host, port = REME_DEFAULT_HOST, REME_DEFAULT_PORT
else:
host, port = REME_DEFAULT_HOST, REME_DEFAULT_PORT
host = host or fallback_host
port = port or fallback_port
return normalize_connect_host(host), port
async def _start(self) -> None:

View file

@ -0,0 +1,84 @@
"""Regression tests for BaseClient service address resolution."""
import pytest
from reme.components.client.http_client import HttpClient
from reme.components.client.mcp_client import MCPClient
from reme.constants import REME_DEFAULT_HOST, REME_DEFAULT_PORT, REME_SERVICE_INFO
@pytest.fixture(autouse=True)
def clear_service_info(monkeypatch):
"""Address resolution runs without service discovery unless opted in."""
monkeypatch.delenv(REME_SERVICE_INFO, raising=False)
def test_explicit_port_only_keeps_port_and_defaults_host():
"""An explicit port is not discarded when the host is omitted."""
assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123"
client = MCPClient(port=8123)
assert client.host == REME_DEFAULT_HOST
assert client.port == 8123
def test_explicit_host_only_keeps_host_and_defaults_port():
"""An explicit host is not discarded when the port is omitted."""
assert HttpClient(host="192.0.2.10").base_url == f"http://192.0.2.10:{REME_DEFAULT_PORT}"
client = MCPClient(host="192.0.2.10")
assert client.host == "192.0.2.10"
assert client.port == REME_DEFAULT_PORT
def test_explicit_fields_win_over_service_discovery(monkeypatch):
"""Discovery fills only the missing field; explicit fields are kept."""
monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1", "port": 9000}')
assert HttpClient(port=8123).base_url == "http://10.0.0.1:8123"
assert HttpClient(host="192.0.2.10").base_url == "http://192.0.2.10:9000"
assert HttpClient(host="192.0.2.10", port=8123).base_url == "http://192.0.2.10:8123"
client = MCPClient(port=8123)
assert client.host == "10.0.0.1"
assert client.port == 8123
client = MCPClient(host="192.0.2.10")
assert client.host == "192.0.2.10"
assert client.port == 9000
def test_service_discovery_fills_both_fields_when_nothing_is_explicit(monkeypatch):
"""Without explicit arguments the discovered address is used as before."""
monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1", "port": 9000}')
assert HttpClient().base_url == "http://10.0.0.1:9000"
client = MCPClient()
assert client.host == "10.0.0.1"
assert client.port == 9000
def test_invalid_service_info_keeps_explicit_fields_and_defaults_the_rest(monkeypatch):
"""An undecodable discovery payload still preserves explicit fields."""
monkeypatch.setenv(REME_SERVICE_INFO, "not-json")
assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123"
assert HttpClient(host="192.0.2.10").base_url == f"http://192.0.2.10:{REME_DEFAULT_PORT}"
assert HttpClient().base_url == f"http://{REME_DEFAULT_HOST}:{REME_DEFAULT_PORT}"
def test_incomplete_service_info_keeps_explicit_fields_and_defaults_the_rest(monkeypatch):
"""A discovery payload missing host/port keys preserves explicit fields."""
monkeypatch.setenv(REME_SERVICE_INFO, '{"host": "10.0.0.1"}')
assert HttpClient(port=8123).base_url == f"http://{REME_DEFAULT_HOST}:8123"
assert HttpClient().base_url == f"http://{REME_DEFAULT_HOST}:{REME_DEFAULT_PORT}"
def test_mcp_stdio_transport_skips_network_address_resolution():
"""The stdio transport never resolves a network service address."""
client = MCPClient(transport="stdio", command="python", args=["server.py"])
assert not hasattr(client, "host")
assert not hasattr(client, "port")