From c621ad998528922b1c9225630bce8d6b391153eb Mon Sep 17 00:00:00 2001 From: Devika Verma Date: Mon, 2 Nov 2026 07:47:29 +0530 Subject: [PATCH 1/4] fix(ollama): default to 127.0.0.1, not localhost, so Windows skips a 2 s wait per connection (OPE-229) On Windows `localhost` resolves to ::1 first. Ollama listens only on 127.0.0.1, so every new connection waited about 2 seconds before falling back. The model-info lookup opens one connection per model, so with five models pulled a cold /v1/settings took about 19.6 s and the composer's model picker (which gives up after 10 s) stayed on "Loading models...". The built-in defaults (registry.DEFAULT_OLLAMA_URL, ollama_facts and the manager's liveness probe) are now http://127.0.0.1:11434. An address the user saved is used as typed. Measured on Windows 11 with five models: a cold settings call takes 3.1 s instead of 19.6 s. --- coworker/providers/ollama_facts.py | 3 ++- coworker/providers/openai_provider.py | 2 +- coworker/providers/registry.py | 5 ++++- coworker/server/manager.py | 6 +++--- tests/test_provider_router.py | 16 ++++++++++++++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/coworker/providers/ollama_facts.py b/coworker/providers/ollama_facts.py index 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 base diff --git a/coworker/providers/openai_provider.py b/coworker/providers/openai_provider.py index 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 carry diff --git a/coworker/providers/registry.py b/coworker/providers/registry.py index bfc94da3..25422ff5 100644 --- a/coworker/providers/registry.py +++ b/coworker/providers/registry.py @@ -32,7 +32,10 @@ from .openai_provider import OpenAIProvider from .openai_responses import OpenAIResponsesProvider from .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.py index 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_URL from ..secrets import SecretStore, state_dir from ..sessions import SessionRecord, usage_totals from ..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:` 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.1 endpoint, same as `_ollama_alive` (keyless Ollama needs no saved form).""" from ..providers import ollama_facts diff --git a/tests/test_provider_router.py b/tests/test_provider_router.py index 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): From addd78f3b9edd58ff25a4a77ea8fe28a481bc458 Mon Sep 17 00:00:00 2001 From: Devika Verma Date: Mon, 2 Nov 2026 07:53:28 +0530 Subject: [PATCH 2/4] fix(engine): time model calls, tools and waits with perf_counter, which ticks every millisecond on Windows (OPE-245) time.monotonic() on Windows is GetTickCount64, which ticks every 15.625 ms, so the transcript's timing sidecar (model_ms, tool_ms, waited_ms) read 0 or 15.6 for anything short. perf_counter() is monotonic too and ticks in fractions of a microsecond on every OS. The new test fails on Windows without the change. --- coworker/engine.py | 12 ++++---- tests/test_timing_clock.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 tests/test_timing_clock.py diff --git a/coworker/engine.py b/coworker/engine.py index 716c262b..351e8eac 100644 --- a/coworker/engine.py +++ b/coworker/engine.py @@ -484,11 +484,11 @@ class TurnEngine: cancel_wait.cancel() async def _wait_tool(self, tool_call, coro, interrupted): - started, clock = time.time(), time.monotonic() + started, clock = time.time(), time.perf_counter() try: return await self._interruptible(coro, interrupted) finally: - self._tool_timings.setdefault(tool_call.id, {}).update(waited_started=started, waited_ms=(time.monotonic() - clock) * 1000) + self._tool_timings.setdefault(tool_call.id, {}).update(waited_started=started, waited_ms=(time.perf_counter() - clock) * 1000) def _timed_result(self, tool_call, result): message = _tool_result_message(tool_call, result) @@ -802,7 +802,7 @@ class TurnEngine: reasoning="".join(streamed_reasoning) or None, ) - model_started, model_clock = time.time(), time.monotonic() + model_started, model_clock = time.time(), time.perf_counter() try: async for chunk in self._astream(): if chunk.reasoning_delta: @@ -879,7 +879,7 @@ class TurnEngine: effort_setting=self.model_settings.get("reasoning_effort"), ) ) - self.messages[-1]["timing"] = {"model_started": model_started, "model_ms": (time.monotonic() - model_clock) * 1000} + self.messages[-1]["timing"] = {"model_started": model_started, "model_ms": (time.perf_counter() - model_clock) * 1000} payload: dict[str, Any] = { "text": turn.text, "tool_calls": [tc.name for tc in turn.tool_calls], @@ -2283,7 +2283,7 @@ class TurnEngine: def _execute_sync(self, tool_call: ToolCall) -> tuple[Any, str]: """Execute one authorized call (runs in a worker thread).""" - started, clock = time.time(), time.monotonic() + started, clock = time.time(), time.perf_counter() try: delegated = self._authorized_delegates.pop(tool_call.id, None) if delegated and delegated != self._delegated_context(tool_call): @@ -2302,7 +2302,7 @@ class TurnEngine: except Exception as exc: return {"error": str(exc), "error_type": type(exc).__name__}, "error" finally: - self._tool_timings.setdefault(tool_call.id, {}).update(tool_started=started, tool_ms=(time.monotonic() - clock) * 1000) + self._tool_timings.setdefault(tool_call.id, {}).update(tool_started=started, tool_ms=(time.perf_counter() - clock) * 1000) def _record_result(self, tool_call: ToolCall, result: Any, status: str) -> Event: spec = self.registry.get(tool_call.name) diff --git a/tests/test_timing_clock.py b/tests/test_timing_clock.py new file mode 100644 index 00000000..fbd55585 --- /dev/null +++ b/tests/test_timing_clock.py @@ -0,0 +1,62 @@ +"""OPE-245: the `timing` sidecar measures with a millisecond clock on every OS. + +On Windows `time.monotonic()` ticks every 15.625 ms (GetTickCount64), so a tool that took +4 ms was saved as `tool_ms` 0 or 15.6. `time.perf_counter()` is monotonic too and ticks in +fractions of a microsecond everywhere.""" + +from __future__ import annotations + +import asyncio +import time + +from coworker.engine import TurnEngine +from coworker.permissions import PermissionEngine +from coworker.providers import AssistantTurn, ModelCapabilities, ProviderClient, ToolCall +from coworker.tools import ToolRegistry + + +class _Provider(ProviderClient): + def __init__(self, turns): + self._turns = list(turns) + + def complete(self, *, model, messages, tools=None, **settings): + return self._turns.pop(0) + + def capabilities(self, model): + return ModelCapabilities() + + +class _ReadOnly: + requires_approval = False + risk_level = "low" + + +def four_ms() -> dict: + """Busy for about 4 ms.""" + end = time.perf_counter() + 0.004 + while time.perf_counter() < end: + pass + return {"ok": True} + + +def test_tool_ms_has_millisecond_resolution(tmp_path): + registry = ToolRegistry() + registry.register(four_ms, metadata=_ReadOnly()) + engine = TurnEngine( + provider=_Provider([ + AssistantTurn(tool_calls=[ToolCall(id="c1", name="four_ms", arguments={})], + finish_reason="tool_calls"), + AssistantTurn(text="done", finish_reason="stop"), + ]), + registry=registry, + permissions=PermissionEngine(workspace_root=tmp_path), + model="gpt-5.5", + ) + + async def go(): + return [ev async for ev in engine.run("go")] + + asyncio.run(go()) + tool = next(m for m in engine.messages if m.get("role") == "tool") + # 15.6 ms ticks would give 0 or ~15.6 here. + assert 3.5 <= tool["timing"]["tool_ms"] < 15 From 185ad5142d08e88797110f1c1996ef438688f0ff Mon Sep 17 00:00:00 2001 From: Devika Verma Date: Mon, 2 Nov 2026 14:20:14 +0530 Subject: [PATCH 3/4] fix(server): session titles for models that cannot turn reasoning off (OPE-201) The title call sends reasoning_effort "none". Endpoints whose reasoning is mandatory refuse it (OpenRouter GLM 5.3: 400 "Reasoning is mandatory for this endpoint and cannot be disabled"), so those sessions never got a title. When the refusal is about reasoning or effort, the title is asked once more without the effort and with room for the reasoning. Other failures are not retried. The native Anthropic KeyError first reported here is already fixed on main (effort.NO_EFFORT handling). --- coworker/server/manager.py | 71 ++++++++++++++++++++++++-------------- tests/test_autotitle.py | 36 +++++++++++++++++++ 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/coworker/server/manager.py b/coworker/server/manager.py index 9aa5c4b3..04b8927b 100644 --- a/coworker/server/manager.py +++ b/coworker/server/manager.py @@ -8094,6 +8094,9 @@ class SessionManager: # instruction ("4-5 word title") yields English titles for Chinese/Japanese/… # sessions. Same-language also relaxes the word count to a length that is natural # for the opener's script (CJK titles are a few characters, not 4-5 "words"). + # A title asked again without an effort setting (OPE-201): room for the reasoning a + # model that cannot turn it off spends before the few words of the title. + _AUTOTITLE_REASONING_MAX_TOKENS = 1024 _AUTOTITLE_PROMPT = ( "You title chat sessions. Given the user's opening message(s) — and, when " "present, the assistant's first reply for context — reply with ONLY a short " @@ -8192,31 +8195,49 @@ class SessionManager: fallback stays; the small-talk sentinel leaves auto_title unset so the turn-2 retry can run.""" try: - turn = await asyncio.to_thread( - engine.provider.complete, - model=engine.model, - messages=[ - {"role": "system", "content": self._AUTOTITLE_PROMPT}, - { - "role": "user", - "content": "\n\n".join(openers) - + ( - f"\n\n[the assistant's first reply]\n{assistant}" - if assistant - else "" - ), - }, - ], - temperature=0.2, - # Reasoning-routed models spend hidden tokens BEFORE emitting text; a - # tight cap plus default effort yields an empty completion and a silent - # no-op. Effort "none" means "no thinking": every provider's effort - # mapper handles it (OpenAI-vocabulary endpoints get it on the wire, - # Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves - # headroom either way. - max_tokens=64, - reasoning_effort="none", - ) + messages = [ + {"role": "system", "content": self._AUTOTITLE_PROMPT}, + { + "role": "user", + "content": "\n\n".join(openers) + + ( + f"\n\n[the assistant's first reply]\n{assistant}" + if assistant + else "" + ), + }, + ] + try: + turn = await asyncio.to_thread( + engine.provider.complete, + model=engine.model, + messages=messages, + temperature=0.2, + # Reasoning-routed models spend hidden tokens BEFORE emitting text; a + # tight cap plus default effort yields an empty completion and a silent + # no-op. Effort "none" means "no thinking": every provider's effort + # mapper handles it (OpenAI-vocabulary endpoints get it on the wire, + # Anthropic omits the field; see effort.NO_EFFORT), and 64 leaves + # headroom either way. + max_tokens=64, + reasoning_effort="none", + ) + except Exception as exc: + # OPE-201: some endpoints cannot turn reasoning off and refuse "none" + # (OpenRouter GLM 5.3: 400 "Reasoning is mandatory for this endpoint and + # cannot be disabled"). Ask once more with the model's own effort, and + # room for the reasoning it spends before the few words of the title. + text = str(exc).lower() + if "reasoning" not in text and "effort" not in text: + raise + logger.info("autotitle for %s: retrying without reasoning effort (%s)", session_id, exc) + turn = await asyncio.to_thread( + engine.provider.complete, + model=engine.model, + messages=messages, + temperature=0.2, + max_tokens=self._AUTOTITLE_REASONING_MAX_TOKENS, + ) raw = (getattr(turn, "text", None) or "").strip() # Sanitize: surrounding quotes off, whitespace collapsed, capped at 60. title = " ".join(raw.strip("\"'“”‘’`").split()) diff --git a/tests/test_autotitle.py b/tests/test_autotitle.py index f722c2b7..541fa449 100644 --- a/tests/test_autotitle.py +++ b/tests/test_autotitle.py @@ -208,3 +208,39 @@ async def test_non_english_small_talk_still_hits_the_sentinel(tmp_path): await _turn(mgr, "lang3", "帮我整理季度汇报") assert len(provider.title_calls) == 2 assert mgr.session_store.load("lang3").title == "季度汇报整理" + + +class _SettingsRecorder(TitleAwareProvider): + def __init__(self, chat_turns, titles): + super().__init__(chat_turns, titles) + self.title_settings: list[dict] = [] + + def complete(self, *, model, messages, tools=None, **settings): + if messages and "title chat sessions" in str(messages[0].get("content", "")): + self.title_settings.append(dict(settings)) + return super().complete(model=model, messages=messages, tools=tools, **settings) + + +async def test_a_model_that_cannot_turn_reasoning_off_still_gets_a_title(tmp_path): + # OPE-201: an endpoint whose reasoning is mandatory refuses effort "none" (OpenRouter + # GLM 5.3 answers 400). The title is asked once more without the effort, with room for + # the reasoning the model spends first. + refused = RuntimeError("Error code: 400 - {'error': {'message': 'Reasoning is mandatory for " + "this endpoint and cannot be disabled.', 'code': 400}}") + provider = _SettingsRecorder([_text("sure")], [refused, "Release Notes Draft"]) + mgr = SessionManager(workspace=tmp_path, provider=provider) + await _turn(mgr, "s20", "draft the release notes") + + first, second = provider.title_settings + assert first["reasoning_effort"] == "none" and first["max_tokens"] == 64 + assert "reasoning_effort" not in second + assert second["max_tokens"] == SessionManager._AUTOTITLE_REASONING_MAX_TOKENS + assert mgr.session_store.title_state("s20")["auto_title"] == "Release Notes Draft" + + +async def test_other_title_failures_are_not_retried(tmp_path): + provider = _SettingsRecorder([_text("ok")], [RuntimeError("model down"), "Never Used"]) + mgr = SessionManager(workspace=tmp_path, provider=provider) + await _turn(mgr, "s21", "summarize this doc") + assert len(provider.title_settings) == 1 + assert mgr.session_store.title_state("s21")["auto_title"] is None From f16aea3604085dde156a7035facaa6355e6fdc75 Mon Sep 17 00:00:00 2001 From: Devika Verma Date: Mon, 2 Nov 2026 14:20:13 +0530 Subject: [PATCH 4/4] fix(engine): changing a session's model no longer closes it when the reviewer is OpenAI Decisions or Jev (OPE-246) Changing a session's model also points the reviewer at the new model. That is right for the session-model reviewer, which checks with the same model. OpenAI Decisions and Jev use their own fixed model (DecisionReviewer.model is read-only), so the switch raised AttributeError and the session's connection closed. With those reviewers the switch now leaves the reviewer as it is. --- coworker/engine.py | 9 +++++++-- tests/test_auto_approve.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/coworker/engine.py b/coworker/engine.py index 716c262b..150ba7ff 100644 --- a/coworker/engine.py +++ b/coworker/engine.py @@ -589,9 +589,14 @@ class TurnEngine: # The reviewer judges with the session's own model (§1.5: "if it's trusted to # drive the agent, it's strong enough to review it"). Bound once at session build, # it would otherwise keep the OLD model for the rest of the session after a - # switch — silently reviewing with a model the user moved away from. + # switch — silently reviewing with a model the user moved away from. A + # decision-model reviewer (OpenAI Decisions, Jev) judges with its own service's + # model, which a session switch must not change (its `model` is read-only). if self.reviewer is not None: - self.reviewer.model = model + from .decision_reviewer import DecisionReviewer + + if not isinstance(self.reviewer, DecisionReviewer): + self.reviewer.model = model if not had_history: return None from .providers.matrix import model_labels diff --git a/tests/test_auto_approve.py b/tests/test_auto_approve.py index 7e613157..85f8523b 100644 --- a/tests/test_auto_approve.py +++ b/tests/test_auto_approve.py @@ -1091,6 +1091,23 @@ def test_reviewer_follows_a_model_switch(tmp_path): assert engine.reviewer.model == "anthropic:claude-sonnet-5" +def test_a_decision_model_reviewer_keeps_its_model_on_a_switch(tmp_path): + # With OpenAI Decisions or Jev as the reviewer, picking a model in a session raised + # AttributeError (DecisionReviewer.model has no setter) and closed the session's socket. + # Those reviewers judge with their own service's model, so a switch leaves it alone. + from coworker.decision_reviewer import build_decision_reviewer + + engine, _rows, _approvals = _engine(tmp_path, [AssistantTurn(text="ok", finish_reason="stop")]) + engine.reviewer = build_decision_reviewer("jev", api_key="test-key-not-real") + before = engine.reviewer.model + engine.messages.append({"role": "user", "content": "hi"}) + + engine.switch_model("openai:model-b") # must not raise + + assert engine.model == "openai:model-b" + assert engine.reviewer.model == before + + def test_model_switch_with_no_reviewer_attached_still_works(tmp_path): engine, _rows, _approvals = _engine(tmp_path, [AssistantTurn(text="ok", finish_reason="stop")]) engine.reviewer = None