fix(ollama): a profile saved as the old localhost default becomes 127.0.0.1, once (OPE-250)

The Settings form prefills the Ollama address with the default and saves
every field, so anyone who saved the Ollama card before OPE-229 (2026-11-02)
carries `http://localhost:11434` without having typed it. On Windows
`localhost` tries ::1 first, where Ollama does not listen, so every new
connection (each probe, each chat request) waited 0.3-2 s. With the rest of
this branch in place that profile alone kept a cold Models & Keys load at
1.0 s; with it migrated the same load takes 0.39 s.

At manager start, a saved `base_url` that is exactly the old default (a
trailing slash or `/v1` allowed, case-insensitive host) is rewritten to
`DEFAULT_OLLAMA_URL`, keeping the `/v1` and the rest of the profile. OPE-229's
rule that a saved address is used as typed stands for anything else:
`localhost:11435`, a path after the port, or another host are left alone.

Test: the three migrated spellings, three addresses that must stay, and the
no-profile case.

(cherry picked from commit 5a94252ee4f528a8a8e1f200730e558320b3790e)
This commit is contained in:
Devika Verma
2026-11-17 18:38:05 +05:30
parent 53b4a8eb30
commit 96ef2d07f6
2 changed files with 64 additions and 0 deletions
+30
View File
@@ -446,6 +446,7 @@ class SessionManager:
self._autotitle_sig: dict[str, int] = {}
self.workspace_trust = WorkspaceTrustStore()
self.secrets = SecretStore()
self._migrate_legacy_ollama_default()
# OB-6: record on or off, keep for, and sending recordings to Langfuse or an
# OpenTelemetry endpoint. Settings live in the machine's state folder (beside the
# secrets), never in a project. Off by default: then no exporter exists at all.
@@ -5086,6 +5087,35 @@ class SessionManager:
self._save_prefs()
return {"ok": True, "dm_session": self.dm_session()}
# The Ollama default before OPE-229 (2026-11-02). The Settings form prefills the
# field and saves every field, so anyone who saved the Ollama card before then
# carries this value without having typed it.
LEGACY_OLLAMA_URL = "http://localhost:11434"
def _migrate_legacy_ollama_default(self) -> None:
"""A saved Ollama `base_url` that is exactly the pre-OPE-229 default becomes the
current one (127.0.0.1), once. On Windows `localhost` tries ::1 first, where Ollama
does not listen, so every new connection waited 0.3-2 s (OPE-250). OPE-229's rule
that a saved address is used as typed stands for anything else: a copy of the old
default is not an address anyone typed, so `localhost:11435` or a path after the
port is left alone."""
profile = self.secrets.get("provider:ollama")
if not isinstance(profile, dict):
return
saved = str(profile.get("base_url") or "").strip()
bare = saved.rstrip("/")
suffix = ""
if bare.endswith("/v1"):
bare, suffix = bare[: -len("/v1")], "/v1"
if bare.lower() != self.LEGACY_OLLAMA_URL:
return
migrated = dict(profile)
migrated["base_url"] = DEFAULT_OLLAMA_URL + suffix
try:
self.secrets.put("provider:ollama", migrated)
except OSError:
return # a read-only store keeps the slow address; nothing else depends on this
def _ollama_alive(self) -> bool:
"""Best-effort local-Ollama liveness, cached 30s (get_settings runs on every GUI
fetch — no 2s probe inline). Keyless is not the same as PRESENT: `ollama:*` picker
+34
View File
@@ -133,6 +133,40 @@ def test_a_server_that_does_not_answer_is_not_asked_for_its_models(tmp_path, mon
manager.observability.close()
def test_an_ollama_profile_saved_as_the_old_default_becomes_127_0_0_1(tmp_path, monkeypatch):
"""The Settings form saved the prefilled default, so profiles from before OPE-229 carry
`http://localhost:11434` without anyone having typed it. That exact value (a trailing
slash or `/v1` allowed) becomes the current default once; any other address stays as
typed, as OPE-229 decided."""
from coworker.secrets import SecretStore
monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))
store = SecretStore()
def after(saved: str) -> str:
store.put("provider:ollama", {"base_url": saved, "note": "kept"})
manager = SessionManager(workspace=tmp_path)
got = manager.secrets.get("provider:ollama")
assert got["note"] == "kept" # the rest of the profile is untouched
if hasattr(manager, "observability"):
manager.observability.close()
return got["base_url"]
assert after("http://localhost:11434") == "http://127.0.0.1:11434"
assert after("http://localhost:11434/") == "http://127.0.0.1:11434"
assert after("http://LOCALHOST:11434/v1") == "http://127.0.0.1:11434/v1"
# Addresses someone chose are used as typed.
assert after("http://localhost:11435") == "http://localhost:11435"
assert after("http://localhost:11434/ollama") == "http://localhost:11434/ollama"
assert after("http://gpu-box.lan:11434") == "http://gpu-box.lan:11434"
# No profile at all: nothing is written.
store.delete("provider:ollama")
manager = SessionManager(workspace=tmp_path)
assert manager.secrets.get("provider:ollama") is None
if hasattr(manager, "observability"):
manager.observability.close()
def test_local_server_defaults_name_127_0_0_1_not_localhost():
"""`localhost` resolves to ::1 first on Windows, so a probe of a server that is not
running waited out two connects; the defaults name the address directly (as OPE-229