Python
Ollama: a context size for this machine, errors mid-stream, and a kept answer
Devika Verma72b487ec67c6
diff --git a/coworker/engine.py b/coworker/engine.pyindex e22b6280..bdd4b91c 100644--- a/coworker/engine.py+++ b/coworker/engine.py@@ -62,7 +62,7 @@ _REVIEWER_PAUSED_TEXT = (from .permissions import CLEARED_BY_MODE, Mode, PermissionEnginefrom .providers import AssistantTurn, ProviderClient, ToolCallfrom .providers.errors import friendly_model_error-from .providers.openai_provider import looks_like_unparsed_tool_call+from .providers.openai_provider import looks_like_unparsed_tool_call, strip_dangling_tool_tagfrom .tools import ToolRegistry@@ -735,6 +735,14 @@ class TurnEngine:if not self._turn_truncated:self._continuations = 0_sanitize_mangled_calls(turn)+ if not turn.tool_calls:+ # One stray `<tool_call>` at the very end of an otherwise finished+ # answer is noise, not a call: drop it and keep the answer.+ cleaned, dangling = strip_dangling_tool_tag(turn.text)+ if dangling and not looks_like_unparsed_tool_call(+ cleaned, self.registry.schemas() or None+ ):+ turn.text = cleanedself.messages.append(_assistant_message(turn,diff --git a/coworker/providers/local_machine.py b/coworker/providers/local_machine.pynew file mode 100644index 00000000..8c542139--- /dev/null+++ b/coworker/providers/local_machine.py@@ -0,0 +1,120 @@+"""What this machine can give a local model: the memory models run in, and the context+size that memory supports.++A local model's context window is a trade: every token of context costs memory while the+model is loaded, and a window the machine cannot hold either fails to load or crawls. The+model's trained maximum (131K to 1M on current models) is the wrong default; the machine+decides. Ollama does the same on its side (4K under 24 GiB of GPU memory, 32K to 48 GiB,+256K above) but its lowest tier is smaller than OpenWorker's first request, so OpenWorker+sets the window itself (ollama_context.py) and uses the tiers here.++Memory is read once per process: it does not change while OpenWorker runs. An NVIDIA card+reports its own memory through `nvidia-smi`; everything else (Apple silicon, CPU-only+machines, DGX Spark with its shared memory, where nvidia-smi reports none) uses system+memory, which is what those models load into.+"""++from __future__ import annotations++import functools+import os+import shutil+import subprocess+import sys+from typing import Optional++GB = 1024**3++# Context tiers by the memory models run in. Each step keeps the KV cache well inside+# what is left beside a 20 to 30 GB model: measured on a 30B Q4 model, 128K of context+# costs about 13 GB on top of the weights.+CONTEXT_TIERS: tuple[tuple[int, int], ...] = (+ (16 * GB, 16_384),+ (32 * GB, 32_768),+ (64 * GB, 65_536),+)+CONTEXT_ABOVE_TIERS = 131_072++# Below this, OpenWorker's own first request (system prompt plus tools, ~6K tokens) does+# not fit with room to work.+MIN_AGENT_CONTEXT = 16_384+++def system_memory_bytes() -> Optional[int]:+ """Total system memory, or None when it cannot be read."""+ try:+ if sys.platform == "win32":+ import ctypes++ class _Status(ctypes.Structure):+ _fields_ = [+ ("dwLength", ctypes.c_ulong),+ ("dwMemoryLoad", ctypes.c_ulong),+ ("ullTotalPhys", ctypes.c_ulonglong),+ ("ullAvailPhys", ctypes.c_ulonglong),+ ("ullTotalPageFile", ctypes.c_ulonglong),+ ("ullAvailPageFile", ctypes.c_ulonglong),+ ("ullTotalVirtual", ctypes.c_ulonglong),+ ("ullAvailVirtual", ctypes.c_ulonglong),+ ("ullAvailExtendedVirtual", ctypes.c_ulonglong),+ ]++ status = _Status()+ status.dwLength = ctypes.sizeof(_Status)+ if ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(status)): # type: ignore[attr-defined]+ return int(status.ullTotalPhys)+ return None+ pages = os.sysconf("SC_PHYS_PAGES")+ page = os.sysconf("SC_PAGE_SIZE")+ if pages > 0 and page > 0:+ return int(pages) * int(page)+ except (AttributeError, ValueError, OSError):+ pass+ return None+++def nvidia_gpu_memory_bytes() -> Optional[int]:+ """Memory of the largest NVIDIA card `nvidia-smi` reports, or None: no tool, no+ card, or a card with no memory of its own (DGX Spark answers "N/A")."""+ exe = shutil.which("nvidia-smi")+ if not exe:+ return None+ try:+ out = subprocess.run(+ [exe, "--query-gpu=memory.total", "--format=csv,noheader,nounits"],+ capture_output=True, text=True, timeout=5, check=False,+ ).stdout+ except (OSError, subprocess.SubprocessError):+ return None+ best = 0+ for line in out.splitlines():+ try:+ best = max(best, int(float(line.strip())) * 1024 * 1024)+ except ValueError:+ continue+ return best or None++[email protected]_cache(maxsize=1)+def model_memory_bytes() -> Optional[int]:+ """The memory a local model loads into on this machine."""+ return nvidia_gpu_memory_bytes() or system_memory_bytes()+++def recommended_context(+ model_max: Optional[int], memory_bytes: Optional[int]+) -> Optional[int]:+ """The context window to run a local model at, from the memory available.++ None when the memory is unknown: the caller keeps its own default. Never above the+ model's own maximum, never below the smallest tier."""+ if memory_bytes is None:+ return None+ tier = CONTEXT_ABOVE_TIERS+ for limit, ctx in CONTEXT_TIERS:+ if memory_bytes < limit:+ tier = ctx+ break+ if model_max:+ tier = min(tier, model_max)+ return max(tier, min(MIN_AGENT_CONTEXT, model_max or MIN_AGENT_CONTEXT))diff --git a/coworker/providers/ollama_context.py b/coworker/providers/ollama_context.pyindex efca6692..f1d7352c 100644--- a/coworker/providers/ollama_context.py+++ b/coworker/providers/ollama_context.py@@ -8,12 +8,13 @@ minus a few tokens — 2,051 — keeping only the tail. The system prompt and thtools sit at the front, so the model never sees them.The native `/api/chat` endpoint accepts `options.num_ctx`. This transport rewrites-chat-completions calls onto that endpoint and asks for the window the model was-trained with, read once per model from `/api/show` (`model_info["<arch>.context_length"]`,-or a `num_ctx` the Modelfile pins). `OPENWORKER_OLLAMA_NUM_CTX` caps that on machines-that cannot hold the full KV cache. `shift` is off: once the prompt no longer fits,-Ollama must error instead of silently discarding the front of the prompt again; the-engine treats that error as a compaction trigger.+chat-completions calls onto that endpoint and asks for a window chosen for this machine+(local_machine.py: 16K to 128K by the memory models load into), never above the window+the model was trained with, read once per model from `/api/show`+(`model_info["<arch>.context_length"]`). A `num_ctx` the Modelfile pins is the user's+choice and wins. `OPENWORKER_OLLAMA_NUM_CTX` caps either. `shift` is off: once the prompt+no longer fits, Ollama must error instead of silently discarding the front of the prompt+again; the engine treats that error as a compaction trigger."""from __future__ import annotations@@ -26,6 +27,8 @@ from typing import Any, Iterator, Optionalimport httpx+from . import local_machine+# Used when `/api/show` is unreachable or does not report a context length. 128k is# the trained window of most current local models (Gemma 4, Qwen 3, Llama 3.x).DEFAULT_OLLAMA_NUM_CTX = 131_072@@ -56,16 +59,22 @@ def apply_cap(value: int) -> int:return min(value, cap) if cap else value-def context_length_from_show(show: dict[str, Any]) -> Optional[int]:- """The window a model should run at, from an `/api/show` response.-- A `num_ctx` pinned in the Modelfile wins (the user chose it deliberately); otherwise- the trained length under `model_info["<architecture>.context_length"]`."""+def pinned_num_ctx(show: dict[str, Any]) -> Optional[int]:+ """A `num_ctx` the Modelfile pins: the user chose it deliberately, so it wins."""params = show.get("parameters")if isinstance(params, str):match = _MODELFILE_NUM_CTX.search(params)if match and int(match.group(1)) >= MIN_OLLAMA_NUM_CTX:return int(match.group(1))+ return None+++def context_length_from_show(show: dict[str, Any]) -> Optional[int]:+ """The window a model can run at, from an `/api/show` response: a pinned `num_ctx`,+ else the trained length under `model_info["<architecture>.context_length"]`."""+ pinned = pinned_num_ctx(show)+ if pinned:+ return pinnedinfo = show.get("model_info")if not isinstance(info, dict):return None@@ -79,10 +88,20 @@ def context_length_from_show(show: dict[str, Any]) -> Optional[int]:return keyed if isinstance(keyed, int) and keyed >= MIN_OLLAMA_NUM_CTX else None-def resolve_num_ctx(show: Optional[dict[str, Any]]) -> int:- """Trained window (or the default when unknown), capped by the env override."""+def resolve_num_ctx(+ show: Optional[dict[str, Any]], memory_bytes: Optional[int] = None+) -> int:+ """The window to send: the Modelfile's pin if any, else the machine's recommended+ size held under the trained window (or the default when unknown). The env override+ caps either. `memory_bytes` stands in for the machine's memory in tests."""+ if show:+ pinned = pinned_num_ctx(show)+ if pinned:+ return apply_cap(pinned)trained = context_length_from_show(show) if show else None- return apply_cap(trained or DEFAULT_OLLAMA_NUM_CTX)+ memory = memory_bytes if memory_bytes is not None else local_machine.model_memory_bytes()+ fitted = local_machine.recommended_context(trained or DEFAULT_OLLAMA_NUM_CTX, memory)+ return apply_cap(fitted or trained or DEFAULT_OLLAMA_NUM_CTX)def context_window_for(model: str) -> Optional[int]:@@ -93,7 +112,7 @@ def context_window_for(model: str) -> Optional[int]:vendor, _, bare = model.partition(":")if vendor != "ollama":return None- return _resolved.get(bare) or apply_cap(DEFAULT_OLLAMA_NUM_CTX)+ return _resolved.get(bare) or resolve_num_ctx(None)def ollama_http_client() -> httpx.Client:@@ -121,9 +140,13 @@ class OllamaContextTransport(httpx.BaseTransport):window is looked up per model from `/api/show` on first use and cached."""def __init__(- self, num_ctx: Optional[int] = None, inner: Optional[httpx.BaseTransport] = None+ self,+ num_ctx: Optional[int] = None,+ inner: Optional[httpx.BaseTransport] = None,+ memory_bytes: Optional[int] = None,) -> None:self.num_ctx = num_ctx+ self.memory_bytes = memory_bytes # tests: the machine's memory, stood in forself._inner = inner if inner is not None else httpx.HTTPTransport()self._by_model: dict[str, int] = {}@@ -135,12 +158,16 @@ class OllamaContextTransport(httpx.BaseTransport):except (UnicodeDecodeError, json.JSONDecodeError):return self._inner.handle_request(request)model = str(body.get("model") or "")- native = to_native_chat(body, self._num_ctx_for(model, request.url))+ native = to_native_chat(body, self._num_ctx_for(model, request))+ # The native call keeps what the caller set on the original: its Authorization+ # header (a proxy or a hosted Ollama asks for one) and its timeout, which lives in+ # the request's extensions.native_request = httpx.Request("POST",_native_url(request.url, "/api/chat"),- headers={"content-type": "application/json"},+ headers=_forwarded_headers(request),content=json.dumps(native),+ extensions=dict(request.extensions),)response = self._inner.handle_request(native_request)if body.get("stream") and response.status_code == 200:@@ -168,6 +195,15 @@ class OllamaContextTransport(httpx.BaseTransport):native = json.loads(line)except json.JSONDecodeError:continue+ error = native.get("error")+ if error and not native.get("message"):+ # Ollama reports a failure mid-stream as its own line (the+ # model unloaded, the prompt outgrew the window). Hand it to+ # the SDK as an error chunk, which it raises; dropping it ended+ # the turn as if the reply were complete.+ payload = {"error": {"message": str(error), "type": "server_error"}}+ yield f"data: {json.dumps(payload)}\n\n".encode()+ breakcalls = (native.get("message") or {}).get("tool_calls") or []if calls:saw_tools = True@@ -184,27 +220,28 @@ class OllamaContextTransport(httpx.BaseTransport):stream=_ByteChunks(chunks()),)- def _num_ctx_for(self, model: str, url: httpx.URL) -> int:+ def _num_ctx_for(self, model: str, request: httpx.Request) -> int:if self.num_ctx is not None:return self.num_ctxcached = self._by_model.get(model)if cached:return cached- show = self._show(model, url)- value = resolve_num_ctx(show)+ show = self._show(model, request)+ value = resolve_num_ctx(show, self.memory_bytes)if show is not None:self._by_model[model] = value_resolved[model] = valuereturn value- def _show(self, model: str, url: httpx.URL) -> Optional[dict[str, Any]]:+ def _show(self, model: str, original: httpx.Request) -> Optional[dict[str, Any]]:"""`/api/show` for `model`, or None when the server cannot answer. A failedlookup is not cached: the chat call that follows will surface the real error."""request = httpx.Request("POST",- _native_url(url, "/api/show"),- headers={"content-type": "application/json"},+ _native_url(original.url, "/api/show"),+ headers=_forwarded_headers(original),content=json.dumps({"model": model}),+ extensions=dict(original.extensions),)try:response = self._inner.handle_request(request)@@ -225,6 +262,14 @@ class OllamaContextTransport(httpx.BaseTransport):self._inner.close()+def _forwarded_headers(request: httpx.Request) -> dict[str, str]:+ headers = {"content-type": "application/json"}+ auth = request.headers.get("authorization")+ if auth and auth.strip().lower() != "bearer ollama": # the SDK's placeholder key+ headers["authorization"] = auth+ return headers++def _native_url(url: httpx.URL, endpoint: str) -> httpx.URL:"""`…/v1/chat/completions` → `…/api/<endpoint>`, keeping any path prefix in front of`/v1` (a reverse proxy mounting Ollama under a sub-path)."""diff --git a/coworker/providers/openai_provider.py b/coworker/providers/openai_provider.pyindex 34f715e7..b9db639f 100644--- a/coworker/providers/openai_provider.py+++ b/coworker/providers/openai_provider.py@@ -516,6 +516,23 @@ _LEAKED_TOOL_SYNTAX = (_FENCED = re.compile(r"```.*?```|~~~.*?~~~|`[^`\n]*`", re.DOTALL)+# A finished answer with one stray tool tag at its very end and nothing after it. Seen+# live on qwen3-coder:30b (Ollama 0.35.1): a complete, correct answer ended in `<tool_call>`+# and was thrown away as a failed call, and the retry gave a weaker second answer.+_DANGLING_TOOL_TAG = re.compile(r"\s*</?tool_call>\s*$", re.IGNORECASE)+++def strip_dangling_tool_tag(text: Optional[str]) -> tuple[Optional[str], bool]:+ """(text without a trailing empty tool tag, whether one was removed). Only the tag+ at the end goes; a tag with anything after it is a call that did not parse."""+ if not text:+ return text, False+ stripped = _DANGLING_TOOL_TAG.sub("", text, count=1)+ if stripped == text or not stripped.strip():+ return text, False+ return stripped.rstrip(), True++def looks_like_unparsed_tool_call(text: Optional[str], tools: Optional[list[dict[str, Any]]] = None) -> bool:diff --git a/tests/test_headless_run.py b/tests/test_headless_run.pyindex f9909f31..60c3f3d1 100644--- a/tests/test_headless_run.py+++ b/tests/test_headless_run.py@@ -255,6 +255,19 @@ def test_the_first_waits_are_short_and_the_waits_grow(monkeypatch: pytest.Monkeyassert 15 * 60 <= sum(delays) <= 20 * 60 # an outage of minutes is still waited out+def test_a_finished_answer_with_a_stray_tool_tag_is_kept(tmp_path: Path) -> None:+ # Seen live: the answer was complete, ended in `<tool_call>`, and was retried as a+ # failed call; the second answer was worse. The tag goes, the answer stays.+ proc, _ws, out = _run(+ tmp_path, "bypass-approvals", [{"text": "The output is 100.<tool_call>"}],+ prompt="Square ten.", env={"OPENWORKER_RUN_RETRY_DELAYS": "0"},+ )+ assert proc.returncode == 0, proc.stdout + proc.stderr+ assert proc.stdout.strip() == "The output is 100."+ assert _summary(out)["provider_retries"] == 0+ assert "provider_retry" not in [e["type"] for e in _events(out)]++def test_transient_provider_error_is_waited_out_and_the_run_completes(tmp_path: Path) -> None:fail = {"error": "Service unavailable", "error_type": "APIError"}write = {"text": "ok", "tool_calls": [{"name": "write_file", "arguments": {"path": "hello.txt", "content": "hello"}}]}diff --git a/tests/test_ollama_context.py b/tests/test_ollama_context.pyindex 0c53b3bd..1fcb3195 100644--- a/tests/test_ollama_context.py+++ b/tests/test_ollama_context.py@@ -11,8 +11,10 @@ import pytestfrom coworker.compaction import is_context_overflowfrom coworker.engine import TurnEnginefrom coworker.providers import ollama_context+from coworker.providers import local_machinefrom coworker.providers.ollama_context import (DEFAULT_OLLAMA_NUM_CTX,+ resolve_num_ctx,OllamaContextTransport,context_length_from_show,context_window_for,@@ -103,12 +105,11 @@ class _Inner(httpx.BaseTransport):return [r for r in self.requests if r.url.path.endswith("/api/chat")]-def _chat_request(model: str = "gemma4:e4b", base: str = "http://127.0.0.1:11434/v1"):- return httpx.Request(- "POST",- f"{base}/chat/completions",- json={"model": model, "messages": [{"role": "user", "content": "hi"}]},- )+def _chat_request(model: str = "gemma4:e4b", base: str = "http://127.0.0.1:11434/v1", stream: bool = False):+ body = {"model": model, "messages": [{"role": "user", "content": "hi"}]}+ if stream:+ body["stream"] = True+ return httpx.Request("POST", f"{base}/chat/completions", json=body)def _done(model: str = "gemma4:e4b") -> httpx.Response:@@ -133,45 +134,105 @@ def test_context_length_prefers_modelfile_num_ctx_then_trained_length():assert context_length_from_show({}) is None-def test_transport_reads_the_trained_window_from_api_show_once_per_model():+BIG = 512 * local_machine.GB # a machine with memory to spare: the top tier, 128K+++def test_transport_reads_the_window_from_api_show_once_per_model():inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))- transport = OllamaContextTransport(inner=inner)+ transport = OllamaContextTransport(inner=inner, memory_bytes=BIG)transport.handle_request(_chat_request())transport.handle_request(_chat_request())shows = [r for r in inner.requests if r.url.path == "/api/show"]assert len(shows) == 1assert json.loads(shows[0].content) == {"model": "gemma4:e4b"}+ # The trained window is 262K; the default never goes above the top tier.for chat in inner.chat_requests():- assert json.loads(chat.content)["options"]["num_ctx"] == 262144+ assert json.loads(chat.content)["options"]["num_ctx"] == 131072# The engine sizes compaction against the window actually in use.- assert context_window_for("ollama:gemma4:e4b") == 262144+ assert context_window_for("ollama:gemma4:e4b") == 131072+++def test_the_window_follows_the_memory_models_load_into():+ # Seen live: a 30B model at its full 262K window took 46 GB; the model's own maximum+ # is the wrong default on a laptop. The machine's memory picks the tier.+ assert resolve_num_ctx(GEMMA_SHOW, memory_bytes=8 * local_machine.GB) == 16384+ assert resolve_num_ctx(GEMMA_SHOW, memory_bytes=24 * local_machine.GB) == 32768+ assert resolve_num_ctx(GEMMA_SHOW, memory_bytes=48 * local_machine.GB) == 65536+ assert resolve_num_ctx(GEMMA_SHOW, memory_bytes=96 * local_machine.GB) == 131072+ # Never above what the model was trained for.+ small = {"model_info": {"general.architecture": "llama", "llama.context_length": 8192}}+ assert resolve_num_ctx(small, memory_bytes=96 * local_machine.GB) == 8192+ # A Modelfile pin is the user's choice and wins over the machine rule.+ pinned = {**GEMMA_SHOW, "parameters": "num_ctx 200000\nstop <eos>"}+ assert resolve_num_ctx(pinned, memory_bytes=8 * local_machine.GB) == 200000+ # Unknown memory: the trained window, as before.+ assert resolve_num_ctx(GEMMA_SHOW, memory_bytes=None) in (262144, 131072, 65536, 32768, 16384)+++def test_recommended_context_tiers():+ GB = local_machine.GB+ assert local_machine.recommended_context(1_000_000, 15 * GB) == 16384+ assert local_machine.recommended_context(1_000_000, 16 * GB) == 32768+ assert local_machine.recommended_context(1_000_000, 64 * GB) == 131072+ assert local_machine.recommended_context(4096, 64 * GB) == 4096 # the model's own limit+ assert local_machine.recommended_context(1_000_000, None) is None+++def test_native_calls_keep_the_authorization_header_and_the_timeout():+ inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))+ request = _chat_request()+ request.headers["authorization"] = "Bearer secret-proxy-token"+ request.extensions["timeout"] = {"connect": 10.0, "read": 600.0, "write": 10.0, "pool": 10.0}+ OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(request)+ for sent in inner.requests:+ assert sent.headers.get("authorization") == "Bearer secret-proxy-token"+ assert sent.extensions.get("timeout", {}).get("read") == 600.0+ # The SDK's placeholder key for a keyless Ollama is not forwarded.+ inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))+ request = _chat_request()+ request.headers["authorization"] = "Bearer ollama"+ OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(request)+ assert all("authorization" not in r.headers for r in inner.requests)+++def test_an_error_line_mid_stream_becomes_an_error_chunk():+ lines = [+ {"model": "gemma4:e4b", "message": {"role": "assistant", "content": "Hel"}, "done": False},+ {"error": "model runner has unexpectedly stopped"},+ ]+ body = "\n".join(json.dumps(line) for line in lines) + "\n"+ inner = _Inner(httpx.Response(200, content=body.encode()), show=httpx.Response(200, json=GEMMA_SHOW))+ response = OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(_chat_request(stream=True))+ events = [json.loads(e[len("data: "):]) for e in response.read().decode().split("\n\n") if e.startswith("data: {")]+ assert events[0]["choices"][0]["delta"]["content"] == "Hel"+ assert events[-1] == {"error": {"message": "model runner has unexpectedly stopped", "type": "server_error"}}-def test_env_override_caps_the_trained_window(monkeypatch):+def test_env_override_caps_the_window(monkeypatch):monkeypatch.setenv("OPENWORKER_OLLAMA_NUM_CTX", "32768")inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))- OllamaContextTransport(inner=inner).handle_request(_chat_request())+ OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(_chat_request())assert json.loads(inner.chat_requests()[0].content)["options"]["num_ctx"] == 32768assert context_window_for("ollama:gemma4:e4b") == 32768# A cap above the trained window changes nothing.monkeypatch.setenv("OPENWORKER_OLLAMA_NUM_CTX", "1000000")small = {"model_info": {"general.architecture": "llama", "llama.context_length": 8192}}inner = _Inner(_done(), show=httpx.Response(200, json=small))- OllamaContextTransport(inner=inner).handle_request(_chat_request(model="llama3.2"))+ OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(_chat_request(model="llama3.2"))assert json.loads(inner.chat_requests()[0].content)["options"]["num_ctx"] == 8192-def test_unreachable_api_show_falls_back_to_the_default_and_retries_later():+def test_unreachable_api_show_falls_back_to_the_machine_rule_and_retries_later():inner = _Inner(_done(), show=None)- transport = OllamaContextTransport(inner=inner)+ transport = OllamaContextTransport(inner=inner, memory_bytes=24 * local_machine.GB)transport.handle_request(_chat_request())- assert json.loads(inner.chat_requests()[0].content)["options"]["num_ctx"] == (- DEFAULT_OLLAMA_NUM_CTX- )- # Now the server answers; the cached failure must not stick.- inner.show = httpx.Response(200, json=GEMMA_SHOW)+ assert json.loads(inner.chat_requests()[0].content)["options"]["num_ctx"] == 32768+ # Now the server answers; the cached failure must not stick, and a model trained+ # for less than the tier gets its own limit.+ small = {"model_info": {"general.architecture": "llama", "llama.context_length": 8192}}+ inner.show = httpx.Response(200, json=small)transport.handle_request(_chat_request())- assert json.loads(inner.chat_requests()[1].content)["options"]["num_ctx"] == 262144+ assert json.loads(inner.chat_requests()[1].content)["options"]["num_ctx"] == 8192def test_native_endpoints_keep_a_reverse_proxy_prefix():@@ -288,15 +349,15 @@ def test_ollama_compaction_window_matches_the_requested_context():compaction_settings=None,_warned_context_fallback=False,)- # Before the first chat call resolves the model: the default, no 128k-guess warning.+ # Before the first chat call resolves the model: the machine rule, no 128k-guess warning.cfg = TurnEngine._compaction_config(engine)- assert cfg["context_window"] == DEFAULT_OLLAMA_NUM_CTX+ assert cfg["context_window"] == resolve_num_ctx(None)assert engine._warned_context_fallback is Falseassert context_window_for("gpt-5.5") is None# After: exactly the window the transport sent.inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))- OllamaContextTransport(inner=inner).handle_request(_chat_request())- assert TurnEngine._compaction_config(engine)["context_window"] == 262144+ OllamaContextTransport(inner=inner, memory_bytes=BIG).handle_request(_chat_request())+ assert TurnEngine._compaction_config(engine)["context_window"] == 131072def test_ollama_context_overflow_is_recognized():diff --git a/tests/test_provider_router.py b/tests/test_provider_router.pyindex f7b2a134..4642f948 100644--- a/tests/test_provider_router.py+++ b/tests/test_provider_router.py@@ -293,6 +293,22 @@ def test_salvage_truncated_xml_prefers_a_complete_call_and_filters_unknown_namesassert _salvage_tool_calls_from_text("<function=rm_rf>\n<parameter=p>/", _TODO_TOOLS) == []+def test_a_stray_tool_tag_at_the_end_of_a_finished_answer_is_dropped():+ from coworker.providers.openai_provider import strip_dangling_tool_tag++ # Seen live on qwen3-coder:30b: a complete answer, then `<tool_call>` and nothing.+ assert strip_dangling_tool_tag("10 squared is 100<tool_call>") == ("10 squared is 100", True)+ assert strip_dangling_tool_tag("Done.\n\n</tool_call>\n") == ("Done.", True)+ # A tag with anything after it is a call that did not parse: left alone.+ leaked = 'Sure.<tool_call>{"name": "todo_write"'+ assert strip_dangling_tool_tag(leaked) == (leaked, False)+ assert looks_like_unparsed_tool_call(leaked, _TODO_TOOLS) is True+ # Nothing to drop, or nothing left: unchanged.+ assert strip_dangling_tool_tag("Plain answer.") == ("Plain answer.", False)+ assert strip_dangling_tool_tag("<tool_call>") == ("<tool_call>", False)+ assert strip_dangling_tool_tag(None) == (None, False)++def test_looks_like_unparsed_tool_call_ignores_code_and_needs_tools():"""Distinguishes a leaked call from a model *explaining* tool syntax — the latter is a realanswer and must not be turned into an error."""@@ -438,6 +454,9 @@ def test_set_provider_skips_recommended_when_not_pulled(tmp_path, monkeypatch):mgr = SessionManager(data_dir=tmp_path)monkeypatch.setattr(mgr, "_suggested_models", lambda name: []) # nothing pulled+ # The picker also lists what a live Ollama has pulled; on a developer's machine that+ # is a real list, so stand it in too or the test depends on the machine.+ monkeypatch.setattr(mgr, "_ollama_models", lambda: [])mgr.set_provider("ollama", {"base_url": "http://localhost:11434"})assert "ollama:qwen3-coder:30b" not in mgr.get_settings()["models"]