Repository navigation
fix(agent): fix Ollama context-window truncation and strengthen integration tests - #1447
Merged
Merged
Conversation
…ration tests (closes #1443) Three compounding bugs caused Ollama to silently truncate at 4096 tokens: 1. `OllamaOptions` was missing `num_ctx` — Ollama defaulted to 4096 regardless of model capacity. Now fetches context window from /api/show and sets num_ctx on all tool-bearing requests. 2. `OllamaShowResponse` deserialised field `modelinfo` but the Ollama API returns `model_info` — fixed with `#[serde(rename = "model_info")]`. 3. Context key lookup only tried `llm.context_length` but Gemma 4 uses `gemma4.context_length`. Fixed by trying a list of known family-specific keys, then falling back to any key ending in `.context_length`. Default fallback changed from 4096 to 32768. Integration test changes: - `test_ollama_inference_with_tools`: asserts non-empty response (tool call OR text token); empty responses are now treated as inference failures, not valid outcomes. - New `test_ollama_model_info_context_window_detection`: asserts context_window > 4096, pinning the serde field name and key lookup correctness. - New `test_ollama_inference_with_production_prompt`: sends a realistic production system prompt (~1,900 tokens) plus all 14 production tools against LocalAgentService, asserts non-empty response and completion_tokens > 1 (guards against silent truncation). Co-Authored-By: Claude <[email protected]>
…inline helper - Add OnceCell<u32> to OllamaInferenceEngine to cache the context window fetched from /api/show; eliminates the per-request /api/show round-trip on multi-turn conversations (was one HTTP call per generate() invocation with tools). - Remove redundant #[serde(rename = "model_info")] — serde's default snake_case mapping already handles this; the attribute implied the JSON key differs from the Rust field name, which is misleading. - Inline completion_tokens_indicate_real_response (was a one-liner called once); added an explanatory comment so the > 1 threshold is self-documenting. Skipped: production tool definition helper — the two call sites serve genuinely different purposes (full production schemas vs. minimal stubs) and a shared helper would obscure that distinction without reducing maintenance burden. Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1443
Summary
num_ctxtoOllamaOptionsand sets it from model info on every tool-bearing request, eliminating the 4096-token default context truncationOllamaShowResponsefield name (modelinfo→model_info) so serde correctly deserializes the Ollama/api/showresponsegemma4.context_length,llama.context_length, etc.) before falling back; default fallback changed from 4096 to 32768OnceCell<u32>to avoid a per-request/api/showround-triptest_ollama_inference_with_toolsto assert non-empty responsetest_ollama_model_info_context_window_detection(pins serde field name + key lookup)test_ollama_inference_with_production_prompt(full system prompt + 14 tools, guards against silent truncation)Test plan
bun run test:allpasses (140 frontend + 957 Rust unit tests, no new failures)🤖 Generated with Claude Code