Repository navigation
Description
Overview
Three bugs caused Ollama to silently truncate the system prompt at 4096 tokens, producing a 1-token empty response with no tool calls. None of the existing integration tests caught this because test_ollama_inference_with_tools explicitly treats an empty response as a valid outcome.
Problem Statement
What broke: Sending a chat message in the app produced no response and logged Agent returned empty response with no tool calls with prompt_tokens=4095 and completion_tokens=1.
Root cause — three compounding bugs in packages/agent/src/local_agent/ollama_inference.rs:
-
num_ctxis never included inOllamaOptions— Ollama defaults to 4096 regardless of the model's actual context window. The production system prompt is ~1,900 tokens and 14 tool definitions push total prompt tokens to ~4,095, hitting this hard limit. -
OllamaShowResponsedeserializes fieldmodelinfo(camelCase) but the Ollama/api/showresponse usesmodel_info(snake_case) — serde always producesNone, context detection always falls through to the 4096 fallback. -
Key mismatch: even if serde were correct, the code looks for
llm.context_lengthbut Gemma 4 reportsgemma4.context_length. The key is model-family-specific.
Why tests didn't catch it:
test_ollama_inference_with_tools (line 186, packages/agent/tests/ollama_integration.rs) explicitly accepts empty responses:
// Model may or may not call the tool — both are valid responses.
// Not all models support tool calling; some return no chunks in that case.
// Verify only that no Error chunks were emitted.The test uses a tiny 2-message prompt (system + user) well under 4096 tokens, so truncation doesn't happen in that test. And even if it did, the assertion only checks for StreamingChunk::Error chunks — an empty response passes silently.
There is no test that exercises LocalAgentService::send_message() with a realistic production prompt (full system prompt + 14 tools + chat history) against a real Ollama, which is exactly the scenario that broke.
Proposed Solution
Fix the three bugs
- Add
num_ctx: Option<u32>toOllamaOptions; set it to the model's context window (from/api/show) when tools are present, defaulting to 32768 if detection fails - Fix
OllamaShowResponsefield name: renamemodelinfo→model_info(add#[serde(rename = "model_info")]or use the snake_case field name directly) - Fix the context key lookup: try model-family-specific keys (
gemma4.context_length,llama.context_length, etc.) before falling back; or iterate all keys looking for one ending in.context_length
Strengthen the integration tests
test_ollama_inference_with_tools: assert the response is non-empty — either at least oneToolCallStartchunk OR at least one non-emptyTokenchunk. An empty response is an inference failure, not a valid outcome for a tool-capable model.- Add
test_ollama_inference_with_production_prompt: use a realistic system prompt (~7KB) and all 14 production tools againstLocalAgentService::send_message(), assert a non-empty response. This is the scenario that was broken in production. - Add
test_ollama_model_info_context_window_detection: assertmodel_info()returns acontext_window > 4096for the available model (Gemma 4 has 131072). This pins the serde field name and key lookup correctness.
Acceptance Criteria
-
num_ctxis set on all Ollama requests where tools are present -
OllamaShowResponsecorrectly deserializesmodel_infofrom the Ollama API response - Context window detection correctly reads
gemma4.context_length(and equivalent keys for other model families) -
test_ollama_inference_with_toolsasserts a non-empty response (tool call or text) - New test
test_ollama_inference_with_production_promptpasses against gemma4:e4b with a full system prompt and 14 tools - New test
test_ollama_model_info_context_window_detectionassertscontext_window > 4096 - All tests skip gracefully when Ollama is not running
-
bun run test:allpasses with no new failures - Code passes
bun run quality:fix
Technical Specifications
Files to Modify
-
ollama_inference.rs:packages/agent/src/local_agent/ollama_inference.rsOllamaOptionsstruct (line 71): addnum_ctx: Option<u32>OllamaShowResponsestruct (line 107): fix field namemodelinfo→model_infomodel_info()method (line 304): fix context key lookup chaingenerate()method (line 113): populatenum_ctxfrom model info or safe default
-
ollama_integration.rs:packages/agent/tests/ollama_integration.rstest_ollama_inference_with_tools(line 186): strengthen assertion- Add
test_ollama_inference_with_production_prompt - Add
test_ollama_model_info_context_window_detection
Context Window Detection Fix
Suggested key lookup order for model_info():
let context_window = ["llm.context_length", "gemma4.context_length", "llama.context_length", "qwen2.context_length"]
.iter()
.find_map(|key| modelinfo.get(*key).and_then(|v| v.as_u64()))
.unwrap_or(32_768) as u32;Or more robustly: iterate modelinfo keys and return the first value where the key ends with .context_length.
Safe Default
If /api/show fails or the key is not found, default num_ctx to 32768 (not 4096). The production system prompt + 14 tools fits within 32K for any current model.
Activity
malibio commentedon Jun 22, 2026
Frontend: 140 passed (baseline). Rust unit: 248 passed. test:all: 957 passed. Implementing fixes now.
fix(agent): fix Ollama context-window truncation and strengthen integ…
fix(agent): fix Ollama context-window truncation and strengthen integ…
test(agent): strengthen Ollama integration assertions and add product…
test(agent): strengthen Ollama integration assertions and add product…