Skip to content
Python

Ollama: request the model's real context window instead of the 4,096 default

Nuriahmat1ac4db02d201
diff --git a/coworker/compaction.py b/coworker/compaction.py
index cd25b0cb..bb6653aa 100644
--- a/coworker/compaction.py
+++ b/coworker/compaction.py
@@ -718,6 +718,9 @@ _OVERFLOW_MARKERS = (
"maximum context length",
"context window",
"prompt is too long",
+ # Ollama, when context shift is disabled: "the prompt is longer than the context
+ # length currently available to the model".
+ "longer than the context length",
"input is too long",
"too many tokens",
"input length and `max_tokens` exceed",
diff --git a/coworker/engine.py b/coworker/engine.py
index 081b60a0..3013bb6f 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -856,6 +856,12 @@ class TurnEngine:
from .providers.matrix import model_context_windows
cfg["context_window"] = model_context_windows().get(self.model)
+ if not cfg["context_window"]:
+ # Ollama's window is the num_ctx we send (providers/ollama_context.py),
+ # not the 128k guess — compact before that window context-shifts.
+ from .providers.ollama_context import context_window_for
+
+ cfg["context_window"] = context_window_for(self.model)
if not cfg["context_window"] and not self._warned_context_fallback:
# OPE-170: an unlisted model compacts on the 128k guess, which for a
# 1M-window model means compacting at a tenth of the window and
diff --git a/coworker/providers/ollama_context.py b/coworker/providers/ollama_context.py
new file mode 100644
index 00000000..efca6692
--- /dev/null
+++ b/coworker/providers/ollama_context.py
@@ -0,0 +1,469 @@
+"""Give Ollama a context window big enough for a Cowork prompt.
+
+Ollama's OpenAI-compatible `/v1/chat/completions` handler has no `num_ctx` field, so a
+request inherits the server's VRAM default. On a machine under ~24 GiB that default is
+4,096 tokens. A Cowork turn (instructions + tool schemas + the user message) is about
+7,000. Ollama then context-shifts the rendered prompt down to roughly half the window
+minus a few tokens — 2,051 — keeping only the tail. The system prompt and the file
+tools 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.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import re
+import time
+from typing import Any, Iterator, Optional
+
+import httpx
+
+# 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
+# Below this the Cowork prompt cannot fit at all, so smaller caps are ignored.
+MIN_OLLAMA_NUM_CTX = 2048
+_NUM_CTX_ENV = "OPENWORKER_OLLAMA_NUM_CTX"
+_MODELFILE_NUM_CTX = re.compile(r"^\s*num_ctx\s+(\d+)\s*$", re.MULTILINE)
+
+# Bare model name → the num_ctx the transport actually sent for it. The engine reads
+# this to size compaction against the real window rather than a guess.
+_resolved: dict[str, int] = {}
+
+
+def num_ctx_cap() -> Optional[int]:
+ """`OPENWORKER_OLLAMA_NUM_CTX` as an upper bound, or None when unset or invalid."""
+ raw = os.environ.get(_NUM_CTX_ENV, "").strip()
+ if not raw:
+ return None
+ try:
+ value = int(raw)
+ except ValueError:
+ return None
+ return value if value >= MIN_OLLAMA_NUM_CTX else None
+
+
+def apply_cap(value: int) -> int:
+ cap = num_ctx_cap()
+ 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"]`."""
+ 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))
+ info = show.get("model_info")
+ if not isinstance(info, dict):
+ return None
+ arch = info.get("general.architecture")
+ keyed = info.get(f"{arch}.context_length") if isinstance(arch, str) else None
+ if not isinstance(keyed, int):
+ keyed = next(
+ (v for k, v in info.items() if k.endswith(".context_length") and isinstance(v, int)),
+ None,
+ )
+ 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."""
+ trained = context_length_from_show(show) if show else None
+ return apply_cap(trained or DEFAULT_OLLAMA_NUM_CTX)
+
+
+def context_window_for(model: str) -> Optional[int]:
+ """Compaction window for an `ollama:` model. None for every other provider.
+
+ Before the first chat call resolves the model's real window this is the default
+ (capped); afterwards it is exactly the num_ctx being sent."""
+ vendor, _, bare = model.partition(":")
+ if vendor != "ollama":
+ return None
+ return _resolved.get(bare) or apply_cap(DEFAULT_OLLAMA_NUM_CTX)
+
+
+def ollama_http_client() -> httpx.Client:
+ """SDK client whose chat calls carry `num_ctx`. A long local generation must not
+ die on httpx's 5s default; the OpenAI SDK's own timeout still applies per request
+ when it sets one."""
+ return httpx.Client(
+ transport=OllamaContextTransport(),
+ timeout=httpx.Timeout(600.0, connect=10.0),
+ )
+
+
+class _ByteChunks(httpx.SyncByteStream):
+ def __init__(self, chunks: Iterator[bytes]) -> None:
+ self._chunks = chunks
+
+ def __iter__(self) -> Iterator[bytes]:
+ yield from self._chunks
+
+
+class OllamaContextTransport(httpx.BaseTransport):
+ """Rewrite `/v1/chat/completions` onto Ollama's native `/api/chat`.
+
+ `num_ctx` pins one window for every call. When None (the production default) the
+ 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
+ ) -> None:
+ self.num_ctx = num_ctx
+ self._inner = inner if inner is not None else httpx.HTTPTransport()
+ self._by_model: dict[str, int] = {}
+
+ def handle_request(self, request: httpx.Request) -> httpx.Response:
+ if request.method != "POST" or not request.url.path.endswith("/chat/completions"):
+ return self._inner.handle_request(request)
+ try:
+ body = json.loads(request.content.decode() or "{}")
+ 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_request = httpx.Request(
+ "POST",
+ _native_url(request.url, "/api/chat"),
+ headers={"content-type": "application/json"},
+ content=json.dumps(native),
+ )
+ response = self._inner.handle_request(native_request)
+ if body.get("stream") and response.status_code == 200:
+ return self._stream_response(response)
+ # The native transport hands back an unread stream. Buffer it so the OpenAI
+ # SDK can parse a normal JSON body.
+ response.read()
+ if response.status_code != 200:
+ return httpx.Response(response.status_code, content=response.content)
+ return httpx.Response(
+ 200,
+ headers={"content-type": "application/json"},
+ content=json.dumps(to_openai_completion(response.json())),
+ )
+
+ def _stream_response(self, response: httpx.Response) -> httpx.Response:
+ def chunks() -> Iterator[bytes]:
+ saw_tools = False
+ first = True
+ try:
+ for line in response.iter_lines():
+ if not line:
+ continue
+ try:
+ native = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ calls = (native.get("message") or {}).get("tool_calls") or []
+ if calls:
+ saw_tools = True
+ for event in to_openai_stream_events(native, include_role=first, saw_tools=saw_tools):
+ first = False
+ yield f"data: {json.dumps(event)}\n\n".encode()
+ yield b"data: [DONE]\n\n"
+ finally:
+ response.close()
+
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ stream=_ByteChunks(chunks()),
+ )
+
+ def _num_ctx_for(self, model: str, url: httpx.URL) -> int:
+ if self.num_ctx is not None:
+ return self.num_ctx
+ cached = self._by_model.get(model)
+ if cached:
+ return cached
+ show = self._show(model, url)
+ value = resolve_num_ctx(show)
+ if show is not None:
+ self._by_model[model] = value
+ _resolved[model] = value
+ return value
+
+ def _show(self, model: str, url: httpx.URL) -> Optional[dict[str, Any]]:
+ """`/api/show` for `model`, or None when the server cannot answer. A failed
+ lookup 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"},
+ content=json.dumps({"model": model}),
+ )
+ try:
+ response = self._inner.handle_request(request)
+ except httpx.HTTPError:
+ return None
+ try:
+ response.read()
+ if response.status_code != 200:
+ return None
+ body = response.json()
+ except (httpx.HTTPError, json.JSONDecodeError):
+ return None
+ finally:
+ response.close()
+ return body if isinstance(body, dict) else None
+
+ def close(self) -> None:
+ self._inner.close()
+
+
+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)."""
+ path = url.path
+ marker = "/v1/"
+ prefix = path[: path.rfind(marker)] if marker in path else ""
+ return url.copy_with(path=f"{prefix}{endpoint}")
+
+
+def to_native_chat(body: dict[str, Any], num_ctx: int) -> dict[str, Any]:
+ """OpenAI chat-completions JSON → Ollama `/api/chat` JSON."""
+ messages = body.get("messages") or []
+ native: dict[str, Any] = {
+ "model": body.get("model") or "",
+ "messages": [_native_message(m, messages) for m in messages if isinstance(m, dict)],
+ "stream": bool(body.get("stream")),
+ # Never discard the front of an over-long prompt. The engine compacts instead.
+ "shift": False,
+ "options": {"num_ctx": num_ctx},
+ }
+ tools = body.get("tools")
+ if tools:
+ native["tools"] = tools
+ options = native["options"]
+ predict = body.get("max_tokens", body.get("max_completion_tokens"))
+ if isinstance(predict, int):
+ options["num_predict"] = predict
+ if isinstance(body.get("temperature"), (int, float)):
+ options["temperature"] = body["temperature"]
+ if isinstance(body.get("top_p"), (int, float)):
+ options["top_p"] = body["top_p"]
+ if body.get("stop") is not None:
+ options["stop"] = body["stop"]
+ effort = body.get("reasoning_effort")
+ if effort == "none":
+ native["think"] = False
+ elif isinstance(effort, str) and effort:
+ native["think"] = effort
+ return native
+
+
+def _native_message(msg: dict[str, Any], messages: list[Any]) -> dict[str, Any]:
+ text, images = _content_parts(msg.get("content"))
+ out: dict[str, Any] = {"role": msg.get("role") or "user", "content": text}
+ thinking = msg.get("reasoning") or msg.get("reasoning_content")
+ if isinstance(thinking, str) and thinking:
+ out["thinking"] = thinking
+ calls = _native_tool_calls(msg.get("tool_calls"))
+ if calls:
+ out["tool_calls"] = calls
+ if images:
+ out["images"] = images
+ if out["role"] == "tool":
+ tool_call_id = msg.get("tool_call_id") or ""
+ if tool_call_id:
+ out["tool_call_id"] = tool_call_id
+ name = msg.get("name") or _tool_name(messages, tool_call_id)
+ if name:
+ out["tool_name"] = name
+ return out
+
+
+def _content_parts(content: Any) -> tuple[str, list[str]]:
+ if isinstance(content, str):
+ return content, []
+ if not isinstance(content, list):
+ return ("" if content is None else str(content)), []
+ texts: list[str] = []
+ images: list[str] = []
+ for part in content:
+ if not isinstance(part, dict):
+ continue
+ if part.get("type") == "text" and isinstance(part.get("text"), str):
+ texts.append(part["text"])
+ elif part.get("type") == "image_url":
+ image = _image_b64(part.get("image_url"))
+ if image:
+ images.append(image)
+ return "\n".join(texts), images
+
+
+def _image_b64(image_url: Any) -> str:
+ url = image_url.get("url") if isinstance(image_url, dict) else image_url
+ if not isinstance(url, str) or not url:
+ return ""
+ if url.startswith("data:") and "," in url:
+ return url.split(",", 1)[1]
+ return url
+
+
+def _native_tool_calls(raw: Any) -> list[dict[str, Any]]:
+ calls: list[dict[str, Any]] = []
+ for index, call in enumerate(raw or []):
+ if not isinstance(call, dict):
+ continue
+ function = call.get("function") or {}
+ arguments = function.get("arguments")
+ if isinstance(arguments, str):
+ try:
+ arguments = json.loads(arguments) if arguments else {}
+ except json.JSONDecodeError:
+ arguments = {"_raw": arguments}
+ if not isinstance(arguments, dict):
+ arguments = {}
+ calls.append(
+ {
+ "id": call.get("id") or f"call_{index}",
+ "function": {
+ "index": index,
+ "name": function.get("name") or "",
+ "arguments": arguments,
+ },
+ }
+ )
+ return calls
+
+
+def _tool_name(messages: list[Any], tool_call_id: str) -> str:
+ if not tool_call_id:
+ return ""
+ for msg in reversed(messages):
+ if not isinstance(msg, dict):
+ continue
+ for call in msg.get("tool_calls") or []:
+ if isinstance(call, dict) and call.get("id") == tool_call_id:
+ return str((call.get("function") or {}).get("name") or "")
+ return ""
+
+
+def to_openai_completion(native: dict[str, Any]) -> dict[str, Any]:
+ """One native chat response → an OpenAI chat.completion object."""
+ message = native.get("message") or {}
+ tool_calls = _openai_tool_calls(message.get("tool_calls"))
+ finish = _finish_reason(native.get("done_reason") or "stop", bool(tool_calls))
+ prompt, completion = _usage_counts(native)
+ body: dict[str, Any] = {
+ "id": "chatcmpl-ollama",
+ "object": "chat.completion",
+ "created": int(time.time()),
+ "model": native.get("model") or "",
+ "choices": [
+ {
+ "index": 0,
+ "message": {
+ "role": message.get("role") or "assistant",
+ "content": message.get("content") or "",
+ "reasoning": message.get("thinking") or None,
+ "tool_calls": tool_calls or None,
+ },
+ "finish_reason": finish,
+ }
+ ],
+ "usage": {
+ "prompt_tokens": prompt,
+ "completion_tokens": completion,
+ "total_tokens": prompt + completion,
+ },
+ }
+ return body
+
+
+def to_openai_stream_events(
+ native: dict[str, Any], *, include_role: bool, saw_tools: bool
+) -> list[dict[str, Any]]:
+ """One native stream object → zero or more OpenAI chat.completion.chunk objects."""
+ message = native.get("message") or {}
+ content = message.get("content") or ""
+ thinking = message.get("thinking") or ""
+ tool_calls = _openai_tool_calls(message.get("tool_calls"))
+ events: list[dict[str, Any]] = []
+ if content or thinking or tool_calls or include_role:
+ delta: dict[str, Any] = {}
+ if include_role:
+ delta["role"] = "assistant"
+ if thinking:
+ delta["reasoning"] = thinking
+ if content:
+ delta["content"] = content
+ if tool_calls:
+ delta["tool_calls"] = tool_calls
+ events.append(_chunk(native, delta))
+ if native.get("done"):
+ prompt, completion = _usage_counts(native)
+ events.append(
+ {
+ **_chunk(
+ native,
+ {},
+ finish_reason=_finish_reason(
+ native.get("done_reason") or "stop", saw_tools or bool(tool_calls)
+ ),
+ ),
+ "usage": {
+ "prompt_tokens": prompt,
+ "completion_tokens": completion,
+ "total_tokens": prompt + completion,
+ },
+ }
+ )
+ return events
+
+
+def _chunk(
+ native: dict[str, Any], delta: dict[str, Any], finish_reason: Optional[str] = None
+) -> dict[str, Any]:
+ return {
+ "id": "chatcmpl-ollama",
+ "object": "chat.completion.chunk",
+ "created": int(time.time()),
+ "model": native.get("model") or "",
+ "choices": [{"index": 0, "delta": delta, "finish_reason": finish_reason}],
+ }
+
+
+def _openai_tool_calls(raw: Any) -> list[dict[str, Any]]:
+ calls: list[dict[str, Any]] = []
+ for index, call in enumerate(raw or []):
+ if not isinstance(call, dict):
+ continue
+ function = call.get("function") or {}
+ arguments = function.get("arguments")
+ if not isinstance(arguments, str):
+ arguments = json.dumps(arguments if isinstance(arguments, dict) else {})
+ calls.append(
+ {
+ "index": function.get("index", index),
+ "id": call.get("id") or f"call_{index}",
+ "type": "function",
+ "function": {"name": function.get("name") or "", "arguments": arguments},
+ }
+ )
+ return calls
+
+
+def _finish_reason(reason: str, has_tools: bool) -> str:
+ if reason == "stop" and has_tools:
+ return "tool_calls"
+ return reason or "stop"
+
+
+def _usage_counts(native: dict[str, Any]) -> tuple[int, int]:
+ return int(native.get("prompt_eval_count") or 0), int(native.get("eval_count") or 0)
diff --git a/coworker/providers/openai_provider.py b/coworker/providers/openai_provider.py
index 72636ff1..632eca00 100644
--- a/coworker/providers/openai_provider.py
+++ b/coworker/providers/openai_provider.py
@@ -223,6 +223,7 @@ class OpenAIProvider(ProviderClient):
api_key: Optional[str] = None,
base_url: Optional[str] = None,
secrets: Any = None,
+ http_client: Any = None,
):
# The SDK client is built lazily on first use, NOT at construction. This lets an engine
# be assembled before any key exists — the desktop app lets you enter the key in Settings
@@ -233,10 +234,14 @@ class OpenAIProvider(ProviderClient):
# `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,
# 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
+ # a transport that rewrites chat calls onto the native API (see ollama_context.py).
self._client = client
self._api_key = api_key
self._base_url = base_url
self._secrets = secrets
+ self._http_client = http_client
self.default_model = default_model
def _ensure_client(self) -> Any:
@@ -253,6 +258,8 @@ class OpenAIProvider(ProviderClient):
kwargs: dict[str, Any] = {"api_key": key}
if self._base_url:
kwargs["base_url"] = self._base_url
+ if self._http_client is not None:
+ kwargs["http_client"] = self._http_client
self._client = OpenAI(**kwargs)
return self._client
diff --git a/coworker/providers/registry.py b/coworker/providers/registry.py
index 5e2d2954..6a9f6895 100644
--- a/coworker/providers/registry.py
+++ b/coworker/providers/registry.py
@@ -197,8 +197,12 @@ def _build_vertex(profile: dict[str, Any], secrets: Any) -> ProviderClient:
def _build_ollama(profile: dict[str, Any], secrets: Any) -> ProviderClient:
# Ollama's OpenAI-compatible endpoint ignores the key but the SDK requires a non-empty
# string, so we pass a placeholder. `base_url` comes from the stored profile (or the default).
+ # Chat calls are rewritten onto the native API so we can set num_ctx — the /v1 handler
+ # cannot, and its 4,096-token default drops the Cowork prompt (see ollama_context.py).
+ from .ollama_context import ollama_http_client
+
base_url = _normalize_ollama_url((profile or {}).get("base_url"))
- return OpenAIProvider(api_key="ollama", base_url=base_url)
+ return OpenAIProvider(api_key="ollama", base_url=base_url, http_client=ollama_http_client())
def _openai_compat(vendor: str, default_base_url: str, env_key: Optional[str] = None):
diff --git a/tests/test_ollama_context.py b/tests/test_ollama_context.py
new file mode 100644
index 00000000..0c53b3bd
--- /dev/null
+++ b/tests/test_ollama_context.py
@@ -0,0 +1,306 @@
+"""Ollama chat calls request a context window the Cowork prompt actually fits in."""
+
+from __future__ import annotations
+
+import json
+from types import SimpleNamespace
+
+import httpx
+import pytest
+
+from coworker.compaction import is_context_overflow
+from coworker.engine import TurnEngine
+from coworker.providers import ollama_context
+from coworker.providers.ollama_context import (
+ DEFAULT_OLLAMA_NUM_CTX,
+ OllamaContextTransport,
+ context_length_from_show,
+ context_window_for,
+ to_native_chat,
+)
+from coworker.providers.openai_provider import OpenAIProvider
+
+GEMMA_SHOW = {
+ "parameters": "temperature 1\ntop_p 0.95",
+ "model_info": {"general.architecture": "gemma4", "gemma4.context_length": 262144},
+}
+
+
[email protected](autouse=True)
+def _fresh_resolution(monkeypatch):
+ monkeypatch.delenv("OPENWORKER_OLLAMA_NUM_CTX", raising=False)
+ monkeypatch.setattr(ollama_context, "_resolved", {})
+
+
+def test_native_chat_sets_num_ctx_and_refuses_to_shift():
+ native = to_native_chat(
+ {
+ "model": "gemma4:e4b",
+ "messages": [
+ {"role": "system", "content": "You are Cowork."},
+ {
+ "role": "user",
+ "content": "Analyze the files in this folder and summarize what matters.",
+ },
+ ],
+ "tools": [{"type": "function", "function": {"name": "list_files"}}],
+ "max_tokens": 32000,
+ "stream": True,
+ },
+ num_ctx=32768,
+ )
+ assert native["options"]["num_ctx"] == 32768
+ assert native["options"]["num_predict"] == 32000
+ assert native["shift"] is False
+ assert native["stream"] is True
+ assert native["tools"][0]["function"]["name"] == "list_files"
+ assert native["messages"][0]["content"] == "You are Cowork."
+
+
+def test_tool_history_round_trips_with_a_name():
+ messages = [
+ {
+ "role": "assistant",
+ "content": "",
+ "reasoning": "listing the folder",
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {"name": "list_files", "arguments": '{"path": "."}'},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": "call_1", "content": "README.md"},
+ ]
+ native = to_native_chat({"model": "m", "messages": messages}, num_ctx=32768)
+ assistant, tool = native["messages"]
+ assert assistant["thinking"] == "listing the folder"
+ assert assistant["tool_calls"][0]["function"]["arguments"] == {"path": "."}
+ assert tool["tool_name"] == "list_files"
+ assert tool["content"] == "README.md"
+
+
+class _Inner(httpx.BaseTransport):
+ """Fake Ollama: one canned `/api/chat` response, optional `/api/show` per path."""
+
+ def __init__(
+ self, response: httpx.Response, show: httpx.Response | None = None
+ ) -> None:
+ self.response = response
+ self.show = show
+ self.requests: list[httpx.Request] = []
+
+ def handle_request(self, request: httpx.Request) -> httpx.Response:
+ self.requests.append(request)
+ if request.url.path.endswith("/api/show"):
+ if self.show is None:
+ raise httpx.ConnectError("no show endpoint", request=request)
+ return self.show
+ return self.response
+
+ def chat_requests(self) -> list[httpx.Request]:
+ 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 _done(model: str = "gemma4:e4b") -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "model": model,
+ "message": {"role": "assistant", "content": "ok"},
+ "done": True,
+ "done_reason": "stop",
+ },
+ )
+
+
+def test_context_length_prefers_modelfile_num_ctx_then_trained_length():
+ assert context_length_from_show(GEMMA_SHOW) == 262144
+ pinned = {**GEMMA_SHOW, "parameters": "num_ctx 16384\ntop_p 0.9"}
+ assert context_length_from_show(pinned) == 16384
+ # Unknown architecture key still finds the one *.context_length entry.
+ assert context_length_from_show({"model_info": {"qwen3.context_length": 40960}}) == 40960
+ assert context_length_from_show({"model_info": {"general.architecture": "x"}}) is None
+ assert context_length_from_show({}) is None
+
+
+def test_transport_reads_the_trained_window_from_api_show_once_per_model():
+ inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))
+ transport = OllamaContextTransport(inner=inner)
+ 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) == 1
+ assert json.loads(shows[0].content) == {"model": "gemma4:e4b"}
+ for chat in inner.chat_requests():
+ assert json.loads(chat.content)["options"]["num_ctx"] == 262144
+ # The engine sizes compaction against the window actually in use.
+ assert context_window_for("ollama:gemma4:e4b") == 262144
+
+
+def test_env_override_caps_the_trained_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())
+ assert json.loads(inner.chat_requests()[0].content)["options"]["num_ctx"] == 32768
+ assert 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"))
+ 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():
+ inner = _Inner(_done(), show=None)
+ transport = OllamaContextTransport(inner=inner)
+ 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)
+ transport.handle_request(_chat_request())
+ assert json.loads(inner.chat_requests()[1].content)["options"]["num_ctx"] == 262144
+
+
+def test_native_endpoints_keep_a_reverse_proxy_prefix():
+ inner = _Inner(_done(), show=httpx.Response(200, json=GEMMA_SHOW))
+ OllamaContextTransport(inner=inner).handle_request(
+ _chat_request(base="http://gateway.local/ollama/v1")
+ )
+ paths = [r.url.path for r in inner.requests]
+ assert paths == ["/ollama/api/show", "/ollama/api/chat"]
+
+
+def test_transport_rewrites_chat_onto_native_api_and_back():
+ native = {
+ "model": "gemma4:e4b",
+ "message": {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "id": "call_abc",
+ "function": {"name": "list_files", "arguments": {"path": "."}},
+ }
+ ],
+ },
+ "done": True,
+ "done_reason": "stop",
+ "prompt_eval_count": 7123,
+ "eval_count": 40,
+ }
+ inner = _Inner(httpx.Response(200, json=native))
+ transport = OllamaContextTransport(num_ctx=DEFAULT_OLLAMA_NUM_CTX, inner=inner)
+ request = httpx.Request(
+ "POST",
+ "http://127.0.0.1:11434/v1/chat/completions",
+ json={
+ "model": "gemma4:e4b",
+ "messages": [{"role": "user", "content": "list the files"}],
+ "max_tokens": 32,
+ },
+ )
+ response = transport.handle_request(request)
+ sent = json.loads(inner.requests[0].content)
+ assert inner.requests[0].url.path == "/api/chat"
+ assert sent["options"]["num_ctx"] == DEFAULT_OLLAMA_NUM_CTX
+ assert sent["shift"] is False
+ parsed = response.json()
+ assert parsed["usage"]["prompt_tokens"] == 7123
+ assert parsed["choices"][0]["finish_reason"] == "tool_calls"
+ call = parsed["choices"][0]["message"]["tool_calls"][0]
+ assert call["function"]["name"] == "list_files"
+ assert json.loads(call["function"]["arguments"]) == {"path": "."}
+
+
+def test_provider_stream_sees_the_full_prompt_usage():
+ lines = [
+ json.dumps(
+ {
+ "model": "gemma4:e4b",
+ "message": {"role": "assistant", "content": "README.md matters."},
+ "done": False,
+ }
+ ),
+ json.dumps(
+ {
+ "model": "gemma4:e4b",
+ "message": {"role": "assistant", "content": ""},
+ "done": True,
+ "done_reason": "stop",
+ "prompt_eval_count": 6900,
+ "eval_count": 12,
+ }
+ ),
+ ]
+ inner = _Inner(
+ httpx.Response(200, stream=httpx.ByteStream("\n".join(lines).encode()))
+ )
+ transport = OllamaContextTransport(num_ctx=32768, inner=inner)
+ provider = OpenAIProvider(
+ api_key="ollama",
+ base_url="http://127.0.0.1:11434/v1",
+ http_client=httpx.Client(transport=transport),
+ )
+ turn = None
+ text = []
+ for chunk in provider.stream(
+ model="gemma4:e4b",
+ messages=[{"role": "user", "content": "summarize the folder"}],
+ ):
+ if chunk.text_delta:
+ text.append(chunk.text_delta)
+ if chunk.turn is not None:
+ turn = chunk.turn
+ assert "".join(text) == "README.md matters."
+ assert turn is not None
+ assert turn.usage is not None
+ assert turn.usage.input == 6900
+ sent = json.loads(inner.requests[0].content)
+ assert sent["options"]["num_ctx"] == 32768
+
+
+def test_registry_builds_ollama_with_the_context_transport():
+ from coworker.providers.registry import build_provider_client
+
+ provider = build_provider_client("ollama", {"base_url": "http://127.0.0.1:11434"}, None)
+ client = provider._ensure_client()
+ transport = client._client._transport
+ assert isinstance(transport, OllamaContextTransport)
+ assert transport.num_ctx is None # resolved per model from /api/show
+
+
+def test_ollama_compaction_window_matches_the_requested_context():
+ engine = SimpleNamespace(
+ model="ollama:gemma4:e4b",
+ compaction_settings=None,
+ _warned_context_fallback=False,
+ )
+ # Before the first chat call resolves the model: the default, no 128k-guess warning.
+ cfg = TurnEngine._compaction_config(engine)
+ assert cfg["context_window"] == DEFAULT_OLLAMA_NUM_CTX
+ assert engine._warned_context_fallback is False
+ assert 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
+
+
+def test_ollama_context_overflow_is_recognized():
+ exc = RuntimeError(
+ "the prompt is longer than the context length currently available to the model"
+ )
+ assert is_context_overflow(exc)