The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(llm): integrate capability-aware Ollama tools from PR 3275
haelyra544601912aec
diff --cc src/llm/providers/ollama.pyindex 6ee4aa8d4,47f5f1cc6..0787c250e--- a/src/llm/providers/ollama.py+++ b/src/llm/providers/ollama.py@@@ -15,12 -19,35 +19,36 @@@ from llm.core.interface importfrom llm.core.types import (LLMInput,LLMOutput,+ Message,ModelInfo,ProviderType,+ Role,ToolCall,)+from llm.providers.reasoning import prefilled_reasoning_from_env, strip_reasoning+ logger = logging.getLogger(__name__)+++ def _messages_payload(messages: list[Message]) -> list[dict[str, Any]]:+ """Serialize messages, naming the tool each tool result answers.++ Ollama correlates a tool result with its call through ``tool_name``; ids+ are optional on its side, so the name is what keeps several results apart.+ """+ names: dict[str, str] = {}+ payload = []+ for msg in messages:+ item = msg.to_dict()+ for tc in msg.tool_calls or []:+ names[tc.id] = tc.name+ if msg.role == Role.TOOL:+ tool_name = msg.name or names.get(msg.tool_call_id or "")+ if tool_name:+ item["tool_name"] = tool_name+ payload.append(item)+ return payload+class OllamaProvider(LLMProvider):provider_type = ProviderType.OLLAMA@@@ -66,11 -85,38 +94,41 @@@context_window=16384,),]+ self._tool_support: dict[str, bool] = {}- def generate(self, input: LLMInput) -> LLMOutput:- import json- import urllib.request+ def model_supports_tools(self, model: str) -> bool:+ """Whether ``model`` declares tool support.+ Catalogued models answer from :attr:`ModelInfo.supports_tools`. Any other+ model is looked up once through ``/api/show``, whose ``capabilities``+ list is what the installed model itself declares. Only an answered lookup+ is cached: a failed one counts as no tool support for that request and+ is retried on the next.+ """+ for info in self._models:+ if info.name == model:+ return info.supports_tools+ if model in self._tool_support:+ return self._tool_support[model]+ try:+ req = urllib.request.Request(+ f"{self.base_url}/api/show",+ data=json.dumps({"model": model}).encode("utf-8"),+ headers={"Content-Type": "application/json"},+ )+ with urllib.request.urlopen(req, timeout=10) as response:+ capabilities = json.loads(response.read().decode("utf-8")).get("capabilities") or []+ except Exception as e:+ logger.warning("Could not read capabilities for Ollama model '%s': %s", model, type(e).__name__)+ return False++ if not isinstance(capabilities, list) or not all(isinstance(item, str) for item in capabilities):++ logger.warning("Invalid capabilities for Ollama model '%s'; sending no tools", model)++ return False+ supports_tools = "tools" in capabilities+ self._tool_support = {**self._tool_support, model: supports_tools}+ return supports_tools++ def generate(self, input: LLMInput) -> LLMOutput:try:url = f"{self.base_url}/api/chat"model = input.model or self.default_modeldiff --cc tests/test_provider_tools.pyindex 7155953f7,accdef762..c95da7c2a--- a/tests/test_provider_tools.py+++ b/tests/test_provider_tools.py@@@ -1,9 -1,9 +1,10 @@@+ import asyncioimport jsonimport urllib.request+from collections.abc import Callablefrom io import BytesIOfrom types import SimpleNamespace-from typing import Any+from typing import Any, castimport pytest@@@ -275,3 -366,3 +458,15 @@@ def test_claude_provider_serializes_too"input_schema": {"type": "object", "properties": {"query": {"type": "string"}}},}]++++[email protected]('capabilities', ['tools', {'tools': True}, ['tools', 1]])++def test_ollama_malformed_capability_declaration_is_not_authority(monkeypatch, capabilities):++ server = _FakeOllama(monkeypatch, [_ollama_answer('ok'), _ollama_answer('ok')], capabilities=capabilities)++ provider = OllamaProvider(default_model='custom')++ request = LLMInput(messages=[Message(role=Role.USER, content='hi')], tools=[_tool()])++ provider.generate(request)++ server.capabilities = ['completion', 'tools']++ provider.generate(request)++ assert ['tools' in chat for chat in server.chats] == [False, True]++ assert server.shows == ['custom', 'custom']