Python
fix(ollama): default to 127.0.0.1, not localhost, so Windows skips a 2 s wait per connection (OPE-229)
Devika Vermac621ad998528
diff --git a/coworker/providers/ollama_facts.py b/coworker/providers/ollama_facts.pyindex 1d753522..0938c4c5 100644--- a/coworker/providers/ollama_facts.py+++ b/coworker/providers/ollama_facts.py@@ -29,7 +29,8 @@ _lock = threading.Lock()def native_base(base_url: Optional[str]) -> str:- base = (base_url or "http://localhost:11434").strip().rstrip("/")+ # The same default as `registry.DEFAULT_OLLAMA_URL` (127.0.0.1, OPE-229).+ base = (base_url or "http://127.0.0.1:11434").strip().rstrip("/")return base[: -len("/v1")] if base.endswith("/v1") else basediff --git a/coworker/providers/openai_provider.py b/coworker/providers/openai_provider.pyindex b9db639f..45db982c 100644--- a/coworker/providers/openai_provider.py+++ b/coworker/providers/openai_provider.py@@ -229,7 +229,7 @@ class OpenAIProvider(ProviderClient):# inject a `client` directly, bypassing all of this.## `base_url` points the same OpenAI SDK at any OpenAI-compatible endpoint — used by the- # provider router for Ollama (`http://localhost:11434/v1`, with a placeholder key) and,+ # provider router for Ollama (`http://127.0.0.1:11434/v1`, with a placeholder key) and,# later, other OpenAI-shaped backends. When None, behavior is identical to stock OpenAI.## `http_client` is an optional httpx.Client handed to the SDK. Ollama uses it to carrydiff --git a/coworker/providers/registry.py b/coworker/providers/registry.pyindex bfc94da3..25422ff5 100644--- a/coworker/providers/registry.py+++ b/coworker/providers/registry.py@@ -32,7 +32,10 @@ from .openai_provider import OpenAIProviderfrom .openai_responses import OpenAIResponsesProviderfrom .vertex_provider import VertexProvider-DEFAULT_OLLAMA_URL = "http://localhost:11434"+# 127.0.0.1, not localhost: on Windows `localhost` tries ::1 first, where Ollama does not+# listen, and every new connection waits about 2 s before falling back (OPE-229). An+# address the user typed is used as typed.+DEFAULT_OLLAMA_URL = "http://127.0.0.1:11434"# The index deliberately lives in the SecretStore too: it names profiles which contain# credentials, and keeping the two together makes a future secure-store backend a drop-in.CUSTOM_PROVIDERS_PROFILE = "providers:custom"diff --git a/coworker/server/manager.py b/coworker/server/manager.pyindex 9aa5c4b3..652b9aab 100644--- a/coworker/server/manager.py+++ b/coworker/server/manager.py@@ -92,7 +92,7 @@ from ..providers import (verify_provider_key,)from ..providers.local_server import LOCAL_SERVERS-from ..providers.registry import CUSTOM_PROVIDERS_PROFILE+from ..providers.registry import CUSTOM_PROVIDERS_PROFILE, DEFAULT_OLLAMA_URLfrom ..secrets import SecretStore, state_dirfrom ..sessions import SessionRecord, usage_totalsfrom ..teams import Actor as TeamActor@@ -4682,7 +4682,7 @@ class SessionManager:if cached and now - cached[0] < 30:return cached[1]profile = self.secrets.get("provider:ollama") or {}- base = (profile.get("base_url") or "http://localhost:11434").strip().rstrip("/")+ base = (profile.get("base_url") or DEFAULT_OLLAMA_URL).strip().rstrip("/")if base.endswith("/v1"):base = base[: -len("/v1")]try:@@ -4697,7 +4697,7 @@ class SessionManager:def _ollama_models(self) -> list[str]:"""Live list of models pulled into the configured Ollama server (via its native`/api/tags`), as `ollama:<name>` so they're directly selectable. Empty if Ollama is- unreachable — best-effort, never raises. No stored profile → the default localhost+ unreachable — best-effort, never raises. No stored profile → the default 127.0.0.1endpoint, same as `_ollama_alive` (keyless Ollama needs no saved form)."""from ..providers import ollama_factsdiff --git a/tests/test_provider_router.py b/tests/test_provider_router.pyindex bb3a5d14..d0dfec33 100644--- a/tests/test_provider_router.py+++ b/tests/test_provider_router.py@@ -52,12 +52,24 @@ def test_base_url_omitted_when_none(monkeypatch):# -- ollama URL normalization ---------------------------------------------------def test_normalize_ollama_url():- assert _normalize_ollama_url(None) == "http://localhost:11434/v1"+ # The default is 127.0.0.1 (OPE-229); an address the user typed is kept as typed.+ assert _normalize_ollama_url(None) == "http://127.0.0.1:11434/v1"assert (_normalize_ollama_url("http://localhost:11434") == "http://localhost:11434/v1")assert _normalize_ollama_url("http://h:1/v1/") == "http://h:1/v1"- assert _normalize_ollama_url(" ") == "http://localhost:11434/v1"+ assert _normalize_ollama_url(" ") == "http://127.0.0.1:11434/v1"+++def test_ollama_defaults_never_use_localhost():+ """OPE-229: on Windows `localhost` tries ::1 first and every new Ollama connection+ waits about 2 s. Every built-in default must be 127.0.0.1."""+ from coworker.providers import ollama_facts+ from coworker.providers.registry import DEFAULT_OLLAMA_URL++ assert DEFAULT_OLLAMA_URL == "http://127.0.0.1:11434"+ assert ollama_facts.native_base(None) == "http://127.0.0.1:11434"+ assert ollama_facts.native_base("http://localhost:11434/v1") == "http://localhost:11434"def test_build_ollama_client_uses_base_url(monkeypatch):