From 7d324b0e47444887fc615e1c428aa30c3dfcd2e8 Mon Sep 17 00:00:00 2001 From: Rod Boev Date: Tue, 7 Jul 2026 00:38:01 -0400 Subject: [PATCH] fix(agent): probe localhost via IPv4 for LM Studio detection --- agent/model_metadata.py | 15 ++++++++------- tests/agent/test_model_metadata_local_ctx.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/agent/model_metadata.py b/agent/model_metadata.py index c9c1f65ba1c..eb7be9b80c0 100644 --- a/agent/model_metadata.py +++ b/agent/model_metadata.py @@ -631,16 +631,17 @@ def detect_local_server_type(base_url: str, api_key: str = "") -> Optional[str]: import httpx normalized = _normalize_base_url(base_url) + + # Resolve localhost to IPv4 to avoid 2s IPv6 timeout on Windows dual-stack. + normalized = normalized.replace("://localhost:", "://127.0.0.1:") + normalized = normalized.replace("://localhost/", "://127.0.0.1/") + if normalized.endswith("://localhost"): + normalized = normalized[:-len("localhost")] + "127.0.0.1" + server_url = normalized if server_url.endswith("/v1"): server_url = server_url[:-3] - lmstudio_url = _lmstudio_server_root(base_url) - - # Resolve localhost to IPv4 to avoid 2s IPv6 timeout on Windows dual-stack. - server_url = server_url.replace("://localhost:", "://127.0.0.1:") - server_url = server_url.replace("://localhost/", "://127.0.0.1/") - if server_url.endswith("://localhost"): - server_url = server_url[:-len("localhost")] + "127.0.0.1" + lmstudio_url = _lmstudio_server_root(normalized) headers = _auth_headers(api_key) diff --git a/tests/agent/test_model_metadata_local_ctx.py b/tests/agent/test_model_metadata_local_ctx.py index 879ca4bd264..ccda0a94392 100644 --- a/tests/agent/test_model_metadata_local_ctx.py +++ b/tests/agent/test_model_metadata_local_ctx.py @@ -504,7 +504,7 @@ class TestDetectLocalServerTypeAuth: result = detect_local_server_type("http://localhost:1234/api/v1") assert result == "lm-studio" - assert client_mock.get.call_args_list[0].args[0] == "http://localhost:1234/api/v1/models" + assert client_mock.get.call_args_list[0].args[0] == "http://127.0.0.1:1234/api/v1/models" class TestDetectLocalServerTypeLocalhostIPv4: